
ブロックをうねうねさせるiPhoneアプリのサンプルコードを描いてみます。
#import “ViewController.h”
@import SceneKit;
@interface ViewController () <SCNSceneRendererDelegate>
@property (nonatomic, weak) SCNView *sceneView;
@property (nonatomic) int count;
@end
@implementation ViewController
– (void)viewDidLoad {
[super viewDidLoad];
[self setupScene];
[self createWave];
[self createLight];
[self createCamera];
}
– (void)setupScene {
SCNView *sv = [[SCNView alloc] initWithFrame:self.view.bounds];
sv.scene = [SCNScene scene];
sv.backgroundColor = [self color:0];
sv.allowsCameraControl = YES;
sv.delegate = self;
[self.view addSubview:sv];
self.sceneView = sv;
}
– (void)createWave {
for (int i=0; i<4; i++) {
SCNBox *box = [SCNBox boxWithWidth:2 height:2 length:2 chamferRadius:0.2];
box.firstMaterial.diffuse.contents = [UIColor orangeColor];//[self color:1];
box.firstMaterial.specular.contents = [self color:3];
SCNNode *boxNode = [SCNNode nodeWithGeometry:box];
boxNode.position = SCNVector3Make(4 – i * 2, 0, 0);
[self.sceneView.scene.rootNode addChildNode:boxNode];
SCNAction *up = [SCNAction moveByX:0 y:2 z:0 duration:1.3];
up.timingMode = SCNActionTimingModeEaseInEaseOut;
SCNAction *down = [SCNAction moveByX:0 y:-0.5 z:0 duration:0.7];
down.timingMode = SCNActionTimingModeEaseInEaseOut;
[boxNode runAction:
[SCNAction repeatAction:
[SCNAction group:@[[SCNAction sequence:@[up, down]],
[SCNAction moveByX:0 y:0 z:-4 duration:2]]] count:3] completionHandler:^{
[boxNode removeFromParentNode];
}];
}
}
– (void)createLight {
SCNLight *light = [SCNLight light];
light.type = SCNLightTypeSpot;
SCNNode *lightNode = [SCNNode node];
lightNode.light = light;
lightNode.position = SCNVector3Make(0, 4, 20);
[self.sceneView.scene.rootNode addChildNode:lightNode];
}
– (void)createCamera {
SCNNode *camera = [SCNNode node];
camera.camera = [SCNCamera camera];
camera.position = SCNVector3Make(0, 0, 30);
[self.sceneView.scene.rootNode addChildNode:camera];
}
– (void)renderer:(id<SCNSceneRenderer>)aRenderer updateAtTime:(NSTimeInterval)time
{
if (self.count == 29) {
[self createWave];
}
self.count = (self.count + 1) % 30;
}
#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[] = {0x303030, 0x141414, 0x1C1919, 0x191716, 0x24201F};
return ColorHex(colorCode[i]);
}
@end