Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SCNLookAtConstraint, specify how the node looks at the target node?

Tags:

scenekit

I have a text node. I am telling the node to always look at the camera node like so:

let lookAt = SCNLookAtConstraint(target: cameraNode)
textNode.constraints = [lookAt]

Before I apply that constraint the text looks like this: (see image) enter image description here

If I apply that constraint, the node look flipped like in the image (see image). So, the text does always keep that orientation and correctly look at the camera node, so the constraint is working.

enter image description here

I am trying to understand how the node "looks" at the target node though. I would obviously want the text to simply stay like shown in the first image as the camera moves around.

I tried to apply a rotation to the text to correct the behavior I get when applying the constraint but didn't work.

So, how can I get the text to have look at another node but not be flipped like in the second image?

like image 678
zumzum Avatar asked Jan 19 '15 18:01

zumzum


3 Answers

The documentation for SCNLookAtConstraint notes:

When SceneKit 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.

But SCNText geometry is created facing forward along its positive z-axis, so your constraint system needs to account for that in some way. I advise using SCNBillboardConstraint, which is designed for this kind of case. Its documentation says:

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.

With this, you shouldn't need to reparent the node.

like image 102
Erik Foss Avatar answered May 11 '23 00:05

Erik Foss


The "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."

textNode.constraints = [SCNBillboardConstraint()]
like image 40
Aaron Halvorsen Avatar answered May 11 '23 01:05

Aaron Halvorsen


You can also modify lookAt constraint by specifying the axis through which it will "look" via the localFront property:

let lookAtConstraint = SCNLookAtConstraint(target: distantNode)
lookAtConstraint.localFront = SCNVector3Make(0, 1, 0)
node.constraints = [lookAtConstraint]

The above code will orient the node to distantNode via it's Y-axis.

like image 28
ejovrh Avatar answered May 11 '23 00:05

ejovrh