babylon.solidParticle.ts 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. module BABYLON {
  2. /**
  3. * Represents one particle of a solid particle system.
  4. * @see SolidParticleSystem
  5. */
  6. export class SolidParticle {
  7. /**
  8. * particle global index
  9. */
  10. public idx: number = 0;
  11. /**
  12. * The color of the particle
  13. */
  14. public color: Nullable<Color4> = new Color4(1.0, 1.0, 1.0, 1.0);
  15. /**
  16. * The world space position of the particle.
  17. */
  18. public position: Vector3 = Vector3.Zero();
  19. /**
  20. * The world space rotation of the particle. (Not use if rotationQuaternion is set)
  21. */
  22. public rotation: Vector3 = Vector3.Zero();
  23. /**
  24. * The world space rotation quaternion of the particle.
  25. */
  26. public rotationQuaternion: Nullable<Quaternion>;
  27. /**
  28. * The scaling of the particle.
  29. */
  30. public scaling: Vector3 = Vector3.One();
  31. /**
  32. * The uvs of the particle.
  33. */
  34. public uvs: Vector4 = new Vector4(0.0, 0.0, 1.0, 1.0);
  35. /**
  36. * The current speed of the particle.
  37. */
  38. public velocity: Vector3 = Vector3.Zero();
  39. /**
  40. * The pivot point in the particle local space.
  41. */
  42. public pivot: Vector3 = Vector3.Zero();
  43. /**
  44. * Is the particle active or not ?
  45. */
  46. public alive: boolean = true;
  47. /**
  48. * Is the particle visible or not ?
  49. */
  50. public isVisible: boolean = true;
  51. /**
  52. * Index of this particle in the global "positions" array (Internal use)
  53. */
  54. public _pos: number = 0;
  55. /**
  56. * Index of this particle in the global "indices" array (Internal use)
  57. */
  58. public _ind: number = 0;
  59. /**
  60. * ModelShape of this particle (Internal use)
  61. */
  62. public _model: ModelShape;
  63. /**
  64. * ModelShape id of this particle
  65. */
  66. public shapeId: number = 0;
  67. /**
  68. * Index of the particle in its shape id (Internal use)
  69. */
  70. public idxInShape: number = 0;
  71. /**
  72. * Reference to the shape model BoundingInfo object (Internal use)
  73. */
  74. public _modelBoundingInfo: BoundingInfo;
  75. /**
  76. * Particle BoundingInfo object (Internal use)
  77. */
  78. public _boundingInfo: BoundingInfo;
  79. /**
  80. * Reference to the SPS what the particle belongs to (Internal use)
  81. */
  82. public _sps: SolidParticleSystem;
  83. /**
  84. * Still set as invisible in order to skip useless computations (Internal use)
  85. */
  86. public _stillInvisible: boolean = false;
  87. /**
  88. * Last computed particle rotation matrix
  89. */
  90. public _rotationMatrix: number[] = [1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0];
  91. /**
  92. * Creates a Solid Particle object.
  93. * Don't create particles manually, use instead the Solid Particle System internal tools like _addParticle()
  94. * @param particleIndex (integer) is the particle index in the Solid Particle System pool. It's also the particle identifier.
  95. * @param positionIndex (integer) is the starting index of the particle vertices in the SPS "positions" array.
  96. * @param indiceIndex (integer) is the starting index of the particle indices in the SPS "indices" array.
  97. * @param model (ModelShape) is a reference to the model shape on what the particle is designed.
  98. * @param shapeId (integer) is the model shape identifier in the SPS.
  99. * @param idxInShape (integer) is the index of the particle in the current model (ex: the 10th box of addShape(box, 30))
  100. * @param modelBoundingInfo is the reference to the model BoundingInfo used for intersection computations.
  101. */
  102. constructor(particleIndex: number, positionIndex: number, indiceIndex: number, model: Nullable<ModelShape>, shapeId: number, idxInShape: number, sps: SolidParticleSystem, modelBoundingInfo: Nullable<BoundingInfo> = null) {
  103. this.idx = particleIndex;
  104. this._pos = positionIndex;
  105. this._ind = indiceIndex;
  106. this._model = <ModelShape>model;
  107. this.shapeId = shapeId;
  108. this.idxInShape = idxInShape;
  109. this._sps = sps;
  110. if (modelBoundingInfo) {
  111. this._modelBoundingInfo = modelBoundingInfo;
  112. this._boundingInfo = new BoundingInfo(modelBoundingInfo.minimum, modelBoundingInfo.maximum);
  113. }
  114. }
  115. /**
  116. * Legacy support, changed scale to scaling
  117. */
  118. public get scale(): Vector3 {
  119. return this.scaling;
  120. }
  121. /**
  122. * Legacy support, changed scale to scaling
  123. */
  124. public set scale(scale: Vector3) {
  125. this.scaling = scale;
  126. }
  127. /**
  128. * Legacy support, changed quaternion to rotationQuaternion
  129. */
  130. public get quaternion(): Nullable<Quaternion> {
  131. return this.rotationQuaternion;
  132. }
  133. /**
  134. * Legacy support, changed quaternion to rotationQuaternion
  135. */
  136. public set quaternion(q: Nullable<Quaternion>) {
  137. this.rotationQuaternion = q;
  138. }
  139. /**
  140. * Returns a boolean. True if the particle intersects another particle or another mesh, else false.
  141. * The intersection is computed on the particle bounding sphere and Axis Aligned Bounding Box (AABB)
  142. * @param target is the object (solid particle or mesh) what the intersection is computed against.
  143. * @returns true if it intersects
  144. */
  145. public intersectsMesh(target: Mesh | SolidParticle): boolean {
  146. if (!this._boundingInfo || !target._boundingInfo) {
  147. return false;
  148. }
  149. if (this._sps._bSphereOnly) {
  150. return BoundingSphere.Intersects(this._boundingInfo.boundingSphere, target._boundingInfo.boundingSphere);
  151. }
  152. return this._boundingInfo.intersects(target._boundingInfo, false);
  153. }
  154. }
  155. /**
  156. * Represents the shape of the model used by one particle of a solid particle system.
  157. * SPS internal tool, don't use it manually.
  158. * @see SolidParticleSystem
  159. */
  160. export class ModelShape {
  161. /**
  162. * The shape id.
  163. */
  164. public shapeID: number;
  165. /**
  166. * flat array of model positions (internal use)
  167. */
  168. public _shape: Vector3[];
  169. /**
  170. * flat array of model UVs (internal use)
  171. */
  172. public _shapeUV: number[];
  173. /**
  174. * length of the shape in the model indices array (internal use)
  175. */
  176. public _indicesLength: number = 0;
  177. /**
  178. * Custom position function (internal use)
  179. */
  180. public _positionFunction: Nullable<(particle: SolidParticle, i: number, s: number) => void>;
  181. /**
  182. * Custom vertex function (internal use)
  183. */
  184. public _vertexFunction: Nullable<(particle: SolidParticle, vertex: Vector3, i: number) => void>;
  185. /**
  186. * Creates a ModelShape object. This is an internal simplified reference to a mesh used as for a model to replicate particles from by the SPS.
  187. * SPS internal tool, don't use it manually.
  188. * @ignore
  189. */
  190. constructor(id: number, shape: Vector3[], indicesLength: number, shapeUV: number[],
  191. posFunction: Nullable<(particle: SolidParticle, i: number, s: number) => void>, vtxFunction: Nullable<(particle: SolidParticle, vertex: Vector3, i: number) => void>) {
  192. this.shapeID = id;
  193. this._shape = shape;
  194. this._indicesLength = indicesLength;
  195. this._shapeUV = shapeUV;
  196. this._positionFunction = posFunction;
  197. this._vertexFunction = vtxFunction;
  198. }
  199. }
  200. /**
  201. * Represents a Depth Sorted Particle in the solid particle system.
  202. * @see SolidParticleSystem
  203. */
  204. export class DepthSortedParticle {
  205. /**
  206. * Index of the particle in the "indices" array
  207. */
  208. public ind: number = 0;
  209. /**
  210. * Length of the particle shape in the "indices" array
  211. */
  212. public indicesLength: number = 0;
  213. /**
  214. * Squared distance from the particle to the camera
  215. */
  216. public sqDistance: number = 0.0;
  217. }
  218. }