mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-16 08:12:24 +00:00
Merge branch 'sameh/renderer-annotations' into sameh/renderer-seam
This commit is contained in:
commit
aea5c3acc6
16 files changed
+2283
-752
No files matched your search
+504
-43
@@ -239,8 +239,63 @@ function radialRimMaterial(fill) {
|
||||
return material;
|
||||
}
|
||||
|
||||
/**
|
||||
* An ellipse radius at a camera `height`: a fixed one as it is, one by
|
||||
* camera height scaled by its factor and held within its bounds.
|
||||
* @param {number|{byCameraHeight: {factor: number, min: number, max: number}}} radius
|
||||
* @param {number} height
|
||||
*/
|
||||
export function radiusAt(radius, height) {
|
||||
if (typeof radius === 'number') return radius;
|
||||
const { factor, min, max } = radius.byCameraHeight;
|
||||
return Math.max(min, Math.min(max, height * factor));
|
||||
}
|
||||
|
||||
/**
|
||||
* A shape's `animate`, checked: the Cesium renderer pulses alpha, forever.
|
||||
* Null for a shape that does not pulse.
|
||||
*/
|
||||
function pulseOf(item) {
|
||||
const pulse = item.animate;
|
||||
if (!pulse) return null;
|
||||
if (pulse.property !== 'alpha' || pulse.repeat !== undefined)
|
||||
throw new Error('the Cesium renderer pulses alpha only, forever');
|
||||
if (
|
||||
!Number.isFinite(pulse.from) ||
|
||||
!Number.isFinite(pulse.to) ||
|
||||
!(pulse.periodMs > 0 && Number.isFinite(pulse.periodMs))
|
||||
)
|
||||
throw new Error(
|
||||
'the Cesium renderer pulses between finite alphas, over a positive period',
|
||||
);
|
||||
const plain = (value) =>
|
||||
value == null || typeof value === 'string' || Array.isArray(value);
|
||||
if (!plain(item.fill) || !plain(item.outline))
|
||||
throw new Error(
|
||||
'the Cesium renderer pulses colour fills and outlines only',
|
||||
);
|
||||
return pulse;
|
||||
}
|
||||
|
||||
/**
|
||||
* A pulse's alpha now: a sine between `from` and `to` on the wall clock,
|
||||
* `periodMs` a swing and back, at their middle at each whole period.
|
||||
* @param {{from: number, to: number, periodMs: number}} pulse
|
||||
* @param {number} now
|
||||
*/
|
||||
export function pulseAlpha(pulse, now) {
|
||||
const turn = pulse.periodMs > 0 ? (2 * Math.PI * now) / pulse.periodMs : 0;
|
||||
return pulse.from + (pulse.to - pulse.from) * (0.5 + 0.5 * Math.sin(turn));
|
||||
}
|
||||
|
||||
/** Whether a shape is an ellipse with a radius by camera height. */
|
||||
const byCameraHeight = (item) =>
|
||||
item.geometry?.kind === 'ellipse' &&
|
||||
(typeof item.geometry.semiMajor !== 'number' ||
|
||||
typeof item.geometry.semiMinor !== 'number');
|
||||
|
||||
/** The line styles the port drapes. */
|
||||
const DRAPED_STYLES = new Set(['solid', 'glow', 'dash', 'flow']);
|
||||
const DRAPED_STYLES = new Set(['solid', 'glow', 'dash', 'flow', 'arrow']);
|
||||
|
||||
// Whether Cesium's approximate terrain heights have been asked for.
|
||||
let terrainHeightsAsked = false;
|
||||
@@ -424,9 +479,17 @@ export function createCesiumDraw({
|
||||
if (synced === ICONS_CHANGED) setsVersion += 1;
|
||||
requestRender();
|
||||
};
|
||||
// A shape's pulse the renderer cannot run is refused before anything
|
||||
// changes.
|
||||
const checkPulses = (list) => {
|
||||
if (kind !== 'shapes') return;
|
||||
for (const item of list) pulseOf(item);
|
||||
if (list.some((item) => item.animate)) record.mayPulse = true;
|
||||
};
|
||||
const handle = {
|
||||
id: spec.id,
|
||||
set(list) {
|
||||
checkPulses(list);
|
||||
if (onGpu(record, list, true)) {
|
||||
items.clear();
|
||||
for (const item of list) items.set(item.id, item);
|
||||
@@ -442,6 +505,7 @@ export function createCesiumDraw({
|
||||
requestRender();
|
||||
},
|
||||
upsert(list) {
|
||||
checkPulses(list);
|
||||
if (onGpu(record, list)) {
|
||||
for (const item of list) items.set(item.id, item);
|
||||
record.gpu.write(list);
|
||||
@@ -2110,7 +2174,7 @@ export function createCesiumDraw({
|
||||
for (const item of record.items.values())
|
||||
if (item.mode === 'draped' && !DRAPED_STYLES.has(item.style.kind))
|
||||
throw new Error(
|
||||
'the Cesium renderer drapes solid, glow, dash and flow lines only',
|
||||
'the Cesium renderer drapes solid, glow, dash, flow and arrow lines only',
|
||||
);
|
||||
else if (
|
||||
item.frame === 'inertial' &&
|
||||
@@ -2998,6 +3062,14 @@ export function createCesiumDraw({
|
||||
showOn(part.ground, part.id, part.pendingShow);
|
||||
part.pendingShow = undefined;
|
||||
}
|
||||
if (
|
||||
part.pendingColour !== undefined &&
|
||||
part.ground &&
|
||||
!part.ground.isDestroyed()
|
||||
) {
|
||||
colourOn(part.ground, part.id, part.pendingColour);
|
||||
part.pendingColour = undefined;
|
||||
}
|
||||
}
|
||||
for (const part of record.lineParts?.values() ?? []) {
|
||||
if (part.batchHidden && part.batch && !part.batch.isDestroyed())
|
||||
@@ -3105,7 +3177,7 @@ export function createCesiumDraw({
|
||||
? Cesium.Cartographic.fromCartesian(toCartesian(pos)).height
|
||||
: (pos.height ?? 0);
|
||||
|
||||
function shapeGeometry(item) {
|
||||
function shapeGeometry(item, vertexFormat) {
|
||||
const g = item.geometry;
|
||||
const extrudedHeight = item.extrudeHeight;
|
||||
switch (g.kind) {
|
||||
@@ -3115,18 +3187,29 @@ export function createCesiumDraw({
|
||||
outer,
|
||||
holes.map((h) => new Cesium.PolygonHierarchy(h)),
|
||||
);
|
||||
const polygon = {
|
||||
polygonHierarchy: hierarchy,
|
||||
height: g.perPositionHeight ? undefined : heightOf(g.rings[0][0]),
|
||||
extrudedHeight,
|
||||
perPositionHeight: g.perPositionHeight,
|
||||
};
|
||||
return {
|
||||
fill: new Cesium.PolygonGeometry({
|
||||
polygonHierarchy: hierarchy,
|
||||
height: g.perPositionHeight ? undefined : heightOf(g.rings[0][0]),
|
||||
extrudedHeight,
|
||||
perPositionHeight: g.perPositionHeight,
|
||||
}),
|
||||
fill: new Cesium.PolygonGeometry({ ...polygon, vertexFormat }),
|
||||
// Extruded, its outline is the volume's edges (both faces and the
|
||||
// walls' corners), as the entities' extruded polygons draw it.
|
||||
outline:
|
||||
item.outline && extrudedHeight !== undefined
|
||||
? new Cesium.PolygonOutlineGeometry(polygon)
|
||||
: undefined,
|
||||
// Its holes are outlined too, as the entities' polygons outline them.
|
||||
rings: [outer, ...holes],
|
||||
};
|
||||
}
|
||||
case 'ellipse': {
|
||||
if (byCameraHeight(item))
|
||||
throw new Error(
|
||||
'the Cesium renderer takes radii by camera height on clamped ellipses only',
|
||||
);
|
||||
const ellipse = {
|
||||
center: toCartesian(g.center),
|
||||
semiMajorAxis: g.semiMajor,
|
||||
@@ -3136,7 +3219,7 @@ export function createCesiumDraw({
|
||||
extrudedHeight,
|
||||
};
|
||||
return {
|
||||
fill: new Cesium.EllipseGeometry(ellipse),
|
||||
fill: new Cesium.EllipseGeometry({ ...ellipse, vertexFormat }),
|
||||
// Its outline is an outline geometry, as the entities' ellipses draw it.
|
||||
outline: item.outline
|
||||
? new Cesium.EllipseOutlineGeometry(ellipse)
|
||||
@@ -3157,6 +3240,7 @@ export function createCesiumDraw({
|
||||
// An image fill turns with the rectangle, as a KML overlay's does.
|
||||
rotation: g.rotation ?? 0,
|
||||
stRotation: g.rotation ?? 0,
|
||||
vertexFormat,
|
||||
}),
|
||||
};
|
||||
case 'box': {
|
||||
@@ -3164,6 +3248,7 @@ export function createCesiumDraw({
|
||||
return {
|
||||
fill: Cesium.BoxGeometry.fromDimensions({
|
||||
dimensions: new Cesium.Cartesian3(d.x, d.y, d.z),
|
||||
vertexFormat,
|
||||
}),
|
||||
modelMatrix: Cesium.Transforms.eastNorthUpToFixedFrame(
|
||||
toCartesian(g.center),
|
||||
@@ -3175,6 +3260,86 @@ export function createCesiumDraw({
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The camera's height for radii by camera height: above the ellipsoid,
|
||||
* as Cesium places it (1000 m while it has none).
|
||||
*/
|
||||
const cameraHeight = () => {
|
||||
const height = scene.camera?.positionCartographic?.height;
|
||||
return Number.isFinite(height) ? height : 1000;
|
||||
};
|
||||
|
||||
/** A clamped ellipse's radii by camera height at this frame's height, or null. */
|
||||
function radiiOf(item) {
|
||||
if (!byCameraHeight(item)) return null;
|
||||
const height = cameraHeight();
|
||||
return [
|
||||
radiusAt(item.geometry.semiMajor, height),
|
||||
radiusAt(item.geometry.semiMinor, height),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Before a frame draws: clamped ellipses with radii by camera height
|
||||
* whose radii have changed with the camera are drawn at the new ones. A
|
||||
* colour fill is drawn alone where it is, built in this frame
|
||||
* (`moveShape`), as Cesium's entities rebuild a ground ellipse whose
|
||||
* radius follows a callback, while no more than `MOVING_SHAPES` of the
|
||||
* set would be drawn alone; past that, and for any other fill once its
|
||||
* radii have changed by more than `REBUILD_RADII`, its set rebuilds (one
|
||||
* rebuild at a time). Hidden ones wait until they show.
|
||||
*/
|
||||
function followCameraHeight() {
|
||||
for (const record of sets.values()) {
|
||||
const parts = record.shapeParts;
|
||||
if (record.kind !== 'shapes' || !parts || record.spec.show === false)
|
||||
continue;
|
||||
try {
|
||||
followIn(record, parts);
|
||||
} catch (error) {
|
||||
// The set draws as it did; the next frame tries again.
|
||||
report(error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Relative change of radii past which a fill drawn in a batch rebuilds. */
|
||||
const REBUILD_RADII = 0.02;
|
||||
|
||||
function followIn(record, parts) {
|
||||
const changed = (radii, part, by) =>
|
||||
Math.abs(radii[0] - part.radii[0]) > by * part.radii[0] ||
|
||||
Math.abs(radii[1] - part.radii[1]) > by * part.radii[1];
|
||||
let alone = 0;
|
||||
for (const part of parts.values()) if (part.solo) alone += 1;
|
||||
// Those the host still retiring draws alone count too, until it goes.
|
||||
if (record.retiring)
|
||||
for (const part of record.retiringShapeParts?.values() ?? [])
|
||||
if (part.solo && part.ground && !part.ground.isDestroyed()) alone += 1;
|
||||
let rebuild = false;
|
||||
for (const [id, part] of parts) {
|
||||
if (!part.radii || !part.ground) continue;
|
||||
const item = record.items.get(id);
|
||||
if (!item || item.show === false || !byCameraHeight(item)) continue;
|
||||
const radii = radiiOf(item);
|
||||
if (!changed(radii, part, 1e-3)) continue;
|
||||
if (part.ground.isDestroyed() || part.batch?.isDestroyed()) continue;
|
||||
const movable =
|
||||
colourFill(item) &&
|
||||
groundSyncs() &&
|
||||
typeof record.host?.add === 'function' &&
|
||||
(part.solo || alone < MOVING_SHAPES);
|
||||
if (movable) {
|
||||
if (!part.solo) alone += 1;
|
||||
moveShape(record, part, item);
|
||||
} else if (changed(radii, part, REBUILD_RADII)) rebuild = true;
|
||||
}
|
||||
if (rebuild && !record.pending) {
|
||||
record.rebuild();
|
||||
requestRender();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A clamped shape's fill geometry, on the ellipsoid for draping; an
|
||||
* ellipse with a rim fill is cut finely, so its rim runs round.
|
||||
@@ -3191,14 +3356,19 @@ export function createCesiumDraw({
|
||||
),
|
||||
});
|
||||
}
|
||||
case 'ellipse':
|
||||
if (typeof g.semiMajor !== 'number' || typeof g.semiMinor !== 'number')
|
||||
throw new Error('the Cesium renderer takes fixed ellipse radii only');
|
||||
case 'ellipse': {
|
||||
// Radii by camera height are the ones it has at this frame's height.
|
||||
const height = cameraHeight();
|
||||
const major = radiusAt(g.semiMajor, height);
|
||||
const minor = radiusAt(g.semiMinor, height);
|
||||
// Radii that follow the camera apart may cross: the longer is then
|
||||
// drawn as Cesium's semi-major axis, a quarter turn on.
|
||||
const crossed = minor > major;
|
||||
return new Cesium.EllipseGeometry({
|
||||
center: toCartesian(g.center),
|
||||
semiMajorAxis: g.semiMajor,
|
||||
semiMinorAxis: g.semiMinor,
|
||||
rotation: g.rotation ?? 0,
|
||||
semiMajorAxis: crossed ? minor : major,
|
||||
semiMinorAxis: crossed ? major : minor,
|
||||
rotation: (g.rotation ?? 0) + (crossed ? Math.PI / 2 : 0),
|
||||
...(rimFill
|
||||
? {
|
||||
granularity: RIM_GRANULARITY,
|
||||
@@ -3208,6 +3378,7 @@ export function createCesiumDraw({
|
||||
}
|
||||
: {}),
|
||||
});
|
||||
}
|
||||
case 'rectangle':
|
||||
return new Cesium.RectangleGeometry({
|
||||
rectangle: Cesium.Rectangle.fromDegrees(
|
||||
@@ -3279,6 +3450,10 @@ export function createCesiumDraw({
|
||||
look: lookOf(item),
|
||||
id: pickId(record, item),
|
||||
ground: null,
|
||||
// Radii by camera height as drawn (`followCameraHeight`).
|
||||
radii: radiiOf(item),
|
||||
pulse: pulseOf(item),
|
||||
show: item.show !== false,
|
||||
};
|
||||
parts.set(item.id, part);
|
||||
const show = new Cesium.ShowGeometryInstanceAttribute(
|
||||
@@ -3458,25 +3633,37 @@ export function createCesiumDraw({
|
||||
*/
|
||||
function patchShapes(record, list) {
|
||||
const parts = record.shapeParts;
|
||||
if (record.kind !== 'shapes' || !parts) return false;
|
||||
// Each item's way in: `true` moves it (`moveShape`), else it only
|
||||
// shows, hides or takes its `meta`.
|
||||
const solids = record.solidParts;
|
||||
if (record.kind !== 'shapes' || (!parts && !solids)) return false;
|
||||
// Each item's way in: `true` moves it (`moveShape`), `'solid'` changes
|
||||
// an unclamped one in place, `'recolour'` recolours a clamped one in
|
||||
// place, else it only shows, hides or takes its `meta`.
|
||||
const moves = [];
|
||||
for (const item of list) {
|
||||
const was = parts.get(item.id);
|
||||
const solid = solids?.get(item.id);
|
||||
if (solid) {
|
||||
if (!solidPatchable(solid, item)) return false;
|
||||
moves.push('solid');
|
||||
continue;
|
||||
}
|
||||
const was = parts?.get(item.id);
|
||||
if (!was) return false;
|
||||
if (was.ground?.isDestroyed() || was.batch?.isDestroyed()) return false;
|
||||
if (sameLook(was, item)) {
|
||||
moves.push(false);
|
||||
continue;
|
||||
}
|
||||
if (groundRecolourable(was, item)) {
|
||||
moves.push('recolour');
|
||||
continue;
|
||||
}
|
||||
if (!movable(record, was, item)) return false;
|
||||
moves.push(true);
|
||||
}
|
||||
// Drawn alone after this upsert: a few at most, or the batches rebuild.
|
||||
const alone = new Set();
|
||||
for (const [id, part] of parts) if (part.solo) alone.add(id);
|
||||
list.forEach((item, k) => moves[k] && alone.add(item.id));
|
||||
for (const [id, part] of parts ?? []) if (part.solo) alone.add(id);
|
||||
list.forEach((item, k) => moves[k] === true && alone.add(item.id));
|
||||
// Those the host still retiring draws alone count too, until it goes.
|
||||
let retiringAlone = 0;
|
||||
if (record.retiring)
|
||||
@@ -3485,11 +3672,14 @@ export function createCesiumDraw({
|
||||
retiringAlone += 1;
|
||||
if (alone.size + retiringAlone > MOVING_SHAPES) return false;
|
||||
list.forEach((item, k) => {
|
||||
if (moves[k] === 'solid') return patchSolid(solids.get(item.id), item);
|
||||
const was = parts.get(item.id);
|
||||
if (moves[k]) return moveShape(record, was, item);
|
||||
if (moves[k] === true) return moveShape(record, was, item);
|
||||
if (moves[k] === 'recolour') recolourGround(was, item);
|
||||
if (!was.ground) return;
|
||||
was.id.meta = item.meta;
|
||||
const show = item.show !== false;
|
||||
was.show = show;
|
||||
if (was.ground.ready) {
|
||||
showOn(was.ground, was.id, show);
|
||||
was.pendingShow = undefined;
|
||||
@@ -3501,6 +3691,194 @@ export function createCesiumDraw({
|
||||
return true;
|
||||
}
|
||||
|
||||
/** Whether a fill or outline is a plain colour (or none). */
|
||||
const plainColour = (value) =>
|
||||
value == null || typeof value === 'string' || Array.isArray(value);
|
||||
|
||||
/**
|
||||
* How a shape draws but for its colours, pulse, show and `meta`, and
|
||||
* whether it has a fill and an outline: shapes alike in it draw alike,
|
||||
* but for their colours.
|
||||
*/
|
||||
const colourlessLookOf = (item) => {
|
||||
const { show, meta, fill, outline, animate, ...look } = item;
|
||||
return plainCopy({
|
||||
...look,
|
||||
filled: fill != null,
|
||||
outlined: outline != null,
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Whether an upsert changes a shape's colours, its pulse, its show or its
|
||||
* `meta` and nothing else, its fill and outline staying plain colours:
|
||||
* those it takes in place.
|
||||
*/
|
||||
function colourOnly(look, item) {
|
||||
if (!plainColour(item.fill) || !plainColour(item.outline)) return false;
|
||||
if (!plainColour(look.fill) || !plainColour(look.outline)) return false;
|
||||
return sameValues(colourlessLookOf(look), colourlessLookOf(item));
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether a clamped colour fill's upsert recolours it (or changes its
|
||||
* pulse), and changes nothing else but its show and `meta`.
|
||||
*/
|
||||
const groundRecolourable = (was, item) =>
|
||||
Boolean(was.id && was.ground) &&
|
||||
colourFill(item) &&
|
||||
colourFill(was.look) &&
|
||||
colourOnly(was.look, item) &&
|
||||
!sameValues(
|
||||
plainCopy({ fill: was.look.fill, animate: was.look.animate ?? null }),
|
||||
{ fill: item.fill, animate: item.animate ?? null },
|
||||
);
|
||||
|
||||
/**
|
||||
* Whether an unclamped shape's upsert only recolours, pulses, shows or
|
||||
* hides it: a colour drawn opaque cannot become translucent in place.
|
||||
*/
|
||||
function solidPatchable(part, item) {
|
||||
if (!colourOnly(part.look, item)) return false;
|
||||
if (part.fill?.isDestroyed?.() || part.outline?.isDestroyed?.())
|
||||
return false;
|
||||
// Not yet drawn: it has no colours to write, and rebuilds instead.
|
||||
if (
|
||||
(part.fill && !part.fill.ready) ||
|
||||
(part.outline && !part.outline.ready)
|
||||
)
|
||||
return false;
|
||||
const pulses = Boolean(item.animate);
|
||||
const fits = (translucent, colour) =>
|
||||
colour == null || translucent || (!pulses && toColor(colour).alpha >= 1);
|
||||
// Ring outlines are polylines, whose materials take any alpha in place.
|
||||
return (
|
||||
fits(part.translucent.fill, item.fill) &&
|
||||
fits(part.translucent.outline || !part.outline, item.outline)
|
||||
);
|
||||
}
|
||||
|
||||
/** An instance's colour, in a ready primitive. */
|
||||
function colourOn(primitive, id, color) {
|
||||
const attributes = primitive.getGeometryInstanceAttributes(id);
|
||||
attributes.color = Cesium.ColorGeometryInstanceAttribute.toValue(
|
||||
color,
|
||||
attributes.color,
|
||||
);
|
||||
}
|
||||
|
||||
/** An unclamped shape changed in place: its colours, pulse, show and `meta`. */
|
||||
function patchSolid(part, item) {
|
||||
part.look = lookOf(item);
|
||||
part.pulse = pulseOf(item);
|
||||
part.id.meta = item.meta;
|
||||
part.show = item.show !== false;
|
||||
for (const primitive of [part.fill, part.outline])
|
||||
if (primitive) primitive.show = part.show;
|
||||
for (const polyline of part.rings) polyline.show = part.show;
|
||||
paintSolid(
|
||||
part,
|
||||
part.pulse ? pulseAlpha(part.pulse, performance.now()) : null,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Write an unclamped shape's colours where it draws: its own, or with
|
||||
* `alpha` (a pulse's) in place of theirs. A primitive not yet ready
|
||||
* takes them when it is (`runPulses`, or its next patch).
|
||||
*/
|
||||
function paintSolid(part, alpha) {
|
||||
const look = part.look;
|
||||
// Primitives copy the colour they are given; polylines keep theirs.
|
||||
const tint = (colour, result) => {
|
||||
const base = baseColour(part, colour);
|
||||
return alpha === null
|
||||
? Cesium.Color.clone(base, result)
|
||||
: base.withAlpha(alpha, result);
|
||||
};
|
||||
if (look.fill && part.fill?.ready && !part.fill.isDestroyed())
|
||||
colourOn(part.fill, part.id, tint(look.fill, scratchColour));
|
||||
if (look.outline) {
|
||||
if (part.outline?.ready && !part.outline.isDestroyed())
|
||||
colourOn(part.outline, part.id, tint(look.outline, scratchColour));
|
||||
for (const polyline of part.rings)
|
||||
polyline.material.uniforms.color = tint(look.outline);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A clamped colour fill recoloured in place, on its ground primitive when
|
||||
* it is ready, else once it is (`showPending`).
|
||||
*/
|
||||
function recolourGround(part, item) {
|
||||
part.look = lookOf(item);
|
||||
part.pulse = pulseOf(item);
|
||||
const colour = toColor(item.fill);
|
||||
if (part.ground.ready) {
|
||||
colourOn(part.ground, part.id, colour);
|
||||
part.pendingColour = undefined;
|
||||
} else part.pendingColour = colour;
|
||||
}
|
||||
|
||||
/**
|
||||
* Before a frame draws: pulsing shapes take their alpha now, where they
|
||||
* draw (on primitives that are ready). Whether any pulses, so frames go
|
||||
* on being asked for.
|
||||
*/
|
||||
function runPulses() {
|
||||
const now = performance.now();
|
||||
let pulsing = false;
|
||||
for (const record of sets.values()) {
|
||||
if (record.kind !== 'shapes' || record.spec.show === false) continue;
|
||||
if (!record.mayPulse && !record.retiring) continue;
|
||||
for (const part of record.solidParts?.values() ?? []) {
|
||||
if (!part.pulse || !part.show) continue;
|
||||
// Only what it draws pulses: a shape with nothing drawn asks for
|
||||
// no frames.
|
||||
const live = (primitive) => primitive && !primitive.isDestroyed();
|
||||
if (!live(part.fill) && !live(part.outline) && !part.rings.length)
|
||||
continue;
|
||||
pulsing = true;
|
||||
paintSolid(part, pulseAlpha(part.pulse, now));
|
||||
}
|
||||
for (const parts of [record.shapeParts, record.retiringShapeParts])
|
||||
for (const part of parts?.values() ?? []) {
|
||||
if (!part.pulse || !part.show || !part.ground) continue;
|
||||
const ground = part.ground;
|
||||
if (ground.isDestroyed()) continue;
|
||||
// One still building asks for frames, to pulse once it is ready.
|
||||
pulsing = true;
|
||||
if (!ground.ready) continue;
|
||||
colourOn(
|
||||
ground,
|
||||
part.id,
|
||||
baseColour(part, part.look.fill).withAlpha(
|
||||
pulseAlpha(part.pulse, now),
|
||||
scratchColour,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
return pulsing;
|
||||
}
|
||||
|
||||
const scratchColour = new Cesium.Color();
|
||||
|
||||
/**
|
||||
* A part's colour as a Cesium colour, parsed once for each colour its
|
||||
* look holds (a patch gives it a new look, and new values).
|
||||
*/
|
||||
function baseColour(part, colour) {
|
||||
const cache = part.baseColours ?? (part.baseColours = new Map());
|
||||
let base = cache.get(colour);
|
||||
if (!base) {
|
||||
if (cache.size > 4) cache.clear();
|
||||
base = toColor(colour);
|
||||
cache.set(colour, base);
|
||||
}
|
||||
return base;
|
||||
}
|
||||
|
||||
/** Whether a clamped item is a plain colour fill, drawn in a batch. */
|
||||
const colourFill = (item) =>
|
||||
item.height === 'clampGround' &&
|
||||
@@ -3525,13 +3903,7 @@ export function createCesiumDraw({
|
||||
if (!was.id || !was.ground || !colourFill(item)) return false;
|
||||
if (!colourFill(was.look)) return false;
|
||||
if (!sameValues(placedLookOf(was.look), placedLookOf(item))) return false;
|
||||
// Refused geometry (an ellipse radius by camera height) rebuilds, to throw.
|
||||
const g = item.geometry;
|
||||
if (
|
||||
g?.kind === 'ellipse' &&
|
||||
(typeof g.semiMajor !== 'number' || typeof g.semiMinor !== 'number')
|
||||
)
|
||||
return false;
|
||||
if (!['polygon', 'ellipse', 'rectangle'].includes(g?.kind)) return false;
|
||||
return groundSyncs() && typeof record.host?.add === 'function';
|
||||
}
|
||||
@@ -3545,6 +3917,8 @@ export function createCesiumDraw({
|
||||
*/
|
||||
function moveShape(record, part, item) {
|
||||
const host = record.host;
|
||||
// Built first: geometry it cannot build leaves the part as it was.
|
||||
const geometry = groundGeometry(item);
|
||||
if (part.solo) {
|
||||
if (part.ground && !part.ground.isDestroyed()) host.remove(part.ground);
|
||||
} else {
|
||||
@@ -3558,6 +3932,10 @@ export function createCesiumDraw({
|
||||
part.solo = true;
|
||||
part.pendingShow = undefined;
|
||||
part.look = lookOf(item);
|
||||
part.radii = radiiOf(item);
|
||||
part.pulse = pulseOf(item);
|
||||
part.show = item.show !== false;
|
||||
part.pendingColour = undefined;
|
||||
part.id.meta = item.meta;
|
||||
const pickable = record.spec.pickable !== false;
|
||||
const Ground = pickable
|
||||
@@ -3566,7 +3944,7 @@ export function createCesiumDraw({
|
||||
part.ground = new Ground({
|
||||
geometryInstances: [
|
||||
new Cesium.GeometryInstance({
|
||||
geometry: groundGeometry(item),
|
||||
geometry,
|
||||
attributes: {
|
||||
color: Cesium.ColorGeometryInstanceAttribute.fromColor(
|
||||
toColor(item.fill),
|
||||
@@ -3652,10 +4030,35 @@ export function createCesiumDraw({
|
||||
item.extrudeHeight === undefined &&
|
||||
(typeof item.fill !== 'object' || Array.isArray(item.fill));
|
||||
|
||||
/**
|
||||
* Whether an unclamped shape classifies what is inside its volume: one
|
||||
* with a volume (extruded, or a box) and a fill, on a scene that can
|
||||
* classify. Where it cannot, it draws as the volume itself.
|
||||
*/
|
||||
function classifies(item) {
|
||||
if (!item.classifyVolume || !item.fill) return false;
|
||||
if (item.extrudeHeight === undefined && item.geometry.kind !== 'box')
|
||||
return false;
|
||||
try {
|
||||
return Cesium.ClassificationPrimitive.isSupported(scene);
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
function buildShapes(record) {
|
||||
// Refused before anything is built: a pulse it cannot run.
|
||||
record.mayPulse = false;
|
||||
for (const item of record.items.values())
|
||||
if (pulseOf(item)) record.mayPulse = true;
|
||||
const host = new Cesium.PrimitiveCollection();
|
||||
const outlines = new Cesium.PolylineCollection();
|
||||
const clamped = [];
|
||||
// Each unclamped item as drawn, for upserts that only recolour, show or
|
||||
// hide it (`patchShapes`), and its pulse.
|
||||
// Published once the build has gone through.
|
||||
const solids = new Map();
|
||||
record.solidParts = null;
|
||||
// Image fills' materials hold their textures, which primitives leave
|
||||
// alone: the host destroys them as it goes.
|
||||
const materials = [];
|
||||
@@ -3688,13 +4091,61 @@ export function createCesiumDraw({
|
||||
: null;
|
||||
if (imageFill && !imageFill.image)
|
||||
throw new Error('the Cesium renderer fills shapes with colours only');
|
||||
const shape = shapeGeometry(item);
|
||||
// A colour fill's geometry carries what its appearance reads, as the
|
||||
// entities' do. With texture coordinates too, Cesium packs them with
|
||||
// the normals, which a colour appearance then misreads: a lit colour
|
||||
// fill shaded darker than the same entity.
|
||||
const shape = shapeGeometry(
|
||||
item,
|
||||
imageFill
|
||||
? Cesium.MaterialAppearance.MaterialSupport.TEXTURED.vertexFormat
|
||||
: Cesium.PerInstanceColorAppearance.VERTEX_FORMAT,
|
||||
);
|
||||
const id = pickId(record, item);
|
||||
const pulse = pulseOf(item);
|
||||
const part = {
|
||||
look: lookOf(item),
|
||||
id,
|
||||
fill: null,
|
||||
outline: null,
|
||||
rings: [],
|
||||
pulse,
|
||||
// A pulsing colour is drawn translucent whatever its alpha now.
|
||||
translucent: { fill: false, outline: false },
|
||||
show: true,
|
||||
};
|
||||
solids.set(item.id, part);
|
||||
const solid =
|
||||
item.extrudeHeight !== undefined || item.geometry.kind === 'box';
|
||||
// `lit`: shaded by Cesium's light, or flat; by default solids are lit.
|
||||
const lit = item.lit ?? solid;
|
||||
if (imageFill && !item.outlineOnly) {
|
||||
if (classifies(item) && !item.outlineOnly) {
|
||||
if (imageFill)
|
||||
throw new Error(
|
||||
'the Cesium renderer classifies volumes with colours only',
|
||||
);
|
||||
// What lies inside the volume is tinted: the tiles and terrain it
|
||||
// covers, as `clampTo` names them.
|
||||
part.fill = host.add(
|
||||
new Cesium.ClassificationPrimitive({
|
||||
geometryInstances: new Cesium.GeometryInstance({
|
||||
geometry: shape.fill,
|
||||
modelMatrix: shape.modelMatrix,
|
||||
attributes: {
|
||||
color: Cesium.ColorGeometryInstanceAttribute.fromColor(
|
||||
toColor(item.fill),
|
||||
),
|
||||
},
|
||||
id,
|
||||
}),
|
||||
classificationType: classificationOf(
|
||||
item.clampTo ?? record.spec.clampTo ?? 'both',
|
||||
),
|
||||
allowPicking: record.spec.pickable !== false,
|
||||
asynchronous: false,
|
||||
}),
|
||||
);
|
||||
} else if (imageFill && !item.outlineOnly) {
|
||||
const material = ownMaterial(
|
||||
Cesium.Material.fromType('Image', {
|
||||
image: toImage(imageFill.image),
|
||||
@@ -3721,7 +4172,8 @@ export function createCesiumDraw({
|
||||
);
|
||||
} else if (item.fill && !item.outlineOnly) {
|
||||
const color = toColor(item.fill);
|
||||
host.add(
|
||||
part.translucent.fill = color.alpha < 1 || Boolean(pulse);
|
||||
part.fill = host.add(
|
||||
new Cesium.Primitive({
|
||||
geometryInstances: new Cesium.GeometryInstance({
|
||||
geometry: shape.fill,
|
||||
@@ -3732,7 +4184,7 @@ export function createCesiumDraw({
|
||||
id,
|
||||
}),
|
||||
appearance: new Cesium.PerInstanceColorAppearance({
|
||||
translucent: color.alpha < 1,
|
||||
translucent: part.translucent.fill,
|
||||
closed: solid,
|
||||
// Flat shapes unlit by default: lit, their faces shade by
|
||||
// the light (a lit box's sides differ, an unlit one's match).
|
||||
@@ -3745,7 +4197,8 @@ export function createCesiumDraw({
|
||||
}
|
||||
if (item.outline && shape.outline) {
|
||||
const color = toColor(item.outline);
|
||||
host.add(
|
||||
part.translucent.outline = color.alpha < 1 || Boolean(pulse);
|
||||
part.outline = host.add(
|
||||
new Cesium.Primitive({
|
||||
geometryInstances: new Cesium.GeometryInstance({
|
||||
geometry: shape.outline,
|
||||
@@ -3756,7 +4209,7 @@ export function createCesiumDraw({
|
||||
}),
|
||||
appearance: new Cesium.PerInstanceColorAppearance({
|
||||
flat: true,
|
||||
translucent: color.alpha < 1,
|
||||
translucent: part.translucent.outline,
|
||||
renderState: {
|
||||
// Clamped to what the context draws, as entities do.
|
||||
lineWidth:
|
||||
@@ -3773,17 +4226,20 @@ export function createCesiumDraw({
|
||||
);
|
||||
} else if (item.outline && shape.rings)
|
||||
for (const ring of shape.rings)
|
||||
outlines.add({
|
||||
positions: [...ring, ring[0]],
|
||||
width: item.outlineWidth ?? 1,
|
||||
material: Cesium.Material.fromType('Color', {
|
||||
color: toColor(item.outline),
|
||||
part.rings.push(
|
||||
outlines.add({
|
||||
positions: [...ring, ring[0]],
|
||||
width: item.outlineWidth ?? 1,
|
||||
material: Cesium.Material.fromType('Color', {
|
||||
color: toColor(item.outline),
|
||||
}),
|
||||
id,
|
||||
}),
|
||||
id,
|
||||
});
|
||||
);
|
||||
}
|
||||
host.add(outlines);
|
||||
const ground = groundFills(record, clamped);
|
||||
record.solidParts = solids;
|
||||
if (!ground.length) return replaceHost(record, host);
|
||||
replaceHost(
|
||||
record,
|
||||
@@ -3805,6 +4261,8 @@ export function createCesiumDraw({
|
||||
requestRender,
|
||||
});
|
||||
const buildModels = (record) => modelSets.build(record);
|
||||
// Whether shapes pulsed in the last frame, which draws on in the next.
|
||||
let pulsing = false;
|
||||
const lineWidth = (px) =>
|
||||
typeof scene.clampLineWidth === 'function'
|
||||
? scene.clampLineWidth(px)
|
||||
@@ -3921,6 +4379,8 @@ export function createCesiumDraw({
|
||||
building: () => [...sets.values()].some((record) => record.pending),
|
||||
/** Each frame, before it draws: the horizon cull. */
|
||||
beforeRender() {
|
||||
followCameraHeight();
|
||||
pulsing = runPulses();
|
||||
cullHorizon();
|
||||
runFlows();
|
||||
for (const record of sets.values())
|
||||
@@ -3953,6 +4413,7 @@ export function createCesiumDraw({
|
||||
if (
|
||||
[...sets.values()].some((record) => record.gpu?.animating()) ||
|
||||
flowing() ||
|
||||
pulsing ||
|
||||
playing
|
||||
)
|
||||
requestRender();
|
||||
|
||||
+733
-23
@@ -452,14 +452,22 @@ test('a clamped fill that only shows, hides or changes meta is changed in place'
|
||||
});
|
||||
frame();
|
||||
const moved = scene.added.at(-1);
|
||||
// Anything else rebuilds: a new colour, a new item.
|
||||
// A new colour is written in place too.
|
||||
const colours = new Map();
|
||||
groundOf(moved).getGeometryInstanceAttributes = (id) => {
|
||||
if (!colours.has(id.id))
|
||||
colours.set(id.id, {
|
||||
color: new Uint8Array(4),
|
||||
show: new Uint8Array([1]),
|
||||
});
|
||||
return colours.get(id.id);
|
||||
};
|
||||
set.upsert([{ ...fill('a', true), fill: [0, 1, 0, 0.3] }]);
|
||||
assert.notEqual(scene.added.at(-1), moved, 'a new look rebuilds');
|
||||
const rebuilt = scene.added.at(-1);
|
||||
Object.defineProperty(groundOf(rebuilt), 'ready', { value: true });
|
||||
frame();
|
||||
assert.equal(scene.added.at(-1), moved, 'a new colour does not rebuild');
|
||||
assert.deepEqual([...colours.get('a').color], [0, 255, 0, 76]);
|
||||
// A new item rebuilds.
|
||||
set.upsert([fill('c', true)]);
|
||||
assert.notEqual(scene.added.at(-1), rebuilt, 'a new item rebuilds');
|
||||
assert.notEqual(scene.added.at(-1), moved, 'a new item rebuilds');
|
||||
set.dispose();
|
||||
assert.equal(scene.added.length, 0);
|
||||
});
|
||||
@@ -503,10 +511,9 @@ test('hidden clamped fills wait for nothing, and the hidden ones it cannot build
|
||||
fill('c', 2, false, { fill: { preset: 'radialRim' } }),
|
||||
fill('d', 3, false, {
|
||||
geometry: {
|
||||
kind: 'ellipse',
|
||||
kind: 'box',
|
||||
center: { lon: 3, lat: 0 },
|
||||
semiMajor: { byCameraHeight: { factor: 1, min: 1, max: 2 } },
|
||||
semiMinor: 1000,
|
||||
dimensions: { x: 1, y: 1, z: 1 },
|
||||
},
|
||||
}),
|
||||
]),
|
||||
@@ -616,22 +623,532 @@ test('clamped shapes refuse what they cannot draw', () => {
|
||||
]),
|
||||
/colours only/,
|
||||
);
|
||||
set.dispose();
|
||||
// Radii by camera height are for clamped ellipses alone.
|
||||
const unclamped = draw.shapes({ id: 'refuse-unclamped' });
|
||||
assert.throws(
|
||||
() =>
|
||||
set.set([
|
||||
unclamped.set([
|
||||
{
|
||||
...disc,
|
||||
height: 'absolute',
|
||||
geometry: {
|
||||
...disc.geometry,
|
||||
semiMajor: { byCameraHeight: { factor: 1, min: 1, max: 2 } },
|
||||
},
|
||||
},
|
||||
]),
|
||||
/fixed ellipse radii/,
|
||||
/radii by camera height on clamped ellipses only/,
|
||||
);
|
||||
unclamped.dispose();
|
||||
});
|
||||
|
||||
test('a clamped ellipse with radii by camera height draws at the camera height and follows it', () =>
|
||||
withTerrainHeights(() => {
|
||||
const scene = fakeScene();
|
||||
let height = 2000;
|
||||
Object.defineProperty(scene.camera, 'positionCartographic', {
|
||||
get: () => ({ height }),
|
||||
});
|
||||
const { draw, frame } = drawOver(scene);
|
||||
const set = draw.shapes({ id: 'rings' });
|
||||
const radius = { byCameraHeight: { factor: 0.03, min: 14, max: 170 } };
|
||||
const ring = (show = true) => ({
|
||||
id: 'ring',
|
||||
geometry: {
|
||||
kind: 'ellipse',
|
||||
center: { lon: 0, lat: 0 },
|
||||
semiMajor: radius,
|
||||
semiMinor: radius,
|
||||
},
|
||||
fill: [0.5, 0.9, 1, 0.38],
|
||||
height: 'clampGround',
|
||||
show,
|
||||
});
|
||||
set.set([ring()]);
|
||||
const host = scene.added[0];
|
||||
const radiusIn = (ground) =>
|
||||
ground.geometryInstances[0].geometry._semiMajorAxis;
|
||||
const last = () => host.get(host.length - 1);
|
||||
assert.equal(radiusIn(last()), 60, '3% of 2000 m');
|
||||
// The camera has not moved: nothing is built.
|
||||
const built = last();
|
||||
frame();
|
||||
assert.equal(last(), built);
|
||||
// It climbs: the ring is drawn wider in the frame that draws it, alone.
|
||||
height = 4000;
|
||||
frame();
|
||||
const wider = last();
|
||||
assert.notEqual(wider, built);
|
||||
assert.equal(wider.asynchronous, false);
|
||||
assert.equal(radiusIn(wider), 120);
|
||||
assert.equal(wider.geometryInstances[0].id.id, 'ring', 'its own pick id');
|
||||
// Held within its bounds.
|
||||
height = 1e6;
|
||||
frame();
|
||||
assert.equal(radiusIn(last()), 170);
|
||||
height = 10;
|
||||
frame();
|
||||
assert.equal(radiusIn(last()), 14);
|
||||
// Hidden, it waits; shown, it catches up.
|
||||
set.upsert([ring(false)]);
|
||||
const lastShown = last();
|
||||
height = 3000;
|
||||
frame();
|
||||
assert.equal(last(), lastShown, 'hidden: not rebuilt');
|
||||
set.upsert([ring(true)]);
|
||||
frame();
|
||||
assert.equal(radiusIn(last()), 90);
|
||||
assert.equal(scene.added[0], host, 'never a rebuild of the set');
|
||||
set.dispose();
|
||||
assert.equal(scene.added.length, 0);
|
||||
}));
|
||||
|
||||
test('radii by camera height that cross draw the longer as the major axis; past the cap of shapes drawn alone, the set rebuilds', () =>
|
||||
withTerrainHeights(() => {
|
||||
const scene = fakeScene();
|
||||
let height = 1000;
|
||||
Object.defineProperty(scene.camera, 'positionCartographic', {
|
||||
get: () => ({ height }),
|
||||
});
|
||||
const { draw, frame } = drawOver(scene);
|
||||
const set = draw.shapes({ id: 'crossing' });
|
||||
const ring = (id, lon) => ({
|
||||
id,
|
||||
geometry: {
|
||||
kind: 'ellipse',
|
||||
center: { lon, lat: 0 },
|
||||
semiMajor: { byCameraHeight: { factor: 0.03, min: 14, max: 170 } },
|
||||
semiMinor: { byCameraHeight: { factor: 0.05, min: 20, max: 100 } },
|
||||
rotation: 0.25,
|
||||
},
|
||||
fill: [0.5, 0.9, 1, 0.38],
|
||||
height: 'clampGround',
|
||||
});
|
||||
const rings = Array.from({ length: MOVING_SHAPES + 4 }, (_, i) =>
|
||||
ring(`r${i}`, i * 0.1),
|
||||
);
|
||||
assert.doesNotThrow(() => set.set(rings));
|
||||
const first = scene.added[0];
|
||||
const ground = (host) => {
|
||||
for (let i = 0; i < host.length; i++)
|
||||
if (host.get(i) instanceof Cesium.GroundPrimitive) return host.get(i);
|
||||
return null;
|
||||
};
|
||||
const geometry = ground(first).geometryInstances[0].geometry;
|
||||
assert.equal(geometry._semiMajorAxis, 50, 'the longer, 5% of 1000 m');
|
||||
assert.equal(geometry._semiMinorAxis, 30);
|
||||
assert.ok(Math.abs(geometry._rotation - (0.25 + Math.PI / 2)) < 1e-12);
|
||||
// Its batches built.
|
||||
for (let i = 0; i < first.length; i++)
|
||||
if (first.get(i) instanceof Cesium.GroundPrimitive) {
|
||||
Object.defineProperty(first.get(i), 'ready', { value: true });
|
||||
first.get(i).getGeometryInstanceAttributes = () => ({
|
||||
show: new Uint8Array([1]),
|
||||
});
|
||||
}
|
||||
frame();
|
||||
// The camera climbs: no more than the cap are drawn alone; the set
|
||||
// rebuilds for the rest, and nothing throws in the frame.
|
||||
height = 1500;
|
||||
assert.doesNotThrow(() => frame());
|
||||
let solo = 0;
|
||||
for (let i = 0; i < first.length; i++) {
|
||||
const p = first.get(i);
|
||||
if (p instanceof Cesium.GroundPrimitive && p.asynchronous === false)
|
||||
solo += 1;
|
||||
}
|
||||
assert.equal(solo, MOVING_SHAPES);
|
||||
assert.notEqual(scene.added.at(-1), first, 'rebuilt for the rest');
|
||||
set.dispose();
|
||||
}));
|
||||
|
||||
test('rings drawn alone by a host still retiring count toward the cap when the camera moves again', () =>
|
||||
withTerrainHeights(() => {
|
||||
const scene = fakeScene();
|
||||
let height = 1000;
|
||||
Object.defineProperty(scene.camera, 'positionCartographic', {
|
||||
get: () => ({ height }),
|
||||
});
|
||||
const { draw, frame } = drawOver(scene);
|
||||
const set = draw.shapes({ id: 'climbing' });
|
||||
const radius = { byCameraHeight: { factor: 0.03, min: 14, max: 170 } };
|
||||
const rings = Array.from({ length: MOVING_SHAPES + 4 }, (_, i) => ({
|
||||
id: `r${i}`,
|
||||
geometry: {
|
||||
kind: 'ellipse',
|
||||
center: { lon: i * 0.1, lat: 0 },
|
||||
semiMajor: radius,
|
||||
semiMinor: radius,
|
||||
},
|
||||
fill: [0.5, 0.9, 1, 0.38],
|
||||
height: 'clampGround',
|
||||
}));
|
||||
const ready = (host) => {
|
||||
for (let i = 0; i < host.length; i++)
|
||||
if (host.get(i) instanceof Cesium.GroundPrimitive) {
|
||||
Object.defineProperty(host.get(i), 'ready', { value: true });
|
||||
host.get(i).getGeometryInstanceAttributes = () => ({
|
||||
show: new Uint8Array([1]),
|
||||
});
|
||||
}
|
||||
};
|
||||
const solo = (host) => {
|
||||
let n = 0;
|
||||
for (let i = 0; i < host.length; i++) {
|
||||
const p = host.get(i);
|
||||
if (p instanceof Cesium.GroundPrimitive && p.asynchronous === false)
|
||||
n += 1;
|
||||
}
|
||||
return n;
|
||||
};
|
||||
set.set(rings);
|
||||
const first = scene.added[0];
|
||||
ready(first);
|
||||
frame();
|
||||
// A climb: the cap drawn alone, and a rebuild for the rest.
|
||||
height = 1500;
|
||||
frame();
|
||||
assert.equal(solo(first), MOVING_SHAPES);
|
||||
const second = scene.added.at(-1);
|
||||
assert.notEqual(second, first, 'rebuilding, the old host retiring');
|
||||
ready(second);
|
||||
// Climbing on while the old host retires: its rings drawn alone still
|
||||
// count, so none more are drawn alone.
|
||||
height = 2500;
|
||||
frame();
|
||||
assert.equal(solo(second), 0, 'nothing more drawn alone');
|
||||
set.dispose();
|
||||
}));
|
||||
|
||||
test('shapes that only change colour, pulse, show or meta change in place; a pulse runs on the frames', () =>
|
||||
withTerrainHeights(() => {
|
||||
const scene = fakeScene();
|
||||
const asked = [];
|
||||
scene.requestRender = () => asked.push(1);
|
||||
const { draw, frame } = drawOver(scene);
|
||||
const set = draw.shapes({ id: 'fading' });
|
||||
const ring = [
|
||||
{ lon: 0, lat: 0, height: 10 },
|
||||
{ lon: 0.001, lat: 0, height: 10 },
|
||||
{ lon: 0.001, lat: 0.001, height: 10 },
|
||||
];
|
||||
const pulse = (alpha) => ({
|
||||
property: 'alpha',
|
||||
from: alpha * 0.6,
|
||||
to: alpha,
|
||||
periodMs: 1000,
|
||||
});
|
||||
const column = (alpha, extra = {}) => ({
|
||||
id: 'column',
|
||||
geometry: { kind: 'polygon', rings: [ring] },
|
||||
fill: [1, 0.7, 0.3, 0.45 * alpha],
|
||||
animate: pulse(0.45 * alpha),
|
||||
extrudeHeight: 100,
|
||||
height: 'absolute',
|
||||
...extra,
|
||||
});
|
||||
const cage = (alpha) => ({
|
||||
id: 'cage',
|
||||
geometry: { kind: 'polygon', rings: [ring] },
|
||||
outline: [1, 0.7, 0.3, 0.32 * alpha],
|
||||
outlineOnly: true,
|
||||
animate: pulse(0.32 * alpha),
|
||||
extrudeHeight: 100,
|
||||
height: 'absolute',
|
||||
});
|
||||
const fill = (alpha) => ({
|
||||
id: 'fill',
|
||||
geometry: {
|
||||
kind: 'polygon',
|
||||
rings: [ring.map(({ lon, lat }) => ({ lon: lon + 1, lat }))],
|
||||
},
|
||||
fill: [0.4, 1, 0.6, 0.2 * alpha],
|
||||
animate: pulse(0.2 * alpha),
|
||||
height: 'clampGround',
|
||||
});
|
||||
set.set([column(0), cage(0), fill(0)]);
|
||||
const host = scene.added[0];
|
||||
const solids = host.get(0);
|
||||
const [columnPrimitive, cagePrimitive] = [solids.get(0), solids.get(1)];
|
||||
let ground = null;
|
||||
for (let i = 0; i < host.length; i++)
|
||||
if (host.get(i) instanceof Cesium.GroundPrimitive) ground = host.get(i);
|
||||
// A pulsing colour draws translucent, whatever its alpha now.
|
||||
assert.equal(columnPrimitive.appearance.translucent, true);
|
||||
assert.equal(cagePrimitive.appearance.translucent, true);
|
||||
// Ready, with instance attributes to write.
|
||||
const colours = new Map();
|
||||
const ready = (primitive, key) => {
|
||||
Object.defineProperty(primitive, 'ready', { value: true });
|
||||
primitive.getGeometryInstanceAttributes = () => {
|
||||
if (!colours.has(key))
|
||||
colours.set(key, {
|
||||
color: new Uint8Array(4),
|
||||
show: new Uint8Array([1]),
|
||||
});
|
||||
return colours.get(key);
|
||||
};
|
||||
};
|
||||
ready(columnPrimitive, 'column');
|
||||
ready(cagePrimitive, 'cage');
|
||||
ready(ground, 'fill');
|
||||
frame();
|
||||
// Faded in: the same primitives take it.
|
||||
set.upsert([column(1, { meta: 'm' }), cage(1), fill(1)]);
|
||||
assert.equal(scene.added[0], host, 'not rebuilt');
|
||||
assert.equal(solids.get(0), columnPrimitive);
|
||||
assert.equal(columnPrimitive.geometryInstances.id.meta, 'm');
|
||||
// The pulse writes each frame between 60% and all of the alpha, and asks
|
||||
// for the next frame.
|
||||
const seen = { column: [], cage: [], fill: [] };
|
||||
const realNow = performance.now;
|
||||
let now = 0;
|
||||
performance.now = () => now;
|
||||
try {
|
||||
for (now = 0; now < 1000; now += 50) {
|
||||
asked.length = 0;
|
||||
frame();
|
||||
assert.ok(asked.length > 0, 'a pulse asks for frames');
|
||||
for (const key of Object.keys(seen))
|
||||
seen[key].push(colours.get(key).color[3] / 255);
|
||||
}
|
||||
} finally {
|
||||
performance.now = realNow;
|
||||
}
|
||||
const range = (values) => [Math.min(...values), Math.max(...values)];
|
||||
const near = (a, b) => Math.abs(a - b) < 0.01;
|
||||
assert.ok(
|
||||
near(range(seen.column)[0], 0.27) && near(range(seen.column)[1], 0.45),
|
||||
`${range(seen.column)}`,
|
||||
);
|
||||
assert.ok(
|
||||
near(range(seen.cage)[0], 0.192) && near(range(seen.cage)[1], 0.32),
|
||||
`${range(seen.cage)}`,
|
||||
);
|
||||
assert.ok(
|
||||
near(range(seen.fill)[0], 0.12) && near(range(seen.fill)[1], 0.2),
|
||||
`${range(seen.fill)}`,
|
||||
);
|
||||
// Hidden in place: its primitives hide, and stop pulsing.
|
||||
set.upsert([column(1, { show: false })]);
|
||||
assert.equal(scene.added[0], host);
|
||||
assert.equal(columnPrimitive.show, false);
|
||||
// An opaque colour drawn translucent is fine in place; a shape that
|
||||
// changes more than its colours rebuilds.
|
||||
set.upsert([column(1, { extrudeHeight: 200 })]);
|
||||
assert.notEqual(scene.added.at(-1), host, 'rebuilt');
|
||||
set.dispose();
|
||||
}));
|
||||
|
||||
test('colour fills carry what their appearance reads, as entities do', () =>
|
||||
withImageClasses(() => {
|
||||
const { scene, draw } = drawOver();
|
||||
const set = draw.shapes({ id: 'formats' });
|
||||
const ring = [
|
||||
{ lon: 0, lat: 0, height: 10 },
|
||||
{ lon: 0.001, lat: 0, height: 10 },
|
||||
{ lon: 0.001, lat: 0.001, height: 10 },
|
||||
];
|
||||
set.set([
|
||||
{
|
||||
id: 'column',
|
||||
geometry: { kind: 'polygon', rings: [ring] },
|
||||
fill: [1, 0.7, 0.3, 0.45],
|
||||
extrudeHeight: 100,
|
||||
height: 'absolute',
|
||||
},
|
||||
{
|
||||
id: 'box',
|
||||
geometry: {
|
||||
kind: 'box',
|
||||
center: { lon: 1, lat: 0, height: 10 },
|
||||
dimensions: { x: 10, y: 10, z: 10 },
|
||||
},
|
||||
fill: '#fff',
|
||||
height: 'absolute',
|
||||
},
|
||||
{
|
||||
id: 'disc',
|
||||
geometry: {
|
||||
kind: 'ellipse',
|
||||
center: { lon: 2, lat: 0, height: 10 },
|
||||
semiMajor: 50,
|
||||
semiMinor: 30,
|
||||
},
|
||||
fill: '#fff',
|
||||
extrudeHeight: 40,
|
||||
height: 'absolute',
|
||||
},
|
||||
{
|
||||
id: 'slab',
|
||||
geometry: {
|
||||
kind: 'rectangle',
|
||||
rect: { west: 3, south: 0, east: 3.01, north: 0.01 },
|
||||
height: 10,
|
||||
},
|
||||
fill: '#fff',
|
||||
extrudeHeight: 40,
|
||||
height: 'absolute',
|
||||
},
|
||||
]);
|
||||
const host = scene.added[0];
|
||||
const formats = [];
|
||||
const walk = (c) => {
|
||||
for (let i = 0; i < c.length; i++) {
|
||||
const p = c.get(i);
|
||||
if (typeof p.length === 'number' && p.get) walk(p);
|
||||
else if (p instanceof Cesium.Primitive && p.geometryInstances?.geometry)
|
||||
formats.push(p.geometryInstances.geometry._vertexFormat);
|
||||
}
|
||||
};
|
||||
walk(host);
|
||||
assert.equal(formats.length, 4);
|
||||
// Position and normal only: with texture coordinates too, Cesium packs
|
||||
// them with the normals, and the colour appearance misreads them.
|
||||
for (const format of formats) {
|
||||
assert.equal(format.normal, true);
|
||||
assert.equal(format.st, false);
|
||||
}
|
||||
set.dispose();
|
||||
}));
|
||||
|
||||
test('a pulse the Cesium renderer cannot run is refused before anything changes', () => {
|
||||
const { scene, draw } = drawOver();
|
||||
const set = draw.shapes({ id: 'pulses' });
|
||||
const disc = (animate) => ({
|
||||
id: 'a',
|
||||
geometry: {
|
||||
kind: 'ellipse',
|
||||
center: { lon: 0, lat: 0 },
|
||||
semiMajor: 1000,
|
||||
semiMinor: 1000,
|
||||
},
|
||||
fill: '#fff',
|
||||
height: 'clampGround',
|
||||
animate,
|
||||
});
|
||||
set.set([disc(undefined)]);
|
||||
const host = scene.added[0];
|
||||
assert.throws(
|
||||
() => set.set([disc({ property: 'radius', from: 1, to: 2, periodMs: 1 })]),
|
||||
/pulses alpha only, forever/,
|
||||
);
|
||||
assert.throws(
|
||||
() =>
|
||||
set.upsert([
|
||||
disc({ property: 'alpha', from: 0, to: 1, periodMs: 1, repeat: 2 }),
|
||||
]),
|
||||
/pulses alpha only, forever/,
|
||||
);
|
||||
assert.equal(scene.added[0], host, 'still drawn as it was');
|
||||
// Behind an item that would rebuild, and in a whole new list, too.
|
||||
const radius = { property: 'radius', from: 1, to: 2, periodMs: 1 };
|
||||
assert.throws(
|
||||
() => set.upsert([{ ...disc(undefined), id: 'b' }, disc(radius)]),
|
||||
/pulses alpha only, forever/,
|
||||
);
|
||||
assert.throws(() => set.set([disc(radius)]), /pulses alpha only, forever/);
|
||||
assert.equal(scene.added[0], host, 'still drawn as it was');
|
||||
assert.doesNotThrow(() => set.upsert([{ ...disc(undefined), id: 'c' }]));
|
||||
// Nor numbers it cannot draw, nor a fill that is not a colour.
|
||||
assert.throws(
|
||||
() =>
|
||||
set.upsert([disc({ property: 'alpha', from: 0, to: 1, periodMs: 0 })]),
|
||||
/positive period/,
|
||||
);
|
||||
assert.throws(
|
||||
() =>
|
||||
set.upsert([
|
||||
{
|
||||
...disc({ property: 'alpha', from: 0, to: 1, periodMs: 1000 }),
|
||||
fill: { image: 'x.png' },
|
||||
},
|
||||
]),
|
||||
/colour fills and outlines only/,
|
||||
);
|
||||
set.dispose();
|
||||
});
|
||||
|
||||
test('a pulsing ring outline changes in place; a pulsing shape that draws nothing asks for no frames', () =>
|
||||
withImageClasses(() => {
|
||||
const scene = fakeScene();
|
||||
const asked = [];
|
||||
scene.requestRender = () => asked.push(1);
|
||||
const { draw, frame } = drawOver(scene);
|
||||
const set = draw.shapes({ id: 'rings-pulse' });
|
||||
const ring = [
|
||||
{ lon: 0, lat: 0, height: 10 },
|
||||
{ lon: 0.001, lat: 0, height: 10 },
|
||||
{ lon: 0.001, lat: 0.001, height: 10 },
|
||||
];
|
||||
const pulse = { property: 'alpha', from: 0.2, to: 0.5, periodMs: 1000 };
|
||||
const outlined = (extra = {}) => ({
|
||||
id: 'o',
|
||||
geometry: { kind: 'polygon', rings: [ring] },
|
||||
outline: [1, 1, 1, 0.5],
|
||||
outlineOnly: true,
|
||||
animate: pulse,
|
||||
height: 'absolute',
|
||||
...extra,
|
||||
});
|
||||
set.set([outlined()]);
|
||||
const host = scene.added[0];
|
||||
set.upsert([outlined({ meta: 'm' })]);
|
||||
assert.equal(scene.added[0], host, 'not rebuilt');
|
||||
set.set([
|
||||
{
|
||||
id: 'nothing',
|
||||
geometry: { kind: 'polygon', rings: [ring] },
|
||||
animate: pulse,
|
||||
height: 'absolute',
|
||||
},
|
||||
]);
|
||||
frame();
|
||||
asked.length = 0;
|
||||
frame();
|
||||
assert.equal(asked.length, 0, 'nothing drawn, no frames asked');
|
||||
set.dispose();
|
||||
}));
|
||||
|
||||
test('an unclamped shape recoloured before it is drawn rebuilds; frames stop once nothing pulses', () =>
|
||||
withTerrainHeights(() => {
|
||||
const scene = fakeScene();
|
||||
const asked = [];
|
||||
scene.requestRender = () => asked.push(1);
|
||||
const { draw, frame } = drawOver(scene);
|
||||
const set = draw.shapes({ id: 'early' });
|
||||
const ring = [
|
||||
{ lon: 0, lat: 0, height: 10 },
|
||||
{ lon: 0.001, lat: 0, height: 10 },
|
||||
{ lon: 0.001, lat: 0.001, height: 10 },
|
||||
];
|
||||
const column = (alpha, extra = {}) => ({
|
||||
id: 'column',
|
||||
geometry: { kind: 'polygon', rings: [ring] },
|
||||
fill: [1, 0.7, 0.3, alpha],
|
||||
extrudeHeight: 100,
|
||||
height: 'absolute',
|
||||
...extra,
|
||||
});
|
||||
set.set([column(0.4)]);
|
||||
const host = scene.added[0];
|
||||
set.upsert([column(0.5)]);
|
||||
assert.notEqual(scene.added.at(-1), host, 'not yet drawn: rebuilt');
|
||||
// Pulsing, it asks for frames; hidden, it stops.
|
||||
const pulse = { property: 'alpha', from: 0.2, to: 0.5, periodMs: 1000 };
|
||||
set.set([column(0.5, { animate: pulse })]);
|
||||
asked.length = 0;
|
||||
frame();
|
||||
assert.ok(asked.length > 0, 'a pulse asks for frames');
|
||||
set.set([column(0.5, { animate: pulse, show: false })]);
|
||||
frame();
|
||||
asked.length = 0;
|
||||
frame();
|
||||
assert.equal(asked.length, 0, 'nothing pulses, no frames asked');
|
||||
set.dispose();
|
||||
}));
|
||||
|
||||
test('the earthquakes layer draws its discs as ground fills on a viewer of its own', async () => {
|
||||
const { createEarthquakesLayer } =
|
||||
await import('../../layers/earthquakes/index.js');
|
||||
@@ -669,6 +1186,101 @@ test('the earthquakes layer draws its discs as ground fills on a viewer of its o
|
||||
assert.equal(scene.added.length, 0);
|
||||
});
|
||||
|
||||
test('the world annotation renderer draws, fades, hides and lets go of every kind on a viewer of its own', async () => {
|
||||
const { createWorldAnnotationRenderer } =
|
||||
await import('../../annotations/worldAnnotationRenderer.js');
|
||||
await withImageClasses(() =>
|
||||
withTerrainHeights(() => {
|
||||
const scene = fakeScene();
|
||||
const viewer = { scene };
|
||||
const renderer = createWorldAnnotationRenderer(viewer);
|
||||
const box = (lon, d) => [
|
||||
[lon - d, -d],
|
||||
[lon + d, -d],
|
||||
[lon + d, d],
|
||||
[lon - d, d],
|
||||
];
|
||||
const marks = [
|
||||
{
|
||||
id: 'building',
|
||||
type: 'area',
|
||||
color: 'amber',
|
||||
footprintKind: 'building',
|
||||
buildingHeight: 40,
|
||||
anchor: { lon: 0, lat: 0, height: 200 },
|
||||
ring: box(0, 0.0004),
|
||||
},
|
||||
{
|
||||
id: 'area',
|
||||
type: 'area',
|
||||
color: 'green',
|
||||
footprintKind: 'area',
|
||||
anchor: { lon: 0.01, lat: 0 },
|
||||
ring: box(0.01, 0.002),
|
||||
polygons: [[box(0.01, 0.002), box(0.01, 0.0005)]],
|
||||
},
|
||||
{
|
||||
id: 'synth',
|
||||
type: 'area',
|
||||
color: 'cyan',
|
||||
synthesized: true,
|
||||
anchor: { lon: 0.02, lat: 0 },
|
||||
ring: box(0.02, 0.002),
|
||||
},
|
||||
{
|
||||
id: 'route',
|
||||
type: 'route',
|
||||
color: 'red',
|
||||
anchor: { lon: 0, lat: 0.01 },
|
||||
path: [
|
||||
{ lon: 0, lat: 0.01 },
|
||||
{ lon: 0.01, lat: 0.012 },
|
||||
],
|
||||
},
|
||||
{
|
||||
id: 'arrow',
|
||||
type: 'arrow',
|
||||
color: 'primary',
|
||||
anchor: { lon: 0, lat: 0.02 },
|
||||
to: { lon: 0.01, lat: 0.02 },
|
||||
},
|
||||
{
|
||||
id: 'pin',
|
||||
type: 'pin',
|
||||
color: 'primary',
|
||||
anchor: { lon: 0.03, lat: 0 },
|
||||
},
|
||||
{
|
||||
id: 'label',
|
||||
type: 'label',
|
||||
color: 'amber',
|
||||
anchor: { lon: 0.04, lat: 0 },
|
||||
},
|
||||
];
|
||||
for (const mark of marks) {
|
||||
mark.alpha = 0.25;
|
||||
renderer.add(
|
||||
mark,
|
||||
mark.footprintKind === 'building' ? { sampledGroundHeight: 150 } : {},
|
||||
);
|
||||
}
|
||||
assert.ok(scene.added.length >= marks.length, 'every kind drew');
|
||||
for (const alpha of [0.5, 1]) {
|
||||
for (const mark of marks) mark.alpha = alpha;
|
||||
assert.doesNotThrow(() => renderer.sync());
|
||||
scene.preRender.raiseEvent();
|
||||
scene.postRender.raiseEvent();
|
||||
}
|
||||
for (const mark of marks) mark.hidden = true;
|
||||
renderer.sync();
|
||||
for (const mark of marks) mark.hidden = false;
|
||||
renderer.sync();
|
||||
renderer.destroy();
|
||||
assert.equal(scene.added.length, 0, 'nothing left drawn');
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
test('the air quality layer draws its stations as ground fills on a viewer of its own', async () => {
|
||||
const { createAirQualityLayer } =
|
||||
await import('../../layers/airQuality/index.js');
|
||||
@@ -3543,6 +4155,66 @@ test('an aircraft layer draws its contacts as one GPU icon set moving on the del
|
||||
unbind();
|
||||
}
|
||||
});
|
||||
test('an extruded shape that classifies its volume tints what it covers; its outline is the volume edges', () => {
|
||||
const scene = fakeScene();
|
||||
const { draw } = drawOver(scene);
|
||||
const set = draw.shapes({ id: 'volumes' });
|
||||
const ring = [
|
||||
{ lon: 0, lat: 0, height: 100 },
|
||||
{ lon: 0.001, lat: 0, height: 100 },
|
||||
{ lon: 0.001, lat: 0.001, height: 100 },
|
||||
];
|
||||
const volume = {
|
||||
id: 'volume',
|
||||
geometry: { kind: 'polygon', rings: [ring] },
|
||||
fill: [1, 0.7, 0.3, 0.45],
|
||||
extrudeHeight: 220,
|
||||
height: 'absolute',
|
||||
classifyVolume: true,
|
||||
};
|
||||
const cage = {
|
||||
id: 'cage',
|
||||
geometry: { kind: 'polygon', rings: [ring] },
|
||||
outline: [1, 0.7, 0.3, 0.32],
|
||||
outlineOnly: true,
|
||||
extrudeHeight: 220,
|
||||
height: 'absolute',
|
||||
};
|
||||
// A scene that cannot classify draws the volume itself.
|
||||
set.set([volume]);
|
||||
assert.ok(scene.added[0].get(0) instanceof Cesium.Primitive);
|
||||
assert.ok(!(scene.added[0].get(0) instanceof Cesium.ClassificationPrimitive));
|
||||
scene.context = { stencilBuffer: true };
|
||||
set.set([volume, cage]);
|
||||
const host = scene.added[0];
|
||||
const classified = host.get(0);
|
||||
assert.ok(classified instanceof Cesium.ClassificationPrimitive);
|
||||
assert.equal(classified.classificationType, Cesium.ClassificationType.BOTH);
|
||||
const [instance] = [classified.geometryInstances].flat();
|
||||
assert.ok(instance.geometry instanceof Cesium.PolygonGeometry);
|
||||
assert.deepEqual(
|
||||
[instance.geometry._extrudedHeight, instance.geometry._height].sort(
|
||||
(a, b) => a - b,
|
||||
),
|
||||
[100, 220],
|
||||
'from its base to its extruded face',
|
||||
);
|
||||
assert.equal(instance.id.id, 'volume');
|
||||
// The cage: the extruded outline, not a ring at its base.
|
||||
const edges = host.get(1);
|
||||
assert.ok(edges instanceof Cesium.Primitive);
|
||||
assert.ok(
|
||||
[edges.geometryInstances].flat()[0].geometry instanceof
|
||||
Cesium.PolygonOutlineGeometry,
|
||||
);
|
||||
// An image cannot classify.
|
||||
assert.throws(
|
||||
() => set.set([{ ...volume, fill: { image: 'a.png' } }]),
|
||||
/classifies volumes with colours only/,
|
||||
);
|
||||
set.dispose();
|
||||
});
|
||||
|
||||
test('an unclamped shape filled with an image draws a textured primitive and frees its material with its host', () =>
|
||||
withImageClasses(() => {
|
||||
const scene = fakeScene();
|
||||
@@ -3578,6 +4250,8 @@ test('an unclamped shape filled with an image draws a textured primitive and fre
|
||||
[true, true, true],
|
||||
);
|
||||
assert.ok(geometryInstances.geometry instanceof Cesium.RectangleGeometry);
|
||||
// Texture coordinates for the image, as its appearance reads them.
|
||||
assert.equal(geometryInstances.geometry._vertexFormat.st, true);
|
||||
assert.equal(geometryInstances.id.id, 'image');
|
||||
assert.equal(
|
||||
drawHitOf({ primitive, id: geometryInstances.id }).set,
|
||||
@@ -3927,19 +4601,19 @@ test('flow lines run their dashes toward the end, draped or not, on the frame cl
|
||||
asked = 0;
|
||||
frame();
|
||||
assert.equal(asked, 0, 'an emptied set asks for none');
|
||||
// Arrows and patterns are not draped.
|
||||
// Patterns are not draped.
|
||||
assert.throws(
|
||||
() =>
|
||||
set.set([
|
||||
{
|
||||
id: 'arrow',
|
||||
id: 'pattern',
|
||||
positions,
|
||||
widthPx: 1,
|
||||
mode: 'draped',
|
||||
style: { kind: 'arrow', color: '#fff' },
|
||||
style: { kind: 'pattern', image: 'a.png', repeat: 2 },
|
||||
},
|
||||
]),
|
||||
/drapes solid, glow, dash and flow lines only/,
|
||||
/drapes solid, glow, dash, flow and arrow lines only/,
|
||||
);
|
||||
set.dispose();
|
||||
}));
|
||||
@@ -4472,6 +5146,42 @@ test('draped glow and dash lines drape with their materials, each on a ground pr
|
||||
set.dispose();
|
||||
}));
|
||||
|
||||
test('a draped arrow drapes its arrow material on a ground polyline of its own, recoloured in place', () =>
|
||||
withImageClasses(() => {
|
||||
const { scene, draw } = drawOver();
|
||||
const set = draw.lines({ id: 'arrows' });
|
||||
const arrow = (color) => ({
|
||||
id: 'to',
|
||||
positions: [
|
||||
{ lon: 0, lat: 0 },
|
||||
{ lon: 0.01, lat: 0.01 },
|
||||
],
|
||||
widthPx: 16,
|
||||
mode: 'draped',
|
||||
style: { kind: 'arrow', color },
|
||||
});
|
||||
set.set([arrow([0.5, 0.9, 1, 0.95])]);
|
||||
const host = scene.added[0];
|
||||
const ground = host.get(1);
|
||||
assert.ok(ground instanceof Cesium.GroundPolylinePrimitive);
|
||||
assert.equal(ground.classificationType, Cesium.ClassificationType.BOTH);
|
||||
assert.equal(ground.geometryInstances[0].geometry.width, 16);
|
||||
const material = ground.appearance.material;
|
||||
assert.equal(material.type, 'PolylineArrow');
|
||||
assert.ok(
|
||||
Cesium.Color.equals(
|
||||
material.uniforms.color,
|
||||
new Cesium.Color(0.5, 0.9, 1, 0.95),
|
||||
),
|
||||
);
|
||||
// Faded, it takes its new colour on the material it has.
|
||||
set.upsert([arrow([0.5, 0.9, 1, 0.4])]);
|
||||
assert.equal(scene.added[0], host, 'not rebuilt');
|
||||
assert.equal(ground.appearance.material, material);
|
||||
assert.equal(material.uniforms.color.alpha, 0.4);
|
||||
set.dispose();
|
||||
}));
|
||||
|
||||
test('a draped line drawn by its own material changes colour in place; a batched one rebuilds', () =>
|
||||
withImageClasses(() => {
|
||||
const { scene, draw } = drawOver();
|
||||
@@ -6384,8 +7094,8 @@ test('a clamped fill that moves or resizes is drawn where it now is in the same
|
||||
set.upsert([box('a', 2, { show: false })]);
|
||||
assert.equal(host.get(host.length - 1), again);
|
||||
assert.deepEqual([...shows.get('1:a').show], [0]);
|
||||
// A new colour is a new look: the set rebuilds.
|
||||
set.upsert([box('a', 2, { fill: [1, 0, 0, 0.5] })]);
|
||||
// A new item: the set rebuilds.
|
||||
set.upsert([box('a', 2), box('z', 3)]);
|
||||
assert.notEqual(scene.added.at(-1), host, 'rebuilt');
|
||||
set.dispose();
|
||||
assert.equal(scene.added.length, 0);
|
||||
@@ -6416,8 +7126,8 @@ test('a moved clamped fill is hidden where a rebuild in flight still draws it',
|
||||
frame();
|
||||
const shown = { show: new Uint8Array([1]) };
|
||||
old.getGeometryInstanceAttributes = () => shown;
|
||||
// A new colour rebuilds; the old host draws on until the new is ready.
|
||||
set.upsert([box('a', 1, [1, 0, 0, 0.5])]);
|
||||
// A new item rebuilds; the old host draws on until the new is ready.
|
||||
set.upsert([box('a', 1, [1, 0, 0, 0.5]), box('z', 0.5)]);
|
||||
const second = scene.added.find((p) => p !== first);
|
||||
assert.ok(
|
||||
second && scene.added.includes(first),
|
||||
@@ -6557,11 +7267,11 @@ test('a moved clamped fill shows again where the retiring host draws it when the
|
||||
frame();
|
||||
const shown = { show: new Uint8Array([1]) };
|
||||
old.getGeometryInstanceAttributes = () => shown;
|
||||
set.upsert([groundBox('a', 1, { fill: [1, 0, 0, 0.5] })]);
|
||||
set.upsert([groundBox('a', 2, { fill: [1, 0, 0, 0.5] })]);
|
||||
set.upsert([groundBox('a', 1), groundBox('z', 0.5)]);
|
||||
set.upsert([groundBox('a', 2)]);
|
||||
assert.deepEqual([...shown.show], [0], 'hidden while drawn alone');
|
||||
// A second rebuild drops the host drawing it alone, still building.
|
||||
set.upsert([groundBox('a', 2, { fill: [0, 1, 0, 0.5] })]);
|
||||
set.upsert([groundBox('a', 2), groundBox('y', 0.5)]);
|
||||
assert.ok(scene.added.includes(first), 'the retiring host draws on');
|
||||
assert.equal(scene.added.length, 2);
|
||||
assert.deepEqual([...shown.show], [1], 'drawn where it was, not gone');
|
||||
@@ -6632,7 +7342,7 @@ test('clamped fills drawn alone by a host still retiring count toward MOVING_SHA
|
||||
frame();
|
||||
// Half the cap moved, then a rebuild while they are drawn alone.
|
||||
set.upsert(ids.slice(0, MOVING_SHAPES).map((id) => groundBox(id, 1.5)));
|
||||
set.upsert([groundBox(ids.at(-1), 1, { fill: [1, 0, 0, 0.5] })]);
|
||||
set.upsert([groundBox('new', 1)]);
|
||||
assert.equal(scene.added.length, 2, 'rebuilding, the old host retiring');
|
||||
const second = scene.added.find((p) => p !== first);
|
||||
const before = second.length;
|
||||
|
||||
@@ -64,7 +64,9 @@ export const CESIUM_CHECKS = new Set([
|
||||
'draw.points.population',
|
||||
'draw.lodGroup',
|
||||
'draw.sets',
|
||||
'draw.shapes.classifyVolume',
|
||||
'draw.shapes.drapedRotation',
|
||||
'draw.shapes.radiusByCameraHeight',
|
||||
'effects.crossfade',
|
||||
'effects.defineStyle',
|
||||
'effects.screenModulation',
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
[
|
||||
"dev/conformance/main.js",
|
||||
"src/annotations/worldAnnotationRenderer.js",
|
||||
"src/app/embed.js",
|
||||
"src/app/scene.js",
|
||||
"src/app/viewer.js",
|
||||
|
||||
@@ -1998,6 +1998,126 @@ export function portChecks() {
|
||||
ok(!mine(at(0)), 'gone with its set');
|
||||
},
|
||||
),
|
||||
check(
|
||||
'draw.shapes.radiusByCameraHeight',
|
||||
'a clamped ellipse with radii by camera height draws at the radius the camera height gives, held within its bounds, and follows the camera',
|
||||
async (h, r) => {
|
||||
const shapes = r.draw.shapes({ id: 'conf:rings' });
|
||||
const radius = { byCameraHeight: { factor: 0.1, min: 50, max: 600 } };
|
||||
shapes.set([
|
||||
{
|
||||
id: 'ring',
|
||||
geometry: {
|
||||
kind: 'ellipse',
|
||||
center: { lon: AUSTIN.lon, lat: AUSTIN.lat },
|
||||
semiMajor: radius,
|
||||
semiMinor: radius,
|
||||
},
|
||||
fill: [0.5, 0.9, 1, 0.6],
|
||||
height: 'clampGround',
|
||||
},
|
||||
]);
|
||||
// Metres east of the centre, at its latitude.
|
||||
const east = (m) =>
|
||||
h.toEcef({
|
||||
lon:
|
||||
AUSTIN.lon +
|
||||
m / (111320 * Math.cos((AUSTIN.lat * Math.PI) / 180)),
|
||||
lat: AUSTIN.lat,
|
||||
});
|
||||
const covers = (m) => {
|
||||
const s = r.query.project(east(m));
|
||||
return Boolean(
|
||||
s &&
|
||||
r.query
|
||||
.drillPick(s.x, s.y)
|
||||
.some((hit) => hit.set === 'conf:rings' && hit.id === 'ring'),
|
||||
);
|
||||
};
|
||||
const until = async (test) => {
|
||||
for (let i = 0; i < 300; i++) {
|
||||
await drawn(h, r, 1);
|
||||
if (test()) return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
// 2000 m up: 200 m across its centre.
|
||||
await overAustin(h, r, 2000);
|
||||
ok(await until(() => covers(150)), 'at 2000 m it reaches 150 m');
|
||||
ok(!covers(260), 'and not 260 m');
|
||||
// 4000 m up: 400 m.
|
||||
await overAustin(h, r, 4000);
|
||||
ok(
|
||||
await until(() => covers(300) && !covers(460)),
|
||||
'at 4000 m it reaches 300 m, not 460 m',
|
||||
);
|
||||
// 20 km up: held at 600 m.
|
||||
await overAustin(h, r, 20000);
|
||||
ok(
|
||||
await until(() => covers(550) && !covers(700)),
|
||||
'held at its largest',
|
||||
);
|
||||
shapes.dispose();
|
||||
await drawn(h, r);
|
||||
ok(!covers(0), 'gone with its set');
|
||||
},
|
||||
),
|
||||
check(
|
||||
'draw.shapes.classifyVolume',
|
||||
'an extruded shape that classifies its volume picks where it tints what it covers, and hides',
|
||||
async (h, r) => {
|
||||
await overAustin(h, r);
|
||||
const shapes = r.draw.shapes({ id: 'conf:volume' });
|
||||
const d = 0.002;
|
||||
const ring = [
|
||||
[-d, -d],
|
||||
[d, -d],
|
||||
[d, d],
|
||||
[-d, d],
|
||||
].map(([dLon, dLat]) => ({
|
||||
lon: AUSTIN.lon + dLon,
|
||||
lat: AUSTIN.lat + dLat,
|
||||
height: -200,
|
||||
}));
|
||||
const volume = (extra = {}) => ({
|
||||
id: 'volume',
|
||||
geometry: { kind: 'polygon', rings: [ring] },
|
||||
fill: [1, 0.7, 0.3, 0.45],
|
||||
extrudeHeight: 300,
|
||||
height: 'absolute',
|
||||
classifyVolume: true,
|
||||
...extra,
|
||||
});
|
||||
const mine = (dLon) => {
|
||||
const s = r.query.project(
|
||||
h.toEcef({ lon: AUSTIN.lon + dLon, lat: AUSTIN.lat }),
|
||||
);
|
||||
return Boolean(
|
||||
s &&
|
||||
r.query
|
||||
.drillPick(s.x, s.y)
|
||||
.some((hit) => hit.set === 'conf:volume' && hit.id === 'volume'),
|
||||
);
|
||||
};
|
||||
const until = async (test) => {
|
||||
for (let i = 0; i < 300; i++) {
|
||||
await drawn(h, r, 1);
|
||||
if (test()) return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
shapes.set([volume()]);
|
||||
ok(await until(() => mine(0)), 'it picks inside');
|
||||
ok(!mine(0.008), 'and not outside');
|
||||
shapes.upsert([volume({ show: false })]);
|
||||
ok(await until(() => !mine(0)), 'hidden');
|
||||
shapes.upsert([volume()]);
|
||||
ok(await until(() => mine(0)), 'shown again');
|
||||
shapes.dispose();
|
||||
await drawn(h, r);
|
||||
ok(!mine(0), 'gone with its set');
|
||||
},
|
||||
),
|
||||
check(
|
||||
'draw.lines.styles',
|
||||
'flow lines draw draped and in space, and a line with a depth-fail colour moves every frame, without stopping the frame or picks',
|
||||
@@ -2069,7 +2189,7 @@ export function portChecks() {
|
||||
),
|
||||
check(
|
||||
'draw.lines.drape',
|
||||
'draped glow and dash lines draw and pick; a revealed line draws only its first part; a draped line and a clamped point that move every frame are drawn where they now are in the next frame',
|
||||
'draped glow, dash and arrow lines draw and pick; a revealed line draws only its first part; a draped line and a clamped point that move every frame are drawn where they now are in the next frame',
|
||||
async (h, r) => {
|
||||
await overAustin(h, r);
|
||||
const at = (dLon, dLat = 0) =>
|
||||
@@ -2112,12 +2232,20 @@ export function portChecks() {
|
||||
style: { kind: 'solid', color: '#20e7f2' },
|
||||
revealFraction: 0.5,
|
||||
},
|
||||
{
|
||||
id: 'arrow',
|
||||
positions: [at(-0.003, -0.004), at(0.003, -0.004)],
|
||||
widthPx: 16,
|
||||
mode: 'draped',
|
||||
style: { kind: 'arrow', color: [0.5, 0.9, 1, 0.95] },
|
||||
},
|
||||
]);
|
||||
await drawn(h, r);
|
||||
await settled(r);
|
||||
await drawn(h, r);
|
||||
ok(mine(at(0, 0.002), 'glow'), 'a draped glow line picks');
|
||||
ok(mine(at(0, -0.002), 'dash'), 'a draped dash line picks');
|
||||
ok(mine(at(-0.002, -0.004), 'arrow'), 'a draped arrow picks');
|
||||
ok(mine(at(-0.0015), 'reveal'), 'revealed: its first half draws');
|
||||
ok(!mine(at(0.0015), 'reveal'), 'revealed: its second half does not');
|
||||
lines.upsert([
|
||||
@@ -2191,7 +2319,7 @@ export function portChecks() {
|
||||
`moved, frame ${i}: not where it was`,
|
||||
);
|
||||
}
|
||||
equal(lines.size(), 4, 'every line held');
|
||||
equal(lines.size(), 5, 'every line held');
|
||||
points.dispose();
|
||||
lines.dispose();
|
||||
},
|
||||
|
||||
@@ -710,6 +710,60 @@ export function createStubRenderer(options = {}) {
|
||||
);
|
||||
}
|
||||
|
||||
/** The eye's height above the ellipsoid, for radii by camera height. */
|
||||
const eyeHeight = () => toCartographicRadians(eye().position)?.height ?? 0;
|
||||
|
||||
/**
|
||||
* Whether `lon`, `lat` (degrees) is in an ellipse geometry, its radii by
|
||||
* camera height at the eye's height, measured in the plane tangent at its
|
||||
* centre.
|
||||
*/
|
||||
function inEllipse(g, lon, lat) {
|
||||
const height = eyeHeight();
|
||||
const radius = (r) =>
|
||||
typeof r === 'number'
|
||||
? r
|
||||
: Math.max(
|
||||
r.byCameraHeight.min,
|
||||
Math.min(r.byCameraHeight.max, height * r.byCameraHeight.factor),
|
||||
);
|
||||
const centre = toCartographicRadians(toEcef(g.center));
|
||||
const metresPerRadian = 6378137;
|
||||
const east =
|
||||
((lon * Math.PI) / 180 - centre.lon) *
|
||||
metresPerRadian *
|
||||
Math.cos(centre.lat);
|
||||
const north = ((lat * Math.PI) / 180 - centre.lat) * metresPerRadian;
|
||||
// The semi-major axis is turned counter-clockwise from north.
|
||||
const turn = g.rotation ?? 0;
|
||||
const along = north * Math.cos(turn) - east * Math.sin(turn);
|
||||
const across = north * Math.sin(turn) + east * Math.cos(turn);
|
||||
return (
|
||||
(along / radius(g.semiMajor)) ** 2 +
|
||||
(across / radius(g.semiMinor)) ** 2 <=
|
||||
1
|
||||
);
|
||||
}
|
||||
|
||||
/** Whether `lon`, `lat` (degrees) is inside a ring of positions. */
|
||||
function inRing(ring, lon, lat) {
|
||||
const points = ring.map((p) => {
|
||||
const c = toCartographicRadians(toEcef(p));
|
||||
return [toDegrees(c.lon), toDegrees(c.lat)];
|
||||
});
|
||||
let inside = false;
|
||||
for (let i = 0, j = points.length - 1; i < points.length; j = i++) {
|
||||
const [xi, yi] = points[i];
|
||||
const [xj, yj] = points[j];
|
||||
if (
|
||||
yi > lat !== yj > lat &&
|
||||
lon < ((xj - xi) * (lat - yi)) / (yj - yi) + xi
|
||||
)
|
||||
inside = !inside;
|
||||
}
|
||||
return inside;
|
||||
}
|
||||
|
||||
/**
|
||||
* The pickable items under a pixel, nearest first. With a `depth`, only
|
||||
* the nearest `depth` things drawn there are looked through, unpickable
|
||||
@@ -745,18 +799,29 @@ export function createStubRenderer(options = {}) {
|
||||
continue;
|
||||
}
|
||||
if (set.kind === 'shapes') {
|
||||
// Draped rectangles are picked where they cover the ellipsoid, turned
|
||||
// as the default renderer turns them; other shapes are not picked.
|
||||
// Draped rectangles and ellipses, and volumes that classify what
|
||||
// they cover, are picked where they cover the ellipsoid (rectangles
|
||||
// turned as the default renderer turns them); other shapes are not
|
||||
// picked.
|
||||
const ground = rayEllipsoid(pickRay(x, y));
|
||||
const at = ground && toCartographicRadians(ground);
|
||||
if (!at) continue;
|
||||
const lon = toDegrees(at.lon);
|
||||
const lat = toDegrees(at.lat);
|
||||
for (const item of set.items.values()) {
|
||||
if (item.show === false) continue;
|
||||
if (!item.fill || item.outlineOnly) continue;
|
||||
if (item.height !== 'clampGround') continue;
|
||||
if (item.geometry?.kind !== 'rectangle') continue;
|
||||
if (!inRectangle(item.geometry, toDegrees(at.lon), toDegrees(at.lat)))
|
||||
continue;
|
||||
const g = item.geometry;
|
||||
const covers =
|
||||
item.height === 'clampGround'
|
||||
? (g?.kind === 'rectangle' && inRectangle(g, lon, lat)) ||
|
||||
(g?.kind === 'ellipse' && inEllipse(g, lon, lat))
|
||||
: item.classifyVolume === true &&
|
||||
item.extrudeHeight !== undefined &&
|
||||
g?.kind === 'polygon' &&
|
||||
inRing(g.rings[0], lon, lat) &&
|
||||
!g.rings.slice(1).some((hole) => inRing(hole, lon, lat));
|
||||
if (!covers) continue;
|
||||
hits.push({
|
||||
set: setId,
|
||||
id: item.id,
|
||||
|
||||
@@ -141,6 +141,40 @@ test('a draped rectangle across the antimeridian picks on both sides of it, turn
|
||||
r.destroy();
|
||||
});
|
||||
|
||||
test('an extruded volume that classifies picks inside its outer ring and not in its holes', () => {
|
||||
const r = createStubRenderer();
|
||||
r.camera.setView({
|
||||
destination: { lon: 0, lat: 0, height: 20_000 },
|
||||
orientation: { heading: 0, pitch: -Math.PI / 2, roll: 0 },
|
||||
});
|
||||
const shapes = r.draw.shapes({ id: 'volume' });
|
||||
const square = (d) =>
|
||||
[
|
||||
[-d, -d],
|
||||
[d, -d],
|
||||
[d, d],
|
||||
[-d, d],
|
||||
].map(([lon, lat]) => ({ lon, lat, height: -50 }));
|
||||
shapes.set([
|
||||
{
|
||||
id: 'v',
|
||||
geometry: { kind: 'polygon', rings: [square(0.02), square(0.005)] },
|
||||
fill: '#f80',
|
||||
extrudeHeight: 100,
|
||||
height: 'absolute',
|
||||
classifyVolume: true,
|
||||
},
|
||||
]);
|
||||
const picked = (lon, lat) => {
|
||||
const s = r.query.project(toEcef({ lon, lat, height: 0 }));
|
||||
return r.query.drillPick(s.x, s.y).some((hit) => hit.set === 'volume');
|
||||
};
|
||||
assert.ok(picked(0.012, 0), 'between its rings');
|
||||
assert.ok(!picked(0, 0), 'not in its hole');
|
||||
assert.ok(!picked(0.03, 0), 'not outside');
|
||||
r.destroy();
|
||||
});
|
||||
|
||||
test('the stub says where it draws an item, for a camera that is not its own', () => {
|
||||
const r = createStubRenderer();
|
||||
const t0 = r.frame.time();
|
||||
|
||||
@@ -117,6 +117,9 @@ const DRAW_FROM_CALLER = new Set([
|
||||
'./infrastructure/geojson',
|
||||
// Its missions draw, pick and fly through the ports alone.
|
||||
'./layers/launches',
|
||||
// Its world marks draw through `drawOf` and probe heights through
|
||||
// `heightsOf`; its screen marks project through the camera and query ports.
|
||||
'./annotations',
|
||||
// Its pins, footprints and outlines draw through `drawOf`; its horizon
|
||||
// cull reads the camera and frame ports.
|
||||
'./layers/ghostmaps',
|
||||
|
||||
Reference in new issue
Block a user