カメラで箱に寄ったり離れたりするiPhoneアプリのサンプルコードを描いてみます。
#import “ViewController.h”
@import SceneKit;
@import SpriteKit;
@interface ViewController ()
@property (nonatomic, weak) SCNView *sceneView;
@end
@implementation ViewController
– (void)viewDidLoad {
[super viewDidLoad];
[self setupScene];
[self createObjects];
[self createLight];
[self createCamera];
[self createConsole];
}
– (void)setupScene {
SCNView *sv = [[SCNView alloc] initWithFrame:self.view.bounds];
sv.scene = [SCNScene scene];
sv.backgroundColor = [UIColor colorWithWhite:0.4 alpha:1];
[self.view addSubview:sv];
self.sceneView = sv;
}
– (void)createObjects {
SCNNode *localSpace = [SCNNode node];
[localSpace runAction:[SCNAction repeatActionForever:[SCNAction rotateByX:1 y:0 z:1 duration:1.0]]];
[self.sceneView.scene.rootNode addChildNode:localSpace];
SCNVector3 v[] = {
SCNVector3Make(1, 0, 0),SCNVector3Make(-1, 0, 0),SCNVector3Make(0, 1, 0),SCNVector3Make(0, –1, 0),SCNVector3Make(0, 0, 1),SCNVector3Make(0, 0, –1)};
float r = 15;
for (int i=0; i< 6; i++) {
SCNBox *box = [SCNBox boxWithWidth:5 height:5 length:5 chamferRadius:0];
box.firstMaterial.diffuse.contents = [[self color:(i % 3) + 1] colorWithAlphaComponent:0.8];
box.firstMaterial.specular.contents = [self color:4];
box.firstMaterial.shininess = 1.0;
SCNNode *boxNode = [SCNNode nodeWithGeometry:box];
boxNode.position = SCNVector3Make(r * v[i].x, r * v[i].y, r * v[i].z);
// ^ xor
[boxNode runAction:[SCNAction repeatActionForever:[SCNAction rotateByX:(abs(v[i].x) ^ 0x1) * M_PI y:(abs(v[i].y) ^ 0x1) * M_PI z:(abs(v[i].z) ^ 0x1) * M_PI duration:1.0]]];
[localSpace addChildNode:boxNode];
}
}
– (void)createLight {
SCNLight *light = [SCNLight light];
light.type = SCNLightTypeOmni;
SCNNode *lightNode = [SCNNode node];
lightNode.light = light;
lightNode.position = SCNVector3Make(0, 0, 40);
[self.sceneView.scene.rootNode addChildNode:lightNode];
}
– (void)createCamera {
SCNNode *camera = [SCNNode node];
camera.name = @”camera”;
camera.camera = [SCNCamera camera];
camera.camera.zFar = 300;
camera.position = SCNVector3Make(0, 0, 200);
[self.sceneView.scene.rootNode addChildNode:camera];
}
– (void)createConsole {
self.sceneView.overlaySKScene = [SKScene sceneWithSize:self.view.bounds.size];
SKSpriteNode *console = [SKSpriteNode spriteNodeWithColor:[self color:4] size:CGSizeMake(CGRectGetMaxX(self.view.bounds) – 40, 100)];
console.name = @”console”;
console.position = CGPointMake(CGRectGetMidX(self.view.bounds), 70);
console.alpha = 0.8;
[self.sceneView.overlaySKScene addChild:console];
SKNode *cameraIcon = [SKNode node];
cameraIcon.name = @”camera icon”;
SKSpriteNode *body = [SKSpriteNode spriteNodeWithColor:[self color:0] size:CGSizeMake(40, 32)];
body.position = CGPointMake(-25, 0);
[cameraIcon addChild:body];
SKSpriteNode *rens = [SKSpriteNode spriteNodeWithColor:[self color:0] size:CGSizeMake(10, 26)];
rens.position = CGPointMake(5, 0);
[cameraIcon addChild:rens];
cameraIcon.position = CGPointMake(-CGRectGetMidX(self.view.bounds) + 100, 0);
[console addChild:cameraIcon];
SKSpriteNode *target = [SKSpriteNode spriteNodeWithColor:[self color:1] size:CGSizeMake(20, 20)];
target.zRotation = M_PI / 4.0;
target.position = CGPointMake(CGRectGetMidX(self.view.bounds) – 50, 0);
[console addChild:target];
}
– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint p = [[touches anyObject] locationInNode:self.sceneView.overlaySKScene];
SKNode *n = [self.sceneView.overlaySKScene nodeAtPoint:p];
if ([n.name isEqual:@”console”]) {
SKNode *cameraIcon = [n childNodeWithName:@”camera icon”];
[cameraIcon runAction:[SKAction moveTo:CGPointMake([n convertPoint:p fromNode:n.parent].x, 0) duration:1.0]];
float rate = 200.0 / CGRectGetMaxX(self.view.bounds);
SCNNode *camera = [self.sceneView.scene.rootNode childNodeWithName:@”camera” recursively:NO];
[camera runAction:[SCNAction moveTo:SCNVector3Make(0, 0, 200 – p.x * rate + 20) duration:1.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 {
if (i > 4) return nil;
int colorCode[] = {0x000000, 0xFFE547, 0x172C99, 0xFF5C00, 0xFFFFFF};
return ColorHex(colorCode[i]);
}
@end