まえならえで遊ぶiPhoneアプリのサンプルコードを描いてみます。
import UIKit
import SceneKit
class ViewController: UIViewController {
weak var sceneView : SCNView?
override func viewDidLoad() {
super.viewDidLoad()
setupScene()
createGround()
createSticker()
}
func setupScene() {
let sv = SCNView(frame: view.bounds)
sv.backgroundColor = UIColor(hue: 0.25, saturation: 0.1, brightness: 0.7, alpha: 1)
sv.scene = SCNScene()
sv.autoenablesDefaultLighting = true
view.addSubview(sv)
self.sceneView = sv
}
func createGround() {
let ground = SCNBox(width: 10, height: 0.2, length: 2, chamferRadius: 0)
ground.firstMaterial?.diffuse.contents = UIColor.brownColor()
let groundNode = SCNNode(geometry: ground)
self.sceneView?.scene?.rootNode.addChildNode(groundNode)
}
func createSticker() {
for n in (0…4) {
let sticker = SCNNode()
sticker.name = “sticker”
sticker.position = SCNVector3(x: Float(n) – 2 , y: 0.5, z: 0)
self.sceneView?.scene?.rootNode.addChildNode(sticker)
let head = SCNSphere(radius: 0.3)
head.firstMaterial?.diffuse.contents = UIColor.darkGrayColor()
let headNode = SCNNode(geometry: head)
headNode.position = SCNVector3(x: 0, y: 0.5, z: 0)
sticker.addChildNode(headNode)
let body = SCNCylinder(radius: 0.15, height: 1)
body.firstMaterial?.diffuse.contents = UIColor.darkGrayColor()
let bodyNode = SCNNode(geometry: body)
sticker.addChildNode(bodyNode)
for i in 0…1 {
let hand = SCNCylinder(radius: 0.1, height: 0.5)
hand.firstMaterial?.diffuse.contents = UIColor.darkGrayColor()
let handNode = SCNNode(geometry: hand)
handNode.name = “hand”
handNode.pivot = SCNMatrix4MakeTranslation(0, 0.23, 0)
handNode.position = SCNVector3(x:i==0 ? –0.25 : 0.25 , y: 0.2, z: 0)
sticker.addChildNode(handNode)
}
}
}
var count = 0
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
var delay : NSTimeInterval = 0
sceneView?.scene?.rootNode.childNodes
.filter { ($0 as! SCNNode).name == “sticker” }
.each { i in
i.runAction(SCNAction.sequence([
SCNAction.waitForDuration(delay),
SCNAction.rotateByAngle(CGFloat(M_PI) * 0.5, aroundAxis: SCNVector3(x: 0, y: 1, z: 0), duration: 0.5)]))
i.childNodes
.filter {($0 as! SCNNode).name == “hand”}
.each { h in
h.runAction(SCNAction.sequence([
SCNAction.waitForDuration(delay + 0.2),
SCNAction.rotateByAngle(CGFloat(M_PI) * 0.5, aroundAxis: SCNVector3(x: 1, y: 0, z: 0), duration: 0.5)]))
}
delay += 0.5
}
}
}
extension Array {
func each(doit:T->Void) {for i in self { doit(i) }}
}