Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
541 views
in Technique[技术] by (71.8m points)

ios - Transform and Rotate in Scenekit

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)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

Where to get node info from a dae file.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...