SceneKit Training for OSX by Swift (2): Default Objects
基本的なオブジェクトを表示してみる
Box
func createCube() -> SCNNode {
let cube = SCNBox(width: 1, height: 1, length: 1, chamferRadius: 0)
let cubeNode = SCNNode(geometry: cube)
let rotateDegree : CGFloat = CGFloat(M_PI_4) / 2
cubeNode.transform = SCNMatrix4MakeRotation(1.0, rotateDegree, rotateDegree, 0)
return cubeNode
}
Capsule
func createCapsule() -> SCNNode {
let capsule = SCNCapsule(capRadius: 1, height: 4)
let capsuleNode = SCNNode(geometry: capsule)
return capsuleNode
}
capsuleは height >= capRadius * 2 出ないとうまく表示できないので注意.
Cone
func createCone() -> SCNNode {
let cone = SCNCone(topRadius: 1, bottomRadius: 2, height: 2)
let coneNode = SCNNode(geometry: cone)
let rotateDegree : CGFloat = CGFloat(M_PI_4) / 2
coneNode.transform = SCNMatrix4MakeRotation(1.0, rotateDegree, rotateDegree, 0)
return coneNode
}
topRadius = bottomRadiusならば円柱形、topRadius = 0 ならば円錐形になる.
Cylinder
func createCylinder() -> SCNNode {
let cylinder = SCNCylinder(radius: 1.0, height: 2.0)
let cylinderNode = SCNNode(geometry: cylinder)
let rotateDegree : CGFloat = CGFloat(M_PI_4) / 2
cylinderNode.transform = SCNMatrix4MakeRotation(1.0, rotateDegree, rotateDegree, 0)
return cylinderNode
}
円柱。SCNConeでも表現できる。
Floor
無限に広がる床.
floorはそのままだと遠近感がつかめないので、傾けて対象にcubeを表示。
func createFloor() -> SCNNode {
let floor = SCNFloor()
let floorNode = SCNNode(geometry: floor)
let rotateDegree : CGFloat = CGFloat(M_PI_4)
floorNode.transform = SCNMatrix4MakeRotation(rotateDegree, 0.1, 1, 0)
return floorNode
}
Plane
有限の板.
planeはxy平面に表示され、floorがzy平面に表示されるのと異なる。
func createPlane() -> SCNNode {
let plane = SCNPlane(width: 1.5, height: 1.5)
plane.firstMaterial!.doubleSided = true
plane.firstMaterial!.diffuse.contents = NSColor.darkGrayColor()
let planeNode = SCNNode(geometry: plane)
return planeNode
}
firstMaterial.diffuse.contentsに色を設定できる。
pyramid
四角錐.
func createPyramid() -> SCNNode {
let pyramid = SCNPyramid(width: 1.5, height: 1.5, length: 1.5)
pyramid.firstMaterial!.doubleSided = true
pyramid.firstMaterial!.diffuse.contents = NSColor.darkGrayColor()
let pyramidNode = SCNNode(geometry: pyramid)
let rotateDegree : CGFloat = CGFloat(M_PI_4)
pyramidNode.transform = SCNMatrix4MakeRotation(rotateDegree, 1, 0, 0)
return pyramidNode
}
pyramidもfirstMaterial.diffuse.contentsで色を指定しないと表示されない.
Shape
shapeは任意の2次元の形。
func createShape() -> SCNNode {
let path = NSBezierPath()
let step : CGFloat = CGFloat(M_PI * 2 / 5)
let scale : CGFloat = CGFloat(0.4)
path.moveToPoint(NSMakePoint(cos(0), sin(0)))
path.lineToPoint(NSMakePoint(scale * cos(step * 0.5), scale * sin(step * 0.5)))
let i : CGFloat = 0
for i in 1...4 {
let pos : CGFloat = CGFloat(step * CGFloat(i))
let pos_inner : CGFloat = CGFloat(step * CGFloat(CGFloat(i) + 0.5))
path.lineToPoint(NSMakePoint(cos(pos), sin(pos)))
path.lineToPoint(NSMakePoint(scale * cos(pos_inner), scale * sin(pos_offset)))
}
path.closePath()
let shape = SCNShape(path: path, extrusionDepth: 1.0)
shape.firstMaterial!.diffuse.contents = NSColor.darkGrayColor()
let shapeNode = SCNNode(geometry: shape)
return shapeNode
}
shapeもmaterial.diffuse.contentsで色をつけないと表示されない。
Sphere
球。
func createSphere() -> SCNNode {
let sphere = SCNSphere(radius: 1)
let sphereNode = SCNNode(geometry: sphere)
return sphereNode
}
Torus
トーラス、円環面、ドーナッツ型。
func createTorus() -> SCNNode {
let torus = SCNTorus(ringRadius: 1.0, pipeRadius: 0.4)
let torusNode = SCNNode(geometry: torus)
torusNode.transform = SCNMatrix4MakeRotation(CGFloat(M_PI_4), 1, 0, 0)
return torusNode
}
Tube
チューブ
簡単にトイレットペーパーオブジェクトが作れる
func createTube() -> SCNNode {
let tube = SCNTube(innerRadius: 0.5, outerRadius: 1.0, height: 2)
let tubeNode = SCNNode(geometry: tube)
tubeNode.transform = SCNMatrix4MakeRotation(CGFloat(M_PI_4 * 2 / 3), 1, 0, 0)
return tubeNode
}
Text
文字.
func createText() -> SCNNode {
let text = SCNText(string: "Hello Text", extrusionDepth: 2)
text.font = NSFont(name: "Arial", size: 1.0)
text.chamferRadius = 0.3
text.flatness = 0.1
text.firstMaterial!.diffuse.contents = NSColor.darkGrayColor()
text.alignmentMode = kCAAlignmentCenter
text.containerFrame = CGRectMake(-2.4, 0.0, 5, 1)
let textNode = SCNNode(geometry: text)
let rotateDegree : CGFloat = CGFloat(M_PI_4)
return textNode
}
alignmentModeで指定したアラインメントを実現するためには、containerFrameの大きさの設定が必要な点に注意。