Files
gods-eye-view/src/globe/cesium/sonar.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

215 lines
6.8 KiB
JavaScript

// The Cyber HUD sonar's contact treatment on Cesium (render.setContactSonar,
// the `contactSonar` capability), drawn from the state the app hands the
// engine each frame (src/cyberSonarEngine.js). Native dots, icons and
// glyphs get derived draw commands whose shaders apply the sweep
// (sonarCommands.js); models keep their reversible CPU colour treatment
// (src/cyberSonar.js applyCyberSonarPrimitive), without walking native
// records. Nothing joins the scene before the treatment is first on; turning
// it off gives the models their colours back at once.
import * as Cesium from 'cesium';
import {
applyCyberSonarPrimitive,
cyberSonarSampler,
} from '../../cyberSonar.js';
import {
CYBER_SONAR_CESIUM_VERSION,
createCyberSonarGpu,
} from './sonarCommands.js';
const alive = (value) => value && !value.isDestroyed?.();
export function visitCyberSonarModels(primitive, visit, visible = true) {
if (!alive(primitive)) return;
visible = visible && primitive.show !== false;
if (primitive instanceof Cesium.PrimitiveCollection) {
for (let i = 0; i < primitive.length; i++)
visitCyberSonarModels(primitive.get(i), visit, visible);
} else if (primitive instanceof Cesium.Model) {
visit(primitive, primitive, 'model', visible && primitive.ready);
}
}
/**
* The sonar on `viewer`'s scene. `set(state)` takes the merged
* ContactSonar (src/globe/contract.js) for the next drawn frame;
* `diagnostics()` is what the treatment holds, live: whether the command
* adapter runs on this Cesium (`supported`), the models it tinted last
* frame (`contacts`) and the native batches it drew (`gpu`: commands,
* visibleCommands, programs, error).
* @param {Cesium.Viewer} viewer
* @param {{ createGpu?: typeof createCyberSonarGpu }} [options] A test seam.
*/
export function createCesiumSonar(
viewer,
{ createGpu = createCyberSonarGpu } = {},
) {
const scene = viewer?.scene;
let state = null;
let treatment = null;
const stats = { contacts: 0, updateMs: 0, frames: 0, gpu: null };
// Read every frame (src/cyberSonarEngine.js): one live view, no copies.
const report = Object.freeze({
get supported() {
return (
stats.gpu?.supported ?? Cesium.VERSION === CYBER_SONAR_CESIUM_VERSION
);
},
get contacts() {
return stats.contacts;
},
get updateMs() {
return stats.updateMs;
},
get frames() {
return stats.frames;
},
get gpu() {
return stats.gpu;
},
});
function attach() {
if (
!scene?.postUpdate?.addEventListener ||
!scene?.preRender?.addEventListener
)
return null;
const tracked = new Map();
const matrix = new Cesium.Matrix4();
const position = new Cesium.Cartesian3();
let epoch = 0;
let sampler, width, height, active, count;
const gpu = createGpu(scene, () => state);
stats.gpu = gpu.stats;
const restore = (visual, entry) => {
if (!alive(entry.owner)) return;
for (const key of entry.keys)
applyCyberSonarPrimitive(visual, 1, false, undefined, key);
};
const restoreAll = () => {
for (const [visual, entry] of tracked) restore(visual, entry);
tracked.clear();
stats.contacts = 0;
stats.updateMs = 0;
};
const visit = (visual, owner, kind, visible) => {
let entry = tracked.get(visual);
if (!visible) {
if (entry) {
restore(visual, entry);
tracked.delete(visual);
}
return;
}
let p = visual.position;
if (kind === 'model')
p = Cesium.Matrix4.getTranslation(visual.modelMatrix, position);
else if (
p &&
owner.modelMatrix &&
!Cesium.Matrix4.equals(owner.modelMatrix, Cesium.Matrix4.IDENTITY)
)
p = Cesium.Matrix4.multiplyByPoint(owner.modelMatrix, p, position);
if (!p) return;
const w =
matrix[3] * p.x + matrix[7] * p.y + matrix[11] * p.z + matrix[15];
const x =
(matrix[0] * p.x + matrix[4] * p.y + matrix[8] * p.z + matrix[12]) / w;
const y =
(matrix[1] * p.x + matrix[5] * p.y + matrix[9] * p.z + matrix[13]) / w;
if (w <= 0 || Math.abs(x) > 1.1 || Math.abs(y) > 1.1) return;
if (!entry) {
entry = {
owner,
keys:
kind === 'label'
? ['fillColor', 'outlineColor', 'backgroundColor']
: kind === 'marker' && visual.outlineColor
? ['color', 'outlineColor']
: ['color'],
epoch,
};
tracked.set(visual, entry);
}
entry.epoch = epoch;
const factor = active
? sampler.at((x + 1) * width * 0.5, (1 - y) * height * 0.5)
: sampler.neutral;
for (const key of entry.keys) {
if (key === 'outlineColor' && visual.outlineWidth === 0) continue;
if (key === 'backgroundColor' && !visual.showBackground) continue;
applyCyberSonarPrimitive(
visual,
kind === 'label' ? sampler.label(factor) : factor,
true,
sampler,
key,
kind === 'model',
);
}
count++;
};
const update = () => {
if (!state?.enabled || !gpu.stats.supported) {
restoreAll();
return;
}
const started = performance.now();
width = scene.canvas.clientWidth;
height = scene.canvas.clientHeight;
sampler = cyberSonarSampler(width, height, state);
active = state.active;
Cesium.Matrix4.multiply(
scene.camera.frustum.projectionMatrix,
scene.camera.viewMatrix,
matrix,
);
epoch++;
count = 0;
visitCyberSonarModels(scene.primitives, visit);
for (const [visual, entry] of tracked)
if (entry.epoch !== epoch) {
restore(visual, entry);
tracked.delete(visual);
}
stats.contacts = count;
stats.updateMs = performance.now() - started;
stats.frames++;
};
let removeRender;
// Aircraft fleet writers run in preRender, while tracked models and
// entity visualizers update earlier. Compose AFTER all those owners and
// BEFORE Cesium uploads primitive attributes for this draw. Requeue at
// postUpdate so writers installed later (enable/re-enable/track) cannot
// overwrite sonar. This only reorders one callback; the contact walk
// runs once per actual draw.
const removeUpdate = scene.postUpdate.addEventListener(() => {
removeRender?.();
removeRender = scene.preRender.addEventListener(update);
});
return {
restoreAll,
destroy() {
removeUpdate();
removeRender?.();
gpu.destroy();
restoreAll();
},
};
}
return {
set(next) {
state = next;
if (next.enabled) treatment ??= attach();
else treatment?.restoreAll();
},
diagnostics: () => report,
destroy() {
treatment?.destroy();
treatment = null;
},
};
}