babylon.solidParticleSystem.js 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  1. var BABYLON;
  2. (function (BABYLON) {
  3. /**
  4. * Full documentation here : http://doc.babylonjs.com/tutorials/Solid_Particle_System
  5. */
  6. var SolidParticleSystem = (function () {
  7. /**
  8. * Creates a SPS (Solid Particle System) object.
  9. * @param name the SPS name, this will be the underlying mesh name
  10. * @param updatable (default true) if the SPS must be updatable or immutable
  11. * @param isPickable (default false) if the solid particles must be pickable
  12. */
  13. function SolidParticleSystem(name, scene, options) {
  14. // public members
  15. this.particles = new Array();
  16. this.nbParticles = 0;
  17. this.billboard = false;
  18. this.counter = 0;
  19. this.vars = {};
  20. this._positions = new Array();
  21. this._indices = new Array();
  22. this._normals = new Array();
  23. this._colors = new Array();
  24. this._uvs = new Array();
  25. this._index = 0; // indices index
  26. this._updatable = true;
  27. this._pickable = false;
  28. this._isVisibilityBoxLocked = false;
  29. this._alwaysVisible = false;
  30. this._shapeCounter = 0;
  31. this._copy = new BABYLON.SolidParticle(null, null, null, null, null);
  32. this._color = new BABYLON.Color4(0, 0, 0, 0);
  33. this._computeParticleColor = true;
  34. this._computeParticleTexture = true;
  35. this._computeParticleRotation = true;
  36. this._computeParticleVertex = false;
  37. this._cam_axisZ = BABYLON.Vector3.Zero();
  38. this._cam_axisY = BABYLON.Vector3.Zero();
  39. this._cam_axisX = BABYLON.Vector3.Zero();
  40. this._axisX = BABYLON.Axis.X;
  41. this._axisY = BABYLON.Axis.Y;
  42. this._axisZ = BABYLON.Axis.Z;
  43. this._fakeCamPos = BABYLON.Vector3.Zero();
  44. this._rotMatrix = new BABYLON.Matrix();
  45. this._invertMatrix = new BABYLON.Matrix();
  46. this._rotated = BABYLON.Vector3.Zero();
  47. this._quaternion = new BABYLON.Quaternion();
  48. this._vertex = BABYLON.Vector3.Zero();
  49. this._normal = BABYLON.Vector3.Zero();
  50. this._yaw = 0.0;
  51. this._pitch = 0.0;
  52. this._roll = 0.0;
  53. this._halfroll = 0.0;
  54. this._halfpitch = 0.0;
  55. this._halfyaw = 0.0;
  56. this._sinRoll = 0.0;
  57. this._cosRoll = 0.0;
  58. this._sinPitch = 0.0;
  59. this._cosPitch = 0.0;
  60. this._sinYaw = 0.0;
  61. this._cosYaw = 0.0;
  62. this._w = 0.0;
  63. this.name = name;
  64. this._scene = scene;
  65. this._camera = scene.activeCamera;
  66. this._pickable = options ? options.isPickable : false;
  67. if (options && options.updatable) {
  68. this._updatable = options.updatable;
  69. }
  70. else {
  71. this._updatable = true;
  72. }
  73. if (this._pickable) {
  74. this.pickedParticles = [];
  75. }
  76. }
  77. /**
  78. * Builds the SPS underlying mesh. Returns a standard Mesh.
  79. * If no model shape was added to the SPS, the return mesh is only a single triangular plane.
  80. */
  81. SolidParticleSystem.prototype.buildMesh = function () {
  82. if (this.nbParticles === 0) {
  83. var triangle = BABYLON.MeshBuilder.CreateDisc("", { radius: 1, tessellation: 3 }, this._scene);
  84. this.addShape(triangle, 1);
  85. triangle.dispose();
  86. }
  87. this._positions32 = new Float32Array(this._positions);
  88. this._uvs32 = new Float32Array(this._uvs);
  89. this._colors32 = new Float32Array(this._colors);
  90. BABYLON.VertexData.ComputeNormals(this._positions32, this._indices, this._normals);
  91. this._normals32 = new Float32Array(this._normals);
  92. this._fixedNormal32 = new Float32Array(this._normals);
  93. var vertexData = new BABYLON.VertexData();
  94. vertexData.set(this._positions32, BABYLON.VertexBuffer.PositionKind);
  95. vertexData.indices = this._indices;
  96. vertexData.set(this._normals32, BABYLON.VertexBuffer.NormalKind);
  97. if (this._uvs32) {
  98. vertexData.set(this._uvs32, BABYLON.VertexBuffer.UVKind);
  99. ;
  100. }
  101. if (this._colors32) {
  102. vertexData.set(this._colors32, BABYLON.VertexBuffer.ColorKind);
  103. }
  104. var mesh = new BABYLON.Mesh(this.name, this._scene);
  105. vertexData.applyToMesh(mesh, this._updatable);
  106. this.mesh = mesh;
  107. this.mesh.isPickable = this._pickable;
  108. // free memory
  109. this._positions = null;
  110. this._normals = null;
  111. this._uvs = null;
  112. this._colors = null;
  113. if (!this._updatable) {
  114. this.particles.length = 0;
  115. }
  116. return mesh;
  117. };
  118. //reset copy
  119. SolidParticleSystem.prototype._resetCopy = function () {
  120. this._copy.position.x = 0;
  121. this._copy.position.y = 0;
  122. this._copy.position.z = 0;
  123. this._copy.rotation.x = 0;
  124. this._copy.rotation.y = 0;
  125. this._copy.rotation.z = 0;
  126. this._copy.quaternion = null;
  127. this._copy.scale.x = 1;
  128. this._copy.scale.y = 1;
  129. this._copy.scale.z = 1;
  130. this._copy.uvs.x = 0;
  131. this._copy.uvs.y = 0;
  132. this._copy.uvs.z = 1;
  133. this._copy.uvs.w = 1;
  134. this._copy.color = null;
  135. };
  136. // _meshBuilder : inserts the shape model in the global SPS mesh
  137. SolidParticleSystem.prototype._meshBuilder = function (p, shape, positions, meshInd, indices, meshUV, uvs, meshCol, colors, idx, idxInShape, options) {
  138. var i;
  139. var u = 0;
  140. var c = 0;
  141. this._resetCopy();
  142. if (options && options.positionFunction) {
  143. options.positionFunction(this._copy, idx, idxInShape);
  144. }
  145. if (this._copy.quaternion) {
  146. this._quaternion.x = this._copy.quaternion.x;
  147. this._quaternion.y = this._copy.quaternion.y;
  148. this._quaternion.z = this._copy.quaternion.z;
  149. this._quaternion.w = this._copy.quaternion.w;
  150. }
  151. else {
  152. this._yaw = this._copy.rotation.y;
  153. this._pitch = this._copy.rotation.x;
  154. this._roll = this._copy.rotation.z;
  155. this._quaternionRotationYPR();
  156. }
  157. this._quaternionToRotationMatrix();
  158. for (i = 0; i < shape.length; i++) {
  159. this._vertex.x = shape[i].x;
  160. this._vertex.y = shape[i].y;
  161. this._vertex.z = shape[i].z;
  162. if (options && options.vertexFunction) {
  163. options.vertexFunction(this._copy, this._vertex, i);
  164. }
  165. this._vertex.x *= this._copy.scale.x;
  166. this._vertex.y *= this._copy.scale.y;
  167. this._vertex.z *= this._copy.scale.z;
  168. BABYLON.Vector3.TransformCoordinatesToRef(this._vertex, this._rotMatrix, this._rotated);
  169. positions.push(this._copy.position.x + this._rotated.x, this._copy.position.y + this._rotated.y, this._copy.position.z + this._rotated.z);
  170. if (meshUV) {
  171. 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);
  172. u += 2;
  173. }
  174. if (this._copy.color) {
  175. this._color = this._copy.color;
  176. }
  177. else if (meshCol && meshCol[c]) {
  178. this._color.r = meshCol[c];
  179. this._color.g = meshCol[c + 1];
  180. this._color.b = meshCol[c + 2];
  181. this._color.a = meshCol[c + 3];
  182. }
  183. else {
  184. this._color.r = 1;
  185. this._color.g = 1;
  186. this._color.b = 1;
  187. this._color.a = 1;
  188. }
  189. colors.push(this._color.r, this._color.g, this._color.b, this._color.a);
  190. c += 4;
  191. }
  192. for (i = 0; i < meshInd.length; i++) {
  193. indices.push(p + meshInd[i]);
  194. }
  195. if (this._pickable) {
  196. var nbfaces = meshInd.length / 3;
  197. for (i = 0; i < nbfaces; i++) {
  198. this.pickedParticles.push({ idx: idx, faceId: i });
  199. }
  200. }
  201. };
  202. // returns a shape array from positions array
  203. SolidParticleSystem.prototype._posToShape = function (positions) {
  204. var shape = [];
  205. for (var i = 0; i < positions.length; i += 3) {
  206. shape.push(new BABYLON.Vector3(positions[i], positions[i + 1], positions[i + 2]));
  207. }
  208. return shape;
  209. };
  210. // returns a shapeUV array from a Vector4 uvs
  211. SolidParticleSystem.prototype._uvsToShapeUV = function (uvs) {
  212. var shapeUV = [];
  213. if (uvs) {
  214. for (var i = 0; i < uvs.length; i++)
  215. shapeUV.push(uvs[i]);
  216. }
  217. return shapeUV;
  218. };
  219. // adds a new particle object in the particles array
  220. SolidParticleSystem.prototype._addParticle = function (idx, idxpos, model, shapeId, idxInShape) {
  221. this.particles.push(new BABYLON.SolidParticle(idx, idxpos, model, shapeId, idxInShape));
  222. };
  223. /**
  224. * Adds some particles to the SPS from the model shape.
  225. * Please read the doc : http://doc.babylonjs.com/tutorials/Solid_Particle_System#create-an-immutable-sps
  226. * @param mesh any Mesh object that will be used as a model for the solid particles.
  227. * @param nb the number of particles to be created from this model
  228. * @param positionFunction an optional javascript function to called for each particle on SPS creation
  229. * @param vertexFunction an optional javascript function to called for each vertex of each particle on SPS creation
  230. */
  231. SolidParticleSystem.prototype.addShape = function (mesh, nb, options) {
  232. var meshPos = mesh.getVerticesData(BABYLON.VertexBuffer.PositionKind);
  233. var meshInd = mesh.getIndices();
  234. var meshUV = mesh.getVerticesData(BABYLON.VertexBuffer.UVKind);
  235. var meshCol = mesh.getVerticesData(BABYLON.VertexBuffer.ColorKind);
  236. var shape = this._posToShape(meshPos);
  237. var shapeUV = this._uvsToShapeUV(meshUV);
  238. var posfunc = options ? options.positionFunction : null;
  239. var vtxfunc = options ? options.vertexFunction : null;
  240. var modelShape = new BABYLON.ModelShape(this._shapeCounter, shape, shapeUV, posfunc, vtxfunc);
  241. // particles
  242. var idx = this.nbParticles;
  243. for (var i = 0; i < nb; i++) {
  244. this._meshBuilder(this._index, shape, this._positions, meshInd, this._indices, meshUV, this._uvs, meshCol, this._colors, idx, i, options);
  245. if (this._updatable) {
  246. this._addParticle(idx, this._positions.length, modelShape, this._shapeCounter, i);
  247. }
  248. this._index += shape.length;
  249. idx++;
  250. }
  251. this.nbParticles += nb;
  252. this._shapeCounter++;
  253. return this._shapeCounter - 1;
  254. };
  255. // rebuilds a particle back to its just built status : if needed, recomputes the custom positions and vertices
  256. SolidParticleSystem.prototype._rebuildParticle = function (particle) {
  257. this._resetCopy();
  258. if (particle._model._positionFunction) {
  259. particle._model._positionFunction(this._copy, particle.idx, particle.idxInShape);
  260. }
  261. if (this._copy.quaternion) {
  262. this._quaternion.x = this._copy.quaternion.x;
  263. this._quaternion.y = this._copy.quaternion.y;
  264. this._quaternion.z = this._copy.quaternion.z;
  265. this._quaternion.w = this._copy.quaternion.w;
  266. }
  267. else {
  268. this._yaw = this._copy.rotation.y;
  269. this._pitch = this._copy.rotation.x;
  270. this._roll = this._copy.rotation.z;
  271. this._quaternionRotationYPR();
  272. }
  273. this._quaternionToRotationMatrix();
  274. this._shape = particle._model._shape;
  275. for (var pt = 0; pt < this._shape.length; pt++) {
  276. this._vertex.x = this._shape[pt].x;
  277. this._vertex.y = this._shape[pt].y;
  278. this._vertex.z = this._shape[pt].z;
  279. if (particle._model._vertexFunction) {
  280. particle._model._vertexFunction(this._copy, this._vertex, pt); // recall to stored vertexFunction
  281. }
  282. this._vertex.x *= this._copy.scale.x;
  283. this._vertex.y *= this._copy.scale.y;
  284. this._vertex.z *= this._copy.scale.z;
  285. BABYLON.Vector3.TransformCoordinatesToRef(this._vertex, this._rotMatrix, this._rotated);
  286. this._positions32[particle._pos + pt * 3] = this._copy.position.x + this._rotated.x;
  287. this._positions32[particle._pos + pt * 3 + 1] = this._copy.position.y + this._rotated.y;
  288. this._positions32[particle._pos + pt * 3 + 2] = this._copy.position.z + this._rotated.z;
  289. }
  290. particle.position.x = 0;
  291. particle.position.y = 0;
  292. particle.position.z = 0;
  293. particle.rotation.x = 0;
  294. particle.rotation.y = 0;
  295. particle.rotation.z = 0;
  296. particle.quaternion = null;
  297. particle.scale.x = 1;
  298. particle.scale.y = 1;
  299. particle.scale.z = 1;
  300. };
  301. /**
  302. * Rebuilds the whole mesh and updates the VBO : custom positions and vertices are recomputed if needed.
  303. */
  304. SolidParticleSystem.prototype.rebuildMesh = function () {
  305. for (var p = 0; p < this.particles.length; p++) {
  306. this._rebuildParticle(this.particles[p]);
  307. }
  308. this.mesh.updateVerticesData(BABYLON.VertexBuffer.PositionKind, this._positions32, false, false);
  309. };
  310. /**
  311. * Sets all the particles : this method actually really updates the mesh according to the particle positions, rotations, colors, textures, etc.
  312. * This method calls updateParticle() for each particles of the SPS.
  313. * For an animated SPS, it is usually called within the render loop.
  314. * @param start (default 0) the particle index in the particle array where to start to compute the particle property values
  315. * @param end (default nbParticle - 1) the particle index in the particle array where to stop to compute the particle property values
  316. * @param update (default true) if the mesh must be finally updated on this call after all the particle computations.
  317. */
  318. SolidParticleSystem.prototype.setParticles = function (start, end, update) {
  319. if (start === void 0) { start = 0; }
  320. if (end === void 0) { end = this.nbParticles - 1; }
  321. if (update === void 0) { update = true; }
  322. if (!this._updatable) {
  323. return;
  324. }
  325. // custom beforeUpdate
  326. this.beforeUpdateParticles(start, end, update);
  327. this._cam_axisX.x = 1;
  328. this._cam_axisX.y = 0;
  329. this._cam_axisX.z = 0;
  330. this._cam_axisY.x = 0;
  331. this._cam_axisY.y = 1;
  332. this._cam_axisY.z = 0;
  333. this._cam_axisZ.x = 0;
  334. this._cam_axisZ.y = 0;
  335. this._cam_axisZ.z = 1;
  336. // if the particles will always face the camera
  337. if (this.billboard) {
  338. // compute a fake camera position : un-rotate the camera position by the current mesh rotation
  339. this._yaw = this.mesh.rotation.y;
  340. this._pitch = this.mesh.rotation.x;
  341. this._roll = this.mesh.rotation.z;
  342. this._quaternionRotationYPR();
  343. this._quaternionToRotationMatrix();
  344. this._rotMatrix.invertToRef(this._invertMatrix);
  345. BABYLON.Vector3.TransformCoordinatesToRef(this._camera.globalPosition, this._invertMatrix, this._fakeCamPos);
  346. // set two orthogonal vectors (_cam_axisX and and _cam_axisY) to the cam-mesh axis (_cam_axisZ)
  347. (this._fakeCamPos).subtractToRef(this.mesh.position, this._cam_axisZ);
  348. BABYLON.Vector3.CrossToRef(this._cam_axisZ, this._axisX, this._cam_axisY);
  349. BABYLON.Vector3.CrossToRef(this._cam_axisZ, this._cam_axisY, this._cam_axisX);
  350. this._cam_axisY.normalize();
  351. this._cam_axisX.normalize();
  352. this._cam_axisZ.normalize();
  353. }
  354. BABYLON.Matrix.IdentityToRef(this._rotMatrix);
  355. var idx = 0;
  356. var index = 0;
  357. var colidx = 0;
  358. var colorIndex = 0;
  359. var uvidx = 0;
  360. var uvIndex = 0;
  361. // particle loop
  362. end = (end > this.nbParticles - 1) ? this.nbParticles - 1 : end;
  363. for (var p = start; p <= end; p++) {
  364. this._particle = this.particles[p];
  365. this._shape = this._particle._model._shape;
  366. this._shapeUV = this._particle._model._shapeUV;
  367. // call to custom user function to update the particle properties
  368. this.updateParticle(this._particle);
  369. // particle rotation matrix
  370. if (this.billboard) {
  371. this._particle.rotation.x = 0.0;
  372. this._particle.rotation.y = 0.0;
  373. }
  374. if (this._computeParticleRotation) {
  375. if (this._particle.quaternion) {
  376. this._quaternion.x = this._particle.quaternion.x;
  377. this._quaternion.y = this._particle.quaternion.y;
  378. this._quaternion.z = this._particle.quaternion.z;
  379. this._quaternion.w = this._particle.quaternion.w;
  380. }
  381. else {
  382. this._yaw = this._particle.rotation.y;
  383. this._pitch = this._particle.rotation.x;
  384. this._roll = this._particle.rotation.z;
  385. this._quaternionRotationYPR();
  386. }
  387. this._quaternionToRotationMatrix();
  388. }
  389. for (var pt = 0; pt < this._shape.length; pt++) {
  390. idx = index + pt * 3;
  391. colidx = colorIndex + pt * 4;
  392. uvidx = uvIndex + pt * 2;
  393. this._vertex.x = this._shape[pt].x;
  394. this._vertex.y = this._shape[pt].y;
  395. this._vertex.z = this._shape[pt].z;
  396. if (this._computeParticleVertex) {
  397. this.updateParticleVertex(this._particle, this._vertex, pt);
  398. }
  399. // positions
  400. this._vertex.x *= this._particle.scale.x;
  401. this._vertex.y *= this._particle.scale.y;
  402. this._vertex.z *= this._particle.scale.z;
  403. 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];
  404. 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;
  405. 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;
  406. 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;
  407. 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;
  408. 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;
  409. 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;
  410. // normals : if the particles can't be morphed then just rotate the normals
  411. if (!this._computeParticleVertex && !this.billboard) {
  412. this._normal.x = this._fixedNormal32[idx];
  413. this._normal.y = this._fixedNormal32[idx + 1];
  414. this._normal.z = this._fixedNormal32[idx + 2];
  415. 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];
  416. 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;
  417. 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;
  418. 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;
  419. this._normals32[idx] = this._cam_axisX.x * this._rotated.x + this._cam_axisY.x * this._rotated.y + this._cam_axisZ.x * this._rotated.z;
  420. 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;
  421. 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;
  422. }
  423. if (this._computeParticleColor) {
  424. this._colors32[colidx] = this._particle.color.r;
  425. this._colors32[colidx + 1] = this._particle.color.g;
  426. this._colors32[colidx + 2] = this._particle.color.b;
  427. this._colors32[colidx + 3] = this._particle.color.a;
  428. }
  429. if (this._computeParticleTexture) {
  430. this._uvs32[uvidx] = this._shapeUV[pt * 2] * (this._particle.uvs.z - this._particle.uvs.x) + this._particle.uvs.x;
  431. this._uvs32[uvidx + 1] = this._shapeUV[pt * 2 + 1] * (this._particle.uvs.w - this._particle.uvs.y) + this._particle.uvs.y;
  432. }
  433. }
  434. index = idx + 3;
  435. colorIndex = colidx + 4;
  436. uvIndex = uvidx + 2;
  437. }
  438. if (update) {
  439. if (this._computeParticleColor) {
  440. this.mesh.updateVerticesData(BABYLON.VertexBuffer.ColorKind, this._colors32, false, false);
  441. }
  442. if (this._computeParticleTexture) {
  443. this.mesh.updateVerticesData(BABYLON.VertexBuffer.UVKind, this._uvs32, false, false);
  444. }
  445. this.mesh.updateVerticesData(BABYLON.VertexBuffer.PositionKind, this._positions32, false, false);
  446. if (!this.mesh.areNormalsFrozen) {
  447. if (this._computeParticleVertex || this.billboard) {
  448. // recompute the normals only if the particles can be morphed, update then the normal reference array
  449. BABYLON.VertexData.ComputeNormals(this._positions32, this._indices, this._normals32);
  450. for (var i = 0; i < this._normals32.length; i++) {
  451. this._fixedNormal32[i] = this._normals32[i];
  452. }
  453. }
  454. this.mesh.updateVerticesData(BABYLON.VertexBuffer.NormalKind, this._normals32, false, false);
  455. }
  456. }
  457. this.afterUpdateParticles(start, end, update);
  458. };
  459. SolidParticleSystem.prototype._quaternionRotationYPR = function () {
  460. this._halfroll = this._roll * 0.5;
  461. this._halfpitch = this._pitch * 0.5;
  462. this._halfyaw = this._yaw * 0.5;
  463. this._sinRoll = Math.sin(this._halfroll);
  464. this._cosRoll = Math.cos(this._halfroll);
  465. this._sinPitch = Math.sin(this._halfpitch);
  466. this._cosPitch = Math.cos(this._halfpitch);
  467. this._sinYaw = Math.sin(this._halfyaw);
  468. this._cosYaw = Math.cos(this._halfyaw);
  469. this._quaternion.x = (this._cosYaw * this._sinPitch * this._cosRoll) + (this._sinYaw * this._cosPitch * this._sinRoll);
  470. this._quaternion.y = (this._sinYaw * this._cosPitch * this._cosRoll) - (this._cosYaw * this._sinPitch * this._sinRoll);
  471. this._quaternion.z = (this._cosYaw * this._cosPitch * this._sinRoll) - (this._sinYaw * this._sinPitch * this._cosRoll);
  472. this._quaternion.w = (this._cosYaw * this._cosPitch * this._cosRoll) + (this._sinYaw * this._sinPitch * this._sinRoll);
  473. };
  474. SolidParticleSystem.prototype._quaternionToRotationMatrix = function () {
  475. this._rotMatrix.m[0] = 1.0 - (2.0 * (this._quaternion.y * this._quaternion.y + this._quaternion.z * this._quaternion.z));
  476. this._rotMatrix.m[1] = 2.0 * (this._quaternion.x * this._quaternion.y + this._quaternion.z * this._quaternion.w);
  477. this._rotMatrix.m[2] = 2.0 * (this._quaternion.z * this._quaternion.x - this._quaternion.y * this._quaternion.w);
  478. this._rotMatrix.m[3] = 0;
  479. this._rotMatrix.m[4] = 2.0 * (this._quaternion.x * this._quaternion.y - this._quaternion.z * this._quaternion.w);
  480. this._rotMatrix.m[5] = 1.0 - (2.0 * (this._quaternion.z * this._quaternion.z + this._quaternion.x * this._quaternion.x));
  481. this._rotMatrix.m[6] = 2.0 * (this._quaternion.y * this._quaternion.z + this._quaternion.x * this._quaternion.w);
  482. this._rotMatrix.m[7] = 0;
  483. this._rotMatrix.m[8] = 2.0 * (this._quaternion.z * this._quaternion.x + this._quaternion.y * this._quaternion.w);
  484. this._rotMatrix.m[9] = 2.0 * (this._quaternion.y * this._quaternion.z - this._quaternion.x * this._quaternion.w);
  485. this._rotMatrix.m[10] = 1.0 - (2.0 * (this._quaternion.y * this._quaternion.y + this._quaternion.x * this._quaternion.x));
  486. this._rotMatrix.m[11] = 0;
  487. this._rotMatrix.m[12] = 0;
  488. this._rotMatrix.m[13] = 0;
  489. this._rotMatrix.m[14] = 0;
  490. this._rotMatrix.m[15] = 1.0;
  491. };
  492. /**
  493. * Disposes the SPS
  494. */
  495. SolidParticleSystem.prototype.dispose = function () {
  496. this.mesh.dispose();
  497. this.vars = null;
  498. // drop references to internal big arrays for the GC
  499. this._positions = null;
  500. this._indices = null;
  501. this._normals = null;
  502. this._uvs = null;
  503. this._colors = null;
  504. this._positions32 = null;
  505. this._normals32 = null;
  506. this._fixedNormal32 = null;
  507. this._uvs32 = null;
  508. this._colors32 = null;
  509. this.pickedParticles = null;
  510. };
  511. /**
  512. * Visibilty helper : Recomputes the visible size according to the mesh bounding box
  513. * doc : http://doc.babylonjs.com/tutorials/Solid_Particle_System#sps-visibility
  514. */
  515. SolidParticleSystem.prototype.refreshVisibleSize = function () {
  516. if (!this._isVisibilityBoxLocked) {
  517. this.mesh.refreshBoundingInfo();
  518. }
  519. };
  520. /** Visibility helper : Sets the size of a visibility box, this sets the underlying mesh bounding box.
  521. * @param size the size (float) of the visibility box
  522. * note : this doesn't lock the SPS mesh bounding box.
  523. * doc : http://doc.babylonjs.com/tutorials/Solid_Particle_System#sps-visibility
  524. */
  525. SolidParticleSystem.prototype.setVisibilityBox = function (size) {
  526. var vis = size / 2;
  527. this.mesh._boundingInfo = new BABYLON.BoundingInfo(new BABYLON.Vector3(-vis, -vis, -vis), new BABYLON.Vector3(vis, vis, vis));
  528. };
  529. Object.defineProperty(SolidParticleSystem.prototype, "isAlwaysVisible", {
  530. // getter and setter
  531. /**
  532. * True if the SPS is set as always visible
  533. */
  534. get: function () {
  535. return this._alwaysVisible;
  536. },
  537. /**
  538. * Sets the SPS as always visible or not
  539. * doc : http://doc.babylonjs.com/tutorials/Solid_Particle_System#sps-visibility
  540. */
  541. set: function (val) {
  542. this._alwaysVisible = val;
  543. this.mesh.alwaysSelectAsActiveMesh = val;
  544. },
  545. enumerable: true,
  546. configurable: true
  547. });
  548. Object.defineProperty(SolidParticleSystem.prototype, "isVisibilityBoxLocked", {
  549. /**
  550. * True if the SPS visibility box is locked. The underlying mesh bounding box is then not updatable any more.
  551. */
  552. get: function () {
  553. return this._isVisibilityBoxLocked;
  554. },
  555. /**
  556. * Sets the SPS visibility box as locked or not. This enables/disables the underlying mesh bounding box updates.
  557. * doc : http://doc.babylonjs.com/tutorials/Solid_Particle_System#sps-visibility
  558. */
  559. set: function (val) {
  560. this._isVisibilityBoxLocked = val;
  561. this.mesh.getBoundingInfo().isLocked = val;
  562. },
  563. enumerable: true,
  564. configurable: true
  565. });
  566. Object.defineProperty(SolidParticleSystem.prototype, "computeParticleRotation", {
  567. // getters
  568. get: function () {
  569. return this._computeParticleRotation;
  570. },
  571. // Optimizer setters
  572. /**
  573. * Tells to setParticle() to compute the particle rotations or not.
  574. * Default value : true. The SPS is faster when it's set to false.
  575. * Note : the particle rotations aren't stored values, so setting computeParticleRotation to false will prevents the particle to rotate.
  576. */
  577. set: function (val) {
  578. this._computeParticleRotation = val;
  579. },
  580. enumerable: true,
  581. configurable: true
  582. });
  583. Object.defineProperty(SolidParticleSystem.prototype, "computeParticleColor", {
  584. get: function () {
  585. return this._computeParticleColor;
  586. },
  587. /**
  588. * Tells to setParticle() to compute the particle colors or not.
  589. * Default value : true. The SPS is faster when it's set to false.
  590. * Note : the particle colors are stored values, so setting computeParticleColor to false will keep yet the last colors set.
  591. */
  592. set: function (val) {
  593. this._computeParticleColor = val;
  594. },
  595. enumerable: true,
  596. configurable: true
  597. });
  598. Object.defineProperty(SolidParticleSystem.prototype, "computeParticleTexture", {
  599. get: function () {
  600. return this._computeParticleTexture;
  601. },
  602. /**
  603. * Tells to setParticle() to compute the particle textures or not.
  604. * Default value : true. The SPS is faster when it's set to false.
  605. * Note : the particle textures are stored values, so setting computeParticleTexture to false will keep yet the last colors set.
  606. */
  607. set: function (val) {
  608. this._computeParticleTexture = val;
  609. },
  610. enumerable: true,
  611. configurable: true
  612. });
  613. Object.defineProperty(SolidParticleSystem.prototype, "computeParticleVertex", {
  614. get: function () {
  615. return this._computeParticleVertex;
  616. },
  617. /**
  618. * Tells to setParticle() to call the vertex function for each vertex of each particle, or not.
  619. * Default value : false. The SPS is faster when it's set to false.
  620. * Note : the particle custom vertex positions aren't stored values.
  621. */
  622. set: function (val) {
  623. this._computeParticleVertex = val;
  624. },
  625. enumerable: true,
  626. configurable: true
  627. });
  628. // =======================================================================
  629. // Particle behavior logic
  630. // these following methods may be overwritten by the user to fit his needs
  631. /**
  632. * This function does nothing. It may be overwritten to set all the particles first values.
  633. * The SPS doesn't call this function, you may have to call it by your own.
  634. * doc : http://doc.babylonjs.com/tutorials/Solid_Particle_System#particle-management
  635. */
  636. SolidParticleSystem.prototype.initParticles = function () {
  637. };
  638. /**
  639. * This function does nothing. It may be overwritten to recycle a particle.
  640. * The SPS doesn't call this function, you may have to call it by your own.
  641. * doc : http://doc.babylonjs.com/tutorials/Solid_Particle_System#particle-management
  642. */
  643. SolidParticleSystem.prototype.recycleParticle = function (particle) {
  644. return particle;
  645. };
  646. /**
  647. * Updates a particle : this function should be overwritten by the user.
  648. * It is called on each particle by setParticles(). This is the place to code each particle behavior.
  649. * doc : http://doc.babylonjs.com/tutorials/Solid_Particle_System#particle-management
  650. * ex : just set a particle position or velocity and recycle conditions
  651. */
  652. SolidParticleSystem.prototype.updateParticle = function (particle) {
  653. return particle;
  654. };
  655. /**
  656. * Updates a vertex of a particle : it can be overwritten by the user.
  657. * This will be called on each vertex particle by setParticles() if computeParticleVertex is set to true only.
  658. * @param particle the current particle
  659. * @param vertex the current index of the current particle
  660. * @param pt the index of the current vertex in the particle shape
  661. * doc : http://doc.babylonjs.com/tutorials/Solid_Particle_System#update-each-particle-shape
  662. * ex : just set a vertex particle position
  663. */
  664. SolidParticleSystem.prototype.updateParticleVertex = function (particle, vertex, pt) {
  665. return vertex;
  666. };
  667. /**
  668. * This will be called before any other treatment by setParticles() and will be passed three parameters.
  669. * This does nothing and may be overwritten by the user.
  670. * @param start the particle index in the particle array where to stop to iterate, same than the value passed to setParticle()
  671. * @param stop the particle index in the particle array where to stop to iterate, same than the value passed to setParticle()
  672. * @param update the boolean update value actually passed to setParticles()
  673. */
  674. SolidParticleSystem.prototype.beforeUpdateParticles = function (start, stop, update) {
  675. };
  676. /**
  677. * This will be called by setParticles() after all the other treatments and just before the actual mesh update.
  678. * This will be passed three parameters.
  679. * This does nothing and may be overwritten by the user.
  680. * @param start the particle index in the particle array where to stop to iterate, same than the value passed to setParticle()
  681. * @param stop the particle index in the particle array where to stop to iterate, same than the value passed to setParticle()
  682. * @param update the boolean update value actually passed to setParticles()
  683. */
  684. SolidParticleSystem.prototype.afterUpdateParticles = function (start, stop, update) {
  685. };
  686. return SolidParticleSystem;
  687. })();
  688. BABYLON.SolidParticleSystem = SolidParticleSystem;
  689. })(BABYLON || (BABYLON = {}));