babylon.solidParticleSystem.ts 57 KB

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