babylon.solidParticleSystem.ts 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016
  1. module BABYLON {
  2. /**
  3. * Full documentation here : http://doc.babylonjs.com/overviews/Solid_Particle_System
  4. */
  5. export class SolidParticleSystem implements IDisposable {
  6. // public members
  7. /**
  8. * The SPS array of Solid Particle objects. Just access each particle as with any classic array.
  9. * Example : var p = SPS.particles[i];
  10. */
  11. public particles: SolidParticle[] = new Array<SolidParticle>();
  12. /**
  13. * The SPS total number of particles. Read only. Use SPS.counter instead if you need to set your own value.
  14. */
  15. public nbParticles: number = 0;
  16. /**
  17. * If the particles must ever face the camera (default false). Useful for planar particles.
  18. */
  19. public billboard: boolean = false;
  20. /**
  21. * Recompute normals when adding a shape
  22. */
  23. public recomputeNormals: boolean = true;
  24. /**
  25. * This a counter ofr your own usage. It's not set by any SPS functions.
  26. */
  27. public counter: number = 0;
  28. /**
  29. * The SPS name. This name is also given to the underlying mesh.
  30. */
  31. public name: string;
  32. /**
  33. * The SPS mesh. It's a standard BJS Mesh, so all the methods from the Mesh class are avalaible.
  34. */
  35. public mesh: Mesh;
  36. /**
  37. * This empty object is intended to store some SPS specific or temporary values in order to lower the Garbage Collector activity.
  38. * Please read : http://doc.babylonjs.com/overviews/Solid_Particle_System#garbage-collector-concerns
  39. */
  40. public vars: any = {};
  41. /**
  42. * This array is populated when the SPS is set as 'pickable'.
  43. * Each key of this array is a `faceId` value that you can get from a pickResult object.
  44. * Each element of this array is an object `{idx: int, faceId: int}`.
  45. * `idx` is the picked particle index in the `SPS.particles` array
  46. * `faceId` is the picked face index counted within this particle.
  47. * Please read : http://doc.babylonjs.com/overviews/Solid_Particle_System#pickable-particles
  48. */
  49. public pickedParticles: { idx: number; faceId: number }[];
  50. // private members
  51. private _scene: Scene;
  52. private _positions: number[] = new Array<number>();
  53. private _indices: number[] = new Array<number>();
  54. private _normals: number[] = new Array<number>();
  55. private _colors: number[] = new Array<number>();
  56. private _uvs: number[] = new Array<number>();
  57. private _positions32: Float32Array;
  58. private _normals32: Float32Array; // updated normals for the VBO
  59. private _fixedNormal32: Float32Array; // initial normal references
  60. private _colors32: Float32Array;
  61. private _uvs32: Float32Array;
  62. private _index: number = 0; // indices index
  63. private _updatable: boolean = true;
  64. private _pickable: boolean = false;
  65. private _isVisibilityBoxLocked = false;
  66. private _alwaysVisible: boolean = false;
  67. private _shapeCounter: number = 0;
  68. private _copy: SolidParticle = new SolidParticle(null, null, null, null, null);
  69. private _shape: Vector3[];
  70. private _shapeUV: number[];
  71. private _color: Color4 = new Color4(0, 0, 0, 0);
  72. private _computeParticleColor: boolean = true;
  73. private _computeParticleTexture: boolean = true;
  74. private _computeParticleRotation: boolean = true;
  75. private _computeParticleVertex: boolean = false;
  76. private _computeBoundingBox: boolean = false;
  77. private _cam_axisZ: Vector3 = Vector3.Zero();
  78. private _cam_axisY: Vector3 = Vector3.Zero();
  79. private _cam_axisX: Vector3 = Vector3.Zero();
  80. private _axisX: Vector3 = Axis.X;
  81. private _axisY: Vector3 = Axis.Y;
  82. private _axisZ: Vector3 = Axis.Z;
  83. private _camera: TargetCamera;
  84. private _particle: SolidParticle;
  85. private _camDir: Vector3 = Vector3.Zero();
  86. private _rotMatrix: Matrix = new Matrix();
  87. private _invertMatrix: Matrix = new Matrix();
  88. private _rotated: Vector3 = Vector3.Zero();
  89. private _quaternion: Quaternion = new Quaternion();
  90. private _vertex: Vector3 = Vector3.Zero();
  91. private _normal: Vector3 = Vector3.Zero();
  92. private _yaw: number = 0.0;
  93. private _pitch: number = 0.0;
  94. private _roll: number = 0.0;
  95. private _halfroll: number = 0.0;
  96. private _halfpitch: number = 0.0;
  97. private _halfyaw: number = 0.0;
  98. private _sinRoll: number = 0.0;
  99. private _cosRoll: number = 0.0;
  100. private _sinPitch: number = 0.0;
  101. private _cosPitch: number = 0.0;
  102. private _sinYaw: number = 0.0;
  103. private _cosYaw: number = 0.0;
  104. private _w: number = 0.0;
  105. private _minimum: Vector3 = Tmp.Vector3[0];
  106. private _maximum: Vector3 = Tmp.Vector3[1];
  107. private _scale: Vector3 = Tmp.Vector3[2];
  108. private _translation: Vector3 = Tmp.Vector3[3];
  109. private _minBbox: Vector3 = Tmp.Vector3[4];
  110. private _maxBbox: Vector3 = Tmp.Vector3[5];
  111. private _particlesIntersect: boolean = false;
  112. /**
  113. * Creates a SPS (Solid Particle System) object.
  114. * `name` (String) is the SPS name, this will be the underlying mesh name.
  115. * `scene` (Scene) is the scene in which the SPS is added.
  116. * `updatable` (default true) : if the SPS must be updatable or immutable.
  117. * `isPickable` (default false) : if the solid particles must be pickable.
  118. * `particleIntersection` (default false) : if the solid particle intersections must be computed
  119. */
  120. constructor(name: string, scene: Scene, options?: { updatable?: boolean; isPickable?: boolean; particleIntersection?: boolean }) {
  121. this.name = name;
  122. this._scene = scene;
  123. this._camera = <TargetCamera>scene.activeCamera;
  124. this._pickable = options ? options.isPickable : false;
  125. this._particlesIntersect = options ? options.particleIntersection : false;
  126. if (options && options.updatable) {
  127. this._updatable = options.updatable;
  128. } else {
  129. this._updatable = true;
  130. }
  131. if (this._pickable) {
  132. this.pickedParticles = [];
  133. }
  134. }
  135. /**
  136. * Builds the SPS underlying mesh. Returns a standard Mesh.
  137. * If no model shape was added to the SPS, the returned mesh is just a single triangular plane.
  138. */
  139. public buildMesh(): Mesh {
  140. if (this.nbParticles === 0) {
  141. var triangle = MeshBuilder.CreateDisc("", { radius: 1, tessellation: 3 }, this._scene);
  142. this.addShape(triangle, 1);
  143. triangle.dispose();
  144. }
  145. this._positions32 = new Float32Array(this._positions);
  146. this._uvs32 = new Float32Array(this._uvs);
  147. this._colors32 = new Float32Array(this._colors);
  148. if (this.recomputeNormals) {
  149. VertexData.ComputeNormals(this._positions32, this._indices, this._normals);
  150. }
  151. this._normals32 = new Float32Array(this._normals);
  152. this._fixedNormal32 = new Float32Array(this._normals);
  153. var vertexData = new VertexData();
  154. vertexData.set(this._positions32, VertexBuffer.PositionKind);
  155. vertexData.indices = this._indices;
  156. vertexData.set(this._normals32, VertexBuffer.NormalKind);
  157. if (this._uvs32) {
  158. vertexData.set(this._uvs32, VertexBuffer.UVKind);;
  159. }
  160. if (this._colors32) {
  161. vertexData.set(this._colors32, VertexBuffer.ColorKind);
  162. }
  163. var mesh = new Mesh(this.name, this._scene);
  164. vertexData.applyToMesh(mesh, this._updatable);
  165. this.mesh = mesh;
  166. this.mesh.isPickable = this._pickable;
  167. // free memory
  168. this._positions = null;
  169. this._normals = null;
  170. this._uvs = null;
  171. this._colors = null;
  172. if (!this._updatable) {
  173. this.particles.length = 0;
  174. }
  175. return mesh;
  176. }
  177. /**
  178. * Digests the mesh and generates as many solid particles in the system as wanted. Returns the SPS.
  179. * These particles will have the same geometry than the mesh parts and will be positioned at the same localisation than the mesh original places.
  180. * Thus the particles generated from `digest()` have their property `position` set yet.
  181. * `mesh` ( Mesh ) is the mesh to be digested
  182. * `facetNb` (optional integer, default 1) is the number of mesh facets per particle, this parameter is overriden by the parameter `number` if any
  183. * `delta` (optional integer, default 0) is the random extra number of facets per particle , each particle will have between `facetNb` and `facetNb + delta` facets
  184. * `number` (optional positive integer) is the wanted number of particles : each particle is built with `mesh_total_facets / number` facets
  185. */
  186. public digest(mesh: Mesh, options?: { facetNb?: number; number?: number; delta?: number }): SolidParticleSystem {
  187. var size: number = (options && options.facetNb) || 1;
  188. var number: number = (options && options.number);
  189. var delta: number = (options && options.delta) || 0;
  190. var meshPos = mesh.getVerticesData(VertexBuffer.PositionKind);
  191. var meshInd = mesh.getIndices();
  192. var meshUV = mesh.getVerticesData(VertexBuffer.UVKind);
  193. var meshCol = mesh.getVerticesData(VertexBuffer.ColorKind);
  194. var meshNor = mesh.getVerticesData(VertexBuffer.NormalKind);
  195. var f: number = 0; // facet counter
  196. var totalFacets: number = meshInd.length / 3; // a facet is a triangle, so 3 indices
  197. // compute size from number
  198. if (number) {
  199. number = (number > totalFacets) ? totalFacets : number;
  200. size = Math.round(totalFacets / number);
  201. delta = 0;
  202. } else {
  203. size = (size > totalFacets) ? totalFacets : size;
  204. }
  205. var facetPos: number[] = []; // submesh positions
  206. var facetInd: number[] = []; // submesh indices
  207. var facetUV: number[] = []; // submesh UV
  208. var facetCol: number[] = []; // submesh colors
  209. var barycenter: Vector3 = Tmp.Vector3[0];
  210. var rand: number;
  211. var sizeO: number = size;
  212. while (f < totalFacets) {
  213. size = sizeO + Math.floor((1 + delta) * Math.random());
  214. if (f > totalFacets - size) {
  215. size = totalFacets - f;
  216. }
  217. // reset temp arrays
  218. facetPos.length = 0;
  219. facetInd.length = 0;
  220. facetUV.length = 0;
  221. facetCol.length = 0;
  222. // iterate over "size" facets
  223. var fi: number = 0;
  224. for (var j = f * 3; j < (f + size) * 3; j++) {
  225. facetInd.push(fi);
  226. var i: number = meshInd[j];
  227. facetPos.push(meshPos[i * 3], meshPos[i * 3 + 1], meshPos[i * 3 + 2]);
  228. if (meshUV) {
  229. facetUV.push(meshUV[i * 2], meshUV[i * 2 + 1]);
  230. }
  231. if (meshCol) {
  232. facetCol.push(meshCol[i * 4], meshCol[i * 4 + 1], meshCol[i * 4 + 2], meshCol[i * 4 + 3]);
  233. }
  234. fi++;
  235. }
  236. // create a model shape for each single particle
  237. var idx: number = this.nbParticles;
  238. var shape: Vector3[] = this._posToShape(facetPos);
  239. var shapeUV: number[] = this._uvsToShapeUV(facetUV);
  240. // compute the barycenter of the shape
  241. var v: number;
  242. for (v = 0; v < shape.length; v++) {
  243. barycenter.addInPlace(shape[v]);
  244. }
  245. barycenter.scaleInPlace(1 / shape.length);
  246. // shift the shape from its barycenter to the origin
  247. for (v = 0; v < shape.length; v++) {
  248. shape[v].subtractInPlace(barycenter);
  249. }
  250. var bInfo;
  251. if (this._particlesIntersect) {
  252. bInfo = new BoundingInfo(barycenter, barycenter);
  253. }
  254. var modelShape = new ModelShape(this._shapeCounter, shape, shapeUV, null, null);
  255. // add the particle in the SPS
  256. this._meshBuilder(this._index, shape, this._positions, facetInd, this._indices, facetUV, this._uvs, facetCol, this._colors, meshNor, this._normals, idx, 0, null);
  257. this._addParticle(idx, this._positions.length, modelShape, this._shapeCounter, 0, bInfo);
  258. // initialize the particle position
  259. this.particles[this.nbParticles].position.addInPlace(barycenter);
  260. this._index += shape.length;
  261. idx++;
  262. this.nbParticles++;
  263. this._shapeCounter++;
  264. f += size;
  265. }
  266. return this;
  267. }
  268. //reset copy
  269. private _resetCopy() {
  270. this._copy.position.x = 0;
  271. this._copy.position.y = 0;
  272. this._copy.position.z = 0;
  273. this._copy.rotation.x = 0;
  274. this._copy.rotation.y = 0;
  275. this._copy.rotation.z = 0;
  276. this._copy.rotationQuaternion = null;
  277. this._copy.scaling.x = 1;
  278. this._copy.scaling.y = 1;
  279. this._copy.scaling.z = 1;
  280. this._copy.uvs.x = 0;
  281. this._copy.uvs.y = 0;
  282. this._copy.uvs.z = 1;
  283. this._copy.uvs.w = 1;
  284. this._copy.color = null;
  285. }
  286. // _meshBuilder : inserts the shape model in the global SPS mesh
  287. private _meshBuilder(p, shape, positions, meshInd, indices, meshUV, uvs, meshCol, colors, meshNor, normals, idx, idxInShape, options): void {
  288. var i;
  289. var u = 0;
  290. var c = 0;
  291. var n = 0;
  292. this._resetCopy();
  293. if (options && options.positionFunction) { // call to custom positionFunction
  294. options.positionFunction(this._copy, idx, idxInShape);
  295. }
  296. if (this._copy.rotationQuaternion) {
  297. this._quaternion.copyFrom(this._copy.rotationQuaternion);
  298. } else {
  299. this._yaw = this._copy.rotation.y;
  300. this._pitch = this._copy.rotation.x;
  301. this._roll = this._copy.rotation.z;
  302. this._quaternionRotationYPR();
  303. }
  304. this._quaternionToRotationMatrix();
  305. for (i = 0; i < shape.length; i++) {
  306. this._vertex.x = shape[i].x;
  307. this._vertex.y = shape[i].y;
  308. this._vertex.z = shape[i].z;
  309. if (options && options.vertexFunction) {
  310. options.vertexFunction(this._copy, this._vertex, i);
  311. }
  312. this._vertex.x *= this._copy.scaling.x;
  313. this._vertex.y *= this._copy.scaling.y;
  314. this._vertex.z *= this._copy.scaling.z;
  315. Vector3.TransformCoordinatesToRef(this._vertex, this._rotMatrix, this._rotated);
  316. positions.push(this._copy.position.x + this._rotated.x, this._copy.position.y + this._rotated.y, this._copy.position.z + this._rotated.z);
  317. if (meshUV) {
  318. 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);
  319. u += 2;
  320. }
  321. if (this._copy.color) {
  322. this._color = this._copy.color;
  323. } else if (meshCol && meshCol[c] !== undefined) {
  324. this._color.r = meshCol[c];
  325. this._color.g = meshCol[c + 1];
  326. this._color.b = meshCol[c + 2];
  327. this._color.a = meshCol[c + 3];
  328. } else {
  329. this._color.r = 1;
  330. this._color.g = 1;
  331. this._color.b = 1;
  332. this._color.a = 1;
  333. }
  334. colors.push(this._color.r, this._color.g, this._color.b, this._color.a);
  335. c += 4;
  336. if (!this.recomputeNormals && meshNor) {
  337. this._normal.x = meshNor[n];
  338. this._normal.y = meshNor[n + 1];
  339. this._normal.z = meshNor[n + 2];
  340. Vector3.TransformCoordinatesToRef(this._normal, this._rotMatrix, this._normal);
  341. normals.push(this._normal.x, this._normal.y, this._normal.z);
  342. n += 3;
  343. }
  344. }
  345. for (i = 0; i < meshInd.length; i++) {
  346. indices.push(p + meshInd[i]);
  347. }
  348. if (this._pickable) {
  349. var nbfaces = meshInd.length / 3;
  350. for (i = 0; i < nbfaces; i++) {
  351. this.pickedParticles.push({ idx: idx, faceId: i });
  352. }
  353. }
  354. }
  355. // returns a shape array from positions array
  356. private _posToShape(positions): Vector3[] {
  357. var shape = [];
  358. for (var i = 0; i < positions.length; i += 3) {
  359. shape.push(new Vector3(positions[i], positions[i + 1], positions[i + 2]));
  360. }
  361. return shape;
  362. }
  363. // returns a shapeUV array from a Vector4 uvs
  364. private _uvsToShapeUV(uvs): number[] {
  365. var shapeUV = [];
  366. if (uvs) {
  367. for (var i = 0; i < uvs.length; i++)
  368. shapeUV.push(uvs[i]);
  369. }
  370. return shapeUV;
  371. }
  372. // adds a new particle object in the particles array
  373. private _addParticle(idx: number, idxpos: number, model: ModelShape, shapeId: number, idxInShape: number, bInfo?: BoundingInfo): void {
  374. this.particles.push(new SolidParticle(idx, idxpos, model, shapeId, idxInShape, bInfo));
  375. }
  376. /**
  377. * Adds some particles to the SPS from the model shape. Returns the shape id.
  378. * Please read the doc : http://doc.babylonjs.com/overviews/Solid_Particle_System#create-an-immutable-sps
  379. * `mesh` is any Mesh object that will be used as a model for the solid particles.
  380. * `nb` (positive integer) the number of particles to be created from this model
  381. * `positionFunction` is an optional javascript function to called for each particle on SPS creation.
  382. * `vertexFunction` is an optional javascript function to called for each vertex of each particle on SPS creation
  383. */
  384. public addShape(mesh: Mesh, nb: number, options?: { positionFunction?: any; vertexFunction?: any }): number {
  385. var meshPos = mesh.getVerticesData(VertexBuffer.PositionKind);
  386. var meshInd = mesh.getIndices();
  387. var meshUV = mesh.getVerticesData(VertexBuffer.UVKind);
  388. var meshCol = mesh.getVerticesData(VertexBuffer.ColorKind);
  389. var meshNor = mesh.getVerticesData(VertexBuffer.NormalKind);
  390. var bbInfo;
  391. if (this._particlesIntersect) {
  392. bbInfo = mesh.getBoundingInfo();
  393. }
  394. var shape = this._posToShape(meshPos);
  395. var shapeUV = this._uvsToShapeUV(meshUV);
  396. var posfunc = options ? options.positionFunction : null;
  397. var vtxfunc = options ? options.vertexFunction : null;
  398. var modelShape = new ModelShape(this._shapeCounter, shape, shapeUV, posfunc, vtxfunc);
  399. // particles
  400. var idx = this.nbParticles;
  401. for (var i = 0; i < nb; i++) {
  402. this._meshBuilder(this._index, shape, this._positions, meshInd, this._indices, meshUV, this._uvs, meshCol, this._colors, meshNor, this._normals, idx, i, options);
  403. if (this._updatable) {
  404. this._addParticle(idx, this._positions.length, modelShape, this._shapeCounter, i, bbInfo);
  405. }
  406. this._index += shape.length;
  407. idx++;
  408. }
  409. this.nbParticles += nb;
  410. this._shapeCounter++;
  411. return this._shapeCounter - 1;
  412. }
  413. // rebuilds a particle back to its just built status : if needed, recomputes the custom positions and vertices
  414. private _rebuildParticle(particle: SolidParticle): void {
  415. this._resetCopy();
  416. if (particle._model._positionFunction) { // recall to stored custom positionFunction
  417. particle._model._positionFunction(this._copy, particle.idx, particle.idxInShape);
  418. }
  419. if (this._copy.rotationQuaternion) {
  420. this._quaternion.copyFrom(this._copy.rotationQuaternion);
  421. } else {
  422. this._yaw = this._copy.rotation.y;
  423. this._pitch = this._copy.rotation.x;
  424. this._roll = this._copy.rotation.z;
  425. this._quaternionRotationYPR();
  426. }
  427. this._quaternionToRotationMatrix();
  428. this._shape = particle._model._shape;
  429. for (var pt = 0; pt < this._shape.length; pt++) {
  430. this._vertex.x = this._shape[pt].x;
  431. this._vertex.y = this._shape[pt].y;
  432. this._vertex.z = this._shape[pt].z;
  433. if (particle._model._vertexFunction) {
  434. particle._model._vertexFunction(this._copy, this._vertex, pt); // recall to stored vertexFunction
  435. }
  436. this._vertex.x *= this._copy.scaling.x;
  437. this._vertex.y *= this._copy.scaling.y;
  438. this._vertex.z *= this._copy.scaling.z;
  439. Vector3.TransformCoordinatesToRef(this._vertex, this._rotMatrix, this._rotated);
  440. this._positions32[particle._pos + pt * 3] = this._copy.position.x + this._rotated.x;
  441. this._positions32[particle._pos + pt * 3 + 1] = this._copy.position.y + this._rotated.y;
  442. this._positions32[particle._pos + pt * 3 + 2] = this._copy.position.z + this._rotated.z;
  443. }
  444. particle.position.x = 0.0;
  445. particle.position.y = 0.0;
  446. particle.position.z = 0.0;
  447. particle.rotation.x = 0.0;
  448. particle.rotation.y = 0.0;
  449. particle.rotation.z = 0.0;
  450. particle.rotationQuaternion = null;
  451. particle.scaling.x = 1.0;
  452. particle.scaling.y = 1.0;
  453. particle.scaling.z = 1.0;
  454. }
  455. /**
  456. * Rebuilds the whole mesh and updates the VBO : custom positions and vertices are recomputed if needed.
  457. */
  458. public rebuildMesh(): void {
  459. for (var p = 0; p < this.particles.length; p++) {
  460. this._rebuildParticle(this.particles[p]);
  461. }
  462. this.mesh.updateVerticesData(VertexBuffer.PositionKind, this._positions32, false, false);
  463. }
  464. /**
  465. * Sets all the particles : this method actually really updates the mesh according to the particle positions, rotations, colors, textures, etc.
  466. * This method calls `updateParticle()` for each particle of the SPS.
  467. * For an animated SPS, it is usually called within the render loop.
  468. * @param start The particle index in the particle array where to start to compute the particle property values _(default 0)_
  469. * @param end The particle index in the particle array where to stop to compute the particle property values _(default nbParticle - 1)_
  470. * @param update If the mesh must be finally updated on this call after all the particle computations _(default true)_
  471. */
  472. public setParticles(start: number = 0, end: number = this.nbParticles - 1, update: boolean = true): void {
  473. if (!this._updatable) {
  474. return;
  475. }
  476. // custom beforeUpdate
  477. this.beforeUpdateParticles(start, end, update);
  478. this._cam_axisX.x = 1.0;
  479. this._cam_axisX.y = 0.0;
  480. this._cam_axisX.z = 0.0;
  481. this._cam_axisY.x = 0.0;
  482. this._cam_axisY.y = 1.0;
  483. this._cam_axisY.z = 0.0;
  484. this._cam_axisZ.x = 0.0;
  485. this._cam_axisZ.y = 0.0;
  486. this._cam_axisZ.z = 1.0;
  487. // if the particles will always face the camera
  488. if (this.billboard) {
  489. // compute the camera position and un-rotate it by the current mesh rotation
  490. if (this.mesh._worldMatrix.decompose(this._scale, this._quaternion, this._translation)) {
  491. this._quaternionToRotationMatrix();
  492. this._rotMatrix.invertToRef(this._invertMatrix);
  493. this._camera._currentTarget.subtractToRef(this._camera.globalPosition, this._camDir);
  494. Vector3.TransformCoordinatesToRef(this._camDir, this._invertMatrix, this._cam_axisZ);
  495. this._cam_axisZ.normalize();
  496. // set two orthogonal vectors (_cam_axisX and and _cam_axisY) to the rotated camDir axis (_cam_axisZ)
  497. Vector3.CrossToRef(this._cam_axisZ, this._axisX, this._cam_axisY);
  498. Vector3.CrossToRef(this._cam_axisY, this._cam_axisZ, this._cam_axisX);
  499. this._cam_axisY.normalize();
  500. this._cam_axisX.normalize();
  501. }
  502. }
  503. Matrix.IdentityToRef(this._rotMatrix);
  504. var idx = 0;
  505. var index = 0;
  506. var colidx = 0;
  507. var colorIndex = 0;
  508. var uvidx = 0;
  509. var uvIndex = 0;
  510. var pt = 0;
  511. if (this._computeBoundingBox) {
  512. Vector3.FromFloatsToRef(Number.MAX_VALUE, Number.MAX_VALUE, Number.MAX_VALUE, this._minimum);
  513. Vector3.FromFloatsToRef(-Number.MAX_VALUE, -Number.MAX_VALUE, -Number.MAX_VALUE, this._maximum);
  514. }
  515. // particle loop
  516. end = (end > this.nbParticles - 1) ? this.nbParticles - 1 : end;
  517. for (var p = start; p <= end; p++) {
  518. this._particle = this.particles[p];
  519. this._shape = this._particle._model._shape;
  520. this._shapeUV = this._particle._model._shapeUV;
  521. // call to custom user function to update the particle properties
  522. this.updateParticle(this._particle);
  523. if (this._particle.isVisible) {
  524. // particle rotation matrix
  525. if (this.billboard) {
  526. this._particle.rotation.x = 0.0;
  527. this._particle.rotation.y = 0.0;
  528. }
  529. if (this._computeParticleRotation || this.billboard) {
  530. if (this._particle.rotationQuaternion) {
  531. this._quaternion.copyFrom(this._particle.rotationQuaternion);
  532. } else {
  533. this._yaw = this._particle.rotation.y;
  534. this._pitch = this._particle.rotation.x;
  535. this._roll = this._particle.rotation.z;
  536. this._quaternionRotationYPR();
  537. }
  538. this._quaternionToRotationMatrix();
  539. }
  540. // particle vertex loop
  541. for (pt = 0; pt < this._shape.length; pt++) {
  542. idx = index + pt * 3;
  543. colidx = colorIndex + pt * 4;
  544. uvidx = uvIndex + pt * 2;
  545. this._vertex.x = this._shape[pt].x;
  546. this._vertex.y = this._shape[pt].y;
  547. this._vertex.z = this._shape[pt].z;
  548. if (this._computeParticleVertex) {
  549. this.updateParticleVertex(this._particle, this._vertex, pt);
  550. }
  551. // positions
  552. this._vertex.x *= this._particle.scaling.x;
  553. this._vertex.y *= this._particle.scaling.y;
  554. this._vertex.z *= this._particle.scaling.z;
  555. 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];
  556. 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;
  557. 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;
  558. 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;
  559. 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;
  560. 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;
  561. 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;
  562. if (this._computeBoundingBox) {
  563. if (this._positions32[idx] < this._minimum.x) {
  564. this._minimum.x = this._positions32[idx];
  565. }
  566. if (this._positions32[idx] > this._maximum.x) {
  567. this._maximum.x = this._positions32[idx];
  568. }
  569. if (this._positions32[idx + 1] < this._minimum.y) {
  570. this._minimum.y = this._positions32[idx + 1];
  571. }
  572. if (this._positions32[idx + 1] > this._maximum.y) {
  573. this._maximum.y = this._positions32[idx + 1];
  574. }
  575. if (this._positions32[idx + 2] < this._minimum.z) {
  576. this._minimum.z = this._positions32[idx + 2];
  577. }
  578. if (this._positions32[idx + 2] > this._maximum.z) {
  579. this._maximum.z = this._positions32[idx + 2];
  580. }
  581. }
  582. // normals : if the particles can't be morphed then just rotate the normals, what if much more faster than ComputeNormals()
  583. if (!this._computeParticleVertex) {
  584. this._normal.x = this._fixedNormal32[idx];
  585. this._normal.y = this._fixedNormal32[idx + 1];
  586. this._normal.z = this._fixedNormal32[idx + 2];
  587. 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];
  588. 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;
  589. 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;
  590. 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;
  591. this._normals32[idx] = this._cam_axisX.x * this._rotated.x + this._cam_axisY.x * this._rotated.y + this._cam_axisZ.x * this._rotated.z;
  592. 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;
  593. 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;
  594. }
  595. if (this._computeParticleColor) {
  596. this._colors32[colidx] = this._particle.color.r;
  597. this._colors32[colidx + 1] = this._particle.color.g;
  598. this._colors32[colidx + 2] = this._particle.color.b;
  599. this._colors32[colidx + 3] = this._particle.color.a;
  600. }
  601. if (this._computeParticleTexture) {
  602. this._uvs32[uvidx] = this._shapeUV[pt * 2] * (this._particle.uvs.z - this._particle.uvs.x) + this._particle.uvs.x;
  603. this._uvs32[uvidx + 1] = this._shapeUV[pt * 2 + 1] * (this._particle.uvs.w - this._particle.uvs.y) + this._particle.uvs.y;
  604. }
  605. }
  606. }
  607. // particle not visible : scaled to zero and positioned to the camera position
  608. else {
  609. for (pt = 0; pt < this._shape.length; pt++) {
  610. idx = index + pt * 3;
  611. colidx = colorIndex + pt * 4;
  612. uvidx = uvIndex + pt * 2;
  613. this._positions32[idx] = this._camera.position.x;
  614. this._positions32[idx + 1] = this._camera.position.y;
  615. this._positions32[idx + 2] = this._camera.position.z;
  616. this._normals32[idx] = 0.0;
  617. this._normals32[idx + 1] = 0.0;
  618. this._normals32[idx + 2] = 0.0;
  619. if (this._computeParticleColor) {
  620. this._colors32[colidx] = this._particle.color.r;
  621. this._colors32[colidx + 1] = this._particle.color.g;
  622. this._colors32[colidx + 2] = this._particle.color.b;
  623. this._colors32[colidx + 3] = this._particle.color.a;
  624. }
  625. if (this._computeParticleTexture) {
  626. this._uvs32[uvidx] = this._shapeUV[pt * 2] * (this._particle.uvs.z - this._particle.uvs.x) + this._particle.uvs.x;
  627. this._uvs32[uvidx + 1] = this._shapeUV[pt * 2 + 1] * (this._particle.uvs.w - this._particle.uvs.y) + this._particle.uvs.y;
  628. }
  629. }
  630. }
  631. // if the particle intersections must be computed : update the bbInfo
  632. if (this._particlesIntersect) {
  633. var bInfo = this._particle._boundingInfo;
  634. var bBox = bInfo.boundingBox;
  635. var bSphere = bInfo.boundingSphere;
  636. // place, scale and rotate the particle bbox within the SPS local system
  637. for (var b = 0; b < bBox.vectors.length; b++) {
  638. if (this._particle.isVisible) {
  639. this._vertex.x = this._particle._modelBoundingInfo.boundingBox.vectors[b].x * this._particle.scaling.x;
  640. this._vertex.y = this._particle._modelBoundingInfo.boundingBox.vectors[b].y * this._particle.scaling.y;
  641. this._vertex.z = this._particle._modelBoundingInfo.boundingBox.vectors[b].z * this._particle.scaling.z;
  642. 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];
  643. 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;
  644. 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;
  645. 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;
  646. bBox.vectors[b].x = 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;
  647. bBox.vectors[b].y = 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;
  648. bBox.vectors[b].z = 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;
  649. }
  650. else {
  651. bBox.vectors[b].x = this._camera.position.x;
  652. bBox.vectors[b].y = this._camera.position.y;
  653. bBox.vectors[b].z = this._camera.position.z;
  654. }
  655. }
  656. // place and scale the particle bouding sphere in the SPS local system
  657. if (this._particle.isVisible) {
  658. this._minBbox.x = this._particle._modelBoundingInfo.minimum.x * this._particle.scaling.x;
  659. this._minBbox.y = this._particle._modelBoundingInfo.minimum.y * this._particle.scaling.y;
  660. this._minBbox.z = this._particle._modelBoundingInfo.minimum.z * this._particle.scaling.z;
  661. this._maxBbox.x = this._particle._modelBoundingInfo.maximum.x * this._particle.scaling.x;
  662. this._maxBbox.y = this._particle._modelBoundingInfo.maximum.y * this._particle.scaling.y;
  663. this._maxBbox.z = this._particle._modelBoundingInfo.maximum.z * this._particle.scaling.z;
  664. bSphere.center.x = this._particle.position.x + (this._minBbox.x + this._maxBbox.x) * 0.5;
  665. bSphere.center.y = this._particle.position.y + (this._minBbox.y + this._maxBbox.y) * 0.5;
  666. bSphere.center.z = this._particle.position.z + (this._minBbox.z + this._maxBbox.z) * 0.5;
  667. bSphere.radius = Vector3.Distance(this._minimum, this._maximum) * 0.5;
  668. } else {
  669. bSphere.center.x = this._camera.position.x;
  670. bSphere.center.y = this._camera.position.x;
  671. bSphere.center.z = this._camera.position.x;
  672. bSphere.radius = 0.0;
  673. }
  674. // then update the bbox and the bsphere into the world system
  675. bBox._update(this.mesh._worldMatrix);
  676. bSphere._update(this.mesh._worldMatrix);
  677. }
  678. // increment indexes for the next particle
  679. index = idx + 3;
  680. colorIndex = colidx + 4;
  681. uvIndex = uvidx + 2;
  682. }
  683. // if the VBO must be updated
  684. if (update) {
  685. if (this._computeParticleColor) {
  686. this.mesh.updateVerticesData(VertexBuffer.ColorKind, this._colors32, false, false);
  687. }
  688. if (this._computeParticleTexture) {
  689. this.mesh.updateVerticesData(VertexBuffer.UVKind, this._uvs32, false, false);
  690. }
  691. this.mesh.updateVerticesData(VertexBuffer.PositionKind, this._positions32, false, false);
  692. if (!this.mesh.areNormalsFrozen) {
  693. if (this._computeParticleVertex) {
  694. // recompute the normals only if the particles can be morphed, update then also the normal reference array _fixedNormal32[]
  695. VertexData.ComputeNormals(this._positions32, this._indices, this._normals32);
  696. for (var i = 0; i < this._normals32.length; i++) {
  697. this._fixedNormal32[i] = this._normals32[i];
  698. }
  699. }
  700. this.mesh.updateVerticesData(VertexBuffer.NormalKind, this._normals32, false, false);
  701. }
  702. }
  703. if (this._computeBoundingBox) {
  704. this.mesh._boundingInfo = new BoundingInfo(this._minimum, this._maximum);
  705. this.mesh._boundingInfo.update(this.mesh._worldMatrix);
  706. }
  707. this.afterUpdateParticles(start, end, update);
  708. }
  709. private _quaternionRotationYPR(): void {
  710. this._halfroll = this._roll * 0.5;
  711. this._halfpitch = this._pitch * 0.5;
  712. this._halfyaw = this._yaw * 0.5;
  713. this._sinRoll = Math.sin(this._halfroll);
  714. this._cosRoll = Math.cos(this._halfroll);
  715. this._sinPitch = Math.sin(this._halfpitch);
  716. this._cosPitch = Math.cos(this._halfpitch);
  717. this._sinYaw = Math.sin(this._halfyaw);
  718. this._cosYaw = Math.cos(this._halfyaw);
  719. this._quaternion.x = (this._cosYaw * this._sinPitch * this._cosRoll) + (this._sinYaw * this._cosPitch * this._sinRoll);
  720. this._quaternion.y = (this._sinYaw * this._cosPitch * this._cosRoll) - (this._cosYaw * this._sinPitch * this._sinRoll);
  721. this._quaternion.z = (this._cosYaw * this._cosPitch * this._sinRoll) - (this._sinYaw * this._sinPitch * this._cosRoll);
  722. this._quaternion.w = (this._cosYaw * this._cosPitch * this._cosRoll) + (this._sinYaw * this._sinPitch * this._sinRoll);
  723. }
  724. private _quaternionToRotationMatrix(): void {
  725. this._rotMatrix.m[0] = 1.0 - (2.0 * (this._quaternion.y * this._quaternion.y + this._quaternion.z * this._quaternion.z));
  726. this._rotMatrix.m[1] = 2.0 * (this._quaternion.x * this._quaternion.y + this._quaternion.z * this._quaternion.w);
  727. this._rotMatrix.m[2] = 2.0 * (this._quaternion.z * this._quaternion.x - this._quaternion.y * this._quaternion.w);
  728. this._rotMatrix.m[3] = 0;
  729. this._rotMatrix.m[4] = 2.0 * (this._quaternion.x * this._quaternion.y - this._quaternion.z * this._quaternion.w);
  730. this._rotMatrix.m[5] = 1.0 - (2.0 * (this._quaternion.z * this._quaternion.z + this._quaternion.x * this._quaternion.x));
  731. this._rotMatrix.m[6] = 2.0 * (this._quaternion.y * this._quaternion.z + this._quaternion.x * this._quaternion.w);
  732. this._rotMatrix.m[7] = 0;
  733. this._rotMatrix.m[8] = 2.0 * (this._quaternion.z * this._quaternion.x + this._quaternion.y * this._quaternion.w);
  734. this._rotMatrix.m[9] = 2.0 * (this._quaternion.y * this._quaternion.z - this._quaternion.x * this._quaternion.w);
  735. this._rotMatrix.m[10] = 1.0 - (2.0 * (this._quaternion.y * this._quaternion.y + this._quaternion.x * this._quaternion.x));
  736. this._rotMatrix.m[11] = 0;
  737. this._rotMatrix.m[12] = 0;
  738. this._rotMatrix.m[13] = 0;
  739. this._rotMatrix.m[14] = 0;
  740. this._rotMatrix.m[15] = 1.0;
  741. }
  742. /**
  743. * Disposes the SPS
  744. */
  745. public dispose(): void {
  746. this.mesh.dispose();
  747. this.vars = null;
  748. // drop references to internal big arrays for the GC
  749. this._positions = null;
  750. this._indices = null;
  751. this._normals = null;
  752. this._uvs = null;
  753. this._colors = null;
  754. this._positions32 = null;
  755. this._normals32 = null;
  756. this._fixedNormal32 = null;
  757. this._uvs32 = null;
  758. this._colors32 = null;
  759. this.pickedParticles = null;
  760. }
  761. /**
  762. * Visibilty helper : Recomputes the visible size according to the mesh bounding box
  763. * doc : http://doc.babylonjs.com/overviews/Solid_Particle_System#sps-visibility
  764. */
  765. public refreshVisibleSize(): void {
  766. if (!this._isVisibilityBoxLocked) {
  767. this.mesh.refreshBoundingInfo();
  768. }
  769. }
  770. /**
  771. * Visibility helper : Sets the size of a visibility box, this sets the underlying mesh bounding box.
  772. * @param size the size (float) of the visibility box
  773. * note : this doesn't lock the SPS mesh bounding box.
  774. * doc : http://doc.babylonjs.com/overviews/Solid_Particle_System#sps-visibility
  775. */
  776. public setVisibilityBox(size: number): void {
  777. var vis = size / 2;
  778. this.mesh._boundingInfo = new BoundingInfo(new Vector3(-vis, -vis, -vis), new Vector3(vis, vis, vis));
  779. }
  780. // getter and setter
  781. public get isAlwaysVisible(): boolean {
  782. return this._alwaysVisible;
  783. }
  784. /**
  785. * Sets the SPS as always visible or not
  786. * doc : http://doc.babylonjs.com/overviews/Solid_Particle_System#sps-visibility
  787. */
  788. public set isAlwaysVisible(val: boolean) {
  789. this._alwaysVisible = val;
  790. this.mesh.alwaysSelectAsActiveMesh = val;
  791. }
  792. /**
  793. * Sets the SPS visibility box as locked or not. This enables/disables the underlying mesh bounding box updates.
  794. * doc : http://doc.babylonjs.com/overviews/Solid_Particle_System#sps-visibility
  795. */
  796. public set isVisibilityBoxLocked(val: boolean) {
  797. this._isVisibilityBoxLocked = val;
  798. this.mesh.getBoundingInfo().isLocked = val;
  799. }
  800. public get isVisibilityBoxLocked(): boolean {
  801. return this._isVisibilityBoxLocked;
  802. }
  803. // Optimizer setters
  804. /**
  805. * Tells to `setParticles()` to compute the particle rotations or not.
  806. * Default value : true. The SPS is faster when it's set to false.
  807. * Note : the particle rotations aren't stored values, so setting `computeParticleRotation` to false will prevents the particle to rotate.
  808. */
  809. public set computeParticleRotation(val: boolean) {
  810. this._computeParticleRotation = val;
  811. }
  812. /**
  813. * Tells to `setParticles()` to compute the particle colors or not.
  814. * Default value : true. The SPS is faster when it's set to false.
  815. * Note : the particle colors are stored values, so setting `computeParticleColor` to false will keep yet the last colors set.
  816. */
  817. public set computeParticleColor(val: boolean) {
  818. this._computeParticleColor = val;
  819. }
  820. /**
  821. * Tells to `setParticles()` to compute the particle textures or not.
  822. * Default value : true. The SPS is faster when it's set to false.
  823. * Note : the particle textures are stored values, so setting `computeParticleTexture` to false will keep yet the last colors set.
  824. */
  825. public set computeParticleTexture(val: boolean) {
  826. this._computeParticleTexture = val;
  827. }
  828. /**
  829. * Tells to `setParticles()` to call the vertex function for each vertex of each particle, or not.
  830. * Default value : false. The SPS is faster when it's set to false.
  831. * Note : the particle custom vertex positions aren't stored values.
  832. */
  833. public set computeParticleVertex(val: boolean) {
  834. this._computeParticleVertex = val;
  835. }
  836. /**
  837. * Tells to `setParticles()` to compute or not the mesh bounding box when computing the particle positions.
  838. */
  839. public set computeBoundingBox(val: boolean) {
  840. this._computeBoundingBox = val;
  841. }
  842. // getters
  843. public get computeParticleRotation(): boolean {
  844. return this._computeParticleRotation;
  845. }
  846. public get computeParticleColor(): boolean {
  847. return this._computeParticleColor;
  848. }
  849. public get computeParticleTexture(): boolean {
  850. return this._computeParticleTexture;
  851. }
  852. public get computeParticleVertex(): boolean {
  853. return this._computeParticleVertex;
  854. }
  855. public get computeBoundingBox(): boolean {
  856. return this._computeBoundingBox;
  857. }
  858. // =======================================================================
  859. // Particle behavior logic
  860. // these following methods may be overwritten by the user to fit his needs
  861. /**
  862. * This function does nothing. It may be overwritten to set all the particle first values.
  863. * The SPS doesn't call this function, you may have to call it by your own.
  864. * doc : http://doc.babylonjs.com/overviews/Solid_Particle_System#particle-management
  865. */
  866. public initParticles(): void {
  867. }
  868. /**
  869. * This function does nothing. It may be overwritten to recycle a particle.
  870. * The SPS doesn't call this function, you may have to call it by your own.
  871. * doc : http://doc.babylonjs.com/overviews/Solid_Particle_System#particle-management
  872. */
  873. public recycleParticle(particle: SolidParticle): SolidParticle {
  874. return particle;
  875. }
  876. /**
  877. * Updates a particle : this function should be overwritten by the user.
  878. * It is called on each particle by `setParticles()`. This is the place to code each particle behavior.
  879. * doc : http://doc.babylonjs.com/overviews/Solid_Particle_System#particle-management
  880. * ex : just set a particle position or velocity and recycle conditions
  881. */
  882. public updateParticle(particle: SolidParticle): SolidParticle {
  883. return particle;
  884. }
  885. /**
  886. * Updates a vertex of a particle : it can be overwritten by the user.
  887. * This will be called on each vertex particle by `setParticles()` if `computeParticleVertex` is set to true only.
  888. * @param particle the current particle
  889. * @param vertex the current index of the current particle
  890. * @param pt the index of the current vertex in the particle shape
  891. * doc : http://doc.babylonjs.com/overviews/Solid_Particle_System#update-each-particle-shape
  892. * ex : just set a vertex particle position
  893. */
  894. public updateParticleVertex(particle: SolidParticle, vertex: Vector3, pt: number): Vector3 {
  895. return vertex;
  896. }
  897. /**
  898. * This will be called before any other treatment by `setParticles()` and will be passed three parameters.
  899. * This does nothing and may be overwritten by the user.
  900. * @param start the particle index in the particle array where to stop to iterate, same than the value passed to setParticle()
  901. * @param stop the particle index in the particle array where to stop to iterate, same than the value passed to setParticle()
  902. * @param update the boolean update value actually passed to setParticles()
  903. */
  904. public beforeUpdateParticles(start?: number, stop?: number, update?: boolean): void {
  905. }
  906. /**
  907. * This will be called by `setParticles()` after all the other treatments and just before the actual mesh update.
  908. * This will be passed three parameters.
  909. * This does nothing and may be overwritten by the user.
  910. * @param start the particle index in the particle array where to stop to iterate, same than the value passed to setParticle()
  911. * @param stop the particle index in the particle array where to stop to iterate, same than the value passed to setParticle()
  912. * @param update the boolean update value actually passed to setParticles()
  913. */
  914. public afterUpdateParticles(start?: number, stop?: number, update?: boolean): void {
  915. }
  916. }
  917. }