Swift-如何更改SCNNode对象的枢轴 [英] Swift - How to change the Pivot of a SCNNode object
问题描述
我已经使用SCNNode对象已有一段时间了,而迷失了Pivot.如何更改SCNNode的枢轴(SCNBox为条形)并将枢轴放置在条形的边缘之一上?
I've been playing with the SCNNode object for a while now and I'm lost with the Pivot. How can I change the pivot of a SCNNode (SCNBox as a bar) and place the pivot on one of the edge of the bar?
推荐答案
A node's pivot
is a transformation matrix, the inverse of which is applied to the node before its transform
property takes effect. For example, take a look at this bit from the default SceneKit Game template in Xcode:
let boxNode = SCNNode()
boxNode.geometry = SCNBox(width: 1, height: 1, length: 1, chamferRadius: 0.02)
如果设置boxNode
的position
,则该点对应于立方体的中心,并且如果旋转它(如模板在动画中所做的那样),它将绕其中心旋转.
If you set the boxNode
's position
, that point corresponds to the center of the cube, and if you rotate it (as the template does in an animation), it spins around its center.
要更改锚点,请将pivot
设置为平移变换:
To change the anchor point, set the pivot
to a translation transform:
boxNode.pivot = SCNMatrix4MakeTranslation(0.5, 0.5, 0.5)
现在,当您设置position
时,该点对应于多维数据集的右上前角,并且当您旋转多维数据集时,它围绕该角旋转.
Now, when you set the position
that point corresponds to the top-right-front corner of the cube, and when you rotate the cube it spins around that corner.
更一般地,枢轴相对于节点自己的变换来变换节点的内容.假设您要模拟地球自转轴的进动.您可以通过创建两个动画来做到这一点:一个动画使pivot
动画以使节点绕其自身的Y轴旋转,另一个动画给rotation
动画以使该轴相对于包含该节点的空间移动.
More generally, a pivot transforms the contents of a node relative to the node's own transform. Suppose you wanted to model the precession of the Earth's axis of rotation. You could do this by creating two animations: one that animates pivot
to spin the node around its own Y axis, and another that animates rotation
to move that axis relative to the space containing the node.
这篇关于Swift-如何更改SCNNode对象的枢轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!