UNPKG

threex

Version:

Game Extensions for three.js http://www.threejsgames.com/extensions/

123 lines (113 loc) 3.78 kB
<!DOCTYPE html> <script src="../../../vendor/three.js/build/three.min.js"></script> <body><script> var renderer = new THREE.WebGLRenderer(); renderer.setSize( window.innerWidth, window.innerHeight ); document.body.appendChild( renderer.domElement ); var updateFcts = []; var scene = new THREE.Scene(); var camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 1000 ); camera.position.z = 3; // emit 3 puff every seconds var smokePuff = new SmokePuff(scene); updateFcts.push(function(delta, now){ smokePuff.update(delta, now) }) setInterval(function(){ var position = new THREE.Vector3(0,-1,-2) smokePuff.emit(position); smokePuff.emit(position); smokePuff.emit(position); }, 1*1000); ////////////////////////////////////////////////////////////////////////////////// // render the scene // ////////////////////////////////////////////////////////////////////////////////// updateFcts.push(function(){ renderer.render( scene, camera ); }) ////////////////////////////////////////////////////////////////////////////////// // loop runner // ////////////////////////////////////////////////////////////////////////////////// var lastTimeMsec= null requestAnimationFrame(function animate(nowMsec){ // keep looping requestAnimationFrame( animate ); // measure time lastTimeMsec = lastTimeMsec || nowMsec-1000/60 var deltaMsec = Math.min(200, nowMsec - lastTimeMsec) lastTimeMsec = nowMsec // call each update function updateFcts.forEach(function(updateFn){ updateFn(deltaMsec/1000, nowMsec/1000) }) }) ////////////////////////////////////////////////////////////////////////////////// // comment // ////////////////////////////////////////////////////////////////////////////////// function SmokePuff(container){ // load the texture var texture = THREE.ImageUtils.loadTexture('images/cloud10.png') var updateFcts = [] this.emit = function(position){ // randomize the initial position position = position.clone() position.x += (Math.random()-0.5)*0.3 position.y += (Math.random()-0.5)*0.1 position.z += (Math.random()-0.5)*0.3 // init sprite material var material = new THREE.SpriteMaterial({ map : texture, useScreenCoordinates : false, color : 0x666666 }) // init sprite var sprite = new THREE.Sprite(material) sprite.rotation = Math.random() * Math.PI*2 sprite.position.copy(position) container.add(sprite) var maxAge = 1 + Math.random()*0.4 // set velocity var velocity = new THREE.Vector3(0, 1, 0) velocity.x += (Math.random()-0.5)*0.5 velocity.y += (Math.random()-0.5)*0.1 velocity.z += (Math.random()-0.5)*0.5 velocity.setLength(3 + (Math.random()-0.5)*0.5) // init opacity var age2Opacity = createTweenMidi(maxAge, 0.05*maxAge, 0.4*maxAge) material.opacity= age2Opacity(0) var birthDate = Date.now()/1000 updateFcts.push(function callback(delta, now){ var age = Date.now()/1000 - birthDate if( age >= maxAge ){ sprite.parent.remove(sprite) updateFcts.splice(updateFcts.indexOf(callback),1) return; } // handle friction velocity.multiplyScalar( 0.97 ) // move by velocity sprite.position.add( velocity.clone().multiplyScalar(delta) ) // make it grow sprite.scale.multiplyScalar( 1.0075 ) // handle opacity material.opacity= age2Opacity(age) }) } this.update = function(delta, now){ updateFcts.forEach(function(updateFct){ updateFct(delta, now) }) } function createTweenMidi(maxAge, attackTime, releaseTime){ return function(age){ if( age < attackTime ){ return age / attackTime }else if( age < maxAge - releaseTime ){ return 1; }else{ return (maxAge - age) / releaseTime } } } } </script></body>