babylon.solidParticleSystem.ts 26 KB

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