iPhone turn disk

4枚のディスクを回すiPhoneアプリのサンプルコードを描いてみます。

#import “ViewController.h”

@import SceneKit;

@interface ViewController ()

@property (nonatomic, weak) SCNView *sceneView;

@end

@implementation ViewController

– (void)viewDidLoad {

    [super viewDidLoad];

    [self setupScene];

    [self createDiscs];

    [self createCamera];

}

– (void)setupScene {

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

    sv.backgroundColor = [self color:0];

    sv.scene = [SCNScene scene];

    sv.allowsCameraControl = true;

    [self.view addSubview:sv];

    

    self.sceneView = sv;

}

– (void)createDiscs {

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

        float x = (i % 2) * 5;

        float z = (i / 2) * 5;

        

        SCNCylinder *c = [SCNCylinder cylinderWithRadius:2 height:0.1];

        c.firstMaterial.diffuse.contents = [self color:i + 1];

        SCNNode *disc = [SCNNode nodeWithGeometry:c];

        disc.position = SCNVector3Make(x, 0, z);

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

        

        if (i == 0) {

            SCNBox *b = [SCNBox boxWithWidth:1.2 height:0.1 length:1.2 chamferRadius:0];

            b.firstMaterial.diffuse.contents = [self color:4 – i];

            SCNNode *n0 = [SCNNode nodeWithGeometry:b];

            n0.position = SCNVector3Make(0.5, 0.3, 0.4);

            SCNNode *n1 = [SCNNode nodeWithGeometry:b];

            n1.position = SCNVector3Make(-0.5, 0.1, –0.4);

            [disc addChildNode:n0];

            [disc addChildNode:n1];

        }

        else if (i==1) {

            UIBezierPath *triangle = [UIBezierPath bezierPath];

            [triangle moveToPoint:CGPointMake(1.5, 0)];

            [triangle addLineToPoint:CGPointMake(0, 0)];

            [triangle addLineToPoint:CGPointMake(0, 1.5)];

            [triangle closePath];

            SCNShape *t = [SCNShape shapeWithPath:triangle extrusionDepth:0.2];

            t.firstMaterial.diffuse.contents = [self color:4-i];

            SCNNode *tn = [SCNNode nodeWithGeometry:t];

            tn.pivot = SCNMatrix4MakeRotation(M_PI/2.0, 1, 0, 0);

            tn.position = SCNVector3Make(0, 0, 0.2);

            [disc addChildNode:tn];

        }

        else if (i==2) {

            SCNBox *bar = [SCNBox boxWithWidth:3 height:0.2 length:0.2 chamferRadius:0];

            bar.firstMaterial.diffuse.contents = [self color:4 – i];

            SCNNode *barNode = [SCNNode nodeWithGeometry:bar];

            barNode.rotation = SCNVector4Make(0, 1, 0, M_PI * 0.5);

            [disc addChildNode:barNode];

            SCNNode *barNode2 = [SCNNode nodeWithGeometry:bar];

            [disc addChildNode:barNode2];

        }

        else if (i==3) {

            SCNText *t = [SCNText textWithString:@”Disc” extrusionDepth:0.2];

            t.firstMaterial.diffuse.contents = [self color:4 – i];

            t.font = [UIFont systemFontOfSize:0.8];

            SCNNode *tNode = [SCNNode nodeWithGeometry:t];

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

            [disc addChildNode:tNode];

        }

    }

}

– (void)createCamera {

    SCNNode *camera = [SCNNode node];

    camera.camera = [SCNCamera camera];

    camera.position = SCNVector3Make(2.5, 15, 3);

    camera.rotation = SCNVector4Make(1, 0, 0, –M_PI/1.95);

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

}

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

    CGPoint p = [[touches anyObject] locationInView:self.sceneView];

    SCNHitTestResult *hit = [self.sceneView hitTest:p options:@{SCNHitTestSortResultsKey : @YES}].firstObject;

    if (hit) {

        if (!hit.node.hasActions) {

            [hit.node runAction:[SCNAction repeatActionForever:[SCNAction rotateByX:0 y:M_PI z:0 duration:1.0]]];

        }

    }

}

#define ColorHex(rgb) [UIColor colorWithRed:((rgb & 0xFF0000) >> 16)/255.0 green:((rgb & 0xFF00) >> 8)/255.0 blue:(rgb & 0xFF)/255.0 alpha:1.0]

– (UIColor *)color:(int)i {

    if (i > 4) {

        return nil;

    }

    int colorCode[] = {0x04BFBF, 0xCAFCD8, 0xF7E967, 0xA9CF54, 0x588F27};

    return ColorHex(colorCode[i]);

}

@end