solidParticle.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. import { Vector3, TmpVectors, Quaternion, Vector4, Vector2 } from "../Maths/math.vector.js";
  2. import { Color4 } from "../Maths/math.color.js";
  3. import { BoundingInfo } from "../Culling/boundingInfo.js";
  4. import { BoundingSphere } from "../Culling/boundingSphere.js";
  5. import { AbstractMesh } from "../Meshes/abstractMesh.js";
  6. /**
  7. * Represents one particle of a solid particle system.
  8. */
  9. export class SolidParticle {
  10. /**
  11. * Particle BoundingInfo object
  12. * @returns a BoundingInfo
  13. */
  14. getBoundingInfo() {
  15. return this._boundingInfo;
  16. }
  17. /**
  18. * Returns true if there is already a bounding info
  19. */
  20. get hasBoundingInfo() {
  21. return this._boundingInfo !== null;
  22. }
  23. /**
  24. * Creates a Solid Particle object.
  25. * Don't create particles manually, use instead the Solid Particle System internal tools like _addParticle()
  26. * @param particleIndex (integer) is the particle index in the Solid Particle System pool.
  27. * @param particleId (integer) is the particle identifier. Unless some particles are removed from the SPS, it's the same value than the particle idx.
  28. * @param positionIndex (integer) is the starting index of the particle vertices in the SPS "positions" array.
  29. * @param indiceIndex (integer) is the starting index of the particle indices in the SPS "indices" array.
  30. * @param model (ModelShape) is a reference to the model shape on what the particle is designed.
  31. * @param shapeId (integer) is the model shape identifier in the SPS.
  32. * @param idxInShape (integer) is the index of the particle in the current model (ex: the 10th box of addShape(box, 30))
  33. * @param sps defines the sps it is associated to
  34. * @param modelBoundingInfo is the reference to the model BoundingInfo used for intersection computations.
  35. * @param materialIndex is the particle material identifier (integer) when the MultiMaterials are enabled in the SPS.
  36. */
  37. constructor(particleIndex, particleId, positionIndex, indiceIndex, model, shapeId, idxInShape, sps, modelBoundingInfo = null, materialIndex = null) {
  38. /**
  39. * particle global index
  40. */
  41. this.idx = 0;
  42. /**
  43. * particle identifier
  44. */
  45. this.id = 0;
  46. /**
  47. * The color of the particle
  48. */
  49. this.color = new Color4(1.0, 1.0, 1.0, 1.0);
  50. /**
  51. * The world space position of the particle.
  52. */
  53. this.position = Vector3.Zero();
  54. /**
  55. * The world space rotation of the particle. (Not use if rotationQuaternion is set)
  56. */
  57. this.rotation = Vector3.Zero();
  58. /**
  59. * The scaling of the particle.
  60. */
  61. this.scaling = Vector3.One();
  62. /**
  63. * The uvs of the particle.
  64. */
  65. this.uvs = new Vector4(0.0, 0.0, 1.0, 1.0);
  66. /**
  67. * The current speed of the particle.
  68. */
  69. this.velocity = Vector3.Zero();
  70. /**
  71. * The pivot point in the particle local space.
  72. */
  73. this.pivot = Vector3.Zero();
  74. /**
  75. * Must the particle be translated from its pivot point in its local space ?
  76. * In this case, the pivot point is set at the origin of the particle local space and the particle is translated.
  77. * Default : false
  78. */
  79. this.translateFromPivot = false;
  80. /**
  81. * Is the particle active or not ?
  82. */
  83. this.alive = true;
  84. /**
  85. * Is the particle visible or not ?
  86. */
  87. this.isVisible = true;
  88. /**
  89. * Index of this particle in the global "positions" array (Internal use)
  90. * @internal
  91. */
  92. this._pos = 0;
  93. /**
  94. * @internal Index of this particle in the global "indices" array (Internal use)
  95. */
  96. this._ind = 0;
  97. /**
  98. * ModelShape id of this particle
  99. */
  100. this.shapeId = 0;
  101. /**
  102. * Index of the particle in its shape id
  103. */
  104. this.idxInShape = 0;
  105. /**
  106. * @internal Still set as invisible in order to skip useless computations (Internal use)
  107. */
  108. this._stillInvisible = false;
  109. /**
  110. * @internal Last computed particle rotation matrix
  111. */
  112. this._rotationMatrix = [1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0];
  113. /**
  114. * Parent particle Id, if any.
  115. * Default null.
  116. */
  117. this.parentId = null;
  118. /**
  119. * The particle material identifier (integer) when MultiMaterials are enabled in the SPS.
  120. */
  121. this.materialIndex = null;
  122. /**
  123. * Custom object or properties.
  124. */
  125. this.props = null;
  126. /**
  127. * The culling strategy to use to check whether the solid particle must be culled or not when using isInFrustum().
  128. * The possible values are :
  129. * - AbstractMesh.CULLINGSTRATEGY_STANDARD
  130. * - AbstractMesh.CULLINGSTRATEGY_BOUNDINGSPHERE_ONLY
  131. * - AbstractMesh.CULLINGSTRATEGY_OPTIMISTIC_INCLUSION
  132. * - AbstractMesh.CULLINGSTRATEGY_OPTIMISTIC_INCLUSION_THEN_BSPHERE_ONLY
  133. * The default value for solid particles is AbstractMesh.CULLINGSTRATEGY_BOUNDINGSPHERE_ONLY
  134. * Please read each static variable documentation in the class AbstractMesh to get details about the culling process.
  135. * */
  136. this.cullingStrategy = AbstractMesh.CULLINGSTRATEGY_BOUNDINGSPHERE_ONLY;
  137. /**
  138. * @internal Internal global position in the SPS.
  139. */
  140. this._globalPosition = Vector3.Zero();
  141. this.idx = particleIndex;
  142. this.id = particleId;
  143. this._pos = positionIndex;
  144. this._ind = indiceIndex;
  145. this._model = model;
  146. this.shapeId = shapeId;
  147. this.idxInShape = idxInShape;
  148. this._sps = sps;
  149. if (modelBoundingInfo) {
  150. this._modelBoundingInfo = modelBoundingInfo;
  151. this._boundingInfo = new BoundingInfo(modelBoundingInfo.minimum, modelBoundingInfo.maximum);
  152. }
  153. if (materialIndex !== null) {
  154. this.materialIndex = materialIndex;
  155. }
  156. }
  157. /**
  158. * Copies the particle property values into the existing target : position, rotation, scaling, uvs, colors, pivot, parent, visibility, alive
  159. * @param target the particle target
  160. * @returns the current particle
  161. */
  162. copyToRef(target) {
  163. target.position.copyFrom(this.position);
  164. target.rotation.copyFrom(this.rotation);
  165. if (this.rotationQuaternion) {
  166. if (target.rotationQuaternion) {
  167. target.rotationQuaternion.copyFrom(this.rotationQuaternion);
  168. }
  169. else {
  170. target.rotationQuaternion = this.rotationQuaternion.clone();
  171. }
  172. }
  173. target.scaling.copyFrom(this.scaling);
  174. if (this.color) {
  175. if (target.color) {
  176. target.color.copyFrom(this.color);
  177. }
  178. else {
  179. target.color = this.color.clone();
  180. }
  181. }
  182. target.uvs.copyFrom(this.uvs);
  183. target.velocity.copyFrom(this.velocity);
  184. target.pivot.copyFrom(this.pivot);
  185. target.translateFromPivot = this.translateFromPivot;
  186. target.alive = this.alive;
  187. target.isVisible = this.isVisible;
  188. target.parentId = this.parentId;
  189. target.cullingStrategy = this.cullingStrategy;
  190. if (this.materialIndex !== null) {
  191. target.materialIndex = this.materialIndex;
  192. }
  193. return this;
  194. }
  195. /**
  196. * Legacy support, changed scale to scaling
  197. */
  198. get scale() {
  199. return this.scaling;
  200. }
  201. /**
  202. * Legacy support, changed scale to scaling
  203. */
  204. set scale(scale) {
  205. this.scaling = scale;
  206. }
  207. /**
  208. * Legacy support, changed quaternion to rotationQuaternion
  209. */
  210. get quaternion() {
  211. return this.rotationQuaternion;
  212. }
  213. /**
  214. * Legacy support, changed quaternion to rotationQuaternion
  215. */
  216. set quaternion(q) {
  217. this.rotationQuaternion = q;
  218. }
  219. /**
  220. * Returns a boolean. True if the particle intersects another particle or another mesh, else false.
  221. * The intersection is computed on the particle bounding sphere and Axis Aligned Bounding Box (AABB)
  222. * @param target is the object (solid particle or mesh) what the intersection is computed against.
  223. * @returns true if it intersects
  224. */
  225. intersectsMesh(target) {
  226. if (!this._boundingInfo || !target.hasBoundingInfo) {
  227. return false;
  228. }
  229. if (this._sps._bSphereOnly) {
  230. return BoundingSphere.Intersects(this._boundingInfo.boundingSphere, target.getBoundingInfo().boundingSphere);
  231. }
  232. return this._boundingInfo.intersects(target.getBoundingInfo(), false);
  233. }
  234. /**
  235. * Returns `true` if the solid particle is within the frustum defined by the passed array of planes.
  236. * A particle is in the frustum if its bounding box intersects the frustum
  237. * @param frustumPlanes defines the frustum to test
  238. * @returns true if the particle is in the frustum planes
  239. */
  240. isInFrustum(frustumPlanes) {
  241. return this._boundingInfo !== null && this._boundingInfo.isInFrustum(frustumPlanes, this.cullingStrategy);
  242. }
  243. /**
  244. * get the rotation matrix of the particle
  245. * @internal
  246. */
  247. getRotationMatrix(m) {
  248. let quaternion;
  249. if (this.rotationQuaternion) {
  250. quaternion = this.rotationQuaternion;
  251. }
  252. else {
  253. quaternion = TmpVectors.Quaternion[0];
  254. const rotation = this.rotation;
  255. Quaternion.RotationYawPitchRollToRef(rotation.y, rotation.x, rotation.z, quaternion);
  256. }
  257. quaternion.toRotationMatrix(m);
  258. }
  259. }
  260. /**
  261. * Represents the shape of the model used by one particle of a solid particle system.
  262. * SPS internal tool, don't use it manually.
  263. */
  264. export class ModelShape {
  265. /**
  266. * Get or set the shapeId
  267. * @deprecated Please use shapeId instead
  268. */
  269. get shapeID() {
  270. return this.shapeId;
  271. }
  272. set shapeID(shapeID) {
  273. this.shapeId = shapeID;
  274. }
  275. /**
  276. * 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.
  277. * SPS internal tool, don't use it manually.
  278. * @internal
  279. */
  280. constructor(id, shape, indices, normals, colors, shapeUV, posFunction, vtxFunction, material) {
  281. /**
  282. * length of the shape in the model indices array (internal use)
  283. * @internal
  284. */
  285. this._indicesLength = 0;
  286. this.shapeId = id;
  287. this._shape = shape;
  288. this._indices = indices;
  289. this._indicesLength = indices.length;
  290. this._shapeUV = shapeUV;
  291. this._shapeColors = colors;
  292. this._normals = normals;
  293. this._positionFunction = posFunction;
  294. this._vertexFunction = vtxFunction;
  295. this._material = material;
  296. }
  297. }
  298. /**
  299. * Represents a Depth Sorted Particle in the solid particle system.
  300. * @internal
  301. */
  302. export class DepthSortedParticle {
  303. /**
  304. * Creates a new sorted particle
  305. * @param idx
  306. * @param ind
  307. * @param indLength
  308. * @param materialIndex
  309. */
  310. constructor(idx, ind, indLength, materialIndex) {
  311. /**
  312. * Particle index
  313. */
  314. this.idx = 0;
  315. /**
  316. * Index of the particle in the "indices" array
  317. */
  318. this.ind = 0;
  319. /**
  320. * Length of the particle shape in the "indices" array
  321. */
  322. this.indicesLength = 0;
  323. /**
  324. * Squared distance from the particle to the camera
  325. */
  326. this.sqDistance = 0.0;
  327. /**
  328. * Material index when used with MultiMaterials
  329. */
  330. this.materialIndex = 0;
  331. this.idx = idx;
  332. this.ind = ind;
  333. this.indicesLength = indLength;
  334. this.materialIndex = materialIndex;
  335. }
  336. }
  337. /**
  338. * Represents a solid particle vertex
  339. */
  340. export class SolidParticleVertex {
  341. /**
  342. * Creates a new solid particle vertex
  343. */
  344. constructor() {
  345. this.position = Vector3.Zero();
  346. this.color = new Color4(1.0, 1.0, 1.0, 1.0);
  347. this.uv = Vector2.Zero();
  348. }
  349. // Getters and Setters for back-compatibility
  350. /** Vertex x coordinate */
  351. get x() {
  352. return this.position.x;
  353. }
  354. set x(val) {
  355. this.position.x = val;
  356. }
  357. /** Vertex y coordinate */
  358. get y() {
  359. return this.position.y;
  360. }
  361. set y(val) {
  362. this.position.y = val;
  363. }
  364. /** Vertex z coordinate */
  365. get z() {
  366. return this.position.z;
  367. }
  368. set z(val) {
  369. this.position.z = val;
  370. }
  371. }
  372. //# sourceMappingURL=solidParticle.js.map