在 Scenekit 中变换和旋转 [英] Transform and Rotate in Scenekit

查看:48
本文介绍了在 Scenekit 中变换和旋转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 SCN 节点,我已将其转换为所需的旋转和角度.

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

我现在想在它的新 x 轴上无限旋转它,但我似乎无法让它旋转 360 度,它只是稍微移动.

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.

转换代码:

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)

推荐答案

回答您的评论和可能的问题.安装 .dae 文件后,您可以访问节点,如下例所示:

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)!

如果您需要查找这些节点的名称,请在编辑器窗口中打开 .dae 文件,然后单击左下方的小侧边栏图标.在那个侧栏中是场景图(下图).您不仅可以见证层次结构,还可以重命名,并在必要时移动节点并重新设置父节点(小心).您可能需要添加其他保护措施以防止找不到文件.我正在使用它来构建档案,而不是客户端.

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.

继续这个例子:

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

这些现在的行为就像您在 SceneKit 中创建的节点一样.在我的示例中,如果 chain 有一些调整的方向阻止了简单的动画,比如世界的 y 轴,则可以改为旋转 baseNode.或者,如果它需要与 hetero 分开旋转,我会将它放在一个新的、否则为空的节点中并旋转它.

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

这篇关于在 Scenekit 中变换和旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆