Browse Source

Add an option to force bounding box computation

Popov72 5 years ago
parent
commit
f7e3fa9812
2 changed files with 24 additions and 13 deletions
  1. 19 13
      loaders/src/glTF/2.0/glTFLoader.ts
  2. 5 0
      loaders/src/glTF/glTFFileLoader.ts

+ 19 - 13
loaders/src/glTF/2.0/glTFLoader.ts

@@ -689,22 +689,28 @@ export class GLTFLoader implements IGLTFLoader {
         return Promise.all(promises).then(() => {
             const min = TmpVectors.Vector3[0], max = TmpVectors.Vector3[1];
             this._forEachPrimitive(node, (babylonMesh) => {
-                const tmp = GLTFLoader.GetTmpMetadata(babylonMesh);
-                const mmin: [number, number, number] = tmp.positionMin, mmax: [number, number, number] = tmp.positionMax;
-                if (mmin !== undefined && mmax !== undefined) {
-                    min.copyFromFloats(...mmin);
-                    max.copyFromFloats(...mmax);
-                    babylonMesh.getBoundingInfo().reConstruct(min, max);
-                    if (babylonMesh.subMeshes) {
-                        for (let index = 0; index < babylonMesh.subMeshes.length; index++) {
-                            babylonMesh.subMeshes[index].getBoundingInfo().reConstruct(min, max);
+                let recomputeBoundingInfo = true;
+                if (!this.parent.alwaysComputeBoundingBox) {
+                    const tmp = GLTFLoader.GetTmpMetadata(babylonMesh);
+                    const mmin: [number, number, number] = tmp.positionMin, mmax: [number, number, number] = tmp.positionMax;
+                    if (mmin !== undefined && mmax !== undefined) {
+                        recomputeBoundingInfo = false;
+                        min.copyFromFloats(...mmin);
+                        max.copyFromFloats(...mmax);
+                        babylonMesh.getBoundingInfo().reConstruct(min, max);
+                        if (babylonMesh.subMeshes) {
+                            for (let index = 0; index < babylonMesh.subMeshes.length; index++) {
+                                babylonMesh.subMeshes[index].getBoundingInfo().reConstruct(min, max);
+                            }
                         }
+                        babylonMesh._updateBoundingInfo();
                     }
-                    babylonMesh._updateBoundingInfo();
-                } else {
+                }
+                if (recomputeBoundingInfo) {
                     babylonMesh.refreshBoundingInfo(true);
+                } else {
+                    GLTFLoader.RemoveTmpMetadata(babylonMesh);
                 }
-                GLTFLoader.RemoveTmpMetadata(babylonMesh);
             });
 
             return node._babylonTransformNode!;
@@ -883,7 +889,7 @@ export class GLTFLoader implements IGLTFLoader {
                 babylonGeometry.setVerticesBuffer(babylonVertexBuffer, accessor.count);
             }));
 
-            if (attribute === "POSITION" && accessor.min !== undefined && accessor.max !== undefined) {
+            if (attribute === "POSITION" && !this.parent.alwaysComputeBoundingBox && accessor.min !== undefined && accessor.max !== undefined) {
                 const tmp = GLTFLoader.GetTmpMetadata(babylonMesh);
                 tmp.positionMin = accessor.min;
                 tmp.positionMax = accessor.max;

+ 5 - 0
loaders/src/glTF/glTFFileLoader.ts

@@ -234,6 +234,11 @@ export class GLTFFileLoader implements IDisposable, ISceneLoaderPluginAsync, ISc
     public createInstances = true;
 
     /**
+     * Defines if the loader should always compute the bounding boxes of meshes and not use the min/max values from the position accessor. Defaults to false.
+     */
+    public alwaysComputeBoundingBox = false;
+
+    /**
      * Function called before loading a url referenced by the asset.
      */
     public preprocessUrlAsync = (url: string) => Promise.resolve(url);