particles become only instances

beta.r3js.org
-=yb4f310 2017-11-06 14:45:05 +01:00
parent abc78b2993
commit 60491aa240
3 changed files with 42 additions and 44 deletions

View File

@ -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);
};

View File

@ -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}

View File

@ -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();