Treat contacts with the Cyber sonar sweep on Navara behind an optional render.setContactSonar capability

The sprite and point mover shaders rate each contact by its anchor against the sweep with the cyberSonar.js sampler maths; the engine sonar scene feeds the theme, settings and shared sweep angle each drawn frame and keeps the fleet cadence only while the sweep runs over contacts.
This commit is contained in:
German Khokhlov committed 2026-11-03 21:49:56 +04:00
1 parent c2d7a6dbb0
commit 1a3f6f2486
16 files changed
+1040 -6

No files matched your search

+94
View File
@@ -0,0 +1,94 @@
// The Cyber HUD sonar's contact treatment on a globe engine that draws it
// itself (render.setContactSonar, the `contactSonar` capability: Navara's
// sprite and point movers), the counterpart of src/cyberSonarScene.js on a
// Cesium viewer. 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, as on Cesium.
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);
/**
* @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,
setTimer = (callback, ms) => setTimeout(callback, ms),
clearTimer = (id) => clearTimeout(id),
} = {},
) {
if (!engineCapabilities(engine).contactSonar) return () => {};
let timer = null;
let disposed = false;
// The Cyber contact theme counts on the contact adapter (cyberSonar.js
// isCyberContactThemeActive): this engine has one.
if (root?.dataset) root.dataset.cyberSonarGpu = 'supported';
const removeRender = engine.frames.preRender.addEventListener(() => {
if (isCyberSonarActive({ root })) syncCyberSonarAnimation(root);
engine.render.setContactSonar(cyberSonarEngineState(now(), root));
});
const removePostRender = engine.frames.postRender.addEventListener(() => {
if (timer !== null) clearTimer(timer);
timer = null;
if (isCyberContactSonarActive({ root }) && contactCount(engine))
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 });
};
}