iPhoneカット卵

カットしたゆで卵を箱につめるiPhoneアプリのサンプルコードを書いてみます。

import UIKit

import SceneKit

class ViewController: UIViewController {

    weak var sceneView : SCNView?

    

    override func viewDidLoad() {

        super.viewDidLoad()

        setupScene()

        createRoom()

        createCamera()

    }

    func setupScene() {

        let sv = SCNView(frame: view.bounds)

        sv.scene = SCNScene()

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

        sv.allowsCameraControl = true

        sv.autoenablesDefaultLighting = true

        view?.addSubview(sv)

        

        sceneView = sv

    }

    func createRoom() {

        let room = SCNNode()

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

        

        let floorboard = SCNBox(width: 40, height: 1, length: 40, chamferRadius: 0)

        floorboard.firstMaterial?.diffuse.contents = UIColor(hue: 0.15, saturation: 0.2, brightness: 1, alpha: 1)

        let floorNode = SCNNode(geometry: floorboard)

        floorNode.position = SCNVector3(x: 0, y: –5, z: 0)

        room.addChildNode(floorNode)

        

        [Int](03).each { i in

            let wall = SCNBox(width: 40, height: 10, length: 1, chamferRadius: 0)

            wall.firstMaterial?.diffuse.contents = floorboard.firstMaterial?.diffuse.contents

            let wallNode = SCNNode(geometry: wall)

            wallNode.transform = SCNMatrix4Translate(wallNode.transform, 0, 0, 20)

            wallNode.transform = SCNMatrix4Rotate(wallNode.transform, Float(M_PI) * 0.5 * i, 0, 1, 0)

            room.addChildNode(wallNode)

        }

        

        room.physicsBody = SCNPhysicsBody.staticBody()

    }

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

        let egg = SCNNode()

        

        let white = SCNTube(innerRadius: 2, outerRadius: 3, height: 1)

        white.firstMaterial?.lightingModelName = SCNLightingModelConstant

        let whiteNode = SCNNode(geometry: white)

        whiteNode.transform = SCNMatrix4MakeRotation(Float(M_PI) * 0.5, 1, 0, 0)

        egg.addChildNode(whiteNode)

        

        let yellow = SCNCylinder(radius: 2, height: 1)

        yellow.firstMaterial?.diffuse.contents = UIColor(hue: 0.15, saturation: 1, brightness: 1, alpha: 1)

        yellow.firstMaterial?.lightingModelName = SCNLightingModelConstant

        let yellowNode = SCNNode(geometry: yellow)

        yellowNode.transform = SCNMatrix4MakeRotation(Float(M_PI) * 0.5, 1, 0, 0)

        egg.addChildNode(yellowNode)

        

        [-1, 1].each {i in

            let eye = SCNSphere(radius: 0.4)

            eye.firstMaterial?.diffuse.contents = UIColor.blackColor()

            let eyeNode = SCNNode(geometry: eye)

            eyeNode.position = SCNVector3(x: Float(i), y: 0.5, z: 1)

            eyeNode.transform = SCNMatrix4Scale(eyeNode.transform, 1, 0.5, 0.3)

            egg.addChildNode(eyeNode)

        }

        

        let mouth = SCNSphere(radius: 0.9)

        mouth.firstMaterial?.diffuse.contents = UIColor.whiteColor()

        mouth.firstMaterial?.lightingModelName = SCNLightingModelConstant

        let mouthNode = SCNNode(geometry: mouth)

        mouthNode.position = SCNVector3(x: 0, y: –0.5, z: 1)

        mouthNode.transform = SCNMatrix4Scale(mouthNode.transform, 1, 0.2, 0.01)

        egg.addChildNode(mouthNode)

        

        egg.physicsBody = SCNPhysicsBody.dynamicBody()

        egg.physicsBody?.restitution = 0.3

        egg.physicsBody?.applyForce(SCNVector3(x: 6, y: 10, z: 0), impulse: true)

        egg.physicsBody?.applyTorque(SCNVector4(x: 0, y: 0, z: 1, w: –0.5), impulse: true)

        

        egg.position = SCNVector3(x: –30, y: 15 + Float(arc4random() % 10), z: 0)

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

    }

    

    func createCamera() {

        let camera = SCNNode()

        camera.camera = SCNCamera()

        camera.transform = SCNMatrix4Translate(camera.transform, –5, 10, 70)

        camera.transform = SCNMatrix4Rotate(camera.transform, –0.6, 1, 0, 0)

        camera.transform = SCNMatrix4Rotate(camera.transform, –0.15, 0, 1, 0)

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

    }

}

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

    return left * Float(right)

}

extension Array {

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

}