Swift - 如何更改 SCNNode 对象的 Pivot

Swift - How to change the Pivot of a SCNNode object(Swift - 如何更改 SCNNode 对象的 Pivot)

本文介绍了Swift - 如何更改 SCNNode 对象的 Pivot的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我玩 SCNNode 对象已经有一段时间了,但我对 Pivot 感到迷茫.如何更改 SCNNode(SCNBox 作为条形)的轴心并将轴心放置在条形的边缘之一上?

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?

推荐答案

一个节点的pivot是一个变换矩阵,它的逆被应用到它的transform 属性生效.例如,看一下 Xcode 中默认的 SceneKit 游戏模板中的这一点:

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)

如果你设置boxNodeposition,那个点对应立方体的中心,如果你旋转它(就像模板在动画中做的那样),它围绕它的中心旋转.

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.

要更改锚点,请将 pivot 设置为平移变换:

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

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

现在,当您设置 position 时,该点对应于立方体的右上角,当您旋转立方体时,它会围绕该角旋转.

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.

更一般地说,枢轴会相对于节点自身的变换来变换节点的内容.假设您想要模拟地球自转轴的岁差.您可以通过创建两个动画来做到这一点:一个动画 pivot 使节点围绕其自己的 Y 轴旋转,另一个动画 rotation 动画以相对于空间移动该轴包含节点.

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.

这篇关于Swift - 如何更改 SCNNode 对象的 Pivot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Swift - 如何更改 SCNNode 对象的 Pivot

基础教程推荐