From 60491aa240e3a66bf43fc8861eae19427ec8f58e Mon Sep 17 00:00:00 2001 From: -=yb4f310 Date: Mon, 6 Nov 2017 14:45:05 +0100 Subject: [PATCH] particles become only instances --- src/game-lib-d3-particle-engine.js | 35 ++++++++++++++++-------------- src/game-lib-d3-particle.js | 23 +++++++++++++++++++- src/game-lib-system-particle.js | 28 +----------------------- 3 files changed, 42 insertions(+), 44 deletions(-) diff --git a/src/game-lib-d3-particle-engine.js b/src/game-lib-d3-particle-engine.js index 092e042..1f33e22 100644 --- a/src/game-lib-d3-particle-engine.js +++ b/src/game-lib-d3-particle-engine.js @@ -128,28 +128,31 @@ GameLib.D3.ParticleEngine.prototype.updateInstance = function(property) { }; GameLib.D3.ParticleEngine.prototype.processParticles = function(delta) { - this.particles.map( - function(particle){ + this.particles = this.particles.reduce( + function(result, particle){ - particle.mesh.position.x += particle.direction.x * delta; - particle.mesh.position.y += particle.direction.y * delta; - particle.mesh.position.z += particle.direction.z * delta; - particle.mesh.updateInstancePosition(); + particle.position.x += particle.userData.direction.x * delta; + particle.position.y += particle.userData.direction.y * delta; + particle.position.z += particle.userData.direction.z * delta; - particle.elapsed += delta; - if (particle.elapsed > particle.lifeTime) { - GameLib.Event.Emit( - GameLib.Event.REMOVE_COMPONENT, - { - component: particle - } - ) + particle.userData.elapsed += delta; + if (particle.userData.elapsed > particle.userData.lifeTime) { + particle.parent.remove(particle); + } else { + result.push(particle); } - } + + return result; + }, + [] ); }; -GameLib.D3.ParticleEngine.prototype.createNewParticle = function(delta) { +GameLib.D3.ParticleEngine.prototype.createNewParticle = function(camera) { + + var particle = this.templateParticle.clone(camera); + + this.particles.push(particle); }; diff --git a/src/game-lib-d3-particle.js b/src/game-lib-d3-particle.js index e6ca8c1..e94657a 100644 --- a/src/game-lib-d3-particle.js +++ b/src/game-lib-d3-particle.js @@ -136,7 +136,7 @@ GameLib.D3.Particle.prototype.createInstance = function() { return } - this.instance = true; + this.instance = this.mesh.instance; GameLib.Component.prototype.createInstance.call(this); }; @@ -148,6 +148,27 @@ GameLib.D3.Particle.prototype.updateInstance = function(property) { console.log('Update Particle Property : ' + property); }; + +/** + * Clones a particle - this only clones 3rd party instances, so we have less overhead of creating these types of objects + */ +GameLib.D3.Particle.prototype.clone = function(camera) { + + var position = camera.instance.position.clone(); + var lookAt = camera.lookAt.instance.clone(); + + var direction = lookAt.sub(position).negate(); + + var instance = this.instance.clone(); + instance.lookAt(direction); + + instance.userData.direction = this.direction.instance; + instance.userData.elapsed = 0; + instance.userData.lifeTime = this.lifeTime; + this.mesh.parentScene.instance.add(instance); + return instance; +}; + /** * Converts a GameLib.D3.Particle to a new GameLib.D3.API.Particle * @returns {GameLib.D3.API.Particle} diff --git a/src/game-lib-system-particle.js b/src/game-lib-system-particle.js index 664e705..88b8aa8 100644 --- a/src/game-lib-system-particle.js +++ b/src/game-lib-system-particle.js @@ -44,18 +44,11 @@ GameLib.System.Particle.prototype.start = function() { this.particleEngines = GameLib.EntityManager.Instance.queryComponents(GameLib.D3.ParticleEngine); - //this.camera = GameLib.EntityManager.Instance.defaultRenderer.camera; - this.instanceCreatedSubscription = GameLib.Event.Subscribe( GameLib.Event.INSTANCE_CREATED, this.instanceCreated.bind(this) ); - // this.instanceUpdatedSubscription = GameLib.Event.Subscribe( - // GameLib.Event.PARTICLE_INSTANCE_UPDATED, - // this.instanceUpdated.bind(this) - // ); - this.removeComponentSubscription = GameLib.Event.Subscribe( GameLib.Event.REMOVE_COMPONENT, this.removeComponent.bind(this) @@ -81,10 +74,6 @@ GameLib.System.Particle.prototype.instanceCreated = function(data) { }; -GameLib.System.Particle.prototype.instanceUpdated = function(data) { - // console.log('particle engine updated : ' + data.component.name); -}; - /** * Removes a particle engine from this system * @param data @@ -125,24 +114,10 @@ GameLib.System.Particle.prototype.beforeRender = function(data) { if (particleEngine.elapsed > particleEngine.frequency) { particleEngine.elapsed = 0; - particleEngine.createNewParticle(); + particleEngine.createNewParticle(this.camera); } } - // var position = this.camera.position.instance.clone(); - // var lookAt = this.camera.lookAt.instance.clone(); - // - // var direction = lookAt.sub(position).negate(); - // - // particleEngine.lookAt.setFrom(direction); - // particleEngine.updateInstance('lookAt'); - - - // if (particleEngine.elapsed > particleEngine.frequency) { - // particleEngine.elapsed = 0; - // console.log('change time'); - // } - }.bind(this) ) @@ -157,7 +132,6 @@ GameLib.System.Particle.prototype.stop = function() { GameLib.System.prototype.stop.call(this); this.instanceCreatedSubscription.remove(); - // this.instanceUpdatedSubscription.remove(); this.removeComponentSubscription.remove(); this.beforeRenderSubscription.remove();