Files
gods-eye-view/src/cyberSonarEngine.js
T
German Khokhlov 97c4dc76ce Move scene effects and imported geometry behind the globe contract
Move Cesium viewer, map, sky, projection, trail and sonar internals into
its engine. Drive styles, bloom, sharpening and contact sonar through
engine.render. Count camera-input suspensions across all three engines
and migrate the feature callers. Preserve Cesium's effects and input
behaviour; fix transit lens clicks on Cesium and make zero sharpening on
Navara disable sharpening, as it does on Cesium.

Parse imported files into plain records and draw them through movers,
lines, ground and imagery on both engines. Follow Cesium's KML style
precedence, preserve local KMZ images and frame the drawable records.
Open overlays use imagery when the engine has no native map-overlay API.
Run every imported-display case on fake and headless Cesium engines.

The shared import path retains these known differences from Cesium's old
DataSources: wall fills, extruded prisms and gx:LatLonQuad are undrawn;
raised polygon fills drape while outlines retain altitude; GroundOverlay
rotation, altitude and RGB tint are not applied; rhumb lines become great
circles. Ground heights use the loaded surface synchronously instead of
waiting for most-detailed sampling. Supported KMZ ground overlays now
draw on Navara too. Document the limits rather than claiming full parity.

Combine batches B and C because their disjoint path manifests cannot
place the shared boundary and format files in a later commit while
keeping B independently valid. This combined commit owns the shared
docs, package boundaries, format scope, locale catalogs and coupling
baseline. Remove five obsolete import strings from all eleven catalogs.
Make the load-time source scan include new files and skip deleted paths
so the same checks work before and after recording the moves.

Coupling: Navara renderer lines fall from 1695 to 1294, Cesium import
files from 25 to 9, viewer lines from 1177 to 1026, SDK loads from 4 to 3
and engine-name lines from 62 to 42. No count grows.

The full unit suite and the boundary, format and coupling checks pass.
Browser validation remains to be run.

Browser checks to run
GOOGLE_MAPS_API_KEY= CESIUM_ION_TOKEN= npm run dev -- --port 4291
node scripts/qa-contract-suite.mjs http://localhost:4291
node scripts/qa-navara-visual-styles.mjs http://localhost:4291
node scripts/qa-navara-bloom.mjs http://localhost:4291
node scripts/qa-navara-sonar.mjs http://localhost:4291
QA_BASE_URL=http://localhost:4291 node scripts/qa-visual-effects.mjs
QA_BASE_URL=http://localhost:4291 node scripts/qa-zoom-out-guard.mjs
node scripts/qa-sky-model-lighting.mjs --url http://localhost:4291
node scripts/qa-geo-import.mjs http://localhost:4291
node scripts/qa-geo-import-regressions.mjs http://localhost:4291
node scripts/qa-geo-import-large.mjs http://localhost:4291
node scripts/qa-navara-geo-import.mjs http://localhost:4291

Use the keyless server for every check. Photoreal-only cases remain
unverified; do not enable paid tiles to run them.
2026-11-07 12:45:21 +04:00

124 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
// sprite and point 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,
};
}
/** Sprite and point instances the engine draws (models keep their colours). */
const contactCount = (engine) =>
engine.movers
.list()
.filter((set) => set.kind !== 'model')
.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: the engine's movers, and what its own
// treatment drew (Cesium's tinted models and visible native batches).
const contactsShown = () => {
if (contactCount(engine)) return true;
const drawn = diagnostics();
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 });
};
}