
どっちに行こうかなと。手を伸ばした人形がくるくるまわる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 createFigure];
[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)createPlate
{
SCNCylinder *plate = [SCNCylinder cylinderWithRadius:25 height:1];
plate.firstMaterial.diffuse.contents = [self color:1];
SCNNode *plateNode = [SCNNode nodeWithGeometry:plate];
[self.sceneView.scene.rootNode addChildNode:plateNode];
}
– (void)createFigure {
SCNNode *figureNode = [SCNNode node];
figureNode.name = @”figure”;
figureNode.position = SCNVector3Make(0, 5, 0);
[self.sceneView.scene.rootNode addChildNode:figureNode];
SCNBox *head = [SCNBox boxWithWidth:5 height:4 length:4 chamferRadius:0.2];
head.firstMaterial.diffuse.contents = [self color:0];
SCNNode *headNode = [SCNNode nodeWithGeometry:head];
headNode.position = SCNVector3Make(0, 5, 0);
[figureNode addChildNode:headNode];
SCNBox *body = [SCNBox boxWithWidth:3 height:5.0 length:1.5 chamferRadius:0.2];
body.firstMaterial.diffuse.contents = [self color:0];
SCNNode *bodyNode = [SCNNode nodeWithGeometry:body];
[figureNode addChildNode:bodyNode];
for (int i=0; i<2; i++) {
SCNBox *arm = [SCNBox boxWithWidth:1.2 height:4 length:1.2 chamferRadius:0.2];
arm.firstMaterial.diffuse.contents = [self color:0];
SCNNode *armNode = [SCNNode nodeWithGeometry:arm];
armNode.pivot = SCNMatrix4MakeTranslation(0, 1.8, 0);
armNode.position = SCNVector3Make(-2.2 + 4.4 * i, 1.8, 0);
[figureNode addChildNode:armNode];
if (i == 0) {
armNode.name = @”movable hand”;
}
SCNBox *foot = [SCNBox boxWithWidth:1.3 height:2 length:1.3 chamferRadius:0.2];
foot.firstMaterial.diffuse.contents = [self color:0];
SCNNode *footNode = [SCNNode nodeWithGeometry:foot];
footNode.position = SCNVector3Make(-0.8 + 1.6 * i, –3.5, 0);
[figureNode addChildNode:footNode];
}
figureNode.physicsBody = [SCNPhysicsBody dynamicBody];
figureNode.physicsBody.velocityFactor = SCNVector3Make(0, 0, 0);
figureNode.physicsBody.angularVelocityFactor = SCNVector3Make(0, 1, 0);
}
– (void)createCamera {
SCNNode *camera = [SCNNode node];
camera.camera = [SCNCamera camera];
camera.camera.zFar = 160;
camera.position = SCNVector3Make(0, 30, 100);
camera.rotation = SCNVector4Make(1, 0, 0, –0.02 * M_PI);
[self.sceneView.scene.rootNode addChildNode:camera];
}
– (void)createLight {
SCNLight *light = [SCNLight light];
light.type = SCNLightTypeSpot;
light.castsShadow = YES;
SCNNode *lightNode = [SCNNode node];
lightNode.light = light;
lightNode.position = SCNVector3Make(20, 30, 70);
lightNode.rotation = SCNVector4Make(1, 0, 0, –0.35);
[self.sceneView.scene.rootNode addChildNode:lightNode];
SCNLight *ambient = [SCNLight light];
ambient.type = SCNLightTypeAmbient;
ambient.color = [UIColor colorWithWhite:0.5 alpha:1.0];
SCNNode *ambientNode = [SCNNode node];
ambientNode.light = ambient;
ambientNode.position = SCNVector3Make(0, 30, 70);
ambientNode.rotation = SCNVector4Make(1, 0, 0, –0.35);
[self.sceneView.scene.rootNode addChildNode:ambientNode];
}
– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
SCNNode *hand = [self.sceneView.scene.rootNode childNodeWithName:@”movable hand” recursively:YES];
[hand runAction:[SCNAction rotateByAngle:0.5 * M_PI aroundAxis:SCNVector3Make(1, 0, 0) duration:0.5]];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
SCNNode *figure = [self.sceneView.scene.rootNode childNodeWithName:@”figure” recursively:NO];
[figure.physicsBody applyTorque:SCNVector4Make(0, 1, 0, 50.0) impulse:YES];
});
}
#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 {
if (i > 2) return nil;
int colors[] = {0x142426, 0xD1B748, 0xFFFDBE};
return ColorHex(colors[i]);
}
@end