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

Another try and adding back useBoundingInfoFromGeometry

Popov72 5 éve
szülő
commit
8b259c835f
2 módosított fájl, 26 hozzáadás és 22 törlés
  1. 3 2
      loaders/src/glTF/2.0/glTFLoader.ts
  2. 23 20
      src/Meshes/geometry.ts

+ 3 - 2
loaders/src/glTF/2.0/glTFLoader.ts

@@ -689,7 +689,7 @@ export class GLTFLoader implements IGLTFLoader {
 
         return Promise.all(promises).then(() => {
             this._forEachPrimitive(node, (babylonMesh) => {
-                if ((babylonMesh as Mesh).geometry && (babylonMesh as Mesh).geometry!._boundingInfo) {
+                if ((babylonMesh as Mesh).geometry && (babylonMesh as Mesh).geometry!.useBoundingInfoFromGeometry) {
                     // simply apply the world matrices to the bounding info - the extends are already ok
                     babylonMesh._updateBoundingInfo();
                 } else {
@@ -870,7 +870,6 @@ export class GLTFLoader implements IGLTFLoader {
 
             const accessor = ArrayItem.Get(`${context}/attributes/${attribute}`, this._gltf.accessors, attributes[attribute]);
             promises.push(this._loadVertexAccessorAsync(`/accessors/${accessor.index}`, accessor, kind).then((babylonVertexBuffer) => {
-                babylonGeometry.setVerticesBuffer(babylonVertexBuffer, accessor.count);
                 if (babylonVertexBuffer.getKind() === VertexBuffer.PositionKind && !this.parent.alwaysComputeBoundingBox && !babylonMesh.skeleton) {
                     const mmin = accessor.min as [number, number, number], mmax = accessor.max as [number, number, number];
                     if (mmin !== undefined && mmax !== undefined) {
@@ -878,8 +877,10 @@ export class GLTFLoader implements IGLTFLoader {
                         min.copyFromFloats(...mmin);
                         max.copyFromFloats(...mmax);
                         babylonGeometry._boundingInfo = new BoundingInfo(min, max);
+                        babylonGeometry.useBoundingInfoFromGeometry = true;
                     }
                 }
+                babylonGeometry.setVerticesBuffer(babylonVertexBuffer, accessor.count);
             }));
 
             if (kind == VertexBuffer.MatricesIndicesExtraKind) {

+ 23 - 20
src/Meshes/geometry.ts

@@ -113,6 +113,12 @@ export class Geometry implements IGetSetVerticesData {
     }
 
     /**
+     * If set to true (false by defaut), the bounding info applied to the meshes sharing this geometry will be the bounding info defined at the class level
+     * and won't be computed based on the vertex positions (which is what we get when useBoundingInfoFromGeometry = false)
+     */
+    public useBoundingInfoFromGeometry = false;
+
+    /**
      * Creates a new geometry
      * @param id defines the unique ID
      * @param scene defines the hosting scene
@@ -275,12 +281,9 @@ export class Geometry implements IGetSetVerticesData {
             var meshes = this._meshes;
             var numOfMeshes = meshes.length;
 
-            var minimum = this._boundingInfo?.minimum ?? this._extend.minimum;
-            var maximum = this._boundingInfo?.maximum ?? this._extend.maximum;
-
             for (var index = 0; index < numOfMeshes; index++) {
                 var mesh = meshes[index];
-                mesh._boundingInfo = new BoundingInfo(minimum, maximum);
+                mesh._boundingInfo = new BoundingInfo(this._extend.minimum, this._extend.maximum);
                 mesh._createGlobalSubMesh(false);
                 mesh.computeWorldMatrix(true);
             }
@@ -344,16 +347,13 @@ export class Geometry implements IGetSetVerticesData {
         this._resetPointsArrayCache();
 
         if (updateExtends) {
-            var minimum = this._boundingInfo?.minimum ?? this._extend.minimum;
-            var maximum = this._boundingInfo?.maximum ?? this._extend.maximum;
-
             var meshes = this._meshes;
             for (const mesh of meshes) {
                 if (mesh._boundingInfo) {
-                    mesh._boundingInfo.reConstruct(minimum, maximum);
+                    mesh._boundingInfo.reConstruct(this._extend.minimum, this._extend.maximum);
                 }
                 else {
-                    mesh._boundingInfo = new BoundingInfo(minimum, maximum);
+                    mesh._boundingInfo = new BoundingInfo(this._extend.minimum, this._extend.maximum);
                 }
 
                 const subMeshes = mesh.subMeshes;
@@ -702,11 +702,18 @@ export class Geometry implements IGetSetVerticesData {
     }
 
     private _updateExtend(data: Nullable<FloatArray> = null) {
-        if (!data) {
-            data = this.getVerticesData(VertexBuffer.PositionKind)!;
-        }
+        if (this.useBoundingInfoFromGeometry && this._boundingInfo) {
+            this._extend = {
+                minimum: this._boundingInfo.minimum.clone(),
+                maximum: this._boundingInfo.maximum.clone()
+            }
+        } else {
+            if (!data) {
+                data = this.getVerticesData(VertexBuffer.PositionKind)!;
+            }
 
-        this._extend = extractMinAndMax(data, 0, this._totalVertices, this.boundingBias, 3);
+            this._extend = extractMinAndMax(data, 0, this._totalVertices, this.boundingBias, 3);
+        }
     }
 
     private _applyToMesh(mesh: Mesh): void {
@@ -723,14 +730,10 @@ export class Geometry implements IGetSetVerticesData {
             }
 
             if (kind === VertexBuffer.PositionKind) {
-                if (this._boundingInfo) {
-                    mesh._boundingInfo = new BoundingInfo(this._boundingInfo.minimum, this._boundingInfo.maximum);
-                } else {
-                    if (!this._extend) {
-                        this._updateExtend();
-                    }
-                    mesh._boundingInfo = new BoundingInfo(this._extend.minimum, this._extend.maximum);
+                if (!this._extend) {
+                    this._updateExtend();
                 }
+                mesh._boundingInfo = new BoundingInfo(this._extend.minimum, this._extend.maximum);
 
                 mesh._createGlobalSubMesh(false);