トランペットをピコピコしてるかんじのiPhoneアプリのサンプルコードを描いてみます。
import UIKit
import SceneKit
class ViewController: UIViewController {
weak var sceneView : SCNView?
override func viewDidLoad() {
super.viewDidLoad()
setupScene()
createTrumpet()
createPiston()
}
func setupScene() {
let sv = SCNView(frame: view.bounds)
sv.backgroundColor = UIColor(hue: 0, saturation: 0.2, brightness: 1, alpha: 1)
sv.scene = SCNScene()
sv.allowsCameraControl = true
sv.autoenablesDefaultLighting = true
view.addSubview(sv)
sceneView = sv
}
func createTrumpet() {
var trumpet = SCNNode()
sceneView?.scene?.rootNode.addChildNode(trumpet)
for var i=0; i<5; i++ {
let geo = SCNTube(innerRadius: CGFloat(i) + 0.5, outerRadius: CGFloat(i) + 1.5, height: 0.5)
geo.firstMaterial?.diffuse.contents = UIColor.yellowColor()
let node = SCNNode(geometry: geo)
node.rotation = SCNVector4(x: 0, y: 0, z: 1, w: Float(M_PI) / 2.0)
node.position = SCNVector3(x: Float(i) * 0.5 + 2, y: 0, z: 0)
trumpet.addChildNode(node)
}
let bar = SCNTube(innerRadius: 0.5, outerRadius: 0.8, height: 16)
bar.firstMaterial?.diffuse.contents = UIColor.yellowColor()
let barNode = SCNNode(geometry: bar)
barNode.position = SCNVector3(x: –6, y: 0, z: 0)
barNode.rotation = SCNVector4(x: 0, y: 0, z: 1, w: Float(M_PI) * 0.5)
sceneView?.scene?.rootNode.addChildNode(barNode)
let bar2 = SCNTube(innerRadius: 0.3, outerRadius: 0.6, height: 10)
bar2.firstMaterial?.diffuse.contents = UIColor.yellowColor()
let bar2Node = SCNNode(geometry: bar2)
bar2Node.position = SCNVector3(x: –5, y: –3, z: 0)
bar2Node.rotation = SCNVector4(x: 0, y: 0, z: 1, w: Float(M_PI) * 0.5)
sceneView?.scene?.rootNode.addChildNode(bar2Node)
for var i=0; i<2; i++ {
let b = SCNTube(innerRadius: 0.3, outerRadius: 0.6, height: 3)
b.firstMaterial?.diffuse.contents = UIColor.yellowColor()
let n = SCNNode(geometry: b)
n.position = SCNVector3(x: Float(i) * 10.0 – 10.0, y: –2, z: 0)
sceneView?.scene?.rootNode.addChildNode(n)
}
for var i=0; i<5; i++ {
let geo = SCNTube(innerRadius: 0.3 * CGFloat(i) + 0.5, outerRadius: 0.3 * CGFloat(i) + 1.0, height: 0.5)
geo.firstMaterial?.diffuse.contents = UIColor.yellowColor()
let node = SCNNode(geometry: geo)
node.rotation = SCNVector4(x: 0, y: 0, z: 1, w: Float(M_PI) / 2.0)
node.position = SCNVector3(x: –13 – Float(i) * 0.4, y: 0, z: 0)
trumpet.addChildNode(node)
}
for var i=0; i<3; i++ {
let geo = SCNCylinder(radius: 0.8, height: 6)
geo.firstMaterial?.diffuse.contents = UIColor.yellowColor()
let node = SCNNode(geometry: geo)
node.position = SCNVector3(x:-1.7 * Float(i) – 3, y: –1.5, z: 0)
trumpet.addChildNode(node)
}
}
func createPiston() {
for var i=0; i<3; i++ {
let piston = SCNCylinder(radius: 0.8, height: 0.3)
piston.firstMaterial?.diffuse.contents = UIColor.lightGrayColor()
let pistonNode = SCNNode(geometry: piston)
pistonNode.name = “piston\(i)”
pistonNode.position = SCNVector3(x: –1.8 * Float(i) – 2.9, y: 2.5, z: 0)
let bar = SCNCylinder(radius: 0.3, height: 4)
bar.firstMaterial?.diffuse.contents = UIColor.lightGrayColor()
let barNode = SCNNode(geometry: bar)
barNode.position = SCNVector3(x: 0, y: –2, z: 0)
pistonNode.addChildNode(barNode)
sceneView?.scene?.rootNode.addChildNode(pistonNode)
}
}
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
let n = arc4random_uniform(3)
if let piston = sceneView?.scene?.rootNode.childNodeWithName(“piston\(n)”, recursively: false) {
piston.runAction(SCNAction.sequence([SCNAction.moveTo(SCNVector3(x: piston.position.x, y: 1.8, z: 0), duration: 0.3), SCNAction.waitForDuration(0.5), SCNAction.moveTo(SCNVector3(x: piston.position.x, y: 2.5, z: 0), duration: 0.3)]))
}
}
}