改变节点的pivot
从概念上讲,与在此节点及其父节点之间插入中间节点相同。这在不同情况下很有用。一个例子是当节点的几何中心不在您期望的位置时。
例如,如果您有一个SCNBox
,它的边界框是
- min:
(-0.5 * width, -0.5 * height, -0.5 * length)
- max:
(+0.5 * width, +0.5 * height, +0.5 * length)
- center:
(0.0, 0.0, 0.0)
如果你想要length
of the SCNBox
只增加正Z轴,那么你想要的是
- min:
(-0.5 * width, -0.5 * height, 0.0)
- max:
(+0.5 * width, +0.5 * height, length)
- center:
(0.0, 0.0, +0.5 * length)
几何体的边界框永远不会改变,但有多种方法可以排列节点并更改其边界框。
方案一:中间节点
处理变换时的一种常见解决方案是使用中间节点来更好地理解变换的应用方式。
在您的情况下,您需要更改节点层次结构
- parentNode
| - node
| * geometry
| * transform = SCNMatrix4MakeScale(...)
to
- parentNode
| - intermediateNode
| * transform = SCNMatrix4MakeScale(...)
| | - node
| | * geometry
| | * transform = SCNMatrix4MakeTranslation(0, 0, +0.5 * length)
通过这个新的层次结构,边界框的中心node
还是(0.0, 0.0, 0.0)
,但是边界框的中心intermediateNode
is (0.0, 0.0, +0.5 * length)
.
通过缩放intermediateNode
代替node
你会得到想要的结果。
解决方案 2:枢轴
事实证明,这正是pivot
属性的作用是:
node.pivot = SCNMatrix4MakeTranslation(0, 0, -0.5 * length);
一旦你在心里弄清楚了中间节点的变换,只需设置它的inverse to the pivot
财产。
您可以找到有关的更多信息pivot
这里的财产:https://developer.apple.com/reference/scenekit/scnnode/1408044-pivot
这与 Core Animation 非常相似anchorPoint
属性于CALayer
,除了在核心动画中锚点被指定为相对于图层的边界框(来自0
to 1
作为图层宽度和高度的百分比),而在 SceneKit 中它是绝对的。