Move the bikeshare layer onto the renderer's draw and camera ports

Station dots and the selected-station highlight are now two point draw
sets, picked through the draw hit, and the layer follows the camera
through the camera port at its own threshold instead of the shared
percentageChanged ledger. Altitude and view centre come from the camera
pose and view rectangle. All five bikeshare files leave the Cesium
allow-list.

The Cesium draw port now keeps a rebuilt host in its place in the scene
instead of re-adding it on top, tags hosts with their set id so sprite
order can raise a draw set (registerSpriteSet), and restyles unclamped
points in place when they have not moved, so a status poll or selection
no longer rebuilds every station. Other draw-port layers (cyclone
markers, earthquake discs) now also keep their first-build slot rather
than jumping above the sprite layers on each rebuild.
This commit is contained in:
Sameh Khamis committed 2026-11-06 20:26:35 -08:00
1 parent 2b66c46a30
commit 8c6fa6258b
22 files changed
+732 -249

No files matched your search

+104 -34
View File
@@ -1,4 +1,3 @@
import * as Cesium from 'cesium';
import {
DEFAULT_CAPACITY,
POINT_SIZE_MIN,
@@ -11,8 +10,21 @@ import {
POINT_HEIGHT_OFFSET_M,
MAX_TOTAL_POINTS,
COLOR_OUTLINE,
POINT_SCALE_BY_DISTANCE,
POINT_FADE_BY_DISTANCE,
POINT_DISABLE_DEPTH_TEST_M,
} from './policy.js';
import { heightsFor } from '../../queries/heights.js';
import { fromDegrees } from '../../geo/ellipsoid.js';
import { drawOf } from '../../render/rendererOf.js';
/**
* The draw sets the stations are drawn in start with this; each layer has
* its own (`bikeshare:1:stations`, `bikeshare:1:selected`), so several can
* share a renderer. Picks name them as `Hit.set`.
*/
export const BIKESHARE_SET_ID = 'bikeshare';
let instances = 0;
export function createRendering({
state: layerState,
@@ -22,6 +34,68 @@ export function createRendering({
}) {
const { governorRequestRender } = services.render;
/**
* Make the layer's draw sets on `viewer`'s renderer (`drawOf`): the
* station dots, hidden until the layer is enabled, and the selected
* station's highlight. Sets made before are let go first.
* @param {object} viewer
*/
function attachSets(viewer) {
const draw = drawOf(viewer);
detachSets();
const prefix = `${BIKESHARE_SET_ID}:${++instances}`;
layerState._stationSet = draw.points({
id: `${prefix}:stations`,
blend: 'translucent',
show: false,
});
layerState._selectedSet = draw.points({ id: `${prefix}:selected` });
// Drawn from the start, empty, so both take their place in the scene
// now, as the layer's collections did; the stations' place among the
// other sprite layers is the sprite order's (`restoreSpriteOrder`).
layerState._stationSet.set([]);
layerState._selectedSet.set([]);
layerState._spriteEntry = services.sprites?.registerSpriteSet?.(
'bikeshare',
layerState._stationSet.id,
);
}
/** Let the layer's draw sets go. */
function detachSets() {
if (layerState._spriteEntry)
services.sprites?.unregisterSpriteCollection?.(
'bikeshare',
layerState._spriteEntry,
);
layerState._spriteEntry = null;
layerState._stationSet?.dispose();
layerState._selectedSet?.dispose();
layerState._stationSet = null;
layerState._selectedSet = null;
}
/**
* A station's dot, from its render record; the selected station's own dot
* is hidden while its highlight replaces it.
* @param {Object} record - Render record from _stationRenderMap.
* @returns {import('../../render/contract/draw').PointItem}
*/
function stationItem(record) {
return {
id: record.key,
pos: record.position,
pixelSize: record.pixelSize,
color: record.color,
outlineColor: COLOR_OUTLINE,
outlineWidth: 1,
scaleByDistance: POINT_SCALE_BY_DISTANCE,
fadeByDistance: POINT_FADE_BY_DISTANCE,
disableDepthTest: POINT_DISABLE_DEPTH_TEST_M,
show: record.key !== layerState._selectedKey,
};
}
/**
* Resolve the effective dock capacity for a station.
* Prefers the explicit capacity from station_information; falls back to
@@ -69,7 +143,7 @@ export function createRendering({
* - <30% bikes available: red.
* @param {Object|null} status - Station status object.
* @param {number} capacity - Resolved station capacity.
* @returns {Cesium.Color} Color to apply to the station point.
* @returns {readonly number[]} Renderer colour `[r, g, b, a]` for the station point.
*/
function statusToColor(status, capacity) {
@@ -88,10 +162,10 @@ export function createRendering({
}
/**
* Create a Cartesian3 position for a station, terrain-clamped when possible.
* The ECEF position for a station, on the sampled surface when possible.
* Falls back to a fixed small height offset if terrain sampling is unsupported.
* @param {Object} station - Station info object with lat/lon.
* @returns {Cesium.Cartesian3|null} World position, or null if coordinates are invalid.
* @returns {{x: number, y: number, z: number}|null} ECEF metres, or null if coordinates are invalid.
*/
function createStationPosition(station) {
@@ -107,7 +181,7 @@ export function createRendering({
if (sampled !== null) height = sampled + POINT_HEIGHT_OFFSET_M;
}
return Cesium.Cartesian3.fromDegrees(lon, lat, height);
return fromDegrees(lon, lat, height);
}
/**
@@ -125,8 +199,8 @@ export function createRendering({
}
/**
* Ensure point primitives exist for all stations in a city.
* Creates new points for stations not yet rendered; skips existing ones.
* Ensure dots exist for all stations in a city.
* Creates new dots for stations not yet rendered; skips existing ones.
* Respects the MAX_TOTAL_POINTS global cap to prevent GPU overload.
* @param {string} cityId - City identifier.
* @param {Map<string, Object>} stationMap - Parsed station info map for the city.
@@ -138,6 +212,7 @@ export function createRendering({
governorRequestRender('bikeshare-points');
const runtime = ensureCityRuntime(cityId);
const added = [];
for (const station of stationMap.values()) {
const key = parts.model.stationKey(cityId, station.stationId);
if (layerState._stationRenderMap.has(key)) {
@@ -159,30 +234,14 @@ export function createRendering({
const position = createStationPosition(station);
if (!position) continue;
// Add a point primitive with distance-based scale and translucency falloff
const point = layerState._pointCollection.add({
position,
pixelSize: capacityToPixelSize(station.capacity),
color: COLOR_NEUTRAL,
outlineColor: COLOR_OUTLINE,
outlineWidth: 1,
scaleByDistance: new Cesium.NearFarScalar(200, 1.35, 130000, 0.4),
translucencyByDistance: new Cesium.NearFarScalar(
200,
1.0,
180000,
0.15,
),
disableDepthTestDistance: 2500,
id: key,
});
layerState._stationRenderMap.set(key, {
const record = {
key,
cityId,
stationId: station.stationId,
stationName: station.name,
point,
position,
pixelSize: capacityToPixelSize(station.capacity),
color: COLOR_NEUTRAL,
capacity: parts.model.toNonNegativeInteger(station.capacity),
publishedCapacity: parts.model.toNonNegativeInteger(station.capacity),
bikesAvailable: null,
@@ -190,16 +249,20 @@ export function createRendering({
isInstalled: station.isInstalled,
isRenting: station.isRenting,
isReturning: station.isReturning,
});
};
layerState._stationRenderMap.set(key, record);
// A dot with distance-based scale and translucency falloff.
added.push(stationItem(record));
runtime.stationKeys.add(key);
}
if (added.length) layerState._stationSet?.upsert(added);
layerState._count = layerState._stationRenderMap.size;
}
/**
* Remove all rendered point primitives for a city and clean up runtime state.
* Remove all rendered dots for a city and clean up runtime state.
* Also clears any active selection if it belongs to this city.
* @param {string} cityId - City identifier to remove.
*/
@@ -216,12 +279,13 @@ export function createRendering({
parts.selection._clearSelection();
}
const removed = [];
for (const key of runtime.stationKeys) {
const record = layerState._stationRenderMap.get(key);
if (!record) continue;
layerState._pointCollection.remove(record.point);
if (!layerState._stationRenderMap.has(key)) continue;
removed.push(key);
layerState._stationRenderMap.delete(key);
}
if (removed.length) layerState._stationSet?.remove(removed);
layerState._cityRuntime.delete(cityId);
layerState._count = layerState._stationRenderMap.size;
@@ -240,6 +304,7 @@ export function createRendering({
const runtime = layerState._cityRuntime.get(cityId);
if (!runtime) return;
const updated = [];
for (const key of runtime.stationKeys) {
const record = layerState._stationRenderMap.get(key);
if (!record) continue;
@@ -266,11 +331,16 @@ export function createRendering({
);
// Update visual properties based on current status
record.point.pixelSize = capacityToPixelSize(capacity);
record.point.color = statusToColor(status, capacity);
record.pixelSize = capacityToPixelSize(capacity);
record.color = statusToColor(status, capacity);
updated.push(stationItem(record));
}
if (updated.length) layerState._stationSet?.upsert(updated);
}
return {
attachSets,
detachSets,
stationItem,
resolveCapacity,
capacityToPixelSize,
statusToColor,