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

查看:154
本文介绍了在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 相反。或者,如果它需要与 heter 分开旋转,我会把它放在一个新的,否则为空的节点并旋转它。

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天全站免登陆