Skip to content

Instantly share code, notes, and snippets.

@moxuse
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save moxuse/093ae9f28a6bbc1e7bba to your computer and use it in GitHub Desktop.
Save moxuse/093ae9f28a6bbc1e7bba to your computer and use it in GitHub Desktop.
ScenKit Shader Handle Binding Test
import UIKit
import QuartzCore
import SceneKit
class GameViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let numSphere = 25
let camDistance:Float = 25.0
let gameView = self.view as SCNView;
// create a new scene
let scene = SCNScene()
// create SCNProgram
let program = SCNProgram()
// load shader vert/frag file
let vertexShaderURL = NSBundle.mainBundle().URLForResource("myShader", withExtension: "vert")
let fragmentalShaderURL = NSBundle.mainBundle().URLForResource("myShader", withExtension: "frag")
// set program
let vertexShader = NSString(contentsOfURL: vertexShaderURL!, encoding: NSUTF8StringEncoding, error: nil)
program.vertexShader = vertexShader
let fragmentalShader = NSString(contentsOfURL: fragmentalShaderURL!, encoding: NSUTF8StringEncoding, error: nil)
program.fragmentShader = fragmentalShader
// bind Uniform and Attribute defines
program.setSemantic(SCNModelViewTransform, forSymbol: "position", options: nil)
program.setSemantic(SCNModelViewProjectionTransform, forSymbol: "modelViewProjection", options: nil)
// create and add a camera t the scene
let cameraNode = SCNNode()
cameraNode.camera = SCNCamera()
scene.rootNode.addChildNode(cameraNode)
cameraNode.position = SCNVector3(x:0, y:0, z:camDistance)
// create and add a 3D box to the scene
for x in 0..<numSphere {
let node = SCNNode()
let rdx = randomCGFloat() * camDistance - camDistance / 2
let rdy = randomCGFloat() * camDistance - camDistance / 2
let rdz = randomCGFloat() * camDistance - camDistance / 2
node.position = SCNVector3Make(rdx, rdy, rdz)
var sphiereGeometry = SCNSphere(radius: 1.0)
node.geometry = sphiereGeometry
scene.rootNode.addChildNode(node)
// create and configure a material
let material = SCNMaterial()
// set material's program
material.program = program;
material.specular.contents = UIColor.grayColor()
material.locksAmbientWithDiffuse = true
// timer for shader Unform 'time'
let startTime = CFAbsoluteTimeGetCurrent()
// buind Uniform in block
material.handleBindingOfSymbol("time") {
programID, location, renderedNode, renderer in
glUniform1f(GLint(location), GLfloat(CFAbsoluteTimeGetCurrent() - startTime))
}
// set the material to the 3D object geometry
node.geometry?.firstMaterial = material
// animate the 3D object
let animation:CABasicAnimation = CABasicAnimation(keyPath:"rotation")
animation.toValue = NSValue(SCNVector4:SCNVector4(x:Float(1.0), y:Float(1.0), z:Float(0.0), w:Float(M_PI * 2)))
animation.duration = 5
animation.repeatCount = MAXFLOAT // repeat forever
node.addAnimation(animation, forKey:nil)
}
// set the scene to the view
gameView.scene = scene
// allows the user to manipulate the camera
gameView.allowsCameraControl = true
// show statistics such as fps and timing information
gameView.showsStatistics = true
// configure the view
gameView.backgroundColor = UIColor.blackColor()
}
func randomCGFloat() -> Float {
return Float(arc4random()) / Float(UInt32.max)
}
override func shouldAutorotate() -> Bool {
return true
}
override func supportedInterfaceOrientations() -> Int {
if UIDevice.currentDevice().userInterfaceIdiom == .Phone {
return Int(UIInterfaceOrientationMask.AllButUpsideDown.toRaw())
} else {
return Int(UIInterfaceOrientationMask.All.toRaw())
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Release any cached data, images, etc that aren't in use.
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment