
ポーンを表示するiPhoneアプリのサンプルコードを描いてみます。
#import “ViewController.h”
@import SceneKit;
@interface ViewController ()
@property (nonatomic, weak) SCNView *sceneView;
@property (nonatomic) int state;
@end
@implementation ViewController
– (void)viewDidLoad {
[super viewDidLoad];
[self setupScene];
[self createBoard];
[self createCamera];
[self createLight];
}
– (void)setupScene {
SCNView *sv = [[SCNView alloc] initWithFrame:self.view.bounds];
sv.backgroundColor = [UIColor brownColor];
sv.scene = [SCNScene scene];
sv.allowsCameraControl = YES;
[self.view addSubview:sv];
self.sceneView = sv;
}
– (void)createBoard {
float w = 5;
SCNGeometry *whitePlate = [SCNBox boxWithWidth:w height:1 length:w chamferRadius:0];
whitePlate.firstMaterial.diffuse.contents = [UIColor whiteColor];
SCNGeometry *blackPlate = [SCNBox boxWithWidth:w height:1 length:w chamferRadius:0];
blackPlate.firstMaterial.diffuse.contents = [UIColor blackColor];
for (int i=0; i<4; i++) {
float x = (i % 2) * w;
float z = (i / 2) * w;
SCNNode *plate = [SCNNode nodeWithGeometry:(x == z) ? whitePlate : blackPlate];
plate.position = SCNVector3Make(x, 0, z);
plate.physicsBody = [SCNPhysicsBody staticBody];
[self.sceneView.scene.rootNode addChildNode:plate];
}
}
– (void)createPawnWithColor:(UIColor*)color {
SCNNode *pawn = [SCNNode node];
SCNSphere *head = [SCNSphere sphereWithRadius:1];
head.firstMaterial.diffuse.contents = color;
SCNNode *headNode = [SCNNode nodeWithGeometry:head];
headNode.position = SCNVector3Make(0, 2, 0);
[pawn addChildNode:headNode];
SCNCylinder *neck = [SCNCylinder cylinderWithRadius:1 height:0.3];
neck.firstMaterial.diffuse.contents = color;
SCNNode *neckNode = [SCNNode nodeWithGeometry:neck];
neckNode.position = SCNVector3Make(0, 1, 0);
[pawn addChildNode:neckNode];
SCNCone *body = [SCNCone coneWithTopRadius:0.5 bottomRadius:1 height:3];
body.firstMaterial.diffuse.contents = color;
SCNNode *bodyNode = [SCNNode nodeWithGeometry:body];
bodyNode.position = SCNVector3Make(0, 0, 0);
[pawn addChildNode:bodyNode];
SCNCone *base = [SCNCone coneWithTopRadius:1.5 bottomRadius:1.8 height:1];
base.firstMaterial.diffuse.contents = color;
SCNNode *baseNode = [SCNNode nodeWithGeometry:base];
baseNode.position = SCNVector3Make(0, –2, 0);
[pawn addChildNode:baseNode];
pawn.position = SCNVector3Make(0, 6, 0);
pawn.physicsBody = [SCNPhysicsBody dynamicBody];
[self.sceneView.scene.rootNode addChildNode:pawn];
}
– (void)createCamera {
SCNNode *camera = [SCNNode node];
camera.camera = [SCNCamera camera];
camera.position = SCNVector3Make(2.5, 10, 25);
camera.rotation = SCNVector4Make(1, 0, 0, –0.2);
[self.sceneView.scene.rootNode addChildNode:camera];
}
– (void)createLight {
SCNLight *light = [SCNLight light];
light.type = SCNLightTypeOmni;
SCNNode *lightNode = [SCNNode node];
lightNode.position = SCNVector3Make(0, 20, 20);
lightNode.light = light;
[self.sceneView.scene.rootNode addChildNode:lightNode];
SCNLight *light2 = [SCNLight light];
light2.type = SCNLightTypeDirectional;
SCNNode *light2Node = [SCNNode node];
light2Node.position = SCNVector3Make(0, 30, 30);
light2Node.rotation = SCNVector4Make(1, 0, 0, –0.2);
light2Node.light = light2;
[self.sceneView.scene.rootNode addChildNode:light2Node];
}
– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
if (self.state == 0) {
[self createPawnWithColor:[UIColor colorWithWhite:0.1 alpha:1]];
++self.state;
}
}
@end