cloudPoint.ts 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. import { Nullable } from "../types";
  2. import { Color4, Vector2, Vector3, TmpVectors, Matrix, Quaternion } from "../Maths/math";
  3. import { Mesh } from "../Meshes/mesh";
  4. import { BoundingInfo } from "../Culling/boundingInfo";
  5. import { PointsCloudSystem } from "./pointsCloudSystem";
  6. /**
  7. * Represents one particle of a points cloud system.
  8. */
  9. export class CloudPoint {
  10. /**
  11. * particle global index
  12. */
  13. public idx: number = 0;
  14. /**
  15. * The color of the particle
  16. */
  17. public color: Nullable<Color4> = new Color4(1.0, 1.0, 1.0, 1.0);
  18. /**
  19. * The world space position of the particle.
  20. */
  21. public position: Vector3 = Vector3.Zero();
  22. /**
  23. * The world space rotation of the particle. (Not use if rotationQuaternion is set)
  24. */
  25. public rotation: Vector3 = Vector3.Zero();
  26. /**
  27. * The world space rotation quaternion of the particle.
  28. */
  29. public rotationQuaternion: Nullable<Quaternion>;
  30. /**
  31. * The uv of the particle.
  32. */
  33. public uv: Nullable<Vector2> = new Vector2(0.0, 0.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. * Index of this particle in the global "positions" array (Internal use)
  50. * @hidden
  51. */
  52. public _pos: number = 0;
  53. /**
  54. * @hidden Index of this particle in the global "indices" array (Internal use)
  55. */
  56. public _ind: number = 0;
  57. /**
  58. * Group this particle belongs to
  59. */
  60. public _group: PointsGroup;
  61. /**
  62. * Group id of this particle
  63. */
  64. public groupId: number = 0;
  65. /**
  66. * Index of the particle in its group id (Internal use)
  67. */
  68. public idxInGroup: number = 0;
  69. /**
  70. * @hidden Particle BoundingInfo object (Internal use)
  71. */
  72. public _boundingInfo: BoundingInfo;
  73. /**
  74. * @hidden Reference to the PCS that the particle belongs to (Internal use)
  75. */
  76. public _pcs: PointsCloudSystem;
  77. /**
  78. * @hidden Still set as invisible in order to skip useless computations (Internal use)
  79. */
  80. public _stillInvisible: boolean = false;
  81. /**
  82. * @hidden Last computed particle rotation matrix
  83. */
  84. public _rotationMatrix: number[] = [1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0];
  85. /**
  86. * Parent particle Id, if any.
  87. * Default null.
  88. */
  89. public parentId: Nullable<number> = null;
  90. /**
  91. * @hidden Internal global position in the PCS.
  92. */
  93. public _globalPosition: Vector3 = Vector3.Zero();
  94. /**
  95. * Creates a Point Cloud object.
  96. * Don't create particles manually, use instead the PCS internal tools like _addParticle()
  97. * @param particleIndex (integer) is the particle index in the PCS pool. It's also the particle identifier.
  98. * @param group (PointsGroup) is the group the particle belongs to
  99. * @param groupId (integer) is the group identifier in the PCS.
  100. * @param idxInGroup (integer) is the index of the particle in the current point group (ex: the 10th point of addPoints(30))
  101. * @param pcs defines the PCS it is associated to
  102. */
  103. constructor(particleIndex: number, group: PointsGroup, groupId: number, idxInGroup: number, pcs: PointsCloudSystem) {
  104. this.idx = particleIndex;
  105. this._group = group;
  106. this.groupId = groupId;
  107. this.idxInGroup = idxInGroup;
  108. this._pcs = pcs;
  109. }
  110. /**
  111. * get point size
  112. */
  113. public get size(): Vector3 {
  114. return this.size;
  115. }
  116. /**
  117. * Set point size
  118. */
  119. public set size(scale: Vector3) {
  120. this.size = scale;
  121. }
  122. /**
  123. * Legacy support, changed quaternion to rotationQuaternion
  124. */
  125. public get quaternion(): Nullable<Quaternion> {
  126. return this.rotationQuaternion;
  127. }
  128. /**
  129. * Legacy support, changed quaternion to rotationQuaternion
  130. */
  131. public set quaternion(q: Nullable<Quaternion>) {
  132. this.rotationQuaternion = q;
  133. }
  134. /**
  135. * Returns a boolean. True if the particle intersects a mesh, else false
  136. * The intersection is computed on the particle position and Axis Aligned Bounding Box (AABB) or Sphere
  137. * @param target is the object (point or mesh) what the intersection is computed against
  138. * @param isSphere is boolean flag when false (default) bounding box of mesh is used, when true the bounding sphere is used
  139. * @returns true if it intersects
  140. */
  141. public intersectsMesh(target: Mesh, isSphere: boolean): boolean {
  142. if (!target._boundingInfo) {
  143. return false;
  144. }
  145. isSphere = isSphere ? isSphere : false;
  146. if (isSphere) {
  147. return target.getBoundingInfo().boundingSphere.intersectsPoint(this.position.add(this._pcs.mesh.position));
  148. }
  149. else {
  150. let maxX = 0;
  151. let minX = 0;
  152. let maxY = 0;
  153. let minY = 0;
  154. let maxZ = 0;
  155. let minZ = 0;
  156. maxX = target.getBoundingInfo().boundingBox.maximumWorld.x;
  157. minX = target.getBoundingInfo().boundingBox.minimumWorld.x;
  158. maxY = target.getBoundingInfo().boundingBox.maximumWorld.y;
  159. minY = target.getBoundingInfo().boundingBox.minimumWorld.y;
  160. maxZ = target.getBoundingInfo().boundingBox.maximumWorld.z;
  161. minZ = target.getBoundingInfo().boundingBox.minimumWorld.z;
  162. let x = this.position.x + this._pcs.mesh.position.x;
  163. let y = this.position.y + this._pcs.mesh.position.y;
  164. let z = this.position.z + this._pcs.mesh.position.z;
  165. return minX <= x && x <= maxX && minY <= y && y <= maxY && minZ <= z && z <= maxZ;
  166. }
  167. }
  168. /**
  169. * get the rotation matrix of the particle
  170. * @hidden
  171. */
  172. public getRotationMatrix(m: Matrix) {
  173. let quaternion: Quaternion;
  174. if (this.rotationQuaternion) {
  175. quaternion = this.rotationQuaternion;
  176. }
  177. else {
  178. quaternion = TmpVectors.Quaternion[0];
  179. const rotation = this.rotation;
  180. Quaternion.RotationYawPitchRollToRef(rotation.y, rotation.x, rotation.z, quaternion);
  181. }
  182. quaternion.toRotationMatrix(m);
  183. }
  184. }
  185. /**
  186. * Represents a group of points in a points cloud system
  187. * * PCS internal tool, don't use it manually.
  188. */
  189. export class PointsGroup {
  190. /**
  191. * The group id
  192. * @hidden
  193. */
  194. public groupID: number;
  195. /**
  196. * image data for group (internal use)
  197. * @hidden
  198. */
  199. public _groupImageData: Nullable<ArrayBufferView>;
  200. /**
  201. * Image Width (internal use)
  202. * @hidden
  203. */
  204. public _groupImgWidth: number;
  205. /**
  206. * Image Height (internal use)
  207. * @hidden
  208. */
  209. public _groupImgHeight: number;
  210. /**
  211. * Custom position function (internal use)
  212. * @hidden
  213. */
  214. public _positionFunction: Nullable<(particle: CloudPoint, i?: number, s?: number) => void>;
  215. /**
  216. * density per facet for surface points
  217. * @hidden
  218. */
  219. public _groupDensity: number[];
  220. /**
  221. * Only when points are colored by texture carries pointer to texture list array
  222. * @hidden
  223. */
  224. public _textureNb: number;
  225. /**
  226. * Creates a points group object. This is an internal reference to produce particles for the PCS.
  227. * PCS internal tool, don't use it manually.
  228. * @hidden
  229. */
  230. constructor(id: number, posFunction: Nullable<(particle: CloudPoint, i?: number, s?: number) => void>) {
  231. this.groupID = id;
  232. this._positionFunction = posFunction;
  233. }
  234. }