diff --git a/src/layers/satellites/lifecycle.js b/src/layers/satellites/lifecycle.js index 3a4ba31cd4..553e59ee40 100644 --- a/src/layers/satellites/lifecycle.js +++ b/src/layers/satellites/lifecycle.js @@ -141,7 +141,7 @@ export function createLifecycle({ parts.rendering._setPointsShown(false); parts.rendering._setOrbitPathsShown(false); for (const model of parts.stationModels ?? []) - model._updateStationModel(); // off: hides the model + model._updateStationModel(); // off: releases the model parts.tracking._clearTracking(); parts.stationPanel?._syncStationPanel(); // off: takes the panel down parts.labels._syncIssOverlay(); diff --git a/src/layers/satellites/stationModel.js b/src/layers/satellites/stationModel.js index 274d2fa6b8..d4b6c6bf2c 100644 --- a/src/layers/satellites/stationModel.js +++ b/src/layers/satellites/stationModel.js @@ -64,8 +64,14 @@ import { stationTrackedOn } from './policy.js'; * @property {(() => void)|null} syncOrbit Puts the station's ring back to * its own rules on leaving the close-up; null for the layer's orbit toggle * (a ring that exists only while the station is tracked). + * @property {number} [unloadAfterMs] How long the model may go undrawn (out + * of range, or hidden) before its draw set is released and its GPU memory + * with it; asked for again on the next approach. Default one minute. */ +/** Default `unloadAfterMs`: a minute undrawn releases the model. */ +export const STATION_MODEL_UNLOAD_AFTER_MS = 60_000; + /** * Whether a station model may be drawn: the dot's own rules (layer on, points * on, the station loaded and its class shown or the station tracked), and @@ -245,6 +251,8 @@ export function createStationModel( const handedCompanions = new Set(); /** Whether the model draws and the docked dots are left to it. */ let handingOver = false; + /** Wall-clock ms since when the model has not been drawn, or null. */ + let undrawnSince = null; let lastCompanionScanMs = Number.NEGATIVE_INFINITY; /** Whether the station's orbit ring is hidden for the close-up. */ let orbitHidden = false; @@ -296,10 +304,12 @@ export function createStationModel( if (id !== itemId) return; ready = true; loading = false; + failed = false; }); offFailed = set.on('loadFailed', (id, message) => { if (id !== itemId) return; - // The dot keeps drawing at every distance; nothing else depends on this. + // The dots keep drawing at every distance (the model is never ready); + // the next approach after the model is released tries again. failed = true; loading = false; console.warn( @@ -619,6 +629,10 @@ export function createStationModel( if (handedCompanions.size) _releaseCompanions(); handingOver = false; _syncOrbitCloseUp(false); + // Off, the layer gives the model's memory back at once; hidden, after + // a while. + if (!layerState._enabled) _unload(); + else _undrawn(); return; } const dotPosition = parts.rendering._pointPosition(norad); @@ -639,6 +653,17 @@ export function createStationModel( _load(dotPosition); return; } + // Until the renderer has the model loaded and posed, the dots stand at + // every distance: the station never goes dark while the file loads, nor + // for good when it fails to. + if (!ready) { + _hide(); + _releaseDots(); + if (handedCompanions.size) _releaseCompanions(); + handingOver = false; + _syncOrbitCloseUp(false); + return; + } // The docked dots are the model's within its range: handed over at // once when the model starts drawing, then with each scan. if (!handingOver) { @@ -651,8 +676,10 @@ export function createStationModel( // Out of range the model would not draw anyway: skip the SGP4 sample. if (cameraDistance > spec.maxDistanceM * 1.5) { _hide(); + _undrawn(); return; } + undrawnSince = null; const frame = _stationFrame(scratchPosition); if (!frame) { @@ -710,20 +737,35 @@ export function createStationModel( ); } - function _destroyStationModel() { + /** + * The model has not been drawn this frame: once that has lasted + * `unloadAfterMs`, release it. + */ + function _undrawn() { + if (!item) return; + const nowMs = Date.now(); + undrawnSince ??= nowMs; + if ( + nowMs - undrawnSince >= + (spec.unloadAfterMs ?? STATION_MODEL_UNLOAD_AFTER_MS) + ) + _unload(); + } + + /** + * Release the model's draw set, and the renderer's model and GPU memory + * with it; the dots stand at every distance. The next approach loads it + * again (a failed load is tried again then too). + */ + function _unload() { + _releaseDots(); + _releaseCompanions(); + handingOver = false; loading = false; failed = false; ready = false; visualValid = false; - articulation.reset(); - nodePoses.clear(); - telemetry?.destroy(); - _releaseDots(); - _releaseCompanions(); - handingOver = false; - dockedByOrbit.clear(); - lastCompanionScanMs = Number.NEGATIVE_INFINITY; - _syncOrbitCloseUp(false); + undrawnSince = null; offReady?.(); offFailed?.(); offReady = null; @@ -733,6 +775,16 @@ export function createStationModel( item = null; } + function _destroyStationModel() { + _unload(); + articulation.reset(); + nodePoses.clear(); + telemetry?.destroy(); + dockedByOrbit.clear(); + lastCompanionScanMs = Number.NEGATIVE_INFINITY; + _syncOrbitCloseUp(false); + } + /** Whether the model is standing in for this docked object's dot. */ function _standsInFor(noradId) { return handedCompanions.has(noradId) && _visualPosition() !== null; diff --git a/src/layers/satellites/stationModel.test.mjs b/src/layers/satellites/stationModel.test.mjs index 2b85285284..556a3baab2 100644 --- a/src/layers/satellites/stationModel.test.mjs +++ b/src/layers/satellites/stationModel.test.mjs @@ -171,6 +171,9 @@ function fixture(t) { draw.ready(MODEL_SET, MODEL_ITEM); await settle(); }, + /** The renderer reports the model loaded, or failed to. */ + readyModel: () => draw.ready(MODEL_SET, MODEL_ITEM), + failModel: () => draw.loadFailed(MODEL_SET, MODEL_ITEM), /** Track `noradId`, placing the tracked sample on the station. */ track(noradId, at) { state._trackedNorad = noradId; @@ -305,6 +308,74 @@ test('a station without views on its model hides it while the Cockpit rides the assert.equal(f.modelItem().show, true); }); +test('until the renderer has the model the dots stand at every distance, and stay so when it fails to load', async (t) => { + const f = fixture(t); + f.parts.rendering._showOrbitPath(STATION, TRACKED_ORBIT_COLOR); + f.track(COMPANION, f.stationPos); + f.cameraAt(MAX_M * 1.4); + f.tick(); // asks for the model + f.cameraAt(20_000); + f.tick(); + f.tick(); + // Loading: nothing is handed over, so the station never goes dark. + assert.equal(f.modelItem().show, false); + assert.equal(f.drawn(STATION).displayRange, undefined); + assert.equal(f.drawn(COMPANION).displayRange, undefined); + assert.equal(f.trackedDot().displayRange, undefined); + assert.equal( + f.ring(STATION).show, + true, + 'the ring is not hidden for a model not drawn', + ); + assert.equal(f.model._visualPosition(), null, 'the card keeps the dot'); + assert.equal(f.model._cockpitMount(), null); + // The load fails: the dots stay at every distance, frame after frame. + f.failModel(); + for (let i = 0; i < 3; i++) f.tick(); + assert.equal(f.modelItem().show, false); + assert.equal(f.drawn(STATION).displayRange, undefined); + assert.equal(f.drawn(COMPANION).displayRange, undefined); + assert.equal(f.trackedDot().displayRange, undefined); + // A load that does finish hands them over. + f.readyModel(); + f.tick(); + assert.equal(f.modelItem().show, true); + assert.deepEqual(f.drawn(STATION).displayRange, FAR_RANGE); + assert.deepEqual(f.trackedDot().displayRange, FAR_RANGE); +}); + +test('turning the layer off, or a while undrawn, releases the model; the next approach asks for it again', async (t) => { + const f = fixture(t); + await f.load(); + f.cameraAt(20_000); + f.tick(); + assert.equal(f.modelItem().show, true); + // Out of range, within the grace: kept. + f.cameraAt(MAX_M * 5); + f.tick(); + assert.ok(f.modelSet(), 'kept for a while'); + // Past it: released, and the dots stand everywhere. + f.model.spec.unloadAfterMs = 0; + f.tick(); + assert.equal(f.modelSet(), undefined, 'its set is disposed'); + assert.equal(f.drawn(STATION).displayRange, undefined); + // The next approach asks for it again. + f.cameraAt(MAX_M * 1.4); + f.tick(); + assert.ok(f.modelItem(), 'asked for again'); + f.readyModel(); + f.cameraAt(20_000); + f.tick(); + assert.equal(f.modelItem().show, true); + // Turning the layer off releases it at once. + f.model.spec.unloadAfterMs = 60_000; + // (The layer's disable asks the model once, as here: no frame runs then.) + f.state._enabled = false; + f.model._updateStationModel(); + assert.equal(f.modelSet(), undefined); + assert.equal(f.drawn(STATION).displayRange, undefined); +}); + test('within its range the model stands in for the dots and hides the ring; beyond it the dots draw', async (t) => { const f = fixture(t); await f.load();