boundingInfo.js 11 KB

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