babylon.solidParticleSystem.js 51 KB

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