babylon.boundingBox.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. module BABYLON {
  2. /**
  3. * Class used to store bounding box information
  4. */
  5. export class BoundingBox implements ICullable {
  6. /**
  7. * Gets the 8 vectors representing the bounding box in local space
  8. */
  9. public vectors: Vector3[] = Tools.BuildArray(8, Vector3.Zero);
  10. /**
  11. * Gets the center of the bounding box in local space
  12. */
  13. public center: Vector3 = Vector3.Zero();
  14. /**
  15. * Gets the center of the bounding box in world space
  16. */
  17. public centerWorld: Vector3 = Vector3.Zero();
  18. /**
  19. * Gets the extend size in local space
  20. */
  21. public extendSize: Vector3 = Vector3.Zero();
  22. /**
  23. * Gets the extend size in world space
  24. */
  25. public extendSizeWorld: Vector3 = Vector3.Zero();
  26. /**
  27. * Gets the OBB (object bounding box) directions
  28. */
  29. public directions: Vector3[] = Tools.BuildArray(3, Vector3.Zero);
  30. /**
  31. * Gets the 8 vectors representing the bounding box in world space
  32. */
  33. public vectorsWorld: Vector3[] = Tools.BuildArray(8, Vector3.Zero);
  34. /**
  35. * Gets the minimum vector in world space
  36. */
  37. public minimumWorld: Vector3 = Vector3.Zero();
  38. /**
  39. * Gets the maximum vector in world space
  40. */
  41. public maximumWorld: Vector3 = Vector3.Zero();
  42. /**
  43. * Gets the minimum vector in local space
  44. */
  45. public minimum: Vector3 = Vector3.Zero();
  46. /**
  47. * Gets the maximum vector in local space
  48. */
  49. public maximum: Vector3 = Vector3.Zero();
  50. private _worldMatrix: Matrix;
  51. private static TmpVector3 = Tools.BuildArray(3, Vector3.Zero);
  52. /**
  53. * @hidden
  54. */
  55. public _tag: number;
  56. /**
  57. * Creates a new bounding box
  58. * @param min defines the minimum vector (in local space)
  59. * @param max defines the maximum vector (in local space)
  60. * @param worldMatrix defines the new world matrix
  61. */
  62. constructor(min: Vector3, max: Vector3, worldMatrix?: Matrix) {
  63. this.reConstruct(min, max, worldMatrix);
  64. }
  65. // Methods
  66. /**
  67. * Recreates the entire bounding box from scratch
  68. * @param min defines the new minimum vector (in local space)
  69. * @param max defines the new maximum vector (in local space)
  70. * @param worldMatrix defines the new world matrix
  71. */
  72. public reConstruct(min: Vector3, max: Vector3, worldMatrix?: Matrix) {
  73. const minX = min.x, minY = min.y, minZ = min.z, maxX = max.x, maxY = max.y, maxZ = max.z;
  74. const vectors = this.vectors;
  75. this.minimum.copyFromFloats(minX, minY, minZ);
  76. this.maximum.copyFromFloats(maxX, maxY, maxZ);
  77. vectors[0].copyFromFloats(minX, minY, minZ);
  78. vectors[1].copyFromFloats(maxX, maxY, maxZ);
  79. vectors[2].copyFromFloats(maxX, minY, minZ);
  80. vectors[3].copyFromFloats(minX, maxY, minZ);
  81. vectors[4].copyFromFloats(minX, minY, maxZ);
  82. vectors[5].copyFromFloats(maxX, maxY, minZ);
  83. vectors[6].copyFromFloats(minX, maxY, maxZ);
  84. vectors[7].copyFromFloats(maxX, minY, maxZ);
  85. // OBB
  86. max.addToRef(min, this.center).scaleInPlace(0.5);
  87. max.subtractToRef(min, this.extendSize).scaleInPlace(0.5);
  88. this._update(worldMatrix || this._worldMatrix || Matrix.Identity());
  89. }
  90. /**
  91. * Scale the current bounding box by applying a scale factor
  92. * @param factor defines the scale factor to apply
  93. * @returns the current bounding box
  94. */
  95. public scale(factor: number): BoundingBox {
  96. const tmpVectors = BoundingBox.TmpVector3;
  97. const diff = this.maximum.subtractToRef(this.minimum, tmpVectors[0]);
  98. const len = diff.length();
  99. diff.normalizeFromLength(len);
  100. const distance = len * factor;
  101. const newRadius = diff.scaleInPlace(distance * 0.5);
  102. const min = this.center.subtractToRef(newRadius, tmpVectors[1]);
  103. const max = this.center.addToRef(newRadius, tmpVectors[2]);
  104. this.reConstruct(min, max);
  105. return this;
  106. }
  107. /**
  108. * Gets the world matrix of the bounding box
  109. * @returns a matrix
  110. */
  111. public getWorldMatrix(): Matrix {
  112. return this._worldMatrix;
  113. }
  114. /**
  115. * Sets the world matrix stored in the bounding box
  116. * @param matrix defines the matrix to store
  117. * @returns current bounding box
  118. */
  119. public setWorldMatrix(matrix: Matrix): BoundingBox {
  120. this._worldMatrix.copyFrom(matrix);
  121. return this;
  122. }
  123. /** @hidden */
  124. public _update(world: Matrix): void {
  125. const minWorld = this.minimumWorld;
  126. const maxWorld = this.maximumWorld;
  127. const directions = this.directions;
  128. minWorld.setAll(Number.MAX_VALUE);
  129. maxWorld.setAll(-Number.MAX_VALUE);
  130. const vectorsWorld = this.vectorsWorld;
  131. const vectors = this.vectors;
  132. for (let index = 0; index < 8; ++index) {
  133. const v = vectorsWorld[index];
  134. Vector3.TransformCoordinatesToRef(vectors[index], world, v);
  135. minWorld.minimizeInPlace(v);
  136. maxWorld.maximizeInPlace(v);
  137. }
  138. // Extend
  139. maxWorld.subtractToRef(minWorld, this.extendSizeWorld).scaleInPlace(0.5);
  140. // OOBB
  141. maxWorld.addToRef(minWorld, this.centerWorld).scaleInPlace(0.5);
  142. Vector3.FromArrayToRef(world.m, 0, directions[0]);
  143. Vector3.FromArrayToRef(world.m, 4, directions[1]);
  144. Vector3.FromArrayToRef(world.m, 8, directions[2]);
  145. this._worldMatrix = world;
  146. }
  147. /**
  148. * Tests if the bounding box is intersecting the frustum planes
  149. * @param frustumPlanes defines the frustum planes to test
  150. * @returns true if there is an intersection
  151. */
  152. public isInFrustum(frustumPlanes: Plane[]): boolean {
  153. return BoundingBox.IsInFrustum(this.vectorsWorld, frustumPlanes);
  154. }
  155. /**
  156. * Tests if the bounding box is entirely inside the frustum planes
  157. * @param frustumPlanes defines the frustum planes to test
  158. * @returns true if there is an inclusion
  159. */
  160. public isCompletelyInFrustum(frustumPlanes: Plane[]): boolean {
  161. return BoundingBox.IsCompletelyInFrustum(this.vectorsWorld, frustumPlanes);
  162. }
  163. /**
  164. * Tests if a point is inside the bounding box
  165. * @param point defines the point to test
  166. * @returns true if the point is inside the bounding box
  167. */
  168. public intersectsPoint(point: Vector3): boolean {
  169. const min = this.minimumWorld;
  170. const max = this.maximumWorld;
  171. const minX = min.x, minY = min.y, minZ = min.z, maxX = max.x, maxY = max.y, maxZ = max.z;
  172. const pointX = point.x, pointY = point.y, pointZ = point.z;
  173. var delta = -Epsilon;
  174. if (maxX - pointX < delta || delta > pointX - minX) {
  175. return false;
  176. }
  177. if (maxY - pointY < delta || delta > pointY - minY) {
  178. return false;
  179. }
  180. if (maxZ - pointZ < delta || delta > pointZ - minZ) {
  181. return false;
  182. }
  183. return true;
  184. }
  185. /**
  186. * Tests if the bounding box intersects with a bounding sphere
  187. * @param sphere defines the sphere to test
  188. * @returns true if there is an intersection
  189. */
  190. public intersectsSphere(sphere: BoundingSphere): boolean {
  191. return BoundingBox.IntersectsSphere(this.minimumWorld, this.maximumWorld, sphere.centerWorld, sphere.radiusWorld);
  192. }
  193. /**
  194. * Tests if the bounding box intersects with a box defined by a min and max vectors
  195. * @param min defines the min vector to use
  196. * @param max defines the max vector to use
  197. * @returns true if there is an intersection
  198. */
  199. public intersectsMinMax(min: Vector3, max: Vector3): boolean {
  200. const myMin = this.minimumWorld;
  201. const myMax = this.maximumWorld;
  202. const myMinX = myMin.x, myMinY = myMin.y, myMinZ = myMin.z, myMaxX = myMax.x, myMaxY = myMax.y, myMaxZ = myMax.z;
  203. const minX = min.x, minY = min.y, minZ = min.z, maxX = max.x, maxY = max.y, maxZ = max.z;
  204. if (myMaxX < minX || myMinX > maxX) {
  205. return false;
  206. }
  207. if (myMaxY < minY || myMinY > maxY) {
  208. return false;
  209. }
  210. if (myMaxZ < minZ || myMinZ > maxZ) {
  211. return false;
  212. }
  213. return true;
  214. }
  215. // Statics
  216. /**
  217. * Tests if two bounding boxes are intersections
  218. * @param box0 defines the first box to test
  219. * @param box1 defines the second box to test
  220. * @returns true if there is an intersection
  221. */
  222. public static Intersects(box0: BoundingBox, box1: BoundingBox): boolean {
  223. return box0.intersectsMinMax(box1.minimumWorld, box1.maximumWorld);
  224. }
  225. /**
  226. * Tests if a bounding box defines by a min/max vectors intersects a sphere
  227. * @param minPoint defines the minimum vector of the bounding box
  228. * @param maxPoint defines the maximum vector of the bounding box
  229. * @param sphereCenter defines the sphere center
  230. * @param sphereRadius defines the sphere radius
  231. * @returns true if there is an intersection
  232. */
  233. public static IntersectsSphere(minPoint: Vector3, maxPoint: Vector3, sphereCenter: Vector3, sphereRadius: number): boolean {
  234. var vector = Vector3.Clamp(sphereCenter, minPoint, maxPoint);
  235. var num = Vector3.DistanceSquared(sphereCenter, vector);
  236. return (num <= (sphereRadius * sphereRadius));
  237. }
  238. /**
  239. * Tests if a bounding box defined with 8 vectors is entirely inside frustum planes
  240. * @param boundingVectors defines an array of 8 vectors representing a bounding box
  241. * @param frustumPlanes defines the frustum planes to test
  242. * @return true if there is an inclusion
  243. */
  244. public static IsCompletelyInFrustum(boundingVectors: Vector3[], frustumPlanes: Plane[]): boolean {
  245. for (var p = 0; p < 6; ++p) {
  246. const frustumPlane = frustumPlanes[p];
  247. for (var i = 0; i < 8; ++i) {
  248. if (frustumPlane.dotCoordinate(boundingVectors[i]) < 0) {
  249. return false;
  250. }
  251. }
  252. }
  253. return true;
  254. }
  255. /**
  256. * Tests if a bounding box defined with 8 vectors intersects frustum planes
  257. * @param boundingVectors defines an array of 8 vectors representing a bounding box
  258. * @param frustumPlanes defines the frustum planes to test
  259. * @return true if there is an intersection
  260. */
  261. public static IsInFrustum(boundingVectors: Vector3[], frustumPlanes: Plane[]): boolean {
  262. for (var p = 0; p < 6; ++p) {
  263. let canReturnFalse = true;
  264. const frustumPlane = frustumPlanes[p];
  265. for (var i = 0; i < 8; ++i) {
  266. if (frustumPlane.dotCoordinate(boundingVectors[i]) >= 0) {
  267. canReturnFalse = false;
  268. break;
  269. }
  270. }
  271. if (canReturnFalse) {
  272. return false;
  273. }
  274. }
  275. return true;
  276. }
  277. }
  278. }