SceneKit:理解SCNNode的pivot属性 [英] SceneKit: understanding the pivot property of SCNNode

查看:109
本文介绍了SceneKit:理解SCNNode的pivot属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标是增加 SCNBox 的长度,使其仅在正 z 方向增长.

The goal is to increase the length of a SCNBox such that it only grows in the positive-z direction.

这个答案建议使用 pivot 属性.

但是,pivot 属性的文档在 SCNNode 页面上很少,在 SCNBox 页面上没有任何内容.

However, the documentation for the pivot property is sparse on the SCNNode page, and there is nothing on the SCNBox page.

有人能解释一下 pivot 属性是如何工作的吗?

Can someone explain how the pivot property works?

推荐答案

更改节点的 pivot 在概念上与在此节点与其父节点之间插入中间节点相同.这在不同情况下很有用.一个例子是当节点的几何中心不在您期望的位置时.

Changing a node's pivot is conceptually the same as inserting an intermediate node between this node and its parent. This can be useful in different cases. One example is when the center of the node's geometry isn't where you expect it to be.

例如,如果你有一个 SCNBox,它的边界框是

For instance if you have an SCNBox, it's bounding box is

  • min: (-0.5 * 宽,-0.5 * 高,-0.5 * 长)
  • max: (+0.5 * 宽,+0.5 * 高,+0.5 * 长)
  • 中心:(0.0, 0.0, 0.0)

如果你想让 SCNBoxlength 只在 Z 轴正方向增加,那么你想要的是

If you want the length of the SCNBox to only increase in the positive Z axis, then what you want is

  • min: (-0.5 * width, -0.5 * height, 0.0)
  • max: (+0.5 * width, +0.5 * height, length)
  • 中心:(0.0, 0.0, +0.5 * 长度)

几何体的边界框永远不会改变,但有一些方法可以排列节点并改变它们的边界框.

A geometry's bounding box will never change, but there are ways to arrange nodes and change their bounding boxes.

方案一:中间节点

处理转换时的一种常见解决方案是使用中间节点来更好地了解如何应用转换.

One common solution when dealing with transforms is to use intermediate nodes to get a better understanding of how the transforms are applied.

在您的情况下,您需要更改节点层次结构

In your case you will want to change the node hierarchy from

- parentNode
  | - node
  |   * geometry
  |   * transform = SCNMatrix4MakeScale(...)

- parentNode
  | - intermediateNode
  |   * transform = SCNMatrix4MakeScale(...)
  |   | - node
  |   |   * geometry
  |   |   * transform = SCNMatrix4MakeTranslation(0, 0, +0.5 * length)

有了这个新的层级,node的边界框的中心仍然是(0.0, 0.0, 0.0),但的边界框的中心是(0.0, 0.0, 0.0)>intermediateNode(0.0, 0.0, +0.5 * length).

With this new hierarchy, the center of the bounding box of node is still (0.0, 0.0, 0.0), but the center of the bounding box of intermediateNode is (0.0, 0.0, +0.5 * length).

通过缩放 intermediateNode 而不是 node,您将获得想要的结果.

By scaling intermediateNode instead of node you'll obtain the wanted result.

解决方案 2:透视

事实证明,这正是 pivot 属性的作用:

It turns out that's exactly what the pivot property does:

node.pivot = SCNMatrix4MakeTranslation(0, 0, -0.5 * length);

一旦您在头脑中弄清楚了中间节点的变换,只需将其inverse 设置为 pivot 属性即可.

Once you have mentally figured out the transform of the intermediate node, simply set its inverse to the pivot property.

您可以在此处找到有关 pivot 属性的更多信息:https://developer.apple.com/reference/scenekit/scnnode/1408044-pivot

You can find more information about the pivot property here: https://developer.apple.com/reference/scenekit/scnnode/1408044-pivot

这与 CALayer 上的 Core Animation 的 anchorPoint 属性非常相似,除了在 Core Animation 中,锚点被指定为相对于图层的边界框(来自 CALayercode>0 到 1 作为图层宽度和高度的百分比),而在 SceneKit 中它是绝对的.

This is very similar to Core Animation's anchorPoint property on CALayer, except that in Core Animation the anchor point is specified as relative to the layer's bounding box (goes from 0 to 1 as a percentage of the layer's width and height), while in SceneKit it's absolute.

这篇关于SceneKit:理解SCNNode的pivot属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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