iPhoneぱっちの盛り

なんか、たまごっちブームが家の子にやってきた、ぱっちグッズが欲しい。という想いでiPhoneアプリのサンプルコードを描いてみます。

import UIKit

import SceneKit

class ViewController: UIViewController {

    weak var sceneView : SCNView?

    

    override func viewDidLoad() {

        super.viewDidLoad()

        setupScene()

        createPatch()

        createCamera()

        createLight()

        createDish()

    }

    func setupScene() {

        sceneView = {

            let sv = SCNView(frame: self.view.bounds)

            sv.scene = SCNScene()

            sv.backgroundColor = UIColor(hue: 0.3, saturation: 0.1, brightness: 1, alpha: 1)

            sv.allowsCameraControl = true

            self.view.addSubview(sv)

            return sv

        }()

    }

    

    func createPatch() {

        let body = SCNBox(width: 4, height: 5, length: 4, chamferRadius: 1.8)

        body.firstMaterial?.diffuse.contents = UIColor(hue: 0.4, saturation: 0.4, brightness: 0.9, alpha: 1)

        let bodyNode = SCNNode(geometry: body)

        let x = Float(arc4random_uniform(10)) – 5

        bodyNode.position = SCNVector3(x: x, y: 30, z: 0)

        sceneView?.scene?.rootNode .addChildNode(bodyNode)

        

        [SCNVector3(x:-0.8, y:1.4, z:1.9), SCNVector3(x:0.8, y:1.4, z:1.9)].each { p -> Void in

            let eye = SCNCylinder(radius: 0.22, height: 0.1)

            eye.firstMaterial?.diffuse.contents = UIColor(white: 0.1, alpha: 1)

            let eyeNode = SCNNode(geometry: eye)

            eyeNode.position = p

            eyeNode.rotation = SCNVector4Make(1, 0, 0, M_PI * 0.5)

            bodyNode.addChildNode(eyeNode)

        }

        

        [SCNVector3(x: 0, y: 0.7, z: 2), SCNVector3(x: 0, y: 0.2, z: 2)].each { p -> Void in

            let lip = SCNBox(width: 1.4, height: 0.5, length: 1, chamferRadius: 0.2)

            lip.firstMaterial?.diffuse.contents = body.firstMaterial?.diffuse.contents

            let lipNode = SCNNode(geometry: lip)

            lipNode.position = p

            bodyNode.addChildNode(lipNode)

        }

        

        bodyNode.physicsBody = SCNPhysicsBody.dynamicBody()

        bodyNode.physicsBody?.restitution = 0.5

    }

    

    func createDish() {

        let dish = SCNNode()

        dish.position = SCNVector3(x: 0, y: –10, z: 0)

        sceneView?.scene?.rootNode.addChildNode(dish)

        

        [Int](010).each { i -> Void in

            var part : SCNGeometry?

            if i == 0 {

                part = SCNCylinder(radius: 10, height: 0.8)

            } else {

                part = SCNTube(innerRadius: 9 + CGFloat(i), outerRadius: 10 + CGFloat(i), height: 0.8)

            }

            part?.firstMaterial?.diffuse.contents = UIColor(hue: 0.14, saturation: 0.3, brightness: 0.6, alpha: 1)

            let partNode = SCNNode(geometry: part!)

            partNode.position = SCNVector3(x: 0, y: Float(i) * 0.8, z: 0)

            dish.addChildNode(partNode)

        }

        

        dish.physicsBody = SCNPhysicsBody.staticBody()

        dish.physicsBody?.physicsShape = SCNPhysicsShape(node: dish, options: [SCNPhysicsShapeTypeKey : SCNPhysicsShapeTypeConcavePolyhedron])

    }

    

    func createCamera() {

        let camera = SCNNode()

        camera.camera = SCNCamera()

        camera.position = SCNVector3(x: 0, y: 10, z: 80)

        sceneView?.scene?.rootNode.addChildNode(camera)

    }

    

    func createLight() {

        for (type, white : CGFloat) in [(SCNLightTypeOmni, 0.4), (SCNLightTypeAmbient, 0.7)] {

            let light = SCNLight()

            light.type = type

            light.color = UIColor(white: white, alpha: 1)

            let lightNode = SCNNode()

            lightNode.light = light

            lightNode.position = SCNVector3(x: 0, y: 0, z: 80)

            sceneView?.scene?.rootNode.addChildNode(lightNode)

        }

    }

    

    override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {

        createPatch()

    }

}

func * (left: Double, right: Float) -> Float {

    return Float(left) * right

}

extension Array {

    func each(doit:(T) -> Void) {for i in self {doit(i)} }

}