Browse Source

PluginAnimationLoop

David Sehnal 4 năm trước cách đây
mục cha
commit
eeed48a1f7

+ 11 - 3
src/mol-canvas3d/canvas3d.ts

@@ -106,9 +106,11 @@ interface Canvas3D {
     syncVisibility(): void
 
     requestDraw(force?: boolean): void
+
+    /** Reset the timers, used by "animate" */
+    resetTime(t: number): void
     animate(): void
     pause(): void
-    setCurrentTime(t: number): void
     identify(x: number, y: number): PickData | undefined
     mark(loci: Representation.Loci, action: MarkerAction): void
     getLoci(pickingId: PickingId | undefined): Representation.Loci
@@ -204,7 +206,7 @@ namespace Canvas3D {
         const reprUpdatedSubscriptions = new Map<Representation.Any, Subscription>();
         const reprCount = new BehaviorSubject(0);
 
-        const startTime = now();
+        let startTime = now();
         const didDraw = new BehaviorSubject<now.Timestamp>(0 as now.Timestamp);
 
         const { gl, contextRestored } = webgl;
@@ -344,7 +346,13 @@ namespace Canvas3D {
             animationFrameHandle = requestAnimationFrame(_animate);
         }
 
+        function resetTime(t: now.Timestamp) {
+            startTime = t;
+            controls.start(t);
+        }
+
         function animate() {
+            controls.start(now());
             if (animationFrameHandle === 0) _animate();
         }
 
@@ -567,8 +575,8 @@ namespace Canvas3D {
             requestDraw,
             tick,
             animate,
+            resetTime,
             pause,
-            setCurrentTime: t => currentTime = t,
             identify,
             mark,
             getLoci,

+ 7 - 3
src/mol-canvas3d/controls/trackball.ts

@@ -60,6 +60,7 @@ interface TrackballControls {
     readonly props: Readonly<TrackballControlsProps>
     setProps: (props: Partial<TrackballControlsProps>) => void
 
+    start: (t: number) => void
     update: (t: number) => void
     reset: () => void
     dispose: () => void
@@ -286,7 +287,7 @@ namespace TrackballControls {
         /** Update the object's position, direction and up vectors */
         function update(t: number) {
             if (lastUpdated === t) return;
-            if (p.spin) spin(t - lastUpdated);
+            if (p.spin && lastUpdated > 0) spin(t - lastUpdated);
 
             Vec3.sub(_eye, camera.position, camera.target);
 
@@ -412,8 +413,10 @@ namespace TrackballControls {
             if (!_isInteracting) Vec2.add(_rotCurr, _rotPrev, _spinSpeed);
         }
 
-        // force an update at start
-        update(0);
+        function start(t: number) {
+            lastUpdated = -1;
+            update(t);
+        }
 
         return {
             viewport,
@@ -423,6 +426,7 @@ namespace TrackballControls {
                 Object.assign(p, props);
             },
 
+            start,
             update,
             reset,
             dispose

+ 5 - 0
src/mol-plugin/animation-loop.ts

@@ -23,8 +23,13 @@ export class PluginAnimationLoop {
         }
     }
 
+    resetTime() {
+        this.plugin.canvas3d?.resetTime(now());
+    }
+
     start() {
         this._isAnimating = true;
+        this.resetTime();
         this.currentFrame = requestAnimationFrame(this.frame);
     }