| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- module BABYLON {
- const _result0 = { min: 0, max: 0};
- const _result1 = { min: 0, max: 0};
- const computeBoxExtents = (axis: DeepImmutable<Vector3>, box: DeepImmutable<BoundingBox>, result: {min: number, max: number}) => {
- const p = Vector3.Dot(box.centerWorld, axis);
- const r0 = Math.abs(Vector3.Dot(box.directions[0], axis)) * box.extendSize.x;
- const r1 = Math.abs(Vector3.Dot(box.directions[1], axis)) * box.extendSize.y;
- const r2 = Math.abs(Vector3.Dot(box.directions[2], axis)) * box.extendSize.z;
- const r = r0 + r1 + r2;
- result.min = p - r;
- result.max = p + r;
- };
- const axisOverlap = (axis: DeepImmutable<Vector3>, box0: DeepImmutable<BoundingBox>, box1: DeepImmutable<BoundingBox>): boolean => {
- computeBoxExtents(axis, box0, _result0);
- computeBoxExtents(axis, box1, _result1);
- return !(_result0.min > _result1.max || _result1.min > _result0.max);
- };
- /**
- * Interface for cullable objects
- * @see https://doc.babylonjs.com/babylon101/materials#back-face-culling
- */
- export interface ICullable {
- /**
- * Checks if the object or part of the object is in the frustum
- * @param frustumPlanes Camera near/planes
- * @returns true if the object is in frustum otherwise false
- */
- isInFrustum(frustumPlanes: Plane[]): boolean;
- /**
- * Checks if a cullable object (mesh...) is in the camera frustum
- * Unlike isInFrustum this cheks the full bounding box
- * @param frustumPlanes Camera near/planes
- * @returns true if the object is in frustum otherwise false
- */
- isCompletelyInFrustum(frustumPlanes: Plane[]): boolean;
- }
- /**
- * Info for a bounding data of a mesh
- */
- export class BoundingInfo implements ICullable {
- /**
- * Bounding box for the mesh
- */
- public readonly boundingBox: BoundingBox;
- /**
- * Bounding sphere for the mesh
- */
- public readonly boundingSphere: BoundingSphere;
- private _isLocked = false;
- private static readonly TmpVector3 = Tools.BuildArray(2, Vector3.Zero);
- /**
- * Constructs bounding info
- * @param minimum min vector of the bounding box/sphere
- * @param maximum max vector of the bounding box/sphere
- * @param worldMatrix defines the new world matrix
- */
- constructor(minimum: DeepImmutable<Vector3>, maximum: DeepImmutable<Vector3>, worldMatrix?: DeepImmutable<Matrix>) {
- this.boundingBox = new BoundingBox(minimum, maximum, worldMatrix);
- this.boundingSphere = new BoundingSphere(minimum, maximum, worldMatrix);
- }
- /**
- * Recreates the entire bounding info from scratch as if we call the constructor in place
- * @param min defines the new minimum vector (in local space)
- * @param max defines the new maximum vector (in local space)
- * @param worldMatrix defines the new world matrix
- */
- public reConstruct(min: DeepImmutable<Vector3>, max: DeepImmutable<Vector3>, worldMatrix?: DeepImmutable<Matrix>) {
- this.boundingBox.reConstruct(min, max, worldMatrix);
- this.boundingSphere.reConstruct(min, max, worldMatrix);
- }
- /**
- * min vector of the bounding box/sphere
- */
- public get minimum(): Vector3 {
- return this.boundingBox.minimum;
- }
- /**
- * max vector of the bounding box/sphere
- */
- public get maximum(): Vector3 {
- return this.boundingBox.maximum;
- }
- /**
- * If the info is locked and won't be updated to avoid perf overhead
- */
- public get isLocked(): boolean {
- return this._isLocked;
- }
- public set isLocked(value: boolean) {
- this._isLocked = value;
- }
- // Methods
- /**
- * Updates the bounding sphere and box
- * @param world world matrix to be used to update
- */
- public update(world: DeepImmutable<Matrix>) {
- if (this._isLocked) {
- return;
- }
- this.boundingBox._update(world);
- this.boundingSphere._update(world);
- }
- /**
- * Recreate the bounding info to be centered around a specific point given a specific extend.
- * @param center New center of the bounding info
- * @param extend New extend of the bounding info
- * @returns the current bounding info
- */
- public centerOn(center: DeepImmutable<Vector3>, extend: DeepImmutable<Vector3>): BoundingInfo {
- const minimum = BoundingInfo.TmpVector3[0].copyFrom(center).subtractInPlace(extend);
- const maximum = BoundingInfo.TmpVector3[1].copyFrom(center).addInPlace(extend);
- this.boundingBox.reConstruct(minimum, maximum, this.boundingBox.getWorldMatrix());
- this.boundingSphere.reConstruct(minimum, maximum, this.boundingBox.getWorldMatrix());
- return this;
- }
- /**
- * Scale the current bounding info by applying a scale factor
- * @param factor defines the scale factor to apply
- * @returns the current bounding info
- */
- public scale(factor: number): BoundingInfo {
- this.boundingBox.scale(factor);
- this.boundingSphere.scale(factor);
- return this;
- }
- /**
- * Returns `true` if the bounding info is within the frustum defined by the passed array of planes.
- * @param frustumPlanes defines the frustum to test
- * @param strategy defines the strategy to use for the culling (default is BABYLON.Scene.CULLINGSTRATEGY_STANDARD)
- * @returns true if the bounding info is in the frustum planes
- */
- public isInFrustum(frustumPlanes: Array<DeepImmutable<Plane>>, strategy: number = AbstractMesh.CULLINGSTRATEGY_STANDARD): boolean {
- if (!this.boundingSphere.isInFrustum(frustumPlanes)) {
- return false;
- }
- if (strategy === AbstractMesh.CULLINGSTRATEGY_BOUNDINGSPHERE_ONLY) {
- return true;
- }
- return this.boundingBox.isInFrustum(frustumPlanes);
- }
- /**
- * Gets the world distance between the min and max points of the bounding box
- */
- public get diagonalLength(): number {
- const boundingBox = this.boundingBox;
- const diag = boundingBox.maximumWorld.subtractToRef(boundingBox.minimumWorld, BoundingInfo.TmpVector3[0]);
- return diag.length();
- }
- /**
- * Checks if a cullable object (mesh...) is in the camera frustum
- * Unlike isInFrustum this cheks the full bounding box
- * @param frustumPlanes Camera near/planes
- * @returns true if the object is in frustum otherwise false
- */
- public isCompletelyInFrustum(frustumPlanes: Array<DeepImmutable<Plane>>): boolean {
- return this.boundingBox.isCompletelyInFrustum(frustumPlanes);
- }
- /** @hidden */
- public _checkCollision(collider: Collider): boolean {
- return collider._canDoCollision(this.boundingSphere.centerWorld, this.boundingSphere.radiusWorld, this.boundingBox.minimumWorld, this.boundingBox.maximumWorld);
- }
- /**
- * Checks if a point is inside the bounding box and bounding sphere or the mesh
- * @see https://doc.babylonjs.com/babylon101/intersect_collisions_-_mesh
- * @param point the point to check intersection with
- * @returns if the point intersects
- */
- public intersectsPoint(point: DeepImmutable<Vector3>): boolean {
- if (!this.boundingSphere.centerWorld) {
- return false;
- }
- if (!this.boundingSphere.intersectsPoint(point)) {
- return false;
- }
- if (!this.boundingBox.intersectsPoint(point)) {
- return false;
- }
- return true;
- }
- /**
- * Checks if another bounding info intersects the bounding box and bounding sphere or the mesh
- * @see https://doc.babylonjs.com/babylon101/intersect_collisions_-_mesh
- * @param boundingInfo the bounding info to check intersection with
- * @param precise if the intersection should be done using OBB
- * @returns if the bounding info intersects
- */
- public intersects(boundingInfo: DeepImmutable<BoundingInfo>, precise: boolean): boolean {
- if (!BoundingSphere.Intersects(this.boundingSphere, boundingInfo.boundingSphere)) {
- return false;
- }
- if (!BoundingBox.Intersects(this.boundingBox, boundingInfo.boundingBox)) {
- return false;
- }
- if (!precise) {
- return true;
- }
- var box0 = this.boundingBox;
- var box1 = boundingInfo.boundingBox;
- if (!axisOverlap(box0.directions[0], box0, box1)) { return false; }
- if (!axisOverlap(box0.directions[1], box0, box1)) { return false; }
- if (!axisOverlap(box0.directions[2], box0, box1)) { return false; }
- if (!axisOverlap(box1.directions[0], box0, box1)) { return false; }
- if (!axisOverlap(box1.directions[1], box0, box1)) { return false; }
- if (!axisOverlap(box1.directions[2], box0, box1)) { return false; }
- if (!axisOverlap(Vector3.Cross(box0.directions[0], box1.directions[0]), box0, box1)) { return false; }
- if (!axisOverlap(Vector3.Cross(box0.directions[0], box1.directions[1]), box0, box1)) { return false; }
- if (!axisOverlap(Vector3.Cross(box0.directions[0], box1.directions[2]), box0, box1)) { return false; }
- if (!axisOverlap(Vector3.Cross(box0.directions[1], box1.directions[0]), box0, box1)) { return false; }
- if (!axisOverlap(Vector3.Cross(box0.directions[1], box1.directions[1]), box0, box1)) { return false; }
- if (!axisOverlap(Vector3.Cross(box0.directions[1], box1.directions[2]), box0, box1)) { return false; }
- if (!axisOverlap(Vector3.Cross(box0.directions[2], box1.directions[0]), box0, box1)) { return false; }
- if (!axisOverlap(Vector3.Cross(box0.directions[2], box1.directions[1]), box0, box1)) { return false; }
- if (!axisOverlap(Vector3.Cross(box0.directions[2], box1.directions[2]), box0, box1)) { return false; }
- return true;
- }
- }
- }
|