boundingInfo.ts 12 KB

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