mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-16 08:12:24 +00:00
The renderer contract is what the application asks of a 3D renderer. It is nine ports (frame, globe, draw, camera, query, input, effects, credits, quality) plus the renderer itself, one strict .d.ts per port in src/render/contract/. PORTS lists every member at run time, and a TypeScript-compiler test fails when the list and the types disagree on one member. An optional member exists only with its capability; a required one answers null or empty without it. Nothing in the application uses the contract yet. The stub renderer (src/render/stub/) implements every port in memory and draws nothing. Its camera is a port of the default camera's maths: views, lookAt, flights with their height arc and easing, rectangle framing on the geodesic midpoint, the view rectangle with its horizon and pole cases, changed thresholds, follows, and flights that finish inside the call. The conformance suite (src/render/conformance/) runs 59 checks against a renderer through the contract alone, in Node or in a browser page. The camera checks compare traces recorded from Cesium's own Camera, headless on a virtual clock (scripts/record-camera-goldens.mjs), to 1 mm, 1e-9 and 1e-3 px. The other checks cover listener rules on every subscription, the frame loop, draw sets, picks, motion clocks, heights, leases, imagery, styles, credits and quality knobs. The stub passes all of them but the model check, which needs a glTF asset. Reference scenes draw one scene per primitive family, a city and a calibration path from orbit to the street, from contract calls only and with no network. /dev/conformance/ draws them on the Cesium scene renderer (src/render/cesium/sceneRenderer.js), records references through a dev-server-only, loopback-only endpoint, and compares captures with pixelmatch's antialias detection plus a solid-pixel count. Measured on Cesium: every MSAA setting passes, and a dropped 4 px point, a dropped 2 px line and a 1 px shift of the view fail. visual.js states the limits. The package exports ./render/contract, ./render/stub and ./render/conformance. Documented in docs/RENDERER-CONTRACT.md, docs/CODE-BOUNDARIES.md and docs/CURRENT-STATE.md.
211 lines
6.4 KiB
TypeScript
211 lines
6.4 KiB
TypeScript
/**
|
||
* The base world: photorealistic tiles or a globe with terrain and imagery,
|
||
* the sky, and what drapes onto them.
|
||
*/
|
||
import type { Credit } from './credits';
|
||
import type { GeoRect, Off } from './common';
|
||
|
||
export interface PhotorealSpec {
|
||
provider: 'google';
|
||
key?: string;
|
||
/** Renews a session token; called again when the token expires. */
|
||
token?: () => Promise<string>;
|
||
/** Fall back to an ion-hosted copy when Google refuses. */
|
||
ionFallback?: boolean;
|
||
}
|
||
|
||
export interface PhotorealStatus {
|
||
/** Which route is drawing, or `'none'` when photoreal is off or failed. */
|
||
route: 'google' | 'ion' | 'none';
|
||
error?: { code: string; message: string };
|
||
}
|
||
|
||
/**
|
||
* Heights everywhere in the contract are physical (metres above the
|
||
* ellipsoid, unexaggerated): absolute positions, `HeightSample.height`,
|
||
* `clampToSurface`. Only drawing follows the exaggerated surface:
|
||
* `clampGround` and `relativeGround` items sit on the ground as drawn.
|
||
*/
|
||
export type TerrainSpec = (
|
||
| { kind: 'ellipsoid' }
|
||
| { kind: 'quantizedMesh'; url: string; requestVertexNormals?: boolean }
|
||
| { kind: 'ion'; assetId: number }
|
||
/** A DEM image pyramid (`{z}/{x}/{y}`), heights encoded in RGB. */
|
||
| {
|
||
kind: 'heightmap';
|
||
url: string;
|
||
encoding: 'terrarium' | 'mapbox';
|
||
maxLevel?: number;
|
||
/** The geoid the encoded heights are relative to; omitted: the ellipsoid. */
|
||
geoid?: 'egm96' | 'egm2008';
|
||
}
|
||
) & {
|
||
/** Vertical exaggeration of the drawn terrain; default 1. */
|
||
exaggeration?: number;
|
||
};
|
||
|
||
export type TilingScheme = 'webMercator' | 'geographic';
|
||
|
||
/** One tile request, as an admission hook sees it. */
|
||
export interface TileRequest {
|
||
x: number;
|
||
y: number;
|
||
level: number;
|
||
}
|
||
|
||
export interface ImagerySpec {
|
||
kind: 'xyz' | 'wmts' | 'arcgis' | 'bing' | 'single-image' | 'canvas';
|
||
/** URL or URL template (`{x}`, `{y}`, `{z}`, `{s}`, `{time}`). */
|
||
url?: string;
|
||
/** `'canvas'`: draws one tile. */
|
||
drawTile?: (tile: TileRequest, canvas: HTMLCanvasElement) => void;
|
||
/** WMTS: layer, style, tile matrix set and format. */
|
||
wmts?: {
|
||
layer: string;
|
||
style: string;
|
||
tileMatrixSetId: string;
|
||
format?: string;
|
||
};
|
||
subdomains?: string[];
|
||
tiling?: TilingScheme;
|
||
rectangle?: GeoRect;
|
||
minLevel?: number;
|
||
maxLevel?: number;
|
||
tileSize?: number;
|
||
credit?: Credit;
|
||
/** Requests in flight at once for this layer. */
|
||
maxConcurrentRequests?: number;
|
||
/** HTTP statuses retried rather than treated as missing. */
|
||
retryStatuses?: number[];
|
||
/** Changes a tile's image before it is drawn. */
|
||
tileTransform?: (
|
||
tile: TileRequest,
|
||
image: ImageBitmap,
|
||
) => ImageBitmap | Promise<ImageBitmap>;
|
||
/**
|
||
* Decides each tile request: call `issue()` to fetch it, or return
|
||
* `undefined` to defer it and be asked again on a later frame.
|
||
*/
|
||
admit?: (
|
||
tile: TileRequest,
|
||
issue: () => Promise<unknown> | undefined,
|
||
) => Promise<unknown> | undefined;
|
||
/** Draw order among imagery layers; higher draws on top. */
|
||
zOrder?: number;
|
||
}
|
||
|
||
export interface TimedUrl {
|
||
/** Epoch ms the frame shows. */
|
||
time: number;
|
||
url: string;
|
||
}
|
||
|
||
export interface ImageryLayer {
|
||
alpha: number;
|
||
show: boolean;
|
||
/** Which side of the split this layer shows on. */
|
||
split: 'none' | 'left' | 'right';
|
||
/** What it drapes onto. `'active'` follows whichever base is showing. */
|
||
drapeOn: 'globe' | 'tiles' | 'both' | 'active';
|
||
/** `imageryFrames`: a timed sequence, prefetched hidden. */
|
||
setFrames?(
|
||
frames: TimedUrl[],
|
||
options?: { mode?: 'nearest' | 'blend'; prefetch?: number },
|
||
): void;
|
||
/** `imageryFrames`: which time to show. */
|
||
setFrameTime?(ms: number): void;
|
||
on(event: 'frameReady', cb: (time: number) => void): Off;
|
||
on(
|
||
event: 'tileError',
|
||
cb: (error: { tile: TileRequest; status?: number }) => void,
|
||
): Off;
|
||
raise(): void;
|
||
lower(): void;
|
||
remove(): void;
|
||
}
|
||
|
||
export interface BaseSpec {
|
||
photoreal?: PhotorealSpec | null;
|
||
/** `vectorBase`: a vector basemap from a style document. */
|
||
vector?: { styleUrl: string } | null;
|
||
terrain?: TerrainSpec | null;
|
||
imagery?: ImagerySpec[];
|
||
credit?: Credit;
|
||
}
|
||
|
||
export interface BaseStatus {
|
||
photoreal: PhotorealStatus;
|
||
imagery: ImageryLayer[];
|
||
settled: boolean;
|
||
}
|
||
|
||
/**
|
||
* The sky. A renderer whose sun, moon or fog are not separate from its
|
||
* atmosphere follows `atmosphere` for them and reports them as it draws them.
|
||
*/
|
||
export interface SkySettings {
|
||
atmosphere: boolean;
|
||
atmosphereTuning?: {
|
||
brightness?: number;
|
||
hueShift?: number;
|
||
saturation?: number;
|
||
};
|
||
stars: boolean;
|
||
/** Milky Way brightness, 0..1. */
|
||
milkyWay: number;
|
||
sun: boolean;
|
||
moon: boolean;
|
||
/** Distance fog, separate from atmosphere on models. */
|
||
fog: boolean;
|
||
/** Sun lighting of the globe and models. */
|
||
lighting: boolean;
|
||
}
|
||
|
||
export interface SurfaceShading {
|
||
kind: 'relief';
|
||
/** Elevation-like field to shade, row-major, `width × height`. */
|
||
values: Float32Array;
|
||
width: number;
|
||
height: number;
|
||
rectangle: GeoRect;
|
||
strength: number;
|
||
}
|
||
|
||
export interface LoadProgress {
|
||
tilesPending: number;
|
||
imageryPending: number;
|
||
terrainPending: number;
|
||
}
|
||
|
||
export type SettleKind = 'base' | 'tiles' | 'terrain' | 'imagery';
|
||
|
||
export interface GlobePort {
|
||
/** Switch the whole base at once; resolves when the new base has settled. */
|
||
setBase(base: BaseSpec): Promise<BaseStatus>;
|
||
/** `photoreal`: on, off or a new token route. */
|
||
setPhotoreal(spec: PhotorealSpec | null): Promise<PhotorealStatus>;
|
||
setTerrain(spec: TerrainSpec | null): Promise<void>;
|
||
/** Show or hide the globe surface (hidden under photoreal). */
|
||
setGlobeVisible(visible: boolean): void;
|
||
/** `surfaceShading`. */
|
||
setSurfaceShading?(shading: SurfaceShading | null): void;
|
||
readonly imagery: {
|
||
add(spec: ImagerySpec, index?: number): ImageryLayer;
|
||
list(): ImageryLayer[];
|
||
/** What imagery drapes onto now. */
|
||
host(): 'globe' | 'tiles' | 'both';
|
||
onHostChange(cb: (host: 'globe' | 'tiles' | 'both') => void): Off;
|
||
/** `splitSwipe`: split position, a fraction of the canvas's CSS width. */
|
||
getSplit?(): number;
|
||
/** `splitSwipe`: move the split to `x01` of the canvas's CSS width. */
|
||
setSplit?(x01: number): void;
|
||
};
|
||
readonly sky: {
|
||
get(): SkySettings;
|
||
set(settings: Partial<SkySettings>): void;
|
||
};
|
||
/** Whether that part of the base has finished loading what is in view. */
|
||
isSettled(kind: SettleKind): boolean;
|
||
on(event: 'progress', cb: (progress: LoadProgress) => void): Off;
|
||
}
|