
箱の中に箱をいれていくiPhoneアプリのサンプルコードを描いてみます。
#import “ViewController.h”
@import SceneKit;
@interface ViewController ()
@property (nonatomic, weak) SCNView *sceneView;
@end
@implementation ViewController
– (void)viewDidLoad {
[super viewDidLoad];
[self setupScene];
[self createBox];
[self createCamera];
}
– (void)setupScene {
SCNView *sv = [[SCNView alloc] initWithFrame:self.view.bounds];
sv.backgroundColor = [self color:0];
sv.scene = [SCNScene scene];
sv.autoenablesDefaultLighting = YES;
sv.allowsCameraControl = YES;
[self.view addSubview:sv];
self.sceneView = sv;
}
– (void)createBox {
NSArray *boxSetting = @[
@{ @”x”: @0, @”y”: @0, @”z”: @0, @”size”:@10},
@{ @”x”: @(–10), @”y”: @(–10), @”z”: @10, @”size”:@8},
@{ @”x”: @0, @”y”: @(–10), @”z”: @10, @”size”:@4},
@{ @”x”: @10, @”y”: @(–10), @”z”: @10, @”size”:@6},
];
int color = 0;
for (NSDictionary *setting in boxSetting) {
float size = [setting[@”size”] floatValue];
SCNBox *plate = [SCNBox boxWithWidth:size height:size length:0.2 chamferRadius:0];
plate.firstMaterial.diffuse.contents = [self color:++color];
SCNNode *box = [SCNNode node];
box.position = SCNVector3Make([setting[@”x”] floatValue], [setting[@”y”] floatValue], [setting[@”z”] floatValue]);
[self.sceneView.scene.rootNode addChildNode:box];
for (int i=0; i<4; i++) {
SCNNode *plateNode = [SCNNode nodeWithGeometry:plate];
plateNode.transform = SCNMatrix4Rotate(plateNode.transform, M_PI_2, 0, 1, 0);
plateNode.transform = SCNMatrix4Translate(plateNode.transform, [setting[@”size”] floatValue] / 2.0, 0, 0);
plateNode.transform = SCNMatrix4Rotate(plateNode.transform, M_PI_2 * i , 0, 1, 0);
[box addChildNode:plateNode];
}
SCNNode *plateNode = [SCNNode nodeWithGeometry:plate];
plateNode.transform = SCNMatrix4Rotate(plateNode.transform, M_PI_2, 1, 0, 0);
plateNode.transform = SCNMatrix4Translate(plateNode.transform, 0, -[setting[@”size”] floatValue] / 2.0, 0);
[box addChildNode:plateNode];
box.physicsBody = [SCNPhysicsBody staticBody];
box.name = [NSString stringWithFormat:@”box%d”, color];
}
}
– (void)createCamera {
SCNNode *camera = [SCNNode node];
camera.camera = [SCNCamera camera];
camera.position = SCNVector3Make(0, 25, 20);
camera.rotation = SCNVector4Make(1, 0, 0, –M_PI * 0.32);
[self.sceneView.scene.rootNode addChildNode:camera];
}
– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
static int count = 0;
SCNNode *box = [self.sceneView.scene.rootNode childNodeWithName:[NSString stringWithFormat:@”box%@”, @[@2, @4, @3][count]] recursively:NO];
[box runAction:[SCNAction sequence:@[
[SCNAction moveTo:SCNVector3Make(box.position.x, 10, box.position.z) duration:1.0],
[SCNAction moveTo:SCNVector3Make(0, 10, 0) duration:1.0]]]
completionHandler:^{
box.physicsBody = [SCNPhysicsBody dynamicBody];
}];
count = (count + 1) % 3;
}
#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 > 4) {
return nil;
}
int colorCode[] = {0x96CEB4, 0xFFEEACD, 0xFF6F69, 0xFFCC5C, 0xAAD8B0};
return ColorHex(colorCode[i]);
}
@end