SceneKit: understanding the pivot property of SCNNode

前端 未结 3 547
梦如初夏
梦如初夏 2021-01-12 19:05

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

This answer suggests playing with the pivot property

相关标签:
3条回答
  • 2021-01-12 19:47

    Sounds like you want to increase the length of the SCNBox(a geometry). So you can simply increase the length property. The answer you mentioned is about the pivot property. As you can see from the doc:

    The pivot point for the node’s position, rotation, and scale.

    For example, by setting the pivot to a translation transform you can position a node containing a sphere geometry relative to where the sphere would rest on a floor instead of relative to its center.

    0 讨论(0)
  • 2021-01-12 19:51

    Say you have a box created like this:

    SCNBox(width: 1, height: 1, length: 1, chamferRadius: 0)
    

    The pivot point will be in the center of that box, you now want to move it to one of the edges. This can be done by translating the pivot node by 0.5. (This is half the width of the box or the distance between the center and the edge.)

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

    The pivot point will now be located at center X, center Y, and zero Z of the object. If you now scale the box it will only grow in the positive Z direction.

    0 讨论(0)
  • 2021-01-12 19:58

    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.

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

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

    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)
    • center: (0.0, 0.0, +0.5 * length)

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


    Solution 1: Intermediate node

    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(...)
    

    to

    - parentNode
      | - intermediateNode
      |   * transform = SCNMatrix4MakeScale(...)
      |   | - node
      |   |   * geometry
      |   |   * transform = SCNMatrix4MakeTranslation(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).

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

    Solution 2: Pivot

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

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

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


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

    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.

    0 讨论(0)
提交回复
热议问题