Files
gods-eye-view/src/cyberSonarEngine.js
T
German Khokhlov 0ef29781e5 Fix the review findings across the engines, layers, data and QA
Navara now matches Cesium where the review found it diverging: a flight
holds the camera inputs, camera.changed counts turns as Cesium does,
render requests made during a frame survive it, follow pinches zoom,
ground widths follow the pixel ratio and Cyber sonar reaches glyph caches
and models. On Cesium, a staged imagery swap keeps loading tiles, setView
replaces a running flight, and a cleared weather detail goes away.

The contract gains MoverSet.failed, maps.terrainRevision,
render.maxTextureSize and surface.sampleTerrainMostDetailed on every
engine, so layers retry failed models, key road heights by terrain,
size weather textures to the GPU and place Bhote Koshi cards on detailed
terrain.

Engine switches keep the live view, follows, focus, layer options and
cards: a pick hands over the exact pose, applies no address options and
pans no carried-over card; a history visit still applies its entry.

Layers put GhostMaps pins, cable landings, OSM Places and directions on
the measured surface without per-frame sampling, draw the Cockpit contact
dot, hide disabled CCTV on attach, and keep ALPR running outside secure
contexts. KML imports draw raised polygon fills, rotated and coloured
ground overlays and north-referenced icon headings, and report files
with nothing to draw.

QA scripts read the contract instead of Cesium internals (test:track is
109/109 again), wait for the camera to rest before clicking, and give
cold switches and parked engines realistic time. Docs follow the code.
2026-11-09 21:34:42 +04:00

122 lines
4.8 KiB
JavaScript

// The Cyber HUD sonar's contact treatment on the globe engine, which draws
// it (render.setContactSonar, the `contactSonar` capability: Navara's
// movers, Cesium's native draw commands and models). Each
// drawn frame hands the engine the theme's state, the sweep's settings and
// its angle on the shared clock (the CSS sweep's), so the contacts and the
// rings turn together; while the sweep runs over visible contacts a parked
// scene draws at the fleet cadence. An engine that can lose its treatment
// (Cesium's command adapter is tested against one Cesium version) says so
// through contactSonarDiagnostics, and the theme then leaves native
// contacts as they are.
import { engineCapabilities } from './globe/contract.js';
import {
CYBER_SONAR_RENDER_INTERVAL_MS,
cyberSonarAngleDeg,
isCyberContactSonarActive,
isCyberContactThemeActive,
isCyberSonarActive,
readCyberSonarSettings,
syncCyberSonarAnimation,
} from './cyberSonar.js';
/** The engine's sonar state for this moment (render.setContactSonar). */
export function cyberSonarEngineState(
nowMs,
root = globalThis.document?.documentElement,
) {
const settings = readCyberSonarSettings(root);
return {
enabled: isCyberContactThemeActive({ root }),
active: isCyberContactSonarActive({ root }),
angleDeg: cyberSonarAngleDeg(nowMs),
range: settings.range,
opacity: settings.opacity,
sector: settings.sector,
};
}
/** Every mover instance the engine holds, on screen or not. */
const contactCount = (engine) =>
engine.movers.list().reduce((count, set) => count + set.count, 0);
const UNSUPPORTED_MESSAGE =
'Contact GPU unavailable. Native contacts remain visible.';
/**
* @param {import('./globe/contract.js').GlobeEngine} engine
* @param {{ subscribe?: Function }} [manager] The layer manager: a
* visibility change draws a frame.
* @returns {() => void} Removes the treatment.
*/
export function createCyberSonarEngineScene(
engine,
manager,
{
now = () => performance.now(),
root = globalThis.document?.documentElement,
documentRef = globalThis.document,
setTimer = (callback, ms) => setTimeout(callback, ms),
clearTimer = (id) => clearTimeout(id),
} = {},
) {
if (!engineCapabilities(engine).contactSonar) return () => {};
let timer = null;
let disposed = false;
// Not contract: what the engine's treatment holds (Cesium's), or null.
const diagnostics = () => engine.contactSonarDiagnostics?.() ?? null;
// The Cyber contact theme counts on the contact adapter (cyberSonar.js
// isCyberContactThemeActive). Returns whether its support changed.
const syncSupport = () => {
const supported = diagnostics()?.supported !== false;
const support = supported ? 'supported' : 'unsupported';
const status = documentRef?.getElementById?.('cyber-sonar-status');
const message = supported ? '' : UNSUPPORTED_MESSAGE;
if (status && status.textContent !== message) status.textContent = message;
if (!root?.dataset || root.dataset.cyberSonarGpu === support) return false;
root.dataset.cyberSonarGpu = support;
return true;
};
syncSupport();
const removeRender = engine.frames.preRender.addEventListener(() => {
if (isCyberSonarActive({ root })) syncCyberSonarAnimation(root);
engine.render.setContactSonar(cyberSonarEngineState(now(), root));
});
// Contacts the sweep passes over on screen, as the engine's treatment
// reports them (Navara's movers in view; Cesium's tinted models and
// visible native batches); an engine without a report, all its movers.
const contactsShown = () => {
const drawn = diagnostics();
if (!drawn) return contactCount(engine) > 0;
return Boolean(drawn.contacts || drawn.gpu?.visibleCommands);
};
const removePostRender = engine.frames.postRender.addEventListener(() => {
// A treatment lost while the scene is parked (Sonar OFF, reduced
// motion) draws once more, so the native contacts come back.
if (syncSupport()) engine.render.request('cyber-sonar');
if (timer !== null) clearTimer(timer);
timer = null;
if (isCyberContactSonarActive({ root }) && contactsShown())
timer = setTimer(() => {
timer = null;
if (!disposed && isCyberContactSonarActive({ root }))
engine.render.request('cyber-sonar');
}, CYBER_SONAR_RENDER_INTERVAL_MS);
});
const unsubscribe = manager?.subscribe?.((change) => {
if (change.type === 'visibility') engine.render.request('cyber-sonar');
});
return () => {
if (disposed) return;
disposed = true;
removeRender();
removePostRender();
unsubscribe?.();
if (timer !== null) clearTimer(timer);
timer = null;
if (root?.dataset) delete root.dataset.cyberSonarGpu;
if (!engine.isDestroyed?.())
engine.render.setContactSonar({ enabled: false, active: false });
};
}