babylon.solidParticle.ts 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. * Creates a Solid Particle object.
  89. * Don't create particles manually, use instead the Solid Particle System internal tools like _addParticle()
  90. * @param particleIndex (integer) is the particle index in the Solid Particle System pool. It's also the particle identifier.
  91. * @param positionIndex (integer) is the starting index of the particle vertices in the SPS "positions" array.
  92. * @param indiceIndex (integer) is the starting index of the particle indices in the SPS "indices" array.
  93. * @param model (ModelShape) is a reference to the model shape on what the particle is designed.
  94. * @param shapeId (integer) is the model shape identifier in the SPS.
  95. * @param idxInShape (integer) is the index of the particle in the current model (ex: the 10th box of addShape(box, 30))
  96. * @param modelBoundingInfo is the reference to the model BoundingInfo used for intersection computations.
  97. */
  98. constructor(particleIndex: number, positionIndex: number, indiceIndex: number, model: Nullable<ModelShape>, shapeId: number, idxInShape: number, sps: SolidParticleSystem, modelBoundingInfo: Nullable<BoundingInfo> = null) {
  99. this.idx = particleIndex;
  100. this._pos = positionIndex;
  101. this._ind = indiceIndex;
  102. this._model = <ModelShape>model;
  103. this.shapeId = shapeId;
  104. this.idxInShape = idxInShape;
  105. this._sps = sps;
  106. if (modelBoundingInfo) {
  107. this._modelBoundingInfo = modelBoundingInfo;
  108. this._boundingInfo = new BoundingInfo(modelBoundingInfo.minimum, modelBoundingInfo.maximum);
  109. }
  110. }
  111. /**
  112. * Legacy support, changed scale to scaling
  113. */
  114. public get scale(): Vector3 {
  115. return this.scaling;
  116. }
  117. /**
  118. * Legacy support, changed scale to scaling
  119. */
  120. public set scale(scale: Vector3) {
  121. this.scaling = scale;
  122. }
  123. /**
  124. * Legacy support, changed quaternion to rotationQuaternion
  125. */
  126. public get quaternion(): Nullable<Quaternion> {
  127. return this.rotationQuaternion;
  128. }
  129. /**
  130. * Legacy support, changed quaternion to rotationQuaternion
  131. */
  132. public set quaternion(q: Nullable<Quaternion>) {
  133. this.rotationQuaternion = q;
  134. }
  135. /**
  136. * Returns a boolean. True if the particle intersects another particle or another mesh, else false.
  137. * The intersection is computed on the particle bounding sphere and Axis Aligned Bounding Box (AABB)
  138. * @param target is the object (solid particle or mesh) what the intersection is computed against.
  139. * @returns true if it intersects
  140. */
  141. public intersectsMesh(target: Mesh | SolidParticle): boolean {
  142. if (!this._boundingInfo || !target._boundingInfo) {
  143. return false;
  144. }
  145. if (this._sps._bSphereOnly) {
  146. return BoundingSphere.Intersects(this._boundingInfo.boundingSphere, target._boundingInfo.boundingSphere);
  147. }
  148. return this._boundingInfo.intersects(target._boundingInfo, false);
  149. }
  150. }
  151. /**
  152. * Represents the shape of the model used by one particle of a solid particle system.
  153. * SPS internal tool, don't use it manually.
  154. * @see SolidParticleSystem
  155. */
  156. export class ModelShape {
  157. /**
  158. * The shape id.
  159. */
  160. public shapeID: number;
  161. /**
  162. * flat array of model positions (internal use)
  163. */
  164. public _shape: Vector3[];
  165. /**
  166. * flat array of model UVs (internal use)
  167. */
  168. public _shapeUV: number[];
  169. /**
  170. * length of the shape in the model indices array (internal use)
  171. */
  172. public _indicesLength: number = 0;
  173. /**
  174. * Custom position function (internal use)
  175. */
  176. public _positionFunction: Nullable<(particle: SolidParticle, i: number, s: number) => void>;
  177. /**
  178. * Custom vertex function (internal use)
  179. */
  180. public _vertexFunction: Nullable<(particle: SolidParticle, vertex: Vector3, i: number) => void>;
  181. /**
  182. * 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.
  183. * SPS internal tool, don't use it manually.
  184. * @ignore
  185. */
  186. constructor(id: number, shape: Vector3[], indicesLength: number, shapeUV: number[],
  187. posFunction: Nullable<(particle: SolidParticle, i: number, s: number) => void>, vtxFunction: Nullable<(particle: SolidParticle, vertex: Vector3, i: number) => void>) {
  188. this.shapeID = id;
  189. this._shape = shape;
  190. this._indicesLength = indicesLength;
  191. this._shapeUV = shapeUV;
  192. this._positionFunction = posFunction;
  193. this._vertexFunction = vtxFunction;
  194. }
  195. }
  196. /**
  197. * Represents a Depth Sorted Particle in the solid particle system.
  198. * @see SolidParticleSystem
  199. */
  200. export class DepthSortedParticle {
  201. /**
  202. * Index of the particle in the "indices" array
  203. */
  204. public ind: number = 0;
  205. /**
  206. * Length of the particle shape in the "indices" array
  207. */
  208. public indicesLength: number = 0;
  209. /**
  210. * Squared distance from the particle to the camera
  211. */
  212. public sqDistance: number = 0.0;
  213. }
  214. }