babylon.solidParticleSystem.ts 36 KB

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