|
@@ -2,52 +2,52 @@ module BABYLON {
|
|
|
|
|
|
export class SolidParticleSystem implements IDisposable {
|
|
export class SolidParticleSystem implements IDisposable {
|
|
// public members
|
|
// public members
|
|
- public particles: SolidParticle[] = [];
|
|
|
|
- public nbParticles:number = 0;
|
|
|
|
- public billboard: boolean = false;
|
|
|
|
- public counter: number = 0;
|
|
|
|
|
|
+ public particles = new Array<SolidParticle>();
|
|
|
|
+ public nbParticles = 0;
|
|
|
|
+ public billboard = false;
|
|
|
|
+ public counter = 0;
|
|
public name: string;
|
|
public name: string;
|
|
public mesh: Mesh;
|
|
public mesh: Mesh;
|
|
|
|
|
|
// private members
|
|
// private members
|
|
private _scene: Scene;
|
|
private _scene: Scene;
|
|
- private _positions: number[] = [];
|
|
|
|
- private _indices: number[] = [];
|
|
|
|
- private _normals: number[] = [];
|
|
|
|
- private _colors: number[] = [];
|
|
|
|
- private _uvs: number[] = [];
|
|
|
|
- private _index: number = 0; // indices index
|
|
|
|
- private _shapeCounter: number = 0;
|
|
|
|
- private _useParticleColor: boolean = true;
|
|
|
|
- private _useParticleTexture: boolean = true;
|
|
|
|
- private _useParticleRotation: boolean = true;
|
|
|
|
- private _useParticleVertex: boolean = false;
|
|
|
|
- private _cam_axisZ: Vector3 = Vector3.Zero();
|
|
|
|
- private _cam_axisY: Vector3 = Vector3.Zero();
|
|
|
|
- private _cam_axisX: Vector3 = Vector3.Zero();
|
|
|
|
- private _axisX: Vector3 = Axis.X;
|
|
|
|
- private _axisY: Vector3 = Axis.Y;
|
|
|
|
- private _axisZ: Vector3 = Axis.Z;
|
|
|
|
|
|
+ private _positions = new Array<number>();
|
|
|
|
+ private _indices = new Array<number>();
|
|
|
|
+ private _normals = new Array<number>();
|
|
|
|
+ private _colors = new Array<number>();
|
|
|
|
+ private _uvs = new Array<number>();
|
|
|
|
+ private _index = 0; // indices index
|
|
|
|
+ private _shapeCounter = 0;
|
|
|
|
+ private _useParticleColor = true;
|
|
|
|
+ private _useParticleTexture = true;
|
|
|
|
+ private _useParticleRotation = true;
|
|
|
|
+ private _useParticleVertex = false;
|
|
|
|
+ private _cam_axisZ = Vector3.Zero();
|
|
|
|
+ private _cam_axisY = Vector3.Zero();
|
|
|
|
+ private _cam_axisX = Vector3.Zero();
|
|
|
|
+ private _axisX = Axis.X;
|
|
|
|
+ private _axisY = Axis.Y;
|
|
|
|
+ private _axisZ = Axis.Z;
|
|
private _camera: Camera;
|
|
private _camera: Camera;
|
|
private _particle: SolidParticle;
|
|
private _particle: SolidParticle;
|
|
- private _fakeCamPos: Vector3 = Vector3.Zero();
|
|
|
|
- private _rotMatrix: Matrix = new Matrix();
|
|
|
|
- private _invertedMatrix: Matrix = new Matrix();
|
|
|
|
- private _rotated: Vector3 = Vector3.Zero();
|
|
|
|
- private _quaternion: Quaternion = new Quaternion();
|
|
|
|
- private _vertex: Vector3 = Vector3.Zero();
|
|
|
|
- private _yaw: number = 0.0;
|
|
|
|
- private _pitch: number = 0.0;
|
|
|
|
- private _roll: number = 0.0;
|
|
|
|
- private _halfroll: number = 0.0;
|
|
|
|
- private _halfpitch: number = 0.0;
|
|
|
|
- private _halfyaw: number = 0.0;
|
|
|
|
- private _sinRoll: number = 0.0;
|
|
|
|
- private _cosRoll: number = 0.0;
|
|
|
|
- private _sinPitch: number = 0.0;
|
|
|
|
- private _cosPitch: number = 0.0;
|
|
|
|
- private _sinYaw: number = 0.0;
|
|
|
|
- private _cosYaw: number = 0.0;
|
|
|
|
|
|
+ private _fakeCamPos = Vector3.Zero();
|
|
|
|
+ private _rotMatrix = new Matrix();
|
|
|
|
+ private _invertedMatrix = new Matrix();
|
|
|
|
+ private _rotated = Vector3.Zero();
|
|
|
|
+ private _quaternion = new Quaternion();
|
|
|
|
+ private _vertex = Vector3.Zero();
|
|
|
|
+ private _yaw = 0.0;
|
|
|
|
+ private _pitch = 0.0;
|
|
|
|
+ private _roll = 0.0;
|
|
|
|
+ private _halfroll = 0.0;
|
|
|
|
+ private _halfpitch = 0.0;
|
|
|
|
+ private _halfyaw = 0.0;
|
|
|
|
+ private _sinRoll = 0.0;
|
|
|
|
+ private _cosRoll = 0.0;
|
|
|
|
+ private _sinPitch = 0.0;
|
|
|
|
+ private _cosPitch = 0.0;
|
|
|
|
+ private _sinYaw = 0.0;
|
|
|
|
+ private _cosYaw = 0.0;
|
|
|
|
|
|
constructor(name: string, scene: Scene) {
|
|
constructor(name: string, scene: Scene) {
|
|
this.name = name;
|
|
this.name = name;
|
|
@@ -57,8 +57,8 @@ module BABYLON {
|
|
|
|
|
|
// build the SPS mesh : returns the mesh
|
|
// build the SPS mesh : returns the mesh
|
|
public buildMesh(): Mesh {
|
|
public buildMesh(): Mesh {
|
|
- if (this.nbParticles == 0) {
|
|
|
|
- //this.addTriangles(1, 1);
|
|
|
|
|
|
+ if (this.nbParticles === 0) {
|
|
|
|
+ return null;
|
|
}
|
|
}
|
|
VertexData.ComputeNormals(this._positions, this._indices, this._normals);
|
|
VertexData.ComputeNormals(this._positions, this._indices, this._normals);
|
|
var vertexData = new VertexData();
|
|
var vertexData = new VertexData();
|
|
@@ -78,7 +78,7 @@ module BABYLON {
|
|
}
|
|
}
|
|
|
|
|
|
// _meshBuilder : inserts the shape model in the global SPS mesh
|
|
// _meshBuilder : inserts the shape model in the global SPS mesh
|
|
- private _meshBuilder(p, shape, positions, meshInd, indices, meshUV, uvs, meshCol, colors): void {
|
|
|
|
|
|
+ private _meshBuilder(p, shape, positions, meshInd, indices, meshUV, uvs, meshCol, colors): void {
|
|
var i;
|
|
var i;
|
|
var u = 0;
|
|
var u = 0;
|
|
var c = 0;
|
|
var c = 0;
|
|
@@ -92,7 +92,7 @@ module BABYLON {
|
|
colors.push(meshCol[c], meshCol[c + 1], meshCol[c + 2], meshCol[c + 3]);
|
|
colors.push(meshCol[c], meshCol[c + 1], meshCol[c + 2], meshCol[c + 3]);
|
|
c += 4;
|
|
c += 4;
|
|
} else {
|
|
} else {
|
|
- colors.push(1,1,1,1);
|
|
|
|
|
|
+ colors.push(1, 1, 1, 1);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
for (i = 0; i < meshInd.length; i++) {
|
|
for (i = 0; i < meshInd.length; i++) {
|
|
@@ -104,7 +104,7 @@ module BABYLON {
|
|
private _posToShape(positions): Vector3[] {
|
|
private _posToShape(positions): Vector3[] {
|
|
var shape = [];
|
|
var shape = [];
|
|
for (var i = 0; i < positions.length; i += 3) {
|
|
for (var i = 0; i < positions.length; i += 3) {
|
|
- shape.push(new BABYLON.Vector3(positions[i], positions[i + 1], positions[i + 2]));
|
|
|
|
|
|
+ shape.push(new Vector3(positions[i], positions[i + 1], positions[i + 2]));
|
|
}
|
|
}
|
|
return shape;
|
|
return shape;
|
|
}
|
|
}
|
|
@@ -113,7 +113,7 @@ module BABYLON {
|
|
private _uvsToShapeUV(uvs): number[] {
|
|
private _uvsToShapeUV(uvs): number[] {
|
|
var shapeUV = [];
|
|
var shapeUV = [];
|
|
if (uvs) {
|
|
if (uvs) {
|
|
- shapeUV.push(uvs.x, uvs.y, uvs.z, uvs.w);
|
|
|
|
|
|
+ shapeUV.push(uvs.x, uvs.y, uvs.z, uvs.w);
|
|
}
|
|
}
|
|
return shapeUV;
|
|
return shapeUV;
|
|
}
|
|
}
|
|
@@ -140,14 +140,14 @@ module BABYLON {
|
|
this._index += shape.length;
|
|
this._index += shape.length;
|
|
}
|
|
}
|
|
this.nbParticles += nb;
|
|
this.nbParticles += nb;
|
|
- this._shapeCounter ++;
|
|
|
|
|
|
+ this._shapeCounter++;
|
|
return this._shapeCounter;
|
|
return this._shapeCounter;
|
|
}
|
|
}
|
|
|
|
|
|
// resets a particle back to its just built status
|
|
// resets a particle back to its just built status
|
|
public resetParticle(particle: SolidParticle): void {
|
|
public resetParticle(particle: SolidParticle): void {
|
|
for (var pt = 0; pt < particle._shape.length; pt++) {
|
|
for (var pt = 0; pt < particle._shape.length; pt++) {
|
|
- this._positions[particle._pos + pt * 3] = particle._shape[pt].x;
|
|
|
|
|
|
+ this._positions[particle._pos + pt * 3] = particle._shape[pt].x;
|
|
this._positions[particle._pos + pt * 3 + 1] = particle._shape[pt].y;
|
|
this._positions[particle._pos + pt * 3 + 1] = particle._shape[pt].y;
|
|
this._positions[particle._pos + pt * 3 + 2] = particle._shape[pt].z;
|
|
this._positions[particle._pos + pt * 3 + 2] = particle._shape[pt].z;
|
|
}
|
|
}
|
|
@@ -171,13 +171,13 @@ module BABYLON {
|
|
this._cam_axisZ.z = 1;
|
|
this._cam_axisZ.z = 1;
|
|
|
|
|
|
// if the particles will always face the camera
|
|
// if the particles will always face the camera
|
|
- if (this.billboard) {
|
|
|
|
|
|
+ if (this.billboard) {
|
|
// compute a fake camera position : un-rotate the camera position by the current mesh rotation
|
|
// compute a fake camera position : un-rotate the camera position by the current mesh rotation
|
|
this._yaw = this.mesh.rotation.y;
|
|
this._yaw = this.mesh.rotation.y;
|
|
this._pitch = this.mesh.rotation.x;
|
|
this._pitch = this.mesh.rotation.x;
|
|
this._roll = this.mesh.rotation.z;
|
|
this._roll = this.mesh.rotation.z;
|
|
this._quaternionRotationYPR();
|
|
this._quaternionRotationYPR();
|
|
- this._quaternionToRotationMatrix();
|
|
|
|
|
|
+ this._quaternionToRotationMatrix();
|
|
this._rotMatrix.invertToRef(this._invertedMatrix);
|
|
this._rotMatrix.invertToRef(this._invertedMatrix);
|
|
Vector3.TransformCoordinatesToRef(this._camera.globalPosition, this._invertedMatrix, this._fakeCamPos);
|
|
Vector3.TransformCoordinatesToRef(this._camera.globalPosition, this._invertedMatrix, this._fakeCamPos);
|
|
|
|
|
|
@@ -189,7 +189,6 @@ module BABYLON {
|
|
this._cam_axisX.normalize();
|
|
this._cam_axisX.normalize();
|
|
this._cam_axisZ.normalize();
|
|
this._cam_axisZ.normalize();
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
|
|
Matrix.IdentityToRef(this._rotMatrix);
|
|
Matrix.IdentityToRef(this._rotMatrix);
|
|
var idx = 0;
|
|
var idx = 0;
|
|
@@ -200,7 +199,7 @@ module BABYLON {
|
|
var uvIndex = 0;
|
|
var uvIndex = 0;
|
|
|
|
|
|
// particle loop
|
|
// particle loop
|
|
- for (var p = 0; p < this.nbParticles; p++) {
|
|
|
|
|
|
+ for (var p = 0; p < this.nbParticles; p++) {
|
|
this._particle = this.particles[p];
|
|
this._particle = this.particles[p];
|
|
|
|
|
|
// call to custom user function to update the particle properties
|
|
// call to custom user function to update the particle properties
|
|
@@ -223,7 +222,7 @@ module BABYLON {
|
|
this._roll = this._particle.rotation.z;
|
|
this._roll = this._particle.rotation.z;
|
|
this._quaternionRotationYPR();
|
|
this._quaternionRotationYPR();
|
|
}
|
|
}
|
|
- this._quaternionToRotationMatrix();
|
|
|
|
|
|
+ this._quaternionToRotationMatrix();
|
|
}
|
|
}
|
|
|
|
|
|
for (var pt = 0; pt < this._particle._shape.length; pt++) {
|
|
for (var pt = 0; pt < this._particle._shape.length; pt++) {
|
|
@@ -241,9 +240,9 @@ module BABYLON {
|
|
|
|
|
|
Vector3.TransformCoordinatesToRef(this._vertex, this._rotMatrix, this._rotated);
|
|
Vector3.TransformCoordinatesToRef(this._vertex, this._rotMatrix, this._rotated);
|
|
|
|
|
|
- this._positions[idx] = this._particle.position.x + this._cam_axisX.x * this._rotated.x * this._particle.scale.x + this._cam_axisY.x * this._rotated.y * this._particle.scale.y + this._cam_axisZ.x * this._rotated.z * this._particle.scale.z;
|
|
|
|
- this._positions[idx + 1] = this._particle.position.y + this._cam_axisX.y * this._rotated.x * this._particle.scale.x + this._cam_axisY.y * this._rotated.y * this._particle.scale.y + this._cam_axisZ.y * this._rotated.z * this._particle.scale.z;
|
|
|
|
- this._positions[idx + 2] = this._particle.position.z + this._cam_axisX.z * this._rotated.x * this._particle.scale.x + this._cam_axisY.z * this._rotated.y * this._particle.scale.y + this._cam_axisZ.z * this._rotated.z * this._particle.scale.z;
|
|
|
|
|
|
+ this._positions[idx] = this._particle.position.x + this._cam_axisX.x * this._rotated.x * this._particle.scale.x + this._cam_axisY.x * this._rotated.y * this._particle.scale.y + this._cam_axisZ.x * this._rotated.z * this._particle.scale.z;
|
|
|
|
+ this._positions[idx + 1] = this._particle.position.y + this._cam_axisX.y * this._rotated.x * this._particle.scale.x + this._cam_axisY.y * this._rotated.y * this._particle.scale.y + this._cam_axisZ.y * this._rotated.z * this._particle.scale.z;
|
|
|
|
+ this._positions[idx + 2] = this._particle.position.z + this._cam_axisX.z * this._rotated.x * this._particle.scale.x + this._cam_axisY.z * this._rotated.y * this._particle.scale.y + this._cam_axisZ.z * this._rotated.z * this._particle.scale.z;
|
|
|
|
|
|
if (this._useParticleColor) {
|
|
if (this._useParticleColor) {
|
|
this._colors[colidx] = this._particle.color.r;
|
|
this._colors[colidx] = this._particle.color.r;
|
|
@@ -270,13 +269,13 @@ module BABYLON {
|
|
}
|
|
}
|
|
this.mesh.updateVerticesData(VertexBuffer.PositionKind, this._positions, false, false);
|
|
this.mesh.updateVerticesData(VertexBuffer.PositionKind, this._positions, false, false);
|
|
if (!this.mesh.areNormalsFrozen) {
|
|
if (!this.mesh.areNormalsFrozen) {
|
|
- var indices = this.mesh.getIndices();
|
|
|
|
|
|
+ // var indices = this.mesh.getIndices();
|
|
VertexData.ComputeNormals(this._positions, this._indices, this._normals);
|
|
VertexData.ComputeNormals(this._positions, this._indices, this._normals);
|
|
this.mesh.updateVerticesData(VertexBuffer.NormalKind, this._normals, false, false);
|
|
this.mesh.updateVerticesData(VertexBuffer.NormalKind, this._normals, false, false);
|
|
}
|
|
}
|
|
this.afterUpdateParticles();
|
|
this.afterUpdateParticles();
|
|
}
|
|
}
|
|
- // internal implementation of BJS Quaternion.RotationYawPitchRollToRef() with memory reuse
|
|
|
|
|
|
+
|
|
private _quaternionRotationYPR(): void {
|
|
private _quaternionRotationYPR(): void {
|
|
this._halfroll = this._roll * 0.5;
|
|
this._halfroll = this._roll * 0.5;
|
|
this._halfpitch = this._pitch * 0.5;
|
|
this._halfpitch = this._pitch * 0.5;
|
|
@@ -292,8 +291,7 @@ module BABYLON {
|
|
this._quaternion.z = (this._cosYaw * this._cosPitch * this._sinRoll) - (this._sinYaw * this._sinPitch * this._cosRoll);
|
|
this._quaternion.z = (this._cosYaw * this._cosPitch * this._sinRoll) - (this._sinYaw * this._sinPitch * this._cosRoll);
|
|
this._quaternion.w = (this._cosYaw * this._cosPitch * this._cosRoll) + (this._sinYaw * this._sinPitch * this._sinRoll);
|
|
this._quaternion.w = (this._cosYaw * this._cosPitch * this._cosRoll) + (this._sinYaw * this._sinPitch * this._sinRoll);
|
|
}
|
|
}
|
|
-
|
|
|
|
- // internal implemenation of BJS toRotationMatric() with memory reuse
|
|
|
|
|
|
+
|
|
private _quaternionToRotationMatrix(): void {
|
|
private _quaternionToRotationMatrix(): void {
|
|
this._rotMatrix.m[0] = 1.0 - (2.0 * (this._quaternion.y * this._quaternion.y + this._quaternion.z * this._quaternion.z));
|
|
this._rotMatrix.m[0] = 1.0 - (2.0 * (this._quaternion.y * this._quaternion.y + this._quaternion.z * this._quaternion.z));
|
|
this._rotMatrix.m[1] = 2.0 * (this._quaternion.x * this._quaternion.y + this._quaternion.z * this._quaternion.w);
|
|
this._rotMatrix.m[1] = 2.0 * (this._quaternion.x * this._quaternion.y + this._quaternion.z * this._quaternion.w);
|
|
@@ -310,7 +308,7 @@ module BABYLON {
|
|
this._rotMatrix.m[12] = 0;
|
|
this._rotMatrix.m[12] = 0;
|
|
this._rotMatrix.m[13] = 0;
|
|
this._rotMatrix.m[13] = 0;
|
|
this._rotMatrix.m[14] = 0;
|
|
this._rotMatrix.m[14] = 0;
|
|
- this._rotMatrix.m[15] = 1.0
|
|
|
|
|
|
+ this._rotMatrix.m[15] = 1.0;
|
|
}
|
|
}
|
|
|
|
|
|
// dispose the SPS
|
|
// dispose the SPS
|
|
@@ -329,26 +327,26 @@ module BABYLON {
|
|
|
|
|
|
public set useParticleTexture(val: boolean) {
|
|
public set useParticleTexture(val: boolean) {
|
|
this._useParticleTexture = val;
|
|
this._useParticleTexture = val;
|
|
- }
|
|
|
|
|
|
+ }
|
|
|
|
|
|
public set useParticleVertex(val: boolean) {
|
|
public set useParticleVertex(val: boolean) {
|
|
this._useParticleVertex = val;
|
|
this._useParticleVertex = val;
|
|
}
|
|
}
|
|
|
|
|
|
// getters
|
|
// getters
|
|
- public get useParticleRotation() {
|
|
|
|
|
|
+ public get useParticleRotation(): boolean {
|
|
return this._useParticleRotation;
|
|
return this._useParticleRotation;
|
|
}
|
|
}
|
|
|
|
|
|
- public get useParticleColor() {
|
|
|
|
|
|
+ public get useParticleColor(): boolean {
|
|
return this._useParticleColor;
|
|
return this._useParticleColor;
|
|
}
|
|
}
|
|
|
|
|
|
- public get useParticleTexture() {
|
|
|
|
|
|
+ public get useParticleTexture(): boolean {
|
|
return this._useParticleTexture;
|
|
return this._useParticleTexture;
|
|
- }
|
|
|
|
|
|
+ }
|
|
|
|
|
|
- public get useParticleVertex() {
|
|
|
|
|
|
+ public get useParticleVertex(): boolean {
|
|
return this._useParticleVertex;
|
|
return this._useParticleVertex;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -360,7 +358,7 @@ module BABYLON {
|
|
|
|
|
|
// init : sets all particles first values and calls updateParticle to set them in space
|
|
// init : sets all particles first values and calls updateParticle to set them in space
|
|
// can be overwritten by the user
|
|
// can be overwritten by the user
|
|
- public initParticles(): void {
|
|
|
|
|
|
+ public initParticles(): void {
|
|
}
|
|
}
|
|
|
|
|
|
// recycles a particle : can by overwritten by the user
|
|
// recycles a particle : can by overwritten by the user
|
|
@@ -371,7 +369,7 @@ module BABYLON {
|
|
// updates a particle : can be overwritten by the user
|
|
// updates a particle : can be overwritten by the user
|
|
// will be called on each particle by setParticles() :
|
|
// will be called on each particle by setParticles() :
|
|
// ex : just set a particle position or velocity and recycle conditions
|
|
// ex : just set a particle position or velocity and recycle conditions
|
|
- public updateParticle (particle: SolidParticle): SolidParticle {
|
|
|
|
|
|
+ public updateParticle(particle: SolidParticle): SolidParticle {
|
|
return particle;
|
|
return particle;
|
|
}
|
|
}
|
|
|
|
|