iPhoneプリズム

プリズムで七色っぽいiPhoneアプリのサンプルコードを描いてみます。

import UIKit

import SceneKit

class ViewController: UIViewController {

    weak var sceneView : SCNView?

    

    override func viewDidLoad() {

        super.viewDidLoad()

        self.setupScene()

        self.createBase()

        self.createPrism()

        self.createCamera()

    }

    

    func setupScene() {

        let sv = SCNView(frame: view.bounds)

        sv.scene = SCNScene()

        sv.backgroundColor = UIColor.darkGrayColor()

        sv.allowsCameraControl = true

        sv.autoenablesDefaultLighting = true

        view.addSubview(sv)

        

        sceneView = sv

    }

    

    func createBase() {

        let base = SCNCylinder(radius: 10, height: 0.2)

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

        let baseNode = SCNNode(geometry: base)

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

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

    }

    

    func createPrism() {

        let prism = SCNPyramid(width: 10, height: 6, length: 2)

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

        prism.firstMaterial?.specular.contents = UIColor(white: 0.99, alpha: 1)

        prism.firstMaterial?.reflective.contents = UIColor(hue: 0.5, saturation: 0.1, brightness: 1, alpha: 1)

        prism.firstMaterial?.shininess = 1.0

        let prismNode = SCNNode(geometry: prism)

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

    }

    

    func createCamera() {

        let camera = SCNNode()

        camera.camera = SCNCamera()

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

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

    }

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

        let lineInput = SCNBox(width: 1, height: 0.2, length: 0.2, chamferRadius: 0)

        lineInput.firstMaterial?.diffuse.contents = UIColor.whiteColor().colorWithAlphaComponent(0.9)

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

        let lineInputNode = SCNNode(geometry: lineInput)

        lineInputNode.pivot = SCNMatrix4MakeTranslation(-0.5, 0, 0)

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

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

        

        

        let duration = 2.0

        lineInputNode.runAction(SCNAction.customActionWithDuration(duration, actionBlock: { (node, progress) -> Void in

            let ds = 1.0 + 9.0 * progress / CGFloat(duration)

            node.scale = SCNVector3(x:Float(ds), y: 1, z: 1)

        }), completionHandler: { () -> Void in

            

            for i in 06 {

                let lineOut = SCNBox(width: 1, height: 0.2, length: 0.2, chamferRadius: 0)

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

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

                let lineOutNode = SCNNode(geometry: lineOut)

                lineOutNode.pivot = SCNMatrix4MakeTranslation(-0.5, 0, 0)

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

                lineOutNode.rotation = SCNVector4(x: 0, y: 0, z: 1, w: Float(i) * 0.02 + 0.3)

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

                

                lineOutNode.runAction(SCNAction.customActionWithDuration(duration, actionBlock: { (node, progress) -> Void in

                    let ds = 1.0 + 9.0 * progress / CGFloat(duration)

                    node.scale = SCNVector3(x:Float(ds), y: 1, z: 1)

                }))

            }

            

        })

    }

    

}