babylon.solidParticle.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. * @hidden
  59. */
  60. public _pos: number = 0;
  61. /**
  62. * @hidden Index of this particle in the global "indices" array (Internal use)
  63. */
  64. public _ind: number = 0;
  65. /**
  66. * @hidden 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. * @hidden Reference to the shape model BoundingInfo object (Internal use)
  79. */
  80. public _modelBoundingInfo: BoundingInfo;
  81. /**
  82. * @hidden Particle BoundingInfo object (Internal use)
  83. */
  84. public _boundingInfo: BoundingInfo;
  85. /**
  86. * @hidden Reference to the SPS what the particle belongs to (Internal use)
  87. */
  88. public _sps: SolidParticleSystem;
  89. /**
  90. * @hidden Still set as invisible in order to skip useless computations (Internal use)
  91. */
  92. public _stillInvisible: boolean = false;
  93. /**
  94. * @hidden 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. * The culling strategy to use to check whether the solid particle must be culled or not when using isInFrustum().
  104. * The possible values are :
  105. * - AbstractMesh.CULLINGSTRATEGY_STANDARD
  106. * - AbstractMesh.CULLINGSTRATEGY_BOUNDINGSPHERE_ONLY
  107. * - AbstractMesh.CULLINGSTRATEGY_OPTIMISTIC_INCLUSION
  108. * - AbstractMesh.CULLINGSTRATEGY_OPTIMISTIC_INCLUSION_THEN_BSPHERE_ONLY
  109. * The default value for solid particles is AbstractMesh.CULLINGSTRATEGY_BOUNDINGSPHERE_ONLY
  110. * Please read each static variable documentation in the class AbstractMesh to get details about the culling process.
  111. * */
  112. public cullingStrategy = AbstractMesh.CULLINGSTRATEGY_BOUNDINGSPHERE_ONLY;
  113. /**
  114. * @hidden Internal global position in the SPS.
  115. */
  116. public _globalPosition: Vector3 = Vector3.Zero();
  117. /**
  118. * Creates a Solid Particle object.
  119. * Don't create particles manually, use instead the Solid Particle System internal tools like _addParticle()
  120. * @param particleIndex (integer) is the particle index in the Solid Particle System pool. It's also the particle identifier.
  121. * @param positionIndex (integer) is the starting index of the particle vertices in the SPS "positions" array.
  122. * @param indiceIndex (integer) is the starting index of the particle indices in the SPS "indices" array.
  123. * @param model (ModelShape) is a reference to the model shape on what the particle is designed.
  124. * @param shapeId (integer) is the model shape identifier in the SPS.
  125. * @param idxInShape (integer) is the index of the particle in the current model (ex: the 10th box of addShape(box, 30))
  126. * @param modelBoundingInfo is the reference to the model BoundingInfo used for intersection computations.
  127. */
  128. constructor(particleIndex: number, positionIndex: number, indiceIndex: number, model: Nullable<ModelShape>, shapeId: number, idxInShape: number, sps: SolidParticleSystem, modelBoundingInfo: Nullable<BoundingInfo> = null) {
  129. this.idx = particleIndex;
  130. this._pos = positionIndex;
  131. this._ind = indiceIndex;
  132. this._model = <ModelShape>model;
  133. this.shapeId = shapeId;
  134. this.idxInShape = idxInShape;
  135. this._sps = sps;
  136. if (modelBoundingInfo) {
  137. this._modelBoundingInfo = modelBoundingInfo;
  138. this._boundingInfo = new BoundingInfo(modelBoundingInfo.minimum, modelBoundingInfo.maximum);
  139. }
  140. }
  141. /**
  142. * Legacy support, changed scale to scaling
  143. */
  144. public get scale(): Vector3 {
  145. return this.scaling;
  146. }
  147. /**
  148. * Legacy support, changed scale to scaling
  149. */
  150. public set scale(scale: Vector3) {
  151. this.scaling = scale;
  152. }
  153. /**
  154. * Legacy support, changed quaternion to rotationQuaternion
  155. */
  156. public get quaternion(): Nullable<Quaternion> {
  157. return this.rotationQuaternion;
  158. }
  159. /**
  160. * Legacy support, changed quaternion to rotationQuaternion
  161. */
  162. public set quaternion(q: Nullable<Quaternion>) {
  163. this.rotationQuaternion = q;
  164. }
  165. /**
  166. * Returns a boolean. True if the particle intersects another particle or another mesh, else false.
  167. * The intersection is computed on the particle bounding sphere and Axis Aligned Bounding Box (AABB)
  168. * @param target is the object (solid particle or mesh) what the intersection is computed against.
  169. * @returns true if it intersects
  170. */
  171. public intersectsMesh(target: Mesh | SolidParticle): boolean {
  172. if (!this._boundingInfo || !target._boundingInfo) {
  173. return false;
  174. }
  175. if (this._sps._bSphereOnly) {
  176. return BoundingSphere.Intersects(this._boundingInfo.boundingSphere, target._boundingInfo.boundingSphere);
  177. }
  178. return this._boundingInfo.intersects(target._boundingInfo, false);
  179. }
  180. /**
  181. * Returns `true` if the solid particle is within the frustum defined by the passed array of planes.
  182. * A particle is in the frustum if its bounding box intersects the frustum
  183. * @param frustumPlanes defines the frustum to test
  184. * @returns true if the particle is in the frustum planes
  185. */
  186. public isInFrustum(frustumPlanes: Plane[]): boolean {
  187. return this._boundingInfo !== null && this._boundingInfo.isInFrustum(frustumPlanes, this.cullingStrategy);
  188. }
  189. /**
  190. * get the rotation matrix of the particle
  191. * @hidden
  192. */
  193. public getRotationMatrix(m : Matrix) {
  194. let quaternion: Quaternion;
  195. if (this.rotationQuaternion) {
  196. quaternion = this.rotationQuaternion;
  197. }
  198. else {
  199. quaternion = Tmp.Quaternion[0];
  200. const rotation = this.rotation;
  201. Quaternion.RotationYawPitchRollToRef(rotation.y, rotation.x, rotation.z, quaternion);
  202. }
  203. quaternion.toRotationMatrix(m);
  204. }
  205. }
  206. /**
  207. * Represents the shape of the model used by one particle of a solid particle system.
  208. * SPS internal tool, don't use it manually.
  209. */
  210. export class ModelShape {
  211. /**
  212. * The shape id
  213. * @hidden
  214. */
  215. public shapeID: number;
  216. /**
  217. * flat array of model positions (internal use)
  218. * @hidden
  219. */
  220. public _shape: Vector3[];
  221. /**
  222. * flat array of model UVs (internal use)
  223. * @hidden
  224. */
  225. public _shapeUV: number[];
  226. /**
  227. * length of the shape in the model indices array (internal use)
  228. * @hidden
  229. */
  230. public _indicesLength: number = 0;
  231. /**
  232. * Custom position function (internal use)
  233. * @hidden
  234. */
  235. public _positionFunction: Nullable<(particle: SolidParticle, i: number, s: number) => void>;
  236. /**
  237. * Custom vertex function (internal use)
  238. * @hidden
  239. */
  240. public _vertexFunction: Nullable<(particle: SolidParticle, vertex: Vector3, i: number) => void>;
  241. /**
  242. * 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.
  243. * SPS internal tool, don't use it manually.
  244. * @hidden
  245. */
  246. constructor(id: number, shape: Vector3[], indicesLength: number, shapeUV: number[],
  247. posFunction: Nullable<(particle: SolidParticle, i: number, s: number) => void>, vtxFunction: Nullable<(particle: SolidParticle, vertex: Vector3, i: number) => void>) {
  248. this.shapeID = id;
  249. this._shape = shape;
  250. this._indicesLength = indicesLength;
  251. this._shapeUV = shapeUV;
  252. this._positionFunction = posFunction;
  253. this._vertexFunction = vtxFunction;
  254. }
  255. }
  256. /**
  257. * Represents a Depth Sorted Particle in the solid particle system.
  258. */
  259. export class DepthSortedParticle {
  260. /**
  261. * Index of the particle in the "indices" array
  262. */
  263. public ind: number = 0;
  264. /**
  265. * Length of the particle shape in the "indices" array
  266. */
  267. public indicesLength: number = 0;
  268. /**
  269. * Squared distance from the particle to the camera
  270. */
  271. public sqDistance: number = 0.0;
  272. }
  273. }