Created
August 24, 2015 12:49
-
-
Save mzsima/7ad9c0900ef739d9a74f to your computer and use it in GitHub Desktop.
polygon pattern generator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ViewController.swift | |
// PolygonPattern | |
// | |
// Created by MizushimaYusuke on 8/24/15. | |
// Copyright (c) 2015 MizushimaYusuke. All rights reserved. | |
// | |
import UIKit | |
import SceneKit | |
class ViewController: UIViewController { | |
weak var sceneView : SCNView? | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
setupScene() | |
createPattern() | |
createCamera() | |
createLight() | |
} | |
func setupScene() { | |
let sv = SCNView(frame: view.bounds) | |
sv.scene = SCNScene() | |
sv.allowsCameraControl = true | |
view.addSubview(sv) | |
sceneView = sv | |
} | |
func createPattern() { | |
let g = SCNPyramid(width: 2, height: 3, length: 2) | |
let hue = CGFloat(arc4random() % 10) * 0.1 | |
g.firstMaterial?.diffuse.contents = UIColor(hue: hue, saturation: 0.8, brightness: 1, alpha: 1) | |
for i in 0..<50 { | |
let x = Float(i % 5) - 2 | |
let z = Float(i / 5) - 4 | |
let node = SCNNode(geometry: g) | |
node.name = "pattern" | |
node.position = SCNVector3(x: x, y: 0, z: z) | |
let wx = Float(arc4random() % 10) * 0.01 * Float(M_PI) | |
let wy = Float(arc4random() % 10) * 0.01 * Float(M_PI) | |
let wz = Float(arc4random() % 10) * 0.01 * Float(M_PI) | |
node.transform = SCNMatrix4Rotate(node.transform, wx, 1, 0, 0) | |
node.transform = SCNMatrix4Rotate(node.transform, wy, 0, 1, 0) | |
node.transform = SCNMatrix4Rotate(node.transform, wz, 0, 0, 1) | |
sceneView?.scene?.rootNode.addChildNode(node) | |
} | |
} | |
func createCamera() { | |
let camera = SCNNode() | |
camera.camera = SCNCamera() | |
camera.position = SCNVector3(x: 0, y: 7, z: 0) | |
camera.rotation = SCNVector4(x: 1, y: 0, z: 0, w: -Float(M_PI) * 0.5) | |
camera.camera?.usesOrthographicProjection = true | |
camera.camera?.orthographicScale = 2 | |
sceneView?.scene?.rootNode.addChildNode(camera) | |
} | |
func createLight() { | |
let light = SCNLight() | |
light.type = SCNLightTypeSpot | |
let node = SCNNode() | |
node.light = light | |
node.position = SCNVector3(x: 0, y: 50, z: 0) | |
node.rotation = SCNVector4(x: 1, y: 0, z: 0, w: -Float(M_PI) * 0.5) | |
sceneView?.scene?.rootNode.addChildNode(node) | |
let light2 = SCNLight() | |
light2.type = SCNLightTypeAmbient | |
light2.color = UIColor(white: 0.5, alpha: 1) | |
let node2 = SCNNode() | |
node2.light = light2 | |
node2.position = SCNVector3(x: 0, y: 50, z: 0) | |
node2.rotation = SCNVector4(x: 1, y: 0, z: 0, w: -Float(M_PI) * 0.5) | |
sceneView?.scene?.rootNode.addChildNode(node2) | |
} | |
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) { | |
sceneView?.scene?.rootNode.childNodes | |
.filter { ($0 as! SCNNode).name == "pattern" } | |
.foreach { n in (n as! SCNNode).removeFromParentNode() } | |
createPattern() | |
} | |
} | |
extension Array { | |
func foreach(doit:T -> Void) { for i in self {doit(i)} } | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment