Skip to content

Instantly share code, notes, and snippets.

@joates
Last active August 29, 2015 13:55
Show Gist options
  • Save joates/8722084 to your computer and use it in GitHub Desktop.
Save joates/8722084 to your computer and use it in GitHub Desktop.
this started with me figuring out a sensible way to call the webgl init() method asynchronously, and then i thought i'd add some nervy boxes in a ring pattern (demo here -> http://joat.es/webgl/twitchy-ringer/)
var webgl = require('./webgl')
, boxes = 32
webgl.init(boxes, function(err, data) {
if (err) console.error('init failed')
console.log('ready', data)
})
var domready = require('domready')
, raf = require('raf')
, THREE = require('./three-r65')
, width = window.innerWidth
, height = window.innerHeight
, scene = new THREE.Scene()
, light = new THREE.PointLight(0xffffff, 2, 3000)
, camera = new THREE.PerspectiveCamera(40, width / height, 0.1, 10000)
, object = new THREE.Mesh(
new THREE.CubeGeometry(10, 10, 10)
, new THREE.MeshLambertMaterial({ color: 0xff9000 })
)
module.exports = {
renderer: new THREE.WebGLRenderer({ antialias: false })
, init: function(qty, cb) {
var self = this
process.nextTick(function() {
domready(function() {
scene.add(light)
scene.add(new THREE.AmbientLight(0x202020))
var num = qty
while (--num) {
clone = object.clone()
clone.position.set(Math.cos(num)*qty, Math.sin(num)*qty, -qty)
clone.position.setLength(20)
scene.add(clone)
}
camera.position.z = 40
self.renderer.setSize(width, height)
document.body.appendChild(self.renderer.domElement)
window.addEventListener('resize', self.resize.bind(self), false)
// animation.
raf(self.renderer.domElement).on('data', function(dt) {
self.update(dt)
self.render()
})
// async response.
cb(!scene instanceof THREE.Scene, scene)
})
})
}
, render: function() {
this.renderer.render(scene, camera)
}
, resize: function() {
width = window.innerWidth
height = window.innerHeight
this.renderer.setSize(width, height)
camera.aspect = width / height
camera.updateProjectionMatrix()
}
, update: function(t) {
var s = 0.5 + 0.05 * Math.sin(t * 10)
scene.children.forEach(function(m) {
if (m instanceof THREE.Mesh) {
m.scale.set(s, s, s)
m.rotation.y += Math.random()*0.01
m.rotation.x += Math.random()*0.001
}
})
light.position = camera.position
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment