雛人形っぽいiPhoneアプリのサンプルコードを描いてみます。
#import “ViewController.h”
@import SceneKit;
@interface ViewController ()
@property (nonatomic, weak) SCNView *sceneView;
@property (nonatomic) int count;
@end
@implementation ViewController
– (void)viewDidLoad {
[super viewDidLoad];
[self setupScene];
SCNNode *mochi = [self createHisimochi:[self color:3]];
mochi.physicsBody = [SCNPhysicsBody staticBody];
[self createCamera];
}
– (void)setupScene {
SCNView *sv = [[SCNView alloc] initWithFrame:self.view.bounds];
sv.scene = [SCNScene scene];
sv.backgroundColor = [self color:0];
[self.view addSubview:sv];
self.sceneView = sv;
}
– (SCNNode *)createHisimochi:(UIColor *)color {
SCNBox *box = [SCNBox boxWithWidth:40 height:3 length:40 chamferRadius:1];
box.firstMaterial.diffuse.contents = color;
box.firstMaterial.lightingModelName = SCNLightingModelLambert;
SCNNode *boxNode = [SCNNode nodeWithGeometry:box];
boxNode.transform = SCNMatrix4Rotate(boxNode.transform, M_PI/4.0, 0, 1, 0);
[self.sceneView.scene.rootNode addChildNode:boxNode];
return boxNode;
}
– (void)createFigure:(UIColor *)color position:(SCNVector3)position {
SCNSphere *s = [SCNSphere sphereWithRadius:5];
s.firstMaterial.diffuse.contents = [UIColor whiteColor];
SCNNode *sn = [SCNNode nodeWithGeometry:s];
sn.position = position;
sn.physicsBody = [SCNPhysicsBody dynamicBody];
sn.physicsBody.angularVelocityFactor = SCNVector3Zero;
[self.sceneView.scene.rootNode addChildNode:sn];
for (int i=0; i<2; i++) {
SCNSphere *eye = [SCNSphere sphereWithRadius:0.4];
eye.firstMaterial.diffuse.contents = [UIColor blackColor];
SCNNode *eyeNode = [SCNNode nodeWithGeometry:eye];
eyeNode.position = SCNVector3Make(i==0 ? –2 : 2, 2, 4);
[sn addChildNode:eyeNode];
}
SCNTube *t = [SCNTube tubeWithInnerRadius:5.1 outerRadius:5.2 height:6];
t.firstMaterial.diffuse.contents = color;
SCNNode *tn = [SCNNode nodeWithGeometry:t];
tn.position = SCNVector3Make(position.x, position.y + 10, position.z);
tn.physicsBody = [SCNPhysicsBody dynamicBody];
tn.physicsBody.physicsShape = [SCNPhysicsShape shapeWithGeometry:t options:@{SCNPhysicsShapeTypeKey : SCNPhysicsShapeTypeConcavePolyhedron}];
[self.sceneView.scene.rootNode addChildNode:tn];
}
– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
self.count++;
switch (self.count) {
case 1: {
SCNNode *mochi = [self createHisimochi:[self color:4]];
mochi.transform = SCNMatrix4Translate(mochi.transform, 0, 20, 0);
mochi.physicsBody = [SCNPhysicsBody dynamicBody];
break;
}
case 2: {
SCNNode *mochi = [self createHisimochi:[self color:5]];
mochi.transform = SCNMatrix4Translate(mochi.transform, 0, 20, 0);
mochi.physicsBody = [SCNPhysicsBody dynamicBody];
break;
}
case 3: {
[self createFigure:[self color:1] position:SCNVector3Make(10, 30, 0)];
break;
}
case 4: {
[self createFigure:[self color:2] position:SCNVector3Make(-10, 30, 0)];
break;
}
default:
break;
}
}
– (void)createCamera {
SCNNode *n = [SCNNode node];
n.camera = [SCNCamera camera];
n.camera.zFar = 200;
n.position = SCNVector3Make(-10, 30, 90);
n.rotation = SCNVector4Make(1, 0.3, 0, –0.30);
[self.sceneView.scene.rootNode addChildNode:n];
}
#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 > 5) return nil;
int colorCode[] = {0xB71C1C, 0xE91E63, 0x3F51B5, 0xFCE4EC, 0x9CCC65, 0xF48FB1};
return ColorHex(colorCode[i]);
}
@end