solidParticle.ts 11 KB

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