babylon.solidParticleSystem.ts 57 KB

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