iPhone箱矢

箱をくるっとして矢印を表示するiPhoneアプリのサンプルコードを描いてみます。

#import “ViewController.h”

@import SceneKit;

@interface ViewController ()

@property (nonatomic, weak) SCNView *sceneView;

@end

@implementation ViewController

– (void)viewDidLoad {

    [super viewDidLoad];

    [self setupScene];

    [self createCube];

    [self createCamera];

}

– (void)setupScene {

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

    sv.backgroundColor = [UIColor greenColor];

    sv.scene = [SCNScene scene];

    [self.view addSubview:sv];

    self.sceneView = sv;

}

– (void)createCube {

    

    int colorSetting[9][6] = {

        {0,0,0,1,0,0},

        {0,0,0,0,0,0},

        {0,1,0,0,0,0},

        {0,1,0,1,0,0},

        {0,1,0,1,0,0},

        {0,1,0,1,0,0},

        {0,0,0,1,0,0},

        {0,0,0,0,0,0},

        {0,1,0,0,0,0}

    };

    

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

        float x = (i % 3);

        float y = (i / 3);

        SCNBox *box = [SCNBox boxWithWidth:0.8 height:0.8 length:0.8 chamferRadius:0];

        NSMutableArray *materials = [NSMutableArray array];

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

            SCNMaterial *m = [SCNMaterial material];

            m.diffuse.contents = colorSetting[i][j] == 0 ? [UIColor whiteColor] : [UIColor blackColor];

            [materials addObject:m];

        }

        box.materials = materials;

        

        SCNNode *boxNode = [SCNNode nodeWithGeometry:box];

        boxNode.name = [NSString stringWithFormat:@”box %d”, i];

        boxNode.position = SCNVector3Make(x, y, 0);

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

    }

}

– (void)createCamera {

    SCNNode *camera = [SCNNode node];

    camera.camera = [SCNCamera camera];

    camera.camera.usesOrthographicProjection = YES;

    camera.camera.orthographicScale = 2;

    camera.position = SCNVector3Make(1, 0, 7);

    [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.node.name isEqual:@”box 5″]) {

        [self.sceneView.scene.rootNode enumerateChildNodesUsingBlock:^(SCNNode *child, BOOL *stop) {

            if ([child.name hasPrefix:@”box”]) {

                [child runAction:[SCNAction sequence:@[[SCNAction rotateToX:0 y:0 z:0 duration:1.0], [SCNAction rotateToX:0 y:-M_PI * 0.5 z:0 duration:0.2]]]];

            }

        }];

        

    } else if ([hit.node.name isEqual:@”box 3″]) {

        [self.sceneView.scene.rootNode enumerateChildNodesUsingBlock:^(SCNNode *child, BOOL *stop) {

            if ([child.name hasPrefix:@”box”]) {

                [child runAction:[SCNAction sequence:@[[SCNAction rotateToX:0 y:0 z:0 duration:1.0], [SCNAction rotateToX:0 y:M_PI * 0.5 z:0 duration:0.2]]]];

            }

        }];

    }

}

@end