babylon.boundingInfo.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. * @param worldMatrix defines the new world matrix
  56. * @param extraWorldExtent an extra extent that will be added in all diretionsto the world BoundingInfo only
  57. */
  58. constructor(minimum: Vector3, maximum: Vector3, worldMatrix?: Matrix, extraWorldExtent?: number) {
  59. this.boundingBox = new BoundingBox(minimum, maximum, worldMatrix, extraWorldExtent);
  60. this.boundingSphere = new BoundingSphere(minimum, maximum, worldMatrix, extraWorldExtent);
  61. }
  62. /**
  63. * Recreates the entire bounding info from scratch, producing same values as if the constructor was called.
  64. * @param min defines the new minimum vector (in local space)
  65. * @param max defines the new maximum vector (in local space)
  66. * @param worldMatrix defines the new world matrix.
  67. * @param extraWorldExtent an extra extent that will be added in all diretionsto the world BoundingInfo only
  68. */
  69. public reConstruct(min: Vector3, max: Vector3, worldMatrix?: Matrix, extraWorldExtent?: number) {
  70. this.boundingBox.reConstruct(min, max, worldMatrix, extraWorldExtent);
  71. this.boundingSphere.reConstruct(min, max, worldMatrix, extraWorldExtent);
  72. }
  73. /**
  74. * min vector of the bounding box/sphere
  75. */
  76. public get minimum(): Vector3 {
  77. return this.boundingBox.minimum;
  78. }
  79. /**
  80. * max vector of the bounding box/sphere
  81. */
  82. public get maximum(): Vector3 {
  83. return this.boundingBox.maximum;
  84. }
  85. /**
  86. * extra extent added to the world BoundingBox and BoundingSphere
  87. */
  88. public get extraWorldExtent(): number | undefined {
  89. return this.boundingBox.extraWorldExtent;
  90. }
  91. /**
  92. * If the info is locked and won't be updated to avoid perf overhead
  93. */
  94. public get isLocked(): boolean {
  95. return this._isLocked;
  96. }
  97. public set isLocked(value: boolean) {
  98. this._isLocked = value;
  99. }
  100. // Methods
  101. /**
  102. * Updates the bounding sphere and box
  103. * @param world world matrix to be used to update
  104. */
  105. public update(world: Matrix) {
  106. if (this._isLocked) {
  107. return;
  108. }
  109. const extraWorldExtent = this.boundingBox.extraWorldExtent;
  110. this.boundingBox._update(world, extraWorldExtent);
  111. this.boundingSphere._update(world, extraWorldExtent);
  112. }
  113. /**
  114. * Recreate the bounding info to be centered around a specific point given a specific extend.
  115. * @param center New center of the bounding info
  116. * @param extend New extend of the bounding info
  117. * @returns the current bounding info
  118. */
  119. public centerOn(center: Vector3, extend: Vector3): BoundingInfo {
  120. const minimum = Tmp.Vector3[0].copyFrom(center).subtractInPlace(extend);
  121. const maximum = Tmp.Vector3[1].copyFrom(center).addInPlace(extend);
  122. const extraWorldExtent = this.boundingBox.extraWorldExtent;
  123. this.boundingBox.reConstruct(minimum, maximum, this.boundingBox.getWorldMatrix(), extraWorldExtent);
  124. this.boundingSphere.reConstruct(minimum, maximum, this.boundingSphere.getWorldMatrix(), extraWorldExtent);
  125. return this;
  126. }
  127. /**
  128. * Scale the current bounding info by applying a scale factor
  129. * @param factor defines the scale factor to apply
  130. * @returns the current bounding info
  131. */
  132. public scale(factor: number): BoundingInfo {
  133. this.boundingBox.scale(factor);
  134. this.boundingSphere.scale(factor);
  135. return this;
  136. }
  137. /**
  138. * Returns `true` if the bounding info is within the frustum defined by the passed array of planes.
  139. * @param frustumPlanes defines the frustum to test
  140. * @param strategy defines the strategy to use for the culling (default is BABYLON.Scene.CULLINGSTRATEGY_STANDARD)
  141. * @returns true if the bounding info is in the frustum planes
  142. */
  143. public isInFrustum(frustumPlanes: Plane[], strategy: number = AbstractMesh.CULLINGSTRATEGY_STANDARD): boolean {
  144. if (!this.boundingSphere.isInFrustum(frustumPlanes)) {
  145. return false;
  146. }
  147. if (strategy === AbstractMesh.CULLINGSTRATEGY_BOUNDINGSPHERE_ONLY) {
  148. return true;
  149. }
  150. return this.boundingBox.isInFrustum(frustumPlanes);
  151. }
  152. /**
  153. * Gets the world distance between the min and max points of the bounding box
  154. */
  155. public get diagonalLength(): number {
  156. const boundingBox = this.boundingBox;
  157. const diag = boundingBox.maximumWorld.subtractToRef(boundingBox.minimumWorld, Tmp.Vector3[0]);
  158. return diag.length();
  159. }
  160. /**
  161. * Checks if a cullable object (mesh...) is in the camera frustum
  162. * Unlike isInFrustum this cheks the full bounding box
  163. * @param frustumPlanes Camera near/planes
  164. * @returns true if the object is in frustum otherwise false
  165. */
  166. public isCompletelyInFrustum(frustumPlanes: Plane[]): boolean {
  167. return this.boundingBox.isCompletelyInFrustum(frustumPlanes);
  168. }
  169. /** @hidden */
  170. public _checkCollision(collider: Collider): boolean {
  171. return collider._canDoCollision(this.boundingSphere.centerWorld, this.boundingSphere.radiusWorld, this.boundingBox.minimumWorld, this.boundingBox.maximumWorld);
  172. }
  173. /**
  174. * Checks if a point is inside the bounding box and bounding sphere or the mesh
  175. * @see https://doc.babylonjs.com/babylon101/intersect_collisions_-_mesh
  176. * @param point the point to check intersection with
  177. * @returns if the point intersects
  178. */
  179. public intersectsPoint(point: Vector3): boolean {
  180. if (!this.boundingSphere.centerWorld) {
  181. return false;
  182. }
  183. if (!this.boundingSphere.intersectsPoint(point)) {
  184. return false;
  185. }
  186. if (!this.boundingBox.intersectsPoint(point)) {
  187. return false;
  188. }
  189. return true;
  190. }
  191. /**
  192. * Checks if another bounding info intersects the bounding box and bounding sphere or the mesh
  193. * @see https://doc.babylonjs.com/babylon101/intersect_collisions_-_mesh
  194. * @param boundingInfo the bounding info to check intersection with
  195. * @param precise if the intersection should be done using OBB
  196. * @returns if the bounding info intersects
  197. */
  198. public intersects(boundingInfo: BoundingInfo, precise: boolean): boolean {
  199. if (!this.boundingSphere.centerWorld || !boundingInfo.boundingSphere.centerWorld) {
  200. return false;
  201. }
  202. if (!BoundingSphere.Intersects(this.boundingSphere, boundingInfo.boundingSphere)) {
  203. return false;
  204. }
  205. if (!BoundingBox.Intersects(this.boundingBox, boundingInfo.boundingBox)) {
  206. return false;
  207. }
  208. if (!precise) {
  209. return true;
  210. }
  211. var box0 = this.boundingBox;
  212. var box1 = boundingInfo.boundingBox;
  213. if (!axisOverlap(box0.directions[0], box0, box1)) { return false; }
  214. if (!axisOverlap(box0.directions[1], box0, box1)) { return false; }
  215. if (!axisOverlap(box0.directions[2], box0, box1)) { return false; }
  216. if (!axisOverlap(box1.directions[0], box0, box1)) { return false; }
  217. if (!axisOverlap(box1.directions[1], box0, box1)) { return false; }
  218. if (!axisOverlap(box1.directions[2], box0, box1)) { return false; }
  219. if (!axisOverlap(Vector3.Cross(box0.directions[0], box1.directions[0]), box0, box1)) { return false; }
  220. if (!axisOverlap(Vector3.Cross(box0.directions[0], box1.directions[1]), box0, box1)) { return false; }
  221. if (!axisOverlap(Vector3.Cross(box0.directions[0], box1.directions[2]), box0, box1)) { return false; }
  222. if (!axisOverlap(Vector3.Cross(box0.directions[1], box1.directions[0]), box0, box1)) { return false; }
  223. if (!axisOverlap(Vector3.Cross(box0.directions[1], box1.directions[1]), box0, box1)) { return false; }
  224. if (!axisOverlap(Vector3.Cross(box0.directions[1], box1.directions[2]), box0, box1)) { return false; }
  225. if (!axisOverlap(Vector3.Cross(box0.directions[2], box1.directions[0]), box0, box1)) { return false; }
  226. if (!axisOverlap(Vector3.Cross(box0.directions[2], box1.directions[1]), box0, box1)) { return false; }
  227. if (!axisOverlap(Vector3.Cross(box0.directions[2], box1.directions[2]), box0, box1)) { return false; }
  228. return true;
  229. }
  230. }
  231. }