问题
I want to apply the rotation of the ARCamera to a 3D node so that the node will always face the camera. How can I implement this code in Objective-C?
回答1:
You can get an SCNNode to face the ARCamera
by using an SCNBillboardConstraint
:
An SCNBillboardConstraint object automatically adjusts a node’s orientation so that its local z-axis always points toward the pointOfView node currently being used to render the scene. For example, you can use a billboard constraint to efficiently render parts of a scene using two-dimensional sprite images instead of three-dimensional geometry—by mapping sprites onto planes affected by a billboard constraint, the sprites maintain their orientation with respect to the viewer. To attach constraints to an SCNNode object, use its constraints property.
Objective C:
SCNBillboardConstraint *lookAtConstraint = [SCNBillboardConstraint billboardConstraint];
node.constraints = @[lookAtConstraint];
Swift:
let lookAtConstraint = SCNBillboardConstraint()
node.constraints = [lookAtConstraint]
If you want an SCNNode to face another node then you can use an SCNLookAtConstraint
:
For example, you can use a look-at constraint to ensure that a camera or spotlight always follows the movement of a game character. To attach constraints to an SCNNode object, use its constraints property. A node points in the direction of the negative z-axis of its local coordinate system. This axis defines the view direction for nodes containing cameras and the lighting direction for nodes containing spotlights or directional lights, as well as the orientation of the node’s geometry and child nodes. When Scene Kit evaluates a look-at constraint, it updates the constrained node’s transform property so that the node’s negative z-axis points toward the constraint’s target node.
Objective C:
SCNLookAtConstraint * lookAtNode = [SCNLookAtConstraint lookAtConstraintWithTarget:secondNode];
fistNode.constraints = @[lookAtNode];
Swift:
let lookAtConstraint = SCNLookAtConstraint(target: secondNode)
firstNode.constraints = [lookAtConstraint]
来源:https://stackoverflow.com/questions/49151332/apply-arcamera-rotation-transform-to-node-arkit