GPU icons draw offsets, anchors, rotations, fades, display ranges and clamped heights

Each row reads its extras from a float texture by instance (texelFetch on
gl_InstanceID), so the 16 vertex attributes hold: pixel offset, vertical
anchor, screen rotation and aligned axis (turned as billboards turn),
fadeByDistance, displayRange, and a ground mode with the heights under its
segment's two ends. Clamped rows are sampled after a frame draws, budgeted
across the port's sets (terrain first, then the drawn surface), and again
when the globe's tiles finish loading. WebGL 1 still refuses these; GPU sets
still refuse clustering. The stub picks icons offset, anchored, turned and
clamped to its ellipsoid, and draw.motion.look checks each on a moving icon.
This commit is contained in:
Sameh Khamis committed 2026-11-11 17:58:40 -08:00
1 parent 759308404e
commit 933969b7da
9 files changed
+892 -29

No files matched your search

+6
View File
@@ -5159,6 +5159,12 @@ export function createCesiumDraw({
afterRender() {
watchClamp();
swapReady();
// Clamped GPU icons take the ground under their segments.
const budget = gpu.groundBudget();
let grounded = false;
for (const record of sets.values())
if (record.gpu?.ground(budget)) grounded = true;
if (grounded) requestRender();
for (const record of sets.values())
if (record.kind === 'lines')
try {
+230 -15
View File
@@ -11,9 +11,19 @@
*
* What these sets draw as billboards do not: an icon set becomes one when
* an item first needs it (or it first takes columns), and stays one. They
* refuse loudly what billboards draw and they do not: clamped heights,
* `fadeByDistance`, `displayRange`, pixel offsets, anchors other than the
* centre, axis and screen rotations, and clustering. Their
* draw what billboards draw but clustering, which they refuse loudly; on
* WebGL 1 they also refuse what each row reads from its extras texture
* there is none of: clamped heights, `fadeByDistance`, `displayRange`, pixel
* offsets, anchors other than the centre, and axis and screen rotations.
*
* A clamped row (`clampGround`, `relativeGround`) stands on the ground
* taken between the heights under its segment's two ends, sampled after a
* frame draws, `GROUND_SAMPLES_PER_FRAME` ends a frame across the port's
* sets: the loaded terrain first, then, but for `clampTo: 'terrain'`, the
* drawn surface (`SURFACE_SAMPLES_PER_FRAME`, where the scene samples it).
* An end with no height yet stands on the ellipsoid. Every end is sampled
* again once the globe's tiles finish loading, and, clamped to more than
* terrain, every `GROUND_REFRESH_MS`. Their
* images (URLs, SVG, blobs, keyed canvases and atlas slots of those)
* are rasterised square into the icon atlas every GPU icon set shares, and
* refused when it has no room left for them: an SVG at its own size (its
@@ -37,6 +47,9 @@ import * as Cesium from 'cesium';
import {
createIconSet,
ICON_CULL_LIFT,
ICON_GROUND_CLAMP,
ICON_GROUND_NONE,
ICON_GROUND_RELATIVE,
ICON_RECEDES,
ICON_ROTATES,
} from '../../data/iconSet.js';
@@ -54,6 +67,7 @@ import {
motionEndsAt,
} from '../motion/iconMotion.js';
import { blobUrl, toCartesian } from './common.js';
import { heightsFor } from './heights.js';
/** The capabilities GPU icon sets bring. */
export const GPU_ICON_CAPABILITIES = Object.freeze([
@@ -68,6 +82,24 @@ const nonZero = (v) =>
/** Whether a curve is one (as the billboards read it). */
const isCurve = (curve) => Boolean(curve) && curve.far > curve.near;
/** Whether a `displayRange` shows anything: `[near, far)` is not empty. */
const displayable = (range) => !range || range[1] > range[0];
/** An anchor as the row's vertical origin: the edge on the position. */
const ANCHORS = { center: 0, bottom: 1, top: -1 };
const GROUND_MODES = {
clampGround: ICON_GROUND_CLAMP,
relativeGround: ICON_GROUND_RELATIVE,
};
/** Segment ends sampled for their ground after a frame, across the sets. */
export const GROUND_SAMPLES_PER_FRAME = 256;
/** Of those, how many also sample the drawn surface (each draws a view). */
export const SURFACE_SAMPLES_PER_FRAME = 8;
/** How often rows clamped to more than terrain are sampled again, ms. */
export const GROUND_REFRESH_MS = 5000;
/**
* An image's atlas key: a URL itself, SVG as a data URL, a blob as its
* object URL, a generated canvas by its key; else null.
@@ -209,16 +241,19 @@ export function createGpuIcons({
const refuse = (what) => {
throw new Error(`the Cesium renderer's GPU icons have no ${what}`);
};
if (item.height !== undefined && item.height !== 'absolute')
refuse('clamped heights');
if (item.fadeByDistance) refuse('fadeByDistance');
if (item.displayRange) refuse('displayRange');
if (nonZero(item.pixelOffset)) refuse('pixelOffset');
if (item.anchor !== undefined && item.anchor !== 'center')
refuse('anchors but the centre');
if (item.rotationMode === 'axis') refuse('axis rotation');
if (item.rotationMode === 'screen' && (item.rotation ?? 0) !== 0)
refuse('screen rotation');
// What a row reads from its extras texture, which WebGL 1 has not.
if (scene.context?.webgl2 === false) {
if (item.height !== undefined && item.height !== 'absolute')
refuse('clamped heights on WebGL 1');
if (item.fadeByDistance) refuse('fadeByDistance on WebGL 1');
if (item.displayRange) refuse('displayRange on WebGL 1');
if (nonZero(item.pixelOffset)) refuse('pixelOffset on WebGL 1');
if (item.anchor !== undefined && item.anchor !== 'center')
refuse('anchors but the centre on WebGL 1');
if (item.rotationMode === 'axis') refuse('axis rotation on WebGL 1');
if (item.rotationMode === 'screen' && (item.rotation ?? 0) !== 0)
refuse('screen rotation on WebGL 1');
}
for (const given of [item.image, item.imageLod?.near]) {
// An atlas slot as what it holds now (none yet draws once it does).
const image =
@@ -392,7 +427,47 @@ export function createGpuIcons({
item.pickProxy?.size?.[1] ?? 0,
);
set.setDepthTestDistance(row, item.disableDepthTest ?? 0);
set.setShown(row, item.show !== false);
// An empty range shows it nowhere.
set.setShown(row, item.show !== false && displayable(item.displayRange));
writeExtras(row, item);
writeSegment(row, item, added, motion);
writeGround(row, item);
}
/** A row's offset, anchor, rotation, fade and display range. */
function writeExtras(row, item) {
set.setOffset(row, item.pixelOffset?.x ?? 0, item.pixelOffset?.y ?? 0);
set.setAnchor(row, ANCHORS[item.anchor] ?? 0);
set.setRotation(
row,
item.rotationMode === 'screen' ? (item.rotation ?? 0) : 0,
);
// Its up along the axis, as a billboard's; one of zero length
// stands upright.
const {
x = 0,
y = 0,
z = 0,
} = (item.rotationMode === 'axis' && item.alignedAxis) || {};
const length = Math.hypot(x, y, z);
if (length > 0) set.setAxis(row, x / length, y / length, z / length);
else set.setAxis(row, 0, 0, 0);
set.setFade(
row,
isCurve(item.fadeByDistance) ? item.fadeByDistance : undefined,
);
const range =
item.displayRange && displayable(item.displayRange)
? item.displayRange
: [0, 0];
set.setDisplayRange(row, range[0], range[1]);
}
/**
* Write a row's segment and course easing, unless `motion` is false
* (and the row not new) or its motion is as written.
*/
function writeSegment(row, item, added, motion) {
if (!motion && !added) return;
// A write moving it as it already moves (a restyle, a focus change)
// keeps its segment and course easing as they are.
@@ -428,6 +503,83 @@ export function createGpuIcons({
if (end > movingUntil) movingUntil = end;
}
// Clamped rows by id, those whose ground is still to sample, those never
// sampled yet, and what each row's ground was last written from.
const grounded = new Set();
const groundQueue = new Set();
const unsampled = new Set();
const groundKeys = new Map();
let groundRefreshedAt = -Infinity;
let tilesWereLoaded = false;
/** The two ends of a row's segment, ECEF. */
function spanOf(row, item) {
const motion = can('motionSegments') ? item.motion : undefined;
let t0 = 0;
let t1 = 0;
if (motion?.kind === 'lerp') {
t0 = tauOf(motion.t0);
t1 = Math.max(tauOf(motion.t1), t0 + LERP_STEP_MS / 1000);
} else if (motion?.kind === 'arc') {
const t = tauOf(motion.t0);
t0 = t + motion.minDtS;
t1 = t + motion.maxDtS;
}
return [
set.position(row, t0, { x: 0, y: 0, z: 0 }),
set.position(row, t1, { x: 0, y: 0, z: 0 }),
];
}
/** A row's ground mode and segment ends, queued to sample when new. */
function writeGround(row, item) {
const mode = GROUND_MODES[item.height] ?? ICON_GROUND_NONE;
if (mode === ICON_GROUND_NONE) {
if (grounded.delete(item.id)) {
set.setGround(row, ICON_GROUND_NONE);
set.setGroundHeights(row, 0, 0);
groundQueue.delete(item.id);
unsampled.delete(item.id);
groundKeys.delete(item.id);
}
return;
}
const [from, to] = spanOf(row, item);
const key = `${mode}|${from.x}|${from.y}|${from.z}|${to.x}|${to.y}|${to.z}`;
if (!grounded.has(item.id)) {
grounded.add(item.id);
unsampled.add(item.id);
}
if (groundKeys.get(item.id) === key) return;
groundKeys.set(item.id, key);
set.setGround(row, mode, from, to);
groundQueue.add(item.id);
}
const groundAt = new Cesium.Cartesian3();
const groundCarto = new Cesium.Cartographic();
/**
* The ground's height under `p`: the drawn surface when `surface`
* (else, or where it has none, the loaded terrain, but for `clampTo:
* 'tiles'`); null where neither answers.
*/
function groundHeight(p, surface) {
Cesium.Cartesian3.fromElements(p.x, p.y, p.z, groundAt);
const carto = Cesium.Cartographic.fromCartesian(
groundAt,
undefined,
groundCarto,
);
if (!carto) return null;
const heights = heightsFor(scene);
if (surface) {
const h = heights.now(carto);
if (h !== null) return h;
}
if (spec.clampTo === 'tiles') return null;
return heights.now(carto, { against: 'terrain' });
}
/** Bring the rows in line with every item. */
function sync() {
fresh = false;
@@ -535,6 +687,63 @@ export function createGpuIcons({
if (fresh) return sync();
for (const item of drawing) writeRow(item);
},
/**
* After a frame: sample the ground under clamped rows' segment ends,
* from `budget` (`{ends, surface}`, spent). Whether a row's ground
* changed.
*/
ground(budget) {
if (grounded.size === 0) return false;
const loaded = scene.globe?.tilesLoaded === true;
const now = Date.now();
const clampTo = spec.clampTo ?? 'both';
if (
(loaded && !tilesWereLoaded) ||
(clampTo !== 'terrain' &&
now - groundRefreshedAt >= GROUND_REFRESH_MS)
) {
for (const id of grounded) groundQueue.add(id);
groundRefreshedAt = now;
}
tilesWereLoaded = loaded;
const surface =
clampTo !== 'terrain' && scene.sampleHeightSupported === true;
let changed = false;
const later = [];
for (const id of groundQueue) {
if (budget.ends < 1) break;
const row = set.row(id);
if (row < 0 || set.groundMode(row) === ICON_GROUND_NONE) {
groundQueue.delete(id);
grounded.delete(id);
unsampled.delete(id);
groundKeys.delete(id);
continue;
}
const [from, to] = set.groundSpan(row);
const same = from.x === to.x && from.y === to.y && from.z === to.z;
const ends = same ? 1 : 2;
if (budget.ends < ends) continue;
const drawn = surface && budget.surface >= ends;
// Out of surface samples: a row never sampled takes the terrain
// for now and waits; the rest wait as they stand.
if (surface && !drawn && !unsampled.has(id)) continue;
groundQueue.delete(id);
if (surface && !drawn) later.push(id);
budget.ends -= ends;
if (drawn) budget.surface -= ends;
const h0 = groundHeight(from, drawn) ?? 0;
const h1 = same ? h0 : (groundHeight(to, drawn) ?? 0);
unsampled.delete(id);
const [g0, g1] = set.groundHeights(row);
if (g0 !== Math.fround(h0) || g1 !== Math.fround(h1)) {
set.setGroundHeights(row, h0, h1);
changed = true;
}
}
for (const id of later) groundQueue.add(id);
return changed;
},
/** Whether a live-clock row is still moving: frames should keep coming. */
animating() {
if (spec.motionClock?.kind === 'scene' || spec.show === false)
@@ -557,5 +766,11 @@ export function createGpuIcons({
};
}
return { wants, check, attach };
/** A frame's ground samples, shared by the port's GPU icon sets. */
const groundBudget = () => ({
ends: GROUND_SAMPLES_PER_FRAME,
surface: SURFACE_SAMPLES_PER_FRAME,
});
return { wants, check, attach, groundBudget };
}
+146 -3
View File
@@ -318,8 +318,9 @@ test('without motionSegments, motion is ignored: billboards at the item’s plac
assert.equal(set.setColumns, undefined, 'no columns either');
});
test('GPU icon sets refuse what they cannot draw, changing nothing', () => {
const { part, draw } = drawOver();
test('on WebGL 1, GPU icon sets refuse what their rows read from extras, changing nothing', () => {
const { part, draw, scene } = drawOver();
scene.context = { webgl2: false };
const set = draw.icons({ id: 'gpu:refuse' });
set.set([icon('a', { motion: { kind: 'hold' } })]);
for (const extra of [
@@ -333,7 +334,7 @@ test('GPU icon sets refuse what they cannot draw, changing nothing', () => {
])
assert.throws(
() => set.upsert([icon('b', { motion: { kind: 'hold' }, ...extra })]),
/GPU icons have no/,
/GPU icons have no .* on WebGL 1/,
JSON.stringify(extra),
);
assert.equal(set.size(), 1);
@@ -349,6 +350,148 @@ test('GPU icon sets refuse what they cannot draw, changing nothing', () => {
assert.equal(billboards.size(), 1);
});
test('offsets, anchors, rotations, fades and display ranges go into a row’s extras', () => {
const { part, draw } = drawOver();
const set = draw.icons({ id: 'gpu:extras' });
// A billboard set holding them moves over whole.
set.set([icon('a', { anchor: 'top', pixelOffset: { x: 3, y: -4 } })]);
set.upsert([
icon('b', {
motion: { kind: 'hold' },
anchor: 'bottom',
rotationMode: 'screen',
rotation: 0.5,
fadeByDistance: { near: 10, nearValue: 1, far: 1e5, farValue: 0 },
displayRange: [5, 2e6],
}),
icon('c', {
motion: { kind: 'hold' },
rotationMode: 'axis',
alignedAxis: { x: 0, y: 0, z: 4 },
}),
icon('d', { motion: { kind: 'hold' }, displayRange: [10, 10] }),
]);
const { set: s } = rows(part, 'gpu:extras');
const a = s.row('a');
assert.deepEqual(s.offset(a), { x: 3, y: -4 });
assert.equal(s.anchor(a), -1, 'top');
const b = s.row('b');
assert.equal(s.anchor(b), 1, 'bottom');
assert.equal(s.rotation(b), 0.5);
assert.deepEqual(s.fade(b), {
near: 10,
nearValue: 1,
far: 1e5,
farValue: 0,
});
assert.deepEqual(s.displayRange(b), [5, 2e6]);
assert.equal(s.shown(b), true);
const c = s.row('c');
assert.deepEqual(s.axis(c), { x: 0, y: 0, z: 1 }, 'normalised');
assert.equal(s.rotation(c), 0);
assert.equal(s.shown(s.row('d')), false, 'an empty range shows nowhere');
// Dropped in place, each goes back to none.
set.upsert([icon('b', { motion: { kind: 'hold' } })]);
assert.equal(s.anchor(b), 0);
assert.equal(s.rotation(b), 0);
assert.deepEqual(s.fade(b), { near: 0, nearValue: 0, far: 0, farValue: 0 });
assert.deepEqual(s.displayRange(b), [0, 0]);
// A row moved by a removal keeps its own.
set.remove(['a']);
assert.deepEqual(s.axis(s.row('c')), { x: 0, y: 0, z: 1 });
});
test('clamped rows stand on the ground sampled under their segment ends, after a frame', () => {
const { part, draw, scene } = drawOver();
// Terrain rising 1 m a hundredth of a degree east.
const asked = [];
scene.globe = {
tilesLoaded: false,
getHeight: (carto) => {
asked.push(carto);
return Cesium.Math.toDegrees(carto.longitude) * 100;
},
};
const set = draw.icons({ id: 'gpu:ground', clampTo: 'terrain' });
const from = Cesium.Cartesian3.fromDegrees(4, 52, 900);
const to = Cesium.Cartesian3.fromDegrees(5, 52, 900);
const t0 = Date.now();
set.set([
icon('a', {
height: 'clampGround',
motion: { kind: 'lerp', from, to, t0, t1: t0 + 1000 },
}),
icon('b', { height: 'relativeGround', motion: { kind: 'hold' } }),
icon('c', { motion: { kind: 'hold' } }),
]);
const { set: s } = rows(part, 'gpu:ground');
const a = s.row('a');
assert.deepEqual(s.groundHeights(a), [0, 0], 'none sampled while drawing');
assert.equal(asked.length, 0);
const renders = scene.renders;
part.afterRender();
const [g0, g1] = s.groundHeights(a);
assert.ok(
Math.abs(g0 - 400) < 1e-3 && Math.abs(g1 - 500) < 1e-3,
`${g0} ${g1}`,
);
const [span0, span1] = s.groundSpan(a);
assert.ok(distance(span0, from) < 1 && distance(span1, to) < 1, 'its ends');
assert.equal(s.groundMode(a), 1, 'clamped');
assert.equal(s.groundMode(s.row('b')), 2, 'relative');
assert.equal(s.groundMode(s.row('c')), 0, 'absolute');
assert.ok(scene.renders > renders, 'a frame for the new ground');
// Sampled once: no samples in frames after.
const once = asked.length;
assert.equal(once, 3, 'two ends, and a held row’s one');
part.afterRender();
assert.equal(asked.length, once);
// Its tiles loaded: sampled again.
scene.globe.tilesLoaded = true;
part.afterRender();
assert.equal(asked.length, once * 2);
// Unclamped, it drops its ground.
set.upsert([icon('a', { motion: { kind: 'hold' } })]);
assert.equal(s.groundMode(a), 0);
assert.deepEqual(s.groundHeights(a), [0, 0]);
});
test('ground samples are budgeted across frames, the drawn surface the fewest', () => {
const { part, draw, scene } = drawOver();
let terrain = 0;
let surface = 0;
scene.globe = { getHeight: () => (terrain++, 10) };
scene.sampleHeightSupported = true;
scene.sampleHeight = () => (surface++, 30);
const set = draw.icons({ id: 'gpu:budget' });
const n = 300;
set.set(
Array.from({ length: n }, (_, i) =>
icon(`r${i}`, { height: 'clampGround', motion: { kind: 'hold' } }),
),
);
const { set: s } = rows(part, 'gpu:budget');
part.afterRender();
assert.ok(
terrain + surface <= 256,
`at most 256 a frame: ${terrain} ${surface}`,
);
assert.equal(surface, 8, 'eight on the drawn surface');
const heights = () =>
[...Array(n).keys()].map((i) => s.groundHeights(s.row(`r${i}`))[0]);
assert.equal(heights().filter((h) => h === 30).length, 8);
assert.equal(
heights().filter((h) => h === 10).length,
248,
'the rest on terrain for now',
);
for (let i = 0; i < 60; i++) part.afterRender();
assert.ok(
heights().every((h) => h === 30),
'in time all on the drawn surface',
);
});
test('GPU icon sets refuse clustering, changing nothing', () => {
const { part, draw } = drawOver();
const cluster = { pixelRange: 10, minimumSize: 2 };
+1
View File
@@ -66,6 +66,7 @@ export const CESIUM_CHECKS = new Set([
'draw.motion.arc',
'draw.motion.climb',
'draw.motion.live',
'draw.motion.look',
'draw.motion.shared',
'draw.pick',
'draw.points.update',
+131 -2
View File
@@ -12,13 +12,22 @@
* `{ id, meta, primitive }` (`primitive.show` hides that one row, as drillPick expects).
* Rows flagged to recede also take the aircraft limb recession, composed with focus.
*
* On WebGL 2 each row also reads its extras ({@link module:iconSet}'s
* `extras`) from a float texture by instance: a screen offset and anchor, a
* screen rotation or aligned axis (turned as a billboard's), a fade and a
* display range by distance, and the ground it is clamped to or stands
* above, taken between the heights under its segment's two ends. On WebGL 1
* the extras are not drawn (the draw port refuses rows that need them).
*
* This module is the one place the icon sets touch Cesium's renderer internals.
*/
import * as Cesium from 'cesium';
import { ICON_SEGMENT_GLSL, SEGMENT_STRIDE } from '../../data/iconSegment.js';
import {
EXTRA_STRIDE,
ICON_CULL_HEIGHT_M,
ICON_CULL_LIFT,
ICON_EXTRA_TEXELS,
STYLE_STRIDE,
} from '../../data/iconSet.js';
import {
@@ -74,6 +83,9 @@ uniform vec3 u_recessionFloors; // scale, alpha, combined alpha
uniform vec4 u_slots[ICON_SLOTS]; // ICON_SLOTS: as many as the context addresses
uniform float u_cull; // 1: hide rows past the ellipsoid's horizon
uniform float u_pickPass; // 1 while drawing for a pick
#ifdef ICON_EXTRAS
uniform highp sampler2D u_extras; // ICON_EXTRA_TEXELS texels a row, ICON_EXTRA_ROWS rows a line
#endif
out vec2 v_uv;
out vec4 v_color;
@@ -119,6 +131,32 @@ void main() {
vec3 rte = iconSegmentPosition(seg0, seg1, seg2, seg3, seg4, seg5, u_tau);
vec3 world = u_cameraPosition + rte;
#ifdef ICON_EXTRAS
ivec2 cell = ivec2(
(gl_InstanceID % ICON_EXTRA_ROWS) * ICON_EXTRA_TEXELS,
gl_InstanceID / ICON_EXTRA_ROWS
);
vec4 extra0 = texelFetch(u_extras, cell, 0); // offset x, y (CSS px, y down), anchor, rotation
vec4 extra1 = texelFetch(u_extras, cell + ivec2(1, 0), 0); // fade by distance
vec4 extra2 = texelFetch(u_extras, cell + ivec2(2, 0), 0); // aligned axis, range near
vec4 extra3 = texelFetch(u_extras, cell + ivec2(3, 0), 0); // range far, ground mode, heights
if (extra3.y > 0.5) {
// The ground under it: between the heights under its segment's ends,
// by how far along from one to the other it is.
vec3 from = texelFetch(u_extras, cell + ivec2(4, 0), 0).xyz;
vec3 span = texelFetch(u_extras, cell + ivec2(5, 0), 0).xyz - from;
float spanSq = dot(span, span);
float along = spanSq > 0.0 ? clamp(dot(world - from, span) / spanSq, 0.0, 1.0) : 0.0;
float ground = mix(extra3.z, extra3.w, along);
vec3 up = czm_geodeticSurfaceNormal(world, vec3(0.0), czm_ellipsoidInverseRadii * czm_ellipsoidInverseRadii);
float radius = length(world);
float height = radius * (1.0 - 1.0 / length(world * czm_ellipsoidInverseRadii));
// Clamped: on the ground; relative: its height above it.
vec3 lift = up * (extra3.y < 1.5 ? ground - height : ground);
rte += lift;
world += lift;
}
#endif
vec3 cullPoint = iconFlag(flags, ${ICON_CULL_LIFT.toFixed(1)}) ? iconCullPoint(world) : world;
if (u_cull > 0.5 && occludedByEllipsoid(cullPoint)) return;
@@ -127,6 +165,13 @@ void main() {
iconAnchorClip = czm_projection * positionEC;
float distanceSq = dot(positionEC.xyz, positionEC.xyz);
#ifdef ICON_EXTRAS
// Out of its display range, or faded to nothing: not drawn.
if (extra3.x > extra2.w &&
(distanceSq < extra2.w * extra2.w || distanceSq > extra3.x * extra3.x)) return;
float fade = extra1.z > extra1.x ? czm_nearFarScalar(extra1, distanceSq) : 1.0;
if (fade <= 0.0) return;
#endif
float distanceScale = style2.z > 0.0 ? czm_nearFarScalar(style2, distanceSq) : 1.0;
float scale = style0.z;
float alpha = style0.w;
@@ -154,6 +199,13 @@ void main() {
if (rect.z <= 0.0) return; // image not loaded yet
vec2 halfSize = style0.xy * (scale * distanceScale * 0.5) * corner;
vec2 shift = vec2(0.0);
#ifdef ICON_EXTRAS
alpha *= fade;
// Its anchor edge on the position, as a billboard's vertical origin, then
// its offset: neither turns with it.
shift = vec2(0.0, extra0.z) * abs(halfSize) + vec2(extra0.x, -extra0.y);
#endif
// A pick proxy: picked over the larger of it and the drawn icon.
if (u_pickPass > 0.5 && style3.y > 0.0 && style3.z > 0.0) {
halfSize = max(abs(halfSize), style3.yz * 0.5) * corner;
@@ -178,7 +230,22 @@ void main() {
halfSize = mat2(c, s, -s, c) * halfSize;
}
}
positionEC.xy += halfSize * czm_metersPerPixel(positionEC);
#ifdef ICON_EXTRAS
else {
// Turned as a billboard is: its rotation, and its up along the axis.
float angle = extra0.w;
vec4 axis = czm_modelView3D * vec4(extra2.xyz, 0.0);
float axisSq = axis.x * axis.x + axis.y * axis.y;
if (dot(extra2.xyz, extra2.xyz) > 0.0 && axisSq > 0.0)
angle += sign(-axis.x) * acos(sign(axis.y) * axis.y * axis.y / axisSq);
if (angle != 0.0) {
float c = cos(angle);
float s = sin(angle);
halfSize = mat2(c, s, -s, c) * halfSize;
}
}
#endif
positionEC.xy += (halfSize + shift) * czm_metersPerPixel(positionEC);
gl_Position = czm_projection * positionEC;
#ifdef LOG_DEPTH
@@ -240,6 +307,9 @@ export function contextIconSlots(context) {
return addressableIconSlots(Cesium.ContextLimits.maximumVertexUniformVectors);
}
/** Rows a line of the extras texture holds. */
export const ICON_EXTRA_ROWS = 256;
const SEGMENT_BYTES = SEGMENT_STRIDE * 4;
const STYLE_BYTES = STYLE_STRIDE * 4;
const EARTH_BOUNDS = new Cesium.BoundingSphere(
@@ -278,6 +348,9 @@ export class IconSetPrimitive {
this._programs = null;
this._commands = null;
this._texture = null;
// The rows' extras, on WebGL 2 (null until allocated, and on WebGL 1).
this._extras = null;
this._extrasLine = null;
this._atlasVersion = -1;
this._slots = Array.from(
{ length: MAX_ICON_SLOTS },
@@ -307,6 +380,7 @@ export class IconSetPrimitive {
u_recessionFloors: () => primitive._recessionFloors,
u_slots: () => primitive._slotUniform,
u_atlas: () => primitive._texture,
u_extras: () => primitive._extras,
u_cull: () => (primitive._cull() ? 1 : 0),
u_pickPass: () => primitive._pickPass,
};
@@ -456,6 +530,19 @@ export class IconSetPrimitive {
indexBuffer: buffers.index,
});
this._pickColors = new Uint8Array(capacity * 4);
if (context.webgl2) {
const lines = Math.ceil(capacity / ICON_EXTRA_ROWS);
this._extras = new Cesium.Texture({
context,
width: ICON_EXTRA_ROWS * ICON_EXTRA_TEXELS,
height: lines,
pixelFormat: Cesium.PixelFormat.RGBA,
pixelDatatype: Cesium.PixelDatatype.FLOAT,
flipY: false,
sampler: Cesium.Sampler.NEAREST,
});
this._extrasLine = new Float32Array(ICON_EXTRA_ROWS * EXTRA_STRIDE);
}
this._generation = set.generation;
for (const rows of Object.values(set.dirty)) rows.markAll();
if (this._commands)
@@ -489,9 +576,40 @@ export class IconSetPrimitive {
set.colors.subarray(from * 4, (to + 1) * 4),
from * 4,
);
if (this._extras) {
// Whole lines of the texture: a run's first to its last.
const lineFloats = ICON_EXTRA_ROWS * EXTRA_STRIDE;
const source = set.extras;
let done = -1;
for (const [from, to] of set.dirty.extras.runs(count)) {
for (
let line = Math.max(done + 1, Math.floor(from / ICON_EXTRA_ROWS));
line <= Math.floor(to / ICON_EXTRA_ROWS);
line++
) {
const start = line * lineFloats;
let view = source.subarray(start, start + lineFloats);
if (view.length < lineFloats) {
this._extrasLine.fill(0);
this._extrasLine.set(view);
view = this._extrasLine;
}
this._extras.copyFrom({
source: {
width: ICON_EXTRA_ROWS * ICON_EXTRA_TEXELS,
height: 1,
arrayBufferView: view,
},
yOffset: line,
});
done = line;
}
}
}
segments.clear();
style.clear();
colors.clear();
set.dirty.extras.clear();
}
_writePick(context, row) {
@@ -595,7 +713,16 @@ export class IconSetPrimitive {
Cesium.ShaderProgram.fromCache({
context,
vertexShaderSource: new Cesium.ShaderSource({
defines: [`ICON_SLOTS ${this._slotCount}`],
defines: [
`ICON_SLOTS ${this._slotCount}`,
...(this._extras
? [
'ICON_EXTRAS',
`ICON_EXTRA_ROWS ${ICON_EXTRA_ROWS}`,
`ICON_EXTRA_TEXELS ${ICON_EXTRA_TEXELS}`,
]
: []),
],
sources: [VERTEX_SHADER],
}),
fragmentShaderSource: new Cesium.ShaderSource({
@@ -644,6 +771,8 @@ export class IconSetPrimitive {
_destroyBuffers() {
this._vertexArray?.destroy(); // destroys its buffers too
this._vertexArray = null;
this._extras?.destroy();
this._extras = null;
this._buffers = null;
}
+138
View File
@@ -1922,6 +1922,144 @@ export function portChecks() {
set.dispose();
},
),
check(
'draw.motion.look',
'moving icons draw offset, anchored, turned, clamped, faded and ranged as icons do',
async (h, r) => {
await overAustin(h, r);
const set = r.draw.icons({
id: 'conf:motion-look',
motionClock: { kind: 'scene' },
});
const pos = h.toEcef({ ...AUSTIN, height: 0 });
const icon = (extra = {}) => ({
id: 'one',
pos,
image: DOT,
size: [16, 16],
motion: { kind: 'hold' },
...extra,
});
const mine = (x, y) =>
r.query
.drillPick(x, y)
.some((hit) => hit.set === 'conf:motion-look' && hit.id === 'one');
const show = async (extra) => {
set.set([icon(extra)]);
await drawn(h, r);
await settled(r);
await drawn(h, r);
};
await show();
const s = r.query.project(pos);
ok(s, 'the icon projects');
ok(mine(s.x, s.y), 'drawn where it is');
await show({ pixelOffset: { x: 40, y: 0 } });
ok(mine(s.x + 40, s.y) && !mine(s.x, s.y), 'offset 40 px right');
await show({ anchor: 'bottom', size: [16, 48] });
ok(
mine(s.x, s.y - 36) && !mine(s.x, s.y + 12),
'anchored by its bottom: above its position',
);
// Long and flat; a quarter turn stands it up.
await show({ size: [64, 8] });
ok(mine(s.x + 24, s.y) && !mine(s.x, s.y + 24), 'unturned, it is wide');
await show({
size: [64, 8],
rotationMode: 'screen',
rotation: Math.PI / 2,
});
ok(
mine(s.x, s.y + 24) && !mine(s.x + 24, s.y),
'a quarter turn on screen stands it up',
);
// Looking north, an up along east turns it a quarter clockwise.
const east = h.toEcef({
lon: AUSTIN.lon + 0.01,
lat: AUSTIN.lat,
height: 0,
});
await show({
size: [64, 8],
rotationMode: 'axis',
alignedAxis: {
x: east.x - pos.x,
y: east.y - pos.y,
z: east.z - pos.z,
},
});
ok(
mine(s.x, s.y + 24) && !mine(s.x + 24, s.y),
'its up along east stands it up',
);
// Off to the east and a kilometre up, clamped: on the ground there.
const where = { lon: AUSTIN.lon + 0.004, lat: AUSTIN.lat };
const ground = r.query.project(h.toEcef({ ...where, height: 0 }));
const raised = r.query.project(h.toEcef({ ...where, height: 1000 }));
ok(
ground && raised && Math.abs(raised.x - ground.x) > 30,
'apart on screen',
);
await show({ pos: h.toEcef({ ...where, height: 1000 }) });
ok(
mine(raised.x, raised.y) && !mine(ground.x, ground.y),
'absolute: up high',
);
await show({
pos: h.toEcef({ ...where, height: 1000 }),
height: 'clampGround',
});
ok(
mine(ground.x, ground.y) && !mine(raised.x, raised.y),
'clamped: on the ground',
);
// Moving between two such points, clamped: on the ground between.
const lerpAt = (a, b, t) => ({
x: a.x + (b.x - a.x) * t,
y: a.y + (b.y - a.y) * t,
z: a.z + (b.z - a.z) * t,
});
const west = { lon: AUSTIN.lon - 0.004, lat: AUSTIN.lat };
const from = h.toEcef({ ...where, height: 1000 });
const to = h.toEcef({ ...west, height: 1000 });
const t0 = r.frame.time();
r.frame.setTime(t0 + 2500);
await show({
pos: from,
height: 'clampGround',
motion: { kind: 'lerp', from, to, t0, t1: t0 + 10000 },
});
const quarter = r.query.project(
lerpAt(
h.toEcef({ ...where, height: 0 }),
h.toEcef({ ...west, height: 0 }),
0.25,
),
);
const up = r.query.project(lerpAt(from, to, 0.25));
ok(
mine(quarter.x, quarter.y) && !mine(up.x, up.y),
'moving clamped: on the ground under it',
);
// The eye is 2 km off.
const curve = (value) => ({
near: 1e4,
nearValue: value,
far: 1e7,
farValue: value,
});
await show({ fadeByDistance: curve(0) });
ok(!mine(s.x, s.y), 'faded out by distance: not drawn');
await show({ fadeByDistance: curve(1) });
ok(mine(s.x, s.y), 'not faded: drawn');
await show({ displayRange: [0, 100] });
ok(!mine(s.x, s.y), 'out of its display range: not drawn');
await show({ displayRange: [0, 1e6] });
ok(mine(s.x, s.y), 'in its display range: drawn');
set.dispose();
},
{ capability: 'motionSegments' },
),
check(
'draw.lines.depthFail',
'a line dashed behind the surface draws, moves in place and changes back to solid',
+47 -9
View File
@@ -754,13 +754,17 @@ export function createStubRenderer(options = {}) {
function itemPosition(set, item, aheadMs = 0) {
if (!item.pos) return null;
const base = toEcef(item.pos);
if (
set.kind === 'icons' &&
item.motion &&
capabilities.has('motionSegments')
)
return motionPosition(item.motion, base, motionTime(set) + aheadMs);
return base;
const p =
set.kind === 'icons' && item.motion && capabilities.has('motionSegments')
? motionPosition(item.motion, base, motionTime(set) + aheadMs)
: base;
// The stub's ground is the ellipsoid: a clamped icon stands on it, one
// relative to it where it is.
if (set.kind !== 'icons' || item.height !== 'clampGround' || !p) return p;
const c = toCartographicRadians(p);
return c
? toEcef({ lon: toDegrees(c.lon), lat: toDegrees(c.lat), height: 0 })
: p;
}
/**
@@ -995,9 +999,43 @@ export function createStubRenderer(options = {}) {
: 1;
// Scaled to nothing, it is not drawn.
if (scale <= 0) continue;
// An icon's centre: its anchor edge on the position (as drawn, not
// as a proxy), then its pixel offset (y down).
let cx = s.x;
let cy = s.y;
let dx = x - cx;
let dy = y - cy;
if (set.kind === 'icons') {
const lift = { bottom: 1, top: -1 }[item.anchor] ?? 0;
cx += item.pixelOffset?.x ?? 0;
cy +=
(item.pixelOffset?.y ?? 0) -
(lift * (item.size?.[1] ?? 0) * scale) / 2;
dx = x - cx;
dy = y - cy;
// Turned counter-clockwise on screen (its up along the axis
// as projected there): the pick turned back.
let turn = item.rotationMode === 'screen' ? (item.rotation ?? 0) : 0;
const axis = item.rotationMode === 'axis' ? item.alignedAxis : null;
if (
axis &&
magnitude(vec(axis.x ?? 0, axis.y ?? 0, axis.z ?? 0)) > 0
) {
const tip = project(
add(p, normalize(vec(axis.x ?? 0, axis.y ?? 0, axis.z ?? 0))),
);
if (tip && (tip.x !== s.x || tip.y !== s.y))
turn = Math.atan2(-(tip.x - s.x), -(tip.y - s.y));
}
if (turn !== 0) {
const c = Math.cos(turn);
const n = Math.sin(turn);
[dx, dy] = [dx * c - dy * n, dx * n + dy * c];
}
}
if (
Math.abs(s.x - x) <= (size[0] * scale) / 2 + halfW &&
Math.abs(s.y - y) <= (size[1] * scale) / 2 + halfH
Math.abs(dx) <= (size[0] * scale) / 2 + halfW &&
Math.abs(dy) <= (size[1] * scale) / 2 + halfH
)
hits.push({
set: setId,