mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-16 08:12:24 +00:00
GPU icon sets cluster their rows where their segments put them
clusterScreenPoints over each GPU set's shown rows at the frame's motion-clock time (on their ground when clamped), the members' rows hidden and one icon a cluster drawn at its seed's size in the set's cluster billboards, picked as each member. Clustered again after the camera moves or the set changes (setColumns included), and every frame while rows still move. Conformance checks take several capabilities; draw.motion.cluster clusters members that come to rest on their segments.
This commit is contained in:
1 parent
933969b7da
commit
d9ad7da9d5
6 files changed
+385
-144
No files matched your search
+52
-14
@@ -648,8 +648,16 @@ export function createCesiumDraw({
|
||||
},
|
||||
};
|
||||
// A clustered set clusters again in the next frame drawn after a change.
|
||||
for (const name of ['set', 'upsert', 'remove', 'clear', 'setOptions']) {
|
||||
for (const name of [
|
||||
'set',
|
||||
'upsert',
|
||||
'remove',
|
||||
'clear',
|
||||
'setOptions',
|
||||
'setColumns',
|
||||
]) {
|
||||
const call = handle[name];
|
||||
if (!call) continue;
|
||||
handle[name] = (...args) => {
|
||||
record.clusterKey = null;
|
||||
return call(...args);
|
||||
@@ -992,8 +1000,7 @@ export function createCesiumDraw({
|
||||
const clusters = (record) =>
|
||||
record.kind === 'icons' &&
|
||||
Boolean(record.spec.cluster) &&
|
||||
capabilities.has('cluster') &&
|
||||
!record.gpu;
|
||||
capabilities.has('cluster');
|
||||
|
||||
/** Let go of a set's cluster icons, and show its members again. */
|
||||
function dropClusters(record) {
|
||||
@@ -1003,6 +1010,7 @@ export function createCesiumDraw({
|
||||
record.clusterState = null;
|
||||
record.clusterKey = null;
|
||||
for (const placed of record.placed) placed.clustered = false;
|
||||
record.gpu?.setClustered(new Set());
|
||||
}
|
||||
|
||||
const clusterScratch = new Cesium.Cartesian2();
|
||||
@@ -1011,7 +1019,7 @@ export function createCesiumDraw({
|
||||
* icons again where the camera moved or the set changed since it last
|
||||
* did (`ClusterSpec`), hiding the members and drawing an icon a cluster.
|
||||
*/
|
||||
function clusterIcons() {
|
||||
function clusterIcons(frameState = scene.frameState) {
|
||||
let pose;
|
||||
const poseNow = () => {
|
||||
const eye = scene.camera?.positionWC;
|
||||
@@ -1027,30 +1035,59 @@ export function createCesiumDraw({
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (!record.billboardHost || record.billboardHost.isDestroyed()) continue;
|
||||
// A rebuilt collection starts afresh.
|
||||
if (record.billboardHost !== record.clusteredHost) {
|
||||
// A rebuilt collection (or the GPU's host in its place) starts afresh.
|
||||
const host = record.gpu?.host ?? record.billboardHost;
|
||||
if (!host || host.isDestroyed()) continue;
|
||||
if (host !== record.clusteredHost) {
|
||||
record.clusterState = null;
|
||||
record.clusterKey = null;
|
||||
record.clusteredHost = record.billboardHost;
|
||||
record.clusteredHost = host;
|
||||
}
|
||||
pose ??= poseNow();
|
||||
if (record.clusterKey === pose) {
|
||||
// Rows still moving on their segments cluster again every frame.
|
||||
const key = record.gpu?.moving(frameState)
|
||||
? `${pose}@${frameState?.frameNumber ?? Math.random()}`
|
||||
: pose;
|
||||
if (record.clusterKey === key) {
|
||||
if (record.clusterHost)
|
||||
record.clusterHost.show = record.spec.show !== false;
|
||||
continue;
|
||||
}
|
||||
record.clusterKey = pose;
|
||||
reclusterIcons(record);
|
||||
record.clusterKey = key;
|
||||
reclusterIcons(record, frameState);
|
||||
}
|
||||
}
|
||||
|
||||
function reclusterIcons(record) {
|
||||
/** The points a GPU set clusters: its shown rows where they are drawn. */
|
||||
function gpuClusterPoints(record, frameState, horizon) {
|
||||
const points = [];
|
||||
for (const { id, item, position } of record.gpu.drawnRows(frameState)) {
|
||||
const at = toCartesian(position);
|
||||
if (horizon && !horizon.isPointVisible(at)) continue;
|
||||
const screen = Cesium.SceneTransforms.worldToWindowCoordinates(
|
||||
scene,
|
||||
at,
|
||||
clusterScratch,
|
||||
);
|
||||
if (!screen) continue;
|
||||
points.push({
|
||||
id,
|
||||
position: at,
|
||||
screen: { x: screen.x, y: screen.y },
|
||||
pixelSize: Math.max(item.size[0], item.size[1]) * (item.scale ?? 1),
|
||||
});
|
||||
}
|
||||
return points;
|
||||
}
|
||||
|
||||
function reclusterIcons(record, frameState) {
|
||||
const spec = record.spec.cluster;
|
||||
const horizon = horizonNow();
|
||||
const points = [];
|
||||
const points = record.gpu
|
||||
? gpuClusterPoints(record, frameState, horizon)
|
||||
: [];
|
||||
const entries = new Map();
|
||||
for (const [id, entry] of record.billboards) {
|
||||
for (const [id, entry] of record.gpu ? [] : record.billboards) {
|
||||
const item = record.items.get(id);
|
||||
const placed = entry.placed;
|
||||
if (!item || !placed.shown) continue;
|
||||
@@ -1089,6 +1126,7 @@ export function createCesiumDraw({
|
||||
});
|
||||
record.clusterState = result.state;
|
||||
const inCluster = new Set(result.clusters.flatMap((c) => c.ids));
|
||||
record.gpu?.setClustered(inCluster);
|
||||
for (const placed of record.placed) placed.clustered = false;
|
||||
for (const id of inCluster) {
|
||||
const entry = entries.get(id);
|
||||
|
||||
@@ -11,8 +11,9 @@
|
||||
*
|
||||
* 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
|
||||
* draw what billboards draw but clustering, which they refuse loudly; on
|
||||
* WebGL 1 they also refuse what each row reads from its extras texture
|
||||
* draw what billboards draw, clustering included (the draw port clusters
|
||||
* the rows where their segments put them in the frame drawn); on WebGL 1
|
||||
* they 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.
|
||||
*
|
||||
@@ -233,8 +234,6 @@ export function createGpuIcons({
|
||||
|
||||
/** Refuse what a GPU icon set cannot draw, before anything changes. */
|
||||
function check(items, spec = {}) {
|
||||
if (spec.cluster)
|
||||
throw new Error("the Cesium renderer's GPU icons have no clustering");
|
||||
// Atlas slots these items would take anew: they must all fit.
|
||||
const fresh = new Set();
|
||||
for (const item of items) {
|
||||
@@ -325,6 +324,8 @@ export function createGpuIcons({
|
||||
// Until when (epoch ms on the motion clock) a row may still move or turn.
|
||||
let movingUntil = -Infinity;
|
||||
const clockMs = () => Cesium.JulianDate.toDate(clockTime()).getTime();
|
||||
// Rows drawn as part of a cluster, hidden.
|
||||
let clustered = new Set();
|
||||
// What each row's segment was last written from, by id.
|
||||
const written = new Map();
|
||||
// The ids of the last columns, while no other write came after them.
|
||||
@@ -428,7 +429,12 @@ export function createGpuIcons({
|
||||
);
|
||||
set.setDepthTestDistance(row, item.disableDepthTest ?? 0);
|
||||
// An empty range shows it nowhere.
|
||||
set.setShown(row, item.show !== false && displayable(item.displayRange));
|
||||
set.setShown(
|
||||
row,
|
||||
item.show !== false &&
|
||||
displayable(item.displayRange) &&
|
||||
!clustered.has(item.id),
|
||||
);
|
||||
writeExtras(row, item);
|
||||
writeSegment(row, item, added, motion);
|
||||
writeGround(row, item);
|
||||
@@ -556,6 +562,49 @@ export function createGpuIcons({
|
||||
groundQueue.add(item.id);
|
||||
}
|
||||
|
||||
/**
|
||||
* `p` on a clamped row's ground, as its shader puts it: the ground
|
||||
* between the heights under its segment's ends, by how far along it is.
|
||||
*/
|
||||
function onGround(row, p) {
|
||||
const [from, to] = set.groundSpan(row);
|
||||
const [g0, g1] = set.groundHeights(row);
|
||||
const sx = to.x - from.x;
|
||||
const sy = to.y - from.y;
|
||||
const sz = to.z - from.z;
|
||||
const spanSq = sx * sx + sy * sy + sz * sz;
|
||||
const along =
|
||||
spanSq > 0
|
||||
? Math.min(
|
||||
1,
|
||||
Math.max(
|
||||
0,
|
||||
((p.x - from.x) * sx +
|
||||
(p.y - from.y) * sy +
|
||||
(p.z - from.z) * sz) /
|
||||
spanSq,
|
||||
),
|
||||
)
|
||||
: 0;
|
||||
const ground = g0 + (g1 - g0) * along;
|
||||
const carto = Cesium.Cartographic.fromCartesian(
|
||||
Cesium.Cartesian3.fromElements(p.x, p.y, p.z, groundAt),
|
||||
undefined,
|
||||
groundCarto,
|
||||
);
|
||||
if (!carto) return p;
|
||||
const height =
|
||||
set.groundMode(row) === ICON_GROUND_CLAMP
|
||||
? ground
|
||||
: carto.height + ground;
|
||||
const c = Cesium.Cartesian3.fromRadians(
|
||||
carto.longitude,
|
||||
carto.latitude,
|
||||
height,
|
||||
);
|
||||
return { x: c.x, y: c.y, z: c.z };
|
||||
}
|
||||
|
||||
const groundAt = new Cesium.Cartesian3();
|
||||
const groundCarto = new Cesium.Cartographic();
|
||||
/**
|
||||
@@ -744,6 +793,53 @@ export function createGpuIcons({
|
||||
for (const id of later) groundQueue.add(id);
|
||||
return changed;
|
||||
},
|
||||
/**
|
||||
* The rows a cluster pass sees: each whose item is shown, where it
|
||||
* is drawn at `frameState`'s time (on its segment, on its ground),
|
||||
* with its item.
|
||||
*/
|
||||
drawnRows(frameState) {
|
||||
const tau = set.seconds(clockTime(frameState));
|
||||
const out = [];
|
||||
for (let row = 0; row < set.count; row++) {
|
||||
const id = set.idAt(row);
|
||||
const item = record.items.get(id);
|
||||
if (!item || item.show === false || !displayable(item.displayRange))
|
||||
continue;
|
||||
const p = set.position(row, tau, { x: 0, y: 0, z: 0 });
|
||||
out.push({
|
||||
id,
|
||||
item,
|
||||
position: grounded.has(id) ? onGround(row, p) : p,
|
||||
});
|
||||
}
|
||||
return out;
|
||||
},
|
||||
/** Hide the rows of `ids` (in clusters); show the rest again. */
|
||||
setClustered(ids) {
|
||||
const before = clustered;
|
||||
clustered = ids;
|
||||
for (const id of new Set([...before, ...ids])) {
|
||||
if (before.has(id) === ids.has(id)) continue;
|
||||
const row = set.row(id);
|
||||
const item = record.items.get(id);
|
||||
if (row < 0 || !item) continue;
|
||||
set.setShown(
|
||||
row,
|
||||
item.show !== false &&
|
||||
displayable(item.displayRange) &&
|
||||
!ids.has(id),
|
||||
);
|
||||
}
|
||||
},
|
||||
/** Whether rows still move at the motion clock's time in `frameState`. */
|
||||
moving(frameState) {
|
||||
return (
|
||||
set.count > 0 &&
|
||||
Cesium.JulianDate.toDate(clockTime(frameState)).getTime() <
|
||||
movingUntil
|
||||
);
|
||||
},
|
||||
/** Whether a live-clock row is still moving: frames should keep coming. */
|
||||
animating() {
|
||||
if (spec.motionClock?.kind === 'scene' || spec.show === false)
|
||||
|
||||
@@ -492,19 +492,92 @@ test('ground samples are budgeted across frames, the drawn surface the fewest',
|
||||
);
|
||||
});
|
||||
|
||||
test('GPU icon sets refuse clustering, changing nothing', () => {
|
||||
const { part, draw } = drawOver();
|
||||
const cluster = { pixelRange: 10, minimumSize: 2 };
|
||||
const set = draw.icons({ id: 'gpu:cluster' });
|
||||
set.set([icon('a', { motion: { kind: 'hold' } })]);
|
||||
assert.throws(() => set.setOptions({ cluster }), /no clustering/);
|
||||
assert.equal(part.sets.get('gpu:cluster').spec.cluster, undefined);
|
||||
const clustered = draw.icons({ id: 'gpu:clustered', cluster });
|
||||
assert.throws(
|
||||
() => clustered.set([icon('a', { motion: { kind: 'hold' } })]),
|
||||
/no clustering/,
|
||||
);
|
||||
assert.equal(clustered.size(), 0);
|
||||
test('a GPU icon set clusters its rows where their segments put them in the frame', () => {
|
||||
const transforms = Cesium.SceneTransforms;
|
||||
const project = transforms.worldToWindowCoordinates;
|
||||
// Window positions: a degree of longitude is `spread` pixels.
|
||||
let spread = 1;
|
||||
transforms.worldToWindowCoordinates = (_, position, result) => {
|
||||
const at = Cesium.Cartographic.fromCartesian(position);
|
||||
result ??= new Cesium.Cartesian2();
|
||||
result.x = 200 + Cesium.Math.toDegrees(at.longitude) * spread;
|
||||
result.y = 150 - Cesium.Math.toDegrees(at.latitude) * spread;
|
||||
return result;
|
||||
};
|
||||
try {
|
||||
const { part, draw, scene } = drawOver({
|
||||
capabilities: new Set(['motionSegments', 'columns', 'cluster']),
|
||||
});
|
||||
scene.camera.directionWC = new Cesium.Cartesian3(-1, 0, 0);
|
||||
scene.camera.positionCartographic = { height: 1000 };
|
||||
scene.canvas = { clientWidth: 400, clientHeight: 300 };
|
||||
const t0 = Cesium.JulianDate.toDate(scene.frameState.time).getTime();
|
||||
const counts = [];
|
||||
const set = draw.icons({
|
||||
id: 'gpu:cluster',
|
||||
motionClock: { kind: 'scene' },
|
||||
cluster: {
|
||||
pixelRange: 10,
|
||||
minimumSize: 2,
|
||||
icon: (count) => (counts.push(count), 'badge.png'),
|
||||
},
|
||||
});
|
||||
// Each given far apart, each moving to within a few pixels of the rest.
|
||||
const mover = (id, lon) => {
|
||||
const from = ecef(Cesium.Cartesian3.fromDegrees(lon * 40, 30));
|
||||
const to = ecef(Cesium.Cartesian3.fromDegrees(lon, 0));
|
||||
return icon(id, {
|
||||
pos: from,
|
||||
size: [8, 8],
|
||||
meta: { id },
|
||||
motion: { kind: 'lerp', from, to, t0, t1: t0 + 1000 },
|
||||
});
|
||||
};
|
||||
set.set([mover('a', 0), mover('b', 2), mover('c', 4)]);
|
||||
// At the start, apart: no cluster.
|
||||
part.beforeRender();
|
||||
const { set: s } = rows(part, 'gpu:cluster');
|
||||
assert.deepEqual(counts, []);
|
||||
assert.ok(['a', 'b', 'c'].every((id) => s.shown(s.row(id))));
|
||||
// At the end, together.
|
||||
scene.frameState.time = julian(t0 + 5000);
|
||||
part.beforeRender();
|
||||
assert.deepEqual(counts, [3]);
|
||||
assert.ok(
|
||||
['a', 'b', 'c'].every((id) => !s.shown(s.row(id))),
|
||||
'members are not drawn',
|
||||
);
|
||||
const clusters = part.sets.get('gpu:cluster').clusterHost;
|
||||
assert.equal(clusters.length, 1);
|
||||
const { id, width } = clusters.get(0);
|
||||
assert.equal(width, 8, 'at its seed’s size');
|
||||
assert.deepEqual(id.cluster.ids, ['a', 'b', 'c'], 'seed first');
|
||||
const mean = Cesium.Cartographic.fromCartesian(
|
||||
new Cesium.Cartesian3(
|
||||
id.cluster.position.x,
|
||||
id.cluster.position.y,
|
||||
id.cluster.position.z,
|
||||
),
|
||||
);
|
||||
assert.ok(Math.abs(Cesium.Math.toDegrees(mean.longitude) - 2) < 1e-3);
|
||||
// Held still, not made again.
|
||||
part.beforeRender();
|
||||
assert.deepEqual(counts, [3]);
|
||||
// The camera moved: apart, each drawn again.
|
||||
spread = 100;
|
||||
scene.camera.positionWC = new Cesium.Cartesian3(2e7, 1, 0);
|
||||
part.beforeRender();
|
||||
assert.equal(clusters.length, 0);
|
||||
assert.ok(['a', 'b', 'c'].every((id) => s.shown(s.row(id))));
|
||||
// A member hidden by its item stays hidden out of a cluster.
|
||||
set.upsert([{ ...mover('b', 2), show: false }]);
|
||||
part.beforeRender();
|
||||
assert.equal(s.shown(s.row('b')), false);
|
||||
set.dispose();
|
||||
assert.ok(!scene.added.includes(clusters), 'let go with the set');
|
||||
} finally {
|
||||
transforms.worldToWindowCoordinates = project;
|
||||
}
|
||||
});
|
||||
|
||||
test('columns update rows by index: positions, courses, shown, scales, tints and meta', () => {
|
||||
|
||||
@@ -65,6 +65,7 @@ export const CESIUM_CHECKS = new Set([
|
||||
'draw.motion',
|
||||
'draw.motion.arc',
|
||||
'draw.motion.climb',
|
||||
'draw.motion.cluster',
|
||||
'draw.motion.live',
|
||||
'draw.motion.look',
|
||||
'draw.motion.shared',
|
||||
|
||||
Reference in new issue
Block a user