瀏覽代碼

Added support for mesh.normalizeToUnitCube

David Catuhe 7 年之前
父節點
當前提交
b14e857542

文件差異過大導致無法顯示
+ 979 - 973
dist/preview release/babylon.d.ts


文件差異過大導致無法顯示
+ 17 - 17
dist/preview release/babylon.js


+ 34 - 14
dist/preview release/babylon.max.js

@@ -13714,6 +13714,22 @@ var BABYLON;
             return this._boundingInfo;
         };
         /**
+         * Uniformly scales the mesh to fit inside of a unit cube (1 X 1 X 1 units).
+         * @param includeDescendants Take the hierarchy's bounding box instead of the mesh's bounding box.
+         */
+        AbstractMesh.prototype.normalizeToUnitCube = function (includeDescendants) {
+            if (includeDescendants === void 0) { includeDescendants = true; }
+            var boundingVectors = this.getHierarchyBoundingVectors(includeDescendants);
+            var sizeVec = boundingVectors.max.subtract(boundingVectors.min);
+            var maxDimension = Math.max(sizeVec.x, sizeVec.y, sizeVec.z);
+            if (maxDimension === 0) {
+                return this;
+            }
+            var scale = 1 / maxDimension;
+            this.scaling.scaleInPlace(scale);
+            return this;
+        };
+        /**
          * Sets a mesh new object BoundingInfo.
          * Returns the AbstractMesh.
          */
@@ -14071,8 +14087,10 @@ var BABYLON;
         };
         /**
          * 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).
          */
-        AbstractMesh.prototype.getHierarchyBoundingVectors = function () {
+        AbstractMesh.prototype.getHierarchyBoundingVectors = function (includeDescendants) {
+            if (includeDescendants === void 0) { includeDescendants = true; }
             this.computeWorldMatrix(true);
             var min;
             var max;
@@ -14085,20 +14103,22 @@ var BABYLON;
                 min = boundingInfo.boundingBox.minimumWorld;
                 max = boundingInfo.boundingBox.maximumWorld;
             }
-            var descendants = this.getDescendants(false);
-            for (var _i = 0, descendants_1 = descendants; _i < descendants_1.length; _i++) {
-                var descendant = descendants_1[_i];
-                var childMesh = descendant;
-                childMesh.computeWorldMatrix(true);
-                var childBoundingInfo = childMesh.getBoundingInfo();
-                if (childMesh.getTotalVertices() === 0 || !childBoundingInfo) {
-                    continue;
+            if (includeDescendants) {
+                var descendants = this.getDescendants(false);
+                for (var _i = 0, descendants_1 = descendants; _i < descendants_1.length; _i++) {
+                    var descendant = descendants_1[_i];
+                    var childMesh = descendant;
+                    childMesh.computeWorldMatrix(true);
+                    var childBoundingInfo = childMesh.getBoundingInfo();
+                    if (childMesh.getTotalVertices() === 0 || !childBoundingInfo) {
+                        continue;
+                    }
+                    var boundingBox = childBoundingInfo.boundingBox;
+                    var minBox = boundingBox.minimumWorld;
+                    var maxBox = boundingBox.maximumWorld;
+                    BABYLON.Tools.CheckExtends(minBox, min, max);
+                    BABYLON.Tools.CheckExtends(maxBox, min, max);
                 }
-                var boundingBox = childBoundingInfo.boundingBox;
-                var minBox = boundingBox.minimumWorld;
-                var maxBox = boundingBox.maximumWorld;
-                BABYLON.Tools.CheckExtends(minBox, min, max);
-                BABYLON.Tools.CheckExtends(maxBox, min, max);
             }
             return {
                 min: min,

文件差異過大導致無法顯示
+ 979 - 973
dist/preview release/babylon.module.d.ts


文件差異過大導致無法顯示
+ 17 - 17
dist/preview release/babylon.worker.js


文件差異過大導致無法顯示
+ 2607 - 2601
dist/preview release/customConfigurations/minimalGLTFViewer/babylon.d.ts


文件差異過大導致無法顯示
+ 17 - 17
dist/preview release/customConfigurations/minimalGLTFViewer/babylon.js


+ 34 - 14
dist/preview release/customConfigurations/minimalGLTFViewer/babylon.max.js

@@ -13714,6 +13714,22 @@ var BABYLON;
             return this._boundingInfo;
         };
         /**
+         * Uniformly scales the mesh to fit inside of a unit cube (1 X 1 X 1 units).
+         * @param includeDescendants Take the hierarchy's bounding box instead of the mesh's bounding box.
+         */
+        AbstractMesh.prototype.normalizeToUnitCube = function (includeDescendants) {
+            if (includeDescendants === void 0) { includeDescendants = true; }
+            var boundingVectors = this.getHierarchyBoundingVectors(includeDescendants);
+            var sizeVec = boundingVectors.max.subtract(boundingVectors.min);
+            var maxDimension = Math.max(sizeVec.x, sizeVec.y, sizeVec.z);
+            if (maxDimension === 0) {
+                return this;
+            }
+            var scale = 1 / maxDimension;
+            this.scaling.scaleInPlace(scale);
+            return this;
+        };
+        /**
          * Sets a mesh new object BoundingInfo.
          * Returns the AbstractMesh.
          */
@@ -14071,8 +14087,10 @@ var BABYLON;
         };
         /**
          * 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).
          */
-        AbstractMesh.prototype.getHierarchyBoundingVectors = function () {
+        AbstractMesh.prototype.getHierarchyBoundingVectors = function (includeDescendants) {
+            if (includeDescendants === void 0) { includeDescendants = true; }
             this.computeWorldMatrix(true);
             var min;
             var max;
@@ -14085,20 +14103,22 @@ var BABYLON;
                 min = boundingInfo.boundingBox.minimumWorld;
                 max = boundingInfo.boundingBox.maximumWorld;
             }
-            var descendants = this.getDescendants(false);
-            for (var _i = 0, descendants_1 = descendants; _i < descendants_1.length; _i++) {
-                var descendant = descendants_1[_i];
-                var childMesh = descendant;
-                childMesh.computeWorldMatrix(true);
-                var childBoundingInfo = childMesh.getBoundingInfo();
-                if (childMesh.getTotalVertices() === 0 || !childBoundingInfo) {
-                    continue;
+            if (includeDescendants) {
+                var descendants = this.getDescendants(false);
+                for (var _i = 0, descendants_1 = descendants; _i < descendants_1.length; _i++) {
+                    var descendant = descendants_1[_i];
+                    var childMesh = descendant;
+                    childMesh.computeWorldMatrix(true);
+                    var childBoundingInfo = childMesh.getBoundingInfo();
+                    if (childMesh.getTotalVertices() === 0 || !childBoundingInfo) {
+                        continue;
+                    }
+                    var boundingBox = childBoundingInfo.boundingBox;
+                    var minBox = boundingBox.minimumWorld;
+                    var maxBox = boundingBox.maximumWorld;
+                    BABYLON.Tools.CheckExtends(minBox, min, max);
+                    BABYLON.Tools.CheckExtends(maxBox, min, max);
                 }
-                var boundingBox = childBoundingInfo.boundingBox;
-                var minBox = boundingBox.minimumWorld;
-                var maxBox = boundingBox.maximumWorld;
-                BABYLON.Tools.CheckExtends(minBox, min, max);
-                BABYLON.Tools.CheckExtends(maxBox, min, max);
             }
             return {
                 min: min,

文件差異過大導致無法顯示
+ 2607 - 2601
dist/preview release/customConfigurations/minimalGLTFViewer/babylon.module.d.ts


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

@@ -22,7 +22,8 @@
 - Complete rework of Unity3D exporter. [Doc here](http://doc.babylonjs.com/resources/intro) ([MackeyK24](https://github.com/MackeyK24))
 
 ## Updates
-- Added scene.onDataLoadedObservable which is raised when SceneLoader.Append or SceneLoader.Load or SceneLoader.ImportMesh were successfully executed ([deltakosh](https://github.com/deltakosh))
+- Added `mesh.normalizeToUnitCube` to uniformly scales the mesh to fit inside of a unit cube (1 X 1 X 1 units) ([deltakosh](https://github.com/deltakosh))
+- Added `scene.onDataLoadedObservable` which is raised when SceneLoader.Append or SceneLoader.Load or SceneLoader.ImportMesh were successfully executed ([deltakosh](https://github.com/deltakosh))
 - Support for adaptiveKernelBlur on MirrorTexture ([deltakosh](https://github.com/deltakosh))
 - Support for non uniform scaling. Normals are now correctly computed ([deltakosh](https://github.com/deltakosh))
 - Added `MultiObserver`. [Doc here](http://doc.babylonjs.com/overviews/observables#multiobserver) ([deltakosh](https://github.com/deltakosh))

+ 38 - 14
src/Mesh/babylon.abstractMesh.ts

@@ -208,6 +208,7 @@
         public enablePointerMoveEvents = false;
         public renderingGroupId = 0;
         private _material: Nullable<Material>
+
         public get material(): Nullable<Material> {
             return this._material;
         }
@@ -797,6 +798,26 @@
         }
 
         /**
+         * Uniformly scales the mesh to fit inside of a unit cube (1 X 1 X 1 units).
+         * @param includeDescendants Take the hierarchy's bounding box instead of the mesh's bounding box.
+         */
+        public normalizeToUnitCube(includeDescendants = true): AbstractMesh {            
+            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);
+
+            return this;
+        }
+
+        /**
          * Sets a mesh new object BoundingInfo.
          * Returns the AbstractMesh.  
          */
@@ -1176,8 +1197,9 @@
 
         /**
          * 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).
          */
-        public getHierarchyBoundingVectors(): { min: Vector3, max: Vector3 }{
+        public getHierarchyBoundingVectors(includeDescendants = true): { min: Vector3, max: Vector3 }{
             this.computeWorldMatrix(true);
 
             let min: Vector3;
@@ -1192,24 +1214,26 @@
                 max = boundingInfo.boundingBox.maximumWorld;
             }
 
-            let descendants = this.getDescendants(false);
+            if (includeDescendants) {
+                let descendants = this.getDescendants(false);
 
-            for (var descendant of descendants) {
-                let childMesh = <AbstractMesh>descendant;
+                for (var descendant of descendants) {
+                    let childMesh = <AbstractMesh>descendant;
 
-                childMesh.computeWorldMatrix(true);
-                let childBoundingInfo = childMesh.getBoundingInfo();
+                    childMesh.computeWorldMatrix(true);
+                    let childBoundingInfo = childMesh.getBoundingInfo();
 
-                if (childMesh.getTotalVertices() === 0 || !childBoundingInfo) {
-                    continue;
-                }
-                let boundingBox = childBoundingInfo.boundingBox;
+                    if (childMesh.getTotalVertices() === 0 || !childBoundingInfo) {
+                        continue;
+                    }
+                    let boundingBox = childBoundingInfo.boundingBox;
 
-                var minBox = boundingBox.minimumWorld;
-                var maxBox = boundingBox.maximumWorld;
+                    var minBox = boundingBox.minimumWorld;
+                    var maxBox = boundingBox.maximumWorld;
 
-                Tools.CheckExtends(minBox, min, max);
-                Tools.CheckExtends(maxBox, min, max);
+                    Tools.CheckExtends(minBox, min, max);
+                    Tools.CheckExtends(maxBox, min, max);
+                }
             }
 
             return {