LoginSignup
11
4

More than 5 years have passed since last update.

SceneKitのメモ

Last updated at Posted at 2018-05-21

SceneKit関連のスニペットや、単体の記事にするほどでもないメモをここに随時書き足していく予定です。

3次元計算

ランダムな3次元単位ベクトルを生成

Swift
func randomVector3() -> SCNVector3 {
    let rand = SCNVector3(random(min: -1.0, max: 1.0),
                          random(min: -1.0, max: 1.0),
                          random(min: -1.0, max: 1.0))
    return rand.normalized
}

いろんなアプローチがあるみたいだが、下記記事を読んだ感じだととりあえずこのシンプルなのでもよさそう。

複数軸方向に回転

swift
let rotX = SCNMatrix4MakeRotation(Float.pi, 1, 0, 0)
let rotZ = SCNMatrix4MakeRotation(Float.pi/2, 0, 0, 1)
node.pivot = SCNMatrix4Mult(rotZ, rotX)
objc
SCNMatrix4 rotX = SCNMatrix4MakeRotation(M_PI, 1, 0, 0);
SCNMatrix4 rotZ = SCNMatrix4MakeRotation(M_PI/2, 0, 0, 1);
node.pivot = SCNMatrix4Mult(rotZ, rotX);

回転行列の積をSCNMatrix4Multで計算する。

pivotはオブジェクトを回転、スケールする際に基準となる点のこと。型はSCNMatrix4。上記の例ではx軸まわりに180°、z軸まわりに90°の回転をpivotとして与えている。

ヒットテスト

サーチモード

SCNHitTestOptionSearchMode(SwiftではSCNHitTestOption.searchMode)のデフォルトはSCNHitTestSearchModeClosest(Swiftでは.closest)なので一番近いのしか返してくれない。(ドキュメントには書いてないが、実際に値をみてそうなっていた)

全結果を取得にはSCNHitTestSearchModeAll(.all)を指定する。

objc
NSDictionary<SCNHitTestOption, id> *options = @{SCNHitTestOptionSearchMode: @(SCNHitTestSearchModeAll)};
NSArray<SCNHitTestResult *> *results = [self.renderer hitTest:pos options:options];
11
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
11
4