Skip to content

Instantly share code, notes, and snippets.

@tado
Created August 30, 2014 09:48
Show Gist options
  • Save tado/f0e2a80947198058dafd to your computer and use it in GitHub Desktop.
Save tado/f0e2a80947198058dafd to your computer and use it in GitHub Desktop.
import SceneKit
import QuartzCore
class GameViewController: NSViewController {
@IBOutlet weak var gameView: GameView!
override func awakeFromNib(){
var scene = SCNScene()
var camera = SCNCamera()
var cameraNode = SCNNode()
cameraNode.camera = camera
cameraNode.position = SCNVector3(x: 0, y: 0, z: 2)
scene.rootNode.addChildNode(cameraNode)
for i in 0..<100 {
var box = SCNBox(width: 1, height: 1, length: 1, chamferRadius: 0.03)
box.firstMaterial.diffuse.contents = NSColor(
calibratedHue: randomCGFloat(),
saturation: randomCGFloat() * 0.5 + 0.5, brightness: 2.0, alpha: 0.75)
box.firstMaterial.specular.contents = NSColor.whiteColor()
var boxNode = SCNNode(geometry: box)
boxNode.rotation = SCNVector4(x: randomCGFloat(), y: randomCGFloat(), z: randomCGFloat(), w: CGFloat(M_PI));
boxNode.runAction(SCNAction.repeatActionForever(
SCNAction.rotateByAngle(
CGFloat(M_PI * 2.0),
aroundAxis: SCNVector3(x: randomCGFloat(), y: randomCGFloat(), z: randomCGFloat()),
duration: NSTimeInterval(randomCGFloat() * 10.0 + 10.0))))
scene.rootNode.addChildNode(boxNode)
}
self.gameView!.scene = scene
self.gameView!.allowsCameraControl = true
self.gameView!.showsStatistics = true
self.gameView!.backgroundColor = NSColor.blackColor()
}
func randomCGFloat() -> CGFloat {
return CGFloat(arc4random()) / CGFloat(UInt32.max)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment