Selaa lähdekoodia

mol-canvas3d: shouldResetCamera check

David Sehnal 5 vuotta sitten
vanhempi
commit
c3a90ab499
1 muutettua tiedostoa jossa 23 lisäystä ja 1 poistoa
  1. 23 1
      src/mol-canvas3d/canvas3d.ts

+ 23 - 1
src/mol-canvas3d/canvas3d.ts

@@ -313,16 +313,38 @@ namespace Canvas3D {
             cameraResetRequested = false;
         }
 
+        const oldBoundary = Sphere3D.zero();
+        function shouldResetCamera() {
+            if (camera.state.radiusMax === 0) return true;
+
+            // check if any renderable center has moved outside of the old boundary
+            for (const r of scene.renderables) {
+                if (!r.state.visible) continue;
+                const { center, radius } = r.values.boundingSphere.ref.value;
+                if (!radius) continue;
+                // TODO: include renderable radius into this?
+                if (Vec3.distance(oldBoundary.center, center) > 1.1 * oldBoundary.radius) {
+                    return true;
+                }
+            }
+            return false;
+        }
+
         const sceneCommitTimeoutMs = 250;
         function commitScene(isSynchronous: boolean) {
             if (!scene.needsCommit) return true;
 
+            // snapshot the current bounding sphere
+            Sphere3D.copy(oldBoundary, scene.boundingSphere);
+
             if (!scene.commit(isSynchronous ? void 0 : sceneCommitTimeoutMs)) return false;
 
             if (debugHelper.isEnabled) debugHelper.update();
-            if (reprCount.value === 0 || camera.state.radiusMax === 0) cameraResetRequested = true;
+            if (reprCount.value === 0 || shouldResetCamera()) cameraResetRequested = true;
+
             camera.setState({ radiusMax: scene.boundingSphere.radius })
             reprCount.next(reprRenderObjects.size);
+
             return true;
         }