LoginSignup
2
4

More than 3 years have passed since last update.

SceneKitでSCNNodeの `boundingBox` はそのノードのローカル座標系における値である

Posted at

iOS の SceneKit で SCNNode の位置や大きさをだいたい把握するために boundingBox プロパティを参照することが多いと思いますが、このプロパティの指す値は公式ドキュメント(下記)にあるとおり、そのノードのローカル座標系における値となるため注意が必要です。

SCNBoundingVolume - boundingBox

Scene Kit defines a bounding box in the local coordinate space using two points identifying its corners, which implicitly determine six axis-aligned planes marking its limits.

これについてボックス形状の SCNBox をジオメトリとした SCNNode を例に整理していきます

まず、各辺が 1 の立方体の SCNNode boxNode を定義してシーンに追加します。

let scene = SCNScene()
let box = SCNBox(width: 1, height: 1, length: 1, chamferRadius: 0)
let boxNode = SCNNode(geometry: box)
scene.rootNode.addChildNode(boxNode)
print(boxNode.boundingBox)

Simulator Screen Shot - iPhone Xs - 2019-06-02 at 22.18.45.png

この時、boxNodeboundingBox は次のようになっています。

console
(min: __C.SCNVector3(x: -0.5, y: -0.5, z: -0.5), max: __C.SCNVector3(x: 0.5, y: 0.5, z: 0.5))

原点を中心に配置されていることがわかります。それではこの boxNode の位置やスケールを変更してみます。

boxNode.position.x = 1
boxNode.scale.y = 2
print(boxNode.boundingBox)

Simulator Screen Shot - iPhone Xs - 2019-06-02 at 22.25.36.png

X軸方向に +1 移動し、Y軸方向に倍に伸びた形状になりました。ここでboundingBox の値はどうなっているかというと...

console
(min: __C.SCNVector3(x: -0.5, y: -0.5, z: -0.5), max: __C.SCNVector3(x: 0.5, y: 0.5, z: 0.5))

はい、変更する前と同じです。

最初に述べた通り、boundingBox はその SCNNode のローカル座標系のものだからです。SCNNode の position, scale といったプロパティを変更すると、その親のノードの座標系での位置やスケールが変化していることになります(position プロパティの公式ドキュメントには次のように書かれています)。

SCNNode - position

The node’s position locates it within the coordinate system of its parent,

次に、boxNode の親となる空の SCNNode parentNode をつくり boxNode のみを子として追加して boundingBox を見てみましょう。

let parentNode = SCNNode()
parentNode.addChildNode(boxNode)
scene.rootNode.addChildNode(parentNode)
print(parentNode.boundingBox)
console
(min: __C.SCNVector3(x: 0.49999997, y: -0.99999994, z: -0.49999997), max: __C.SCNVector3(x: 1.4999999, y: 0.99999994, z: 0.49999997))

計算の誤差のようなものは出ていますが、実際のカメラからの見た目上の位置と大きさに相当する値になりました。この値は SCNNode の各プロパティや convert~ 系のメソッドで求めることはできると思いますが、一つ空の親ノードをかませてあげることで簡単に求めることができました。

2
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
4