babylon.solidParticle.ts 9.0 KB

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