Forráskód Böngészése

move bounding box computation to node

Trevor Baron 6 éve
szülő
commit
923abe05f3
3 módosított fájl, 80 hozzáadás és 56 törlés
  1. 7 0
      src/Gizmos/boundingBoxGizmo.ts
  2. 15 55
      src/Meshes/abstractMesh.ts
  3. 58 1
      src/node.ts

+ 7 - 0
src/Gizmos/boundingBoxGizmo.ts

@@ -85,7 +85,12 @@ export class BoundingBoxGizmo extends Gizmo {
      * Relative bounding box pivot used when scaling the attached mesh. When null object with scale from the opposite corner. 0.5,0.5,0.5 for center and 0.5,0,0.5 for bottom (Default: null)
      */
     public scalePivot: Nullable<Vector3> = null;
+
+    /**
+     * Mesh used as a pivot to rotate the attached mesh
+     */
     private _anchorMesh: AbstractMesh;
+    
     private _existingMeshScale = new Vector3();
 
     // Dragging
@@ -416,6 +421,8 @@ export class BoundingBoxGizmo extends Gizmo {
             boundingMinMax.max.subtractToRef(boundingMinMax.min, this._boundingDimensions);
 
             // Update gizmo to match bounding box scaling and rotation
+            // The position set here is the offset from the origin for the boundingbox when the attached mesh is at the origin
+            // The position of the gizmo is then set to the attachedMesh in gizmo._update
             this._lineBoundingBox.scaling.copyFrom(this._boundingDimensions);
             this._lineBoundingBox.position.set((boundingMinMax.max.x + boundingMinMax.min.x) / 2, (boundingMinMax.max.y + boundingMinMax.min.y) / 2, (boundingMinMax.max.z + boundingMinMax.min.z) / 2);
             this._rotateSpheresParent.position.copyFrom(this._lineBoundingBox.position);

+ 15 - 55
src/Meshes/abstractMesh.ts

@@ -1129,62 +1129,22 @@ export class AbstractMesh extends TransformNode implements IDisposable, ICullabl
         return new Vector3(flipBack * defForwardMult, twirlClockwise, tiltRight * defForwardMult);
     }
 
-    /**
-     * Return the minimum and maximum world vectors of the entire hierarchy under current mesh
-     * @param includeDescendants Include bounding info from descendants as well (true by default)
-     * @param predicate defines a callback function that can be customize to filter what meshes should be included in the list used to compute the bounding vectors
-     * @returns the new bounding vectors
-     */
-    public getHierarchyBoundingVectors(includeDescendants = true, predicate: Nullable<(abstractMesh: AbstractMesh) => boolean> = null): { min: Vector3, max: Vector3 } {
-        // Ensures that all world matrix will be recomputed.
-        this.getScene().incrementRenderId();
-
-        this.computeWorldMatrix(true);
-
-        let min: Vector3;
-        let max: Vector3;
-        let boundingInfo = this.getBoundingInfo();
-
-        if (!this.subMeshes) {
-            min = new Vector3(Number.MAX_VALUE, Number.MAX_VALUE, Number.MAX_VALUE);
-            max = new Vector3(-Number.MAX_VALUE, -Number.MAX_VALUE, -Number.MAX_VALUE);
-        } else {
-            min = boundingInfo.boundingBox.minimumWorld;
-            max = boundingInfo.boundingBox.maximumWorld;
-        }
-
-        if (includeDescendants) {
-            let descendants = this.getDescendants(false);
-
-            for (var descendant of descendants) {
-                let childMesh = <AbstractMesh>descendant;
-                childMesh.computeWorldMatrix(true);
-
-                // Filters meshes based on custom predicate function.
-                if (predicate && !predicate(childMesh)) {
-                    continue;
-                }
-
-                //make sure we have the needed params to get mix and max
-                if (!childMesh.getBoundingInfo || childMesh.getTotalVertices() === 0) {
-                    continue;
-                }
-
-                let childBoundingInfo = childMesh.getBoundingInfo();
-                let boundingBox = childBoundingInfo.boundingBox;
-
-                var minBox = boundingBox.minimumWorld;
-                var maxBox = boundingBox.maximumWorld;
-
-                Tools.CheckExtends(minBox, min, max);
-                Tools.CheckExtends(maxBox, min, max);
-            }
+    protected _getBoudingInfoMin(){
+        if(this.subMeshes){
+            let boundingInfo = this.getBoundingInfo();
+            return boundingInfo.boundingBox.minimumWorld;
+        }else{
+            return new Vector3(Number.MAX_VALUE, Number.MAX_VALUE, Number.MAX_VALUE);
+        }
+        
+    }
+    protected _getBoudingInfoMax(){
+        if(this.subMeshes){
+            let boundingInfo = this.getBoundingInfo();
+            return boundingInfo.boundingBox.maximumWorld;
+        }else{
+            return new Vector3(-Number.MAX_VALUE, -Number.MAX_VALUE, -Number.MAX_VALUE);
         }
-
-        return {
-            min: min,
-            max: max
-        };
     }
 
     /**

+ 58 - 1
src/node.ts

@@ -1,6 +1,6 @@
 import { Scene } from "./scene";
 import { Nullable } from "./types";
-import { Matrix } from "./Maths/math";
+import { Matrix, Vector3 } from "./Maths/math";
 import { Engine } from "./Engines/engine";
 import { IBehaviorAware, Behavior } from "./Behaviors/behavior";
 import { serialize } from "./Misc/decorators";
@@ -9,6 +9,7 @@ import { EngineStore } from "./Engines/engineStore";
 import { _DevTools } from './Misc/devTools';
 import { AbstractActionManager } from './Actions/abstractActionManager';
 import { IInspectable } from './Misc/iInspectable';
+import { Tools } from './Misc/tools';
 
 declare type Animatable = import("./Animations/animatable").Animatable;
 declare type AnimationPropertiesOverride = import("./Animations/animationPropertiesOverride").AnimationPropertiesOverride;
@@ -765,6 +766,13 @@ export class Node implements IBehaviorAware<Node> {
         this._behaviors = [];
     }
 
+    protected _getBoudingInfoMin(){
+        return new Vector3(Number.MAX_VALUE, Number.MAX_VALUE, Number.MAX_VALUE);
+    }
+    protected _getBoudingInfoMax(){
+        return new Vector3(-Number.MAX_VALUE, -Number.MAX_VALUE, -Number.MAX_VALUE);
+    }
+
     /**
      * Parse animation range data from a serialization object and store them into a given node
      * @param node defines where to store the animation ranges
@@ -779,4 +787,53 @@ export class Node implements IBehaviorAware<Node> {
             }
         }
     }
+
+    /**
+     * Return the minimum and maximum world vectors of the entire hierarchy under current mesh
+     * @param includeDescendants Include bounding info from descendants as well (true by default)
+     * @param predicate defines a callback function that can be customize to filter what meshes should be included in the list used to compute the bounding vectors
+     * @returns the new bounding vectors
+     */
+    public getHierarchyBoundingVectors(includeDescendants = true, predicate: Nullable<(abstractMesh: AbstractMesh) => boolean> = null): { min: Vector3, max: Vector3 } {
+        // Ensures that all world matrix will be recomputed.
+        this.getScene().incrementRenderId();
+
+        this.computeWorldMatrix(true);
+
+        let min = this._getBoudingInfoMin();
+        let max = this._getBoudingInfoMax();
+
+        if (includeDescendants) {
+            let descendants = this.getDescendants(false);
+
+            for (var descendant of descendants) {
+                let childMesh = <AbstractMesh>descendant;
+                childMesh.computeWorldMatrix(true);
+
+                // Filters meshes based on custom predicate function.
+                if (predicate && !predicate(childMesh)) {
+                    continue;
+                }
+
+                //make sure we have the needed params to get mix and max
+                if (!childMesh.getBoundingInfo || childMesh.getTotalVertices() === 0) {
+                    continue;
+                }
+
+                let childBoundingInfo = childMesh.getBoundingInfo();
+                let boundingBox = childBoundingInfo.boundingBox;
+
+                var minBox = boundingBox.minimumWorld;
+                var maxBox = boundingBox.maximumWorld;
+
+                Tools.CheckExtends(minBox, min, max);
+                Tools.CheckExtends(maxBox, min, max);
+            }
+        }
+
+        return {
+            min: min,
+            max: max
+        };
+    }
 }