
箱を薄くスライスする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];
[self createLight];
}
– (void)setupScene {
SCNView *sv = [[SCNView alloc] initWithFrame:self.view.bounds];
sv.backgroundColor = [self color:2];
sv.scene = [SCNScene scene];
[self.view addSubview:sv];
self.sceneView = sv;
}
– (void)createCube {
for (int i=0; i<20; i++) {
SCNBox *plate = [SCNBox boxWithWidth:1 height:20 length:20 chamferRadius:0];
plate.firstMaterial.diffuse.contents = [self color:0];
plate.firstMaterial.specular.contents = [self color:1];
SCNNode *plateNode = [SCNNode nodeWithGeometry:plate];
plateNode.name = @”plate”;//[NSString stringWithFormat:@”plate%d”, i];
plateNode.position = SCNVector3Make(-10 + i, 0, 0);
[self.sceneView.scene.rootNode addChildNode:plateNode];
}
}
– (void)createCamera {
SCNNode *camera = [SCNNode node];
camera.camera = [SCNCamera camera];
camera.position = SCNVector3Make(0, 0, 80);
[self.sceneView.scene.rootNode addChildNode:camera];
}
– (void)createLight {
SCNLight *light = [SCNLight light];
SCNNode *lightNode = [SCNNode node];
lightNode.position = SCNVector3Make(0, 0, 50);
lightNode.light = light;
[self.sceneView.scene.rootNode addChildNode:lightNode];
}
– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSPredicate *pred = [NSPredicate predicateWithFormat:@”name == %@”, @”plate”];
NSArray *plates = [self.sceneView.scene.rootNode.childNodes filteredArrayUsingPredicate:pred];
for (int i=0; i<plates.count; i++) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(i * 0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[plates[i] runAction:[SCNAction rotateByAngle:M_PI aroundAxis:SCNVector3Make(1, 0, 0) duration:3.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 {
switch (i) {
case 0: return ColorHex(0x43464C);
case 1: return ColorHex(0x9197A6);
case 2: return ColorHex(0xD3DCF2);
}
return nil;
}
@end