iPhone ライティング

SceneKitでいろんなライトを試すiPhoneアプリのサンプルコードを描いてみます。

import UIKit

import SceneKit

class ViewController: UIViewController {

    weak var sceneView : SCNView?

    var scene = SCNScene()

    var lightTarget = SCNNode()

    

    override func viewDidLoad() {

        super.viewDidLoad()

        self.view.backgroundColor = UIColor(hue: 0.2, saturation: 0.6, brightness: 0.6, alpha: 1)

        

        self.createScene()

        self.createObject()

        self.createCamera()

        self.createLights()

    }

    

    func createScene() {

        var w = CGRectGetMaxX(self.view.bounds)

        var sceneView = SCNView(frame: CGRectMake(0, 0, w, w))

        sceneView.backgroundColor = UIColor(hue: 0.6, saturation: 0.9, brightness: 0.9, alpha: 1)

        sceneView.scene = self.scene

        self.view.addSubview(sceneView)

        

        self.sceneView = sceneView

    }

    func createObject()

    {

        var obj = SCNNode(geometry: SCNTube(innerRadius: 3, outerRadius: 5, height: 8))

        

        var material = SCNMaterial()

        material.diffuse.contents = UIColor(red: 0.9, green: 1.0, blue: 0.9, alpha: 1)

        material.specular.contents = UIColor.whiteColor()

        material.shininess = 1.0

        obj.geometry?.firstMaterial = material

        

        self.scene.rootNode.addChildNode(obj)

        

        obj.runAction(SCNAction.repeatActionForever(SCNAction.rotateByAngle(0.1, aroundAxis: SCNVector3(x: 1, y: 0, z: 0.5), duration: 1.0)))

    }

    

    func createCamera()

    {

        var cameraNode = SCNNode()

        cameraNode.camera = SCNCamera()

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

        self.scene.rootNode.addChildNode(cameraNode)

    }

    

    func createLights()

    {

        var ambientLight = SCNLight()

        ambientLight.type = SCNLightTypeAmbient;

        ambientLight.color = UIColor(white: 0.1, alpha: 1)

        var ambientLightNode = SCNNode()

        ambientLightNode.light = ambientLight

        self.scene.rootNode.addChildNode(ambientLightNode)

        

        var diffuseLight = SCNLight()

        diffuseLight.type = SCNLightTypeOmni

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

        var diffuseLightNode = SCNNode()

        diffuseLightNode.light = diffuseLight

        diffuseLightNode.position = SCNVector3(x: –150, y: 30, z: 80)

        self.scene.rootNode.addChildNode(diffuseLightNode)

        

        var spotA = SCNLight()

        spotA.type = SCNLightTypeSpot

        spotA.color = UIColor(red: 1, green: 0.5, blue: 0.5, alpha: 1)//(hue: 0.5, saturation: 0.9, brightness: 1, alpha: 1)

        var spotNodeA = SCNNode()

        spotNodeA.light = spotA

        spotNodeA.position = SCNVector3(x: 160, y:30, z: 80)

        self.scene.rootNode.addChildNode(spotNodeA)

        

        spotNodeA.constraints = [SCNLookAtConstraint(target: self.lightTarget)]

    }

    

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

        var p = touches.anyObject()!.locationInView(self.sceneView)

        p = CGPoint(x: p.x CGRectGetMidX(self.sceneView!.bounds), y: p.y CGRectGetMidY(self.sceneView!.bounds))

        self.lightTarget.position = SCNVector3(x: Float(p.x/5.0), y: Float(p.y/5.0), z: 0)

    }

    

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

        var p = touches.anyObject()!.locationInView(self.sceneView)

        p = CGPoint(x: p.x CGRectGetMidX(self.sceneView!.bounds), y: p.y CGRectGetMidY(self.sceneView!.bounds))

        self.lightTarget.position = SCNVector3(x: Float(p.x/5.0), y: Float(p.y/5.0), z: 0)

    }

    

    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

    }

}