/** * * Class Name : ParticleEmitter * Created : 28/08/2012 * Require : Three.js * **/ GAME_ENGINE.ParticleEmitter = function(scene, picture, psize, max) { this.scene = scene; this.psize = (psize) ? psize : 20; this.maxParticleCount = (max) ? max : 20; this.emitting = false; this.timer = 0; this.delay = 0; //60 fps -> 1 particle per second this.particleAnimation = null; this.particleInitializer = null; this.particles = []; this.Material = { color: 0xFFFFFF, size: psize, map: THREE.ImageUtils.loadTexture(picture), blending: THREE.AdditiveBlending, transparent: true }; this.position = new THREE.Vector3(0, 0, 0); } GAME_ENGINE.ParticleEmitter.prototype.setParticleAnimation = function(animation) { this.particleAnimation = animation; }; GAME_ENGINE.ParticleEmitter.prototype.setParticleInitializer = function(initializer) { this.particleInitializer = initializer; }; GAME_ENGINE.ParticleEmitter.prototype.start = function() { this.emitting = true; }; GAME_ENGINE.ParticleEmitter.prototype.stop = function() { this.emitting = false; }; GAME_ENGINE.ParticleEmitter.prototype.destroyParticle = function (index) { this.scene.remove( this.particles[index] ); this.particles.splice(index, 1); } GAME_ENGINE.ParticleEmitter.prototype.update = function( delta ) { if (this.emitting) { if (this.timer >= this.delay) { if(this.particles.length < this.maxParticleCount) { var particle = new THREE.Sprite( this.Material ); particle.useScreenCoordinates = false; particle.mergeWith3D = true; particle.affectedByDistance = true; particle.scaleByViewport = true; particle.position = new THREE.Vector3(this.position.x, this.position.y, this.position.z); particle.scale.x = particle.scale.y = this.psize; if (this.particleInitializer != null) this.particleInitializer(particle); this.particles.push( particle ); this.scene.add( particle ); } this.timer = 0; } this.timer += 1; } if (this.particleAnimation != null) { for (var i=0; i 100) this.destroyParticle(index); }); particlesEmitter.start();