babylon.boundingInfo.ts 9.6 KB

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