Swift - How to change the Pivot of a SCNNode object

久未见 提交于 2019-12-04 11:52:13

问题


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?


回答1:


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)

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.

To change the anchor point, set the pivot to a translation transform:

boxNode.pivot = SCNMatrix4MakeTranslation(0.5, 0.5, 0.5)

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.

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.




回答2:


On the pivot topic:

Just in case you do not have dimensions for your geometry/node something like this might help (especially for SCNText).

var minVec = SCNVector3Zero
var maxVec = SCNVector3Zero

if node.getBoundingBoxMin(&minVec, max: &maxVec) {

    let bound = SCNVector3(x: maxVec.x - minVec.x,
                           y: maxVec.y - minVec.y,
                           z: maxVec.z - minVec.z)

    node.pivot = SCNMatrix4MakeTranslation(bound.x / 2, 
                                           bound.y / 2, 
                                           bound.z / 2)
}


来源:https://stackoverflow.com/questions/24734200/swift-how-to-change-the-pivot-of-a-scnnode-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!