
ペンローズの三角形を表示するiPhoneアプリのサンプルコード
#import “ViewController.h”
@import SceneKit;
@interface ViewController ()
@property (nonatomic, weak) SCNView *sceneView;
@end
@implementation ViewController
– (void)viewDidLoad {
[super viewDidLoad];
[self setupScene];
[self createTriangle];
[self createCamera];
}
– (void)setupScene {
SCNView *sv = [[SCNView alloc] initWithFrame:self.view.bounds];
sv.backgroundColor = [UIColor blackColor];
sv.scene = [SCNScene scene];
sv.allowsCameraControl = YES;
sv.autoenablesDefaultLighting = YES;
[self.view addSubview:sv];
self.sceneView = sv;
}
– (void)createTriangle {
SCNBox *barA = [SCNBox boxWithWidth:10 height:1 length:1 chamferRadius:0];
barA.firstMaterial.diffuse.contents = [SCNNode nodeWithGeometry:barA];
barA.firstMaterial.specular.contents = [UIColor whiteColor];
SCNNode *barANode = [SCNNode nodeWithGeometry:barA];
[self.sceneView.scene.rootNode addChildNode:barANode];
SCNBox *barB = [SCNBox boxWithWidth:10 height:1 length:1 chamferRadius:0];
barB.firstMaterial.diffuse.contents = [SCNNode nodeWithGeometry:barB];
SCNNode *barBNode = [SCNNode nodeWithGeometry:barB];
barBNode.pivot = SCNMatrix4Translate(barBNode.transform, 4.5, 0, 0);
barBNode.transform = SCNMatrix4Rotate(barBNode.transform, M_PI/2.0, 0, 0, 1);
barBNode.transform = SCNMatrix4Translate(barBNode.transform, –4.5, 0, 0);
[self.sceneView.scene.rootNode addChildNode:barBNode];
SCNBox *barC = [SCNBox boxWithWidth:6.5 height:1 length:1 chamferRadius:0];
barC.firstMaterial.diffuse.contents = [SCNNode nodeWithGeometry:barC];
SCNNode *barCNode = [SCNNode nodeWithGeometry:barC];
barCNode.pivot = SCNMatrix4Translate(barCNode.transform, 3.5, 0, 0);
barCNode.transform = SCNMatrix4Rotate(barCNode.transform, M_PI/2.0, 0, 1, 0);
barCNode.transform = SCNMatrix4Translate(barCNode.transform, 4.5, 0, 0);
[self.sceneView.scene.rootNode addChildNode:barCNode];
SCNBox *plateC1 = [SCNBox boxWithWidth:2 height:1 length:0.1 chamferRadius:0];
plateC1.firstMaterial.diffuse.contents = [UIColor whiteColor];
SCNNode *plateC1Node = [SCNNode nodeWithGeometry:plateC1];
plateC1Node.position = SCNVector3Make(-4, 0, 0.5);
[barCNode addChildNode:plateC1Node];
SCNBox *plateC2 = [SCNBox boxWithWidth:0.1 height:1 length:1 chamferRadius:0];
plateC2.firstMaterial.diffuse.contents = [UIColor whiteColor];
SCNNode *plateC2Node = [SCNNode nodeWithGeometry:plateC2];
plateC2Node.position = SCNVector3Make(-5, 0, 0);
[barCNode addChildNode:plateC2Node];
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(0, 1)];
[path addLineToPoint:CGPointMake(1, 0)];
[path addLineToPoint:CGPointMake(1, 1)];
[path closePath];
SCNShape *shape = [SCNShape shapeWithPath:path extrusionDepth:0.1];
shape.firstMaterial.diffuse.contents = [UIColor whiteColor];
SCNNode *triangle = [SCNNode nodeWithGeometry:shape];
triangle.position = SCNVector3Make(-4.2, 0.5, –0.5);
triangle.rotation = SCNVector4Make(1, 0, 0, M_PI * 0.5);
[barCNode addChildNode:triangle];
}
– (void)createCamera {
SCNNode *camera = [SCNNode node];
camera.name = @”camera”;
camera.camera = [SCNCamera camera];
camera.camera.zNear = 100;
camera.camera.zFar = 150;
camera.camera.yFov = 15;
camera.position = SCNVector3Make(0, 0, 120);
[self.sceneView.scene.rootNode addChildNode:camera];
}
@end