babylon.solidParticleSystem.js 29 KB

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