Merge branch 'sameh/renderer-camera-support' into sameh/renderer-seam

# Conflicts:
#	src/render/cesiumAllowList.json
This commit is contained in:
Sameh Khamis committed 2026-11-08 21:23:46 -08:00
commit 6eb9ceef46
33 files changed
+2227 -222

No files matched your search

+2 -1
View File
@@ -14,6 +14,7 @@ import {
createRecentImageryLayer,
} from './index.js';
import { createImageryBoxPreview } from './rendering.js';
import { fakeDrawPort } from '../../testSupport/fakeDrawPort.mjs';
import {
BOX,
CANDIDATES,
@@ -1408,7 +1409,7 @@ test('with SELECT BOX armed, Escape clears the preview first and the next Escape
const tool = initImageryBoxTool({
viewer: fakes.viewer,
createPreview: (viewer, dragBox) =>
createImageryBoxPreview(viewer, dragBox, { cesium: fakes.cesium }),
createImageryBoxPreview(viewer, dragBox, { draw: fakeDrawPort() }),
pickWorld: fakes.pickWorld,
documentRef: fakes.documentRef,
onBox: (box) => f.layer.setBox(box),
+112 -80
View File
@@ -7,13 +7,14 @@
* each layer's admission hook; the renderer's own request limits stay
* untouched.
*/
// The layer flies through `cameraOf` and drapes through `findGlobe`; this
// renderer is its Cesium side, so it brings the factories a viewer with no
// renderer bound gets those ports from, and marks a tileset host as a base
// the globe port drapes on.
import * as Cesium from 'cesium';
import '../../render/cesium/camera.js';
import { markBaseTileset } from '../../render/cesium/globe.js';
// The imagery drapes through the globe port and the box preview draws
// through `drawOf`, both on the renderer's ports: a viewer with no renderer
// bound gets them once its owner has loaded the package's
// `./render/cesium/unbound` export. A tileset host is marked as a base the
// globe port drapes on (`markBaseTileset`).
import { fromDegrees } from '../../geo/ellipsoid.js';
import { markBaseTileset } from '../../render/baseTilesets.js';
import { drawOf } from '../../render/rendererOf.js';
import { PRODUCTS, gibsTemplate } from './model.js';
const SLOT_IDS = ['a', 'b'];
@@ -237,96 +238,127 @@ export function createRecentImageryRenderer({
};
}
const PREVIEW_STROKE = Object.freeze([139 / 255, 233 / 255, 1]);
/** Fill alpha and outline alpha of the box being dragged. */
const PREVIEW_FILL_ALPHA = 0.12;
const PREVIEW_LINE_ALPHA = 0.95;
/** Each preview's draw sets have ids of their own. */
let previewSeq = 0;
/** A box with no area has no fill: a press, or a drag along one axis. */
const hasArea = (box) =>
Boolean(box) && box.east > box.west && box.north > box.south;
/** A box with no extent draws nothing: a press that has not moved yet. */
const hasExtent = (box) =>
Boolean(box) && (box.east > box.west || box.north > box.south);
/**
* The box tool's preview: the box being dragged, a translucent fill and a
* dashed outline on the ground (the draw port draws no dashed draped
* lines), read from `dragBox()` each frame it draws.
* dashed outline on the ground, drawn through the draw port. Each change
* moves them where they are (the port draws a moved ground fill and a moved
* draped line in the frame they move), so the box shows every frame of a
* drag.
* @param {object} viewer
* @param {() => ({west: number, south: number, east: number, north: number}|null)} dragBox
* Degrees, or null while no drag is live.
* @param {{cesium?: object}} [options] The Cesium namespace (tests pass a double).
* @param {{draw?: import('../../render/contract/draw').DrawPort}} [options]
* The draw port (tests pass a double); the viewer's by default.
* @returns {{setVisible: (visible: boolean) => void, changed: () => void, destroy: () => Promise<void>}}
*/
export function createImageryBoxPreview(
viewer,
dragBox,
{ cesium = Cesium } = {},
{ draw = drawOf(viewer) } = {},
) {
const dataSource = new cesium.CustomDataSource(
'gev-recent-imagery-box-preview',
);
let attaching = Promise.resolve(viewer.dataSources.add(dataSource)).catch(
() => null,
);
previewSeq += 1;
const fills = draw.shapes({
id: `recent-imagery-box:${previewSeq}:fill`,
pickable: false,
});
const outlines = draw.lines({
id: `recent-imagery-box:${previewSeq}:outline`,
pickable: false,
});
let visible = false;
let destroyed = false;
const stroke = cesium.Color.fromCssColorString('#8be9ff');
const previewRectangle = () => {
const box = dragBox();
return box
? cesium.Rectangle.fromDegrees(box.west, box.south, box.east, box.north)
: undefined;
};
const previewOutline = () => {
const box = dragBox();
if (!box) return [];
return cesium.Cartesian3.fromDegreesArray([
box.west,
box.south,
box.east,
box.south,
box.east,
box.north,
box.west,
box.north,
box.west,
box.south,
// The sets go at once; every caller gets the same settled promise.
const gone = Promise.resolve();
// The last box drawn, and the last with an area: a box without one keeps
// it, hidden, so the next one moves it rather than draw it anew.
let drawn = null;
let filled = null;
function drawFill(box, show) {
const { west, south, east, north } = box;
fills.upsert([
{
id: 'box',
geometry: { kind: 'rectangle', rect: { west, south, east, north } },
fill: [...PREVIEW_STROKE, PREVIEW_FILL_ALPHA],
height: 'clampGround',
clampTo: 'both',
show,
},
]);
};
const fill = dataSource.entities.add({
show: false,
rectangle: {
coordinates: new cesium.CallbackProperty(previewRectangle, false),
material: stroke.withAlpha(0.12),
classificationType: cesium.ClassificationType.BOTH,
},
});
// A rectangle graphic cannot draw a dashed edge on the ground, so the
// outline is its own draped polyline traced round the same corners.
const outline = dataSource.entities.add({
show: false,
polyline: {
positions: new cesium.CallbackProperty(previewOutline, false),
width: 2,
clampToGround: true,
classificationType: cesium.ClassificationType.BOTH,
material: new cesium.PolylineDashMaterialProperty({
color: stroke.withAlpha(0.95),
dashLength: 12,
}),
},
});
const requestRender = () => viewer.scene.requestRender?.();
}
function drawEdge(box, show) {
const { west, south, east, north } = box;
// A rectangle has no dashed edge on the ground, so the outline is a
// draped line traced round the same corners; a drag along one axis
// draws it as a line along the drag, as the fill has no area.
outlines.upsert([
{
id: 'edge',
positions: [
fromDegrees(west, south),
fromDegrees(east, south),
fromDegrees(east, north),
fromDegrees(west, north),
fromDegrees(west, south),
],
widthPx: 2,
mode: 'draped',
clampTo: 'both',
style: {
kind: 'dash',
color: [...PREVIEW_STROKE, PREVIEW_LINE_ALPHA],
length: 12,
},
show,
},
]);
}
function update() {
if (destroyed) return;
const box = visible ? dragBox() : null;
if (hasArea(box)) {
filled = { ...box };
drawFill(filled, true);
} else if (filled) drawFill(filled, false);
if (hasExtent(box)) {
drawn = { ...box };
drawEdge(drawn, true);
} else if (drawn) drawEdge(drawn, false);
}
return {
setVisible(visible) {
fill.show = visible;
outline.show = visible;
requestRender();
setVisible(on) {
visible = Boolean(on);
update();
},
/** The box moved: draw it again. */
changed: requestRender,
/** Detach the preview; resolves once it is gone. */
changed: update,
/** Take the preview away; resolves once it is gone. */
destroy() {
if (destroyed) return attaching;
destroyed = true;
dataSource.entities.removeAll();
attaching = attaching.then(() => {
try {
viewer.dataSources.remove(dataSource, true);
} catch {
/* viewer already disposed */
}
});
return attaching;
if (!destroyed) {
destroyed = true;
fills.dispose();
outlines.dispose();
}
return gone;
},
};
}
@@ -2,10 +2,12 @@ import test from 'node:test';
import assert from 'node:assert/strict';
import {
TILE_RENDER_REASON,
createImageryBoxPreview,
createRecentImageryRenderer,
} from './rendering.js';
import { BOX } from './testDoubles.mjs';
import { fakeGlobePort } from '../../testSupport/fakeGlobePort.mjs';
import { fakeDrawPort } from '../../testSupport/fakeDrawPort.mjs';
const S30 = { key: 'S30:2026-09-18', product: 'S30', day: '2026-09-18' };
const L30 = { key: 'L30:2026-09-16', product: 'L30', day: '2026-09-16' };
@@ -203,3 +205,69 @@ test('replacement imagery uses source-owned tile URLs and credit and tears down
assert.equal(globe.layers.length, 0);
assert.equal(layer.removed, true);
});
test('the box preview draws a ground fill and a draped dashed edge, hidden rather than dropped between drags', async () => {
const draw = fakeDrawPort();
let box = null;
const preview = createImageryBoxPreview({}, () => box, { draw });
const sets = [...draw.sets.values()];
assert.deepEqual(
sets.map((r) => [r.kind, r.spec.pickable]),
[
['shapes', false],
['lines', false],
],
);
const [fills, lines] = sets;
preview.setVisible(true);
assert.equal(fills.items.size, 0, 'no box yet');
box = { west: -98, south: 30, east: -98, north: 30 };
preview.changed();
assert.equal(fills.items.size, 0, 'a press draws nothing');
assert.equal(lines.items.size, 0);
// Dragged along one axis: the edge draws as a line, the fill has no area.
box = { west: -98, south: 30, east: -97.9, north: 30 };
preview.changed();
assert.equal(fills.items.size, 0, 'no fill without an area');
assert.equal(lines.items.get('edge').show, true, 'the edge along the drag');
box = { west: -98, south: 30, east: -97.9, north: 30.1 };
preview.changed();
const fill = fills.items.get('box');
assert.deepEqual(fill.geometry.rect, box);
assert.equal(fill.height, 'clampGround');
assert.equal(fill.clampTo, 'both');
assert.deepEqual(fill.fill, [139 / 255, 233 / 255, 1, 0.12]);
assert.equal(fill.show, true);
const edge = lines.items.get('edge');
assert.equal(edge.mode, 'draped');
assert.equal(edge.clampTo, 'both');
assert.equal(edge.widthPx, 2);
assert.deepEqual(edge.style, {
kind: 'dash',
color: [139 / 255, 233 / 255, 1, 0.95],
length: 12,
});
assert.equal(edge.positions.length, 5, 'round the corners and back');
assert.deepEqual(edge.positions[0], edge.positions[4]);
preview.setVisible(false);
assert.equal(fills.items.get('box').show, false, 'hidden, kept to move');
assert.equal(lines.items.get('edge').show, false);
// The next drag moves it and shows it again.
box = { west: -97, south: 31, east: -96.5, north: 31.5 };
preview.setVisible(true);
assert.deepEqual(fills.items.get('box').geometry.rect, box);
assert.equal(fills.items.get('box').show, true);
assert.equal(lines.items.get('edge').show, true);
// A press with no extent hides the last box.
box = { west: -97, south: 31, east: -97, north: 31 };
preview.changed();
assert.equal(fills.items.get('box').show, false);
assert.equal(lines.items.get('edge').show, false);
const gone = preview.destroy();
assert.equal(preview.destroy(), gone, 'the same promise every time');
await gone;
assert.ok(fills.disposed && lines.disposed);
// Gone: changes after do nothing.
box = { west: -96, south: 32, east: -95, north: 33 };
assert.doesNotThrow(() => preview.changed());
});