babylon.solidParticleSystem.ts 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838
  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. /**
  130. * Digests the mesh and generates as many solid particles in the system as wanted.
  131. * These particles will have the same geometry than the mesh parts and will be positioned at the same localisation than the mesh original places.
  132. * Thus the particles generated from digest() have their property "positiion" yet set.
  133. * @param mesh the mesh to be digested
  134. * @param facetNb the number of mesh facets per particle (optional, default 1), this parameter is overriden by the parameter "number" if any
  135. * @param number the wanted number of particles : each particle is built with mesh_total_facets / number facets (optional)
  136. */
  137. public digest(mesh: Mesh, options?: { facetNb?: number; number?: number }): void {
  138. var size: number = (options && options.facetNb) || 1;
  139. var number: number = (options && options.number);
  140. var meshPos = mesh.getVerticesData(VertexBuffer.PositionKind);
  141. var meshInd = mesh.getIndices();
  142. var meshUV = mesh.getVerticesData(VertexBuffer.UVKind);
  143. var meshCol = mesh.getVerticesData(VertexBuffer.ColorKind);
  144. var f: number = 0; // facet counter
  145. var totalFacets: number = meshInd.length / 3; // a facet is a triangle, so 3 indices
  146. // compute size from number
  147. if (number) {
  148. number = (number > totalFacets) ? totalFacets : number;
  149. size = Math.round(totalFacets / number);
  150. } else {
  151. size = (size > totalFacets) ? totalFacets : size;
  152. }
  153. var facetPos: number[] = []; // submesh positions
  154. var facetInd: number[] = []; // submesh indices
  155. var facetUV: number[] = []; // submesh UV
  156. var facetCol: number[] = []; // submesh colors
  157. var barycenter: Vector3 = Tmp.Vector3[0];
  158. while (f < totalFacets) {
  159. if (f > totalFacets - size) {
  160. size = totalFacets - f;
  161. }
  162. // reset temp arrays
  163. facetPos.length = 0;
  164. facetInd.length = 0;
  165. facetUV.length = 0;
  166. facetCol.length = 0;
  167. // iterate over "size" facets
  168. var fi: number = 0;
  169. for (var j = f * 3; j < (f + size) * 3; j++) {
  170. facetInd.push(fi);
  171. var i: number = meshInd[j];
  172. facetPos.push(meshPos[i * 3], meshPos[i * 3 + 1], meshPos[i * 3 + 2]);
  173. if (meshUV) {
  174. facetUV.push(meshUV[i * 2], meshUV[i * 2 + 1]);
  175. }
  176. if (meshCol) {
  177. facetCol.push(meshCol[i * 4], meshCol[i * 4 + 1], meshCol[i * 4 + 2], meshCol[i * 4 + 3]);
  178. }
  179. fi++;
  180. }
  181. // create a model shape for each single particle
  182. var idx: number = this.nbParticles;
  183. var shape: Vector3[] = this._posToShape(facetPos);
  184. var shapeUV: number[] = this._uvsToShapeUV(facetUV);
  185. // compute the barycenter of the shape
  186. var v: number;
  187. for (v = 0; v < shape.length; v++) {
  188. barycenter.addInPlace(shape[v]);
  189. }
  190. barycenter.scaleInPlace(1 / shape.length);
  191. // shift the shape from its barycenter to the origin
  192. for (v = 0; v < shape.length; v++) {
  193. shape[v].subtractInPlace(barycenter);
  194. }
  195. var modelShape = new ModelShape(this._shapeCounter, shape, shapeUV, null, null);
  196. // add the particle in the SPS
  197. this._meshBuilder(this._index, shape, this._positions, facetInd, this._indices, facetUV, this._uvs, facetCol, this._colors, idx, 0, null);
  198. this._addParticle(idx, this._positions.length, modelShape, this._shapeCounter, 0);
  199. // initialize the particle position
  200. this.particles[this.nbParticles].position.addInPlace(barycenter);
  201. this._index += shape.length;
  202. idx++;
  203. this.nbParticles++;
  204. this._shapeCounter++;
  205. f += size;
  206. }
  207. }
  208. //reset copy
  209. private _resetCopy() {
  210. this._copy.position.x = 0;
  211. this._copy.position.y = 0;
  212. this._copy.position.z = 0;
  213. this._copy.rotation.x = 0;
  214. this._copy.rotation.y = 0;
  215. this._copy.rotation.z = 0;
  216. this._copy.quaternion = null;
  217. this._copy.scale.x = 1;
  218. this._copy.scale.y = 1;
  219. this._copy.scale.z = 1;
  220. this._copy.uvs.x = 0;
  221. this._copy.uvs.y = 0;
  222. this._copy.uvs.z = 1;
  223. this._copy.uvs.w = 1;
  224. this._copy.color = null;
  225. }
  226. // _meshBuilder : inserts the shape model in the global SPS mesh
  227. private _meshBuilder(p, shape, positions, meshInd, indices, meshUV, uvs, meshCol, colors, idx, idxInShape, options): void {
  228. var i;
  229. var u = 0;
  230. var c = 0;
  231. this._resetCopy();
  232. if (options && options.positionFunction) { // call to custom positionFunction
  233. options.positionFunction(this._copy, idx, idxInShape);
  234. }
  235. if (this._copy.quaternion) {
  236. this._quaternion.x = this._copy.quaternion.x;
  237. this._quaternion.y = this._copy.quaternion.y;
  238. this._quaternion.z = this._copy.quaternion.z;
  239. this._quaternion.w = this._copy.quaternion.w;
  240. } else {
  241. this._yaw = this._copy.rotation.y;
  242. this._pitch = this._copy.rotation.x;
  243. this._roll = this._copy.rotation.z;
  244. this._quaternionRotationYPR();
  245. }
  246. this._quaternionToRotationMatrix();
  247. for (i = 0; i < shape.length; i++) {
  248. this._vertex.x = shape[i].x;
  249. this._vertex.y = shape[i].y;
  250. this._vertex.z = shape[i].z;
  251. if (options && options.vertexFunction) {
  252. options.vertexFunction(this._copy, this._vertex, i);
  253. }
  254. this._vertex.x *= this._copy.scale.x;
  255. this._vertex.y *= this._copy.scale.y;
  256. this._vertex.z *= this._copy.scale.z;
  257. Vector3.TransformCoordinatesToRef(this._vertex, this._rotMatrix, this._rotated);
  258. positions.push(this._copy.position.x + this._rotated.x, this._copy.position.y + this._rotated.y, this._copy.position.z + this._rotated.z);
  259. if (meshUV) {
  260. 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);
  261. u += 2;
  262. }
  263. if (this._copy.color) {
  264. this._color = this._copy.color;
  265. } else if (meshCol && meshCol[c]) {
  266. this._color.r = meshCol[c];
  267. this._color.g = meshCol[c + 1];
  268. this._color.b = meshCol[c + 2];
  269. this._color.a = meshCol[c + 3];
  270. } else {
  271. this._color.r = 1;
  272. this._color.g = 1;
  273. this._color.b = 1;
  274. this._color.a = 1;
  275. }
  276. colors.push(this._color.r, this._color.g, this._color.b, this._color.a);
  277. c += 4;
  278. }
  279. for (i = 0; i < meshInd.length; i++) {
  280. indices.push(p + meshInd[i]);
  281. }
  282. if (this._pickable) {
  283. var nbfaces = meshInd.length / 3;
  284. for (i = 0; i < nbfaces; i++) {
  285. this.pickedParticles.push({ idx: idx, faceId: i });
  286. }
  287. }
  288. }
  289. // returns a shape array from positions array
  290. private _posToShape(positions): Vector3[] {
  291. var shape = [];
  292. for (var i = 0; i < positions.length; i += 3) {
  293. shape.push(new Vector3(positions[i], positions[i + 1], positions[i + 2]));
  294. }
  295. return shape;
  296. }
  297. // returns a shapeUV array from a Vector4 uvs
  298. private _uvsToShapeUV(uvs): number[] {
  299. var shapeUV = [];
  300. if (uvs) {
  301. for (var i = 0; i < uvs.length; i++)
  302. shapeUV.push(uvs[i]);
  303. }
  304. return shapeUV;
  305. }
  306. // adds a new particle object in the particles array
  307. private _addParticle(idx: number, idxpos: number, model: ModelShape, shapeId: number, idxInShape: number): void {
  308. this.particles.push(new SolidParticle(idx, idxpos, model, shapeId, idxInShape));
  309. }
  310. /**
  311. * Adds some particles to the SPS from the model shape.
  312. * Please read the doc : http://doc.babylonjs.com/tutorials/Solid_Particle_System#create-an-immutable-sps
  313. * @param mesh any Mesh object that will be used as a model for the solid particles.
  314. * @param nb the number of particles to be created from this model
  315. * @param positionFunction an optional javascript function to called for each particle on SPS creation
  316. * @param vertexFunction an optional javascript function to called for each vertex of each particle on SPS creation
  317. */
  318. public addShape(mesh: Mesh, nb: number, options?: { positionFunction?: any; vertexFunction?: any }): number {
  319. var meshPos = mesh.getVerticesData(VertexBuffer.PositionKind);
  320. var meshInd = mesh.getIndices();
  321. var meshUV = mesh.getVerticesData(VertexBuffer.UVKind);
  322. var meshCol = mesh.getVerticesData(VertexBuffer.ColorKind);
  323. var shape = this._posToShape(meshPos);
  324. var shapeUV = this._uvsToShapeUV(meshUV);
  325. var posfunc = options ? options.positionFunction : null;
  326. var vtxfunc = options ? options.vertexFunction : null;
  327. var modelShape = new ModelShape(this._shapeCounter, shape, shapeUV, posfunc, vtxfunc);
  328. // particles
  329. var idx = this.nbParticles;
  330. for (var i = 0; i < nb; i++) {
  331. this._meshBuilder(this._index, shape, this._positions, meshInd, this._indices, meshUV, this._uvs, meshCol, this._colors, idx, i, options);
  332. if (this._updatable) {
  333. this._addParticle(idx, this._positions.length, modelShape, this._shapeCounter, i);
  334. }
  335. this._index += shape.length;
  336. idx++;
  337. }
  338. this.nbParticles += nb;
  339. this._shapeCounter++;
  340. return this._shapeCounter - 1;
  341. }
  342. // rebuilds a particle back to its just built status : if needed, recomputes the custom positions and vertices
  343. private _rebuildParticle(particle: SolidParticle): void {
  344. this._resetCopy();
  345. if (particle._model._positionFunction) { // recall to stored custom positionFunction
  346. particle._model._positionFunction(this._copy, particle.idx, particle.idxInShape);
  347. }
  348. if (this._copy.quaternion) {
  349. this._quaternion.x = this._copy.quaternion.x;
  350. this._quaternion.y = this._copy.quaternion.y;
  351. this._quaternion.z = this._copy.quaternion.z;
  352. this._quaternion.w = this._copy.quaternion.w;
  353. } else {
  354. this._yaw = this._copy.rotation.y;
  355. this._pitch = this._copy.rotation.x;
  356. this._roll = this._copy.rotation.z;
  357. this._quaternionRotationYPR();
  358. }
  359. this._quaternionToRotationMatrix();
  360. this._shape = particle._model._shape;
  361. for (var pt = 0; pt < this._shape.length; pt++) {
  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 (particle._model._vertexFunction) {
  366. particle._model._vertexFunction(this._copy, this._vertex, pt); // recall to stored vertexFunction
  367. }
  368. this._vertex.x *= this._copy.scale.x;
  369. this._vertex.y *= this._copy.scale.y;
  370. this._vertex.z *= this._copy.scale.z;
  371. Vector3.TransformCoordinatesToRef(this._vertex, this._rotMatrix, this._rotated);
  372. this._positions32[particle._pos + pt * 3] = this._copy.position.x + this._rotated.x;
  373. this._positions32[particle._pos + pt * 3 + 1] = this._copy.position.y + this._rotated.y;
  374. this._positions32[particle._pos + pt * 3 + 2] = this._copy.position.z + this._rotated.z;
  375. }
  376. particle.position.x = 0;
  377. particle.position.y = 0;
  378. particle.position.z = 0;
  379. particle.rotation.x = 0;
  380. particle.rotation.y = 0;
  381. particle.rotation.z = 0;
  382. particle.quaternion = null;
  383. particle.scale.x = 1;
  384. particle.scale.y = 1;
  385. particle.scale.z = 1;
  386. }
  387. /**
  388. * Rebuilds the whole mesh and updates the VBO : custom positions and vertices are recomputed if needed.
  389. */
  390. public rebuildMesh(): void {
  391. for (var p = 0; p < this.particles.length; p++) {
  392. this._rebuildParticle(this.particles[p]);
  393. }
  394. this.mesh.updateVerticesData(VertexBuffer.PositionKind, this._positions32, false, false);
  395. }
  396. /**
  397. * Sets all the particles : this method actually really updates the mesh according to the particle positions, rotations, colors, textures, etc.
  398. * This method calls updateParticle() for each particles of the SPS.
  399. * For an animated SPS, it is usually called within the render loop.
  400. * @param start (default 0) the particle index in the particle array where to start to compute the particle property values
  401. * @param end (default nbParticle - 1) the particle index in the particle array where to stop to compute the particle property values
  402. * @param update (default true) if the mesh must be finally updated on this call after all the particle computations.
  403. */
  404. public setParticles(start: number = 0, end: number = this.nbParticles - 1, update: boolean = true): void {
  405. if (!this._updatable) {
  406. return;
  407. }
  408. // custom beforeUpdate
  409. this.beforeUpdateParticles(start, end, update);
  410. this._cam_axisX.x = 1;
  411. this._cam_axisX.y = 0;
  412. this._cam_axisX.z = 0;
  413. this._cam_axisY.x = 0;
  414. this._cam_axisY.y = 1;
  415. this._cam_axisY.z = 0;
  416. this._cam_axisZ.x = 0;
  417. this._cam_axisZ.y = 0;
  418. this._cam_axisZ.z = 1;
  419. // if the particles will always face the camera
  420. if (this.billboard) {
  421. // compute a fake camera position : un-rotate the camera position by the current mesh rotation
  422. this._yaw = this.mesh.rotation.y;
  423. this._pitch = this.mesh.rotation.x;
  424. this._roll = this.mesh.rotation.z;
  425. this._quaternionRotationYPR();
  426. this._quaternionToRotationMatrix();
  427. this._rotMatrix.invertToRef(this._invertMatrix);
  428. Vector3.TransformCoordinatesToRef(this._camera.globalPosition, this._invertMatrix, this._fakeCamPos);
  429. // set two orthogonal vectors (_cam_axisX and and _cam_axisY) to the cam-mesh axis (_cam_axisZ)
  430. (this._fakeCamPos).subtractToRef(this.mesh.position, this._cam_axisZ);
  431. Vector3.CrossToRef(this._cam_axisZ, this._axisX, this._cam_axisY);
  432. Vector3.CrossToRef(this._cam_axisZ, this._cam_axisY, this._cam_axisX);
  433. this._cam_axisY.normalize();
  434. this._cam_axisX.normalize();
  435. this._cam_axisZ.normalize();
  436. }
  437. Matrix.IdentityToRef(this._rotMatrix);
  438. var idx = 0;
  439. var index = 0;
  440. var colidx = 0;
  441. var colorIndex = 0;
  442. var uvidx = 0;
  443. var uvIndex = 0;
  444. // particle loop
  445. end = (end > this.nbParticles - 1) ? this.nbParticles - 1 : end;
  446. for (var p = start; p <= end; p++) {
  447. this._particle = this.particles[p];
  448. this._shape = this._particle._model._shape;
  449. this._shapeUV = this._particle._model._shapeUV;
  450. // call to custom user function to update the particle properties
  451. this.updateParticle(this._particle);
  452. // particle rotation matrix
  453. if (this.billboard) {
  454. this._particle.rotation.x = 0.0;
  455. this._particle.rotation.y = 0.0;
  456. }
  457. if (this._computeParticleRotation) {
  458. if (this._particle.quaternion) {
  459. this._quaternion.x = this._particle.quaternion.x;
  460. this._quaternion.y = this._particle.quaternion.y;
  461. this._quaternion.z = this._particle.quaternion.z;
  462. this._quaternion.w = this._particle.quaternion.w;
  463. } else {
  464. this._yaw = this._particle.rotation.y;
  465. this._pitch = this._particle.rotation.x;
  466. this._roll = this._particle.rotation.z;
  467. this._quaternionRotationYPR();
  468. }
  469. this._quaternionToRotationMatrix();
  470. }
  471. for (var pt = 0; pt < this._shape.length; pt++) {
  472. idx = index + pt * 3;
  473. colidx = colorIndex + pt * 4;
  474. uvidx = uvIndex + pt * 2;
  475. this._vertex.x = this._shape[pt].x;
  476. this._vertex.y = this._shape[pt].y;
  477. this._vertex.z = this._shape[pt].z;
  478. if (this._computeParticleVertex) {
  479. this.updateParticleVertex(this._particle, this._vertex, pt);
  480. }
  481. // positions
  482. this._vertex.x *= this._particle.scale.x;
  483. this._vertex.y *= this._particle.scale.y;
  484. this._vertex.z *= this._particle.scale.z;
  485. 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];
  486. 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;
  487. 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;
  488. 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;
  489. 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;
  490. 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;
  491. 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;
  492. // normals : if the particles can't be morphed then just rotate the normals
  493. if (!this._computeParticleVertex && !this.billboard) {
  494. this._normal.x = this._fixedNormal32[idx];
  495. this._normal.y = this._fixedNormal32[idx + 1];
  496. this._normal.z = this._fixedNormal32[idx + 2];
  497. 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];
  498. 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;
  499. 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;
  500. 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;
  501. this._normals32[idx] = this._cam_axisX.x * this._rotated.x + this._cam_axisY.x * this._rotated.y + this._cam_axisZ.x * this._rotated.z;
  502. 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;
  503. 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;
  504. }
  505. if (this._computeParticleColor) {
  506. this._colors32[colidx] = this._particle.color.r;
  507. this._colors32[colidx + 1] = this._particle.color.g;
  508. this._colors32[colidx + 2] = this._particle.color.b;
  509. this._colors32[colidx + 3] = this._particle.color.a;
  510. }
  511. if (this._computeParticleTexture) {
  512. this._uvs32[uvidx] = this._shapeUV[pt * 2] * (this._particle.uvs.z - this._particle.uvs.x) + this._particle.uvs.x;
  513. this._uvs32[uvidx + 1] = this._shapeUV[pt * 2 + 1] * (this._particle.uvs.w - this._particle.uvs.y) + this._particle.uvs.y;
  514. }
  515. }
  516. index = idx + 3;
  517. colorIndex = colidx + 4;
  518. uvIndex = uvidx + 2;
  519. }
  520. if (update) {
  521. if (this._computeParticleColor) {
  522. this.mesh.updateVerticesData(VertexBuffer.ColorKind, this._colors32, false, false);
  523. }
  524. if (this._computeParticleTexture) {
  525. this.mesh.updateVerticesData(VertexBuffer.UVKind, this._uvs32, false, false);
  526. }
  527. this.mesh.updateVerticesData(VertexBuffer.PositionKind, this._positions32, false, false);
  528. if (!this.mesh.areNormalsFrozen) {
  529. if (this._computeParticleVertex || this.billboard) {
  530. // recompute the normals only if the particles can be morphed, update then the normal reference array
  531. VertexData.ComputeNormals(this._positions32, this._indices, this._normals32);
  532. for (var i = 0; i < this._normals32.length; i++) {
  533. this._fixedNormal32[i] = this._normals32[i];
  534. }
  535. }
  536. this.mesh.updateVerticesData(VertexBuffer.NormalKind, this._normals32, false, false);
  537. }
  538. }
  539. this.afterUpdateParticles(start, end, update);
  540. }
  541. private _quaternionRotationYPR(): void {
  542. this._halfroll = this._roll * 0.5;
  543. this._halfpitch = this._pitch * 0.5;
  544. this._halfyaw = this._yaw * 0.5;
  545. this._sinRoll = Math.sin(this._halfroll);
  546. this._cosRoll = Math.cos(this._halfroll);
  547. this._sinPitch = Math.sin(this._halfpitch);
  548. this._cosPitch = Math.cos(this._halfpitch);
  549. this._sinYaw = Math.sin(this._halfyaw);
  550. this._cosYaw = Math.cos(this._halfyaw);
  551. this._quaternion.x = (this._cosYaw * this._sinPitch * this._cosRoll) + (this._sinYaw * this._cosPitch * this._sinRoll);
  552. this._quaternion.y = (this._sinYaw * this._cosPitch * this._cosRoll) - (this._cosYaw * this._sinPitch * this._sinRoll);
  553. this._quaternion.z = (this._cosYaw * this._cosPitch * this._sinRoll) - (this._sinYaw * this._sinPitch * this._cosRoll);
  554. this._quaternion.w = (this._cosYaw * this._cosPitch * this._cosRoll) + (this._sinYaw * this._sinPitch * this._sinRoll);
  555. }
  556. private _quaternionToRotationMatrix(): void {
  557. this._rotMatrix.m[0] = 1.0 - (2.0 * (this._quaternion.y * this._quaternion.y + this._quaternion.z * this._quaternion.z));
  558. this._rotMatrix.m[1] = 2.0 * (this._quaternion.x * this._quaternion.y + this._quaternion.z * this._quaternion.w);
  559. this._rotMatrix.m[2] = 2.0 * (this._quaternion.z * this._quaternion.x - this._quaternion.y * this._quaternion.w);
  560. this._rotMatrix.m[3] = 0;
  561. this._rotMatrix.m[4] = 2.0 * (this._quaternion.x * this._quaternion.y - this._quaternion.z * this._quaternion.w);
  562. this._rotMatrix.m[5] = 1.0 - (2.0 * (this._quaternion.z * this._quaternion.z + this._quaternion.x * this._quaternion.x));
  563. this._rotMatrix.m[6] = 2.0 * (this._quaternion.y * this._quaternion.z + this._quaternion.x * this._quaternion.w);
  564. this._rotMatrix.m[7] = 0;
  565. this._rotMatrix.m[8] = 2.0 * (this._quaternion.z * this._quaternion.x + this._quaternion.y * this._quaternion.w);
  566. this._rotMatrix.m[9] = 2.0 * (this._quaternion.y * this._quaternion.z - this._quaternion.x * this._quaternion.w);
  567. this._rotMatrix.m[10] = 1.0 - (2.0 * (this._quaternion.y * this._quaternion.y + this._quaternion.x * this._quaternion.x));
  568. this._rotMatrix.m[11] = 0;
  569. this._rotMatrix.m[12] = 0;
  570. this._rotMatrix.m[13] = 0;
  571. this._rotMatrix.m[14] = 0;
  572. this._rotMatrix.m[15] = 1.0;
  573. }
  574. /**
  575. * Disposes the SPS
  576. */
  577. public dispose(): void {
  578. this.mesh.dispose();
  579. this.vars = null;
  580. // drop references to internal big arrays for the GC
  581. this._positions = null;
  582. this._indices = null;
  583. this._normals = null;
  584. this._uvs = null;
  585. this._colors = null;
  586. this._positions32 = null;
  587. this._normals32 = null;
  588. this._fixedNormal32 = null;
  589. this._uvs32 = null;
  590. this._colors32 = null;
  591. this.pickedParticles = null;
  592. }
  593. /**
  594. * Visibilty helper : Recomputes the visible size according to the mesh bounding box
  595. * doc : http://doc.babylonjs.com/tutorials/Solid_Particle_System#sps-visibility
  596. */
  597. public refreshVisibleSize(): void {
  598. if (!this._isVisibilityBoxLocked) {
  599. this.mesh.refreshBoundingInfo();
  600. }
  601. }
  602. /** Visibility helper : Sets the size of a visibility box, this sets the underlying mesh bounding box.
  603. * @param size the size (float) of the visibility box
  604. * note : this doesn't lock the SPS mesh bounding box.
  605. * doc : http://doc.babylonjs.com/tutorials/Solid_Particle_System#sps-visibility
  606. */
  607. public setVisibilityBox(size: number): void {
  608. var vis = size / 2;
  609. this.mesh._boundingInfo = new BoundingInfo(new Vector3(-vis, -vis, -vis), new Vector3(vis, vis, vis));
  610. }
  611. // getter and setter
  612. /**
  613. * True if the SPS is set as always visible
  614. */
  615. public get isAlwaysVisible(): boolean {
  616. return this._alwaysVisible;
  617. }
  618. /**
  619. * Sets the SPS as always visible or not
  620. * doc : http://doc.babylonjs.com/tutorials/Solid_Particle_System#sps-visibility
  621. */
  622. public set isAlwaysVisible(val: boolean) {
  623. this._alwaysVisible = val;
  624. this.mesh.alwaysSelectAsActiveMesh = val;
  625. }
  626. /**
  627. * Sets the SPS visibility box as locked or not. This enables/disables the underlying mesh bounding box updates.
  628. * doc : http://doc.babylonjs.com/tutorials/Solid_Particle_System#sps-visibility
  629. */
  630. public set isVisibilityBoxLocked(val: boolean) {
  631. this._isVisibilityBoxLocked = val;
  632. this.mesh.getBoundingInfo().isLocked = val;
  633. }
  634. /**
  635. * True if the SPS visibility box is locked. The underlying mesh bounding box is then not updatable any more.
  636. */
  637. public get isVisibilityBoxLocked(): boolean {
  638. return this._isVisibilityBoxLocked;
  639. }
  640. // Optimizer setters
  641. /**
  642. * Tells to setParticle() to compute the particle rotations or not.
  643. * Default value : true. The SPS is faster when it's set to false.
  644. * Note : the particle rotations aren't stored values, so setting computeParticleRotation to false will prevents the particle to rotate.
  645. */
  646. public set computeParticleRotation(val: boolean) {
  647. this._computeParticleRotation = val;
  648. }
  649. /**
  650. * Tells to setParticle() to compute the particle colors or not.
  651. * Default value : true. The SPS is faster when it's set to false.
  652. * Note : the particle colors are stored values, so setting computeParticleColor to false will keep yet the last colors set.
  653. */
  654. public set computeParticleColor(val: boolean) {
  655. this._computeParticleColor = val;
  656. }
  657. /**
  658. * Tells to setParticle() to compute the particle textures or not.
  659. * Default value : true. The SPS is faster when it's set to false.
  660. * Note : the particle textures are stored values, so setting computeParticleTexture to false will keep yet the last colors set.
  661. */
  662. public set computeParticleTexture(val: boolean) {
  663. this._computeParticleTexture = val;
  664. }
  665. /**
  666. * Tells to setParticle() to call the vertex function for each vertex of each particle, or not.
  667. * Default value : false. The SPS is faster when it's set to false.
  668. * Note : the particle custom vertex positions aren't stored values.
  669. */
  670. public set computeParticleVertex(val: boolean) {
  671. this._computeParticleVertex = val;
  672. }
  673. // getters
  674. public get computeParticleRotation(): boolean {
  675. return this._computeParticleRotation;
  676. }
  677. public get computeParticleColor(): boolean {
  678. return this._computeParticleColor;
  679. }
  680. public get computeParticleTexture(): boolean {
  681. return this._computeParticleTexture;
  682. }
  683. public get computeParticleVertex(): boolean {
  684. return this._computeParticleVertex;
  685. }
  686. // =======================================================================
  687. // Particle behavior logic
  688. // these following methods may be overwritten by the user to fit his needs
  689. /**
  690. * This function does nothing. It may be overwritten to set all the particles first values.
  691. * The SPS doesn't call this function, you may have to call it by your own.
  692. * doc : http://doc.babylonjs.com/tutorials/Solid_Particle_System#particle-management
  693. */
  694. public initParticles(): void {
  695. }
  696. /**
  697. * This function does nothing. It may be overwritten to recycle a particle.
  698. * The SPS doesn't call this function, you may have to call it by your own.
  699. * doc : http://doc.babylonjs.com/tutorials/Solid_Particle_System#particle-management
  700. */
  701. public recycleParticle(particle: SolidParticle): SolidParticle {
  702. return particle;
  703. }
  704. /**
  705. * Updates a particle : this function should be overwritten by the user.
  706. * It is called on each particle by setParticles(). This is the place to code each particle behavior.
  707. * doc : http://doc.babylonjs.com/tutorials/Solid_Particle_System#particle-management
  708. * ex : just set a particle position or velocity and recycle conditions
  709. */
  710. public updateParticle(particle: SolidParticle): SolidParticle {
  711. return particle;
  712. }
  713. /**
  714. * Updates a vertex of a particle : it can be overwritten by the user.
  715. * This will be called on each vertex particle by setParticles() if computeParticleVertex is set to true only.
  716. * @param particle the current particle
  717. * @param vertex the current index of the current particle
  718. * @param pt the index of the current vertex in the particle shape
  719. * doc : http://doc.babylonjs.com/tutorials/Solid_Particle_System#update-each-particle-shape
  720. * ex : just set a vertex particle position
  721. */
  722. public updateParticleVertex(particle: SolidParticle, vertex: Vector3, pt: number): Vector3 {
  723. return vertex;
  724. }
  725. /**
  726. * This will be called before any other treatment by setParticles() and will be passed three parameters.
  727. * This does nothing and may be overwritten by the user.
  728. * @param start the particle index in the particle array where to stop to iterate, same than the value passed to setParticle()
  729. * @param stop the particle index in the particle array where to stop to iterate, same than the value passed to setParticle()
  730. * @param update the boolean update value actually passed to setParticles()
  731. */
  732. public beforeUpdateParticles(start?: number, stop?: number, update?: boolean): void {
  733. }
  734. /**
  735. * This will be called by setParticles() after all the other treatments and just before the actual mesh update.
  736. * This will be passed three parameters.
  737. * This does nothing and may be overwritten by the user.
  738. * @param start the particle index in the particle array where to stop to iterate, same than the value passed to setParticle()
  739. * @param stop the particle index in the particle array where to stop to iterate, same than the value passed to setParticle()
  740. * @param update the boolean update value actually passed to setParticles()
  741. */
  742. public afterUpdateParticles(start?: number, stop?: number, update?: boolean): void {
  743. }
  744. }
  745. }