Browse Source

Move normalizeToUnitCube to transformNode instead of abstract mesh and add predicate to exclude sub objects when scaling

Trevor Baron 6 years ago
parent
commit
d95b4c19d6
3 changed files with 49 additions and 36 deletions
  1. 1 0
      dist/preview release/what's new.md
  2. 3 36
      src/Meshes/abstractMesh.ts
  3. 45 0
      src/Meshes/transformNode.ts

+ 1 - 0
dist/preview release/what's new.md

@@ -17,6 +17,7 @@
 - Unify preparation of instance attributes. Added `MaterialHelper.PushAttributesForInstances` ([MarkusBillharz](https://github.com/MarkusBillharz))
 - Added support for PBR [irradiance map](https://doc.babylonjs.com/how_to/physically_based_rendering_master#irradiance-map)
 - Ability to set render camera on utility layer instead of using the latest active camera ([TrevorDev](https://github.com/TrevorDev))
+- Move normalizeToUnitCube to transformNode instead of abstract mesh and add predicate to exclude sub objects when scaling ([TrevorDev](https://github.com/TrevorDev))
 
 ### Engine
 - Added preprocessors for shaders to improve how shaders are compiled for WebGL1/2 or WebGPU ([Deltakosh](https://github.com/deltakosh/))

+ 3 - 36
src/Meshes/abstractMesh.ts

@@ -981,45 +981,12 @@ export class AbstractMesh extends TransformNode implements IDisposable, ICullabl
      * Uniformly scales the mesh to fit inside of a unit cube (1 X 1 X 1 units)
      * @param includeDescendants Use the hierarchy's bounding box instead of the mesh's bounding box. Default is false
      * @param ignoreRotation ignore rotation when computing the scale (ie. object will be axis aligned). Default is false
+     * @param predicate predicate that is passed in to getHierarchyBoundingVectors when selecting which object should be included when scaling
      * @returns the current mesh
      */
-    public normalizeToUnitCube(includeDescendants = true, ignoreRotation = false): AbstractMesh {
-        let storedRotation: Nullable<Vector3> = null;
-        let storedRotationQuaternion: Nullable<Quaternion> = null;
-
-        if (ignoreRotation) {
-            if (this.rotationQuaternion) {
-                storedRotationQuaternion = this.rotationQuaternion.clone();
-                this.rotationQuaternion.copyFromFloats(0, 0, 0, 1);
-            } else if (this.rotation) {
-                storedRotation = this.rotation.clone();
-                this.rotation.copyFromFloats(0, 0, 0);
-            }
-        }
-
-        let boundingVectors = this.getHierarchyBoundingVectors(includeDescendants);
-        let sizeVec = boundingVectors.max.subtract(boundingVectors.min);
-        let maxDimension = Math.max(sizeVec.x, sizeVec.y, sizeVec.z);
-
-        if (maxDimension === 0) {
-            return this;
-        }
-
-        let scale = 1 / maxDimension;
-
-        this.scaling.scaleInPlace(scale);
-
-        if (ignoreRotation) {
-            if (this.rotationQuaternion && storedRotationQuaternion) {
-                this.rotationQuaternion.copyFrom(storedRotationQuaternion);
-            } else if (this.rotation && storedRotation) {
-                this.rotation.copyFrom(storedRotation);
-            }
-        }
-
-        return this;
+    public normalizeToUnitCube(includeDescendants = true, ignoreRotation = false, predicate?: Nullable<(node: AbstractMesh) => boolean>): AbstractMesh {
+        return <AbstractMesh>super.normalizeToUnitCube(includeDescendants, ignoreRotation, predicate);
     }
-
     /**
      * Overwrite the current bounding info
      * @param boundingInfo defines the new bounding info

+ 45 - 0
src/Meshes/transformNode.ts

@@ -8,6 +8,7 @@ import { Scene } from "../scene";
 import { Quaternion, Matrix, Vector3, Tmp, Space } from "../Maths/math";
 import { Node } from "../node";
 import { Bone } from "../Bones/bone";
+import { AbstractMesh } from '../Meshes/abstractMesh';
 /**
  * A TransformNode is an object that is not rendered but can be used as a center of transformation. This can decrease memory usage and increase rendering speed compared to using an empty mesh as a parent and is less complicated than using a pivot matrix.
  * @see https://doc.babylonjs.com/how_to/transformnode
@@ -1264,4 +1265,48 @@ export class TransformNode extends Node {
 
         super.dispose(doNotRecurse, disposeMaterialAndTextures);
     }
+
+    /**
+     * Uniformly scales the mesh to fit inside of a unit cube (1 X 1 X 1 units)
+     * @param includeDescendants Use the hierarchy's bounding box instead of the mesh's bounding box. Default is false
+     * @param ignoreRotation ignore rotation when computing the scale (ie. object will be axis aligned). Default is false
+     * @param predicate predicate that is passed in to getHierarchyBoundingVectors when selecting which object should be included when scaling
+     * @returns the current mesh
+     */
+    public normalizeToUnitCube(includeDescendants = true, ignoreRotation = false, predicate?: Nullable<(node: AbstractMesh) => boolean>): TransformNode {
+        let storedRotation: Nullable<Vector3> = null;
+        let storedRotationQuaternion: Nullable<Quaternion> = null;
+
+        if (ignoreRotation) {
+            if (this.rotationQuaternion) {
+                storedRotationQuaternion = this.rotationQuaternion.clone();
+                this.rotationQuaternion.copyFromFloats(0, 0, 0, 1);
+            } else if (this.rotation) {
+                storedRotation = this.rotation.clone();
+                this.rotation.copyFromFloats(0, 0, 0);
+            }
+        }
+
+        let boundingVectors = this.getHierarchyBoundingVectors(includeDescendants, predicate);
+        let sizeVec = boundingVectors.max.subtract(boundingVectors.min);
+        let maxDimension = Math.max(sizeVec.x, sizeVec.y, sizeVec.z);
+
+        if (maxDimension === 0) {
+            return this;
+        }
+
+        let scale = 1 / maxDimension;
+
+        this.scaling.scaleInPlace(scale);
+
+        if (ignoreRotation) {
+            if (this.rotationQuaternion && storedRotationQuaternion) {
+                this.rotationQuaternion.copyFrom(storedRotationQuaternion);
+            } else if (this.rotation && storedRotation) {
+                this.rotation.copyFrom(storedRotation);
+            }
+        }
+
+        return this;
+    }
 }