babylon.solidParticleSystem.ts 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014
  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. if (!this._computeParticleVertex) {
  594. this._normal.x = this._fixedNormal32[idx];
  595. this._normal.y = this._fixedNormal32[idx + 1];
  596. this._normal.z = this._fixedNormal32[idx + 2];
  597. 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]);
  598. 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]);
  599. 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]);
  600. this._normals32[idx] = this._cam_axisX.x * this._rotated.x + this._cam_axisY.x * this._rotated.y + this._cam_axisZ.x * this._rotated.z;
  601. 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;
  602. 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;
  603. }
  604. if (this._computeParticleColor) {
  605. this._colors32[colidx] = this._particle.color.r;
  606. this._colors32[colidx + 1] = this._particle.color.g;
  607. this._colors32[colidx + 2] = this._particle.color.b;
  608. this._colors32[colidx + 3] = this._particle.color.a;
  609. }
  610. if (this._computeParticleTexture) {
  611. this._uvs32[uvidx] = this._shapeUV[pt * 2] * (this._particle.uvs.z - this._particle.uvs.x) + this._particle.uvs.x;
  612. this._uvs32[uvidx + 1] = this._shapeUV[pt * 2 + 1] * (this._particle.uvs.w - this._particle.uvs.y) + this._particle.uvs.y;
  613. }
  614. }
  615. }
  616. // particle not visible : scaled to zero and positioned to the camera position
  617. else {
  618. for (pt = 0; pt < this._shape.length; pt++) {
  619. idx = index + pt * 3;
  620. colidx = colorIndex + pt * 4;
  621. uvidx = uvIndex + pt * 2;
  622. this._positions32[idx] = this._camera.position.x;
  623. this._positions32[idx + 1] = this._camera.position.y;
  624. this._positions32[idx + 2] = this._camera.position.z;
  625. this._normals32[idx] = 0.0;
  626. this._normals32[idx + 1] = 0.0;
  627. this._normals32[idx + 2] = 0.0;
  628. if (this._computeParticleColor) {
  629. this._colors32[colidx] = this._particle.color.r;
  630. this._colors32[colidx + 1] = this._particle.color.g;
  631. this._colors32[colidx + 2] = this._particle.color.b;
  632. this._colors32[colidx + 3] = this._particle.color.a;
  633. }
  634. if (this._computeParticleTexture) {
  635. this._uvs32[uvidx] = this._shapeUV[pt * 2] * (this._particle.uvs.z - this._particle.uvs.x) + this._particle.uvs.x;
  636. this._uvs32[uvidx + 1] = this._shapeUV[pt * 2 + 1] * (this._particle.uvs.w - this._particle.uvs.y) + this._particle.uvs.y;
  637. }
  638. }
  639. }
  640. // if the particle intersections must be computed : update the bbInfo
  641. if (this._particlesIntersect) {
  642. var bInfo = this._particle._boundingInfo;
  643. var bBox = bInfo.boundingBox;
  644. var bSphere = bInfo.boundingSphere;
  645. if (!this._bSphereOnly) {
  646. // place, scale and rotate the particle bbox within the SPS local system, then update it
  647. for (var b = 0; b < bBox.vectors.length; b++) {
  648. this._vertex.x = this._particle._modelBoundingInfo.boundingBox.vectors[b].x * this._particle.scaling.x;
  649. this._vertex.y = this._particle._modelBoundingInfo.boundingBox.vectors[b].y * this._particle.scaling.y;
  650. this._vertex.z = this._particle._modelBoundingInfo.boundingBox.vectors[b].z * this._particle.scaling.z;
  651. 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];
  652. 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;
  653. 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;
  654. 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;
  655. 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;
  656. 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;
  657. 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;
  658. }
  659. bBox._update(this.mesh._worldMatrix);
  660. }
  661. // place and scale the particle bouding sphere in the SPS local system, then update it
  662. this._minBbox.x = this._particle._modelBoundingInfo.minimum.x * this._particle.scaling.x;
  663. this._minBbox.y = this._particle._modelBoundingInfo.minimum.y * this._particle.scaling.y;
  664. this._minBbox.z = this._particle._modelBoundingInfo.minimum.z * this._particle.scaling.z;
  665. this._maxBbox.x = this._particle._modelBoundingInfo.maximum.x * this._particle.scaling.x;
  666. this._maxBbox.y = this._particle._modelBoundingInfo.maximum.y * this._particle.scaling.y;
  667. this._maxBbox.z = this._particle._modelBoundingInfo.maximum.z * this._particle.scaling.z;
  668. bSphere.center.x = this._particle.position.x + (this._minBbox.x + this._maxBbox.x) * 0.5;
  669. bSphere.center.y = this._particle.position.y + (this._minBbox.y + this._maxBbox.y) * 0.5;
  670. bSphere.center.z = this._particle.position.z + (this._minBbox.z + this._maxBbox.z) * 0.5;
  671. 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));
  672. bSphere._update(this.mesh._worldMatrix);
  673. }
  674. // increment indexes for the next particle
  675. index = idx + 3;
  676. colorIndex = colidx + 4;
  677. uvIndex = uvidx + 2;
  678. }
  679. // if the VBO must be updated
  680. if (update) {
  681. if (this._computeParticleColor) {
  682. this.mesh.updateVerticesData(VertexBuffer.ColorKind, this._colors32, false, false);
  683. }
  684. if (this._computeParticleTexture) {
  685. this.mesh.updateVerticesData(VertexBuffer.UVKind, this._uvs32, false, false);
  686. }
  687. this.mesh.updateVerticesData(VertexBuffer.PositionKind, this._positions32, false, false);
  688. if (!this.mesh.areNormalsFrozen || this.mesh.isFacetDataEnabled) {
  689. if (this._computeParticleVertex || this.mesh.isFacetDataEnabled) {
  690. // recompute the normals only if the particles can be morphed, update then also the normal reference array _fixedNormal32[]
  691. var params = this.mesh.isFacetDataEnabled ? this.mesh.getFacetDataParameters() : null;
  692. VertexData.ComputeNormals(this._positions32, this._indices, this._normals32, params);
  693. for (var i = 0; i < this._normals32.length; i++) {
  694. this._fixedNormal32[i] = this._normals32[i];
  695. }
  696. }
  697. if (!this.mesh.areNormalsFrozen) {
  698. this.mesh.updateVerticesData(VertexBuffer.NormalKind, this._normals32, false, false);
  699. }
  700. }
  701. }
  702. if (this._computeBoundingBox) {
  703. this.mesh._boundingInfo = new BoundingInfo(this._minimum, this._maximum);
  704. this.mesh._boundingInfo.update(this.mesh._worldMatrix);
  705. }
  706. this.afterUpdateParticles(start, end, update);
  707. }
  708. private _quaternionRotationYPR(): void {
  709. this._halfroll = this._roll * 0.5;
  710. this._halfpitch = this._pitch * 0.5;
  711. this._halfyaw = this._yaw * 0.5;
  712. this._sinRoll = Math.sin(this._halfroll);
  713. this._cosRoll = Math.cos(this._halfroll);
  714. this._sinPitch = Math.sin(this._halfpitch);
  715. this._cosPitch = Math.cos(this._halfpitch);
  716. this._sinYaw = Math.sin(this._halfyaw);
  717. this._cosYaw = Math.cos(this._halfyaw);
  718. this._quaternion.x = (this._cosYaw * this._sinPitch * this._cosRoll) + (this._sinYaw * this._cosPitch * this._sinRoll);
  719. this._quaternion.y = (this._sinYaw * this._cosPitch * this._cosRoll) - (this._cosYaw * this._sinPitch * this._sinRoll);
  720. this._quaternion.z = (this._cosYaw * this._cosPitch * this._sinRoll) - (this._sinYaw * this._sinPitch * this._cosRoll);
  721. this._quaternion.w = (this._cosYaw * this._cosPitch * this._cosRoll) + (this._sinYaw * this._sinPitch * this._sinRoll);
  722. }
  723. private _quaternionToRotationMatrix(): void {
  724. this._rotMatrix.m[0] = 1.0 - (2.0 * (this._quaternion.y * this._quaternion.y + this._quaternion.z * this._quaternion.z));
  725. this._rotMatrix.m[1] = 2.0 * (this._quaternion.x * this._quaternion.y + this._quaternion.z * this._quaternion.w);
  726. this._rotMatrix.m[2] = 2.0 * (this._quaternion.z * this._quaternion.x - this._quaternion.y * this._quaternion.w);
  727. this._rotMatrix.m[3] = 0;
  728. this._rotMatrix.m[4] = 2.0 * (this._quaternion.x * this._quaternion.y - this._quaternion.z * this._quaternion.w);
  729. this._rotMatrix.m[5] = 1.0 - (2.0 * (this._quaternion.z * this._quaternion.z + this._quaternion.x * this._quaternion.x));
  730. this._rotMatrix.m[6] = 2.0 * (this._quaternion.y * this._quaternion.z + this._quaternion.x * this._quaternion.w);
  731. this._rotMatrix.m[7] = 0;
  732. this._rotMatrix.m[8] = 2.0 * (this._quaternion.z * this._quaternion.x + this._quaternion.y * this._quaternion.w);
  733. this._rotMatrix.m[9] = 2.0 * (this._quaternion.y * this._quaternion.z - this._quaternion.x * this._quaternion.w);
  734. this._rotMatrix.m[10] = 1.0 - (2.0 * (this._quaternion.y * this._quaternion.y + this._quaternion.x * this._quaternion.x));
  735. this._rotMatrix.m[11] = 0;
  736. this._rotMatrix.m[12] = 0;
  737. this._rotMatrix.m[13] = 0;
  738. this._rotMatrix.m[14] = 0;
  739. this._rotMatrix.m[15] = 1.0;
  740. }
  741. /**
  742. * Disposes the SPS
  743. */
  744. public dispose(): void {
  745. this.mesh.dispose();
  746. this.vars = null;
  747. // drop references to internal big arrays for the GC
  748. this._positions = null;
  749. this._indices = null;
  750. this._normals = null;
  751. this._uvs = null;
  752. this._colors = null;
  753. this._positions32 = null;
  754. this._normals32 = null;
  755. this._fixedNormal32 = null;
  756. this._uvs32 = null;
  757. this._colors32 = null;
  758. this.pickedParticles = null;
  759. }
  760. /**
  761. * Visibilty helper : Recomputes the visible size according to the mesh bounding box
  762. * doc : http://doc.babylonjs.com/overviews/Solid_Particle_System#sps-visibility
  763. */
  764. public refreshVisibleSize(): void {
  765. if (!this._isVisibilityBoxLocked) {
  766. this.mesh.refreshBoundingInfo();
  767. }
  768. }
  769. /**
  770. * Visibility helper : Sets the size of a visibility box, this sets the underlying mesh bounding box.
  771. * @param size the size (float) of the visibility box
  772. * note : this doesn't lock the SPS mesh bounding box.
  773. * doc : http://doc.babylonjs.com/overviews/Solid_Particle_System#sps-visibility
  774. */
  775. public setVisibilityBox(size: number): void {
  776. var vis = size / 2;
  777. this.mesh._boundingInfo = new BoundingInfo(new Vector3(-vis, -vis, -vis), new Vector3(vis, vis, vis));
  778. }
  779. // getter and setter
  780. public get isAlwaysVisible(): boolean {
  781. return this._alwaysVisible;
  782. }
  783. /**
  784. * Sets the SPS as always visible or not
  785. * doc : http://doc.babylonjs.com/overviews/Solid_Particle_System#sps-visibility
  786. */
  787. public set isAlwaysVisible(val: boolean) {
  788. this._alwaysVisible = val;
  789. this.mesh.alwaysSelectAsActiveMesh = val;
  790. }
  791. /**
  792. * Sets the SPS visibility box as locked or not. This enables/disables the underlying mesh bounding box updates.
  793. * doc : http://doc.babylonjs.com/overviews/Solid_Particle_System#sps-visibility
  794. */
  795. public set isVisibilityBoxLocked(val: boolean) {
  796. this._isVisibilityBoxLocked = val;
  797. this.mesh.getBoundingInfo().isLocked = val;
  798. }
  799. public get isVisibilityBoxLocked(): boolean {
  800. return this._isVisibilityBoxLocked;
  801. }
  802. // Optimizer setters
  803. /**
  804. * Tells to `setParticles()` to compute the particle rotations or not.
  805. * Default value : true. The SPS is faster when it's set to false.
  806. * Note : the particle rotations aren't stored values, so setting `computeParticleRotation` to false will prevents the particle to rotate.
  807. */
  808. public set computeParticleRotation(val: boolean) {
  809. this._computeParticleRotation = val;
  810. }
  811. /**
  812. * Tells to `setParticles()` to compute the particle colors or not.
  813. * Default value : true. The SPS is faster when it's set to false.
  814. * Note : the particle colors are stored values, so setting `computeParticleColor` to false will keep yet the last colors set.
  815. */
  816. public set computeParticleColor(val: boolean) {
  817. this._computeParticleColor = val;
  818. }
  819. /**
  820. * Tells to `setParticles()` to compute the particle textures or not.
  821. * Default value : true. The SPS is faster when it's set to false.
  822. * Note : the particle textures are stored values, so setting `computeParticleTexture` to false will keep yet the last colors set.
  823. */
  824. public set computeParticleTexture(val: boolean) {
  825. this._computeParticleTexture = val;
  826. }
  827. /**
  828. * Tells to `setParticles()` to call the vertex function for each vertex of each particle, or not.
  829. * Default value : false. The SPS is faster when it's set to false.
  830. * Note : the particle custom vertex positions aren't stored values.
  831. */
  832. public set computeParticleVertex(val: boolean) {
  833. this._computeParticleVertex = val;
  834. }
  835. /**
  836. * Tells to `setParticles()` to compute or not the mesh bounding box when computing the particle positions.
  837. */
  838. public set computeBoundingBox(val: boolean) {
  839. this._computeBoundingBox = val;
  840. }
  841. // getters
  842. public get computeParticleRotation(): boolean {
  843. return this._computeParticleRotation;
  844. }
  845. public get computeParticleColor(): boolean {
  846. return this._computeParticleColor;
  847. }
  848. public get computeParticleTexture(): boolean {
  849. return this._computeParticleTexture;
  850. }
  851. public get computeParticleVertex(): boolean {
  852. return this._computeParticleVertex;
  853. }
  854. public get computeBoundingBox(): boolean {
  855. return this._computeBoundingBox;
  856. }
  857. // =======================================================================
  858. // Particle behavior logic
  859. // these following methods may be overwritten by the user to fit his needs
  860. /**
  861. * This function does nothing. It may be overwritten to set all the particle first values.
  862. * The SPS doesn't call this function, you may have to call it by your own.
  863. * doc : http://doc.babylonjs.com/overviews/Solid_Particle_System#particle-management
  864. */
  865. public initParticles(): void {
  866. }
  867. /**
  868. * This function does nothing. It may be overwritten to recycle a particle.
  869. * The SPS doesn't call this function, you may have to call it by your own.
  870. * doc : http://doc.babylonjs.com/overviews/Solid_Particle_System#particle-management
  871. */
  872. public recycleParticle(particle: SolidParticle): SolidParticle {
  873. return particle;
  874. }
  875. /**
  876. * Updates a particle : this function should be overwritten by the user.
  877. * It is called on each particle by `setParticles()`. This is the place to code each particle behavior.
  878. * doc : http://doc.babylonjs.com/overviews/Solid_Particle_System#particle-management
  879. * ex : just set a particle position or velocity and recycle conditions
  880. */
  881. public updateParticle(particle: SolidParticle): SolidParticle {
  882. return particle;
  883. }
  884. /**
  885. * Updates a vertex of a particle : it can be overwritten by the user.
  886. * This will be called on each vertex particle by `setParticles()` if `computeParticleVertex` is set to true only.
  887. * @param particle the current particle
  888. * @param vertex the current index of the current particle
  889. * @param pt the index of the current vertex in the particle shape
  890. * doc : http://doc.babylonjs.com/overviews/Solid_Particle_System#update-each-particle-shape
  891. * ex : just set a vertex particle position
  892. */
  893. public updateParticleVertex(particle: SolidParticle, vertex: Vector3, pt: number): Vector3 {
  894. return vertex;
  895. }
  896. /**
  897. * This will be called before any other treatment by `setParticles()` and will be passed three parameters.
  898. * This does nothing and may be overwritten by the user.
  899. * @param start the particle index in the particle array where to stop to iterate, same than the value passed to setParticle()
  900. * @param stop the particle index in the particle array where to stop to iterate, same than the value passed to setParticle()
  901. * @param update the boolean update value actually passed to setParticles()
  902. */
  903. public beforeUpdateParticles(start?: number, stop?: number, update?: boolean): void {
  904. }
  905. /**
  906. * This will be called by `setParticles()` after all the other treatments and just before the actual mesh update.
  907. * This will be passed three parameters.
  908. * This does nothing and may be overwritten by the user.
  909. * @param start the particle index in the particle array where to stop to iterate, same than the value passed to setParticle()
  910. * @param stop the particle index in the particle array where to stop to iterate, same than the value passed to setParticle()
  911. * @param update the boolean update value actually passed to setParticles()
  912. */
  913. public afterUpdateParticles(start?: number, stop?: number, update?: boolean): void {
  914. }
  915. }
  916. }