上から見たら、横からみたらどう見える?投影図を確認する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 createBox];
[self createButton];
[self createCamera];
}
– (void)setupScene {
SCNView *sv = [[SCNView alloc] initWithFrame:self.view.bounds];
sv.scene = [SCNScene scene];
sv.backgroundColor = [self color:0];
sv.autoenablesDefaultLighting = YES;
[self.view addSubview:sv];
self.sceneView = sv;
}
– (void)createBox {
SCNVector3 vec[] = {SCNVector3Make(0, 0, 0),SCNVector3Make(0, 5, 0),SCNVector3Make(5, 0, 0),SCNVector3Make(-5, 0, 0)};
SCNNode *myBlock = [SCNNode node];
myBlock.name = @”my block”;
myBlock.rotation = SCNVector4Make(1, 1, 0, 0.8);
[self.sceneView.scene.rootNode addChildNode:myBlock];
for (int i=0; i<4; i++) {
SCNBox *box = [SCNBox boxWithWidth:5 height:5 length:5 chamferRadius:0];
box.firstMaterial.diffuse.contents = [self color:4];
SCNNode *boxNode = [SCNNode nodeWithGeometry:box];
boxNode.position = vec[i];
[myBlock addChildNode:boxNode];
}
}
– (void)createButton {
self.sceneView.overlaySKScene = [SKScene sceneWithSize:self.view.bounds.size];
SKSpriteNode *button1 = [SKSpriteNode spriteNodeWithColor:[self color:2] size:CGSizeMake(30, 90)];
button1.position = CGPointMake(100, 100);
button1.name = @”button1″;
[self.sceneView.overlaySKScene addChild:button1];
SKSpriteNode *button2 = [SKSpriteNode spriteNodeWithColor:[self color:3] size:CGSizeMake(90, 30)];
SKSpriteNode *n2 = [SKSpriteNode spriteNodeWithColor:[self color:3] size:CGSizeMake(30, 30)];
n2.position = CGPointMake(0, 30);
[button2 addChild:n2];
button2.position = CGPointMake(CGRectGetMidX(self.view.bounds), 80);
button2.name = @”button2″;
[self.sceneView.overlaySKScene addChild:button2];
SKSpriteNode *button3 = [SKSpriteNode spriteNodeWithColor:[self color:1] size:CGSizeMake(60, 30)];
button3.position = CGPointMake(CGRectGetMaxX(self.view.bounds) – 80, 100);
button3.name = @”button3″;
[self.sceneView.overlaySKScene addChild:button3];
}
– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint p = [[touches anyObject] locationInNode:self.sceneView.overlaySKScene];
SKNode *hit = [self.sceneView.overlaySKScene nodeAtPoint:p];
SCNNode *myblock = [self.sceneView.scene.rootNode childNodeWithName:@”my block” recursively:NO];
if ([hit.name isEqual:@”button1″]) {
[myblock runAction:[SCNAction rotateToX:M_PI/2.0 y:0 z:M_PI/2.0 duration:1.0]];
} else if ([hit.name isEqual:@”button2″]) {
[myblock runAction:[SCNAction rotateToX:0 y:0 z:0 duration:1.0]];
} else if ([hit.name isEqual:@”button3″]) {
[myblock runAction:[SCNAction rotateToX:0 y:-M_PI/2.0 z:0 duration:1.0]];
} else {
return;
}
[hit runAction:[SKAction sequence:@[[SKAction fadeAlphaTo:0.6 duration:0.2], [SKAction fadeInWithDuration:0.1]]]];
}
– (void)createCamera {
SCNNode *camera = [SCNNode node];
camera.camera = [SCNCamera camera];
camera.camera.usesOrthographicProjection = YES;
camera.camera.orthographicScale = 10.0;
camera.position = SCNVector3Make(0, 0, 30);
[self.sceneView.scene.rootNode addChildNode:camera];
}
#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[] = {0x1A1F2B, 0x30395C, 0x4A6491, 0x85A5CC, 0xD0E4F2};
return ColorHex(colorCode[i]);
}
@end