diff --git a/changelog.d/1752-uncapped-frame-rate.md b/changelog.d/1752-uncapped-frame-rate.md index 74c827929..db09e754b 100644 --- a/changelog.d/1752-uncapped-frame-rate.md +++ b/changelog.d/1752-uncapped-frame-rate.md @@ -1,3 +1,3 @@ ### Changed -- While anything moves (layers in motion, the camera, a followed contact), the globe now draws at the display's own refresh rate instead of a fixed 60 frames a second. A parked scene still ticks at most 60 times a second. Waits that counted frames (a followed contact's first position, a shared follow view, a trail's temporary copy) now count drawn time, so they last the same at any refresh rate. +- While anything moves (layers in motion, the camera, a followed contact), the globe now draws at the display's own refresh rate instead of a fixed 60 frames a second. A parked scene still ticks at most 60 times a second. Waits that counted frames (a followed contact's first position, a shared follow view, a trail's temporary copy) now count drawn time, so they last the same at any refresh rate. Screen marks over a followed contact fade out over the same time at any refresh rate. diff --git a/src/annotations/screenAnnotationRenderer.js b/src/annotations/screenAnnotationRenderer.js index 0509a47a5..d886fe1de 100644 --- a/src/annotations/screenAnnotationRenderer.js +++ b/src/annotations/screenAnnotationRenderer.js @@ -399,7 +399,12 @@ export function createScreenAnnotationRenderer( // from the host's ACTUAL painted rectangle after final layout; the billboard extent // is unioned for aircraft/satellites that also own a native tracked graphic. const TRACKED_BBOX_MARGIN = 8; // px buffer added around the footprint - const TRACKED_FADE_EASE = 0.22; // per-frame ease toward hidden(0) / visible(1) + // Ease toward hidden(0) / visible(1): this share of the gap closes in one + // 60 Hz frame (about 200 ms to settle), scaled by elapsed time so the fade + // takes as long at any refresh rate. + const TRACKED_FADE_EASE = 0.22; + const TRACKED_FADE_FRAME_MS = 1000 / 60; + let trackedFadeAtMs = NaN; const TRACKED_HYSTERESIS_PX = 18; // dead-band so the overlap test can't flip-flop const _scratchTrackedWin = new Cesium.Cartesian2(); @@ -676,7 +681,17 @@ export function createScreenAnnotationRenderer( // footprints live in the world layer — so fully hiding an overlapped one is // safe and not jarring. function fadeTracked(trackedRect) { - if (!shown()) return; + if (!shown()) { + trackedFadeAtMs = NaN; + return; + } + const nowMs = performance.now(); + const stepMs = Number.isFinite(trackedFadeAtMs) + ? Math.min(Math.max(nowMs - trackedFadeAtMs, 0), 100) + : TRACKED_FADE_FRAME_MS; + trackedFadeAtMs = nowMs; + const ease = + 1 - Math.pow(1 - TRACKED_FADE_EASE, stepMs / TRACKED_FADE_FRAME_MS); let changed = false; for (const rec of records.values()) { const { anno, group } = rec; @@ -705,10 +720,10 @@ export function createScreenAnnotationRenderer( } } const target = overlapping ? 0 : 1; - const next = cur + (target - cur) * TRACKED_FADE_EASE; + const next = cur + (target - cur) * ease; rec._trackedFade = Math.abs(next - target) < 0.02 ? target : next; // The base alpha applies unless faded. (The CSS `transition: opacity` was - // removed from .gev-anno so this per-frame JS ease is the ONLY easing — + // removed from .gev-anno so this JS ease is the ONLY easing — // previously the two fought and produced an opacity oscillation.) const opacity = String( (anno.alpha ?? 1) * (rec._trackedFade !== 1 ? rec._trackedFade : 1), diff --git a/src/renderGovernor.js b/src/renderGovernor.js index 85b4eb5ae..1fc953307 100644 --- a/src/renderGovernor.js +++ b/src/renderGovernor.js @@ -69,7 +69,6 @@ function applyMode() { const CAMERA_SAMPLE_MS = 250; const CAMERA_POSITION_TOLERANCE_M = 1e-3; const CAMERA_DIRECTION_TOLERANCE = 1e-9; -const CAMERA_GIVE_UP_MS = 5 * 60_000; function near(a, b, tolerance) { return ( @@ -86,9 +85,9 @@ function near(a, b, tolerance) { * a rounding error every frame, so `moveEnd` never comes (see * `installGlobeInViewRecovery` in app/viewer.js). So from `moveStart` until * `moveEnd` the pose is also sampled every 250 ms with a tolerance for that - * drift: half a second at rest releases the hold, a later move takes it - * again (Cesium raises no new `moveStart` under that drift), and sampling - * stops five minutes after the camera last moved. + * drift: half a second at rest releases the hold and a later move takes it + * again (Cesium raises no new `moveStart` under that drift). Sampling runs + * until `moveEnd`, however long that takes; it costs four checks a second. * @returns {(() => void)|null} Removes the listeners and the sampling. */ function watchCameraMotion(camera, { schedule, unschedule }) { @@ -96,7 +95,6 @@ function watchCameraMotion(camera, { schedule, unschedule }) { const last = { position: null, direction: null }; let timer = null; let restingSamples = 0; - let restingMs = 0; const copy = (v) => (v ? { x: v.x, y: v.y, z: v.z } : null); const remember = () => { last.position = copy(camera.positionWC); @@ -118,12 +116,9 @@ function watchCameraMotion(camera, { schedule, unschedule }) { near(direction, last.direction, CAMERA_DIRECTION_TOLERANCE)); if (atRest) { restingSamples += 1; - restingMs += CAMERA_SAMPLE_MS; if (restingSamples >= 2) releaseContinuousRender('camera'); - if (restingMs >= CAMERA_GIVE_UP_MS) return; } else { restingSamples = 0; - restingMs = 0; holdContinuousRender('camera'); } remember(); @@ -133,7 +128,6 @@ function watchCameraMotion(camera, { schedule, unschedule }) { holdContinuousRender('camera'); stopSampling(); restingSamples = 0; - restingMs = 0; remember(); timer = schedule(sample, CAMERA_SAMPLE_MS); }); @@ -175,6 +169,7 @@ export function installRenderGovernor( // explicit requests. viewer.scene.maximumRenderTimeChange = Infinity; _removeCameraListeners?.(); + _holds.delete('camera'); _removeCameraListeners = watchCameraMotion( viewer.camera ?? viewer.scene.camera, timers, diff --git a/src/renderGovernor.test.mjs b/src/renderGovernor.test.mjs index 3d43f015d..7cd1a60a4 100644 --- a/src/renderGovernor.test.mjs +++ b/src/renderGovernor.test.mjs @@ -197,3 +197,35 @@ test('a camera that drifts by rounding errors and never raises moveEnd still rel assert.equal(scene.requestRenderMode, true); assert.equal(timers.pending, 0); }); + +test('sampling keeps running until moveEnd, so a move after a long drift still takes the hold', () => { + const { viewer, scene } = makeViewer(); + const timers = fakeTimers(); + viewer.camera = { + moveStart: makeEvent(), + moveEnd: makeEvent(), + positionWC: { x: 1, y: 0, z: 0 }, + directionWC: { x: -1, y: 0, z: 0 }, + }; + installRenderGovernor(viewer, timers); + viewer.camera.moveStart.raise(); + // Ten minutes at rest (2,400 samples): released, still sampling. + for (let i = 0; i < 2_400; i++) timers.run(); + assert.equal(scene.requestRenderMode, true); + assert.equal(timers.pending, 1); + viewer.camera.positionWC = { x: 1_000, y: 0, z: 0 }; + timers.run(); + assert.equal(scene.requestRenderMode, false); +}); + +test('installing again drops a camera hold the old listeners can no longer release', () => { + const { viewer, scene } = makeViewer(); + const timers = fakeTimers(); + viewer.camera = { moveStart: makeEvent(), moveEnd: makeEvent() }; + installRenderGovernor(viewer, timers); + viewer.camera.moveStart.raise(); + assert.equal(scene.requestRenderMode, false); + installRenderGovernor(viewer, timers); + assert.equal(scene.requestRenderMode, true); + assert.equal(timers.pending, 0); +});