SceneKitを使えばXcodeでも3Dが扱えるようになりましたので遊んでみましょう!
遊んだシステム構成
Xcode : ver.6.3.1
Language : Objective-C
Device : iPhone5s / iOS 8.3
プロジェクトファイルの作成
まずは、プロジェクトファイルを作っていきます
Create New Xcode projectを選ぶ
Gameを選んで「Next」
Product Nameを決める
LanguagehaObjective-C
Game Technologyは「SceneKit」を選んで「Next」
保存先を決めて「Create」
プロジェクトファイルができました。
何も考えずに実行。
触ったり、タップしたり、フリックしたりしてちょっと遊んでみてください。
ワクワクしませんか!?
モデルを用意するのは大変ですが、こんなに簡単にXcode上で3D表示できてしまうんですよ!
ソースコードを改造して遊んじゃいましょう!
このプロジェクトファイルのソースコードを改造して、ちょっとカッコよくしてきます。
縦から横表示へ
縦表示なんてカッコ悪いので強制横表示です。
General → portaitのチェックを外す
いいですね!
回転を止める
勝手にぐるぐる回っているので停止させます。
42〜46行あたりをコメントアウト
タップ動作を無効化
機体をタップすると赤く点滅する機能を停止させます。
63〜68行あたりをコメントアウト
機体の向きを変える
機体がこちらを向いていても仕方がないので、反対側(180°)回転させます。
// Ship Rotation 180
SCNNode *ship = [scene.rootNode childNodeWithName:@"ship" recursively:YES];
ship.rotation = SCNVector4Make(0, 1, 0, M_PI);
– (void)viewDidLoadの最後の行に入れる。
アフターバーナーみたい! 古い?^^;
パーティクルを追加して臨場感を出す
これでは、当たり前ですがなんだかわかりません。
次は宇宙空間を飛んでいるような臨場感をだします。
パーティクルSceneの追加
New Fileを選択
Resource → SceneKitParticle System → Next
Starsを選択してNext
シーン名「stars」に決めてCreate
Starsの設定値を変更する
パーティクルをSceneに追加する
// Stars Particle
SCNParticleSystem *starsParticle = [SCNParticleSystem particleSystemNamed:@"stars.scnp" inDirectory:nil];
[scene addParticleSystem:starsParticle withTransform:SCNMatrix4MakeTranslation(0, 0, -30)];
これで改造は終了
グリグリ遊ぶ
Runしてみましょう!
下の動画のようになります。
うまくいきましたでしょうか?
もっともっとパーティクルを追加して楽しんじゃってください。
ソースコード
- (void)viewDidLoad
{
[super viewDidLoad];
// create a new scene
SCNScene *scene = [SCNScene sceneNamed:@"art.scnassets/ship.dae"];
// create and add a camera to the scene
SCNNode *cameraNode = [SCNNode node];
cameraNode.camera = [SCNCamera camera];
[scene.rootNode addChildNode:cameraNode];
// place the camera
cameraNode.position = SCNVector3Make(0, 0, 15);
// create and add a light to the scene
SCNNode *lightNode = [SCNNode node];
lightNode.light = [SCNLight light];
lightNode.light.type = SCNLightTypeOmni;
lightNode.position = SCNVector3Make(0, 10, 10);
[scene.rootNode addChildNode:lightNode];
// create and add an ambient light to the scene
SCNNode *ambientLightNode = [SCNNode node];
ambientLightNode.light = [SCNLight light];
ambientLightNode.light.type = SCNLightTypeAmbient;
ambientLightNode.light.color = [UIColor darkGrayColor];
[scene.rootNode addChildNode:ambientLightNode];
// // retrieve the ship node
// SCNNode *ship = [scene.rootNode childNodeWithName:@"ship" recursively:YES];
//
// // animate the 3d object
// [ship runAction:[SCNAction repeatActionForever:[SCNAction rotateByX:0 y:2 z:0 duration:1]]];
// retrieve the SCNView
SCNView *scnView = (SCNView *)self.view;
// set the scene to the view
scnView.scene = scene;
// allows the user to manipulate the camera
scnView.allowsCameraControl = YES;
// show statistics such as fps and timing information
scnView.showsStatistics = YES;
// configure the view
scnView.backgroundColor = [UIColor blackColor];
// // add a tap gesture recognizer
// UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
// NSMutableArray *gestureRecognizers = [NSMutableArray array];
// [gestureRecognizers addObject:tapGesture];
// [gestureRecognizers addObjectsFromArray:scnView.gestureRecognizers];
// scnView.gestureRecognizers = gestureRecognizers;
// Ship Rotation 180
SCNNode *ship = [scene.rootNode childNodeWithName:@"ship" recursively:YES];
ship.rotation = SCNVector4Make(0, 1, 0, M_PI);
// Stars Particle
SCNParticleSystem *starsParticle = [SCNParticleSystem particleSystemNamed:@"stars.scnp" inDirectory:nil];
[scene addParticleSystem:starsParticle withTransform:SCNMatrix4MakeTranslation(0, 0, -30)];
}


















コメント