iPhone天球儀的な

天球儀みたいなのかっこいいなと思ってそれっぽくみえるiPhoneアプリのサンプルコードを描いてみます。

#import “ViewController.h”

@import SceneKit;

@interface ViewController ()

@property (nonatomic, weak) SCNView *sceneView;

@end

@implementation ViewController

– (void)viewDidLoad {

    [super viewDidLoad];

    [self setupScene];

    [self createWire];

    [self createCamera];

}

– (void)setupScene {

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

    sv.scene = [SCNScene scene];

    sv.backgroundColor = [UIColor colorWithWhite:0.01 alpha:1];

    sv.autoenablesDefaultLighting = YES;

    sv.allowsCameraControl = YES;

    [self.view addSubview:sv];

    

    self.sceneView = sv;

}

– (void)createWire {

    SCNSphere *blueSphere = [SCNSphere sphereWithRadius:1];

    blueSphere.firstMaterial.diffuse.contents = [UIColor blueColor];

    SCNNode *blueNode = [SCNNode nodeWithGeometry:blueSphere];

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

    

    SCNTube *w1 = [SCNTube tubeWithInnerRadius:7.2 outerRadius:8.4 height:0.1];

    w1.firstMaterial.diffuse.contents = [UIColor colorWithHue:0.125 saturation:0.5 brightness:1 alpha:1];

    SCNNode *w1N = [SCNNode nodeWithGeometry:w1];

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

    [w1N runAction:[SCNAction repeatActionForever:[SCNAction rotateByX:M_PI y:0 z:0 duration:6]]];

    

    SCNNode *w2Outer = [SCNNode node];

    SCNTube *w2 = [SCNTube tubeWithInnerRadius:7 outerRadius:7.02 height:0.6];

    w2.firstMaterial.diffuse.contents = [UIColor colorWithHue:0.125 saturation:0.5 brightness:1 alpha:1];

    SCNNode *w2N = [SCNNode nodeWithGeometry:w2];

    [w2Outer addChildNode:w2N];

    w2Outer.pivot = SCNMatrix4MakeRotation(M_PI * 0.5, 1, 0, 0);

    [w2N runAction:[SCNAction repeatActionForever:[SCNAction rotateByX:M_PI y:0 z:0 duration:3]]];

    [w1N addChildNode:w2Outer];

    

    SCNNode *w3Outer = [SCNNode node];

    SCNTube *w3 = [SCNTube tubeWithInnerRadius:6.8 outerRadius:6.82 height:0.6];

    w3.firstMaterial.diffuse.contents = [UIColor colorWithHue:0.125 saturation:0.5 brightness:1 alpha:1];

    SCNNode *w3N = [SCNNode nodeWithGeometry:w3];

    [w3Outer addChildNode:w3N];

    w3Outer.pivot = SCNMatrix4MakeRotation(M_PI * 0.5, 0, 1, 0);

    [w3N runAction:[SCNAction repeatActionForever:[SCNAction rotateByX:M_PI y:0 z:0 duration:3]]];

    [w1N addChildNode:w3Outer];

    

    

    SCNNode *w4Outer = [SCNNode node];

    SCNTube *w4 = [SCNTube tubeWithInnerRadius:6.6 outerRadius:6.62 height:0.6];

    w4.firstMaterial.diffuse.contents = [UIColor colorWithHue:0.125 saturation:0.5 brightness:1 alpha:1];

    SCNNode *w4N = [SCNNode nodeWithGeometry:w4];

    [w4Outer addChildNode:w4N];

    w4Outer.pivot = SCNMatrix4MakeRotation(M_PI * 0.5, 0, 0, 1);

    [w4N runAction:[SCNAction repeatActionForever:[SCNAction rotateByX:M_PI y:0 z:0 duration:3]]];

    [w1N addChildNode:w4Outer];

}

– (void)createCamera {

    SCNNode *camera = [SCNNode node];

    camera.camera = [SCNCamera camera];

    camera.position = SCNVector3Make(0, 0, 30);

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

}

@end