babylon.solidParticleSystem.js 35 KB

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