箱をカギであけるiPhoneアプリのサンプルコードを描いてみます。
#import “ViewController.h”
@import SceneKit;
@interface ViewController ()
@property (nonatomic, weak) SCNView *sceneView;
@property (nonatomic) int count;
@end
@implementation ViewController
– (void)viewDidLoad {
[super viewDidLoad];
[self setupScene];
[self createBox];
[self createKey];
[self createCamera];
}
– (void)setupScene {
SCNView *sv = [[SCNView alloc] initWithFrame:self.view.bounds];
sv.backgroundColor = [UIColor colorWithHue:0.14 saturation:0.1 brightness:1 alpha:1];
sv.scene = [SCNScene scene];
sv.autoenablesDefaultLighting = YES;
[self.view addSubview:sv];
self.sceneView = sv;
}
– (void) createBox {
SCNVector4 ws[] = {
SCNVector4Make(1, 0, 0, 0.5 * M_PI),
SCNVector4Make(1, 0, 0, –0.5 * M_PI),
SCNVector4Make(0, 1, 0, 0.5 * M_PI),
SCNVector4Make(0, 1, 0, 1.0 * M_PI),
SCNVector4Make(0, 1, 0, 1.5 * M_PI),
SCNVector4Make(0, 1, 0, 2.0 * M_PI)
};
SCNNode *lockbox = [SCNNode node];
lockbox.name = @”lock box”;
for (int i=0; i<6; i++) {
SCNBox *box = [SCNBox boxWithWidth:2 height:2 length:0.1 chamferRadius:0];
box.firstMaterial.diffuse.contents = [UIColor brownColor];
SCNNode *boxNode = [SCNNode nodeWithGeometry:box];
boxNode.pivot = SCNMatrix4MakeTranslation(0, 0, –1);
boxNode.rotation = ws[i];
[lockbox addChildNode:boxNode];
if (i==4) {
SCNBox *hole = [SCNBox boxWithWidth:0.3 height:0.09 length:0.09 chamferRadius:0.01];
hole.firstMaterial.diffuse.contents = [UIColor blackColor];
SCNNode *holeNode = [SCNNode nodeWithGeometry:hole];
holeNode.position = SCNVector3Make(0.15, 0, 0.01);
[boxNode addChildNode:holeNode];
SCNCylinder *holeS = [SCNCylinder cylinderWithRadius:0.12 height:0.11];
holeS.firstMaterial.diffuse.contents = [UIColor blackColor];
SCNNode *holeSNode = [SCNNode nodeWithGeometry:holeS];
holeSNode.rotation = SCNVector4Make(1, 0, 0, 0.5 * M_PI);
[boxNode addChildNode:holeSNode];
}
if (i==1) {
boxNode.name = @”top”;
}
}
[self.sceneView.scene.rootNode addChildNode:lockbox];
}
– (void)createKey {
SCNNode *key = [SCNNode node];
key.name = @”key”;
SCNBox *bar = [SCNBox boxWithWidth:0.1 height:0.1 length:0.7 chamferRadius:0.1];
bar.firstMaterial.diffuse.contents = [UIColor yellowColor];
SCNNode *barNode = [SCNNode nodeWithGeometry:bar];
[key addChildNode:barNode];
SCNTube *grip = [SCNTube tubeWithInnerRadius:0.13 outerRadius:0.18 height:0.1];
grip.firstMaterial.diffuse.contents = [UIColor yellowColor];
SCNNode *gripNode = [SCNNode nodeWithGeometry:grip];
gripNode.position = SCNVector3Make(0, 0, 0.5);
[key addChildNode:gripNode];
for (int i=0; i<2; i++) {
SCNBox *b = [SCNBox boxWithWidth:0.2 height:0.08 length:0.08 chamferRadius:0];
b.firstMaterial.diffuse.contents = [UIColor yellowColor];
SCNNode *bnode = [SCNNode nodeWithGeometry:b];
bnode.position = SCNVector3Make(0.1, 0, i==0 ? –0.3 : –0.1);
[key addChildNode:bnode];
}
key.position = SCNVector3Make(0, 0, 5);
[self.sceneView.scene.rootNode addChildNode:key];
}
– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
SCNNode *lockbox = [self.sceneView.scene.rootNode childNodeWithName:@”lock box” recursively:NO];
[lockbox runAction:[SCNAction rotateByX:0 y:-0.5 * M_PI z:0 duration:0.6]];
if (self.count == 2) {
SCNNode *top = [lockbox childNodeWithName:@”top” recursively:NO];
SCNNode *key = [self.sceneView.scene.rootNode childNodeWithName:@”key” recursively:NO];
[key runAction:
[SCNAction sequence:@[
[SCNAction waitForDuration:0.6],
[SCNAction moveTo:SCNVector3Make(0, 0, 1) duration:0.5],
[SCNAction rotateByX:0 y:0 z:-0.5*M_PI duration:0.7]
]]];
[top runAction:
[SCNAction sequence:@[
[SCNAction waitForDuration:1.5],
[SCNAction rotateByX:0 y:0 z:-0.9 duration:0.2]
]]];
}
self.count++;
}
– (void)createCamera {
SCNNode *camera = [SCNNode node];
camera.camera = [SCNCamera camera];
camera.position = SCNVector3Make(0, 3, 8);
camera.rotation = SCNVector4Make(1, 0, 0, –0.4);
[self.sceneView.scene.rootNode addChildNode:camera];
}
@end