babylon.solidParticle.ts 9.1 KB

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