babylon.solidParticleSystem.ts 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. module BABYLON {
  2. export class SolidParticleSystem implements IDisposable {
  3. // public members
  4. public particles: SolidParticle[] = new Array<SolidParticle>();
  5. public nbParticles: number = 0;
  6. public billboard: boolean = false;
  7. public counter: number = 0;
  8. public name: string;
  9. public mesh: Mesh;
  10. // private members
  11. private _scene: Scene;
  12. private _positions: number[] = new Array<number>();
  13. private _indices: number[] = new Array<number>();
  14. private _normals: number[] = new Array<number>();
  15. private _colors: number[] = new Array<number>();
  16. private _uvs: number[] = new Array<number>();
  17. private _positions32: Float32Array;
  18. private _indices32: Float32Array;
  19. private _normals32: Float32Array;
  20. private _colors32: Float32Array;
  21. private _uvs32: Float32Array;
  22. private _index: number = 0; // indices index
  23. private _shapeCounter: number = 0;
  24. private _computeParticleColor: boolean = true;
  25. private _computeParticleTexture: boolean = true;
  26. private _computeParticleRotation: boolean = true;
  27. private _computeParticleVertex: boolean = false;
  28. private _cam_axisZ: Vector3 = Vector3.Zero();
  29. private _cam_axisY: Vector3 = Vector3.Zero();
  30. private _cam_axisX: Vector3 = Vector3.Zero();
  31. private _axisX: Vector3 = Axis.X;
  32. private _axisY: Vector3 = Axis.Y;
  33. private _axisZ: Vector3 = Axis.Z;
  34. private _camera: Camera;
  35. private _particle: SolidParticle;
  36. private _previousParticle: SolidParticle;
  37. private _fakeCamPos: Vector3 = Vector3.Zero();
  38. private _rotMatrix: Matrix = new Matrix();
  39. private _invertedMatrix: Matrix = new Matrix();
  40. private _rotated: Vector3 = Vector3.Zero();
  41. private _quaternion: Quaternion = new Quaternion();
  42. private _vertex: Vector3 = Vector3.Zero();
  43. private _yaw: number = 0.0;
  44. private _pitch: number = 0.0;
  45. private _roll: number = 0.0;
  46. private _halfroll: number = 0.0;
  47. private _halfpitch: number = 0.0;
  48. private _halfyaw: number = 0.0;
  49. private _sinRoll: number = 0.0;
  50. private _cosRoll: number = 0.0;
  51. private _sinPitch: number = 0.0;
  52. private _cosPitch: number = 0.0;
  53. private _sinYaw: number = 0.0;
  54. private _cosYaw: number = 0.0;
  55. constructor(name: string, scene: Scene) {
  56. this.name = name;
  57. this._scene = scene;
  58. this._camera = scene.activeCamera;
  59. }
  60. // build the SPS mesh : returns the mesh
  61. public buildMesh(upgradable: boolean = true): Mesh {
  62. if (this.nbParticles === 0) {
  63. var triangle = MeshBuilder.CreateDisc("", { radius: 1, tessellation: 3 }, this._scene);
  64. this.addShape(triangle, 1);
  65. triangle.dispose();
  66. }
  67. this._positions32 = new Float32Array(this._positions);
  68. this._indices32 = new Float32Array(this._indices);
  69. this._uvs32 = new Float32Array(this._uvs);
  70. this._colors32 = new Float32Array(this._colors);
  71. VertexData.ComputeNormals(this._positions32, this._indices32, this._normals);
  72. this._normals32 = new Float32Array(this._normals);
  73. var vertexData = new VertexData();
  74. vertexData.set(this._positions32, VertexBuffer.PositionKind);
  75. vertexData.indices = this._indices;
  76. vertexData.set(this._normals32, VertexBuffer.NormalKind);
  77. if (this._uvs32) {
  78. vertexData.set(this._uvs32, VertexBuffer.UVKind);;
  79. }
  80. if (this._colors32) {
  81. vertexData.set(this._colors32, VertexBuffer.ColorKind);
  82. }
  83. var mesh = new Mesh(name, this._scene);
  84. vertexData.applyToMesh(mesh, upgradable);
  85. this.mesh = mesh;
  86. // free memory
  87. this._positions = null;
  88. this._indices = null;
  89. this._normals = null;
  90. this._uvs = null;
  91. this._colors = null;
  92. return mesh;
  93. }
  94. // _meshBuilder : inserts the shape model in the global SPS mesh
  95. private _meshBuilder(p, shape, positions, meshInd, indices, meshUV, uvs, meshCol, colors): void {
  96. var i;
  97. var u = 0;
  98. var c = 0;
  99. for (i = 0; i < shape.length; i++) {
  100. positions.push(shape[i].x, shape[i].y, shape[i].z);
  101. if (meshUV) {
  102. uvs.push(meshUV[u], meshUV[u + 1]);
  103. u += 2;
  104. }
  105. if (meshCol) {
  106. colors.push(meshCol[c] || 1, meshCol[c + 1] || 1, meshCol[c + 2] || 1, meshCol[c + 3] || 1);
  107. c += 4;
  108. } else {
  109. colors.push(1, 1, 1, 1);
  110. }
  111. }
  112. for (i = 0; i < meshInd.length; i++) {
  113. indices.push(p + meshInd[i]);
  114. }
  115. }
  116. // returns a shape array from positions array
  117. private _posToShape(positions): Vector3[] {
  118. var shape = [];
  119. for (var i = 0; i < positions.length; i += 3) {
  120. shape.push(new Vector3(positions[i], positions[i + 1], positions[i + 2]));
  121. }
  122. return shape;
  123. }
  124. // returns a shapeUV array from a Vector4 uvs
  125. private _uvsToShapeUV(uvs): number[] {
  126. var shapeUV = [];
  127. if (uvs) {
  128. for (var i = 0; i < uvs.length; i++)
  129. shapeUV.push(uvs[i]);
  130. }
  131. return shapeUV;
  132. }
  133. // adds a new particle object in the particles array and double links the particle (next/previous)
  134. private _addParticle(p: number, idxpos: number, shape: Vector3[], shapeUV: number[], shapeId: number): void {
  135. this._particle = new SolidParticle(p, idxpos, shape, shapeUV, shapeId);
  136. this.particles.push(this._particle);
  137. this._particle.previous = this._previousParticle;
  138. if (this._previousParticle) {
  139. this._previousParticle.next = this._particle;
  140. }
  141. this._previousParticle = this._particle;
  142. }
  143. // add solid particles from a shape model in the particles array
  144. public addShape(mesh: Mesh, nb: number): number {
  145. var meshPos = mesh.getVerticesData(VertexBuffer.PositionKind);
  146. var meshInd = mesh.getIndices();
  147. var meshUV = mesh.getVerticesData(VertexBuffer.UVKind);
  148. var meshCol = mesh.getVerticesData(VertexBuffer.ColorKind);
  149. var shape = this._posToShape(meshPos);
  150. var shapeUV = this._uvsToShapeUV(meshUV);
  151. // particles
  152. for (var i = 0; i < nb; i++) {
  153. this._meshBuilder(this._index, shape, this._positions, meshInd, this._indices, meshUV, this._uvs, meshCol, this._colors);
  154. this._addParticle(this.nbParticles + i, this._positions.length, shape, shapeUV, this._shapeCounter);
  155. this._index += shape.length;
  156. }
  157. this.nbParticles += nb;
  158. this._shapeCounter++;
  159. return this._shapeCounter;
  160. }
  161. // resets a particle back to its just built status
  162. public resetParticle(particle: SolidParticle): void {
  163. for (var pt = 0; pt < particle._shape.length; pt++) {
  164. this._positions[particle._pos + pt * 3] = particle._shape[pt].x;
  165. this._positions[particle._pos + pt * 3 + 1] = particle._shape[pt].y;
  166. this._positions[particle._pos + pt * 3 + 2] = particle._shape[pt].z;
  167. }
  168. }
  169. // sets all the particles
  170. public setParticles(start: number = 0, end: number = this.nbParticles - 1, update: boolean = true): void {
  171. // custom beforeUpdate
  172. this.beforeUpdateParticles(start, end, update);
  173. this._cam_axisX.x = 1;
  174. this._cam_axisX.y = 0;
  175. this._cam_axisX.z = 0;
  176. this._cam_axisY.x = 0;
  177. this._cam_axisY.y = 1;
  178. this._cam_axisY.z = 0;
  179. this._cam_axisZ.x = 0;
  180. this._cam_axisZ.y = 0;
  181. this._cam_axisZ.z = 1;
  182. // if the particles will always face the camera
  183. if (this.billboard) {
  184. // compute a fake camera position : un-rotate the camera position by the current mesh rotation
  185. this._yaw = this.mesh.rotation.y;
  186. this._pitch = this.mesh.rotation.x;
  187. this._roll = this.mesh.rotation.z;
  188. this._quaternionRotationYPR();
  189. this._quaternionToRotationMatrix();
  190. this._rotMatrix.invertToRef(this._invertedMatrix);
  191. Vector3.TransformCoordinatesToRef(this._camera.globalPosition, this._invertedMatrix, this._fakeCamPos);
  192. // set two orthogonal vectors (_cam_axisX and and _cam_axisY) to the cam-mesh axis (_cam_axisZ)
  193. (this._fakeCamPos).subtractToRef(this.mesh.position, this._cam_axisZ);
  194. Vector3.CrossToRef(this._cam_axisZ, this._axisX, this._cam_axisY);
  195. Vector3.CrossToRef(this._cam_axisZ, this._cam_axisY, this._cam_axisX);
  196. this._cam_axisY.normalize();
  197. this._cam_axisX.normalize();
  198. this._cam_axisZ.normalize();
  199. }
  200. Matrix.IdentityToRef(this._rotMatrix);
  201. var idx = 0;
  202. var index = 0;
  203. var colidx = 0;
  204. var colorIndex = 0;
  205. var uvidx = 0;
  206. var uvIndex = 0;
  207. // particle loop
  208. for (var p = start; p <= end; p++) {
  209. this._particle = this.particles[p];
  210. // call to custom user function to update the particle properties
  211. this.updateParticle(this._particle);
  212. // particle rotation matrix
  213. if (this.billboard) {
  214. this._particle.rotation.x = 0.0;
  215. this._particle.rotation.y = 0.0;
  216. }
  217. if (this._computeParticleRotation) {
  218. if (this._particle.quaternion) {
  219. this._quaternion.x = this._particle.quaternion.x;
  220. this._quaternion.y = this._particle.quaternion.y;
  221. this._quaternion.z = this._particle.quaternion.z;
  222. this._quaternion.w = this._particle.quaternion.w;
  223. } else {
  224. this._yaw = this._particle.rotation.y;
  225. this._pitch = this._particle.rotation.x;
  226. this._roll = this._particle.rotation.z;
  227. this._quaternionRotationYPR();
  228. }
  229. this._quaternionToRotationMatrix();
  230. }
  231. for (var pt = 0; pt < this._particle._shape.length; pt++) {
  232. idx = index + pt * 3;
  233. colidx = colorIndex + pt * 4;
  234. uvidx = uvIndex + pt * 2;
  235. this._vertex.x = this._particle._shape[pt].x * this._particle.scale.x;
  236. this._vertex.y = this._particle._shape[pt].y * this._particle.scale.y;
  237. this._vertex.z = this._particle._shape[pt].z * this._particle.scale.z;
  238. if (this._computeParticleVertex) {
  239. this.updateParticleVertex(this._particle, this._vertex, pt);
  240. }
  241. Vector3.TransformCoordinatesToRef(this._vertex, this._rotMatrix, this._rotated);
  242. this._positions32[idx] = this._particle.position.x + this._cam_axisX.x * this._rotated.x + this._cam_axisY.x * this._rotated.y + this._cam_axisZ.x * this._rotated.z;
  243. this._positions32[idx + 1] = this._particle.position.y + this._cam_axisX.y * this._rotated.x + this._cam_axisY.y * this._rotated.y + this._cam_axisZ.y * this._rotated.z;
  244. this._positions32[idx + 2] = this._particle.position.z + this._cam_axisX.z * this._rotated.x + this._cam_axisY.z * this._rotated.y + this._cam_axisZ.z * this._rotated.z;
  245. if (this._computeParticleColor) {
  246. this._colors32[colidx] = this._particle.color.r;
  247. this._colors32[colidx + 1] = this._particle.color.g;
  248. this._colors32[colidx + 2] = this._particle.color.b;
  249. this._colors32[colidx + 3] = this._particle.color.a;
  250. }
  251. if (this._computeParticleTexture) {
  252. this._uvs32[uvidx] = this._particle._shapeUV[pt * 2] * (this._particle.uvs.z - this._particle.uvs.x) + this._particle.uvs.x;
  253. this._uvs32[uvidx + 1] = this._particle._shapeUV[pt * 2 + 1] * (this._particle.uvs.w - this._particle.uvs.y) + this._particle.uvs.y;
  254. }
  255. }
  256. index = idx + 3;
  257. colorIndex = colidx + 4;
  258. uvIndex = uvidx + 2;
  259. }
  260. if (update) {
  261. if (this._computeParticleColor) {
  262. this.mesh.updateVerticesData(VertexBuffer.ColorKind, this._colors32, false, false);
  263. }
  264. if (this._computeParticleTexture) {
  265. this.mesh.updateVerticesData(VertexBuffer.UVKind, this._uvs32, false, false);
  266. }
  267. this.mesh.updateVerticesData(VertexBuffer.PositionKind, this._positions32, false, false);
  268. if (!this.mesh.areNormalsFrozen) {
  269. VertexData.ComputeNormals(this._positions32, this._indices32, this._normals32);
  270. this.mesh.updateVerticesData(VertexBuffer.NormalKind, this._normals32, false, false);
  271. }
  272. }
  273. this.afterUpdateParticles(start, end, update);
  274. }
  275. private _quaternionRotationYPR(): void {
  276. this._halfroll = this._roll * 0.5;
  277. this._halfpitch = this._pitch * 0.5;
  278. this._halfyaw = this._yaw * 0.5;
  279. this._sinRoll = Math.sin(this._halfroll);
  280. this._cosRoll = Math.cos(this._halfroll);
  281. this._sinPitch = Math.sin(this._halfpitch);
  282. this._cosPitch = Math.cos(this._halfpitch);
  283. this._sinYaw = Math.sin(this._halfyaw);
  284. this._cosYaw = Math.cos(this._halfyaw);
  285. this._quaternion.x = (this._cosYaw * this._sinPitch * this._cosRoll) + (this._sinYaw * this._cosPitch * this._sinRoll);
  286. this._quaternion.y = (this._sinYaw * this._cosPitch * this._cosRoll) - (this._cosYaw * this._sinPitch * this._sinRoll);
  287. this._quaternion.z = (this._cosYaw * this._cosPitch * this._sinRoll) - (this._sinYaw * this._sinPitch * this._cosRoll);
  288. this._quaternion.w = (this._cosYaw * this._cosPitch * this._cosRoll) + (this._sinYaw * this._sinPitch * this._sinRoll);
  289. }
  290. private _quaternionToRotationMatrix(): void {
  291. this._rotMatrix.m[0] = 1.0 - (2.0 * (this._quaternion.y * this._quaternion.y + this._quaternion.z * this._quaternion.z));
  292. this._rotMatrix.m[1] = 2.0 * (this._quaternion.x * this._quaternion.y + this._quaternion.z * this._quaternion.w);
  293. this._rotMatrix.m[2] = 2.0 * (this._quaternion.z * this._quaternion.x - this._quaternion.y * this._quaternion.w);
  294. this._rotMatrix.m[3] = 0;
  295. this._rotMatrix.m[4] = 2.0 * (this._quaternion.x * this._quaternion.y - this._quaternion.z * this._quaternion.w);
  296. this._rotMatrix.m[5] = 1.0 - (2.0 * (this._quaternion.z * this._quaternion.z + this._quaternion.x * this._quaternion.x));
  297. this._rotMatrix.m[6] = 2.0 * (this._quaternion.y * this._quaternion.z + this._quaternion.x * this._quaternion.w);
  298. this._rotMatrix.m[7] = 0;
  299. this._rotMatrix.m[8] = 2.0 * (this._quaternion.z * this._quaternion.x + this._quaternion.y * this._quaternion.w);
  300. this._rotMatrix.m[9] = 2.0 * (this._quaternion.y * this._quaternion.z - this._quaternion.x * this._quaternion.w);
  301. this._rotMatrix.m[10] = 1.0 - (2.0 * (this._quaternion.y * this._quaternion.y + this._quaternion.x * this._quaternion.x));
  302. this._rotMatrix.m[11] = 0;
  303. this._rotMatrix.m[12] = 0;
  304. this._rotMatrix.m[13] = 0;
  305. this._rotMatrix.m[14] = 0;
  306. this._rotMatrix.m[15] = 1.0;
  307. }
  308. // dispose the SPS
  309. public dispose(): void {
  310. this.mesh.dispose();
  311. }
  312. // Optimizer setters
  313. public set computeParticleRotation(val: boolean) {
  314. this._computeParticleRotation = val;
  315. }
  316. public set computeParticleColor(val: boolean) {
  317. this._computeParticleColor = val;
  318. }
  319. public set computeParticleTexture(val: boolean) {
  320. this._computeParticleTexture = val;
  321. }
  322. public set computeParticleVertex(val: boolean) {
  323. this._computeParticleVertex = val;
  324. }
  325. // getters
  326. public get computeParticleRotation(): boolean {
  327. return this._computeParticleRotation;
  328. }
  329. public get computeParticleColor(): boolean {
  330. return this._computeParticleColor;
  331. }
  332. public get computeParticleTexture(): boolean {
  333. return this._computeParticleTexture;
  334. }
  335. public get computeParticleVertex(): boolean {
  336. return this._computeParticleVertex;
  337. }
  338. // =======================================================================
  339. // Particle behavior logic
  340. // these following methods may be overwritten by the user to fit his needs
  341. // init : sets all particles first values and calls updateParticle to set them in space
  342. // can be overwritten by the user
  343. public initParticles(): void {
  344. }
  345. // recycles a particle : can by overwritten by the user
  346. public recycleParticle(particle: SolidParticle): SolidParticle {
  347. return particle;
  348. }
  349. // updates a particle : can be overwritten by the user
  350. // will be called on each particle by setParticles() :
  351. // ex : just set a particle position or velocity and recycle conditions
  352. public updateParticle(particle: SolidParticle): SolidParticle {
  353. return particle;
  354. }
  355. // updates a vertex of a particle : can be overwritten by the user
  356. // will be called on each vertex particle by setParticles() :
  357. // ex : just set a vertex particle position
  358. public updateParticleVertex(particle: SolidParticle, vertex: Vector3, i: number): Vector3 {
  359. return vertex;
  360. }
  361. // will be called before any other treatment by setParticles()
  362. public beforeUpdateParticles(start?: number, stop?: number, update?: boolean): void {
  363. }
  364. // will be called after all setParticles() treatments
  365. public afterUpdateParticles(start?: number, stop?: number, update?: boolean): void {
  366. }
  367. }
  368. }