動機
近々遊べそうなおもちゃが手に入るのでそれを使った3Dゲーム(?)を作ろうとしていて予行演習がしたかった。
どのフレームワークを使うか
新規プロジェクトを作る画面でGameを選択すると、Game TechnologyをReality/Sprite/Metal/SceneKitの4つから選ぶようになっている。それぞれ見ていくと以下のような説明がある。
- RealityKit: Simulate and render 3D content for use in your augmented reality apps.*1
- SpriteKit: Add high-performance 2D content with smooth animations to your app, or create a game with a high-level set of 2D game-based tools.*2
- MetalKit: Build Metal apps quicker and easier using a common set of utility classes.*3
- SceneKit: Create 3D games and add 3D content to apps using high-level scene descriptions.*4
作りたいのはARでも2DでもないのでRealityKitとSpriteKitは除外される。となるとMetalKitとSceneKitのどちらかになるわけだが、SceneKitの方にこう書いてあったのでSceneKitを選んだ。今回はおもちゃで遊びつつサクッと作ってしまいたかったので。
Unlike lower-level APIs such as Metal and OpenGL ... SceneKit requires only descriptions of your scene’s contents and the actions or animations you want it to perform.
必要そうな知識
ノード
SceneKitにおいて、scene*5のコンテンツはツリー構造のnode*6で表されており、カメラや光源、オブジェクトなどがnodeにあたる。sceneはroot nodeを持っていて、これにnodeを追加していくことでコンテンツを構築していく。
https://docs-assets.developer.apple.com/published/4f035789b7/592d7da9-814c-4bbd-b1a7-0e07b3003d94.png
nodeは移動や回転などに使う座標系を保持するだけのオブジェクトであり、見た目や振る舞いを変えるにはattachmentを使う必要がある。物体にしたいならSCNGeometry*7、光源にしたいならSCNLight*8、物理法則に従った振る舞いをさせたいならSCNPhysicsBody*9など。
参考
- https://developer.apple.com/documentation/scenekit/organizing_a_scene_with_nodes
- https://developer.apple.com/documentation/scenekit/scnscene
- https://developer.apple.com/documentation/scenekit/scnnode
座標系
シーン内の座標は右手系が使われる。
https://docs-assets.developer.apple.com/published/a9d3c84003/03f7f401-5c21-4ac9-9c8c-e7640ec11d81.png
あるnodeの座標系は親nodeの座標系の中にある。例えば、親が親の座標系でx軸上を+5移動すると、子の座標系での子の座標は変わらないが5の長さだけ移動している。という例が分かりづらいので動かそうと思ったがめちゃくちゃ時間かかった上にできなかったので今日は終わり。リポジトリはこれ酒飲むぞ!!!!!!
*1:https://developer.apple.com/documentation/realitykit/
*2:https://developer.apple.com/documentation/spritekit/
*3:https://developer.apple.com/documentation/metalkit/
*4:https://developer.apple.com/documentation/scenekit/
*5:SCNScene: https://developer.apple.com/documentation/scenekit/scnscene
*6:SCNNode: https://developer.apple.com/documentation/scenekit/scnnode
*7:https://developer.apple.com/documentation/scenekit/scngeometry
*8:https://developer.apple.com/documentation/scenekit/scnlight
*9:https://developer.apple.com/documentation/scenekit/scnphysicsbody