iPhone六角と箱

六角形から箱をポッとつくるiPhoneアプリのサンプルコードを描いてみます。

import UIKit

import SceneKit

class ViewController: UIViewController {

    weak var sceneView : SCNView?

    

    override func viewDidLoad() {

        super.viewDidLoad()

        setupScene()

        createCombHoney()

        createCamera()

    }

    

    func setupScene() {

        let sv = SCNView(frame: view.bounds)

        sv.scene = SCNScene()

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

        sv.autoenablesDefaultLighting = true

        sv.allowsCameraControl = true

        view.addSubview(sv)

        

        self.sceneView = sv

    }

    func createCombHoney() {

        [1, 6, 12].foreach { i in

            [Int](0..<i).foreach { j in

                

                let r = i == 1 ? 0

                    : i == 6 ? 3.4 * Float(i) / 6.0

                    : i == 12 && (j % 2) == 0 ? 3.4 * Float(i) / 6.0 : 3 * Float(i) / 6.0

                

                let x = r * Float(cos(Double(j) * 2.0 * M_PI / Double(i)))

                let y = r * Float(sin(Double(j) * 2.0 * M_PI / Double(i)))

                

                let hex = {() -> SCNNode in

                    let node = SCNNode()

                    for k in 02 {

                        let box = SCNBox(width: 3, height: 1.8, length: 0.1, chamferRadius: 0)

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

                        let n = SCNNode(geometry: box)

                        n.name = “hex”

                        n.rotation = SCNVector4(x: 0, y: 0, z: 1, w: Float(k) * Float(M_PI) / 3.0)

                        node.addChildNode(n)

                    }

                    return node

                }()

                

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

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

            }

        }

    }

    

    func createCamera() {

        let camera = SCNNode()

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

        camera.camera = SCNCamera()

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

    }

    

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

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

        

        if let hit = self.sceneView?.hitTest(p, options: [SCNHitTestSortResultsKey : true])?.first as? SCNHitTestResult {

            

            if hit.node.name == “hex” {

                let hex = hit.node

                let cube = SCNBox(width: 2.5, height: 2.5, length: 2.5, chamferRadius: 0)

                cube.firstMaterial?.diffuse.contents = UIColor(hue: 0.1, saturation: 0.7, brightness: 1, alpha: 1)

                let cubeNode = SCNNode(geometry: cube)

                cubeNode.transform = SCNMatrix4Rotate(cubeNode.transform, Float(M_PI)/4.0, 0, 1, 0)

                cubeNode.transform = SCNMatrix4Rotate(cubeNode.transform, Float(M_PI)/5.0, 1, 0, 0)

                hex.addChildNode(cubeNode)

                

                cubeNode.runAction(SCNAction.moveByX(0, y: 0, z: 1, duration: 0.5))

            }

            

        }

        

    }

    

}

extension Array {

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

}