
タッチでめくるとバンザイという感じでiPhoneアプリのサンプルコードを描いてみます。
#import “ViewController.h”
@import SceneKit;
@interface ViewController ()
@property (nonatomic, weak) SCNView *sceneView;
@end
@implementation ViewController
– (void)viewDidLoad {
[super viewDidLoad];
[self setupScene];
[self createBasePicture];
[self createCoverPicture];
}
– (void)setupScene {
SCNView *sv = [[SCNView alloc] initWithFrame:self.view.bounds];
sv.backgroundColor = [UIColor colorWithHue:0.14 saturation:0.2 brightness:1 alpha:1];
sv.scene = [SCNScene scene];
sv.autoenablesDefaultLighting = YES;
[self.view addSubview:sv];
self.sceneView = sv;
}
– (void)createBasePicture {
float y0 = 0;
float y1 = 2;
float y2 = 6;
float x0 = 0;
float x1 = 4;
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointZero];
[path addLineToPoint:CGPointMake(x0, y1)];
[path addLineToPoint:CGPointMake(x1, y2)];
[path addLineToPoint:CGPointMake(x1, y0)];
[path closePath];
SCNShape *shape = [SCNShape shapeWithPath:path extrusionDepth:0.1];
shape.firstMaterial.diffuse.contents = [UIColor colorWithWhite:0.99 alpha:1];
SCNNode *base = [SCNNode nodeWithGeometry:shape];
[self.sceneView.scene.rootNode addChildNode:base];
UIBezierPath *headPath = [UIBezierPath bezierPathWithArcCenter:CGPointZero radius:0.3 startAngle:0 endAngle:2.0 * M_PI clockwise:NO];
headPath.flatness = 0.001;
SCNShape *head = [SCNShape shapeWithPath:headPath extrusionDepth:0.1];
head.firstMaterial.diffuse.contents = [UIColor blackColor];
SCNNode *headNode = [SCNNode nodeWithGeometry:head];
headNode.position = SCNVector3Make(2, 3, 0.1);
[base addChildNode:headNode];
SCNBox *hand = [SCNBox boxWithWidth:0.7 height:0.2 length:0.1 chamferRadius:0.1];
hand.firstMaterial.diffuse.contents = [UIColor blackColor];
SCNNode *handRNode = [SCNNode nodeWithGeometry:hand];
handRNode.position = SCNVector3Make(1.5, 2.9, 0.1);
handRNode.rotation = SCNVector4Make(0, 0, 1, –M_PI/3.0);
[base addChildNode:handRNode];
SCNNode *handLNode = [SCNNode nodeWithGeometry:hand];
handLNode.position = SCNVector3Make(2.5, 2.9, 0.1);
handLNode.rotation = SCNVector4Make(0, 0, 1, M_PI/3.0);
[base addChildNode:handLNode];
SCNBox *body = [SCNBox boxWithWidth:0.6 height:0.8 length:0.1 chamferRadius:0.1];
body.firstMaterial.diffuse.contents = [UIColor blackColor];
SCNNode *bodyNode = [SCNNode nodeWithGeometry:body];
bodyNode.position = SCNVector3Make(2, 2.3, 0.1);
[base addChildNode:bodyNode];
SCNBox *foot = [SCNBox boxWithWidth:0.28 height:0.6 length:0.1 chamferRadius:0.1];
foot.firstMaterial.diffuse.contents = [UIColor blackColor];
for (int i=0; i<2; i++) {
SCNNode *footNode = [SCNNode nodeWithGeometry:foot];
footNode.position = SCNVector3Make(i ? 1.85 : 2.15, 1.7, 0.1);
[base addChildNode:footNode];
}
}
– (void)createCoverPicture {
float y1 = 0;
float y2 = 4;
float x0 = 0;
float x1 = 4;
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(x0, y1)];
[path addLineToPoint:CGPointMake(x1, y2)];
[path addLineToPoint:CGPointMake(x1, y1)];
[path closePath];
SCNShape *shape = [SCNShape shapeWithPath:path extrusionDepth:0.1];
shape.firstMaterial.diffuse.contents = [UIColor colorWithWhite:0.95 alpha:1];
SCNNode *base = [SCNNode nodeWithGeometry:shape];
base.name = @”cover”;
base.position = SCNVector3Make(0, 2, 0.2);
[self.sceneView.scene.rootNode addChildNode:base];
UIBezierPath *headPath = [UIBezierPath bezierPathWithArcCenter:CGPointZero radius:0.3 startAngle:0 endAngle:2.0 * M_PI clockwise:NO];
headPath.flatness = 0.001;
SCNShape *head = [SCNShape shapeWithPath:headPath extrusionDepth:0.1];
head.firstMaterial.diffuse.contents = [UIColor blackColor];
SCNNode *headNode = [SCNNode nodeWithGeometry:head];
headNode.position = SCNVector3Make(2, 1, 0.1);
[base addChildNode:headNode];
SCNBox *hand = [SCNBox boxWithWidth:0.6 height:0.2 length:0.1 chamferRadius:0.1];
hand.firstMaterial.diffuse.contents = [UIColor blackColor];
SCNNode *handRNode = [SCNNode nodeWithGeometry:hand];
handRNode.position = SCNVector3Make(1.5, 0.4, 0.1);
handRNode.rotation = SCNVector4Make(0, 0, 1, M_PI/3.0);
[base addChildNode:handRNode];
SCNNode *handLNode = [SCNNode nodeWithGeometry:hand];
handLNode.position = SCNVector3Make(2.5, 0.4, 0.1);
handLNode.rotation = SCNVector4Make(0, 0, 1, –M_PI/3.0);
[base addChildNode:handLNode];
SCNBox *body = [SCNBox boxWithWidth:0.6 height:0.7 length:0.1 chamferRadius:0.1];
body.firstMaterial.diffuse.contents = [UIColor blackColor];
SCNNode *bodyNode = [SCNNode nodeWithGeometry:body];
bodyNode.position = SCNVector3Make(2, 0.35, 0.1);
[base addChildNode:bodyNode];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
SCNNode *cover = [self.sceneView.scene.rootNode childNodeWithName:@”cover” recursively:NO];
if (cover.rotation.w == 0)
[cover runAction:[SCNAction rotateByAngle:M_PI aroundAxis:SCNVector3Make(1, 1, 0) duration:0.5]];
else
[cover runAction:[SCNAction rotateByAngle:-M_PI aroundAxis:SCNVector3Make(1, 1, 0) duration:0.5]];
}
@end