Skip to content
Merged
2 changes: 1 addition & 1 deletion builder/web/src/lib/trailview/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ the palette of the `world` region.
| `kinds/` | One module per kind of `data/trailview/kinds.json`, found by its file name (`index.ts`), with the models that it stands on the surface; the shared handlers (`kind.ts`): a line structure on the surface, models at points. The instances draw the models of the kinds' points (`structures/kind-points.ts`). A kind's `near` meshes and points draw only within about 2 km, where a node of level 17 draws; a kind names its models, never their numbers |
| `ground/` | The ground pages, `look.wgsl` (the ground shading of the prototype), the road paint (`roads.wgsl`), the rows of the material fields (`fields.wgsl`), the rules packing and the per-page compute |
| `structures/` | The houses: a worker makes a record per building of each zoom 14 quarter of the zoom 13 feature tiles (`house-records.ts`); `houses.wgsl` fits each to the drawn surface with a basement, culls it, picks its level of detail and draws it from a template, and lights its windows at night |
| `models/` | The house templates: the prototype's buildings as faces whose coordinates name slots that `houses.wgsl` fills per house; the prototype's trees and plants without three.js (`shape.ts`, `trees.ts`, `trees/`, `plant-shape.ts`, `plants/`); the models of flat faces of the kinds and the dressing (`flat.ts`, lifts, landmarks, rocks, animals, crosses) |
| `models/` | The house templates: the prototype's buildings as faces whose coordinates name slots that `houses.wgsl` fills per house; the prototype's trees and plants without three.js (`shape.ts`, `trees.ts`, `trees/`, `plant-shape.ts`, `plants/`); the models of flat faces of the kinds and the dressing (`flat.ts`, lifts, landmarks, rocks, animals, crosses). The build writes the fixed models as files (`bake.ts`, [contract](../../../../../specs/trailview-models.md)) |
| `instances/` | Instances on the drawn surface: producers emit them (`emit.wgsl`), one indirect draw per model and tier (`sort.wgsl`, `draw.wgsl`), a tree's phase models and crown colours, impostors, culling by the last frame's depth (`hiz.wgsl`), point pools of kinds (`points.ts`) |
| `vegetation/` | Each tree's phase on the date (`phenology.ts`); the tree models of the phases, shared by the forest, the shrubs and the plantings (`tree-models.ts`); the canopy tiles about the eye (`canopy.ts`) and the forest placed from them each frame (`forest.wgsl`); the ground plants and shrubs about the eye (`plants.wgsl`); the clearing that moves with the eye (`clearing.wgsl`); the plants of the materials planted in rows, on the rows of the material fields (`rows.wgsl`, `ground/fields.wgsl`) |
| `dressing/` | The herds of the packs on the materials that they graze, and rocks on rough and rocky ground, placed each frame (`dressing.wgsl`); the rocks' density and placement, which the ground also paints past their instances (`rocks.wgsl`) |
Expand Down
8 changes: 4 additions & 4 deletions builder/web/src/lib/trailview/budget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ export interface Budget {
terrainShare: number;
/** The ground look's pages: whole layers of 16. */
groundPages: number;
/** Loads in flight at most: terrain tiles, ground tiles, house tiles, stamp jobs of the worker, canopy tiles. */
loads: { terrain: number; ground: number; houses: number; stamps: number; canopy: number };
/** Loads in flight at most: terrain tiles, ground tiles, house tiles, stamp jobs of the worker, canopy tiles, tree model files. */
loads: { terrain: number; ground: number; houses: number; stamps: number; canopy: number; models: number };
/**
* Byte caps in MiB. They are not hard limits: a decode in flight stays, and the stamp worker keeps the tiles that a
* view wants past its cap. Worker caps: the decoded images of each ground worker, the feature tiles of the stamp and
Expand All @@ -36,12 +36,12 @@ export const MiB = 2 ** 20;
*/
export const BUDGETS: Record<Tier, Budget> = {
desktop: {
frameRate: 60, terrainShare: 1, groundPages: 192, loads: { terrain: 6, ground: 4, houses: 6, stamps: 8, canopy: 9 },
frameRate: 60, terrainShare: 1, groundPages: 192, loads: { terrain: 6, ground: 4, houses: 6, stamps: 8, canopy: 9, models: 6 },
caps: { groundDecode: 96, stamp: 48, house: 32, planHeap: 55, nodes: 64, debugRecords: 32, terrainBodies: 64, featureBodies: 32, groundBodies: 16 },
planWorkers: 2,
},
phone: {
frameRate: 30, terrainShare: 0.5, groundPages: 64, loads: { terrain: 3, ground: 2, houses: 3, stamps: 4, canopy: 5 },
frameRate: 30, terrainShare: 0.5, groundPages: 64, loads: { terrain: 3, ground: 2, houses: 3, stamps: 4, canopy: 5, models: 3 },
caps: { groundDecode: 16, stamp: 24, house: 16, planHeap: 40, nodes: 24, debugRecords: 16, terrainBodies: 16, featureBodies: 8, groundBodies: 2 },
planWorkers: 1,
},
Expand Down
47 changes: 33 additions & 14 deletions builder/web/src/lib/trailview/dressing/dressing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ import { anchorAt, UNITS } from '../frame/space';
import type { Gpu } from '../gpu/device';
import { layouts } from '../gpu/layouts';
import { wgsl } from '../gpu/wgsl';
import { Instances, VIEWS, type Producer, type Surfaces } from '../instances/instances';
import { animal, BREEDS } from '../models/animals';
import { geometry, type Model } from '../models/flat';
import { rock } from '../models/rocks';
import { Instances, VIEWS, type ModelSpec, type Producer, type Surfaces } from '../instances/instances';
import type { ModelFiles } from '../instances/model-files';
import type { Packed } from '../models/model-file';
import { bits, named, type Rules } from '../rules/regions';

/** Herds stand within this many metres, rocks within this many; as HERDS and SLABS of dressing.wgsl and ROCK_REACH_M of rocks.wgsl. */
const HERD_REACH_M = 1500, ROCK_REACH_M = 1000, HERDS = 4, SLABS = 3;
/** Herds stand within this many metres, rocks within this many; as HERDS of dressing.wgsl and ROCK_REACH_M of rocks.wgsl. */
const HERD_REACH_M = 1500, ROCK_REACH_M = 1000, HERDS = 4;
/** Units along a herd cell and a rock point's cell, as HERD_SHIFT of dressing.wgsl and ROCK_SHIFT of rocks.wgsl. */
const HERD_CELL = 2 ** 14, ROCK_CELL = 2 ** 7;
/** Words of `Herd` and bytes of `Dressing` in dressing.wgsl. */
Expand All @@ -20,37 +19,57 @@ interface Resources { gpu: Gpu; herds: GPUComputePipeline; rocks: GPUComputePipe

/**
* The dressing of the ground (dressing.wgsl): the herds of the regions' packs on the materials that
* they graze, and rocks on rough and rocky ground, placed on the GPU each frame.
* they graze, and rocks on rough and rocky ground, placed on the GPU each frame. Their models come from
* model files (models/model-file.ts): the rocks from the objects file, each breed from a file of its own.
*/
export class Dressing implements Producer {
private resources?: Resources;
private readonly table: Uint32Array<ArrayBuffer>;
private readonly rockModel?: number;
private herdSide = 0;
private rockSide = 0;
/** The files of the models. */
private readonly urls: string[] = [];

constructor(rules: Rules, private readonly instances: Instances) {
constructor(rules: Rules, private readonly instances: Instances, private readonly files: ModelFiles) {
instances.produce(this);
const spec = (model: Model, until: number) => ({ tiers: [{ mesh: geometry(model), until }] });
const { objects, breeds: breedModels } = files.models;
const spec = ({ tiers: [mesh], shape }: Packed, until: number): ModelSpec => ({ tiers: [{ mesh, until }], shape });
// No fade of the rocks' tier: rocks_of ends each rock at its own distance before the reach.
this.rockModel = instances.add(...(['slab', 'boulder'] as const).flatMap(name => Array.from({ length: SLABS }, (_, k) => ({ ...spec(rock(name, k + 1), ROCK_REACH_M), fade: 0 }))));
const breeds = new Map<string, number>(), used = new Set(rules.regions.flatMap(region => region.herds.map(herd => herd.model)).filter(name => name in BREEDS));
this.rockModel = this.load(objects.file, objects.rocks, model => ({ ...spec(model, ROCK_REACH_M), fade: 0 }));
const breeds = new Map<string, number>(), used = new Set(rules.regions.flatMap(region => region.herds.map(herd => herd.model)).filter(name => name in breedModels));
for (const name of used) {
const first = instances.add(...BREEDS[name].coats.map((_, coat) => spec(animal(name, coat), HERD_REACH_M)));
const first = this.load(breedModels[name].file, breedModels[name].coats, model => spec(model, HERD_REACH_M));
if (first !== undefined) breeds.set(name, first);
}
this.table = new Uint32Array(Math.max(1, rules.regions.length) * HERDS * HERD_WORDS);
const floats = new Float32Array(this.table.buffer);
rules.regions.forEach((region, r) => {
region.herds.filter(herd => breeds.has(herd.model)).slice(0, HERDS).forEach((herd, k) => {
const at = (r * HERDS + k) * HERD_WORDS;
this.table.set([breeds.get(herd.model)!, BREEDS[herd.model].coats.length], at);
floats.set([herd.groups, BREEDS[herd.model].height, ...herd.size], at + 2);
this.table.set([breeds.get(herd.model)!, breedModels[herd.model].coats], at);
floats.set([herd.groups, breedModels[herd.model].height, ...herd.size], at + 2);
this.table.set(bits(named(rules.vocabulary, herd.on)), at + 6);
});
});
}

/** The first of `count` models of the instances, which take the first `count` models of a file once it arrives. */
private load(file: string, count: number, spec: (model: Packed) => ModelSpec): number | undefined {
const first = this.instances.add(...Array.from({ length: count }, () => ({ tiers: [] })));
this.urls.push(file);
void this.files.load(file).then(models => {
if (first === undefined || !models) return;
for (let k = 0; k < count; k++) this.instances.replace(first + k, spec(models[k]));
});
return first;
}

/** Whether the models have arrived, or failed. */
get idle(): boolean {
return this.files.done(...this.urls);
}

async attach(gpu: Gpu, { surface, features }: Surfaces): Promise<void> {
this.resources = undefined;
const { device } = gpu;
Expand Down
28 changes: 16 additions & 12 deletions builder/web/src/lib/trailview/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@ import { Resolve, resolvePipeline } from './resolve/resolve';
import { MAX_SCALE, Targets } from './resolve/targets';
import { jitter, STILL_FRAMES, Temporal, type Step } from './resolve/temporal';
import { EYE, Instances, SUN, type InstanceStats, type LastFrame } from './instances/instances';
import { ModelFiles } from './instances/model-files';
import { GroundSource } from './stream/ground';
import { eyeTiles, pageDecoder, type EyeTiles } from './stream/terrain';
import { Terrain, terrainPipelines, type Audit, type TerrainStats } from './terrain/terrain';
import { piecePipelines } from './plan/pieces';
import HOUSE_VERSION from 'virtual:house-version';
import PLAN_VERSION from 'virtual:plan-version';
import STAMP_VERSION from 'virtual:stamp-version';
import TREE_VERSION from 'virtual:tree-version';
import MODELS from 'virtual:models';
import { FINEST, NEAR_M } from './terrain/lod';
import { Houses, type HouseStats } from './structures/houses';
import { Forest } from './vegetation/forest';
Expand Down Expand Up @@ -129,6 +130,7 @@ export class TrailView {
private readonly groundLook: Ground;
private readonly houses: Houses;
private readonly instances = new Instances();
private readonly models: ModelFiles;
private readonly trees: TreeModels;
/** The last regions in reach, where they were read from and at which change of the ground pages. */
private reached?: Reach & { east: number; north: number; changes: number };
Expand Down Expand Up @@ -203,12 +205,13 @@ export class TrailView {
this.terrain = new Terrain(this.frame, { fetch: tiles.terrain, decode: pageDecoder(signal) }, features, config.bounds, () => this.request(), signal, budget, groundPlan, finest, tiles.stamps, { release, version: STAMP_VERSION, plan: PLAN_VERSION });
this.groundLook = new Ground(ground, budget, () => this.request());
this.houses = new Houses(this.frame, features, this.groundLook.rules, () => this.request(), signal, budget, { release, version: HOUSE_VERSION });
this.trees = new TreeModels(this.instances, () => this.request(), () => this.treesNear(), signal, TREE_VERSION);
this.models = new ModelFiles(MODELS, () => this.request(), signal);
this.trees = new TreeModels(this.instances, () => this.treesNear(), this.models, budget.loads.models);
this.forest = new Forest(config, this.groundLook.rules, this.trees, this.instances, () => this.request(), signal, budget);
this.plants = new Plants(this.groundLook.rules, this.trees, this.instances, () => this.request(), signal);
this.rows = new Rows(config, this.groundLook.rules, this.trees, this.plants, this.instances);
this.kindPoints = new KindPoints(this.instances);
this.dressing = new Dressing(this.groundLook.rules, this.instances);
this.plants = new Plants(this.groundLook.rules, this.trees, this.instances, this.models);
this.rows = new Rows(config, this.groundLook.rules, this.trees, this.plants, this.instances, MODELS);
this.kindPoints = new KindPoints(this.instances, this.models);
this.dressing = new Dressing(this.groundLook.rules, this.instances, this.models);
// A still has a size of its own and no input: the controls listen for keys on the whole window.
if (output) return;
this.controls = new Controls(canvas, {
Expand Down Expand Up @@ -453,7 +456,7 @@ export class TrailView {

/** Whether the late passes still attach, or a tile, a model or a page is on its way. */
private get loading(): boolean {
return !this.complete || !this.terrain.idle || !this.groundLook.idle || !this.houses.idle || !this.trees.idle || !this.forest.idle || !this.plants.idle || !this.instances.idle
return !this.complete || !this.terrain.idle || !this.groundLook.idle || !this.houses.idle || !this.trees.idle || !this.forest.idle || !this.plants.idle || !this.models.idle || !this.instances.idle
|| !this.sky.idle;
}

Expand Down Expand Up @@ -501,7 +504,7 @@ export class TrailView {

private near(): boolean {
return this.drawing && this.complete && this.terrain.near && this.settled() && this.groundLook.nearIdle && this.houses.nearIdle && this.trees.nearIdle && this.forest.idle
&& this.plants.nearIdle && this.instances.idle && this.shadow.nearDrawn;
&& this.plants.nearIdle && this.kindPoints.idle && this.dressing.idle && this.instances.idle && this.shadow.nearDrawn;
}

/**
Expand Down Expand Up @@ -697,6 +700,7 @@ export class TrailView {
// The shipping render scale selects the nodes, also for a debug view: it shows the LOD that ships.
if (this.terrain.plan) this.terrain.plan.debug = this.extras.plan === 'faces';
this.terrain.update({ lonLat, above: place.above, z: eye?.z, viewProj, pixels: targets.viewport(this.scale)[1], ahead: ahead(this.pose) }, this.groundLook.reads);
this.models.update();
this.kindPoints.update(this.terrain.kindPoints);
this.lightPools.set(this.kindPoints.lights);
const { drawn, wanted, arrived, origins } = this.terrain.selected;
Expand All @@ -721,7 +725,7 @@ export class TrailView {
const camera = [...viewProj, eye.east, eye.north, eye.z, eye.above, scale], extras = JSON.stringify([this.extras, this.skyMagnitude]);
moved = camera.some((value, i) => value !== this.camera[i]);
const newExtras = extras !== this.drawnExtras;
changed = this.completed || this.terrain.newContent || this.groundLook.newContent || this.houses.newContent || this.trees.newContent || this.forest.newContent || this.plants.newContent || this.instances.newContent || this.sky.newContent || this.lightPools.newContent || this.route.newContent || newExtras;
changed = this.completed || this.terrain.newContent || this.groundLook.newContent || this.houses.newContent || this.trees.newContent || this.forest.newContent || this.models.newContent || this.instances.newContent || this.sky.newContent || this.lightPools.newContent || this.route.newContent || newExtras;
// While a view at rest loads, its frames run the updates and the compute but draw LOAD_RATE frames a second. Input and a resize draw
// at once; a frame that would draw nothing does not hold, so it asks for no more frames.
hold = !this.output && !this.grab && (changed || this.temporal.converging) && this.pacer.holds(now, !moved && !newExtras && !resized, this.loading || this.terrain.meshesLoading);
Expand All @@ -732,7 +736,7 @@ export class TrailView {
this.drawnExtras = extras;
this.completed = false;
lights = this.lights();
const light = lights.key, casters = this.houses.newContent || this.trees.newContent || this.forest.newContent || this.plants.newContent;
const light = lights.key, casters = this.houses.newContent || this.trees.newContent || this.forest.newContent || this.models.newContent;
const casting = { casters, reshaped: this.terrain.reshaped };
// A held frame keeps its changes for the next frame that draws: `due()` marks the map that it returns as drawn.
if (this.complete && hold) this.shadow.hold(casting);
Expand Down Expand Up @@ -818,8 +822,8 @@ export class TrailView {
cast();
this.cpu = performance.now() - start;
this.drawn = eye ? this.terrain.drawn : 0;
// The tree models that the workers built go to the device cache once the view is idle, so a load never waits for the writes.
if (this.trees.unsaved && this.idle()) this.trees.save();
// The model files from the network go to the device cache once the view is idle, so a load never waits for the writes.
if (this.models.unsaved && this.idle()) this.models.save();
// Frames go on while the eye moves or settles on the ground, while the terrain or the ground has
// work that no load waits for, and until a still view has all its jittered frames, which a held frame's change restarts.
if (steer || !settled || this.terrain.working || this.groundLook.working || this.houses.working || this.shadow.pending || this.converging) this.request();
Expand Down
Loading
Loading