Created
October 23, 2014 10:39
-
-
Save moxuse/658b92aa9ebba7185aad to your computer and use it in GitHub Desktop.
SceneKit Physics Test
This file contains 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
import SceneKit | |
import QuartzCore | |
class GameViewController: NSViewController { | |
@IBOutlet weak var gameView: GameView! | |
override func awakeFromNib(){ | |
let numBox = 300 | |
let camDistance:CGFloat = 55.0 | |
// Create a new scene | |
let scene = SCNScene() | |
// Create and add a camera t the scene | |
let cameraNode = SCNNode() | |
cameraNode.camera = SCNCamera() | |
scene.rootNode.addChildNode(cameraNode) | |
cameraNode.position = SCNVector3(x: 0, y: 15.0, z: camDistance) | |
// Make floor node | |
let floorNode = SCNNode() | |
let floor = SCNFloor() | |
floor.reflectivity = 0.25 | |
floorNode.geometry = floor | |
// Floor Physics | |
let floorShape = SCNPhysicsShape(geometry: floor, options: nil) | |
let floorBody = SCNPhysicsBody(type: .Static, shape: floorShape) | |
floorNode.physicsBody = floorBody; | |
scene.rootNode.addChildNode(floorNode) | |
// Add box nodes to the scene | |
for x in 0..<numBox { | |
let node = SCNNode() | |
let rdx = randomCGFloat() * camDistance - camDistance / 2 | |
let rdy = randomCGFloat() * 300 | |
let rdz = randomCGFloat() * camDistance - camDistance / 2 | |
node.position = SCNVector3Make(rdx, rdy, rdz) | |
let box = SCNBox(width: randomCGFloat() * 5.0, height: randomCGFloat() * 5.0, length: randomCGFloat() * 5.0, chamferRadius: 0.0) | |
node.geometry = box | |
// Create and configure a material | |
let material = SCNMaterial() | |
material.specular.contents = NSColor.blueColor() | |
material.locksAmbientWithDiffuse = true | |
// Set shaderModifiers properties | |
let snipet = "uniform float Scale = 3.0;\n" + | |
"uniform float Width = 0.5;\n" + | |
"uniform float Blend = 0.0;\n" + | |
"vec2 position = fract(_surface.diffuseTexcoord * Scale);" + | |
"float f1 = clamp(position.y / Blend, 0.0, 1.0);" + | |
"float f2 = clamp((position.y - Width) / Blend, 0.0, 1.0);" + | |
"f1 = f1 * (1.0 - f2);" + | |
"f1 = f1 * f1 * 2.0 * (3. * 2. * f1);" + | |
"_surface.diffuse = mix(vec4(1.0), vec4(0.0), f1);" | |
material.shaderModifiers = [SCNShaderModifierEntryPointSurface: snipet] | |
// Set the material to the 3D object geometry | |
node.geometry?.firstMaterial = material | |
let boxShape = SCNPhysicsShape(geometry: box, options: nil) | |
let boxBody = SCNPhysicsBody(type: .Dynamic, shape: boxShape) | |
node.physicsBody = boxBody; | |
scene.rootNode.addChildNode(node) | |
} | |
// Set the scene to the view | |
self.gameView!.scene = scene | |
// Allows the user to manipulate the camera | |
self.gameView!.allowsCameraControl = true | |
// Show statistics such as fps and timing information | |
self.gameView!.showsStatistics = true | |
// Configure the view | |
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
Hey i have a 3d collada object how can i apply physics on it in scenekit??