babylon.boundingBox.ts 13 KB

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