Created
August 16, 2015 14:56
-
-
Save mzsima/3bbb0833a6135703bec2 to your computer and use it in GitHub Desktop.
regular angled pipe
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ViewController.m | |
// RightAngledPipe | |
// | |
// Created by Mizushima Yusuke on 8/16/15. | |
// Copyright (c) 2015 Yusuke Mizusima. All rights reserved. | |
// | |
#import "ViewController.h" | |
@import SceneKit; | |
@interface ViewController () | |
@property (nonatomic, weak) SCNView *sceneView; | |
@end | |
@implementation ViewController | |
- (void)viewDidLoad { | |
[super viewDidLoad]; | |
[self setupScene]; | |
[self createPipe]; | |
[self createCamera]; | |
} | |
- (void)setupScene { | |
SCNView *sv = [[SCNView alloc] initWithFrame:self.view.bounds]; | |
sv.backgroundColor = [self color:0]; | |
sv.scene = [SCNScene scene]; | |
sv.autoenablesDefaultLighting = YES; | |
sv.allowsCameraControl = YES; | |
[self.view addSubview:sv]; | |
self.sceneView = sv; | |
} | |
- (void)createPipe { | |
SCNNode *p = [[self pipeNode] flattenedClone]; | |
p.position = SCNVector3Make(0, 6, 0); | |
[self.sceneView.scene.rootNode addChildNode:p]; | |
SCNNode *pb = [p flattenedClone]; | |
pb.position = SCNVector3Make(2.8, 3, 0); | |
[self.sceneView.scene.rootNode addChildNode:pb]; | |
SCNNode *pc = [p clone]; | |
pc.position = SCNVector3Make(5, 0, 0); | |
pc.rotation = SCNVector4Make(0, 1, 0, -M_PI * 0.5); | |
[self.sceneView.scene.rootNode addChildNode:pc]; | |
SCNNode *pd = [p clone]; | |
pd.position = SCNVector3Make(5, -3, 2.5); | |
pd.rotation = SCNVector4Make(0, 1, 0, M_PI); | |
[self.sceneView.scene.rootNode addChildNode:pd]; | |
} | |
- (SCNNode *)pipeNode { | |
SCNNode *pipeNode = [SCNNode node]; | |
for (int i=0; i<10; i++) { | |
SCNNode *ringV = [self ring]; | |
ringV.position = SCNVector3Make(0, 1.28 - i * 0.1, 0); | |
[pipeNode addChildNode:ringV]; | |
SCNNode *ringH = [self ring]; | |
ringH.rotation = SCNVector4Make(0, 0, 1, M_PI * 0.5); | |
ringH.position = SCNVector3Make(0.375 + i * 0.1, 0, 0); | |
[pipeNode addChildNode:ringH]; | |
} | |
int cnt = 20; | |
float dw = M_PI * 0.5 / cnt; | |
for (int i=0; i<=cnt; i++) { | |
float w = i * dw + M_PI; | |
float x = 0.3 * cos(w) + 0.3; | |
float y = 0.3 * sin(w) + 0.3; | |
SCNNode *r = [self ring]; | |
r.rotation = SCNVector4Make(0, 0, 1, w); | |
r.position = SCNVector3Make(x, y, 0); | |
[pipeNode addChildNode:r]; | |
} | |
pipeNode.physicsBody = [SCNPhysicsBody staticBody]; | |
pipeNode.physicsBody.physicsShape = [SCNPhysicsShape shapeWithNode:pipeNode options:@{SCNPhysicsShapeTypeKey : SCNPhysicsShapeTypeConcavePolyhedron}]; | |
pipeNode.physicsBody.friction = 0; | |
pipeNode.physicsBody.restitution = 0; | |
return pipeNode; | |
} | |
- (SCNNode *)ring { | |
int cnt = 40; | |
float r = 0.3; | |
float dw = 2.0 * M_PI / cnt; | |
SCNNode *ring = [SCNNode node]; | |
for (int i=0; i<cnt; i++) { | |
float x = r * cos(dw * i); | |
float z = r * sin(dw * i); | |
SCNBox *box = [SCNBox boxWithWidth:0.1 height:0.1 length:0.05 chamferRadius:0]; | |
box.firstMaterial.diffuse.contents = [self color:4]; | |
SCNNode *boxNode = [SCNNode nodeWithGeometry:box]; | |
boxNode.position = SCNVector3Make(x, 0, z); | |
boxNode.rotation = SCNVector4Make(0, 1, 0, -dw * i + M_PI * 0.5); | |
[ring addChildNode:boxNode]; | |
} | |
return ring; | |
} | |
- (void)createCamera { | |
SCNNode *camera = [SCNNode node]; | |
camera.camera = [SCNCamera camera]; | |
camera.position = SCNVector3Make(0, 0, 20); | |
[self.sceneView.scene.rootNode addChildNode:camera]; | |
} | |
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { | |
SCNSphere *ball = [SCNSphere sphereWithRadius:0.25]; | |
ball.firstMaterial.diffuse.contents = [self color:2]; | |
SCNNode *ballNode = [SCNNode nodeWithGeometry:ball]; | |
ballNode.position = SCNVector3Make(0, 10, 0); | |
ballNode.physicsBody = [SCNPhysicsBody dynamicBody]; | |
ballNode.physicsBody.friction = 0; | |
ballNode.physicsBody.restitution = 0; | |
[self.sceneView.scene.rootNode addChildNode:ballNode]; | |
} | |
#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 code[] = {0xF6F792, 0x333745, 0x77C4D3, 0xDAEDE2, 0xEA2E49}; | |
return ColorHex(code[i]); | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment