babylon.solidParticleSystem.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. var BABYLON;
  2. (function (BABYLON) {
  3. var SolidParticleSystem = (function () {
  4. function SolidParticleSystem(name, scene) {
  5. // public members
  6. this.particles = new Array();
  7. this.nbParticles = 0;
  8. this.billboard = false;
  9. this.counter = 0;
  10. this._positions = new Array();
  11. this._indices = new Array();
  12. this._normals = new Array();
  13. this._colors = new Array();
  14. this._uvs = new Array();
  15. this._index = 0; // indices index
  16. this._shapeCounter = 0;
  17. this._useParticleColor = true;
  18. this._useParticleTexture = true;
  19. this._useParticleRotation = true;
  20. this._useParticleVertex = false;
  21. this._cam_axisZ = BABYLON.Vector3.Zero();
  22. this._cam_axisY = BABYLON.Vector3.Zero();
  23. this._cam_axisX = BABYLON.Vector3.Zero();
  24. this._axisX = BABYLON.Axis.X;
  25. this._axisY = BABYLON.Axis.Y;
  26. this._axisZ = BABYLON.Axis.Z;
  27. this._fakeCamPos = BABYLON.Vector3.Zero();
  28. this._rotMatrix = new BABYLON.Matrix();
  29. this._invertedMatrix = new BABYLON.Matrix();
  30. this._rotated = BABYLON.Vector3.Zero();
  31. this._quaternion = new BABYLON.Quaternion();
  32. this._vertex = BABYLON.Vector3.Zero();
  33. this._yaw = 0.0;
  34. this._pitch = 0.0;
  35. this._roll = 0.0;
  36. this._halfroll = 0.0;
  37. this._halfpitch = 0.0;
  38. this._halfyaw = 0.0;
  39. this._sinRoll = 0.0;
  40. this._cosRoll = 0.0;
  41. this._sinPitch = 0.0;
  42. this._cosPitch = 0.0;
  43. this._sinYaw = 0.0;
  44. this._cosYaw = 0.0;
  45. this.name = name;
  46. this._scene = scene;
  47. this._camera = scene.activeCamera;
  48. }
  49. // build the SPS mesh : returns the mesh
  50. SolidParticleSystem.prototype.buildMesh = function () {
  51. if (this.nbParticles === 0) {
  52. var triangle = BABYLON.Mesh.CreateDisc("", { radius: 1, tessellation: 3 }, this._scene);
  53. this.addShape(triangle, 1);
  54. triangle.dispose();
  55. }
  56. BABYLON.VertexData.ComputeNormals(this._positions, this._indices, this._normals);
  57. var vertexData = new BABYLON.VertexData();
  58. vertexData.positions = this._positions;
  59. vertexData.indices = this._indices;
  60. vertexData.normals = this._normals;
  61. if (this._uvs) {
  62. vertexData.uvs = this._uvs;
  63. }
  64. if (this._colors) {
  65. vertexData.colors = this._colors;
  66. }
  67. var mesh = new BABYLON.Mesh(name, this._scene);
  68. vertexData.applyToMesh(mesh, true);
  69. this.mesh = mesh;
  70. return mesh;
  71. };
  72. // _meshBuilder : inserts the shape model in the global SPS mesh
  73. SolidParticleSystem.prototype._meshBuilder = function (p, shape, positions, meshInd, indices, meshUV, uvs, meshCol, colors) {
  74. var i;
  75. var u = 0;
  76. var c = 0;
  77. for (i = 0; i < shape.length; i++) {
  78. positions.push(shape[i].x, shape[i].y, shape[i].z);
  79. if (meshUV) {
  80. uvs.push(meshUV[u], meshUV[u + 1]);
  81. u += 2;
  82. }
  83. if (meshCol) {
  84. colors.push(meshCol[c], meshCol[c + 1], meshCol[c + 2], meshCol[c + 3]);
  85. c += 4;
  86. }
  87. else {
  88. colors.push(1, 1, 1, 1);
  89. }
  90. }
  91. for (i = 0; i < meshInd.length; i++) {
  92. indices.push(p + meshInd[i]);
  93. }
  94. };
  95. // returns a shape array from positions array
  96. SolidParticleSystem.prototype._posToShape = function (positions) {
  97. var shape = [];
  98. for (var i = 0; i < positions.length; i += 3) {
  99. shape.push(new BABYLON.Vector3(positions[i], positions[i + 1], positions[i + 2]));
  100. }
  101. return shape;
  102. };
  103. // returns a shapeUV array from a Vector4 uvs
  104. SolidParticleSystem.prototype._uvsToShapeUV = function (uvs) {
  105. var shapeUV = [];
  106. if (uvs) {
  107. for (var i = 0; i < uvs.length; i++)
  108. shapeUV.push(uvs[i]);
  109. }
  110. return shapeUV;
  111. };
  112. // adds a new particle object in the particles array and double links the particle (next/previous)
  113. SolidParticleSystem.prototype._addParticle = function (p, idxpos, shape, shapeUV, shapeId) {
  114. this._particle = new BABYLON.SolidParticle(p, idxpos, shape, shapeUV, shapeId);
  115. this.particles.push(this._particle);
  116. this._particle.previous = this._previousParticle;
  117. if (this._previousParticle) {
  118. this._previousParticle.next = this._particle;
  119. }
  120. this._previousParticle = this._particle;
  121. };
  122. // add solid particles from a shape model in the particles array
  123. SolidParticleSystem.prototype.addShape = function (mesh, nb) {
  124. var meshPos = mesh.getVerticesData(BABYLON.VertexBuffer.PositionKind);
  125. var meshInd = mesh.getIndices();
  126. var meshUV = mesh.getVerticesData(BABYLON.VertexBuffer.UVKind);
  127. var meshCol = mesh.getVerticesData(BABYLON.VertexBuffer.ColorKind);
  128. var shape = this._posToShape(meshPos);
  129. var shapeUV = this._uvsToShapeUV(meshUV);
  130. // particles
  131. for (var i = 0; i < nb; i++) {
  132. this._meshBuilder(this._index, shape, this._positions, meshInd, this._indices, meshUV, this._uvs, meshCol, this._colors);
  133. this._addParticle(this.nbParticles + i, this._positions.length, shape, shapeUV, this._shapeCounter);
  134. this._index += shape.length;
  135. }
  136. this.nbParticles += nb;
  137. this._shapeCounter++;
  138. return this._shapeCounter;
  139. };
  140. // resets a particle back to its just built status
  141. SolidParticleSystem.prototype.resetParticle = function (particle) {
  142. for (var pt = 0; pt < particle._shape.length; pt++) {
  143. this._positions[particle._pos + pt * 3] = particle._shape[pt].x;
  144. this._positions[particle._pos + pt * 3 + 1] = particle._shape[pt].y;
  145. this._positions[particle._pos + pt * 3 + 2] = particle._shape[pt].z;
  146. }
  147. };
  148. // sets all the particles
  149. SolidParticleSystem.prototype.setParticles = function (start, end, update) {
  150. if (start === void 0) { start = 0; }
  151. if (end === void 0) { end = this.nbParticles - 1; }
  152. if (update === void 0) { update = true; }
  153. // custom beforeUpdate
  154. this.beforeUpdateParticles(start, end, update);
  155. this._cam_axisX.x = 1;
  156. this._cam_axisX.y = 0;
  157. this._cam_axisX.z = 0;
  158. this._cam_axisY.x = 0;
  159. this._cam_axisY.y = 1;
  160. this._cam_axisY.z = 0;
  161. this._cam_axisZ.x = 0;
  162. this._cam_axisZ.y = 0;
  163. this._cam_axisZ.z = 1;
  164. // if the particles will always face the camera
  165. if (this.billboard) {
  166. // compute a fake camera position : un-rotate the camera position by the current mesh rotation
  167. this._yaw = this.mesh.rotation.y;
  168. this._pitch = this.mesh.rotation.x;
  169. this._roll = this.mesh.rotation.z;
  170. this._quaternionRotationYPR();
  171. this._quaternionToRotationMatrix();
  172. this._rotMatrix.invertToRef(this._invertedMatrix);
  173. BABYLON.Vector3.TransformCoordinatesToRef(this._camera.globalPosition, this._invertedMatrix, this._fakeCamPos);
  174. // set two orthogonal vectors (_cam_axisX and and _cam_axisY) to the cam-mesh axis (_cam_axisZ)
  175. (this._fakeCamPos).subtractToRef(this.mesh.position, this._cam_axisZ);
  176. BABYLON.Vector3.CrossToRef(this._cam_axisZ, this._axisX, this._cam_axisY);
  177. BABYLON.Vector3.CrossToRef(this._cam_axisZ, this._cam_axisY, this._cam_axisX);
  178. this._cam_axisY.normalize();
  179. this._cam_axisX.normalize();
  180. this._cam_axisZ.normalize();
  181. }
  182. BABYLON.Matrix.IdentityToRef(this._rotMatrix);
  183. var idx = 0;
  184. var index = 0;
  185. var colidx = 0;
  186. var colorIndex = 0;
  187. var uvidx = 0;
  188. var uvIndex = 0;
  189. // particle loop
  190. for (var p = start; p <= end; p++) {
  191. this._particle = this.particles[p];
  192. // call to custom user function to update the particle properties
  193. this.updateParticle(this._particle);
  194. // particle rotation matrix
  195. if (this.billboard) {
  196. this._particle.rotation.x = 0.0;
  197. this._particle.rotation.y = 0.0;
  198. }
  199. if (this._useParticleRotation) {
  200. if (this._particle.quaternion) {
  201. this._quaternion.x = this._particle.quaternion.x;
  202. this._quaternion.y = this._particle.quaternion.y;
  203. this._quaternion.z = this._particle.quaternion.z;
  204. this._quaternion.w = this._particle.quaternion.w;
  205. }
  206. else {
  207. this._yaw = this._particle.rotation.y;
  208. this._pitch = this._particle.rotation.x;
  209. this._roll = this._particle.rotation.z;
  210. this._quaternionRotationYPR();
  211. }
  212. this._quaternionToRotationMatrix();
  213. }
  214. for (var pt = 0; pt < this._particle._shape.length; pt++) {
  215. idx = index + pt * 3;
  216. colidx = colorIndex + pt * 4;
  217. uvidx = uvIndex + pt * 2;
  218. this._vertex.x = this._particle._shape[pt].x;
  219. this._vertex.y = this._particle._shape[pt].y;
  220. this._vertex.z = this._particle._shape[pt].z;
  221. if (this._useParticleVertex) {
  222. this.updateParticleVertex(this._particle, this._vertex, pt);
  223. }
  224. BABYLON.Vector3.TransformCoordinatesToRef(this._vertex, this._rotMatrix, this._rotated);
  225. this._positions[idx] = this._particle.position.x + this._cam_axisX.x * this._rotated.x * this._particle.scale.x + this._cam_axisY.x * this._rotated.y * this._particle.scale.y + this._cam_axisZ.x * this._rotated.z * this._particle.scale.z;
  226. this._positions[idx + 1] = this._particle.position.y + this._cam_axisX.y * this._rotated.x * this._particle.scale.x + this._cam_axisY.y * this._rotated.y * this._particle.scale.y + this._cam_axisZ.y * this._rotated.z * this._particle.scale.z;
  227. this._positions[idx + 2] = this._particle.position.z + this._cam_axisX.z * this._rotated.x * this._particle.scale.x + this._cam_axisY.z * this._rotated.y * this._particle.scale.y + this._cam_axisZ.z * this._rotated.z * this._particle.scale.z;
  228. if (this._useParticleColor) {
  229. this._colors[colidx] = this._particle.color.r;
  230. this._colors[colidx + 1] = this._particle.color.g;
  231. this._colors[colidx + 2] = this._particle.color.b;
  232. this._colors[colidx + 3] = this._particle.color.a;
  233. }
  234. if (this._useParticleTexture) {
  235. this._uvs[uvidx] = this._particle._shapeUV[pt * 2] * (this._particle.uvs.z - this._particle.uvs.x) + this._particle.uvs.x;
  236. this._uvs[uvidx + 1] = this._particle._shapeUV[pt * 2 + 1] * (this._particle.uvs.w - this._particle.uvs.y) + this._particle.uvs.y;
  237. }
  238. }
  239. index = idx + 3;
  240. colorIndex = colidx + 4;
  241. uvIndex = uvidx + 2;
  242. }
  243. if (update) {
  244. if (this._useParticleColor) {
  245. this.mesh.updateVerticesData(BABYLON.VertexBuffer.ColorKind, this._colors, false, false);
  246. }
  247. if (this._useParticleTexture) {
  248. this.mesh.updateVerticesData(BABYLON.VertexBuffer.UVKind, this._uvs, false, false);
  249. }
  250. this.mesh.updateVerticesData(BABYLON.VertexBuffer.PositionKind, this._positions, false, false);
  251. if (!this.mesh.areNormalsFrozen) {
  252. BABYLON.VertexData.ComputeNormals(this._positions, this._indices, this._normals);
  253. this.mesh.updateVerticesData(BABYLON.VertexBuffer.NormalKind, this._normals, false, false);
  254. }
  255. }
  256. this.afterUpdateParticles(start, end, update);
  257. };
  258. SolidParticleSystem.prototype._quaternionRotationYPR = function () {
  259. this._halfroll = this._roll * 0.5;
  260. this._halfpitch = this._pitch * 0.5;
  261. this._halfyaw = this._yaw * 0.5;
  262. this._sinRoll = Math.sin(this._halfroll);
  263. this._cosRoll = Math.cos(this._halfroll);
  264. this._sinPitch = Math.sin(this._halfpitch);
  265. this._cosPitch = Math.cos(this._halfpitch);
  266. this._sinYaw = Math.sin(this._halfyaw);
  267. this._cosYaw = Math.cos(this._halfyaw);
  268. this._quaternion.x = (this._cosYaw * this._sinPitch * this._cosRoll) + (this._sinYaw * this._cosPitch * this._sinRoll);
  269. this._quaternion.y = (this._sinYaw * this._cosPitch * this._cosRoll) - (this._cosYaw * this._sinPitch * this._sinRoll);
  270. this._quaternion.z = (this._cosYaw * this._cosPitch * this._sinRoll) - (this._sinYaw * this._sinPitch * this._cosRoll);
  271. this._quaternion.w = (this._cosYaw * this._cosPitch * this._cosRoll) + (this._sinYaw * this._sinPitch * this._sinRoll);
  272. };
  273. SolidParticleSystem.prototype._quaternionToRotationMatrix = function () {
  274. this._rotMatrix.m[0] = 1.0 - (2.0 * (this._quaternion.y * this._quaternion.y + this._quaternion.z * this._quaternion.z));
  275. this._rotMatrix.m[1] = 2.0 * (this._quaternion.x * this._quaternion.y + this._quaternion.z * this._quaternion.w);
  276. this._rotMatrix.m[2] = 2.0 * (this._quaternion.z * this._quaternion.x - this._quaternion.y * this._quaternion.w);
  277. this._rotMatrix.m[3] = 0;
  278. this._rotMatrix.m[4] = 2.0 * (this._quaternion.x * this._quaternion.y - this._quaternion.z * this._quaternion.w);
  279. this._rotMatrix.m[5] = 1.0 - (2.0 * (this._quaternion.z * this._quaternion.z + this._quaternion.x * this._quaternion.x));
  280. this._rotMatrix.m[6] = 2.0 * (this._quaternion.y * this._quaternion.z + this._quaternion.x * this._quaternion.w);
  281. this._rotMatrix.m[7] = 0;
  282. this._rotMatrix.m[8] = 2.0 * (this._quaternion.z * this._quaternion.x + this._quaternion.y * this._quaternion.w);
  283. this._rotMatrix.m[9] = 2.0 * (this._quaternion.y * this._quaternion.z - this._quaternion.x * this._quaternion.w);
  284. this._rotMatrix.m[10] = 1.0 - (2.0 * (this._quaternion.y * this._quaternion.y + this._quaternion.x * this._quaternion.x));
  285. this._rotMatrix.m[11] = 0;
  286. this._rotMatrix.m[12] = 0;
  287. this._rotMatrix.m[13] = 0;
  288. this._rotMatrix.m[14] = 0;
  289. this._rotMatrix.m[15] = 1.0;
  290. };
  291. // dispose the SPS
  292. SolidParticleSystem.prototype.dispose = function () {
  293. this.mesh.dispose();
  294. };
  295. Object.defineProperty(SolidParticleSystem.prototype, "useParticleRotation", {
  296. // getters
  297. get: function () {
  298. return this._useParticleRotation;
  299. },
  300. // Optimizer setters
  301. set: function (val) {
  302. this._useParticleRotation = val;
  303. },
  304. enumerable: true,
  305. configurable: true
  306. });
  307. Object.defineProperty(SolidParticleSystem.prototype, "useParticleColor", {
  308. get: function () {
  309. return this._useParticleColor;
  310. },
  311. set: function (val) {
  312. this._useParticleColor = val;
  313. },
  314. enumerable: true,
  315. configurable: true
  316. });
  317. Object.defineProperty(SolidParticleSystem.prototype, "useParticleTexture", {
  318. get: function () {
  319. return this._useParticleTexture;
  320. },
  321. set: function (val) {
  322. this._useParticleTexture = val;
  323. },
  324. enumerable: true,
  325. configurable: true
  326. });
  327. Object.defineProperty(SolidParticleSystem.prototype, "useParticleVertex", {
  328. get: function () {
  329. return this._useParticleVertex;
  330. },
  331. set: function (val) {
  332. this._useParticleVertex = val;
  333. },
  334. enumerable: true,
  335. configurable: true
  336. });
  337. // =======================================================================
  338. // Particle behavior logic
  339. // these following methods may be overwritten by the user to fit his needs
  340. // init : sets all particles first values and calls updateParticle to set them in space
  341. // can be overwritten by the user
  342. SolidParticleSystem.prototype.initParticles = function () {
  343. };
  344. // recycles a particle : can by overwritten by the user
  345. SolidParticleSystem.prototype.recycleParticle = function (particle) {
  346. return particle;
  347. };
  348. // updates a particle : can be overwritten by the user
  349. // will be called on each particle by setParticles() :
  350. // ex : just set a particle position or velocity and recycle conditions
  351. SolidParticleSystem.prototype.updateParticle = function (particle) {
  352. return particle;
  353. };
  354. // updates a vertex of a particle : can be overwritten by the user
  355. // will be called on each vertex particle by setParticles() :
  356. // ex : just set a vertex particle position
  357. SolidParticleSystem.prototype.updateParticleVertex = function (particle, vertex, i) {
  358. return vertex;
  359. };
  360. // will be called before any other treatment by setParticles()
  361. SolidParticleSystem.prototype.beforeUpdateParticles = function (start, stop, update) {
  362. };
  363. // will be called after all setParticles() treatments
  364. SolidParticleSystem.prototype.afterUpdateParticles = function (start, stop, update) {
  365. };
  366. return SolidParticleSystem;
  367. })();
  368. BABYLON.SolidParticleSystem = SolidParticleSystem;
  369. })(BABYLON || (BABYLON = {}));
  370. //# sourceMappingURL=babylon.solidParticleSystem.js.map