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 createCamera];

    [self createLight];

    [self createTriangle];

}

– (void)setupScene {

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

    sv.backgroundColor = [self color:1];

    sv.scene = [SCNScene scene];

    sv.autoenablesDefaultLighting = YES;

    sv.allowsCameraControl = YES;

    [self.view addSubview:sv];

    

    self.sceneView = sv;

}

– (void)createPlate {

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

        SCNCylinder *c = [SCNCylinder cylinderWithRadius:20 height:1];

        c.firstMaterial.diffuse.contents = [self color:4];

        SCNNode *plate = [SCNNode nodeWithGeometry:c];

        plate.position = SCNVector3Make(0, i * 1818, 0);

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

    }

}

– (void)createCamera {

    SCNNode *camera = [SCNNode node];

    camera.camera = [SCNCamera camera];

    camera.camera.zFar = 300;

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

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

}

– (void)createLight {

    SCNLight *light = [SCNLight light];

    light.type = SCNLightTypeAmbient;

    SCNNode *lightNode = [SCNNode node];

    lightNode.light = light;

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

}

– (void)createTriangle {

    

    UIBezierPath *path = [UIBezierPath bezierPath];

    float dAngle = M_PI / 1.5;

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

        float x = 5 * cos(i * dAngle – M_PI / 6.0);

        float y = 5 * sin(i * dAngle – M_PI / 6.0);

        if (i == 0) [path moveToPoint:CGPointMake(x, y)];

        else [path addLineToPoint:CGPointMake(x, y)];

    }

    

    int cnt = 15;

    float a = M_PI / 7.5;

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

        for (int j=0; j<2; j++) {

            SCNShape *triangle = [SCNShape shapeWithPath:path extrusionDepth:0.5];

            triangle.firstMaterial.diffuse.contents = [self color:0];

            SCNNode *node = [SCNNode nodeWithGeometry:triangle];

            node.transform = SCNMatrix4Translate(node.transform, 0, 1020 * j, –20);

            node.transform = SCNMatrix4Rotate(node.transform, a * i, 0, 1, 0);

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

        }

    }

}

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

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

    if (i>4)  return nil;

    int colorCode[] = {0x85DB18, 0x101010, 0xF5F6D4, 0xA7C520, 0x493F0B};

    return ColorHex(colorCode[i]);

}

@end