babylon.solidParticleSystem.ts 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  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. public vars: any = {};
  11. // private members
  12. private _scene: Scene;
  13. private _positions: number[] = new Array<number>();
  14. private _indices: number[] = new Array<number>();
  15. private _normals: number[] = new Array<number>();
  16. private _colors: number[] = new Array<number>();
  17. private _uvs: number[] = new Array<number>();
  18. private _positions32: Float32Array;
  19. private _normals32: Float32Array; // updated normals for the VBO
  20. private _fixedNormal32: Float32Array; // initial normal references
  21. private _colors32: Float32Array;
  22. private _uvs32: Float32Array;
  23. private _index: number = 0; // indices index
  24. private _updatable: boolean = true;
  25. private _shapeCounter: number = 0;
  26. private _copy: SolidParticle = new SolidParticle(null, null, null, null, null);
  27. private _shape: Vector3[];
  28. private _shapeUV: number[];
  29. private _color: Color4 = new Color4(0, 0, 0, 0);
  30. private _computeParticleColor: boolean = true;
  31. private _computeParticleTexture: boolean = true;
  32. private _computeParticleRotation: boolean = true;
  33. private _computeParticleVertex: boolean = false;
  34. private _cam_axisZ: Vector3 = Vector3.Zero();
  35. private _cam_axisY: Vector3 = Vector3.Zero();
  36. private _cam_axisX: Vector3 = Vector3.Zero();
  37. private _axisX: Vector3 = Axis.X;
  38. private _axisY: Vector3 = Axis.Y;
  39. private _axisZ: Vector3 = Axis.Z;
  40. private _camera: Camera;
  41. private _particle: SolidParticle;
  42. private _fakeCamPos: Vector3 = Vector3.Zero();
  43. private _rotMatrix: Matrix = new Matrix();
  44. private _invertedMatrix: Matrix = new Matrix();
  45. private _rotated: Vector3 = Vector3.Zero();
  46. private _quaternion: Quaternion = new Quaternion();
  47. private _vertex: Vector3 = Vector3.Zero();
  48. private _normal: Vector3 = Vector3.Zero();
  49. private _yaw: number = 0.0;
  50. private _pitch: number = 0.0;
  51. private _roll: number = 0.0;
  52. private _halfroll: number = 0.0;
  53. private _halfpitch: number = 0.0;
  54. private _halfyaw: number = 0.0;
  55. private _sinRoll: number = 0.0;
  56. private _cosRoll: number = 0.0;
  57. private _sinPitch: number = 0.0;
  58. private _cosPitch: number = 0.0;
  59. private _sinYaw: number = 0.0;
  60. private _cosYaw: number = 0.0;
  61. private _w: number = 0.0;
  62. constructor(name: string, scene: Scene, options?: { updatable?: boolean }) {
  63. this.name = name;
  64. this._scene = scene;
  65. this._camera = scene.activeCamera;
  66. if (options && options.updatable) {
  67. this._updatable = options.updatable;
  68. } else {
  69. this._updatable = true;
  70. }
  71. }
  72. // build the SPS mesh : returns the mesh
  73. public buildMesh(): Mesh {
  74. if (this.nbParticles === 0) {
  75. var triangle = MeshBuilder.CreateDisc("", { radius: 1, tessellation: 3 }, this._scene);
  76. this.addShape(triangle, 1);
  77. triangle.dispose();
  78. }
  79. this._positions32 = new Float32Array(this._positions);
  80. this._uvs32 = new Float32Array(this._uvs);
  81. this._colors32 = new Float32Array(this._colors);
  82. VertexData.ComputeNormals(this._positions32, this._indices, this._normals);
  83. this._normals32 = new Float32Array(this._normals);
  84. this._fixedNormal32 = new Float32Array(this._normals);
  85. var vertexData = new VertexData();
  86. vertexData.set(this._positions32, VertexBuffer.PositionKind);
  87. vertexData.indices = this._indices;
  88. vertexData.set(this._normals32, VertexBuffer.NormalKind);
  89. if (this._uvs32) {
  90. vertexData.set(this._uvs32, VertexBuffer.UVKind);;
  91. }
  92. if (this._colors32) {
  93. vertexData.set(this._colors32, VertexBuffer.ColorKind);
  94. }
  95. var mesh = new Mesh(name, this._scene);
  96. vertexData.applyToMesh(mesh, this._updatable);
  97. this.mesh = mesh;
  98. // free memory
  99. this._positions = null;
  100. this._normals = null;
  101. this._uvs = null;
  102. this._colors = null;
  103. if (!this._updatable) {
  104. this.particles.length = 0;
  105. }
  106. return mesh;
  107. }
  108. //reset copy
  109. private _resetCopy() {
  110. this._copy.position.x = 0;
  111. this._copy.position.y = 0;
  112. this._copy.position.z = 0;
  113. this._copy.rotation.x = 0;
  114. this._copy.rotation.y = 0;
  115. this._copy.rotation.z = 0;
  116. this._copy.quaternion = null;
  117. this._copy.scale.x = 1;
  118. this._copy.scale.y = 1;
  119. this._copy.scale.z = 1;
  120. this._copy.uvs.x = 0;
  121. this._copy.uvs.y = 0;
  122. this._copy.uvs.z = 1;
  123. this._copy.uvs.w = 1;
  124. this._copy.color = null;
  125. }
  126. // _meshBuilder : inserts the shape model in the global SPS mesh
  127. private _meshBuilder(p, shape, positions, meshInd, indices, meshUV, uvs, meshCol, colors, idx, idxInShape, options): void {
  128. var i;
  129. var u = 0;
  130. var c = 0;
  131. this._resetCopy();
  132. if (options && options.positionFunction) { // call to custom positionFunction
  133. options.positionFunction(this._copy, idx, idxInShape);
  134. }
  135. if (this._copy.quaternion) {
  136. this._quaternion.x = this._copy.quaternion.x;
  137. this._quaternion.y = this._copy.quaternion.y;
  138. this._quaternion.z = this._copy.quaternion.z;
  139. this._quaternion.w = this._copy.quaternion.w;
  140. } else {
  141. this._yaw = this._copy.rotation.y;
  142. this._pitch = this._copy.rotation.x;
  143. this._roll = this._copy.rotation.z;
  144. this._quaternionRotationYPR();
  145. }
  146. this._quaternionToRotationMatrix();
  147. for (i = 0; i < shape.length; i++) {
  148. this._vertex.x = shape[i].x;
  149. this._vertex.y = shape[i].y;
  150. this._vertex.z = shape[i].z;
  151. if (options && options.vertexFunction) {
  152. options.vertexFunction(this._copy, this._vertex, i);
  153. }
  154. this._vertex.x *= this._copy.scale.x;
  155. this._vertex.y *= this._copy.scale.y;
  156. this._vertex.z *= this._copy.scale.z;
  157. Vector3.TransformCoordinatesToRef(this._vertex, this._rotMatrix, this._rotated);
  158. positions.push(this._copy.position.x + this._rotated.x, this._copy.position.y + this._rotated.y, this._copy.position.z + this._rotated.z);
  159. if (meshUV) {
  160. uvs.push((this._copy.uvs.z - this._copy.uvs.x) * meshUV[u] + this._copy.uvs.x, (this._copy.uvs.w - this._copy.uvs.y) * meshUV[u + 1] + this._copy.uvs.y);
  161. u += 2;
  162. }
  163. if (this._copy.color) {
  164. this._color = this._copy.color;
  165. } else if (meshCol && meshCol[c]) {
  166. this._color.r = meshCol[c];
  167. this._color.g = meshCol[c + 1];
  168. this._color.b = meshCol[c + 2];
  169. this._color.a = meshCol[c + 3];
  170. } else {
  171. this._color.r = 1;
  172. this._color.g = 1;
  173. this._color.b = 1;
  174. this._color.a = 1;
  175. }
  176. colors.push(this._color.r, this._color.g, this._color.b, this._color.a);
  177. c += 4;
  178. }
  179. for (i = 0; i < meshInd.length; i++) {
  180. indices.push(p + meshInd[i]);
  181. }
  182. }
  183. // returns a shape array from positions array
  184. private _posToShape(positions): Vector3[] {
  185. var shape = [];
  186. for (var i = 0; i < positions.length; i += 3) {
  187. shape.push(new Vector3(positions[i], positions[i + 1], positions[i + 2]));
  188. }
  189. return shape;
  190. }
  191. // returns a shapeUV array from a Vector4 uvs
  192. private _uvsToShapeUV(uvs): number[] {
  193. var shapeUV = [];
  194. if (uvs) {
  195. for (var i = 0; i < uvs.length; i++)
  196. shapeUV.push(uvs[i]);
  197. }
  198. return shapeUV;
  199. }
  200. // adds a new particle object in the particles array
  201. private _addParticle(p: number, idxpos: number, model: ModelShape, shapeId: number, idxInShape: number): void {
  202. this.particles.push(new SolidParticle(p, idxpos, model, shapeId, idxInShape));
  203. }
  204. // add solid particles from a shape model in the particles array
  205. public addShape(mesh: Mesh, nb: number, options?: { positionFunction?: any, vertexFunction?: any }): number {
  206. var meshPos = mesh.getVerticesData(VertexBuffer.PositionKind);
  207. var meshInd = mesh.getIndices();
  208. var meshUV = mesh.getVerticesData(VertexBuffer.UVKind);
  209. var meshCol = mesh.getVerticesData(VertexBuffer.ColorKind);
  210. var shape = this._posToShape(meshPos);
  211. var shapeUV = this._uvsToShapeUV(meshUV);
  212. var posfunc = options ? options.positionFunction : null;
  213. var vtxfunc = options ? options.vertexFunction : null;
  214. var modelShape = new ModelShape(this._shapeCounter, shape, shapeUV, posfunc, vtxfunc);
  215. // particles
  216. for (var i = 0; i < nb; i++) {
  217. this._meshBuilder(this._index, shape, this._positions, meshInd, this._indices, meshUV, this._uvs, meshCol, this._colors, this.nbParticles + i, i, options);
  218. if (this._updatable) {
  219. this._addParticle(this.nbParticles + i, this._positions.length, modelShape, this._shapeCounter, i);
  220. }
  221. this._index += shape.length;
  222. }
  223. this.nbParticles += nb;
  224. this._shapeCounter++;
  225. return this._shapeCounter;
  226. }
  227. // rebuilds a particle back to its just built status : if needed, recomputes the custom positions and vertices
  228. private _rebuildParticle(particle: SolidParticle): void {
  229. this._resetCopy();
  230. if (particle._model._positionFunction) { // recall to stored custom positionFunction
  231. particle._model._positionFunction(this._copy, particle.idx, particle.idxInShape);
  232. }
  233. if (this._copy.quaternion) {
  234. this._quaternion.x = this._copy.quaternion.x;
  235. this._quaternion.y = this._copy.quaternion.y;
  236. this._quaternion.z = this._copy.quaternion.z;
  237. this._quaternion.w = this._copy.quaternion.w;
  238. } else {
  239. this._yaw = this._copy.rotation.y;
  240. this._pitch = this._copy.rotation.x;
  241. this._roll = this._copy.rotation.z;
  242. this._quaternionRotationYPR();
  243. }
  244. this._quaternionToRotationMatrix();
  245. this._shape = particle._model._shape;
  246. for (var pt = 0; pt < this._shape.length; pt++) {
  247. this._vertex.x = this._shape[pt].x;
  248. this._vertex.y = this._shape[pt].y;
  249. this._vertex.z = this._shape[pt].z;
  250. if (particle._model._vertexFunction) {
  251. particle._model._vertexFunction(this._copy, this._vertex, pt); // recall to stored vertexFunction
  252. }
  253. this._vertex.x *= this._copy.scale.x;
  254. this._vertex.y *= this._copy.scale.y;
  255. this._vertex.z *= this._copy.scale.z;
  256. Vector3.TransformCoordinatesToRef(this._vertex, this._rotMatrix, this._rotated);
  257. this._positions32[particle._pos + pt * 3] = this._copy.position.x + this._rotated.x;
  258. this._positions32[particle._pos + pt * 3 + 1] = this._copy.position.y + this._rotated.y;
  259. this._positions32[particle._pos + pt * 3 + 2] = this._copy.position.z + this._rotated.z;
  260. }
  261. particle.position.x = 0;
  262. particle.position.y = 0;
  263. particle.position.z = 0;
  264. particle.rotation.x = 0;
  265. particle.rotation.y = 0;
  266. particle.rotation.z = 0;
  267. particle.quaternion = null;
  268. particle.scale.x = 1;
  269. particle.scale.y = 1;
  270. particle.scale.z = 1;
  271. }
  272. // rebuilds the whole mesh and updates the VBO : custom positions and vertices are recomputed if needed
  273. public rebuildMesh(): void {
  274. for (var p = 0; p < this.particles.length; p++) {
  275. this._rebuildParticle(this.particles[p]);
  276. }
  277. this.mesh.updateVerticesData(VertexBuffer.PositionKind, this._positions32, false, false);
  278. }
  279. // sets all the particles : updates the VBO
  280. public setParticles(start: number = 0, end: number = this.nbParticles - 1, update: boolean = true): void {
  281. if (!this._updatable) {
  282. return;
  283. }
  284. // custom beforeUpdate
  285. this.beforeUpdateParticles(start, end, update);
  286. this._cam_axisX.x = 1;
  287. this._cam_axisX.y = 0;
  288. this._cam_axisX.z = 0;
  289. this._cam_axisY.x = 0;
  290. this._cam_axisY.y = 1;
  291. this._cam_axisY.z = 0;
  292. this._cam_axisZ.x = 0;
  293. this._cam_axisZ.y = 0;
  294. this._cam_axisZ.z = 1;
  295. // if the particles will always face the camera
  296. if (this.billboard) {
  297. // compute a fake camera position : un-rotate the camera position by the current mesh rotation
  298. this._yaw = this.mesh.rotation.y;
  299. this._pitch = this.mesh.rotation.x;
  300. this._roll = this.mesh.rotation.z;
  301. this._quaternionRotationYPR();
  302. this._quaternionToRotationMatrix();
  303. this._rotMatrix.invertToRef(this._invertedMatrix);
  304. Vector3.TransformCoordinatesToRef(this._camera.globalPosition, this._invertedMatrix, this._fakeCamPos);
  305. // set two orthogonal vectors (_cam_axisX and and _cam_axisY) to the cam-mesh axis (_cam_axisZ)
  306. (this._fakeCamPos).subtractToRef(this.mesh.position, this._cam_axisZ);
  307. Vector3.CrossToRef(this._cam_axisZ, this._axisX, this._cam_axisY);
  308. Vector3.CrossToRef(this._cam_axisZ, this._cam_axisY, this._cam_axisX);
  309. this._cam_axisY.normalize();
  310. this._cam_axisX.normalize();
  311. this._cam_axisZ.normalize();
  312. }
  313. Matrix.IdentityToRef(this._rotMatrix);
  314. var idx = 0;
  315. var index = 0;
  316. var colidx = 0;
  317. var colorIndex = 0;
  318. var uvidx = 0;
  319. var uvIndex = 0;
  320. // particle loop
  321. end = (end > this.nbParticles - 1) ? this.nbParticles - 1 : end;
  322. for (var p = start; p <= end; p++) {
  323. this._particle = this.particles[p];
  324. this._shape = this._particle._model._shape;
  325. this._shapeUV = this._particle._model._shapeUV;
  326. // call to custom user function to update the particle properties
  327. this.updateParticle(this._particle);
  328. // particle rotation matrix
  329. if (this.billboard) {
  330. this._particle.rotation.x = 0.0;
  331. this._particle.rotation.y = 0.0;
  332. }
  333. if (this._computeParticleRotation) {
  334. if (this._particle.quaternion) {
  335. this._quaternion.x = this._particle.quaternion.x;
  336. this._quaternion.y = this._particle.quaternion.y;
  337. this._quaternion.z = this._particle.quaternion.z;
  338. this._quaternion.w = this._particle.quaternion.w;
  339. } else {
  340. this._yaw = this._particle.rotation.y;
  341. this._pitch = this._particle.rotation.x;
  342. this._roll = this._particle.rotation.z;
  343. this._quaternionRotationYPR();
  344. }
  345. this._quaternionToRotationMatrix();
  346. }
  347. for (var pt = 0; pt < this._shape.length; pt++) {
  348. idx = index + pt * 3;
  349. colidx = colorIndex + pt * 4;
  350. uvidx = uvIndex + pt * 2;
  351. this._vertex.x = this._shape[pt].x;
  352. this._vertex.y = this._shape[pt].y;
  353. this._vertex.z = this._shape[pt].z;
  354. if (this._computeParticleVertex) {
  355. this.updateParticleVertex(this._particle, this._vertex, pt);
  356. }
  357. // positions
  358. this._vertex.x *= this._particle.scale.x;
  359. this._vertex.y *= this._particle.scale.y;
  360. this._vertex.z *= this._particle.scale.z;
  361. this._w = (this._vertex.x * this._rotMatrix.m[3]) + (this._vertex.y * this._rotMatrix.m[7]) + (this._vertex.z * this._rotMatrix.m[11]) + this._rotMatrix.m[15];
  362. this._rotated.x = ( (this._vertex.x * this._rotMatrix.m[0]) + (this._vertex.y * this._rotMatrix.m[4]) + (this._vertex.z * this._rotMatrix.m[8]) + this._rotMatrix.m[12] ) / this._w;
  363. this._rotated.y = ( (this._vertex.x * this._rotMatrix.m[1]) + (this._vertex.y * this._rotMatrix.m[5]) + (this._vertex.z * this._rotMatrix.m[9]) + this._rotMatrix.m[13] ) / this._w;
  364. this._rotated.z = ( (this._vertex.x * this._rotMatrix.m[2]) + (this._vertex.y * this._rotMatrix.m[6]) + (this._vertex.z * this._rotMatrix.m[10]) + this._rotMatrix.m[14] ) / this._w;
  365. 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;
  366. 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;
  367. 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;
  368. // normals : if the particles can't be morphed then just rotate the normals
  369. if (!this._computeParticleVertex) {
  370. this._normal.x = this._fixedNormal32[idx];
  371. this._normal.y = this._fixedNormal32[idx + 1];
  372. this._normal.z = this._fixedNormal32[idx + 2];
  373. this._w = (this._normal.x * this._rotMatrix.m[3]) + (this._normal.y * this._rotMatrix.m[7]) + (this._normal.z * this._rotMatrix.m[11]) + this._rotMatrix.m[15];
  374. this._rotated.x = ( (this._normal.x * this._rotMatrix.m[0]) + (this._normal.y * this._rotMatrix.m[4]) + (this._normal.z * this._rotMatrix.m[8]) + this._rotMatrix.m[12] ) / this._w;
  375. this._rotated.y = ( (this._normal.x * this._rotMatrix.m[1]) + (this._normal.y * this._rotMatrix.m[5]) + (this._normal.z * this._rotMatrix.m[9]) + this._rotMatrix.m[13] ) / this._w;
  376. this._rotated.z = ( (this._normal.x * this._rotMatrix.m[2]) + (this._normal.y * this._rotMatrix.m[6]) + (this._normal.z * this._rotMatrix.m[10]) + this._rotMatrix.m[14] ) / this._w;
  377. this._normals32[idx] = this._cam_axisX.x * this._rotated.x + this._cam_axisY.x * this._rotated.y + this._cam_axisZ.x * this._rotated.z;
  378. this._normals32[idx + 1] = this._cam_axisX.y * this._rotated.x + this._cam_axisY.y * this._rotated.y + this._cam_axisZ.y * this._rotated.z;
  379. this._normals32[idx + 2] = this._cam_axisX.z * this._rotated.x + this._cam_axisY.z * this._rotated.y + this._cam_axisZ.z * this._rotated.z;
  380. }
  381. if (this._computeParticleColor) {
  382. this._colors32[colidx] = this._particle.color.r;
  383. this._colors32[colidx + 1] = this._particle.color.g;
  384. this._colors32[colidx + 2] = this._particle.color.b;
  385. this._colors32[colidx + 3] = this._particle.color.a;
  386. }
  387. if (this._computeParticleTexture) {
  388. this._uvs32[uvidx] = this._shapeUV[pt * 2] * (this._particle.uvs.z - this._particle.uvs.x) + this._particle.uvs.x;
  389. this._uvs32[uvidx + 1] = this._shapeUV[pt * 2 + 1] * (this._particle.uvs.w - this._particle.uvs.y) + this._particle.uvs.y;
  390. }
  391. }
  392. index = idx + 3;
  393. colorIndex = colidx + 4;
  394. uvIndex = uvidx + 2;
  395. }
  396. if (update) {
  397. if (this._computeParticleColor) {
  398. this.mesh.updateVerticesData(VertexBuffer.ColorKind, this._colors32, false, false);
  399. }
  400. if (this._computeParticleTexture) {
  401. this.mesh.updateVerticesData(VertexBuffer.UVKind, this._uvs32, false, false);
  402. }
  403. this.mesh.updateVerticesData(VertexBuffer.PositionKind, this._positions32, false, false);
  404. if (!this.mesh.areNormalsFrozen) {
  405. if (this._computeParticleVertex) {
  406. // recompute the normals only if the particles can be morphed, update then the normal reference array
  407. VertexData.ComputeNormals(this._positions32, this._indices, this._normals32);
  408. for (var i = 0; i < this._normals32.length; i++) {
  409. this._fixedNormal32[i] = this._normals32[i];
  410. }
  411. }
  412. this.mesh.updateVerticesData(VertexBuffer.NormalKind, this._normals32, false, false);
  413. }
  414. }
  415. this.afterUpdateParticles(start, end, update);
  416. }
  417. private _quaternionRotationYPR(): void {
  418. this._halfroll = this._roll * 0.5;
  419. this._halfpitch = this._pitch * 0.5;
  420. this._halfyaw = this._yaw * 0.5;
  421. this._sinRoll = Math.sin(this._halfroll);
  422. this._cosRoll = Math.cos(this._halfroll);
  423. this._sinPitch = Math.sin(this._halfpitch);
  424. this._cosPitch = Math.cos(this._halfpitch);
  425. this._sinYaw = Math.sin(this._halfyaw);
  426. this._cosYaw = Math.cos(this._halfyaw);
  427. this._quaternion.x = (this._cosYaw * this._sinPitch * this._cosRoll) + (this._sinYaw * this._cosPitch * this._sinRoll);
  428. this._quaternion.y = (this._sinYaw * this._cosPitch * this._cosRoll) - (this._cosYaw * this._sinPitch * this._sinRoll);
  429. this._quaternion.z = (this._cosYaw * this._cosPitch * this._sinRoll) - (this._sinYaw * this._sinPitch * this._cosRoll);
  430. this._quaternion.w = (this._cosYaw * this._cosPitch * this._cosRoll) + (this._sinYaw * this._sinPitch * this._sinRoll);
  431. }
  432. private _quaternionToRotationMatrix(): void {
  433. this._rotMatrix.m[0] = 1.0 - (2.0 * (this._quaternion.y * this._quaternion.y + this._quaternion.z * this._quaternion.z));
  434. this._rotMatrix.m[1] = 2.0 * (this._quaternion.x * this._quaternion.y + this._quaternion.z * this._quaternion.w);
  435. this._rotMatrix.m[2] = 2.0 * (this._quaternion.z * this._quaternion.x - this._quaternion.y * this._quaternion.w);
  436. this._rotMatrix.m[3] = 0;
  437. this._rotMatrix.m[4] = 2.0 * (this._quaternion.x * this._quaternion.y - this._quaternion.z * this._quaternion.w);
  438. this._rotMatrix.m[5] = 1.0 - (2.0 * (this._quaternion.z * this._quaternion.z + this._quaternion.x * this._quaternion.x));
  439. this._rotMatrix.m[6] = 2.0 * (this._quaternion.y * this._quaternion.z + this._quaternion.x * this._quaternion.w);
  440. this._rotMatrix.m[7] = 0;
  441. this._rotMatrix.m[8] = 2.0 * (this._quaternion.z * this._quaternion.x + this._quaternion.y * this._quaternion.w);
  442. this._rotMatrix.m[9] = 2.0 * (this._quaternion.y * this._quaternion.z - this._quaternion.x * this._quaternion.w);
  443. this._rotMatrix.m[10] = 1.0 - (2.0 * (this._quaternion.y * this._quaternion.y + this._quaternion.x * this._quaternion.x));
  444. this._rotMatrix.m[11] = 0;
  445. this._rotMatrix.m[12] = 0;
  446. this._rotMatrix.m[13] = 0;
  447. this._rotMatrix.m[14] = 0;
  448. this._rotMatrix.m[15] = 1.0;
  449. }
  450. // dispose the SPS
  451. public dispose(): void {
  452. this.mesh.dispose();
  453. this.vars = null;
  454. // drop references to internal big arrays for the GC
  455. this._positions = null;
  456. this._indices = null;
  457. this._normals = null;
  458. this._uvs = null;
  459. this._colors = null;
  460. this._positions32 = null;
  461. this._normals32 = null;
  462. this._fixedNormal32 = null;
  463. this._uvs32 = null;
  464. this._colors32 = null;
  465. }
  466. // Optimizer setters
  467. public set computeParticleRotation(val: boolean) {
  468. this._computeParticleRotation = val;
  469. }
  470. public set computeParticleColor(val: boolean) {
  471. this._computeParticleColor = val;
  472. }
  473. public set computeParticleTexture(val: boolean) {
  474. this._computeParticleTexture = val;
  475. }
  476. public set computeParticleVertex(val: boolean) {
  477. this._computeParticleVertex = val;
  478. }
  479. // getters
  480. public get computeParticleRotation(): boolean {
  481. return this._computeParticleRotation;
  482. }
  483. public get computeParticleColor(): boolean {
  484. return this._computeParticleColor;
  485. }
  486. public get computeParticleTexture(): boolean {
  487. return this._computeParticleTexture;
  488. }
  489. public get computeParticleVertex(): boolean {
  490. return this._computeParticleVertex;
  491. }
  492. // =======================================================================
  493. // Particle behavior logic
  494. // these following methods may be overwritten by the user to fit his needs
  495. // init : sets all particles first values and calls updateParticle to set them in space
  496. // can be overwritten by the user
  497. public initParticles(): void {
  498. }
  499. // recycles a particle : can by overwritten by the user
  500. public recycleParticle(particle: SolidParticle): SolidParticle {
  501. return particle;
  502. }
  503. // updates a particle : can be overwritten by the user
  504. // will be called on each particle by setParticles() :
  505. // ex : just set a particle position or velocity and recycle conditions
  506. public updateParticle(particle: SolidParticle): SolidParticle {
  507. return particle;
  508. }
  509. // updates a vertex of a particle : can be overwritten by the user
  510. // will be called on each vertex particle by setParticles() :
  511. // particle : the current particle
  512. // vertex : the current index of the current particle
  513. // pt : the index of the current vertex in the particle shape
  514. // ex : just set a vertex particle position
  515. public updateParticleVertex(particle: SolidParticle, vertex: Vector3, pt: number): Vector3 {
  516. return vertex;
  517. }
  518. // will be called before any other treatment by setParticles()
  519. public beforeUpdateParticles(start?: number, stop?: number, update?: boolean): void {
  520. }
  521. // will be called after all setParticles() treatments
  522. public afterUpdateParticles(start?: number, stop?: number, update?: boolean): void {
  523. }
  524. }
  525. }