mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-16 08:12:24 +00:00
Draw the ALPR layer's markers through the renderer's ports
The ALPR badges were Cesium entities whose billboards the layer changed in place: the canvas overlay swapped a badge to a faint pick target at its anchor, selection swapped the image, and unresolved floors clamped to the ground. They are now one icon set from the draw port, updated in place through upsert, so selection and the overlay swap change the same billboard without a rebuild or a blink. Badges keep their scale by distance, ground clamping and depth-test settings. Marker state lives in the app (markerState.js): a plain carrier per camera, registered as the camera's context entity, and a marker set that batches changes and sends only the icons that draw differently. Clicks on native badges come from the dispatcher's draw hit. Projection, the on-screen count, height samples (excluding the layer's own set), the screen-centre ellipsoid pick and the camera reach the renderer through the query, camera and frame ports. A resync of the overlay now hands back only the cameras that leave it, so kept ones are not written twice. index.js, overlay.js and presentation.js leave the Cesium allow-list; the layer's entry is Cesium-free, and a caller's own viewer loads the unbound factories. Differences the contract does not let us avoid: - Double-clicking a badge no longer starts the viewer's entity follow, since draw items are not entities. - The terrain fallback for anchors is no longer skipped while the globe is hidden: the globe port has no visibility getter. - Without depth support the query port answers no heights, terrain included, so anchors wait for a cached floor there. - On a viewer with no renderer bound, the surface counts as settled when anchors are refined.
This commit is contained in:
1 parent
faded8a418
commit
2592289612
11 files changed
+722
-537
No files matched your search
+111
-145
@@ -1,4 +1,3 @@
|
||||
import * as Cesium from 'cesium';
|
||||
import {
|
||||
MAX_CANVAS_FRUSTUMS,
|
||||
MARKER_ICON_SIZE,
|
||||
@@ -19,9 +18,18 @@ import {
|
||||
validAlprGroundHeight,
|
||||
} from './visuals.js';
|
||||
import { alprMarkers } from './markerState.js';
|
||||
import { heightsFor } from '../../queries/heights.js';
|
||||
import { distance, fromDegrees, toLonLatH } from '../../geo/index.js';
|
||||
import { cameraOf, findQuery, rendererOf } from '../../render/rendererOf.js';
|
||||
|
||||
/** Per-layer presentation on a caller-owned overlay; Cesium remains the fallback. */
|
||||
let lanes = 0;
|
||||
|
||||
/** Within `epsilon` on every axis. */
|
||||
const near = (a, b, epsilon) =>
|
||||
Math.abs(a.x - b.x) <= epsilon &&
|
||||
Math.abs(a.y - b.y) <= epsilon &&
|
||||
Math.abs(a.z - b.z) <= epsilon;
|
||||
|
||||
/** Per-layer presentation on a caller-owned overlay; the icon set remains the fallback. */
|
||||
export function createAlprOverlay({
|
||||
state,
|
||||
services,
|
||||
@@ -40,76 +48,55 @@ export function createAlprOverlay({
|
||||
if (!destroyed && state.enabled) lane?.requestPaint();
|
||||
};
|
||||
|
||||
function nativeVisible(entity, visible) {
|
||||
if (!entity) return;
|
||||
canvasOwned(entity, !visible);
|
||||
setShown(entity, visible);
|
||||
const markers = state.markers;
|
||||
|
||||
function nativeVisible(carrier, visible) {
|
||||
if (!carrier) return;
|
||||
canvasOwned(carrier, !visible);
|
||||
setShown(carrier, visible);
|
||||
}
|
||||
|
||||
/** Swap a marker between its native look and a faint canvas pick target. */
|
||||
function canvasOwned(entity, owned) {
|
||||
const visible = !owned;
|
||||
const billboard = entity.billboard;
|
||||
const marker = alprMarkers.write(entity);
|
||||
// Keep a faint native pick target under the canvas badge so sibling layer
|
||||
// handlers recognize ALPR ownership instead of treating the click as empty.
|
||||
if (!visible && !marker.nativeAppearance) {
|
||||
marker.nativeAppearance = {
|
||||
position: entity.position,
|
||||
heightReference: billboard.heightReference,
|
||||
scaleByDistance: billboard.scaleByDistance,
|
||||
color: billboard.color,
|
||||
};
|
||||
entity.position = marker.canvasPosition;
|
||||
marker.pickPosition = entity.position;
|
||||
billboard.heightReference = Cesium.HeightReference.NONE;
|
||||
billboard.scaleByDistance = undefined;
|
||||
billboard.color = Cesium.Color.WHITE.withAlpha(0.01);
|
||||
} else if (visible && marker.nativeAppearance) {
|
||||
const saved = marker.nativeAppearance;
|
||||
if (entity.position === marker.pickPosition)
|
||||
entity.position = saved.position;
|
||||
billboard.heightReference = saved.heightReference;
|
||||
billboard.scaleByDistance = saved.scaleByDistance;
|
||||
billboard.color = saved.color;
|
||||
marker.nativeAppearance = null;
|
||||
marker.pickPosition = null;
|
||||
}
|
||||
/**
|
||||
* Swap a marker between its native look and a faint canvas pick target.
|
||||
* The faint icon stays under the canvas badge, at its anchor, so sibling
|
||||
* layer handlers recognize ALPR ownership instead of treating the click
|
||||
* as empty.
|
||||
*/
|
||||
function canvasOwned(carrier, owned) {
|
||||
const marker = alprMarkers.write(carrier);
|
||||
if (Boolean(marker.canvasOwned) === owned) return;
|
||||
marker.canvasOwned = owned;
|
||||
markers.changed(carrier);
|
||||
}
|
||||
|
||||
// Assign only on change: each Entity property write mints a new property
|
||||
// and raises definitionChanged, which paint used to do for every overlay
|
||||
// camera on every frame.
|
||||
function setShown(entity, visible) {
|
||||
const marker = alprMarkers.write(entity);
|
||||
if (marker.shown === visible) return;
|
||||
// Changed only on change: the icon is sent again only when it draws
|
||||
// differently.
|
||||
function setShown(carrier, visible) {
|
||||
const marker = alprMarkers.write(carrier);
|
||||
if ((marker.shown ?? true) === visible) return;
|
||||
marker.shown = visible;
|
||||
entity.billboard.show = visible;
|
||||
if (entity.polyline) entity.polyline.show = visible;
|
||||
if (entity.polygon) entity.polygon.show = visible;
|
||||
markers.changed(carrier);
|
||||
}
|
||||
|
||||
function resetAnchors() {
|
||||
for (const entity of state.dataSource.entities.values) {
|
||||
const marker = alprMarkers.write(entity);
|
||||
for (const carrier of markers.values()) {
|
||||
const marker = alprMarkers.write(carrier);
|
||||
marker.canvasPosition = null;
|
||||
marker.wedge = null;
|
||||
marker.displayPosition = null;
|
||||
nativeVisible(entity, true);
|
||||
nativeVisible(carrier, true);
|
||||
}
|
||||
markers.flush();
|
||||
state.lastAnchorSampleAt = 0;
|
||||
retryUntil = Date.now() + 15000;
|
||||
requestPaint();
|
||||
}
|
||||
|
||||
function anchorFor(record, entity, budget) {
|
||||
const cached = alprMarkers.read(entity)?.canvasPosition;
|
||||
function anchorFor(record, carrier, budget) {
|
||||
const cached = alprMarkers.read(carrier)?.canvasPosition;
|
||||
if (cached) return cached;
|
||||
const scene = state.viewer.scene;
|
||||
const location = Cesium.Cartographic.fromDegrees(
|
||||
record.longitude,
|
||||
record.latitude,
|
||||
);
|
||||
const query = findQuery(state.viewer);
|
||||
const point = { lon: record.longitude, lat: record.latitude };
|
||||
// A floor-placed marker anchors where its native badge sits; only a
|
||||
// marker still waiting for its floor samples the rendered surface, and
|
||||
// each paint samples a few at most (each sample is a depth render).
|
||||
@@ -119,17 +106,17 @@ export function createAlprOverlay({
|
||||
record.longitude,
|
||||
);
|
||||
if (validAlprGroundHeight(floor)) height = floor + MARKER_FLOOR_LIFT_M;
|
||||
else if (scene.sampleHeightSupported) {
|
||||
else if (query) {
|
||||
if (budget.samples <= 0) return null;
|
||||
budget.samples -= 1;
|
||||
// A miss is tiles still streaming.
|
||||
height = heightsFor(scene).now(location, { exclude: [entity] });
|
||||
height = query.sampleHeightNow(point, { exclude: [markers.icons?.id] });
|
||||
}
|
||||
if (!validAlprGroundHeight(height) && scene.globe.show)
|
||||
height = heightsFor(scene).now(location, { against: 'terrain' });
|
||||
if (!validAlprGroundHeight(height))
|
||||
height = query?.sampleHeightNow(point, { against: 'terrain' });
|
||||
if (!validAlprGroundHeight(height)) return null;
|
||||
setAnchor(record, entity, height);
|
||||
return alprMarkers.read(entity)?.canvasPosition;
|
||||
setAnchor(record, carrier, height);
|
||||
return alprMarkers.read(carrier)?.canvasPosition;
|
||||
}
|
||||
|
||||
function floorHeightAt(latitude, longitude) {
|
||||
@@ -137,24 +124,23 @@ export function createAlprOverlay({
|
||||
return validAlprGroundHeight(floor) ? floor + MARKER_FLOOR_LIFT_M : null;
|
||||
}
|
||||
|
||||
function setAnchor(record, entity, height) {
|
||||
const marker = alprMarkers.write(entity);
|
||||
marker.canvasPosition = Cesium.Cartesian3.fromDegrees(
|
||||
function setAnchor(record, carrier, height) {
|
||||
const marker = alprMarkers.write(carrier);
|
||||
marker.canvasPosition = fromDegrees(
|
||||
record.longitude,
|
||||
record.latitude,
|
||||
height,
|
||||
);
|
||||
marker.wedge = directionWedgePositions(record, height, floorHeightAt);
|
||||
if (marker.canvasOwned) markers.changed(carrier);
|
||||
}
|
||||
|
||||
/** Whether the visible photoreal tiles (or globe) have finished streaming. */
|
||||
function surfaceSettled(scene) {
|
||||
if (scene.globe?.show) return scene.globe.tilesLoaded !== false;
|
||||
for (let i = 0; i < (scene.primitives?.length || 0); i++) {
|
||||
const primitive = scene.primitives.get(i);
|
||||
if (primitive?.show && primitive.tilesLoaded === false) return false;
|
||||
}
|
||||
return true;
|
||||
function surfaceSettled() {
|
||||
const globe = rendererOf(state.viewer)?.globe;
|
||||
// A viewer with no renderer of its own cannot say; its samples are
|
||||
// still checked against the floor.
|
||||
return !globe || (globe.isSettled('terrain') && globe.isSettled('tiles'));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -165,47 +151,34 @@ export function createAlprOverlay({
|
||||
* samples far outside the floor prior (a roof or a streaming tile) are
|
||||
* rejected. Returns whether anchors remain to check.
|
||||
*/
|
||||
function refineAnchors(budget) {
|
||||
const camera = state.viewer.camera;
|
||||
const scene = state.viewer.scene;
|
||||
function refineAnchors(budget, pose) {
|
||||
const still =
|
||||
lastCamera &&
|
||||
camera.positionWC &&
|
||||
Cesium.Cartesian3.equalsEpsilon(
|
||||
camera.positionWC,
|
||||
lastCamera.position,
|
||||
0,
|
||||
0.05,
|
||||
) &&
|
||||
Cesium.Cartesian3.equalsEpsilon(
|
||||
camera.directionWC,
|
||||
lastCamera.direction,
|
||||
0,
|
||||
1e-6,
|
||||
);
|
||||
if (camera.positionWC && camera.directionWC)
|
||||
lastCamera = {
|
||||
position: Cesium.Cartesian3.clone(camera.positionWC),
|
||||
direction: Cesium.Cartesian3.clone(camera.directionWC),
|
||||
};
|
||||
if (!scene.sampleHeightSupported) return false;
|
||||
near(pose.position, lastCamera.position, 0.05) &&
|
||||
near(pose.direction, lastCamera.direction, 1e-6);
|
||||
lastCamera = {
|
||||
position: { ...pose.position },
|
||||
direction: { ...pose.direction },
|
||||
};
|
||||
const query = findQuery(state.viewer);
|
||||
if (!query) return false;
|
||||
let pending = false;
|
||||
for (const record of records) {
|
||||
const entity = state.dataSource.entities.getById(record.id);
|
||||
const marker = alprMarkers.read(entity);
|
||||
const carrier = markers.get(record.id);
|
||||
const marker = alprMarkers.read(carrier);
|
||||
const anchor = marker?.canvasPosition;
|
||||
if (!anchor || marker.surfaceCheckedFor === anchor) continue;
|
||||
if (!still || !surfaceSettled(scene) || budget.samples <= 0) {
|
||||
if (!still || !surfaceSettled() || budget.samples <= 0) {
|
||||
pending = true;
|
||||
continue;
|
||||
}
|
||||
budget.samples -= 1;
|
||||
// A miss is tiles still streaming.
|
||||
const sample = heightsFor(scene).now(
|
||||
const sample = query.sampleHeightNow(
|
||||
{ lon: record.longitude, lat: record.latitude },
|
||||
{ exclude: [entity] },
|
||||
{ exclude: [markers.icons?.id] },
|
||||
);
|
||||
const current = Cesium.Cartographic.fromCartesian(anchor).height;
|
||||
const current = toLonLatH(anchor)?.height;
|
||||
const floor = services.groundFloor.cachedGroundFloor(
|
||||
record.latitude,
|
||||
record.longitude,
|
||||
@@ -216,10 +189,10 @@ export function createAlprOverlay({
|
||||
(sample >= floor - SURFACE_BELOW_FLOOR_M &&
|
||||
sample <= floor + SURFACE_ABOVE_FLOOR_M));
|
||||
if (accepted && Math.abs(sample + MARKER_FLOOR_LIFT_M - current) > 1)
|
||||
setAnchor(record, entity, sample + MARKER_FLOOR_LIFT_M);
|
||||
setAnchor(record, carrier, sample + MARKER_FLOOR_LIFT_M);
|
||||
// setAnchor rewrote the same record, so this is the new anchor.
|
||||
marker.surfaceCheckedFor = marker.canvasPosition;
|
||||
if (entity.id === state.selectedId)
|
||||
if (carrier.id === state.selectedId)
|
||||
marker.displayPosition = marker.canvasPosition;
|
||||
}
|
||||
return pending;
|
||||
@@ -231,9 +204,11 @@ export function createAlprOverlay({
|
||||
let unresolved = false;
|
||||
const painted = [];
|
||||
const budget = { samples: ANCHOR_SAMPLES_PER_PAINT };
|
||||
const pose = cameraOf(state.viewer).pose();
|
||||
const query = findQuery(state.viewer);
|
||||
for (const record of records) {
|
||||
const entity = state.dataSource.entities.getById(record.id);
|
||||
if (!entity) continue;
|
||||
const carrier = markers.get(record.id);
|
||||
if (!carrier) continue;
|
||||
const selected = record.id === state.selectedId;
|
||||
const image = images[selected ? 1 : 0];
|
||||
if (
|
||||
@@ -241,45 +216,35 @@ export function createAlprOverlay({
|
||||
!image.naturalWidth ||
|
||||
(selected && !images[2]?.naturalWidth)
|
||||
) {
|
||||
nativeVisible(entity, true);
|
||||
nativeVisible(carrier, true);
|
||||
continue;
|
||||
}
|
||||
const anchor = anchorFor(record, entity, budget);
|
||||
const anchor = anchorFor(record, carrier, budget);
|
||||
if (!anchor) {
|
||||
nativeVisible(entity, true);
|
||||
nativeVisible(carrier, true);
|
||||
unresolved = true;
|
||||
continue;
|
||||
}
|
||||
canvasOwned(entity, true);
|
||||
canvasOwned(carrier, true);
|
||||
if (occluder && !occluder.isPointVisible(anchor)) {
|
||||
setShown(entity, false);
|
||||
setShown(carrier, false);
|
||||
continue;
|
||||
}
|
||||
const scene = state.viewer.scene;
|
||||
const origin = Cesium.SceneTransforms.worldToWindowCoordinates(
|
||||
scene,
|
||||
anchor,
|
||||
);
|
||||
const origin = query?.project(anchor);
|
||||
if (!origin) {
|
||||
setShown(entity, false);
|
||||
setShown(carrier, false);
|
||||
continue;
|
||||
}
|
||||
const alpha =
|
||||
services.overlays.keyholeAlpha?.(origin.x, origin.y, keyhole) ?? 1;
|
||||
if (!(alpha > 0)) {
|
||||
setShown(entity, false);
|
||||
setShown(carrier, false);
|
||||
continue;
|
||||
}
|
||||
const wedge = alprMarkers.read(entity)?.wedge;
|
||||
const wedge = alprMarkers.read(carrier)?.wedge;
|
||||
if (wedge) {
|
||||
const left = Cesium.SceneTransforms.worldToWindowCoordinates(
|
||||
scene,
|
||||
wedge[1],
|
||||
);
|
||||
const right = Cesium.SceneTransforms.worldToWindowCoordinates(
|
||||
scene,
|
||||
wedge[2],
|
||||
);
|
||||
const left = query.project(wedge[1]);
|
||||
const right = query.project(wedge[2]);
|
||||
if (left && right) {
|
||||
ctx.save();
|
||||
ctx.globalAlpha *= alpha;
|
||||
@@ -293,21 +258,19 @@ export function createAlprOverlay({
|
||||
origin.y < -60 ||
|
||||
origin.y > height + 60
|
||||
) {
|
||||
setShown(entity, false);
|
||||
setShown(carrier, false);
|
||||
continue;
|
||||
}
|
||||
setShown(entity, true);
|
||||
setShown(carrier, true);
|
||||
painted.push({
|
||||
record,
|
||||
origin,
|
||||
selected,
|
||||
image,
|
||||
alpha,
|
||||
scale: markerScale(
|
||||
Cesium.Cartesian3.distance(state.viewer.camera.positionWC, anchor),
|
||||
),
|
||||
scale: markerScale(distance(pose.position, anchor)),
|
||||
});
|
||||
if (selected) alprMarkers.write(entity).displayPosition = anchor;
|
||||
if (selected) alprMarkers.write(carrier).displayPosition = anchor;
|
||||
}
|
||||
// Paint all glyphs after the wedges so one camera's cone cannot wash out another.
|
||||
for (const { record, origin, selected, image, alpha, scale } of painted) {
|
||||
@@ -333,9 +296,11 @@ export function createAlprOverlay({
|
||||
ctx.restore();
|
||||
hits.push({ id: record.id, x: origin.x, y: origin.y, radius: size / 2 });
|
||||
}
|
||||
const refining = refineAnchors({
|
||||
samples: Math.min(budget.samples, SURFACE_SAMPLES_PER_PAINT),
|
||||
});
|
||||
const refining = refineAnchors(
|
||||
{ samples: Math.min(budget.samples, SURFACE_SAMPLES_PER_PAINT) },
|
||||
pose,
|
||||
);
|
||||
markers.flush();
|
||||
if ((unresolved || refining) && !retryTimer && Date.now() < retryUntil) {
|
||||
retryTimer = setTimeout(() => {
|
||||
retryTimer = null;
|
||||
@@ -356,7 +321,7 @@ export function createAlprOverlay({
|
||||
if (!services.overlays?.registerPaintLane || typeof Image === 'undefined')
|
||||
return;
|
||||
lane = services.overlays.registerPaintLane('selected', paint, {
|
||||
id: `alpr-${Cesium.createGuid()}`,
|
||||
id: `alpr-${++lanes}`,
|
||||
active: false,
|
||||
});
|
||||
images = [MARKER_IMAGE, SELECTED_IMAGE, BRACKETS_IMAGE].map((src) => {
|
||||
@@ -369,9 +334,8 @@ export function createAlprOverlay({
|
||||
},
|
||||
sync(visible) {
|
||||
if (!lane) return;
|
||||
for (const record of records)
|
||||
nativeVisible(state.dataSource.entities.getById(record.id), true);
|
||||
const position = state.viewer.camera.positionWC;
|
||||
const previous = records;
|
||||
const position = cameraOf(state.viewer).pose().position;
|
||||
records = visible
|
||||
.filter(
|
||||
(record) =>
|
||||
@@ -381,13 +345,10 @@ export function createAlprOverlay({
|
||||
.map((record) => ({
|
||||
record,
|
||||
distance: position
|
||||
? Cesium.Cartesian3.distanceSquared(
|
||||
? distance(
|
||||
position,
|
||||
Cesium.Cartesian3.fromDegrees(
|
||||
record.longitude,
|
||||
record.latitude,
|
||||
),
|
||||
)
|
||||
fromDegrees(record.longitude, record.latitude),
|
||||
) ** 2
|
||||
: 0,
|
||||
}))
|
||||
.sort((a, b) => a.distance - b.distance)
|
||||
@@ -398,6 +359,11 @@ export function createAlprOverlay({
|
||||
if (records.length >= MAX_CANVAS_FRUSTUMS) records.pop();
|
||||
records.push(selected);
|
||||
}
|
||||
// Cameras that leave the cohort go native; those that stay keep the
|
||||
// canvas until the next paint, so their icons are not written twice.
|
||||
const kept = new Set(records.map((record) => record.id));
|
||||
for (const record of previous)
|
||||
if (!kept.has(record.id)) nativeVisible(markers.get(record.id), true);
|
||||
hits = [];
|
||||
retryUntil = Date.now() + 15000;
|
||||
lane.setActive(state.enabled && records.length > 0);
|
||||
@@ -419,8 +385,8 @@ export function createAlprOverlay({
|
||||
clear() {
|
||||
clearTimeout(retryTimer);
|
||||
retryTimer = null;
|
||||
for (const record of records)
|
||||
nativeVisible(state.dataSource?.entities.getById(record.id), true);
|
||||
for (const record of records) nativeVisible(markers.get(record.id), true);
|
||||
markers.flush();
|
||||
records = [];
|
||||
hits = [];
|
||||
lane?.setActive(false);
|
||||
|
||||
Reference in new issue
Block a user