Transform and Rotate in Scenekit

不想你离开。 提交于 2019-12-17 21:35:07

问题


I have a SCN Node that I have Transformed to a desired Rotation and Angle.

I now want to rotate it infinitely on its new x-axis, but I can't seem to get it to rotate 360 degrees, it just shifts slightly.

let spin = CABasicAnimation(keyPath: "rotation")
spin.fromValue = NSValue(SCNVector4: SCNVector4(x: item.rotation.x, y: item.rotation.y, z: item.rotation.z, w: item.rotation.w))
spin.toValue = NSValue(SCNVector4: SCNVector4(x: Float(2*M_PI), y: item.rotation.y, z: item.rotation.z, w: item.rotation.w))
spin.duration = 3
spin.repeatCount = .infinity
item.addAnimation(spin, forKey: "spin around")

Note I found the rotation code from an earlier question, and it works as expected, but not if I want to rotate on the transformed node.

EDIT: The Transform Code:

self.item.transform = SCNMatrix4Mult(SCNMatrix4Mult(SCNMatrix4MakeScale(1.1, 1.1, 1.1), SCNMatrix4MakeRotation(2.2, 228.0, 120.0, 55.2)), SCNMatrix4MakeTranslation(-2.5, 12, 6))
self.item.pivot = SCNMatrix4MakeTranslation(0, 0, 4)
self.item.transform = SCNMatrix4Mult(SCNMatrix4MakeTranslation(0, 0, 4), item.transform)

回答1:


In answer to your comment and possibly your issue. Once you have your .dae file installed, you can access the nodes as in the following example:

    // "rhodopsin" is the namne of a .dae file
    guard let rho = SCNScene(named: "rhodopsin") else {
        print("Couldn't find molecule in dictionary  (rhodopsin)")
        return  }

    let chain = rho.rootNode.childNodeWithName("chain", recursively: true)!
    let hetero = rho.rootNode.childNodeWithName("hetero", recursively: true)!

If you need to find the names of these nodes, open the .dae file in the editor window and click on the little sidebar icon lower left. In that side bar is the Scene graph (pic below). You can not only witness the hierarchy, you can rename and, if necessary, move nodes around and reparent (careful). You may need to add other safeguards against the file not being found. I'm using this to build archives, not client-side.

To continue the example:

    baseNode.addChildNode(chain)
    baseNode.addChildNode(hetero)
    // (baseNode is a child of the current scene's root node) 

These now behave just like nodes you've created within SceneKit. In my example, if chain had some tweaked orientation that prevented an easy animation, say about the world's y-axis, could rotate baseNode instead. Or if it needed rotation separate from hetero I would instead put it in a new, otherwise empty node and rotate that.

    let panNode = SCNNode()
    panNode.addChildNode(chain)
    baseNode.addChildNode(panNode)
    // perform rotations on panNode or baseNode



来源:https://stackoverflow.com/questions/36490351/transform-and-rotate-in-scenekit

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