cloudPoint.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. import { Color4, Vector2, Vector3, TmpVectors, Quaternion } from "../Maths/math.js";
  2. /**
  3. * Represents one particle of a points cloud system.
  4. */
  5. export class CloudPoint {
  6. /**
  7. * Creates a Point Cloud object.
  8. * Don't create particles manually, use instead the PCS internal tools like _addParticle()
  9. * @param particleIndex (integer) is the particle index in the PCS pool. It's also the particle identifier.
  10. * @param group (PointsGroup) is the group the particle belongs to
  11. * @param groupId (integer) is the group identifier in the PCS.
  12. * @param idxInGroup (integer) is the index of the particle in the current point group (ex: the 10th point of addPoints(30))
  13. * @param pcs defines the PCS it is associated to
  14. */
  15. constructor(particleIndex, group, groupId, idxInGroup, pcs) {
  16. /**
  17. * particle global index
  18. */
  19. this.idx = 0;
  20. /**
  21. * The color of the particle
  22. */
  23. this.color = new Color4(1.0, 1.0, 1.0, 1.0);
  24. /**
  25. * The world space position of the particle.
  26. */
  27. this.position = Vector3.Zero();
  28. /**
  29. * The world space rotation of the particle. (Not use if rotationQuaternion is set)
  30. */
  31. this.rotation = Vector3.Zero();
  32. /**
  33. * The uv of the particle.
  34. */
  35. this.uv = new Vector2(0.0, 0.0);
  36. /**
  37. * The current speed of the particle.
  38. */
  39. this.velocity = Vector3.Zero();
  40. /**
  41. * The pivot point in the particle local space.
  42. */
  43. this.pivot = Vector3.Zero();
  44. /**
  45. * Must the particle be translated from its pivot point in its local space ?
  46. * In this case, the pivot point is set at the origin of the particle local space and the particle is translated.
  47. * Default : false
  48. */
  49. this.translateFromPivot = false;
  50. /**
  51. * Index of this particle in the global "positions" array (Internal use)
  52. * @internal
  53. */
  54. this._pos = 0;
  55. /**
  56. * @internal Index of this particle in the global "indices" array (Internal use)
  57. */
  58. this._ind = 0;
  59. /**
  60. * Group id of this particle
  61. */
  62. this.groupId = 0;
  63. /**
  64. * Index of the particle in its group id (Internal use)
  65. */
  66. this.idxInGroup = 0;
  67. /**
  68. * @internal Still set as invisible in order to skip useless computations (Internal use)
  69. */
  70. this._stillInvisible = false;
  71. /**
  72. * @internal Last computed particle rotation matrix
  73. */
  74. this._rotationMatrix = [1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0];
  75. /**
  76. * Parent particle Id, if any.
  77. * Default null.
  78. */
  79. this.parentId = null;
  80. /**
  81. * @internal Internal global position in the PCS.
  82. */
  83. this._globalPosition = Vector3.Zero();
  84. this.idx = particleIndex;
  85. this._group = group;
  86. this.groupId = groupId;
  87. this.idxInGroup = idxInGroup;
  88. this._pcs = pcs;
  89. }
  90. /**
  91. * get point size
  92. */
  93. get size() {
  94. return this.size;
  95. }
  96. /**
  97. * Set point size
  98. */
  99. set size(scale) {
  100. this.size = scale;
  101. }
  102. /**
  103. * Legacy support, changed quaternion to rotationQuaternion
  104. */
  105. get quaternion() {
  106. return this.rotationQuaternion;
  107. }
  108. /**
  109. * Legacy support, changed quaternion to rotationQuaternion
  110. */
  111. set quaternion(q) {
  112. this.rotationQuaternion = q;
  113. }
  114. /**
  115. * Returns a boolean. True if the particle intersects a mesh, else false
  116. * The intersection is computed on the particle position and Axis Aligned Bounding Box (AABB) or Sphere
  117. * @param target is the object (point or mesh) what the intersection is computed against
  118. * @param isSphere is boolean flag when false (default) bounding box of mesh is used, when true the bounding sphere is used
  119. * @returns true if it intersects
  120. */
  121. intersectsMesh(target, isSphere) {
  122. if (!target.hasBoundingInfo) {
  123. return false;
  124. }
  125. if (!this._pcs.mesh) {
  126. throw new Error("Point Cloud System doesnt contain the Mesh");
  127. }
  128. if (isSphere) {
  129. return target.getBoundingInfo().boundingSphere.intersectsPoint(this.position.add(this._pcs.mesh.position));
  130. }
  131. const bbox = target.getBoundingInfo().boundingBox;
  132. const maxX = bbox.maximumWorld.x;
  133. const minX = bbox.minimumWorld.x;
  134. const maxY = bbox.maximumWorld.y;
  135. const minY = bbox.minimumWorld.y;
  136. const maxZ = bbox.maximumWorld.z;
  137. const minZ = bbox.minimumWorld.z;
  138. const x = this.position.x + this._pcs.mesh.position.x;
  139. const y = this.position.y + this._pcs.mesh.position.y;
  140. const z = this.position.z + this._pcs.mesh.position.z;
  141. return minX <= x && x <= maxX && minY <= y && y <= maxY && minZ <= z && z <= maxZ;
  142. }
  143. /**
  144. * get the rotation matrix of the particle
  145. * @internal
  146. */
  147. getRotationMatrix(m) {
  148. let quaternion;
  149. if (this.rotationQuaternion) {
  150. quaternion = this.rotationQuaternion;
  151. }
  152. else {
  153. quaternion = TmpVectors.Quaternion[0];
  154. const rotation = this.rotation;
  155. Quaternion.RotationYawPitchRollToRef(rotation.y, rotation.x, rotation.z, quaternion);
  156. }
  157. quaternion.toRotationMatrix(m);
  158. }
  159. }
  160. /**
  161. * Represents a group of points in a points cloud system
  162. * * PCS internal tool, don't use it manually.
  163. */
  164. export class PointsGroup {
  165. /**
  166. * Get or set the groupId
  167. * @deprecated Please use groupId instead
  168. */
  169. get groupID() {
  170. return this.groupId;
  171. }
  172. set groupID(groupID) {
  173. this.groupId = groupID;
  174. }
  175. /**
  176. * Creates a points group object. This is an internal reference to produce particles for the PCS.
  177. * PCS internal tool, don't use it manually.
  178. * @internal
  179. */
  180. constructor(id, posFunction) {
  181. this.groupId = id;
  182. this._positionFunction = posFunction;
  183. }
  184. }
  185. //# sourceMappingURL=cloudPoint.js.map