コーンの周りをライティングするiPhoneアプリのサンプルコードを描いてみます。
#import “ViewController.h”
@import SceneKit;
@interface ViewController ()
@property (nonatomic, weak) SCNView *sceneView;
@end
@implementation ViewController
– (void)viewDidLoad {
[super viewDidLoad];
[self setupScene];
[self createCone];
[self createCamera];
}
– (void)setupScene {
SCNView *sv = [[SCNView alloc] initWithFrame:self.view.bounds];
sv.scene = [SCNScene scene];
sv.backgroundColor = [self color:1];
[self.view addSubview:sv];
sv.allowsCameraControl = YES;
sv.autoenablesDefaultLighting = YES;
self.sceneView = sv;
}
– (void)createCone {
SCNCone *cone = [SCNCone coneWithTopRadius:10 bottomRadius:20 height:12];
cone.firstMaterial.diffuse.contents = [self color:0];
SCNNode *coneNode = [SCNNode nodeWithGeometry:cone];
[self.sceneView.scene.rootNode addChildNode:coneNode];
int count = 10;
float angle = 2.0 * M_PI / count;
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(-1, –4, 2, 8) cornerRadius:1];
for (int i=0; i<count; i++) {
SCNShape *s = [SCNShape shapeWithPath:path extrusionDepth:0.01];
s.firstMaterial.diffuse.contents = [self color:1];
s.firstMaterial.specular.contents = [self color:2];
SCNNode *sNode = [SCNNode nodeWithGeometry:s];
sNode.name = [NSString stringWithFormat:@”lignt%d”, i];
sNode.position = SCNVector3Make(0, 0, 15.01);
sNode.rotation = SCNVector4Make(1, 0, 0, –atan2(20, 24));
sNode.transform = SCNMatrix4Rotate(sNode.transform, angle * i, 0, 1, 0);
[self.sceneView.scene.rootNode addChildNode:sNode];
[sNode runAction:
[SCNAction sequence:@[
[SCNAction waitForDuration:i * 0.2],
[SCNAction repeatActionForever:
[SCNAction sequence:@[
[SCNAction runBlock:^(SCNNode *node) {
node.geometry.firstMaterial.diffuse.contents = [self color:3];
}],
[SCNAction waitForDuration:0.2],
[SCNAction runBlock:^(SCNNode *node) {
node.geometry.firstMaterial.diffuse.contents = [self color:2];
}],
[SCNAction waitForDuration:1.8]
]]]]]];
}
}
– (void)createCamera {
SCNNode *camera = [SCNNode node];
camera.camera = [SCNCamera camera];
camera.position = SCNVector3Make(0, 0, 80);
[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
{
switch (i) {
case 0: return ColorHex(0x263248);
case 1: return ColorHex(0x7E8AA2);
case 2: return ColorHex(0xFFFFFF);
case 3: return ColorHex(0xFF9800);
}
return nil;
}
@end