
薄くした球体と真ん中をくりぬいた円柱で影絵遊びなiPhoneアプリのサンプルコードを描いてみます。
#import “ViewController.h”
@import SceneKit;
@interface ViewController ()
@property (nonatomic, weak) SCNView *sceneView;
@property (nonatomic, weak) SCNScene *scene;
@property (nonatomic, weak) SCNNode *select;
@end
@implementation ViewController
– (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor blackColor];
[self setupScene];
[self createCamera];
[self createLight];
[self createObject];
}
– (void)setupScene {
float w = CGRectGetMaxX(self.view.bounds);
SCNView *sv = [[SCNView alloc] initWithFrame:CGRectMake(0, 0, w, w)];
sv.backgroundColor = [UIColor yellowColor];
sv.scene = [SCNScene scene];
[self.view addSubview:sv];
self.scene = sv.scene;
self.sceneView = sv;
SCNBox *box = [SCNBox boxWithWidth:50 height:50 length:1 chamferRadius:0];
box.firstMaterial.diffuse.contents = [UIColor whiteColor];
SCNNode *boxNode = [SCNNode nodeWithGeometry:box];
boxNode.position = SCNVector3Make(0, 0, 0);
[sv.scene.rootNode addChildNode:boxNode];
}
– (void)createCamera {
SCNNode *camera = [SCNNode node];
camera.camera = [SCNCamera camera];
camera.position = SCNVector3Make(0, 0, 60);
[self.scene.rootNode addChildNode:camera];
}
– (void)createLight {
SCNLight *light = [SCNLight light];
light.type = SCNLightTypeSpot;
light.castsShadow = YES;
light.zNear = 0;
light.spotOuterAngle = 180.0;
SCNNode *lightNode = [SCNNode node];
lightNode.light = light;
lightNode.position = SCNVector3Make(0, –6, 48);
[self.scene.rootNode addChildNode:lightNode];
}
– (void)createObject {
for (int i=0; i<2; i++) {
SCNSphere *ball = [SCNSphere sphereWithRadius:1];
ball.firstMaterial.diffuse.contents = [[UIColor whiteColor] colorWithAlphaComponent:0.5];
SCNNode *ballNode = [SCNNode nodeWithGeometry:ball];
ballNode.name = @”obj”;
ballNode.position = SCNVector3Make(-2 + i * 4.0, –4, 40);
ballNode.transform = SCNMatrix4Scale(ballNode.transform, 1.0, 1.0, 0.1);
[self.scene.rootNode addChildNode:ballNode];
}
SCNTube *tube = [SCNTube tubeWithInnerRadius:0.3 outerRadius:0.55 height:1];
tube.firstMaterial.diffuse.contents = [[UIColor redColor] colorWithAlphaComponent:0.5];
SCNNode *tubeNode = [SCNNode nodeWithGeometry:tube];
tubeNode.name = @”obj”;
tubeNode.position = SCNVector3Make(1, –5, 44);
[self.scene.rootNode addChildNode:tubeNode];
}
– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint p = [[touches anyObject] locationInView:self.sceneView];
NSArray *hits = [self.sceneView hitTest:p options:@{SCNHitTestSortResultsKey:@YES}];
[self.scene.rootNode.childNodes enumerateObjectsUsingBlock:^(SCNNode *obj, NSUInteger idx, BOOL *stop) {
obj.opacity = 1.0;
[obj removeAllActions];
}];
SCNHitTestResult *hit = hits.firstObject;
if ([hit.node.name isEqual:@”obj”]) {
if (hit.node == self.select) {
SCNMatrix4 trans = hit.node.transform;
hit.node.transform = SCNMatrix4Rotate(SCNMatrix4Identity, M_PI/10.0, 1, 0, 0);
hit.node.transform = SCNMatrix4Mult(hit.node.transform, trans);
} else {
SCNAction *flash = [SCNAction sequence:@[[SCNAction fadeInWithDuration:0.1], [SCNAction fadeOutWithDuration:0.5]]];
[hit.node runAction:[SCNAction repeatActionForever:flash]];
self.select = hit.node;
}
}
}
– (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint p = [[touches anyObject] locationInView:self.sceneView];
SCNVector3 o = [self.sceneView projectPoint:self.select.position];
self.select.position = [self.sceneView unprojectPoint:SCNVector3Make(p.x, p.y, o.z)];
}
@end