|
@@ -1,15 +1,51 @@
|
|
|
module BABYLON {
|
|
|
+ /**
|
|
|
+ * Class used to store bounding box information
|
|
|
+ */
|
|
|
export class BoundingBox implements ICullable {
|
|
|
+ /**
|
|
|
+ * Gets the 8 vectors representing the bounding box in local space
|
|
|
+ */
|
|
|
public vectors: Vector3[];
|
|
|
+ /**
|
|
|
+ * Gets the center of the bounding box in local space
|
|
|
+ */
|
|
|
public center: Vector3;
|
|
|
+ /**
|
|
|
+ * Gets the center of the bounding box in world space
|
|
|
+ */
|
|
|
public centerWorld: Vector3;
|
|
|
+ /**
|
|
|
+ * Gets the extend size in local space
|
|
|
+ */
|
|
|
public extendSize: Vector3;
|
|
|
+ /**
|
|
|
+ * Gets the extend size in world space
|
|
|
+ */
|
|
|
public extendSizeWorld: Vector3;
|
|
|
+ /**
|
|
|
+ * Gets the OBB (object bounding box) directions
|
|
|
+ */
|
|
|
public directions: Vector3[];
|
|
|
+ /**
|
|
|
+ * Gets the 8 vectors representing the bounding box in world space
|
|
|
+ */
|
|
|
public vectorsWorld: Vector3[] = new Array<Vector3>();
|
|
|
+ /**
|
|
|
+ * Gets the minimum vector in world space
|
|
|
+ */
|
|
|
public minimumWorld: Vector3;
|
|
|
+ /**
|
|
|
+ * Gets the maximum vector in world space
|
|
|
+ */
|
|
|
public maximumWorld: Vector3;
|
|
|
+ /**
|
|
|
+ * Gets the minimum vector in local space
|
|
|
+ */
|
|
|
public minimum: Vector3;
|
|
|
+ /**
|
|
|
+ * Gets the maximum vector in local space
|
|
|
+ */
|
|
|
public maximum: Vector3;
|
|
|
|
|
|
private _worldMatrix: Matrix;
|
|
@@ -95,10 +131,19 @@
|
|
|
return this;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Gets the world matrix of the bounding box
|
|
|
+ * @returns a matrix
|
|
|
+ */
|
|
|
public getWorldMatrix(): Matrix {
|
|
|
return this._worldMatrix;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Sets the world matrix stored in the bounding box
|
|
|
+ * @param matrix defines the matrix to store
|
|
|
+ * @returns current bounding box
|
|
|
+ */
|
|
|
public setWorldMatrix(matrix: Matrix): BoundingBox {
|
|
|
this._worldMatrix.copyFrom(matrix);
|
|
|
return this;
|
|
@@ -143,14 +188,29 @@
|
|
|
this._worldMatrix = world;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Tests if the bounding box is intersecting the frustum planes
|
|
|
+ * @param frustumPlanes defines the frustum planes to test
|
|
|
+ * @returns true if there is an intersection
|
|
|
+ */
|
|
|
public isInFrustum(frustumPlanes: Plane[]): boolean {
|
|
|
return BoundingBox.IsInFrustum(this.vectorsWorld, frustumPlanes);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Tests if the bounding box is entirely inside the frustum planes
|
|
|
+ * @param frustumPlanes defines the frustum planes to test
|
|
|
+ * @returns true if there is an inclusion
|
|
|
+ */
|
|
|
public isCompletelyInFrustum(frustumPlanes: Plane[]): boolean {
|
|
|
return BoundingBox.IsCompletelyInFrustum(this.vectorsWorld, frustumPlanes);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Tests if a point is inside the bounding box
|
|
|
+ * @param point defines the point to test
|
|
|
+ * @returns true if the point is inside the bounding box
|
|
|
+ */
|
|
|
public intersectsPoint(point: Vector3): boolean {
|
|
|
var delta = -Epsilon;
|
|
|
|
|
@@ -166,10 +226,21 @@
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Tests if the bounding box intersects with a bounding sphere
|
|
|
+ * @param sphere defines the sphere to test
|
|
|
+ * @returns true if there is an intersection
|
|
|
+ */
|
|
|
public intersectsSphere(sphere: BoundingSphere): boolean {
|
|
|
return BoundingBox.IntersectsSphere(this.minimumWorld, this.maximumWorld, sphere.centerWorld, sphere.radiusWorld);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Tests if the bounding box intersects with a box defined by a min and max vectors
|
|
|
+ * @param min defines the min vector to use
|
|
|
+ * @param max defines the max vector to use
|
|
|
+ * @returns true if there is an intersection
|
|
|
+ */
|
|
|
public intersectsMinMax(min: Vector3, max: Vector3): boolean {
|
|
|
if (this.maximumWorld.x < min.x || this.minimumWorld.x > max.x)
|
|
|
return false;
|
|
@@ -184,6 +255,13 @@
|
|
|
}
|
|
|
|
|
|
// Statics
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Tests if two bounding boxes are intersections
|
|
|
+ * @param box0 defines the first box to test
|
|
|
+ * @param box1 defines the second box to test
|
|
|
+ * @returns true if there is an intersection
|
|
|
+ */
|
|
|
public static Intersects(box0: BoundingBox, box1: BoundingBox): boolean {
|
|
|
if (box0.maximumWorld.x < box1.minimumWorld.x || box0.minimumWorld.x > box1.maximumWorld.x)
|
|
|
return false;
|
|
@@ -197,12 +275,26 @@
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Tests if a bounding box defines by a min/max vectors intersects a sphere
|
|
|
+ * @param minPoint defines the minimum vector of the bounding box
|
|
|
+ * @param maxPoint defines the maximum vector of the bounding box
|
|
|
+ * @param sphereCenter defines the sphere center
|
|
|
+ * @param sphereRadius defines the sphere radius
|
|
|
+ * @returns true if there is an intersection
|
|
|
+ */
|
|
|
public static IntersectsSphere(minPoint: Vector3, maxPoint: Vector3, sphereCenter: Vector3, sphereRadius: number): boolean {
|
|
|
var vector = Vector3.Clamp(sphereCenter, minPoint, maxPoint);
|
|
|
var num = Vector3.DistanceSquared(sphereCenter, vector);
|
|
|
return (num <= (sphereRadius * sphereRadius));
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Tests if a bounding box defined with 8 vectors is entirely inside frustum planes
|
|
|
+ * @param boundingVectors defines an array of 8 vectors representing a bounding box
|
|
|
+ * @param frustumPlanes defines the frustum planes to test
|
|
|
+ * @return true if there is an inclusion
|
|
|
+ */
|
|
|
public static IsCompletelyInFrustum(boundingVectors: Vector3[], frustumPlanes: Plane[]): boolean {
|
|
|
for (var p = 0; p < 6; p++) {
|
|
|
for (var i = 0; i < 8; i++) {
|
|
@@ -214,6 +306,12 @@
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Tests if a bounding box defined with 8 vectors intersects frustum planes
|
|
|
+ * @param boundingVectors defines an array of 8 vectors representing a bounding box
|
|
|
+ * @param frustumPlanes defines the frustum planes to test
|
|
|
+ * @return true if there is an intersection
|
|
|
+ */
|
|
|
public static IsInFrustum(boundingVectors: Vector3[], frustumPlanes: Plane[]): boolean {
|
|
|
for (var p = 0; p < 6; p++) {
|
|
|
var inCount = 8;
|