move all particle properties in

beta.r3js.org
-=yb4f310 2017-11-05 10:51:46 +01:00
parent 0d579f9f13
commit 61d191456d
4 changed files with 161 additions and 70 deletions

View File

@ -111,6 +111,7 @@ GameLib.Event.MOUSE_DOWN = 0x5d;
GameLib.Event.MOUSE_MOVE = 0x5e;
GameLib.Event.MOUSE_WHEEL = 0x5f;
GameLib.Event.MOUSE_UP = 0x60;
GameLib.Event.PARTICLE_INSTANCE_UPDATED = 0x61;
/**
* Returns string name of event ID
@ -217,6 +218,7 @@ GameLib.Event.GetEventName = function(number) {
case 0x5e : return 'mouse_move';
case 0x5f : return 'mouse_wheel';
case 0x60 : return 'mouse_up';
case 0x61 : return 'particle_instance_updated';
break;
}

View File

@ -21,10 +21,14 @@
GameLib.D3.API.ParticleEngine = function(
id,
name,
meshes,
materials,
position,
direction,
lookAt,
particlesPerSecond,
frequency,
elapsed,
distanceType,
minDistance,
maxDistance,
@ -47,6 +51,11 @@ GameLib.D3.API.ParticleEngine = function(
}
this.name = name;
if (GameLib.Utils.UndefinedOrNull(meshes)) {
meshes = [];
}
this.meshes = meshes;
if (GameLib.Utils.UndefinedOrNull(materials)) {
materials = [];
}
@ -62,11 +71,26 @@ GameLib.D3.API.ParticleEngine = function(
}
this.direction = direction;
if (GameLib.Utils.UndefinedOrNull(lookAt)) {
lookAt = new GameLib.API.Vector3(0, 0, -1);
}
this.lookAt = lookAt;
if (GameLib.Utils.UndefinedOrNull(particlesPerSecond)) {
particlesPerSecond = 1;
}
this.particlesPerSecond = particlesPerSecond;
if (GameLib.Utils.UndefinedOrNull(frequency)) {
frequency = Number(1 / Number(particlesPerSecond));
}
this.frequency = frequency;
if (GameLib.Utils.UndefinedOrNull(elapsed)) {
elapsed = 0;
}
this.elapsed = elapsed;
if (GameLib.Utils.UndefinedOrNull(distanceType)) {
distanceType = GameLib.D3.ParticleEngine.DISTANCE_TYPE_CONSTANT;
}
@ -129,6 +153,19 @@ GameLib.D3.API.ParticleEngine.prototype.constructor = GameLib.D3.API.ParticleEng
*/
GameLib.D3.API.ParticleEngine.FromObject = function(objectParticleEngine) {
var apiMeshes = [];
if (objectParticleEngine.meshes) {
apiMeshes = objectParticleEngine.meshes.map(
function (objectMesh) {
if (objectMesh instanceof Object) {
return GameLib.D3.API.Mesh.FromObject(objectMesh);
} else {
return objectMesh
}
}
)
}
var apiMaterials = [];
if (objectParticleEngine.materials) {
apiMaterials = objectParticleEngine.materials.map(
@ -145,10 +182,14 @@ GameLib.D3.API.ParticleEngine.FromObject = function(objectParticleEngine) {
return new GameLib.D3.API.ParticleEngine(
objectParticleEngine.id,
objectParticleEngine.name,
apiMeshes,
apiMaterials,
GameLib.API.Vector3.FromObject(objectParticleEngine.position),
GameLib.API.Vector3.FromObject(objectParticleEngine.direction),
GameLib.API.Vector3.FromObject(objectParticleEngine.lookAt),
objectParticleEngine.particlesPerSecond,
objectParticleEngine.frequency,
objectParticleEngine.elapsed,
objectParticleEngine.distanceType,
objectParticleEngine.minDistance,
objectParticleEngine.maxDistance,

View File

@ -24,10 +24,14 @@ GameLib.D3.ParticleEngine = function(
this,
apiParticleEngine.id,
apiParticleEngine.name,
apiParticleEngine.meshes,
apiParticleEngine.materials,
apiParticleEngine.position,
apiParticleEngine.direction,
apiParticleEngine.lookAt,
apiParticleEngine.particlesPerSecond,
apiParticleEngine.frequency,
apiParticleEngine.elapsed,
apiParticleEngine.distanceType,
apiParticleEngine.minDistance,
apiParticleEngine.maxDistance,
@ -40,6 +44,17 @@ GameLib.D3.ParticleEngine = function(
apiParticleEngine.parentEntity
);
this.meshes = this.meshes.map(
function(mesh) {
if (mesh instanceof GameLib.D3.API.Mesh) {
return new GameLib.D3.Mesh(
graphics,
mesh
)
}
}
);
this.materials = this.materials.map(
function(material) {
if (material instanceof GameLib.D3.API.Material) {
@ -73,11 +88,23 @@ GameLib.D3.ParticleEngine = function(
throw new Error('direction not instance of API.Vector3');
}
if (this.lookAt instanceof GameLib.API.Vector3) {
this.lookAt = new GameLib.Vector3(
graphics,
this.lookAt,
this
);
} else {
console.warn('lookAt not instance of API.Vector3');
throw new Error('lookAt not instance of API.Vector3');
}
GameLib.Component.call(
this,
GameLib.Component.COMPONENT_PARTICLE_ENGINE,
{
materials : [GameLib.D3.Material]
meshes : [GameLib.D3.Mesh],
materials : [GameLib.D3.Material]
}
);
@ -95,7 +122,55 @@ GameLib.D3.ParticleEngine.ROTATION_TYPE_CONSTANT = 0x1;
* @returns true
*/
GameLib.D3.ParticleEngine.prototype.createInstance = function() {
this.instance = true;
this.frequency = Number(1 / this.particlesPerSecond);
for (var i = 0; i < this.frequency; i++) {
var parentMesh = null;
if (i !== 0) {
parentMesh = this.meshes[0];
}
var apiMesh = new GameLib.D3.API.Mesh(
null,
GameLib.D3.Mesh.MESH_TYPE_PLANE,
'Particle Mesh',
null,
null,
this.materials,
parentMesh
);
var mesh = new GameLib.D3.Mesh.Plane(
this.graphics,
apiMesh,
1,
1,
1,
1
);
this.meshes.push(mesh);
}
this.instance = {
position : this.position.instance,
direction : this.direction.instance,
lookAt : this.lookAt.instance,
meshes : this.meshes.map(
function(mesh) {
return mesh.instance
}
),
materials : this.materials.map(
function(material) {
return material.instance
}
)
};
GameLib.Component.prototype.createInstance.call(this);
};
@ -103,7 +178,7 @@ GameLib.D3.ParticleEngine.prototype.createInstance = function() {
* Updates the instance with the current state
*/
GameLib.D3.ParticleEngine.prototype.updateInstance = function() {
this.instance = true;
GameLib.Event.Emit(GameLib.Event.PARTICLE_INSTANCE_UPDATED, {component:this});
};
/**
@ -116,6 +191,11 @@ GameLib.D3.ParticleEngine.prototype.toApiObject = function() {
this.id,
this.name,
this.meshes.map(
function(mesh){
return GameLib.Utils.IdOrNull(mesh);
}
),
this.materials.map(
function(material){
return GameLib.Utils.IdOrNull(material);
@ -123,7 +203,10 @@ GameLib.D3.ParticleEngine.prototype.toApiObject = function() {
),
this.position.toApiObject(),
this.direction.toApiObject(),
this.lookAt.toApiObject(),
this.particlesPerSecond,
this.frequency,
this.elapsed,
this.distanceType,
this.minDistance,
this.maxDistance,

View File

@ -23,6 +23,8 @@ GameLib.System.Particle = function(
*/
this.engines = [];
this.totalTime = 0;
this.beforeRenderSubscription = null;
};
@ -36,15 +38,18 @@ GameLib.System.Particle.prototype.start = function() {
GameLib.System.prototype.start.call(this);
this.particleObjects = GameLib.EntityManager.Instance.queryComponents(GameLib.D3.ParticleEngine);
this.particleObjects.map(this.initialize.bind(this));
this.particleEngines = GameLib.EntityManager.Instance.queryComponents(GameLib.D3.ParticleEngine);
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)
@ -57,51 +62,6 @@ GameLib.System.Particle.prototype.start = function() {
};
/**
* This function does alot of work we want do have done, but we don't want to do it during 'before render' - since we
* don't want to waste cpu power
* @param component
*/
GameLib.System.Particle.prototype.initialize = function(component) {
var lookAt = GameLib.EntityManager.Instance.defaultRenderer.camera.lookAt.instance.clone().negate();
var frequency = Number(1 / component.particlesPerSecond);
var meshes = [];
for (var i = 0; i < frequency; i++) {
var apiMesh = new GameLib.D3.API.Mesh(
null,
GameLib.D3.Mesh.MESH_TYPE_PLANE,
'Particle Mesh',
null,
null,
component.materials
);
var mesh = new GameLib.D3.Mesh.Plane(
this.graphics,
apiMesh,
1,
1,
1,
1
);
meshes.push(mesh);
}
var engine = {
component : component,
elapsed : 0,
frequency : frequency,
lookAt : lookAt,
meshes : meshes
};
this.engines.push(engine);
};
/**
* From now on we want to track everything about a component, only from the systems that are active
* @param data
@ -110,12 +70,25 @@ GameLib.System.Particle.prototype.instanceCreated = function(data) {
if (data.component instanceof GameLib.D3.ParticleEngine) {
console.log('new particle engine');
this.particleObjects.push(data.component);
this.initialize(data.component);
this.particleEngines.push(data.component);
}
};
GameLib.System.Particle.prototype.instanceUpdated = function(data) {
//if (data.component instanceof GameLib.D3.ParticleEngine) {
console.log('particle engine updated : ' + data.component.name);
//this.particleEngines.push(data.component);
//this.initialize(data.component);
//}
};
/**
* Removes a particle engine from this system
* @param data
@ -124,22 +97,13 @@ GameLib.System.Particle.prototype.removeComponent = function(data) {
if (data.component instanceof GameLib.D3.ParticleEngine) {
var index = this.particleObjects.indexOf(data.component);
var index = this.particleEngines.indexOf(data.component);
if (index !== -1) {
console.log('removing particle engine from system');
this.particleObjects.splice(index, 1);
this.particleEngines.splice(index, 1);
this.engines = this.engines.reduce(
function(result, engine){
if (engine.component !== data.component) {
result.push(engine);
}
return result;
},
[]
);
} else {
console.log('failed to find the particle engine in the system : ' + data.component.name);
}
@ -155,14 +119,14 @@ GameLib.System.Particle.prototype.beforeRender = function(data) {
this.totalTime += data.delta;
this.engines.map(
function(engine) {
this.particleEngines.map(
function(particleEngine) {
engine.elapsed += data.delta;
particleEngine.elapsed += data.delta;
if (engine.elapsed > engine.frequency) {
engine.elapsed = 0;
console.log('spawn time');
if (particleEngine.elapsed > particleEngine.frequency) {
particleEngine.elapsed = 0;
console.log('change time');
}
}.bind(this)
@ -179,6 +143,7 @@ GameLib.System.Particle.prototype.stop = function() {
GameLib.System.prototype.stop.call(this);
this.instanceCreatedSubscription.remove();
this.instanceUpdatedSubscription.remove();
this.removeComponentSubscription.remove();
this.beforeRenderSubscription.remove();