iPhone光るコレクション箱

コレクションボックスの中の丸を光らせてみるiPhoneアプリのサンプルコードを描いてみます。

import UIKit

import SceneKit

class ViewController: UIViewController {

    weak var sceneView : SCNView?

    

    override func viewDidLoad() {

        super.viewDidLoad()

        setupScene()

        createBox()

        createLightObject()

        createCamera()

        createMainLight()

    }

    

    func setupScene() {

        sceneView = {

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

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

            sv.scene = SCNScene()

            sv.allowsCameraControl = true

            self.view.addSubview(sv)

            return sv

        }()

    }

    

    func createBox() {

        var node = {() -> SCNNode in

            let box = SCNNode()

            self.sceneView?.scene?.rootNode.addChildNode(box)

            

            let pattern = [

                SCNVector4(x: 0, y: 0, z: 0, w: Float(M_PI) * 0),

                SCNVector4(x: 1, y: 0, z: 0, w: Float(M_PI) * 0.5),

                SCNVector4(x: 1, y: 0, z: 0, w: Float(M_PI) * –0.5),

                SCNVector4(x: 0, y: 1, z: 0, w: Float(M_PI) * 0.5),

                SCNVector4(x: 0, y: 1, z: 0, w: Float(M_PI) * –0.5)]

            for i in pattern {

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

                plate.firstMaterial?.diffuse.contents = UIColor.brownColor()

                let node = SCNNode(geometry: plate)

                

                node.transform = SCNMatrix4Translate(node.transform, 0, 0, –5)

                node.transform = SCNMatrix4Rotate(node.transform, i.w, i.x, i.y, i.z)

                box.addChildNode(node)

            }

            return box

        }

        

        for i in 08 {

            let box = node()

            let x = (Float(i) % 3) * 1010

            let y = Float(i / 3) * 1010

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

        }

    }

    

    func createLightObject() {

        

        for i in 08 {

            let sphere = SCNSphere(radius: 3)

            sphere.firstMaterial?.diffuse.contents = UIColor(hue: CGFloat(i) * 0.1, saturation: 0.2, brightness: 1, alpha: 0.5)

            

            let (x,y) = ((Float(i) % 3) * 1010, Float(i / 3) * 1010)

            let node = SCNNode(geometry: sphere)

            node.name = “lamp”

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

            if (i == 4) { node.turnOn() }  // turnOn : see extension

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

        }

    }

    

    func createCamera() {

        let camera = SCNNode()

        camera.camera = SCNCamera()

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

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

    }

    

    func createMainLight() {

        let light = SCNLight()

        light.type = SCNLightTypeOmni

        light.color = UIColor(hue: 0.5, saturation: 0.1, brightness: 1, alpha: 1)

        let lightNode = SCNNode()

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

        lightNode.light = light

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

    }

    

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

        let p = touches.anyObject()!.locationInView(sceneView)

        if let hits = sceneView?.hitTest(p, options: [SCNHitTestSortResultsKey:true]) {

            hits.filter {$0.node.name == “lamp”}

                .map { $0.node.turnOn() }

        }

    }

}

extension SCNNode {

    

    func turnOn() {

        if (self.light != nil) {

            turnOff()

            return

        }

        self.light = SCNLight()

        self.light?.type = SCNLightTypeSpot

        self.light?.color = UIColor(hue: CGFloat(arc4random_uniform(10)) * 0.1, saturation: 1, brightness: 1, alpha: 1)

        self.light?.zFar = 30

        self.light?.spotOuterAngle = 350

    }

    

    func turnOff() {

        self.light = nil

    }

}