babylon.boundingInfo.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. module BABYLON {
  2. const _result0 = { min: 0, max: 0};
  3. const _result1 = { min: 0, max: 0};
  4. const computeBoxExtents = (axis: DeepImmutable<Vector3>, box: DeepImmutable<BoundingBox>, result: {min: number, max: number}) => {
  5. const p = Vector3.Dot(box.centerWorld, axis);
  6. const r0 = Math.abs(Vector3.Dot(box.directions[0], axis)) * box.extendSize.x;
  7. const r1 = Math.abs(Vector3.Dot(box.directions[1], axis)) * box.extendSize.y;
  8. const r2 = Math.abs(Vector3.Dot(box.directions[2], axis)) * box.extendSize.z;
  9. const r = r0 + r1 + r2;
  10. result.min = p - r;
  11. result.max = p + r;
  12. };
  13. const axisOverlap = (axis: DeepImmutable<Vector3>, box0: DeepImmutable<BoundingBox>, box1: DeepImmutable<BoundingBox>): boolean => {
  14. computeBoxExtents(axis, box0, _result0);
  15. computeBoxExtents(axis, box1, _result1);
  16. return !(_result0.min > _result1.max || _result1.min > _result0.max);
  17. };
  18. /**
  19. * Interface for cullable objects
  20. * @see https://doc.babylonjs.com/babylon101/materials#back-face-culling
  21. */
  22. export interface ICullable {
  23. /**
  24. * Checks if the object or part of the object is in the frustum
  25. * @param frustumPlanes Camera near/planes
  26. * @returns true if the object is in frustum otherwise false
  27. */
  28. isInFrustum(frustumPlanes: Plane[]): boolean;
  29. /**
  30. * Checks if a cullable object (mesh...) is in the camera frustum
  31. * Unlike isInFrustum this cheks the full bounding box
  32. * @param frustumPlanes Camera near/planes
  33. * @returns true if the object is in frustum otherwise false
  34. */
  35. isCompletelyInFrustum(frustumPlanes: Plane[]): boolean;
  36. }
  37. /**
  38. * Info for a bounding data of a mesh
  39. */
  40. export class BoundingInfo implements ICullable {
  41. /**
  42. * Bounding box for the mesh
  43. */
  44. public readonly boundingBox: BoundingBox;
  45. /**
  46. * Bounding sphere for the mesh
  47. */
  48. public readonly boundingSphere: BoundingSphere;
  49. private _isLocked = false;
  50. private static readonly TmpVector3 = Tools.BuildArray(2, Vector3.Zero);
  51. /**
  52. * Constructs bounding info
  53. * @param minimum min vector of the bounding box/sphere
  54. * @param maximum max vector of the bounding box/sphere
  55. * @param worldMatrix defines the new world matrix
  56. */
  57. constructor(minimum: DeepImmutable<Vector3>, maximum: DeepImmutable<Vector3>, worldMatrix?: DeepImmutable<Matrix>) {
  58. this.boundingBox = new BoundingBox(minimum, maximum, worldMatrix);
  59. this.boundingSphere = new BoundingSphere(minimum, maximum, worldMatrix);
  60. }
  61. /**
  62. * Recreates the entire bounding info from scratch as if we call the constructor in place
  63. * @param min defines the new minimum vector (in local space)
  64. * @param max defines the new maximum vector (in local space)
  65. * @param worldMatrix defines the new world matrix
  66. */
  67. public reConstruct(min: DeepImmutable<Vector3>, max: DeepImmutable<Vector3>, worldMatrix?: DeepImmutable<Matrix>) {
  68. this.boundingBox.reConstruct(min, max, worldMatrix);
  69. this.boundingSphere.reConstruct(min, max, worldMatrix);
  70. }
  71. /**
  72. * min vector of the bounding box/sphere
  73. */
  74. public get minimum(): Vector3 {
  75. return this.boundingBox.minimum;
  76. }
  77. /**
  78. * max vector of the bounding box/sphere
  79. */
  80. public get maximum(): Vector3 {
  81. return this.boundingBox.maximum;
  82. }
  83. /**
  84. * If the info is locked and won't be updated to avoid perf overhead
  85. */
  86. public get isLocked(): boolean {
  87. return this._isLocked;
  88. }
  89. public set isLocked(value: boolean) {
  90. this._isLocked = value;
  91. }
  92. // Methods
  93. /**
  94. * Updates the bounding sphere and box
  95. * @param world world matrix to be used to update
  96. */
  97. public update(world: DeepImmutable<Matrix>) {
  98. if (this._isLocked) {
  99. return;
  100. }
  101. this.boundingBox._update(world);
  102. this.boundingSphere._update(world);
  103. }
  104. /**
  105. * Recreate the bounding info to be centered around a specific point given a specific extend.
  106. * @param center New center of the bounding info
  107. * @param extend New extend of the bounding info
  108. * @returns the current bounding info
  109. */
  110. public centerOn(center: DeepImmutable<Vector3>, extend: DeepImmutable<Vector3>): BoundingInfo {
  111. const minimum = BoundingInfo.TmpVector3[0].copyFrom(center).subtractInPlace(extend);
  112. const maximum = BoundingInfo.TmpVector3[1].copyFrom(center).addInPlace(extend);
  113. this.boundingBox.reConstruct(minimum, maximum, this.boundingBox.getWorldMatrix());
  114. this.boundingSphere.reConstruct(minimum, maximum, this.boundingBox.getWorldMatrix());
  115. return this;
  116. }
  117. /**
  118. * Scale the current bounding info by applying a scale factor
  119. * @param factor defines the scale factor to apply
  120. * @returns the current bounding info
  121. */
  122. public scale(factor: number): BoundingInfo {
  123. this.boundingBox.scale(factor);
  124. this.boundingSphere.scale(factor);
  125. return this;
  126. }
  127. /**
  128. * Returns `true` if the bounding info is within the frustum defined by the passed array of planes.
  129. * @param frustumPlanes defines the frustum to test
  130. * @param strategy defines the strategy to use for the culling (default is BABYLON.Scene.CULLINGSTRATEGY_STANDARD)
  131. * @returns true if the bounding info is in the frustum planes
  132. */
  133. public isInFrustum(frustumPlanes: Array<DeepImmutable<Plane>>, strategy: number = AbstractMesh.CULLINGSTRATEGY_STANDARD): boolean {
  134. if (!this.boundingSphere.isInFrustum(frustumPlanes)) {
  135. return false;
  136. }
  137. if (strategy === AbstractMesh.CULLINGSTRATEGY_BOUNDINGSPHERE_ONLY) {
  138. return true;
  139. }
  140. return this.boundingBox.isInFrustum(frustumPlanes);
  141. }
  142. /**
  143. * Gets the world distance between the min and max points of the bounding box
  144. */
  145. public get diagonalLength(): number {
  146. const boundingBox = this.boundingBox;
  147. const diag = boundingBox.maximumWorld.subtractToRef(boundingBox.minimumWorld, BoundingInfo.TmpVector3[0]);
  148. return diag.length();
  149. }
  150. /**
  151. * Checks if a cullable object (mesh...) is in the camera frustum
  152. * Unlike isInFrustum this cheks the full bounding box
  153. * @param frustumPlanes Camera near/planes
  154. * @returns true if the object is in frustum otherwise false
  155. */
  156. public isCompletelyInFrustum(frustumPlanes: Array<DeepImmutable<Plane>>): boolean {
  157. return this.boundingBox.isCompletelyInFrustum(frustumPlanes);
  158. }
  159. /** @hidden */
  160. public _checkCollision(collider: Collider): boolean {
  161. return collider._canDoCollision(this.boundingSphere.centerWorld, this.boundingSphere.radiusWorld, this.boundingBox.minimumWorld, this.boundingBox.maximumWorld);
  162. }
  163. /**
  164. * Checks if a point is inside the bounding box and bounding sphere or the mesh
  165. * @see https://doc.babylonjs.com/babylon101/intersect_collisions_-_mesh
  166. * @param point the point to check intersection with
  167. * @returns if the point intersects
  168. */
  169. public intersectsPoint(point: DeepImmutable<Vector3>): boolean {
  170. if (!this.boundingSphere.centerWorld) {
  171. return false;
  172. }
  173. if (!this.boundingSphere.intersectsPoint(point)) {
  174. return false;
  175. }
  176. if (!this.boundingBox.intersectsPoint(point)) {
  177. return false;
  178. }
  179. return true;
  180. }
  181. /**
  182. * Checks if another bounding info intersects the bounding box and bounding sphere or the mesh
  183. * @see https://doc.babylonjs.com/babylon101/intersect_collisions_-_mesh
  184. * @param boundingInfo the bounding info to check intersection with
  185. * @param precise if the intersection should be done using OBB
  186. * @returns if the bounding info intersects
  187. */
  188. public intersects(boundingInfo: DeepImmutable<BoundingInfo>, precise: boolean): boolean {
  189. if (!BoundingSphere.Intersects(this.boundingSphere, boundingInfo.boundingSphere)) {
  190. return false;
  191. }
  192. if (!BoundingBox.Intersects(this.boundingBox, boundingInfo.boundingBox)) {
  193. return false;
  194. }
  195. if (!precise) {
  196. return true;
  197. }
  198. var box0 = this.boundingBox;
  199. var box1 = boundingInfo.boundingBox;
  200. if (!axisOverlap(box0.directions[0], box0, box1)) { return false; }
  201. if (!axisOverlap(box0.directions[1], box0, box1)) { return false; }
  202. if (!axisOverlap(box0.directions[2], box0, box1)) { return false; }
  203. if (!axisOverlap(box1.directions[0], box0, box1)) { return false; }
  204. if (!axisOverlap(box1.directions[1], box0, box1)) { return false; }
  205. if (!axisOverlap(box1.directions[2], box0, box1)) { return false; }
  206. if (!axisOverlap(Vector3.Cross(box0.directions[0], box1.directions[0]), box0, box1)) { return false; }
  207. if (!axisOverlap(Vector3.Cross(box0.directions[0], box1.directions[1]), box0, box1)) { return false; }
  208. if (!axisOverlap(Vector3.Cross(box0.directions[0], box1.directions[2]), box0, box1)) { return false; }
  209. if (!axisOverlap(Vector3.Cross(box0.directions[1], box1.directions[0]), box0, box1)) { return false; }
  210. if (!axisOverlap(Vector3.Cross(box0.directions[1], box1.directions[1]), box0, box1)) { return false; }
  211. if (!axisOverlap(Vector3.Cross(box0.directions[1], box1.directions[2]), box0, box1)) { return false; }
  212. if (!axisOverlap(Vector3.Cross(box0.directions[2], box1.directions[0]), box0, box1)) { return false; }
  213. if (!axisOverlap(Vector3.Cross(box0.directions[2], box1.directions[1]), box0, box1)) { return false; }
  214. if (!axisOverlap(Vector3.Cross(box0.directions[2], box1.directions[2]), box0, box1)) { return false; }
  215. return true;
  216. }
  217. }
  218. }