iPhone 3Dターンテーブル

3Dのオブジェクトをのっけたテーブルを回すiPhoneアプリのサンプルコードを描いてみます。

#import “ViewController.h”

@import SceneKit;

@interface ViewController ()

@property (nonatomic, weak) SCNView *sceneView;

@end

@implementation ViewController

– (void)viewDidLoad {

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor colorWithHue:0.2 saturation:0.7 brightness:1 alpha:1];

    [self setupScene];

    [self createTable];

    [self createCamera];

    [self createLight];

    [self setObjects];

}

– (void)setupScene {

    SCNView *sv = [[SCNView alloc] initWithFrame:CGRectMake(0, 0, 320, 320)];

    sv.center = CGPointMake(CGRectGetMidX(self.view.bounds), CGRectGetMidY(self.view.bounds));

    sv.backgroundColor = [UIColor colorWithHue:0.4 saturation:0.7 brightness:1 alpha:1];

    sv.scene = [SCNScene scene];

    [self.view addSubview:sv];

    

    self.sceneView = sv;

}

– (void)createTable

{

    SCNBox *box = [SCNBox boxWithWidth:20 height:1 length:20 chamferRadius:0.1];

    SCNMaterial *material = [SCNMaterial material];

    material.diffuse.contents = [UIColor colorWithHue:0.15 saturation:0.5 brightness:0.8 alpha:1];

    box.materials = @[material];

    SCNNode *table = [SCNNode nodeWithGeometry:box];

    table.name = @”table”;

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

}

– (void)createCamera

{

    SCNNode *camera = [SCNNode node];

    camera.camera = [SCNCamera camera];

    camera.position = SCNVector3Make(0, 10, 35);

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

}

– (void)setObjects

{

    SCNNode *table = [self.sceneView.scene.rootNode childNodeWithName:@”table” recursively:NO];

    

    SCNVector3 p[] = {SCNVector3Make(-5, 1, 0), SCNVector3Make(0, 1, –5),SCNVector3Make(5, 1, 1), SCNVector3Make(-2, 1, 5), SCNVector3Make(5, 1, 5)};

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

        SCNCylinder *geometry = [SCNCylinder cylinderWithRadius:2 height:i*1.5 + 2];

        geometry.firstMaterial.diffuse.contents = [UIColor colorWithHue:0.15 * i saturation:0.2 brightness:1 alpha:1];

        SCNNode *n = [SCNNode nodeWithGeometry:geometry];

        n.position = p[i];

        

        [table addChildNode:n];

    }

}

– (void)createLight

{

    SCNLight *light = [SCNLight light];

    light.type = SCNLightTypeOmni;

    light.color = [UIColor colorWithWhite:0.9 alpha:0.8];

    SCNNode *node = [SCNNode node];

    node.light = light;

    node.position = SCNVector3Make(40, 60, 0);

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

}

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

{

    

    SCNNode *table = [self.sceneView.scene.rootNode childNodeWithName:@”table” recursively:NO];

    [SCNTransaction begin];

    [SCNTransaction setAnimationDuration:3.0];

    table.transform = SCNMatrix4Rotate(table.transform, M_PI, 0, 1, 0);

    [SCNTransaction commit];

}

– (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end