
赤い四角に青と黄色の網をかけて違う色に見せるiPhoneアプリのサンプルコードを描いてみよう。
#import “ViewController.h”
@import SceneKit;
@interface ViewController ()
@property (nonatomic, weak) SCNView *sceneView;
@property (nonatomic) BOOL flip;
@end
@implementation ViewController
– (void)viewDidLoad {
[super viewDidLoad];
[self setupScene];
[self createBackPlate];
[self createMark];
[self createGrid];
}
– (void)setupScene {
SCNView *sv =[[SCNView alloc] initWithFrame:self.view.bounds];
sv.scene = [SCNScene scene];
sv.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:sv];
self.sceneView = sv;
}
– (void)createBackPlate {
NSArray *color = @[[UIColor yellowColor], [UIColor blueColor]];
for (int i=0; i<2; i++) {
SCNBox *plate = [SCNBox boxWithWidth:20 height:20 length:0.1 chamferRadius:0];
plate.firstMaterial.diffuse.contents = color[i];
SCNNode *plateNode = [SCNNode nodeWithGeometry:plate];
plateNode.name = [NSString stringWithFormat:@”back %d”, i];
plateNode.pivot = SCNMatrix4MakeTranslation(i==0 ? –10 : 10, 0, 0);
plateNode.position = SCNVector3Make(0, 0, –0.1);
[self.sceneView.scene.rootNode addChildNode:plateNode];
}
}
– (void)createMark {
for (int i=0; i<2; i++) {
SCNBox *box = [SCNBox boxWithWidth:10 height:10 length:0.1 chamferRadius:0];
box.firstMaterial.diffuse.contents = [UIColor redColor];
SCNNode *boxNode = [SCNNode nodeWithGeometry:box];
boxNode.position = SCNVector3Make(i==0 ? –10 : 10, 0, 0);
boxNode.rotation = SCNVector4Make(0, 0, 1, M_PI * 0.25);
[self.sceneView.scene.rootNode addChildNode:boxNode];
}
}
– (void)createGrid {
NSArray *color = @[[UIColor blueColor], [UIColor yellowColor]];
for (int i=0; i<2; i++) {
SCNNode *node = [SCNNode node];
for (int j=0; j<=20; j++) {
SCNBox *vBar = [SCNBox boxWithWidth:20 height:0.2 length:0.1 chamferRadius:0];
vBar.firstMaterial.diffuse.contents = color[i];
SCNNode *vNdode = [SCNNode nodeWithGeometry:vBar];
vNdode.position = SCNVector3Make(0, j * 1 – 10, 0);
[node addChildNode:vNdode];
SCNBox *hBar = [SCNBox boxWithWidth:0.2 height:20 length:0.1 chamferRadius:0];
hBar.firstMaterial.diffuse.contents = color[i];
SCNNode *hNdode = [SCNNode nodeWithGeometry:hBar];
hNdode.position = SCNVector3Make(j * 1 – 10, 0, 0);
[node addChildNode:hNdode];
}
node.name = [NSString stringWithFormat:@”grid %d”, i];
node.pivot = SCNMatrix4MakeTranslation(i==0 ? –10 : 10, 0, 0);
node.position = SCNVector3Make(0, 0, 0.1);
[self.sceneView.scene.rootNode addChildNode:node];
}
}
– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
SCNNode *grid0 = [self.sceneView.scene.rootNode childNodeWithName:@”grid 0″ recursively:NO];
SCNNode *grid1 = [self.sceneView.scene.rootNode childNodeWithName:@”grid 1″ recursively:NO];
SCNNode *back0 = [self.sceneView.scene.rootNode childNodeWithName:@”back 0″ recursively:NO];
SCNNode *back1 = [self.sceneView.scene.rootNode childNodeWithName:@”back 1″ recursively:NO];
if (self.flip) {
[grid0 runAction:[SCNAction rotateToX:0 y:0 z:0 duration:2.0]];
[grid1 runAction:[SCNAction rotateToX:0 y:0 z:0 duration:2.0]];
[back0 runAction:[SCNAction rotateToX:0 y:0 z:0 duration:2.0]];
[back1 runAction:[SCNAction rotateToX:0 y:0 z:0 duration:2.0]];
} else {
[grid0 runAction:[SCNAction rotateToX:0 y:-M_PI z:0 duration:2.0]];
[grid1 runAction:[SCNAction rotateToX:0 y:M_PI z:0 duration:2.0]];
[back0 runAction:[SCNAction rotateToX:0 y:M_PI z:0 duration:2.0]];
[back1 runAction:[SCNAction rotateToX:0 y:-M_PI z:0 duration:2.0]];
}
self.flip = !self.flip;
}
@end