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