|
@@ -74,42 +74,23 @@
|
|
* @param worldMatrix defines the new world matrix
|
|
* @param worldMatrix defines the new world matrix
|
|
*/
|
|
*/
|
|
public reConstruct(min: Vector3, max: Vector3, worldMatrix?: Matrix) {
|
|
public reConstruct(min: Vector3, max: Vector3, worldMatrix?: Matrix) {
|
|
- this.minimum.copyFrom(min);
|
|
|
|
- this.maximum.copyFrom(max);
|
|
|
|
-
|
|
|
|
- // Bounding vectors
|
|
|
|
- this.vectors[0].copyFrom(this.minimum);
|
|
|
|
- this.vectors[1].copyFrom(this.maximum);
|
|
|
|
- this.vectors[2].copyFrom(this.minimum);
|
|
|
|
- this.vectors[3].copyFrom(this.minimum);
|
|
|
|
- this.vectors[4].copyFrom(this.minimum);
|
|
|
|
- this.vectors[5].copyFrom(this.maximum);
|
|
|
|
- this.vectors[6].copyFrom(this.maximum);
|
|
|
|
- this.vectors[7].copyFrom(this.maximum);
|
|
|
|
-
|
|
|
|
- this.vectors[2].x = this.maximum.x;
|
|
|
|
- this.vectors[3].y = this.maximum.y;
|
|
|
|
- this.vectors[4].z = this.maximum.z;
|
|
|
|
- this.vectors[5].z = this.minimum.z;
|
|
|
|
- this.vectors[6].x = this.minimum.x;
|
|
|
|
- this.vectors[7].y = this.minimum.y;
|
|
|
|
|
|
+ const minX = min.x, minY = min.y, minZ = min.z, maxX = max.x, maxY = max.y, maxZ = max.z;
|
|
|
|
+ const vectors = this.vectors;
|
|
|
|
+
|
|
|
|
+ this.minimum.copyFromFloats(minX, minY, minZ);
|
|
|
|
+ this.maximum.copyFromFloats(maxX, maxY, maxZ);
|
|
|
|
+ vectors[0].copyFromFloats(minX, minY, minZ);
|
|
|
|
+ vectors[1].copyFromFloats(maxX, maxY, maxZ);
|
|
|
|
+ vectors[2].copyFromFloats(maxX, minY, minZ);
|
|
|
|
+ vectors[3].copyFromFloats(minX, maxY, minZ);
|
|
|
|
+ vectors[4].copyFromFloats(minX, minY, maxZ);
|
|
|
|
+ vectors[5].copyFromFloats(maxX, maxY, minZ);
|
|
|
|
+ vectors[6].copyFromFloats(minX, maxY, maxZ);
|
|
|
|
+ vectors[7].copyFromFloats(maxX, minY, maxZ);
|
|
|
|
|
|
// OBB
|
|
// OBB
|
|
- this.center.copyFrom(this.maximum).addInPlace(this.minimum).scaleInPlace(0.5);
|
|
|
|
- this.extendSize.copyFrom(this.maximum).subtractInPlace(this.minimum).scaleInPlace(0.5);
|
|
|
|
- for (var index = 0; index < 3; index++) {
|
|
|
|
- this.directions[index].copyFromFloats(0,0,0);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- // World
|
|
|
|
- for (var index = 0; index < 8; index++) {
|
|
|
|
- this.vectorsWorld[index].copyFromFloats(0,0,0);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- this.minimumWorld.copyFromFloats(0,0,0);
|
|
|
|
- this.maximumWorld.copyFromFloats(0,0,0);
|
|
|
|
- this.centerWorld.copyFromFloats(0,0,0);
|
|
|
|
- this.extendSizeWorld.copyFromFloats(0,0,0);
|
|
|
|
|
|
+ this.maximum.addToRef(min, this.center).scaleInPlace(0.5);
|
|
|
|
+ this.maximum.subtractToRef(max, this.extendSize).scaleInPlace(0.5);
|
|
|
|
|
|
this._update(worldMatrix || this._worldMatrix || Matrix.Identity());
|
|
this._update(worldMatrix || this._worldMatrix || Matrix.Identity());
|
|
}
|
|
}
|
|
@@ -120,13 +101,15 @@
|
|
* @returns the current bounding box
|
|
* @returns the current bounding box
|
|
*/
|
|
*/
|
|
public scale(factor: number): BoundingBox {
|
|
public scale(factor: number): BoundingBox {
|
|
- const diff = Tmp.Vector3[0].copyFrom(this.maximum).subtractInPlace(this.minimum);
|
|
|
|
- let distance = diff.length() * factor;
|
|
|
|
- diff.normalize();
|
|
|
|
- let newRadius = diff.scaleInPlace(distance * 0.5);
|
|
|
|
|
|
+ const tmpVectors = Tmp.Vector3;
|
|
|
|
+ const diff = this.maximum.subtractToRef(this.minimum, tmpVectors[0]);
|
|
|
|
+ const len = diff.length();
|
|
|
|
+ diff.normalizeFromLength(len);
|
|
|
|
+ const distance = len * factor;
|
|
|
|
+ const newRadius = diff.scaleInPlace(distance * 0.5);
|
|
|
|
|
|
- const min = Tmp.Vector3[1].copyFrom(this.center).subtractInPlace(newRadius);
|
|
|
|
- const max = Tmp.Vector3[2].copyFrom(this.center).addInPlace(newRadius);
|
|
|
|
|
|
+ const min = this.center.subtractToRef(newRadius, tmpVectors[1]);
|
|
|
|
+ const max = this.center.addToRef(newRadius, tmpVectors[2]);
|
|
|
|
|
|
this.reConstruct(min, max);
|
|
this.reConstruct(min, max);
|
|
|
|
|
|
@@ -153,27 +136,30 @@
|
|
|
|
|
|
/** @hidden */
|
|
/** @hidden */
|
|
public _update(world: Matrix): void {
|
|
public _update(world: Matrix): void {
|
|
- Vector3.FromFloatsToRef(Number.MAX_VALUE, Number.MAX_VALUE, Number.MAX_VALUE, this.minimumWorld);
|
|
|
|
- Vector3.FromFloatsToRef(-Number.MAX_VALUE, -Number.MAX_VALUE, -Number.MAX_VALUE, this.maximumWorld);
|
|
|
|
-
|
|
|
|
- for (var index = 0; index < 8; index++) {
|
|
|
|
- var v = this.vectorsWorld[index];
|
|
|
|
- Vector3.TransformCoordinatesToRef(this.vectors[index], world, v);
|
|
|
|
- this.minimumWorld.minimizeInPlace(v);
|
|
|
|
- this.maximumWorld.maximizeInPlace(v);
|
|
|
|
|
|
+ const minWorld = this.minimumWorld;
|
|
|
|
+ const maxWorld = this.maximumWorld;
|
|
|
|
+ const directions = this.directions;
|
|
|
|
+
|
|
|
|
+ minWorld.setAll(Number.MAX_VALUE);
|
|
|
|
+ maxWorld.setAll(-Number.MAX_VALUE);
|
|
|
|
+
|
|
|
|
+ const vectorsWorld = this.vectorsWorld;
|
|
|
|
+ const vectors = this.vectors;
|
|
|
|
+ for (let index = 0; index < 8; ++index) {
|
|
|
|
+ const v = vectorsWorld[index];
|
|
|
|
+ Vector3.TransformCoordinatesToRef(vectors[index], world, v);
|
|
|
|
+ minWorld.minimizeInPlace(v);
|
|
|
|
+ maxWorld.maximizeInPlace(v);
|
|
}
|
|
}
|
|
|
|
|
|
// Extend
|
|
// Extend
|
|
- this.maximumWorld.subtractToRef(this.minimumWorld, this.extendSizeWorld);
|
|
|
|
- this.extendSizeWorld.scaleInPlace(0.5);
|
|
|
|
|
|
+ maxWorld.subtractToRef(minWorld, this.extendSizeWorld).scaleInPlace(0.5);
|
|
|
|
+ // OOBB
|
|
|
|
+ maxWorld.addToRef(minWorld, this.centerWorld).scaleInPlace(0.5);
|
|
|
|
|
|
- // OBB
|
|
|
|
- this.maximumWorld.addToRef(this.minimumWorld, this.centerWorld);
|
|
|
|
- this.centerWorld.scaleInPlace(0.5);
|
|
|
|
-
|
|
|
|
- Vector3.FromFloatArrayToRef(world.m, 0, this.directions[0]);
|
|
|
|
- Vector3.FromFloatArrayToRef(world.m, 4, this.directions[1]);
|
|
|
|
- Vector3.FromFloatArrayToRef(world.m, 8, this.directions[2]);
|
|
|
|
|
|
+ Vector3.FromArrayToRef(world.m, 0, directions[0]);
|
|
|
|
+ Vector3.FromArrayToRef(world.m, 4, directions[1]);
|
|
|
|
+ Vector3.FromArrayToRef(world.m, 8, directions[2]);
|
|
|
|
|
|
this._worldMatrix = world;
|
|
this._worldMatrix = world;
|
|
}
|
|
}
|
|
@@ -202,15 +188,19 @@
|
|
* @returns true if the point is inside the bounding box
|
|
* @returns true if the point is inside the bounding box
|
|
*/
|
|
*/
|
|
public intersectsPoint(point: Vector3): boolean {
|
|
public intersectsPoint(point: Vector3): boolean {
|
|
|
|
+ const min = this.minimumWorld;
|
|
|
|
+ const max = this.maximumWorld;
|
|
|
|
+ const minX = min.x, minY = min.y, minZ = min.z, maxX = max.x, maxY = max.y, maxZ = max.z;
|
|
|
|
+ const pointX = point.x, pointY = point.y, pointZ = point.z;
|
|
var delta = -Epsilon;
|
|
var delta = -Epsilon;
|
|
|
|
|
|
- if (this.maximumWorld.x - point.x < delta || delta > point.x - this.minimumWorld.x)
|
|
|
|
|
|
+ if (maxX - pointX < delta || delta > pointX - minX)
|
|
return false;
|
|
return false;
|
|
|
|
|
|
- if (this.maximumWorld.y - point.y < delta || delta > point.y - this.minimumWorld.y)
|
|
|
|
|
|
+ if (maxY - pointY < delta || delta > pointY - minY)
|
|
return false;
|
|
return false;
|
|
|
|
|
|
- if (this.maximumWorld.z - point.z < delta || delta > point.z - this.minimumWorld.z)
|
|
|
|
|
|
+ if (maxZ - pointZ < delta || delta > pointZ - minZ)
|
|
return false;
|
|
return false;
|
|
|
|
|
|
return true;
|
|
return true;
|
|
@@ -232,13 +222,17 @@
|
|
* @returns true if there is an intersection
|
|
* @returns true if there is an intersection
|
|
*/
|
|
*/
|
|
public intersectsMinMax(min: Vector3, max: Vector3): boolean {
|
|
public intersectsMinMax(min: Vector3, max: Vector3): boolean {
|
|
- if (this.maximumWorld.x < min.x || this.minimumWorld.x > max.x)
|
|
|
|
|
|
+ const myMin = this.minimumWorld;
|
|
|
|
+ const myMax = this.maximumWorld;
|
|
|
|
+ const myMinX = myMin.x, myMinY = myMin.y, myMinZ = myMin.z, myMaxX = myMax.x, myMaxY = myMax.y, myMaxZ = myMax.z;
|
|
|
|
+ const minX = min.x, minY = min.y, minZ = min.z, maxX = max.x, maxY = max.y, maxZ = max.z;
|
|
|
|
+ if (myMaxX < minX || myMinX > maxX)
|
|
return false;
|
|
return false;
|
|
|
|
|
|
- if (this.maximumWorld.y < min.y || this.minimumWorld.y > max.y)
|
|
|
|
|
|
+ if (myMaxY < minY || myMinY > maxY)
|
|
return false;
|
|
return false;
|
|
|
|
|
|
- if (this.maximumWorld.z < min.z || this.minimumWorld.z > max.z)
|
|
|
|
|
|
+ if (myMaxZ < minZ || myMinZ > maxZ)
|
|
return false;
|
|
return false;
|
|
|
|
|
|
return true;
|
|
return true;
|
|
@@ -253,16 +247,7 @@
|
|
* @returns true if there is an intersection
|
|
* @returns true if there is an intersection
|
|
*/
|
|
*/
|
|
public static Intersects(box0: BoundingBox, box1: BoundingBox): boolean {
|
|
public static Intersects(box0: BoundingBox, box1: BoundingBox): boolean {
|
|
- if (box0.maximumWorld.x < box1.minimumWorld.x || box0.minimumWorld.x > box1.maximumWorld.x)
|
|
|
|
- return false;
|
|
|
|
-
|
|
|
|
- if (box0.maximumWorld.y < box1.minimumWorld.y || box0.minimumWorld.y > box1.maximumWorld.y)
|
|
|
|
- return false;
|
|
|
|
-
|
|
|
|
- if (box0.maximumWorld.z < box1.minimumWorld.z || box0.minimumWorld.z > box1.maximumWorld.z)
|
|
|
|
- return false;
|
|
|
|
-
|
|
|
|
- return true;
|
|
|
|
|
|
+ return box0.intersectsMinMax(box1.minimumWorld, box1.maximumWorld)
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -286,9 +271,10 @@
|
|
* @return true if there is an inclusion
|
|
* @return true if there is an inclusion
|
|
*/
|
|
*/
|
|
public static IsCompletelyInFrustum(boundingVectors: Vector3[], frustumPlanes: Plane[]): boolean {
|
|
public static IsCompletelyInFrustum(boundingVectors: Vector3[], frustumPlanes: Plane[]): boolean {
|
|
- for (var p = 0; p < 6; p++) {
|
|
|
|
- for (var i = 0; i < 8; i++) {
|
|
|
|
- if (frustumPlanes[p].dotCoordinate(boundingVectors[i]) < 0) {
|
|
|
|
|
|
+ for (var p = 0; p < 6; ++p) {
|
|
|
|
+ const frustumPlane = frustumPlanes[p];
|
|
|
|
+ for (var i = 0; i < 8; ++i) {
|
|
|
|
+ if (frustumPlane.dotCoordinate(boundingVectors[i]) < 0) {
|
|
return false;
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -303,17 +289,16 @@
|
|
* @return true if there is an intersection
|
|
* @return true if there is an intersection
|
|
*/
|
|
*/
|
|
public static IsInFrustum(boundingVectors: Vector3[], frustumPlanes: Plane[]): boolean {
|
|
public static IsInFrustum(boundingVectors: Vector3[], frustumPlanes: Plane[]): boolean {
|
|
- for (var p = 0; p < 6; p++) {
|
|
|
|
- var inCount = 8;
|
|
|
|
-
|
|
|
|
- for (var i = 0; i < 8; i++) {
|
|
|
|
- if (frustumPlanes[p].dotCoordinate(boundingVectors[i]) < 0) {
|
|
|
|
- --inCount;
|
|
|
|
- } else {
|
|
|
|
|
|
+ for (var p = 0; p < 6; ++p) {
|
|
|
|
+ let canReturnFalse = true;
|
|
|
|
+ const frustumPlane = frustumPlanes[p];
|
|
|
|
+ for (var i = 0; i < 8; ++i) {
|
|
|
|
+ if (frustumPlane.dotCoordinate(boundingVectors[i]) >= 0) {
|
|
|
|
+ canReturnFalse = false;
|
|
break;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- if (inCount === 0)
|
|
|
|
|
|
+ if (canReturnFalse)
|
|
return false;
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
return true;
|