iPhone星ドーム

星がくるくる回っている中に入っていくiPhoneアプリのサンプルコードを描いてみます。

#import “ViewController.h”

@import SceneKit;

@interface ViewController ()

@property (nonatomic, weak) SCNView *sceneView;

@end

@implementation ViewController

– (void)viewDidLoad {

    [super viewDidLoad];

    [self setupScene];

    [self createPlate];

    [self createDome];

    [self createStella];

    [self createCamera];

}

– (void)setupScene {

    SCNView *sv = [[SCNView alloc] initWithFrame:self.view.bounds];

    sv.backgroundColor = [UIColor blackColor];

    sv.scene = [SCNScene scene];

    sv.autoenablesDefaultLighting = YES;

    sv.allowsCameraControl = YES;

    [self.view addSubview:sv];

    

    self.sceneView = sv;

}

– (void)createPlate {

    SCNCylinder *plate = [SCNCylinder cylinderWithRadius:5 height:0.1];

    plate.firstMaterial.diffuse.contents = [[UIColor whiteColor] colorWithAlphaComponent:0.4];

    SCNNode *plateNode = [SCNNode nodeWithGeometry:plate];

    [self.sceneView.scene.rootNode addChildNode:plateNode];

}

– (void)createDome {

    SCNSphere *sphere = [SCNSphere sphereWithRadius:4];

    sphere.firstMaterial.diffuse.contents = [[UIColor whiteColor] colorWithAlphaComponent:0.5];

    SCNNode *sphereNode = [SCNNode nodeWithGeometry:sphere];

    [self.sceneView.scene.rootNode addChildNode:sphereNode];

}

– (void)createStella {

    

    for (int i=0; i<60; i++) {

        float r = 4;

        float t = (arc4random() % 360);

        float phi = (arc4random() % 360);

        float sr = (arc4random() % 5) * 0.01;

        

        SCNSphere *star = [SCNSphere sphereWithRadius:sr];

        star.firstMaterial.diffuse.contents = [UIColor whiteColor];

        SCNNode *starNode = [SCNNode nodeWithGeometry:star];

        starNode.position = [self sphericalCoordinateTheata:t phi:phi r:r];

        [self.sceneView.scene.rootNode addChildNode:starNode];

        

    }

}

– (SCNVector3)sphericalCoordinateTheata:(float)t phi:(float)p r:(float)r {

    t = M_PI * (t / 180);

    p = M_PI * (p / 180);

    float x = r * sin(t) * cos(p);

    float y = r * sin(t) * sin(p);

    float z = r * cos(t);

    return SCNVector3Make(x, y, z);

}

– (void)createCamera {

    SCNNode *cameraBall = [SCNNode node];

    cameraBall.name = @”camera ball”;

    [self.sceneView.scene.rootNode addChildNode:cameraBall];

    

    SCNNode *camera = [SCNNode node];

    camera.camera = [SCNCamera camera];

    camera.position = SCNVector3Make(0, 5, 16);

    camera.rotation = SCNVector4Make(1, 0, 0, –0.1 * M_PI);

    [cameraBall addChildNode:camera];

    

    [cameraBall runAction:[SCNAction repeatActionForever:[SCNAction rotateByAngle:M_PI aroundAxis:SCNVector3Make(0, 1, 0.1) duration:3.0]]];

}

– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

    SCNNode *camera = [self.sceneView.scene.rootNode childNodeWithName:@”camera ball” recursively:false].childNodes[0];

    [camera runAction:[SCNAction moveTo:SCNVector3Zero duration:4.0]];

}

@end