solidParticle.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. import { Nullable } from "../types";
  2. import { Vector3, Matrix, TmpVectors, Quaternion, Vector4 } from "../Maths/math.vector";
  3. import { Color4 } from '../Maths/math.color';
  4. import { Mesh } from "../Meshes/mesh";
  5. import { BoundingInfo } from "../Culling/boundingInfo";
  6. import { BoundingSphere } from "../Culling/boundingSphere";
  7. import { SolidParticleSystem } from "./solidParticleSystem";
  8. import { AbstractMesh } from '../Meshes/abstractMesh';
  9. import { Plane } from '../Maths/math.plane';
  10. /**
  11. * Represents one particle of a solid particle system.
  12. */
  13. export class SolidParticle {
  14. /**
  15. * particle global index
  16. */
  17. public idx: number = 0;
  18. /**
  19. * The color of the particle
  20. */
  21. public color: Nullable<Color4> = new Color4(1.0, 1.0, 1.0, 1.0);
  22. /**
  23. * The world space position of the particle.
  24. */
  25. public position: Vector3 = Vector3.Zero();
  26. /**
  27. * The world space rotation of the particle. (Not use if rotationQuaternion is set)
  28. */
  29. public rotation: Vector3 = Vector3.Zero();
  30. /**
  31. * The world space rotation quaternion of the particle.
  32. */
  33. public rotationQuaternion: Nullable<Quaternion>;
  34. /**
  35. * The scaling of the particle.
  36. */
  37. public scaling: Vector3 = Vector3.One();
  38. /**
  39. * The uvs of the particle.
  40. */
  41. public uvs: Vector4 = new Vector4(0.0, 0.0, 1.0, 1.0);
  42. /**
  43. * The current speed of the particle.
  44. */
  45. public velocity: Vector3 = Vector3.Zero();
  46. /**
  47. * The pivot point in the particle local space.
  48. */
  49. public pivot: Vector3 = Vector3.Zero();
  50. /**
  51. * Must the particle be translated from its pivot point in its local space ?
  52. * In this case, the pivot point is set at the origin of the particle local space and the particle is translated.
  53. * Default : false
  54. */
  55. public translateFromPivot: boolean = false;
  56. /**
  57. * Is the particle active or not ?
  58. */
  59. public alive: boolean = true;
  60. /**
  61. * Is the particle visible or not ?
  62. */
  63. public isVisible: boolean = true;
  64. /**
  65. * Index of this particle in the global "positions" array (Internal use)
  66. * @hidden
  67. */
  68. public _pos: number = 0;
  69. /**
  70. * @hidden Index of this particle in the global "indices" array (Internal use)
  71. */
  72. public _ind: number = 0;
  73. /**
  74. * @hidden ModelShape of this particle (Internal use)
  75. */
  76. public _model: ModelShape;
  77. /**
  78. * ModelShape id of this particle
  79. */
  80. public shapeId: number = 0;
  81. /**
  82. * Index of the particle in its shape id
  83. */
  84. public idxInShape: number = 0;
  85. /**
  86. * @hidden Reference to the shape model BoundingInfo object (Internal use)
  87. */
  88. public _modelBoundingInfo: BoundingInfo;
  89. /**
  90. * @hidden Particle BoundingInfo object (Internal use)
  91. */
  92. public _boundingInfo: BoundingInfo;
  93. /**
  94. * @hidden Reference to the SPS what the particle belongs to (Internal use)
  95. */
  96. public _sps: SolidParticleSystem;
  97. /**
  98. * @hidden Still set as invisible in order to skip useless computations (Internal use)
  99. */
  100. public _stillInvisible: boolean = false;
  101. /**
  102. * @hidden Last computed particle rotation matrix
  103. */
  104. public _rotationMatrix: number[] = [1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0];
  105. /**
  106. * Parent particle Id, if any.
  107. * Default null.
  108. */
  109. public parentId: Nullable<number> = null;
  110. /**
  111. * The culling strategy to use to check whether the solid particle must be culled or not when using isInFrustum().
  112. * The possible values are :
  113. * - AbstractMesh.CULLINGSTRATEGY_STANDARD
  114. * - AbstractMesh.CULLINGSTRATEGY_BOUNDINGSPHERE_ONLY
  115. * - AbstractMesh.CULLINGSTRATEGY_OPTIMISTIC_INCLUSION
  116. * - AbstractMesh.CULLINGSTRATEGY_OPTIMISTIC_INCLUSION_THEN_BSPHERE_ONLY
  117. * The default value for solid particles is AbstractMesh.CULLINGSTRATEGY_BOUNDINGSPHERE_ONLY
  118. * Please read each static variable documentation in the class AbstractMesh to get details about the culling process.
  119. * */
  120. public cullingStrategy = AbstractMesh.CULLINGSTRATEGY_BOUNDINGSPHERE_ONLY;
  121. /**
  122. * @hidden Internal global position in the SPS.
  123. */
  124. public _globalPosition: Vector3 = Vector3.Zero();
  125. /**
  126. * Creates a Solid Particle object.
  127. * Don't create particles manually, use instead the Solid Particle System internal tools like _addParticle()
  128. * @param particleIndex (integer) is the particle index in the Solid Particle System pool. It's also the particle identifier.
  129. * @param positionIndex (integer) is the starting index of the particle vertices in the SPS "positions" array.
  130. * @param indiceIndex (integer) is the starting index of the particle indices in the SPS "indices" array.
  131. * @param model (ModelShape) is a reference to the model shape on what the particle is designed.
  132. * @param shapeId (integer) is the model shape identifier in the SPS.
  133. * @param idxInShape (integer) is the index of the particle in the current model (ex: the 10th box of addShape(box, 30))
  134. * @param sps defines the sps it is associated to
  135. * @param modelBoundingInfo is the reference to the model BoundingInfo used for intersection computations.
  136. */
  137. constructor(particleIndex: number, positionIndex: number, indiceIndex: number, model: Nullable<ModelShape>, shapeId: number, idxInShape: number, sps: SolidParticleSystem, modelBoundingInfo: Nullable<BoundingInfo> = null) {
  138. this.idx = particleIndex;
  139. this._pos = positionIndex;
  140. this._ind = indiceIndex;
  141. this._model = <ModelShape>model;
  142. this.shapeId = shapeId;
  143. this.idxInShape = idxInShape;
  144. this._sps = sps;
  145. if (modelBoundingInfo) {
  146. this._modelBoundingInfo = modelBoundingInfo;
  147. this._boundingInfo = new BoundingInfo(modelBoundingInfo.minimum, modelBoundingInfo.maximum);
  148. }
  149. }
  150. /**
  151. * Legacy support, changed scale to scaling
  152. */
  153. public get scale(): Vector3 {
  154. return this.scaling;
  155. }
  156. /**
  157. * Legacy support, changed scale to scaling
  158. */
  159. public set scale(scale: Vector3) {
  160. this.scaling = scale;
  161. }
  162. /**
  163. * Legacy support, changed quaternion to rotationQuaternion
  164. */
  165. public get quaternion(): Nullable<Quaternion> {
  166. return this.rotationQuaternion;
  167. }
  168. /**
  169. * Legacy support, changed quaternion to rotationQuaternion
  170. */
  171. public set quaternion(q: Nullable<Quaternion>) {
  172. this.rotationQuaternion = q;
  173. }
  174. /**
  175. * Returns a boolean. True if the particle intersects another particle or another mesh, else false.
  176. * The intersection is computed on the particle bounding sphere and Axis Aligned Bounding Box (AABB)
  177. * @param target is the object (solid particle or mesh) what the intersection is computed against.
  178. * @returns true if it intersects
  179. */
  180. public intersectsMesh(target: Mesh | SolidParticle): boolean {
  181. if (!this._boundingInfo || !target._boundingInfo) {
  182. return false;
  183. }
  184. if (this._sps._bSphereOnly) {
  185. return BoundingSphere.Intersects(this._boundingInfo.boundingSphere, target._boundingInfo.boundingSphere);
  186. }
  187. return this._boundingInfo.intersects(target._boundingInfo, false);
  188. }
  189. /**
  190. * Returns `true` if the solid particle is within the frustum defined by the passed array of planes.
  191. * A particle is in the frustum if its bounding box intersects the frustum
  192. * @param frustumPlanes defines the frustum to test
  193. * @returns true if the particle is in the frustum planes
  194. */
  195. public isInFrustum(frustumPlanes: Plane[]): boolean {
  196. return this._boundingInfo !== null && this._boundingInfo.isInFrustum(frustumPlanes, this.cullingStrategy);
  197. }
  198. /**
  199. * get the rotation matrix of the particle
  200. * @hidden
  201. */
  202. public getRotationMatrix(m: Matrix) {
  203. let quaternion: Quaternion;
  204. if (this.rotationQuaternion) {
  205. quaternion = this.rotationQuaternion;
  206. }
  207. else {
  208. quaternion = TmpVectors.Quaternion[0];
  209. const rotation = this.rotation;
  210. Quaternion.RotationYawPitchRollToRef(rotation.y, rotation.x, rotation.z, quaternion);
  211. }
  212. quaternion.toRotationMatrix(m);
  213. }
  214. }
  215. /**
  216. * Represents the shape of the model used by one particle of a solid particle system.
  217. * SPS internal tool, don't use it manually.
  218. */
  219. export class ModelShape {
  220. /**
  221. * The shape id
  222. * @hidden
  223. */
  224. public shapeID: number;
  225. /**
  226. * flat array of model positions (internal use)
  227. * @hidden
  228. */
  229. public _shape: Vector3[];
  230. /**
  231. * flat array of model UVs (internal use)
  232. * @hidden
  233. */
  234. public _shapeUV: number[];
  235. /**
  236. * length of the shape in the model indices array (internal use)
  237. * @hidden
  238. */
  239. public _indicesLength: number = 0;
  240. /**
  241. * Custom position function (internal use)
  242. * @hidden
  243. */
  244. public _positionFunction: Nullable<(particle: SolidParticle, i: number, s: number) => void>;
  245. /**
  246. * Custom vertex function (internal use)
  247. * @hidden
  248. */
  249. public _vertexFunction: Nullable<(particle: SolidParticle, vertex: Vector3, i: number) => void>;
  250. /**
  251. * 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.
  252. * SPS internal tool, don't use it manually.
  253. * @hidden
  254. */
  255. constructor(id: number, shape: Vector3[], indicesLength: number, shapeUV: number[],
  256. posFunction: Nullable<(particle: SolidParticle, i: number, s: number) => void>, vtxFunction: Nullable<(particle: SolidParticle, vertex: Vector3, i: number) => void>) {
  257. this.shapeID = id;
  258. this._shape = shape;
  259. this._indicesLength = indicesLength;
  260. this._shapeUV = shapeUV;
  261. this._positionFunction = posFunction;
  262. this._vertexFunction = vtxFunction;
  263. }
  264. }
  265. /**
  266. * Represents a Depth Sorted Particle in the solid particle system.
  267. */
  268. export class DepthSortedParticle {
  269. /**
  270. * Index of the particle in the "indices" array
  271. */
  272. public ind: number = 0;
  273. /**
  274. * Length of the particle shape in the "indices" array
  275. */
  276. public indicesLength: number = 0;
  277. /**
  278. * Squared distance from the particle to the camera
  279. */
  280. public sqDistance: number = 0.0;
  281. }