iPhoneペンギンブロック

ブロックでペンギンを作るiPhoneアプリのサンプルコードを描いてみます。

import UIKit

import SceneKit

class ViewController: UIViewController {

    

    weak var sceneView : SCNView?

    var count = 0

    override func viewDidLoad() {

        super.viewDidLoad()

        setupScene()

        createIce()

        createCamera()

        createLight()

    }

    

    func setupScene() {

        let sv = SCNView(frame: view.bounds)

        sv.scene = SCNScene()

        sv.backgroundColor = UIColor(hue: 0.5, saturation: 0.4, brightness: 1, alpha: 1)

        sv.allowsCameraControl = true

        view.addSubview(sv)

        

        sceneView = sv

    }

    func createIce() {

        let ice = SCNCylinder(radius: 12, height: 0.5)

        ice.firstMaterial?.diffuse.contents = UIColor(hue: 0.5, saturation: 0.02, brightness: 1, alpha: 0.7)

        ice.firstMaterial?.specular.contents = UIColor.whiteColor()

        let iceNode = SCNNode(geometry: ice)

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

        iceNode.physicsBody = SCNPhysicsBody.staticBody()

    }

    

    func createCamera() {

        let camera = SCNNode()

        camera.camera = SCNCamera()

        camera.position = SCNVector3(x: 0, y: 5, z: 25)

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

    }

    

    func createLight() {

        let ambient = SCNLight()

        ambient.type = SCNLightTypeAmbient

        ambient.color = UIColor.whiteColor()

        

        let ambientNode = SCNNode()

        ambientNode.light = ambient;

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

        

        

        let omni = SCNLight()

        omni.type = SCNLightTypeOmni

        omni.color = UIColor(white: 0.5, alpha: 1)

        

        let omniNode = SCNNode()

        omniNode.position = SCNVector3(x: 0, y: 20, z: 20)

        omniNode.rotation = SCNVector4(x: 1, y: 0, z: 0, w: –Float(M_PI) * 0.3)

        omniNode.light = omni;

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

    }

    

    func buildPenguin(p : SCNVector3) {

        

        let foot = SCNBox(width: 2, height: 1, length: 3, chamferRadius: 0)

        foot.firstMaterial?.diffuse.contents = UIColor.yellowColor()

        let footNode = SCNNode(geometry: foot)

        footNode.position = SCNVector3(x: p.x, y: p.y + 10, z: p.z)

        

        let tail = SCNBox(width: 2, height: 1, length: 2, chamferRadius: 0)

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

        let tailNode = SCNNode(geometry: tail)

        tailNode.position = SCNVector3(x: p.x, y: p.y + 12, z: p.z + 1.5)

        

        let front = SCNBox(width: 2, height: 2, length: 1, chamferRadius: 0)

        front.firstMaterial?.diffuse.contents = UIColor(white: 0.9, alpha: 1)

        let frontNode = SCNNode(geometry: front)

        frontNode.position = SCNVector3(x: p.x, y: p.y + 12, z: p.z)

        

        let back = SCNBox(width: 2, height: 2, length: 1, chamferRadius: 0)

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

        let backNode = SCNNode(geometry: back)

        backNode.position = SCNVector3(x: p.x, y: p.y + 16, z: p.z + 1)

        

        let beak = SCNBox(width: 2, height: 1, length: 2, chamferRadius: 0)

        beak.firstMaterial?.diffuse.contents = UIColor.yellowColor()

        let beakNode = SCNNode(geometry: beak)

        beakNode.position = SCNVector3(x: p.x, y: p.y + 18, z: p.z0.5)

        

        let head = SCNBox(width: 2, height: 1, length: 2, chamferRadius: 0)

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

        let headNode = SCNNode(geometry: head)

        headNode.position = SCNVector3(x: p.x, y: p.y + 20, z: p.z + 0.5)

        

        for n in [footNode, tailNode, frontNode, backNode, beakNode, headNode] {

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

            n.physicsBody = SCNPhysicsBody.dynamicBody()

            n.physicsBody?.angularVelocityFactor = SCNVector3Zero

            n.physicsBody?.velocityFactor = SCNVector3(x: 0, y: 1, z: 0)

        }

    }

    

    override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {

        

        if count > 5 { return }

        let p = SCNVector3(x: Float(count) * 410, y: 0, z: 0)

        buildPenguin(p)

        count++;

    }

}