diff --git a/builder/web/src/lib/trailview/README.md b/builder/web/src/lib/trailview/README.md index 446070e8f0..71d80549fd 100644 --- a/builder/web/src/lib/trailview/README.md +++ b/builder/web/src/lib/trailview/README.md @@ -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`) | diff --git a/builder/web/src/lib/trailview/budget.ts b/builder/web/src/lib/trailview/budget.ts index 9b8052c495..b8e490e065 100644 --- a/builder/web/src/lib/trailview/budget.ts +++ b/builder/web/src/lib/trailview/budget.ts @@ -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 @@ -36,12 +36,12 @@ export const MiB = 2 ** 20; */ export const BUDGETS: Record = { 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, }, diff --git a/builder/web/src/lib/trailview/dressing/dressing.ts b/builder/web/src/lib/trailview/dressing/dressing.ts index f88ca6e8a1..a6e75437bd 100644 --- a/builder/web/src/lib/trailview/dressing/dressing.ts +++ b/builder/web/src/lib/trailview/dressing/dressing.ts @@ -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. */ @@ -20,7 +19,8 @@ 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; @@ -28,15 +28,18 @@ export class Dressing implements Producer { 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(), 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(), 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); @@ -44,13 +47,29 @@ export class Dressing implements Producer { 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 { this.resources = undefined; const { device } = gpu; diff --git a/builder/web/src/lib/trailview/engine.ts b/builder/web/src/lib/trailview/engine.ts index b71b883f77..1ca8068405 100644 --- a/builder/web/src/lib/trailview/engine.ts +++ b/builder/web/src/lib/trailview/engine.ts @@ -20,6 +20,7 @@ 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'; @@ -27,7 +28,7 @@ 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'; @@ -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 }; @@ -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, { @@ -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; } @@ -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; } /** @@ -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; @@ -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); @@ -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); @@ -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(); diff --git a/builder/web/src/lib/trailview/instances/instances.ts b/builder/web/src/lib/trailview/instances/instances.ts index 7cb57c6835..4fb957a8a8 100644 --- a/builder/web/src/lib/trailview/instances/instances.ts +++ b/builder/web/src/lib/trailview/instances/instances.ts @@ -2,6 +2,7 @@ import type { Gpu } from '../gpu/device'; import { layouts } from '../gpu/layouts'; import { wgsl } from '../gpu/wgsl'; import { CASTER_DEPTH, CASTER_PRIMITIVE } from '../light/shadow'; +import { quantize, VERTEX_BYTES, type Mesh, type Shape } from '../models/model-file'; import { DEPTH, sceneFormat } from '../resolve/targets'; /** Draws per model: up to three mesh tiers and the impostor tier, as TIERS in emit.wgsl. */ @@ -15,8 +16,8 @@ const DRAWS = MODELS * TIERS; * 70 500 on the kit's flights (M1 Pro, measured), the near map's up to 3 100. */ const CAPACITY = 2 ** 17, SUN_CAPACITY = 2 ** 17; -/** Bytes of `Instance`, `Model`, `Tally`, `Mesh` and `Params` in the WGSL, and of a vertex. */ -const INSTANCE_BYTES = 32, MODEL_BYTES = 80, TALLY_BYTES = 16 + 4 * DRAWS, MESH_BYTES = 16, PARAMS_BYTES = 32, VERTEX_BYTES = 16; +/** Bytes of `Instance`, `Model`, `Tally`, `Mesh` and `Params` in the WGSL. */ +const INSTANCE_BYTES = 32, MODEL_BYTES = 80, TALLY_BYTES = 16 + 4 * DRAWS, MESH_BYTES = 16, PARAMS_BYTES = 32; /** The phases of a tree's year, as PHASES of phenology.wgsl, and the bytes of a colour matrix (`mat3x3f`). */ const PHASES = 4, TONE_BYTES = 48; /** Lanes of the sort, as LANES in sort.wgsl. */ @@ -30,8 +31,6 @@ const VERTEX_POOL = 6 * 2 ** 20, INDEX_POOL = 6 * 2 ** 20; * draws on a share of its pixels stages in a farther tier. */ const WHOLE_TIERS = 2; -/** A tree's sink table (`sinks`): its slopes, SINK_STEP apart from SINK_STEP, as in emit.wgsl. */ -const SINK_STEP = 0.25, SINK_SLOPES = 8; /** The render height in pixels that the tier distances are for. */ const REFERENCE_PIXELS = 1120; /** The share of the capacity that the detail keeps to. */ @@ -45,28 +44,24 @@ const SLOTS = 32, ATLAS = 256, ATLAS_LEVELS = 6, FRAMES = 64, UNUSED_FRAMES = 60 /** Texels a side of level 0 of the depth pyramid, its levels, and the bytes of `Occlusion` in emit.wgsl. */ const PYRAMID = 512, PYRAMID_LEVELS = 10, OCCLUSION_BYTES = 96; /** The quad of an impostor: corners at ±1, and a middle row at y 0 that impostor.wgsl places at the crown's base. */ -const QUAD = { position: [-1, -1, 0, 1, -1, 0, -1, 0, 0, 1, 0, 0, -1, 1, 0, 1, 1, 0], normal: new Array(6).fill([0, 0, 1]).flat(), color: new Array(18).fill(1), index: [0, 1, 2, 2, 1, 3, 2, 3, 4, 4, 3, 5] }; +const QUAD: Mesh = { + vertices: quantize({ position: [-1, -1, 0, 1, -1, 0, -1, 0, 0, 1, 0, 0, -1, 1, 0, 1, 1, 0], normal: new Array(6).fill([0, 0, 1]).flat(), color: new Array(18).fill(1), index: [] }, 1), + index: Uint32Array.from([0, 1, 2, 2, 1, 3, 2, 3, 4, 4, 3, 5]), +}; /** - * A mesh: per vertex a position, a normal and a linear colour, in a model 1 tall; three indices per - * triangle. A tree's first `trunk` vertices are its trunk, which stretches to meet its crown when the - * crown lifts (draw.wgsl). - */ -export interface Mesh { position: ArrayLike; normal: ArrayLike; color: ArrayLike; index: ArrayLike; trunk?: number; glow?: ArrayLike; rise?: ArrayLike } - -/** - * A model: its mesh per tier, finest first, and the distance in metres where each tier ends; then, - * optionally, an impostor baked from mesh tier `from` until its own distance. + * A model: its mesh per tier from a model file, finest first, and the distance in metres where each tier ends; its shape, + * which the build measured, where it has tiers; then, optionally, an impostor baked from its last mesh tier until its own + * distance. */ export interface ModelSpec { tiers: { mesh: Mesh; until: number }[]; + shape?: Shape; /** False for a model that casts no shadow. */ casts?: boolean; - impostor?: { from: number; until: number }; + impostor?: { until: number }; /** The share of a tier's end distance over which its instances switch to the next tier. */ fade?: number; - /** A tree's crown: its lowest point in model units (draw.wgsl). */ - crown?: number; /** How far it takes its hue from the ground under it, 0 to 1 (draw.wgsl). */ tie?: number; /** The height in metres of an instance whose tiers end at `until`; the tiers of others end in proportion. */ @@ -205,8 +200,6 @@ export class Instances { /** Models whose layer needs a bake. */ private readonly unbaked = new Set(); private readonly producers: Producer[] = []; - /** The impostor outlines of each mesh and radius (`outline`). */ - private readonly outlined = new WeakMap }>(); /** What `attach` took, for a producer that comes later. */ private attached?: [Gpu, Surfaces]; /** Whether the last frame baked an impostor: what the frames draw changed. */ @@ -417,12 +410,12 @@ export class Instances { if (!r || !this.uploaded) return; for (const model of this.unbaked) { const slot = this.atlas.get(model)?.slot, spec = this.specs[model], placed = this.placed[model]; - if (slot === undefined || !spec?.impostor || !placed.length) continue; - const [first, count, base] = placed[spec.impostor.from]; - r.gpu.device.queue.writeBuffer(r.outlines, model * FRAMES * 16, this.outline(spec.tiers[spec.impostor.from].mesh, radius(spec))); + if (slot === undefined || !spec?.impostor || !spec.shape || !placed.length) continue; + const [first, count, base] = placed[placed.length - 1]; + r.gpu.device.queue.writeBuffer(r.outlines, model * FRAMES * 16, spec.shape.outlines); const data = new ArrayBuffer(16); new Uint32Array(data).set([model, 0, slot]); - new Float32Array(data)[1] = radius(spec); + new Float32Array(data)[1] = spec.shape.radius; r.gpu.device.queue.writeBuffer(r.bakes, slot * STRIDE, data); const layer = (texture: GPUTexture) => texture.createView({ dimension: '2d', baseArrayLayer: slot, arrayLayerCount: 1, mipLevelCount: 1 }); const pass = encoder.beginRenderPass({ @@ -573,15 +566,6 @@ export class Instances { r.gpu.device.queue.writeBuffer(r.slots, 0, slots); } - /** The outlines of a mesh's impostor (`outlines`), once per mesh and radius. */ - private outline(mesh: Mesh, radius: number): Float32Array { - const known = this.outlined.get(mesh); - if (known?.radius === radius) return known.outlines; - const made = outlines(mesh, radius); - this.outlined.set(mesh, { radius, outlines: made }); - return made; - } - /** * Writes the models into the pools. A mesh stays where an earlier upload wrote it, so an upload writes only * the meshes that are new; a model that finds no room packs the pools again from empty, without the meshes @@ -590,42 +574,40 @@ export class Instances { private upload(r: Resources, packed = false): void { const models = new ArrayBuffer(MODELS * MODEL_BYTES), f = new Float32Array(models); const meshes = new Int32Array(DRAWS * MESH_BYTES / 4), pool = this.pool; - const fits = (tiers: Mesh[]) => { - const count = tiers.reduce((sum, mesh) => sum + mesh.position.length / 3, 0), indices = tiers.reduce((sum, mesh) => sum + mesh.index.length, 0); + // The meshes that an upload writes: those that the pools do not hold. + const room = (tiers: Mesh[]) => { + const fresh = tiers.filter(mesh => !pool.at.has(mesh)); + const count = fresh.reduce((sum, mesh) => sum + mesh.vertices.length / VERTEX_BYTES, 0), indices = fresh.reduce((sum, mesh) => sum + mesh.index.length, 0); return (pool.vertex + count) * VERTEX_BYTES <= VERTEX_POOL && (pool.index + indices) * 4 <= INDEX_POOL; }; // A mesh that models share, such as a tree as a tree and as a shrub, lies once in the pools. - const put = (mesh: Mesh, extent: number): [number, number, number] => { + const put = (mesh: Mesh): [number, number, number] => { const known = pool.at.get(mesh); - if (known?.extent === extent) return known.at; - const count = mesh.position.length / 3; - r.gpu.device.queue.writeBuffer(r.vertices, pool.vertex * VERTEX_BYTES, quantize(mesh, extent)); - r.gpu.device.queue.writeBuffer(r.indices, pool.index * 4, Uint32Array.from(mesh.index)); + if (known) return known; + r.gpu.device.queue.writeBuffer(r.vertices, pool.vertex * VERTEX_BYTES, mesh.vertices); + r.gpu.device.queue.writeBuffer(r.indices, pool.index * 4, mesh.index); const at: [number, number, number] = [pool.index, mesh.index.length, pool.vertex]; - pool.vertex += count; + pool.vertex += mesh.vertices.length / VERTEX_BYTES; pool.index += mesh.index.length; - pool.at.set(mesh, { extent, at }); + pool.at.set(mesh, at); return at; }; - this.quad = put(QUAD, 1); + this.quad = put(QUAD); const dropped: number[] = []; - // The meshes that an upload writes: those that the pools do not hold at this extent. - const room = (meshes: Mesh[], extent: number) => fits(meshes.filter(mesh => pool.at.get(mesh)?.extent !== extent)); this.placed = this.specs.map((spec, m) => { - const { tiers } = spec; - if (!tiers.length) return []; - const extent = Math.max(...tiers.map(({ mesh }) => scan(mesh).largest)); - if (!room(tiers.map(({ mesh }) => mesh), extent)) return void dropped.push(m), []; + const { tiers, shape } = spec; + if (!tiers.length || !shape) return []; + if (!room(tiers.map(({ mesh }) => mesh))) return void dropped.push(m), []; const placed = tiers.map(({ mesh, until }, t) => { - const at = put(mesh, extent); + const at = put(mesh); meshes.set([at[1], at[0], at[2], 0], (m * TIERS + t) * 4); f[m * 20 + t] = until; return at; }); if (spec.impostor) f[m * 20 + tiers.length] = spec.impostor.until; - f.set([extent, radius(spec), spec.fade ?? 0.15, spread(spec), spec.crown ?? -1, tiers.some(({ mesh }) => mesh.trunk) ? 1 : 0, spec.tie ?? 0, spec.sized ?? 0, ...sinks(spec)], m * 20 + 4); + f.set([shape.extent, shape.radius, spec.fade ?? 0.15, shape.spread, shape.crown ?? -1, shape.trunk ? 1 : 0, spec.tie ?? 0, spec.sized ?? 0, ...shape.sinks], m * 20 + 4); if (spec.impostor) { - const mesh = tiers[spec.impostor.from].mesh, record = f.slice(m * 20, m * 20 + 20), last = this.baked.get(m); + const mesh = tiers[tiers.length - 1].mesh, record = f.slice(m * 20, m * 20 + 20), last = this.baked.get(m); if (last?.mesh !== mesh || !last.record.every((value, i) => Object.is(value, record[i]))) { this.baked.set(m, { mesh, record }); if (this.atlas.has(m)) this.unbaked.add(m); @@ -661,9 +643,9 @@ export class Instances { const coarsest = this.placed[m].at(-1); if (!coarsest) return; if (spec.impostor) coarse.set([coarsest[1], coarsest[0], coarsest[2], 0], (m * TIERS + spec.tiers.length) * 4); - const hull = scan(spec.tiers.at(-1)!.mesh).proxy, fitted = hull && room([hull], f[m * 20 + 4]); + const hull = spec.shape?.proxy, fitted = hull && room([hull]); hullless ||= !!hull && !fitted; - const at = fitted ? put(hull, f[m * 20 + 4]) : coarsest; + const at = fitted ? put(hull) : coarsest; for (let t = 0; t < TIERS; t++) proxies.set([at[1], at[0], at[2], 0], (m * TIERS + t) * 4); }); // A shadow proxy without room packs the pools once too, as a model without room does. @@ -767,158 +749,6 @@ function attachProducer(producer: Producer, ...args: Parameters console.error('A producer of instances failed to attach:', error)); } -/** - * The proxy of a crown in the far shadow map: a diamond of 8 faces about its crown's vertices, from its - * lowest to its highest point and widest where its widest vertex lies. A mesh whose faces cover less - * than half of the diamond's outline seen from above, such as a bare crown's branches, has none. - */ -export function proxy(mesh: Mesh): Mesh | undefined { - const p = mesh.position, from = mesh.trunk ?? 0, count = p.length / 3; - if (count <= from) return undefined; - let [low, high, middle, widest] = [Infinity, -Infinity, 0, 0]; - const side = [0, 0, 0, 0]; - for (let k = from; k < count; k++) { - const [x, y, z] = [p[3 * k], p[3 * k + 1], p[3 * k + 2]]; - [low, high] = [Math.min(low, z), Math.max(high, z)]; - side[0] = Math.max(side[0], x), side[1] = Math.max(side[1], y), side[2] = Math.max(side[2], -x), side[3] = Math.max(side[3], -y); - if (Math.hypot(x, y) > widest) [widest, middle] = [Math.hypot(x, y), z]; - } - let cover = 0; - for (let i = 0; i < mesh.index.length; i += 3) { - const [a, b, c] = [mesh.index[i], mesh.index[i + 1], mesh.index[i + 2]].map(v => 3 * v); - if (Math.min(a, b, c) < 3 * from) continue; - cover += Math.abs((p[b] - p[a]) * (p[c + 1] - p[a + 1]) - (p[c] - p[a]) * (p[b + 1] - p[a + 1])) / 2; - } - if (cover < (side[0] + side[2]) * (side[1] + side[3]) / 4) return undefined; - const position = [0, 0, high, 0, 0, low, side[0], 0, middle, 0, side[1], middle, -side[2], 0, middle, 0, -side[3], middle]; - return { position, normal: new Array(18).fill(0), color: new Array(18).fill(1), index: [0, 2, 3, 0, 3, 4, 0, 4, 5, 0, 5, 2, 1, 3, 2, 1, 4, 3, 1, 5, 4, 1, 2, 5] }; -} - -/** The largest absolute value. */ -function largest(values: ArrayLike): number { - let most = 0; - for (let i = 0; i < values.length; i++) most = Math.max(most, Math.abs(values[i])); - return most; -} - -/** The radius of a sphere about (0, 0, 0.5) that holds every mesh of a model, in model units; a sized plant may stand taller than 1. */ -function radius(spec: ModelSpec): number { - return Math.max(0, ...spec.tiers.map(({ mesh }) => scan(mesh).radius)); -} /** A pool keyed weakly: a mesh that no model draws any more does not stay in memory for the pool's sake. */ -const emptyPool = () => ({ at: new WeakMap(), vertex: 0, index: 0 }); - -/** What an upload reads of a mesh, made once per mesh: a mesh does not change once made. */ -const scans = new WeakMap(); - -function scan(mesh: Mesh): { largest: number; radius: number; spread: number; proxy?: Mesh } { - let known = scans.get(mesh); - if (!known) { - const p = mesh.position; - let most = 0, spread = 0; - for (let i = 0; i < p.length; i += 3) { - most = Math.max(most, Math.hypot(p[i], p[i + 1], p[i + 2] - 0.5)); - spread = Math.max(spread, Math.hypot(p[i], p[i + 1])); - } - scans.set(mesh, (known = { largest: largest(p), radius: most, spread, proxy: proxy(mesh) })); - } - return known; -} - -/** - * Per frame of a model's impostor (8 × 8, FRAMES of hemi.wgsl), the trapezoid of the frame's square - * (−1 to 1 over the sphere of `radius` about (0, 0, 0.5)) that holds the mesh seen along the frame: its - * bottom and top, and its half widths there, the least in area. - */ -export function outlines(mesh: Mesh, radius: number): Float32Array { - const BANDS = 32, p = mesh.position, n = p.length / 3, out = new Float32Array(FRAMES * 4); - const xs = new Float64Array(n), ys = new Float64Array(n); - for (let frame = 0; frame < FRAMES; frame++) { - // hemi_decode and view_axes of hemi.wgsl. - const ex = (frame % 8) / 3.5 - 1, ey = Math.floor(frame / 8) / 3.5 - 1, px = (ex + ey) / 2, py = (ex - ey) / 2; - const pz = 1 - Math.abs(px) - Math.abs(py), norm = Math.hypot(px, py, pz), d = [px / norm, py / norm, pz / norm]; - const flat = Math.hypot(d[0], d[1]), right = Math.abs(d[2]) < 0.999 ? [-d[1] / flat, d[0] / flat, 0] : [1, 0, 0]; - const up = [d[1] * right[2] - d[2] * right[1], d[2] * right[0] - d[0] * right[2], d[0] * right[1] - d[1] * right[0]]; - let low = Infinity, high = -Infinity; - for (let k = 0; k < n; k++) { - const x = p[3 * k], y = p[3 * k + 1], z = p[3 * k + 2] - 0.5; - xs[k] = Math.abs(x * right[0] + y * right[1] + z * right[2]) / radius; - ys[k] = (x * up[0] + y * up[1] + z * up[2]) / radius; - low = Math.min(low, ys[k]); - high = Math.max(high, ys[k]); - } - // The widest |x| in each of BANDS bands of height, which the trapezoid holds at both ends of the band. - const span = Math.max(high - low, 1e-6), widest = new Float64Array(BANDS).fill(-1); - for (let k = 0; k < n; k++) { - const band = Math.min(BANDS - 1, Math.floor(((ys[k] - low) / span) * BANDS)); - widest[band] = Math.max(widest[band], xs[k]); - } - // The least w0 + w1 where w0 (1 − t) + w1 t ≥ the widest at both ends t of every band: convex in w1. - const bottom = (top: number) => { - let w = 0; - for (let band = 0; band < BANDS; band++) { - const x = widest[band]; - if (x < 0) continue; - // At the band's foot; its top binds harder unless it is the trapezoid's top. - w = Math.max(w, (x - (top * band) / BANDS) / (1 - band / BANDS)); - if (band < BANDS - 1) w = Math.max(w, (x - (top * (band + 1)) / BANDS) / (1 - (band + 1) / BANDS)); - } - return w; - }; - // The top band's top needs w1 at least its widest. - let a = widest[BANDS - 1], b = Math.max(...widest); - for (let step = 0; step < 24; step++) { - const m1 = a + (b - a) / 3, m2 = b - (b - a) / 3; - if (m1 + bottom(m1) <= m2 + bottom(m2)) b = m2; - else a = m1; - } - out.set([low, high, bottom(b) + 1e-4, b + 1e-4], 4 * frame); - } - return out; -} - -/** The farthest that a vertex of a model lies from its axis. */ -function spread(spec: ModelSpec): number { - return Math.max(0, ...spec.tiers.map(({ mesh }) => scan(mesh).spread)); -} - -/** - * A tree's sink table (slope_sink of emit.wgsl): per slope SINK_STEP × (k + 1), the most that the drop - * of the ground under a crown vertex, the slope × its reach, exceeds its height above the crown's base; - * a vertex below the base counts as at it. Over the vertices of every tier but their trunks'. - */ -export function sinks(spec: ModelSpec): number[] { - const out = new Array(SINK_SLOPES).fill(0), crown = spec.crown; - if (crown === undefined) return out; - for (const { mesh: { position: p, trunk = 0 } } of spec.tiers) - for (let i = 3 * trunk; i < p.length; i += 3) { - const reach = Math.hypot(p[i], p[i + 1]), above = Math.max(0, p[i + 2] - crown); - for (let k = 0; k < SINK_SLOPES; k++) out[k] = Math.max(out[k], SINK_STEP * (k + 1) * reach - above); - } - return out; -} - -/** - * The vertices of a mesh as the instance pipeline reads them, 16 bytes each: the position over - * `extent` as snorm16 × 3 and, for a vertex of the trunk, the trunk's top in model units (else 0); the - * normal as snorm8 × 3 and whether it rises at dusk; and the colour in sRGB as unorm8 × 3 with 1 − its glow at night. - */ -function quantize(mesh: Mesh, extent: number): ArrayBuffer { - const count = mesh.position.length / 3, out = new ArrayBuffer(count * VERTEX_BYTES); - const i16 = new Int16Array(out), i8 = new Int8Array(out), u8 = new Uint8Array(out), trunk = mesh.trunk ?? 0; - let top = 0; - for (let k = 0; k < trunk; k++) top = Math.max(top, mesh.position[3 * k + 2]); - const srgb = (c: number) => Math.round(255 * Math.min(1, Math.max(0, c <= 0.0031308 ? 12.92 * c : 1.055 * c ** (1 / 2.4) - 0.055))); - for (let k = 0; k < count; k++) { - for (let d = 0; d < 3; d++) { - i16[8 * k + d] = Math.round(32767 * mesh.position[3 * k + d] / extent); - i8[16 * k + 8 + d] = Math.round(127 * (mesh.normal[3 * k + d] ?? 0)); - u8[16 * k + 12 + d] = srgb(mesh.color[3 * k + d] ?? 1); - } - i8[16 * k + 11] = Math.round(127 * (mesh.rise?.[k] ?? 0)); - i16[8 * k + 3] = k < trunk ? Math.max(1, Math.round(32767 * Math.min(1, top))) : 0; - u8[16 * k + 15] = 255 - Math.round(255 * (mesh.glow?.[k] ?? 0)); - } - return out; -} +const emptyPool = () => ({ at: new WeakMap(), vertex: 0, index: 0 }); diff --git a/builder/web/src/lib/trailview/instances/model-files.test.ts b/builder/web/src/lib/trailview/instances/model-files.test.ts new file mode 100644 index 0000000000..9c7801b3ce --- /dev/null +++ b/builder/web/src/lib/trailview/instances/model-files.test.ts @@ -0,0 +1,38 @@ +import { afterEach, describe, expect, it, vi } from 'vitest'; +import { FakeCaches } from '../../../../test-support/fake-caches'; +import type { Models } from '../models/model-file'; +import { encodeModels, pack } from '../models/pack'; +import { ModelFiles } from './model-files'; + +const triangle = { position: [0, 0, 0, 1, 0, 0, 0, 1, 1], normal: [0, 0, 1, 0, 0, 1, 0, 0, 1], color: [0.2, 0.4, 0.6, 0.2, 0.4, 0.6, 0.2, 0.4, 0.6], index: [0, 1, 2] }; + +describe('the model files', () => { + afterEach(() => (vi.unstubAllGlobals(), vi.restoreAllMocks())); + + it('come from the device cache once a load saved them; a refused file is fetched once, a server error a few times; the old tree cache goes', async () => { + const caches = new FakeCaches(); + vi.stubGlobal('caches', caches); + // The tree cache of earlier builds goes when the store opens. + await caches.open('trailview-trees/release/version'); + vi.spyOn(console, 'warn').mockImplementation(() => {}); + const bytes = await encodeModels([pack([triangle])]); + const fetch = vi.fn(async (url: string) => (url === 'good' ? new Response(bytes) : new Response(null, { status: url === 'gone' ? 404 : 503 }))); + vi.stubGlobal('fetch', fetch); + const first = new ModelFiles({} as Models, () => {}, new AbortController().signal); + const [model] = (await first.load('good'))!; + vi.useFakeTimers({ toFake: ['setTimeout'] }); + const busy = first.load('busy'); + await vi.runAllTimersAsync(); + vi.useRealTimers(); + expect([await first.load('gone'), await first.load('gone'), await busy, await first.load('busy'), fetch.mock.calls.length]).toEqual([undefined, undefined, undefined, undefined, 6]); + expect([first.done('good', 'gone', 'busy'), first.idle]).toEqual([true, true]); + first.save(); + // The writes run in the background: wait until the entry is in the store. + const saved = async () => [...caches.caches.values()].some(entries => [...entries.keys()].some(url => url.endsWith('/good'))); + for (let k = 0; k < 200 && !(await saved()); k++) await new Promise(resolve => setTimeout(resolve, 5)); + const again = await new ModelFiles({} as Models, () => {}, new AbortController().signal).load('good'); + expect([fetch.mock.calls.length, (await caches.keys()).some(name => name.startsWith('trailview-trees/'))]).toEqual([6, false]); + // toStrictEqual compares the classes of the typed arrays too. + expect(again![0]).toStrictEqual(model); + }); +}); diff --git a/builder/web/src/lib/trailview/instances/model-files.ts b/builder/web/src/lib/trailview/instances/model-files.ts new file mode 100644 index 0000000000..a440be7fa5 --- /dev/null +++ b/builder/web/src/lib/trailview/instances/model-files.ts @@ -0,0 +1,109 @@ +// The model files of the build (models/model-file.ts): each read once, from the device cache or else from beside the bundle. +import { decodeModels, type Models, type Packed } from '../models/model-file'; +import { CAPS, DeviceCache } from '../terrain/device-cache'; + +/** The waits before each new try of a load that failed in a way that can pass: the network, a server error, a rate limit. */ +const RETRY_MS = [1000, 3000, 9000]; + +/** + * The model files that the engine loads. A file's name changes with its content, so the device cache keeps each under its + * address with no version. A load tries again after a failure that can pass, RETRY_MS apart. A file that still fails, + * that the server refuses (such as one that a deploy removed) or that does not decode warns once: its models draw nothing + * until the page loads again. What the network gave goes to the device cache once the view is idle (`save`), so a load + * does not wait for the writes. Each caller's load asks for a frame (`wake`) when it settles. + */ +export class ModelFiles { + private readonly cache: Promise; + private readonly loads = new Map>(); + private readonly fetched: [string, Uint8Array][] = []; + /** The files whose load ended, whether or not it failed. */ + private readonly ended = new Set(); + private loading = 0; + private arrived = false; + /** Whether models arrived before the last update. */ + newContent = false; + + constructor(readonly models: Models, private readonly wake: () => void, private readonly stop: AbortSignal) { + this.cache = DeviceCache.open('models', '', '', CAPS.models).catch(() => undefined); + } + + /** The models of the file at `url`; undefined where it failed. */ + load(url: string): Promise { + let load = this.loads.get(url); + if (!load) { + this.loading++; + this.loads.set(url, (load = this.tries(url).then(models => { + this.loading--; + this.ended.add(url); + return models; + }))); + } + // Each caller changes what the frames draw once the load settles, also a caller of a file that arrived earlier. + return load.then(models => { + this.arrived ||= !!models; + this.wake(); + return models; + }); + } + + /** A file's models, with a new try after each failure that can pass while RETRY_MS lasts. */ + private async tries(url: string): Promise { + for (let attempt = 0; ; attempt++) { + const { models, again, error } = await this.read(url); + const last = !again || attempt === RETRY_MS.length; + if (!models && last && !this.stop.aborted) console.warn(`The model file ${url} did not load: ${error}`); + if (models || last || this.stop.aborted) return models; + await new Promise(resolve => setTimeout(resolve, RETRY_MS[attempt])); + } + } + + get idle(): boolean { + return this.loading === 0; + } + + /** Whether the load of each of these files ended. */ + done(...urls: string[]): boolean { + return urls.every(url => this.ended.has(url)); + } + + /** Takes what arrived since the last update, once a frame. */ + update(): void { + this.newContent = this.arrived; + this.arrived = false; + } + + /** A file's models, and whether a failed load may be asked again. */ + private async read(url: string): Promise<{ models?: Packed[]; again?: boolean; error?: string }> { + const kept = await this.cache.then(cache => cache?.read(url)).catch(() => undefined); + if (kept?.length === 1) { + const models = await decodeModels(new Uint8Array(kept[0].buffer)).catch(() => undefined); + if (models) return { models }; + } + // The network, a server error and a rate limit may pass; a refusal or bytes that do not decode stay. + let again = true; + try { + const response = await fetch(url, { signal: this.stop }); + if (!response.ok) { + again = response.status >= 500 || response.status === 408 || response.status === 429; + throw new Error(`HTTP ${response.status}`); + } + const bytes = new Uint8Array(await response.arrayBuffer()); + again = false; + const models = await decodeModels(bytes); + this.fetched.push([url, bytes]); + return { models }; + } catch (error) { + return { again, error: error instanceof Error ? error.message : String(error) }; + } + } + + /** Whether files came from the network that the device cache lacks. */ + get unsaved(): boolean { + return this.fetched.length > 0; + } + + /** Writes the files that came from the network to the device cache. */ + save(): void { + for (const [url, bytes] of this.fetched.splice(0)) void this.cache.then(cache => cache?.write(url, [new Uint32Array(bytes.buffer, bytes.byteOffset, bytes.byteLength / 4)])).catch(() => {}); + } +} diff --git a/builder/web/src/lib/trailview/kinds/kind.ts b/builder/web/src/lib/trailview/kinds/kind.ts index da7ad492a0..0239f00f60 100644 --- a/builder/web/src/lib/trailview/kinds/kind.ts +++ b/builder/web/src/lib/trailview/kinds/kind.ts @@ -3,7 +3,7 @@ // so a new kind changes no other file of the engine. import { geometry, type Model } from '../models/flat'; import { EXTENT, unitScale, type Feature } from '../stream/features'; -import type { ModelSpec } from '../instances/instances'; +import type { FloatMesh } from '../models/model-file'; import type { AnchoredBuilder } from '../structures/anchored'; import { GROUND, mix, type MeshBuilder, station, type Corner } from '../structures/mesh'; import type { Light } from '../structures/light-pools'; @@ -48,10 +48,13 @@ export interface Build { cornered: (x: number, y: number) => boolean; } +/** A model as its generator makes it: its mesh per tier, finest first, where each tier ends in metres, and whether it casts a shadow. */ +export interface ModelSource { tiers: { mesh: FloatMesh; until: number }[]; casts?: boolean } + export interface Kind { build(feature: Placed, out: Build): void; - /** The models that `place` names, each 1 tall with its base at the origin, for the instances. */ - models?: Record ModelSpec>; + /** The models that `place` names, each 1 tall with its base at the origin: the build makes their model files (models/bake.ts). */ + models?: Record ModelSource>; /** The light that a model sheds on the ground about it at night, by the model's name. */ lights?: Record; } @@ -134,7 +137,7 @@ export function modelKind(name: string, model: () => Model, reach: number, { fac } /** A model in metres as the instances take it, 1 tall, with its height in metres; it draws to `reach` metres. */ -export function unitModel(model: () => Model, reach: number): { height: number; spec: () => ModelSpec } { +export function unitModel(model: () => Model, reach: number): { height: number; spec: () => ModelSource } { const height = Math.max(...model().points.filter((_, i) => i % 3 === 2)); return { height, spec: () => ({ tiers: [{ mesh: geometry(model().place(0, [0, 0, 0], 1 / height)), until: reach }] }) }; } diff --git a/builder/web/src/lib/trailview/models/bake.ts b/builder/web/src/lib/trailview/models/bake.ts new file mode 100644 index 0000000000..530e6483bf --- /dev/null +++ b/builder/web/src/lib/trailview/models/bake.ts @@ -0,0 +1,108 @@ +// The model files of the build (specs/trailview-models.md): every model of one fixed shape, made by its generator and +// packed (pack.ts). vite/model-files.ts runs this module in Node and serves what it returns. +import { KINDS } from '../kinds'; +import { SEASONS, type Season } from '../rules/regions'; +import { SUMMER } from '../vegetation/phenology'; +import { animal, BREEDS } from './animals'; +import { geometry } from './flat'; +import { bareIn, tree, TREES } from './library'; +import type { FloatMesh, Models, Packed } from './model-file'; +import { encodeModels, pack } from './pack'; +import { plant, PLANTS } from './plant-library'; +import { rock } from './rocks'; +import { lit, simplified, simplifierReady } from './simplify'; +import { fits, tone } from './tone'; +import type { Grown, Lod } from './trees'; + +/** The crown triangles of tree tiers 1 and 2, which the simplifier makes from tier 0. */ +const TRIANGLES = [600, 200]; +/** A phase draws the summer crown through a colour matrix where that turns every crown vertex within this many sRGB steps of 255. */ +const FIT = 8; +/** Shapes of each plant, and rocks of each kind: as SEEDS of plants.ts and SLABS of dressing.ts. */ +const SEEDS = 3, SLABS = 3; + +/** + * The model budget of specs/trailview-models.md: per kind of model and mesh tier, the most triangles and vertices; and + * the most triangles of tier 0 of a summer crown, as the mean of every tree species. Measured on the models of develop + * 4a5419013, the largest counts rounded up; the owner chose about 5 times for the animals and 4 times for the small + * objects, which few instances draw. + */ +const BUDGET: Record = { + tree: [[4700, 9000], [900, 1800], [230, 600]], + plant: [[350, 1000]], + animal: [[1000, 3000], [250, 750]], + rock: [[100, 300]], + object: [[600, 1800]], +}; +const TREE_MEAN = 3400; + +/** What the build serves: each file by name, and the models with the names of their files. */ +export interface Baked { files: Map; models: Models } + +export async function bake(): Promise { + await simplifierReady; + const files = new Map(), over: string[] = [], summers: number[] = []; + // Models of one kind keep the budget of that kind. + const check = (kind: string, name: string, models: Packed[]) => models.forEach((model, m) => model.tiers.forEach(({ vertices, index }, t) => { + const cap = BUDGET[kind][t], triangles = index.length / 3, count = vertices.length / 16; + if (!cap || triangles > cap[0] || count > cap[1]) over.push(`${name} model ${m} tier ${t}: ${triangles} triangles, ${count} vertices, the budget ${cap ? cap.join(' and ') : 'has no such tier'}`); + })); + const file = async (kind: string, name: string, models: Packed[]) => (check(kind, name, models), files.set(name, await encodeModels(models)), name); + const models: Models = { trees: {}, plants: {}, breeds: {}, objects: { file: '', rocks: 2 * SLABS, points: [] } }; + + for (const [id, spec] of Object.entries(TREES)) { + const summer = ([0, 1, 2] as Lod[]).map(lod => tree(id, 'summer', lod)); + const tones: (number[] | null)[] = [], crowns: (string | null)[] = []; + for (const [phase, season] of SEASONS.entries()) { + if (phase === SUMMER) { + tones.push(null); + crowns.push(null); + continue; + } + const near = bareIn(id, season) ? undefined : tree(id, season, 0), matrix = near && tone(summer[0], near); + tones.push(matrix ?? null); + crowns.push(!matrix || !fits(matrix, summer[0], near!, FIT) ? await file('tree', `tree-${id}-${season}`, [crown(id, season)]) : null); + } + const leafy = treeModel(tiers(summer)); + summers.push(leafy.tiers[0].index.length / 3); + models.trees[id] = { summer: await file('tree', `tree-${id}-summer`, [leafy]), keepsHeading: !!spec.keepsHeading, tones, crowns }; + } + + for (const [id, { ground }] of Object.entries(PLANTS)) { + const seasons: Record = {}; + for (const season of SEASONS) seasons[season] = await file('plant', `plant-${id}-${season}`, Array.from({ length: SEEDS }, (_, k) => pack([float32(plant(id, season, k + 1))]))); + models.plants[id] = { ground, seasons }; + } + + for (const [name, breed] of Object.entries(BREEDS)) models.breeds[name] = { height: breed.height, coats: breed.coats.length, file: await file('animal', `animal-${name}`, breed.coats.map((_, coat) => pack([geometry(animal(name, coat))]))) }; + + const rocks = (['slab', 'boulder'] as const).flatMap(name => Array.from({ length: SLABS }, (_, k) => pack([geometry(rock(name, k + 1))]))); + const points = [...KINDS].flatMap(([, kind]) => Object.values(kind.models ?? {}).map(source => source())); + const objects = points.map(({ tiers }) => pack(tiers.map(({ mesh }) => mesh))); + check('rock', 'objects', rocks); + check('object', 'objects', objects); + files.set('objects', await encodeModels([...rocks, ...objects])); + models.objects = { file: 'objects', rocks: rocks.length, points: points.map(({ tiers, casts }) => ({ until: tiers.map(({ until }) => until), casts })) }; + const mean = summers.reduce((sum, n) => sum + n, 0) / summers.length; + if (mean > TREE_MEAN) over.push(`the tree species: ${Math.round(mean)} triangles in tier 0 on average, the budget ${TREE_MEAN}`); + if (over.length) throw new Error(`Models over the budget of specs/trailview-models.md:\n${over.join('\n')}`); + return { files, models }; +} + +/** A crown of a species' own in a season: bare crowns grow each tier from the tree, the others simplify tier 0. */ +function crown(id: string, season: Season): Packed { + const grown = ([0, 1, 2] as Lod[]).map(lod => tree(id, season, lod)); + return treeModel(bareIn(id, season) ? grown : tiers(grown)); +} + +/** Tier 0, and tiers 1 and 2 simplified from it, each with its own trunk. */ +function tiers([near, middle, far]: Grown[]): Grown[] { + const want = lit(near); + return [near, simplified(near, middle, TRIANGLES[0], want), simplified(near, far, TRIANGLES[1], want)]; +} + +/** A tree model, with its crown's lowest point at every tier. */ +const treeModel = (grown: Grown[]) => pack(grown.map(geo => ({ ...float32(geo), trunk: geo.trunk })), Math.min(...grown.map(geo => geo.crown))); + +/** A tree's or a plant's mesh as the device made it: float32 arrays, without glow or rise. */ +const float32 = (geo: FloatMesh): FloatMesh => ({ position: Float32Array.from(geo.position), normal: Float32Array.from(geo.normal), color: Float32Array.from(geo.color), index: Uint32Array.from(geo.index) }); diff --git a/builder/web/src/lib/trailview/models/model-file.ts b/builder/web/src/lib/trailview/models/model-file.ts new file mode 100644 index 0000000000..4240c429ea --- /dev/null +++ b/builder/web/src/lib/trailview/models/model-file.ts @@ -0,0 +1,114 @@ +// The model files that the web build makes (specs/trailview-models.md): what the instances draw of a model, packed as the +// pools hold it, with the values that the instances derive from its float vertices. The build writes them (pack.ts); the +// engine reads them. +import { MeshoptDecoder } from 'meshoptimizer/decoder'; +import type { Ground } from './plant-library'; + +/** + * A mesh as a generator makes it: per vertex a position, a normal and a linear colour, in a model 1 tall; three indices + * per triangle. A tree's first `trunk` vertices are its trunk, which stretches to meet its crown when the crown lifts + * (draw.wgsl). + */ +export interface FloatMesh { position: ArrayLike; normal: ArrayLike; color: ArrayLike; index: ArrayLike; trunk?: number; glow?: ArrayLike; rise?: ArrayLike } + +/** A mesh as the pools hold it: VERTEX_BYTES a vertex (`quantize`) at its model's extent, and three indices per triangle. */ +export interface Mesh { vertices: Uint8Array; index: Uint32Array } + +/** + * What the instances read of a model's float vertices, which the build measures (pack.ts), as float32: the largest coordinate of its + * tiers (`extent`), the radius of a sphere about (0, 0, 0.5) that holds them, the farthest that a vertex lies from the + * axis, whether a tier has a trunk, a tree's crown base and sink table, the shadow proxy of its crown, and the impostor + * outlines of its last tier. + */ +export interface Shape { + extent: number; + radius: number; + spread: number; + trunk: boolean; + crown?: number; + sinks: number[]; + proxy?: Mesh; + outlines: Float32Array; +} + +/** A model of a file: its mesh tiers, finest first, and its shape. */ +export interface Packed { tiers: Mesh[]; shape: Shape } + +/** Bytes of a vertex, the sinks of a tree and the floats of the impostor outlines (FRAMES × 4). */ +export const VERTEX_BYTES = 16, SINK_SLOPES = 8, OUTLINE_FLOATS = 256; +/** The first word of a file ('TVMF'), and the version of the layout. */ +export const MAGIC = 0x464d5654, VERSION = 1; + +/** + * The vertices of a mesh as the instance pipeline reads them, VERTEX_BYTES each: the position over + * `extent` as snorm16 × 3 and, for a vertex of the trunk, the trunk's top in model units (else 0); the + * normal as snorm8 × 3 and whether it rises at dusk; and the colour in sRGB as unorm8 × 3 with 1 − its glow at night. + */ +export function quantize(mesh: FloatMesh, extent: number): Uint8Array { + const count = mesh.position.length / 3, out = new ArrayBuffer(count * VERTEX_BYTES); + const i16 = new Int16Array(out), i8 = new Int8Array(out), u8 = new Uint8Array(out), trunk = mesh.trunk ?? 0; + let top = 0; + for (let k = 0; k < trunk; k++) top = Math.max(top, mesh.position[3 * k + 2]); + const srgb = (c: number) => Math.round(255 * Math.min(1, Math.max(0, c <= 0.0031308 ? 12.92 * c : 1.055 * c ** (1 / 2.4) - 0.055))); + for (let k = 0; k < count; k++) { + for (let d = 0; d < 3; d++) { + i16[8 * k + d] = Math.round(32767 * mesh.position[3 * k + d] / extent); + i8[16 * k + 8 + d] = Math.round(127 * (mesh.normal[3 * k + d] ?? 0)); + u8[16 * k + 12 + d] = srgb(mesh.color[3 * k + d] ?? 1); + } + i8[16 * k + 11] = Math.round(127 * (mesh.rise?.[k] ?? 0)); + i16[8 * k + 3] = k < trunk ? Math.max(1, Math.round(32767 * Math.min(1, top))) : 0; + u8[16 * k + 15] = 255 - Math.round(255 * (mesh.glow?.[k] ?? 0)); + } + return u8; +} + +/** A tree species: its summer crown's file and, per phase (winter first), its colour matrix (3 × 3, columns first) and the file of a crown of its own. */ +export interface TreeEntry { summer: string; keepsHeading: boolean; tones: (number[] | null)[]; crowns: (string | null)[] } +/** A plant: where it grows, and per season the file of its SEEDS shapes. */ +export interface PlantEntry { ground: Ground; seasons: Record } +/** A breed: its height in metres to the top of the back, its count of coats, and the file of its coats. */ +export interface BreedEntry { height: number; coats: number; file: string } + +/** + * The models of the build (`virtual:models`): what the engine knows before it fetches a file. The objects file holds the + * rocks (`rocks` of them: slabs, then boulders) and then the models of the kinds in the order of POINT_MODELS, with + * where each tier ends and whether the model casts a shadow. + */ +export interface Models { + trees: Record; + plants: Record; + breeds: Record; + objects: { file: string; rocks: number; points: { until: number[]; casts?: boolean }[] }; +} + +/** The models of a file; it throws where the bytes are not a model file of this version. */ +export async function decodeModels(bytes: Uint8Array): Promise { + await MeshoptDecoder.ready; + const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength); + let at = 0; + const u32 = () => ((at += 4), view.getUint32(at - 4, true)), f32 = () => ((at += 4), view.getFloat32(at - 4, true)); + if (u32() !== MAGIC || u32() !== VERSION) throw new Error('not a model file of this version'); + const heads = Array.from({ length: u32() }, () => { + const [extent, radius, spread, crown] = [f32(), f32(), f32(), f32()], sinks = Array.from({ length: SINK_SLOPES }, f32); + const flags = u32(), tiers = u32(), counts = Array.from({ length: tiers + (flags & 2 ? 1 : 0) }, () => [u32(), u32()]); + if (!tiers) throw new Error('a model of a model file has no mesh tier'); + const outlines = new Float32Array(OUTLINE_FLOATS); + for (let k = 0; k < OUTLINE_FLOATS; k++) outlines[k] = f32(); + return { shape: { extent, radius, spread, crown: Number.isNaN(crown) ? undefined : crown, sinks, trunk: !!(flags & 1), outlines }, counts, proxy: !!(flags & 2) }; + }); + const blob = () => { + const length = u32(), out = bytes.subarray(at, at + length); + at += Math.ceil(length / 4) * 4; + return out; + }; + return heads.map(({ shape, counts, proxy }) => { + const meshes = counts.map(([vertices, indices]): Mesh => { + const mesh = { vertices: new Uint8Array(vertices * VERTEX_BYTES), index: new Uint32Array(indices) }; + MeshoptDecoder.decodeVertexBuffer(mesh.vertices, vertices, VERTEX_BYTES, blob()); + MeshoptDecoder.decodeIndexSequence(new Uint8Array(mesh.index.buffer), indices, 4, blob()); + return mesh; + }); + return { tiers: proxy ? meshes.slice(0, -1) : meshes, shape: { ...shape, proxy: proxy ? meshes.at(-1) : undefined } }; + }); +} diff --git a/builder/web/src/lib/trailview/instances/instances.test.ts b/builder/web/src/lib/trailview/models/pack.test.ts similarity index 61% rename from builder/web/src/lib/trailview/instances/instances.test.ts rename to builder/web/src/lib/trailview/models/pack.test.ts index 9c95273c71..b95446ba11 100644 --- a/builder/web/src/lib/trailview/instances/instances.test.ts +++ b/builder/web/src/lib/trailview/models/pack.test.ts @@ -1,6 +1,25 @@ import { describe, expect, it } from 'vitest'; -import { tree } from '../models/library'; -import { outlines } from './instances'; +import { firepit } from './amenities'; +import { geometry } from './flat'; +import { tree } from './library'; +import { decodeModels } from './model-file'; +import { encodeModels, outlines, pack } from './pack'; +import type { Lod } from './trees'; + +describe('the model files', () => { + it('give back the packed models bit for bit: tiers with trunks, a crown and a shadow proxy, and a model that glows', async () => { + const spruce = ([0, 1, 2] as Lod[]).map(lod => tree('norway-spruce', 'summer', lod)); + const models = [pack(spruce, Math.min(...spruce.map(tier => tier.crown))), pack([geometry(firepit())])]; + expect(models[0].shape.proxy).toBeDefined(); + // toStrictEqual compares the classes of the typed arrays too. + expect(await decodeModels(await encodeModels(models))).toStrictEqual(models); + }); + + it('refuse a model with no mesh tier', async () => { + const [model] = [pack([geometry(firepit())])]; + await expect(decodeModels(await encodeModels([{ ...model, tiers: [] }]))).rejects.toThrow('no mesh tier'); + }); +}); describe('the impostor outlines', () => { it('hold the mesh seen along every frame', () => { diff --git a/builder/web/src/lib/trailview/models/pack.ts b/builder/web/src/lib/trailview/models/pack.ts new file mode 100644 index 0000000000..6452c96fd8 --- /dev/null +++ b/builder/web/src/lib/trailview/models/pack.ts @@ -0,0 +1,167 @@ +// Packs the models that a generator makes into model files (model-file.ts), in the build. It measures each model as the +// instances did on the device: from the same float vertices, so the pools and the model records hold the same bytes. +import { MeshoptEncoder } from 'meshoptimizer/encoder'; +import { MAGIC, OUTLINE_FLOATS, quantize, SINK_SLOPES, VERSION, type FloatMesh, type Mesh, type Packed, type Shape } from './model-file'; + +/** A tree's sink table: its slopes, SINK_STEP apart from SINK_STEP, as in emit.wgsl. */ +const SINK_STEP = 0.25; +/** Frames of an impostor (8 × 8, FRAMES of hemi.wgsl). */ +const FRAMES = OUTLINE_FLOATS / 4; + +/** A model from its float tiers, finest first, and a tree's crown base in model units. */ +export function pack(tiers: FloatMesh[], crown?: number): Packed { + const extent = Math.max(...tiers.map(mesh => largest(mesh.position))); + const radius = Math.max(0, ...tiers.map(({ position: p }) => { + let most = 0; + for (let i = 0; i < p.length; i += 3) most = Math.max(most, Math.hypot(p[i], p[i + 1], p[i + 2] - 0.5)); + return most; + })); + const spread = Math.max(0, ...tiers.map(({ position: p }) => { + let most = 0; + for (let i = 0; i < p.length; i += 3) most = Math.max(most, Math.hypot(p[i], p[i + 1])); + return most; + })); + // The model record holds these values as float32; the proxy took its extent from the record. + const hull = proxy(tiers[tiers.length - 1]), f32 = Math.fround; + const shape: Shape = { + extent: f32(extent), radius: f32(radius), spread: f32(spread), trunk: tiers.some(mesh => mesh.trunk), crown: crown === undefined ? undefined : f32(crown), + sinks: sinks(tiers, crown).map(f32), proxy: hull && packMesh(hull, f32(extent)), outlines: outlines(tiers[tiers.length - 1], radius), + }; + return { tiers: tiers.map(mesh => packMesh(mesh, extent)), shape }; +} + +const packMesh = (mesh: FloatMesh, extent: number): Mesh => ({ vertices: quantize(mesh, extent), index: Uint32Array.from(mesh.index) }); + +/** A model file of models: the layout of specs/trailview-models.md. */ +export async function encodeModels(models: Packed[]): Promise> { + await MeshoptEncoder.ready; + const words: number[] = [], floats = new Set(), blobs: Uint8Array[] = []; + const u32 = (value: number) => words.push(value), f32 = (value: number) => (floats.add(words.length), words.push(value)); + u32(MAGIC); + u32(VERSION); + u32(models.length); + for (const { tiers, shape } of models) { + [shape.extent, shape.radius, shape.spread, shape.crown ?? NaN, ...shape.sinks].forEach(f32); + const meshes = shape.proxy ? [...tiers, shape.proxy] : tiers; + u32((shape.trunk ? 1 : 0) | (shape.proxy ? 2 : 0)); + u32(tiers.length); + for (const { vertices, index } of meshes) { + u32(vertices.length / 16); + u32(index.length); + } + shape.outlines.forEach(f32); + for (const { vertices, index } of meshes) blobs.push(MeshoptEncoder.encodeVertexBuffer(vertices, vertices.length / 16, 16), MeshoptEncoder.encodeIndexSequence(new Uint8Array(index.buffer, index.byteOffset, index.byteLength), index.length, 4)); + } + const head = words.length * 4, size = blobs.reduce((sum, blob) => sum + 4 + Math.ceil(blob.length / 4) * 4, head); + const out = new Uint8Array(size), view = new DataView(out.buffer); + words.forEach((value, k) => (floats.has(k) ? view.setFloat32(4 * k, value, true) : view.setUint32(4 * k, value, true))); + let at = head; + for (const blob of blobs) { + view.setUint32(at, blob.length, true); + out.set(blob, at + 4); + at += 4 + Math.ceil(blob.length / 4) * 4; + } + return out; +} + +/** The largest absolute value. */ +function largest(values: ArrayLike): number { + let most = 0; + for (let i = 0; i < values.length; i++) most = Math.max(most, Math.abs(values[i])); + return most; +} + +/** + * The proxy of a crown in the far shadow map: a diamond of 8 faces about its crown's vertices, from its + * lowest to its highest point and widest where its widest vertex lies. A mesh whose faces cover less + * than half of the diamond's outline seen from above, such as a bare crown's branches, has none. + */ +export function proxy(mesh: FloatMesh): FloatMesh | undefined { + const p = mesh.position, from = mesh.trunk ?? 0, count = p.length / 3; + if (count <= from) return undefined; + let [low, high, middle, widest] = [Infinity, -Infinity, 0, 0]; + const side = [0, 0, 0, 0]; + for (let k = from; k < count; k++) { + const [x, y, z] = [p[3 * k], p[3 * k + 1], p[3 * k + 2]]; + [low, high] = [Math.min(low, z), Math.max(high, z)]; + side[0] = Math.max(side[0], x), side[1] = Math.max(side[1], y), side[2] = Math.max(side[2], -x), side[3] = Math.max(side[3], -y); + if (Math.hypot(x, y) > widest) [widest, middle] = [Math.hypot(x, y), z]; + } + let cover = 0; + for (let i = 0; i < mesh.index.length; i += 3) { + const [a, b, c] = [mesh.index[i], mesh.index[i + 1], mesh.index[i + 2]].map(v => 3 * v); + if (Math.min(a, b, c) < 3 * from) continue; + cover += Math.abs((p[b] - p[a]) * (p[c + 1] - p[a + 1]) - (p[c] - p[a]) * (p[b + 1] - p[a + 1])) / 2; + } + if (cover < (side[0] + side[2]) * (side[1] + side[3]) / 4) return undefined; + const position = [0, 0, high, 0, 0, low, side[0], 0, middle, 0, side[1], middle, -side[2], 0, middle, 0, -side[3], middle]; + return { position, normal: new Array(18).fill(0), color: new Array(18).fill(1), index: [0, 2, 3, 0, 3, 4, 0, 4, 5, 0, 5, 2, 1, 3, 2, 1, 4, 3, 1, 5, 4, 1, 2, 5] }; +} + +/** + * Per frame of a model's impostor (8 × 8, FRAMES of hemi.wgsl), the trapezoid of the frame's square + * (−1 to 1 over the sphere of `radius` about (0, 0, 0.5)) that holds the mesh seen along the frame: its + * bottom and top, and its half widths there, the least in area. + */ +export function outlines(mesh: FloatMesh, radius: number): Float32Array { + const BANDS = 32, p = mesh.position, n = p.length / 3, out = new Float32Array(FRAMES * 4); + const xs = new Float64Array(n), ys = new Float64Array(n); + for (let frame = 0; frame < FRAMES; frame++) { + // hemi_decode and view_axes of hemi.wgsl. + const ex = (frame % 8) / 3.5 - 1, ey = Math.floor(frame / 8) / 3.5 - 1, px = (ex + ey) / 2, py = (ex - ey) / 2; + const pz = 1 - Math.abs(px) - Math.abs(py), norm = Math.hypot(px, py, pz), d = [px / norm, py / norm, pz / norm]; + const flat = Math.hypot(d[0], d[1]), right = Math.abs(d[2]) < 0.999 ? [-d[1] / flat, d[0] / flat, 0] : [1, 0, 0]; + const up = [d[1] * right[2] - d[2] * right[1], d[2] * right[0] - d[0] * right[2], d[0] * right[1] - d[1] * right[0]]; + let low = Infinity, high = -Infinity; + for (let k = 0; k < n; k++) { + const x = p[3 * k], y = p[3 * k + 1], z = p[3 * k + 2] - 0.5; + xs[k] = Math.abs(x * right[0] + y * right[1] + z * right[2]) / radius; + ys[k] = (x * up[0] + y * up[1] + z * up[2]) / radius; + low = Math.min(low, ys[k]); + high = Math.max(high, ys[k]); + } + // The widest |x| in each of BANDS bands of height, which the trapezoid holds at both ends of the band. + const span = Math.max(high - low, 1e-6), widest = new Float64Array(BANDS).fill(-1); + for (let k = 0; k < n; k++) { + const band = Math.min(BANDS - 1, Math.floor(((ys[k] - low) / span) * BANDS)); + widest[band] = Math.max(widest[band], xs[k]); + } + // The least w0 + w1 where w0 (1 − t) + w1 t ≥ the widest at both ends t of every band: convex in w1. + const bottom = (top: number) => { + let w = 0; + for (let band = 0; band < BANDS; band++) { + const x = widest[band]; + if (x < 0) continue; + // At the band's foot; its top binds harder unless it is the trapezoid's top. + w = Math.max(w, (x - (top * band) / BANDS) / (1 - band / BANDS)); + if (band < BANDS - 1) w = Math.max(w, (x - (top * (band + 1)) / BANDS) / (1 - (band + 1) / BANDS)); + } + return w; + }; + // The top band's top needs w1 at least its widest. + let a = widest[BANDS - 1], b = Math.max(...widest); + for (let step = 0; step < 24; step++) { + const m1 = a + (b - a) / 3, m2 = b - (b - a) / 3; + if (m1 + bottom(m1) <= m2 + bottom(m2)) b = m2; + else a = m1; + } + out.set([low, high, bottom(b) + 1e-4, b + 1e-4], 4 * frame); + } + return out; +} + +/** + * A tree's sink table (slope_sink of emit.wgsl): per slope SINK_STEP × (k + 1), the most that the drop + * of the ground under a crown vertex, the slope × its reach, exceeds its height above the crown's base; + * a vertex below the base counts as at it. Over the vertices of every tier but their trunks'. + */ +export function sinks(tiers: FloatMesh[], crown: number | undefined): number[] { + const out = new Array(SINK_SLOPES).fill(0); + if (crown === undefined) return out; + for (const { position: p, trunk = 0 } of tiers) + for (let i = 3 * trunk; i < p.length; i += 3) { + const reach = Math.hypot(p[i], p[i + 1]), above = Math.max(0, p[i + 2] - crown); + for (let k = 0; k < SINK_SLOPES; k++) out[k] = Math.max(out[k], SINK_STEP * (k + 1) * reach - above); + } + return out; +} diff --git a/builder/web/src/lib/trailview/structures/kind-points.ts b/builder/web/src/lib/trailview/structures/kind-points.ts index e0b3f1a5bf..6fa54ba5ff 100644 --- a/builder/web/src/lib/trailview/structures/kind-points.ts +++ b/builder/web/src/lib/trailview/structures/kind-points.ts @@ -1,4 +1,5 @@ import type { Instances } from '../instances/instances'; +import type { ModelFiles } from '../instances/model-files'; import { Points, POINT_WORDS } from '../instances/points'; import { KINDS } from '../kinds'; import { poolWords } from './light-pools'; @@ -7,8 +8,8 @@ import { poolWords } from './light-pools'; const CAPACITY = 2 ** 15; /** - * The models on the surface of the kinds: their models in the instances, in the order of POINT_MODELS, - * and the points of the drawn nodes in one pool. + * The models on the surface of the kinds: their models in the instances, in the order of POINT_MODELS, from the objects + * file (models/model-file.ts), and the points of the drawn nodes in one pool. */ export class KindPoints { private readonly pool: Points; @@ -18,11 +19,22 @@ export class KindPoints { private readonly lightOf = [...KINDS].flatMap(([, kind]) => Object.keys(kind.models ?? {}).map(model => kind.lights?.[model])); /** The pools of light of the points, as light-pools.ts takes them. */ lights = new Uint32Array(0); - - constructor(instances: Instances) { - const specs = [...KINDS].flatMap(([, kind]) => Object.values(kind.models ?? {})); - this.first = instances.add(...specs.map(spec => spec())); + constructor(instances: Instances, private readonly files: ModelFiles) { + const { objects } = files.models, first = instances.add(...objects.points.map(() => ({ tiers: [] }))); + this.first = first; this.pool = new Points(instances, CAPACITY); + void files.load(objects.file).then(models => { + if (first === undefined || !models) return; + objects.points.forEach(({ until, casts }, k) => { + const { tiers, shape } = models[objects.rocks + k]; + instances.replace(first + k, { tiers: tiers.map((mesh, t) => ({ mesh, until: until[t] })), shape, casts }); + }); + }); + } + + /** Whether the models have arrived, or failed. */ + get idle(): boolean { + return this.files.done(this.files.models.objects.file); } /** The points of the drawn nodes' kinds, each list as `packPoints` packs it; a new set of lists replaces the pool's points. */ diff --git a/builder/web/src/lib/trailview/terrain/device-cache.ts b/builder/web/src/lib/trailview/terrain/device-cache.ts index e3b6c86c22..a9451f8b79 100644 --- a/builder/web/src/lib/trailview/terrain/device-cache.ts +++ b/builder/web/src/lib/trailview/terrain/device-cache.ts @@ -1,14 +1,16 @@ // The device cache: what a worker makes of the release's tiles, kept in Cache Storage under the release and a // build version, the hash of the code that makes it (model.ts: the plan's cells with `ground=plan`, stamps.ts: -// the stamp worker's nodes, houses.ts: the house records of a tile, tree-models.ts: the tree models, which no -// tile changes). That code is a pure function of its input, so an entry is byte-equal to what the worker makes. -// The least recently used entries leave past the cap. +// the stamp worker's nodes, houses.ts: the house records of a tile). That code is a pure function of its input, so +// an entry is byte-equal to what the worker makes. It also keeps the model files of the build (instances/model-files.ts), +// whose names change with their content. The least recently used entries leave past the cap. /** * The device storage of each kind: past its cap, the least recently used entries of the kind go. Together - * 296 MiB; a view writes more cell bytes than stamp bytes. The cap of the trees holds the models of every species. + * 280 MiB; a view writes more cell bytes than stamp bytes. The cap of the models holds the model files of many regions. */ -export const CAPS = { cells: 192 * 2 ** 20, stamps: 64 * 2 ** 20, houses: 16 * 2 ** 20, trees: 24 * 2 ** 20 }; +export const CAPS = { cells: 192 * 2 ** 20, stamps: 64 * 2 ** 20, houses: 16 * 2 ** 20, models: 8 * 2 ** 20 }; +/** The kinds that no build opens any more: `open` deletes them, as it deletes the caches of another release or build. */ +const RETIRED = ['trailview-trees/']; /** The entry's first word, then the count of parts, their lengths in words and the SHA-256 of the parts. */ const MAGIC = 0x4f424343; const DIGEST_WORDS = 8; @@ -67,7 +69,7 @@ export class DeviceCache { static async open(kind: string, release: string, build: string, cap: number): Promise { if (!globalThis.caches) return undefined; const prefix = `trailview-${kind}/`, name = `${prefix}${await sha256(release)}/${build}`; - void caches.keys().then(names => Promise.all(names.filter(other => other.startsWith(prefix) && other !== name).map(other => caches.delete(other)))).catch(() => {}); + void caches.keys().then(names => Promise.all(names.filter(other => (other.startsWith(prefix) && other !== name) || RETIRED.some(kind => other.startsWith(kind))).map(other => caches.delete(other)))).catch(() => {}); return new DeviceCache(await caches.open(name), cap); } diff --git a/builder/web/src/lib/trailview/vegetation/model-entry.ts b/builder/web/src/lib/trailview/vegetation/model-entry.ts deleted file mode 100644 index db2a185aad..0000000000 --- a/builder/web/src/lib/trailview/vegetation/model-entry.ts +++ /dev/null @@ -1,28 +0,0 @@ -// A built tree model as an entry of the device cache. The tree version hashes this module with the model worker, so a -// change of the layout makes another cache. -import { SEASONS } from '../rules/regions'; -import type { BuiltLeafy, BuiltModel } from './model-worker'; - -/** Numbers in the head per phase of a summer crown: whether the phase draws a crown of its own, whether it has a colour matrix, and the matrix. */ -const PHASE_WORDS = 11; - -/** A built model as the parts of an entry: a float64 head (the crown's lowest point, the tiers' trunks and, for a summer crown, its phases), then the arrays of each tier. */ -export function partsOf(model: BuiltModel | BuiltLeafy): Uint32Array[] { - const { tiers, crown } = model, leafy = 'tones' in model ? model : undefined; - const head = [crown, tiers.length, ...tiers.map(({ trunk }) => trunk), leafy ? 1 : 0]; - if (leafy) SEASONS.forEach((_, phase) => head.push(+!!leafy.own[phase], leafy.tones[phase] ? 1 : 0, ...(leafy.tones[phase] ?? Array(9).fill(0)))); - return [new Uint32Array(Float64Array.from(head).buffer), ...tiers.flatMap(({ position, normal, color, index }) => [position, normal, color, index].map(array => new Uint32Array(array.buffer, array.byteOffset, array.length)))]; -} - -/** The model of an entry; it throws where the entry does not hold one. */ -export function modelOf([words, ...arrays]: Uint32Array[]): BuiltModel | BuiltLeafy { - const head = new Float64Array(words.buffer), count = head[1], at = 2 + count; - if (!Number.isInteger(count) || arrays.length !== 4 * count || head.length !== at + 1 + (head[at] ? PHASE_WORDS * SEASONS.length : 0)) throw new Error('not a tree model'); - const tiers = [...head.subarray(2, at)].map((trunk, t) => { - const [position, normal, color] = arrays.slice(4 * t, 4 * t + 3).map(array => new Float32Array(array.buffer)); - return { position, normal, color, index: arrays[4 * t + 3], trunk }; - }); - if (!head[at]) return { tiers, crown: head[0] }; - const phases = SEASONS.map((_, phase) => head.subarray(at + 1 + PHASE_WORDS * phase, at + 1 + PHASE_WORDS * (phase + 1))); - return { tiers, crown: head[0], own: phases.map(words => words[0] === 1), tones: phases.map(words => (words[1] ? [...words.subarray(2)] : undefined)) }; -} diff --git a/builder/web/src/lib/trailview/vegetation/model-worker.ts b/builder/web/src/lib/trailview/vegetation/model-worker.ts deleted file mode 100644 index f6bb91e8d4..0000000000 --- a/builder/web/src/lib/trailview/vegetation/model-worker.ts +++ /dev/null @@ -1,71 +0,0 @@ -// Builds tree models off the main thread: three tiers of each, as plain arrays. -import { SEASONS, type Season } from '../rules/regions'; -import { bareIn, tree } from '../models/library'; -import { lit, simplified, simplifierReady } from '../models/simplify'; -import { fits, tone } from '../models/tone'; -import type { Grown, Lod } from '../models/trees'; -import { SUMMER } from './phenology'; - -/** - * The crown triangles of tiers 1 and 2, which the simplifier makes from tier 0. A bare crown's tiers - * grow from the same tree (trees.ts). - */ -const TRIANGLES = [600, 200]; -/** A phase draws the summer crown through a colour matrix where that turns every crown vertex within this many sRGB steps of 255. */ -const FIT = 8; - -/** - * A job: species to build in summer, with the colour matrices and crowns of their phases, and crowns of a species in - * a phase. A species that a pack keeps green all year takes the same build and leaves its phases out. - */ -export interface ModelJob { id: number; leafy: { model: string }[]; crowns: { model: string; phase: number }[] } -export interface BuiltTier { position: Float32Array; normal: Float32Array; color: Float32Array; index: Uint32Array } -/** A model's tiers, each with its trunk's vertices first, and its crown's lowest point in model units at every tier. */ -export interface BuiltModel { tiers: (BuiltTier & { trunk: number })[]; crown: number } -/** A job's answer: the models in the order of its `leafy` and its `crowns`. */ -export interface ModelAnswer { id: number; leafy?: BuiltLeafy[]; crowns?: BuiltModel[]; error?: string } -/** - * A species' leafy crown in summer, with per phase (winter first) the colour matrix (3 × 3, columns - * first) of the least error that turns it into that phase's, and whether the phase needs a crown of its - * own: a bare crown, or flecks that the season moves, which the matrix does not turn within FIT. - */ -export interface BuiltLeafy extends BuiltModel { tones: (number[] | undefined)[]; own: boolean[] } - -self.onmessage = async ({ data: { id, leafy, crowns } }: MessageEvent) => { - try { - await simplifierReady; - const species = leafy.map(({ model }): BuiltLeafy => { - const summer = ([0, 1, 2] as Lod[]).map(lod => tree(model, 'summer', lod)); - const tones: (number[] | undefined)[] = [], own: boolean[] = []; - SEASONS.forEach((season, phase) => { - if (phase === SUMMER) return; - const near = bareIn(model, season) ? undefined : tree(model, season, 0), matrix = near && tone(summer[0], near); - tones[phase] = matrix; - own[phase] = !matrix || !fits(matrix, summer[0], near!, FIT); - }); - return { ...arrays(tiers(summer)), tones, own }; - }); - const built = crowns.map(({ model, phase }): BuiltModel => { - const season: Season = SEASONS[phase], grown = ([0, 1, 2] as Lod[]).map(lod => tree(model, season, lod)); - return arrays(bareIn(model, season) ? grown : tiers(grown)); - }); - self.postMessage({ id, leafy: species, crowns: built } satisfies ModelAnswer, { transfer: [...species, ...built].flatMap(m => m.tiers).flatMap(t => [t.position.buffer, t.normal.buffer, t.color.buffer, t.index.buffer]) }); - } catch (error) { - self.postMessage({ id, error: error instanceof Error ? error.message : String(error) } satisfies ModelAnswer); - } -}; - -/** Tier 0, and tiers 1 and 2 simplified from it, each with its own trunk. */ -function tiers([near, middle, far]: Grown[]): Grown[] { - const want = lit(near); - return [near, simplified(near, middle, TRIANGLES[0], want), simplified(near, far, TRIANGLES[1], want)]; -} - -function arrays(grown: Grown[]): BuiltModel { - return { - tiers: grown.map(geo => ({ - position: Float32Array.from(geo.position), normal: Float32Array.from(geo.normal), color: Float32Array.from(geo.color), index: Uint32Array.from(geo.index), trunk: geo.trunk, - })), - crown: Math.min(...grown.map(geo => geo.crown)), - }; -} diff --git a/builder/web/src/lib/trailview/vegetation/plant-worker.ts b/builder/web/src/lib/trailview/vegetation/plant-worker.ts deleted file mode 100644 index 5f9df3685e..0000000000 --- a/builder/web/src/lib/trailview/vegetation/plant-worker.ts +++ /dev/null @@ -1,19 +0,0 @@ -// Builds the plant models off the main thread, as plain arrays: a plant in one of its shapes. -import type { Season } from '../rules/regions'; -import { plant } from '../models/plant-library'; - -/** The models to build, each in a season: a plant's shape by its seed. */ -export interface PlantJob { id: number; models: { model: string; seed: number; season: Season }[] } -export interface BuiltMesh { position: Float32Array; normal: Float32Array; color: Float32Array; index: Uint32Array } - -self.onmessage = ({ data: { id, models } }: MessageEvent) => { - try { - const built = models.map(({ model, seed, season }): BuiltMesh => { - const geo = plant(model, season, seed); - return { position: Float32Array.from(geo.position), normal: Float32Array.from(geo.normal), color: Float32Array.from(geo.color), index: Uint32Array.from(geo.index) }; - }); - self.postMessage({ id, built }, { transfer: built.flatMap(m => [m.position.buffer, m.normal.buffer, m.color.buffer, m.index.buffer]) }); - } catch (error) { - self.postMessage({ id, error: error instanceof Error ? error.message : String(error) }); - } -}; diff --git a/builder/web/src/lib/trailview/vegetation/plants.test.ts b/builder/web/src/lib/trailview/vegetation/plants.test.ts index 6eddd6a0ac..594f96ab2f 100644 --- a/builder/web/src/lib/trailview/vegetation/plants.test.ts +++ b/builder/web/src/lib/trailview/vegetation/plants.test.ts @@ -1,10 +1,14 @@ -import { afterEach, describe, expect, it, vi } from 'vitest'; +import { describe, expect, it } from 'vitest'; import type { Instances, ModelSpec } from '../instances/instances'; +import type { ModelFiles } from '../instances/model-files'; +import type { Packed } from '../models/model-file'; +import { PLANTS } from '../models/plant-library'; import type { GroundRule, Region, Rules, TreeRule, Vocabulary } from '../rules/regions'; import { CLUMPED, groundList, groundRules, materialWord, Plants, SEEDS, SLOTS } from './plants'; import type { TreeModels } from './tree-models'; const region = (ground: GroundRule[]) => ({ ground }) as unknown as Region; +const plants = Object.fromEntries(Object.entries(PLANTS).map(([id, { ground }]) => [id, { ground, seasons: {} }])); const vocabulary: Vocabulary = { materials: [{ id: 'none', cover: 'grass' }, { id: 'heath', cover: 'grass', tags: ['moor'] }, { id: 'scrub', cover: 'shrub' }], lithologies: [{ id: 'unknown', hardness: 0.5 }], @@ -19,18 +23,18 @@ describe('the ground plants of a region', () => { }); it('take a pack\'s clumped: false over the model\'s stands, and the salt of their stands from the species alone', () => { - const [own, plain] = groundRules(region([{ id: 'bilberry', model: 'bilberry' }, { id: 'blaeberry', model: 'bilberry', clumped: false, share: 0.5 }]), vocabulary) + const [own, plain] = groundRules(region([{ id: 'bilberry', model: 'bilberry' }, { id: 'blaeberry', model: 'bilberry', clumped: false, share: 0.5 }]), vocabulary, plants) .sort((a, b) => a.id.localeCompare(b.id)); expect(own.word & CLUMPED).toBe(CLUMPED); expect(plain.word & CLUMPED).toBe(0); - const alone = groundRules(region([{ id: 'blaeberry', model: 'bilberry', share: 0.9 }]), vocabulary)[0]; + const alone = groundRules(region([{ id: 'blaeberry', model: 'bilberry', share: 0.9 }]), vocabulary, plants)[0]; expect(alone.word >>> 16).toBe(plain.word >>> 16); expect(own.word >>> 16).not.toBe(plain.word >>> 16); }); it('grow on the materials that their on names but their off does not', () => { - const [heather] = groundRules(region([{ id: 'heather', model: 'heather', on: ['#moor', 'shrub'] }]), vocabulary); - const [grass] = groundRules(region([{ id: 'grass', model: 'grass', off: ['#moor'] }]), vocabulary); + const [heather] = groundRules(region([{ id: 'heather', model: 'heather', on: ['#moor', 'shrub'] }]), vocabulary, plants); + const [grass] = groundRules(region([{ id: 'grass', model: 'grass', off: ['#moor'] }]), vocabulary, plants); expect(heather.materials).toEqual([0b110, 0]); expect(grass.materials).toEqual([0b001, 0]); }); @@ -45,19 +49,56 @@ describe('the materials of the plants', () => { }); describe('the plant models', () => { - afterEach(() => vi.unstubAllGlobals()); - it('give a plant in rows a run of its own, the same each time, not a shrub of the same plant', () => { - vi.stubGlobal('Worker', class { postMessage() {} }); const specs: ModelSpec[] = []; const instances = { produce: () => {}, add: (...added: ModelSpec[]) => specs.push(...added) - added.length }; const shrub: TreeRule = { model: 'grapevine', leaf: 'broadleaved', cover: 'shrub', elevation: [0, 1000], weight: 1 }; const rules = { vocabulary, regions: [{ ground: [], trees: [shrub] }], styles: [] } as unknown as Rules; - const plants = new Plants(rules, {} as TreeModels, instances as unknown as Instances, () => {}, new AbortController().signal); + const models = new Plants(rules, {} as TreeModels, instances as unknown as Instances, { models: { plants, trees: {} } } as unknown as ModelFiles); expect(specs).toHaveLength(1); - const first = plants.rowModel(0, 'grapevine'); + const first = models.rowModel(0, 'grapevine'); expect(first).toBe(1); - expect(plants.rowModel(0, 'grapevine')).toBe(first); + expect(models.rowModel(0, 'grapevine')).toBe(first); expect(specs).toHaveLength(1 + SEEDS); }); + + it('take the shapes of the season that a file brings, and count a file that failed as done until the season changes', async () => { + const files: { url: string; answer: (models: Packed[] | undefined) => Promise }[] = []; + const load = (url: string) => new Promise(resolve => files.push({ url, answer: models => (resolve(models), new Promise(done => setTimeout(done))) })); + const specs: ModelSpec[] = []; + const instances = { produce: () => {}, add: (...added: ModelSpec[]) => specs.push(...added) - added.length, replace: (m: number, spec: ModelSpec) => (specs[m] = spec) }; + const rules = { vocabulary, regions: [{ ground: [{ id: 'grass', model: 'grass' }], trees: [] }], styles: [] } as unknown as Rules; + const models = { plants: { grass: { ground: PLANTS.grass.ground, seasons: { summer: 'grass/summer', winter: 'grass/winter' } } }, trees: {} }; + const plants = new Plants(rules, {} as TreeModels, instances as unknown as Instances, { models, load } as unknown as ModelFiles); + const reach = { reach: new Set([0]), near: new Set([0]) }, shape = (k: number) => ({ tiers: [{ vertices: new Uint8Array(16 * k), index: new Uint32Array(3) }], shape: {} }) as unknown as Packed; + plants.update([8.4, 46.8], '2026-07-01', 0, reach); + expect(files.map(({ url }) => url)).toEqual(['grass/summer', 'grass/summer', 'grass/summer']); + // A file of a season that passed while it loaded changes nothing. + plants.update([8.4, 46.8], '2026-01-20', 0, reach); + await Promise.all(files.slice(0, 3).map(file => file.answer([shape(1), shape(2), shape(3)]))); + expect(specs.every(spec => !spec.tiers.length)).toBe(true); + await Promise.all(files.slice(3).map(file => file.answer(undefined))); + expect([plants.idle, plants.nearIdle]).toEqual([true, true]); + plants.update([8.4, 46.8], '2026-07-01', 0, reach); + expect(files.length).toBe(9); + }); + + it('draw nothing of a model whose file for the new season failed', async () => { + const answers: ((models: Packed[] | undefined) => void)[] = []; + const load = () => new Promise(resolve => answers.push(resolve)); + const specs: ModelSpec[] = []; + const instances = { produce: () => {}, add: (...added: ModelSpec[]) => specs.push(...added) - added.length, replace: (m: number, spec: ModelSpec) => (specs[m] = spec) }; + const rules = { vocabulary, regions: [{ ground: [{ id: 'grass', model: 'grass' }], trees: [] }], styles: [] } as unknown as Rules; + const models = { plants: { grass: { ground: PLANTS.grass.ground, seasons: { summer: 'grass/summer', winter: 'grass/winter' } } }, trees: {} }; + const plants = new Plants(rules, {} as TreeModels, instances as unknown as Instances, { models, load } as unknown as ModelFiles); + const reach = { reach: new Set([0]), near: new Set([0]) }, shape = { tiers: [{ vertices: new Uint8Array(16), index: new Uint32Array(3) }], shape: {} } as unknown as Packed; + plants.update([8.4, 46.8], '2026-07-01', 0, reach); + answers.splice(0).forEach(answer => answer([shape, shape, shape])); + await new Promise(resolve => setTimeout(resolve)); + expect(specs.every(spec => spec.tiers.length === 1)).toBe(true); + plants.update([8.4, 46.8], '2026-01-20', 0, reach); + answers.splice(0).forEach(answer => answer(undefined)); + await new Promise(resolve => setTimeout(resolve)); + expect([specs.every(spec => !spec.tiers.length), plants.nearIdle]).toEqual([true, true]); + }); }); diff --git a/builder/web/src/lib/trailview/vegetation/plants.ts b/builder/web/src/lib/trailview/vegetation/plants.ts index 60bbb40ade..a0e5189821 100644 --- a/builder/web/src/lib/trailview/vegetation/plants.ts +++ b/builder/web/src/lib/trailview/vegetation/plants.ts @@ -5,11 +5,10 @@ import { layouts } from '../gpu/layouts'; import { wgsl } from '../gpu/wgsl'; import { COVER_ORDER } from '../ground/rules'; import { EYE, Instances, VIEWS, type ModelSpec, type Producer, type Surfaces } from '../instances/instances'; +import type { ModelFiles } from '../instances/model-files'; import { f16 } from '../instances/points'; -import { TREES } from '../models/library'; -import { PLANTS } from '../models/plant-library'; +import type { Models, Packed } from '../models/model-file'; import { bits, named, seasonOf, type Region, type Rules, type Season, type Vocabulary } from '../rules/regions'; -import type { BuiltMesh, PlantJob } from './plant-worker'; import { meets, type Reach, type TreeModels } from './tree-models'; /** As plants.wgsl: the units of a cell of each grid, the reaches in metres, the list slots per region, vec4 per region and shapes per ground plant. */ @@ -34,12 +33,12 @@ export function materialWord({ cover, shrubs = 1 }: Vocabulary['materials'][numb /** * The ground plants of a region that its list holds, what the pack says of each over its plant - * model's `ground`, with their words of plants.wgsl: CLUMPED and at bit 16 the salt of its stands, and + * model's `ground` (of `plants`), with their words of plants.wgsl: CLUMPED and at bit 16 the salt of its stands, and * the bits of the materials that it grows on. The salt comes from the species, so its stands stay put * whatever the shares of the other plants. */ -export function groundRules(region: Region, vocabulary: Vocabulary) { - return groundList(region.ground.filter(rule => rule.model in PLANTS).map(rule => ({ ...PLANTS[rule.model].ground, ...rule }))).map(rule => { +export function groundRules(region: Region, vocabulary: Vocabulary, plants: Models['plants']) { + return groundList(region.ground.filter(rule => rule.model in plants).map(rule => ({ ...plants[rule.model].ground, ...rule }))).map(rule => { const salt = [...rule.id].reduce((h, c) => (31 * h + c.charCodeAt(0)) % 997, 7), off = named(vocabulary, rule.off); return { ...rule, word: (rule.clumped ? CLUMPED : 0) | (salt << 16), materials: bits(named(vocabulary, rule.on).filter(code => !off.includes(code))) }; }); @@ -57,40 +56,39 @@ export function groundList(plants: T[]): T[] { /** * The ground plants and the shrubs about the eye (plants.wgsl): a producer of instances whose models - * come from the packs of the regions, built for the date's season once their region is in reach. A tree that stands as a shrub is - * a model of the forest's (tree-models.ts). + * come from the packs of the regions, loaded for the date's season once their region is in reach (models/model-file.ts). + * A tree that stands as a shrub is a model of the forest's (tree-models.ts). */ export class Plants implements Producer { private resources?: Resources; private readonly list: Model[] = []; private readonly indexes: number[] = []; - /** Per model of the list: the regions that plant it, and the seasons that its mesh was asked for and that it holds. */ + /** + * Per model of the list: the regions that plant it, and the seasons that its mesh was asked for, that it holds and whose + * file failed. A failed file (ModelFiles tried it again where that can help) counts as done. + */ private readonly users: Set[] = []; private readonly asked: (Season | undefined)[] = []; private readonly built: (Season | undefined)[] = []; + private readonly failed: (Season | undefined)[] = []; private readonly data: Uint32Array; - private readonly worker = new Worker(new URL('./plant-worker.ts', import.meta.url), { type: 'module' }); - /** The jobs in the worker by id: their season and their models of the list. */ - private readonly jobs = new Map(); + /** The files that load. */ + private loading = 0; private season?: Season; private reach: Reach = { reach: new Set(), near: new Set() }; - private next = 0; private surface?: GPUBindGroup; private features?: GPUBindGroup; private sides: [number, number] = [0, 0]; - /** Whether the last update changed what the plants draw, other than by the camera. */ - newContent = false; - /** An abort of `stop` ends the worker. */ - constructor(rules: Rules, trees: TreeModels, private readonly instances: Instances, private readonly wake: () => void, stop: AbortSignal) { - stop.addEventListener('abort', () => this.worker.terminate()); + constructor(rules: Rules, trees: TreeModels, private readonly instances: Instances, private readonly files: ModelFiles) { instances.produce(this); + const { plants } = files.models; const { materials } = rules.vocabulary; const data = new Uint32Array(64 + rules.regions.length * REGION_WORDS * 4).fill(NONE), floats = new Float32Array(data.buffer); materials.forEach((material, code) => (data[code] = materialWord(material))); rules.regions.forEach((region, r) => { const at = 64 + r * REGION_WORDS * 4; - const ground = groundRules(region, rules.vocabulary).flatMap(rule => { + const ground = groundRules(region, rules.vocabulary, plants).flatMap(rule => { const tie = rule.own ? OWN_TIE : TIE; const first = this.models(r, Array.from({ length: SEEDS }, (_, k) => ({ model: rule.model, seed: k + 1, evergreen: false, shrub: false, tie }))); return first === undefined ? [] : [{ ...rule, first }]; @@ -101,10 +99,10 @@ export class Plants implements Producer { floats[w + 2] = rule.share; data.set([...rule.materials, 0, 0], w + 4); }); - const shrubs = region.trees.filter(rule => rule.cover === 'shrub' && (rule.model in PLANTS || rule.model in TREES)).sort((a, b) => b.weight - a.weight).slice(0, SLOTS).flatMap(rule => { - const plant = rule.model in PLANTS; + const shrubs = region.trees.filter(rule => rule.cover === 'shrub' && (rule.model in plants || rule.model in files.models.trees)).sort((a, b) => b.weight - a.weight).slice(0, SLOTS).flatMap(rule => { + const plant = rule.model in plants; const model = plant ? this.models(r, [{ model: rule.model, seed: 1, evergreen: !!rule.evergreen, shrub: true, tie: OWN_TIE }]) : trees.index(r, rule.model, rule.evergreen, OWN_TIE); - return model === undefined ? [] : [{ ...rule, index: model, height: plant ? PLANTS[rule.model].ground.height : SHRUB_HEIGHT_M }]; + return model === undefined ? [] : [{ ...rule, index: model, height: plant ? plants[rule.model].ground.height : SHRUB_HEIGHT_M }]; }); shrubs.forEach((rule, k) => { const w = at + 4 * (2 * SLOTS + k); @@ -113,27 +111,15 @@ export class Plants implements Producer { }); }); this.data = data; - this.worker.onmessage = ({ data: message }: MessageEvent<{ id: number; built?: BuiltMesh[]; error?: string }>) => { - const job = this.jobs.get(message.id)!; - this.jobs.delete(message.id); - if (message.error || !message.built) return void console.warn(`Plant models: ${message.error}`); - if (job.season !== this.season) return; - job.models.forEach((i, k) => { - this.instances.replace(this.indexes[i], spec(this.list[i], message.built![k])); - this.built[i] = job.season; - }); - this.newContent = true; - this.wake(); - }; } get idle(): boolean { - return this.jobs.size === 0 && this.list.every((_, i) => this.asked[i] === this.season || !this.plants(i, this.reach.reach)); + return this.loading === 0 && this.list.every((_, i) => this.asked[i] === this.season || !this.plants(i, this.reach.reach)); } - /** Whether the models of the near regions hold their meshes of the season. */ + /** Whether the models of the near regions hold their meshes of the season; a file that failed counts as done. */ get nearIdle(): boolean { - return this.list.every((_, i) => this.built[i] === this.season || !this.plants(i, this.reach.near)); + return this.list.every((_, i) => this.built[i] === this.season || this.failed[i] === this.season || !this.plants(i, this.reach.near)); } /** The first of SEEDS models of a plant that stands in rows (rows.ts) of region `region`, in its own colour; ask before the first update. */ @@ -180,21 +166,21 @@ export class Plants implements Producer { * metres over the ground. */ update(lonLat: [number, number], date: string, above: number, reach: Reach): void { - this.newContent = false; - const r = this.resources; - if (!r) return; const season = seasonOf(date), asking = season !== this.season || reach !== this.reach; this.season = season; this.reach = reach; - // A job for the near regions' models first, then one for the others in reach, each in the season. + // The near regions' models first, then the others in reach, each in the season. for (const regions of asking ? [reach.near, reach.reach] : []) { - const models = this.list.flatMap((_, i) => (this.asked[i] !== season && this.plants(i, regions) ? [i] : [])); - if (!models.length) continue; - models.forEach(i => (this.asked[i] = season)); - const id = this.next++; - this.jobs.set(id, { season, models }); - this.worker.postMessage({ id, models: models.map(i => ({ model: this.list[i].model, seed: this.list[i].seed, season: this.list[i].evergreen ? 'summer' : season })) } satisfies PlantJob); + for (const [i, entry] of this.list.entries()) { + if (this.asked[i] === season || !this.plants(i, regions)) continue; + this.asked[i] = season; + const url = this.files.models.plants[entry.model]?.seasons[entry.evergreen ? 'summer' : season]; + this.loading++; + void (url ? this.files.load(url) : Promise.resolve(undefined)).then(models => this.land(i, season, models?.[entry.seed - 1])); + } } + const r = this.resources; + if (!r) return; const [ux, uy] = worldPixel(lonLat, UNITS), unit = anchorAt(...lonLat).unit; // An eye higher over the ground than a grid's reach dispatches none of it. const window = (units: number, metres: number) => { @@ -207,6 +193,19 @@ export class Plants implements Producer { r.gpu.device.queue.writeBuffer(r.params, 0, params); } + /** Gives a model of the list its mesh of a season, while the season lasts; a model whose file failed draws nothing. */ + private land(i: number, season: Season, model: Packed | undefined): void { + this.loading--; + if (season !== this.season) return; + if (!model) { + this.instances.replace(this.indexes[i], { tiers: [] }); + this.built[i] = undefined; + return void (this.failed[i] = season); + } + this.instances.replace(this.indexes[i], spec(this.list[i], model)); + this.built[i] = season; + } + encode(pass: GPUComputePassEncoder, ground: GPUBindGroup, view: number): void { const r = this.resources; if (!r || !this.surface || !this.features) return; @@ -226,9 +225,9 @@ export class Plants implements Producer { } /** The tiers of a plant model. */ -function spec({ shrub, tie }: Model, mesh: BuiltMesh): ModelSpec { - if (shrub) return { tiers: [{ mesh, until: SHRUB_MESH_M }], impostor: { from: 0, until: SHRUB_M }, tie }; - return { tiers: [{ mesh, until: GROUND_M }], tie }; +function spec({ shrub, tie }: Model, { tiers: [mesh], shape }: Packed): ModelSpec { + if (shrub) return { tiers: [{ mesh, until: SHRUB_MESH_M }], shape, impostor: { until: SHRUB_M }, tie }; + return { tiers: [{ mesh, until: GROUND_M }], shape, tie }; } /** The ground plants and the shrubs about the eye. */ diff --git a/builder/web/src/lib/trailview/vegetation/rows.ts b/builder/web/src/lib/trailview/vegetation/rows.ts index e72c319629..40a70557cc 100644 --- a/builder/web/src/lib/trailview/vegetation/rows.ts +++ b/builder/web/src/lib/trailview/vegetation/rows.ts @@ -5,8 +5,7 @@ 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 { TREES } from '../models/library'; -import { PLANTS } from '../models/plant-library'; +import type { Models } from '../models/model-file'; import type { Region, Rules } from '../rules/regions'; import { SEEDS, type Plants } from './plants'; import type { TreeModels } from './tree-models'; @@ -48,7 +47,7 @@ export class Rows implements Producer { private readonly most: number; private side = 0; - constructor(config: PlannerConfig, rules: Rules, trees: TreeModels, plants: Plants, private readonly instances: Instances) { + constructor(config: PlannerConfig, rules: Rules, trees: TreeModels, plants: Plants, private readonly instances: Instances, models: Models) { instances.produce(this); // tools/trailview/rules.py checks that CROPS materials at most have rows. const crops = rules.vocabulary.materials.flatMap(({ rows }, code) => (rows ? [{ code, rows }] : [])).slice(0, CROPS); @@ -60,12 +59,12 @@ export class Rows implements Producer { }); const evergreen = (region: Region, model: string) => (region.trees.find(rule => rule.model === model) ?? rules.regions.flatMap(r => r.trees).find(rule => rule.model === model))?.evergreen; const word = (region: Region, r: number, model: string | undefined) => { - if (model && model in PLANTS) { + if (model && model in models.plants) { const first = plants.rowModel(r, model); return first === undefined ? NO_MODEL : first | ALONG | (SEEDS << SHAPES); } - const index = model && model in TREES ? trees.index(r, model, evergreen(region, model)) : undefined; - return index === undefined ? NO_MODEL : index | (TREES[model!].keepsHeading ? WINDWARD : 0); + const index = model && model in models.trees ? trees.index(r, model, evergreen(region, model)) : undefined; + return index === undefined ? NO_MODEL : index | (models.trees[model!].keepsHeading ? WINDWARD : 0); }; rules.regions.forEach((region, r) => crops.forEach(({ code, rows }, c) => { data[MODELS_AT + 4 * r + c] = word(region, r, region.rows[rules.vocabulary.materials[code].id] ?? rows.model); diff --git a/builder/web/src/lib/trailview/vegetation/tree-models.test.ts b/builder/web/src/lib/trailview/vegetation/tree-models.test.ts index da7fd89f5d..ef4131809a 100644 --- a/builder/web/src/lib/trailview/vegetation/tree-models.test.ts +++ b/builder/web/src/lib/trailview/vegetation/tree-models.test.ts @@ -1,136 +1,92 @@ -import { afterEach, describe, expect, it, vi } from 'vitest'; -import { FakeCaches } from '../../../../test-support/fake-caches'; +import { describe, expect, it } from 'vitest'; import type { Instances, ModelSpec } from '../instances/instances'; -import type { ModelAnswer, ModelJob } from './model-worker'; -import { IDLE_MS, TreeModels, type Reach } from './tree-models'; +import type { ModelFiles } from '../instances/model-files'; +import type { Models, Packed, TreeEntry } from '../models/model-file'; +import { TreeModels, type Reach } from './tree-models'; -// Workers that keep their jobs, which the test answers. -const jobs: { job: ModelJob; answer: (data: Omit) => void }[] = []; -class FakeWorker { - static live = 0; - onmessage?: (event: { data: ModelAnswer }) => void; - constructor() { - FakeWorker.live++; - } - postMessage(job: ModelJob) { - jobs.push({ job, answer: data => this.onmessage!({ data: { id: job.id, ...data } }) }); - } - terminate() { - FakeWorker.live--; - } -} -const built = (crown: number) => ({ tiers: [], crown }); +/** A species whose crown of its own in winter (phase 0) is a file, or none. */ +const species = (bare: boolean): TreeEntry => ({ summer: 'summer', keepsHeading: false, tones: [], crowns: [bare ? 'winter' : null, null, null, null] }); +const built = (crown: number): Packed => ({ tiers: [], shape: { extent: 1, radius: 1, spread: 1, trunk: false, crown, sinks: [], outlines: new Float32Array(256) } }); + +/** The loads that the files started, which a test answers: a model, or undefined for a file that failed. */ +const loads: { file: string; answer: (model: Packed | undefined) => Promise }[] = []; +const files = (trees: Record) => ({ + models: { trees: Object.fromEntries(Object.entries(trees).map(([model, entry]) => [model, { ...entry, summer: `${model}/summer`, crowns: entry.crowns.map(crown => crown && `${model}/${crown}`) }])) } as unknown as Models, + load: (file: string) => new Promise(resolve => loads.push({ file, answer: model => (resolve(model && [model]), new Promise(done => setTimeout(done))) })), +}) as unknown as ModelFiles; -function trees(near = new Map(), version?: string, reach: Reach = { reach: new Set([0]), near: new Set([0]) }) { - vi.stubGlobal('Worker', FakeWorker); - FakeWorker.live = 0; - jobs.length = 0; +function trees(kinds: Record, near = new Map(), reach: Reach = { reach: new Set([0]), near: new Set([0]) }) { + loads.length = 0; const specs: ModelSpec[] = []; const instances = { add: (spec: ModelSpec) => specs.push(spec) - 1, replace: (m: number, spec: ModelSpec) => (specs[m] = spec), day: 0 }; - const models = new TreeModels(instances as unknown as Instances, () => {}, () => near, new AbortController().signal, version); + const models = new TreeModels(instances as unknown as Instances, () => near, files(kinds), 3); return { models, specs, reach, beech: models.index(0, 'beech')! }; } describe('the tree models', () => { - afterEach(() => (vi.unstubAllGlobals(), vi.restoreAllMocks(), vi.useRealTimers())); - - it('draws a winter start bare from the summer builds', () => { - const { models, specs, reach, beech } = trees(); + it('draws a winter start bare from the summer crown, then from its own crown', async () => { + const { models, specs, reach, beech } = trees({ beech: species(true) }); models.update('2026-01-20', reach); - expect(jobs.map(({ job }) => job)).toEqual([{ id: 0, leafy: [{ model: 'beech' }], crowns: [] }]); - // The summer build of beech and its winter crown both are `beech|0`. - jobs[0].answer({ leafy: [{ ...built(0.1), tones: [], own: [true] }], crowns: [] }); - expect(jobs[1].job).toEqual({ id: 1, leafy: [], crowns: [{ model: 'beech', phase: 0 }] }); - jobs[1].answer({ leafy: [], crowns: [built(0.2)] }); + expect(loads.map(({ file }) => file)).toEqual(['beech/summer']); + await loads[0].answer(built(0.1)); + expect(loads[1].file).toBe('beech/winter'); + await loads[1].answer(built(0.2)); models.update('2026-01-20', reach); const winter = specs[beech].phases![0].model; - expect([specs[beech].crown, winter === beech ? undefined : specs[winter].crown, jobs.length]).toEqual([0.1, 0.2, 2]); - }); - - it('ends a worker after IDLE_MS with no job, and starts one again for the next build', () => { - vi.useFakeTimers(); - const { models, reach } = trees(); - models.update('2026-07-01', reach); - jobs[0].answer({ leafy: [{ ...built(0.1), tones: [], own: [] }], crowns: [] }); - vi.advanceTimersByTime(IDLE_MS - 1); - expect(FakeWorker.live).toBe(1); - vi.advanceTimersByTime(1); - expect(FakeWorker.live).toBe(0); - models.index(0, 'oak'); - models.update('2026-07-01', { ...reach }); - expect([FakeWorker.live, jobs[1].job.leafy[0].model]).toEqual([1, 'oak']); + expect([specs[beech].shape?.crown, winter === beech ? undefined : specs[winter].shape?.crown, loads.length]).toEqual([0.1, 0.2, 2]); }); - it('gives the free workers the species with the most trees about the eye first', () => { - const { models, reach } = trees(new Map([['larch', 0.2], ['spruce', 0.7]])); - for (const model of ['beech', 'oak', 'larch', 'spruce']) models.index(0, model); + it('loads the species with the most trees about the eye first, a few at once', async () => { + const { models, reach } = trees({ beech: species(false), oak: species(false), larch: species(false), spruce: species(false) }, new Map([['larch', 0.2], ['spruce', 0.7]])); + for (const model of ['oak', 'larch', 'spruce']) models.index(0, model); models.update('2026-07-01', reach); - expect(jobs.map(({ job }) => job.leafy[0].model)).toEqual(['spruce', 'larch', 'beech']); - jobs[1].answer({ leafy: [{ ...built(0.1), tones: [], own: [] }], crowns: [] }); - expect(jobs.map(({ job }) => job.leafy[0].model)).toEqual(['spruce', 'larch', 'beech', 'oak']); + expect(loads.map(({ file }) => file)).toEqual(['spruce/summer', 'larch/summer', 'beech/summer']); + await loads[1].answer(built(0.1)); + expect(loads.map(({ file }) => file)).toEqual(['spruce/summer', 'larch/summer', 'beech/summer', 'oak/summer']); }); - it('builds only the species of the regions in reach, those of the near regions first, and is near idle once these are in', () => { - const reach = { reach: new Set([0, 1]), near: new Set([1]) }, { models } = trees(new Map([['beech', 0.9]]), undefined, reach); + it('loads only the species of the regions in reach, those of the near regions first, and is near idle once these are in', async () => { + const reach = { reach: new Set([0, 1]), near: new Set([1]) }, { models } = trees({ beech: species(false), larch: species(false), spruce: species(false), oak: species(false) }, new Map([['beech', 0.9]]), reach); models.index(1, 'larch'); models.index(1, 'spruce'); models.index(2, 'oak'); models.update('2026-07-01', reach); - expect(jobs.map(({ job }) => job.leafy[0].model)).toEqual(['larch', 'spruce', 'beech']); - jobs[0].answer({ leafy: [{ ...built(0.1), tones: [], own: [] }], crowns: [] }); - jobs[1].answer({ leafy: [{ ...built(0.1), tones: [], own: [] }], crowns: [] }); + expect(loads.map(({ file }) => file)).toEqual(['larch/summer', 'spruce/summer', 'beech/summer']); + await loads[0].answer(built(0.1)); + await loads[1].answer(built(0.1)); models.update('2026-07-01', reach); - expect([models.nearIdle, models.idle, jobs.length]).toEqual([true, false, 3]); + expect([models.nearIdle, models.idle, loads.length]).toEqual([true, false, 3]); models.update('2026-07-01', { ...reach, reach: new Set([0, 1, 2]) }); - expect(jobs.map(({ job }) => job.leafy[0].model)).toEqual(['larch', 'spruce', 'beech', 'oak']); + expect(loads.map(({ file }) => file)).toEqual(['larch/summer', 'spruce/summer', 'beech/summer', 'oak/summer']); }); - it('is near idle in winter only once the near trees draw their own bare crowns', () => { - const reach = { reach: new Set([0, 1]), near: new Set([0]) }, { models, specs, beech } = trees(new Map(), undefined, reach); + it('is near idle in winter only once the near trees draw their own bare crowns', async () => { + const reach = { reach: new Set([0, 1]), near: new Set([0]) }, { models, specs, beech } = trees({ beech: species(true), oak: species(false) }, new Map(), reach); models.index(1, 'oak'); models.update('2026-01-20', reach); - expect(jobs.map(({ job }) => job.leafy[0].model)).toEqual(['beech', 'oak']); - jobs[0].answer({ leafy: [{ ...built(0.1), tones: [], own: [true] }], crowns: [] }); - jobs[2].answer({ leafy: [], crowns: [built(0.2)] }); + expect(loads.map(({ file }) => file)).toEqual(['beech/summer', 'oak/summer']); + await loads[0].answer(built(0.1)); + await loads[2].answer(built(0.2)); models.update('2026-01-20', reach); - // Built, but the far species' summer crown still runs: the bare crown has no instance model yet. + // Loaded, but the far species' summer crown still loads: the bare crown has no instance model yet. expect([models.nearIdle, specs[beech].phases![0].model === beech]).toEqual([false, true]); - jobs[1].answer({ leafy: [{ ...built(0.3), tones: [], own: [] }], crowns: [] }); + await loads[1].answer(built(0.3)); models.update('2026-01-20', reach); - expect([models.nearIdle, specs[specs[beech].phases![0].model].crown]).toEqual([true, 0.2]); + expect([models.nearIdle, specs[specs[beech].phases![0].model].shape?.crown]).toEqual([true, 0.2]); }); - it('asks again for the models of a job that failed', () => { - vi.spyOn(console, 'warn').mockImplementation(() => {}); - const { models, reach } = trees(); + it('counts a file that failed as done, and draws the bare crowns of the others once it fails', async () => { + const { models, specs, reach, beech } = trees({ beech: species(true), oak: species(false) }); + models.index(0, 'oak'); + models.update('2026-01-20', reach); + await loads[0].answer(built(0.1)); + await loads[2].answer(built(0.2)); models.update('2026-01-20', reach); - jobs[0].answer({ error: 'lost' }); - // Not in the next frame: when the next job lands or the date's phases change. + expect(specs[beech].phases![0].model).toBe(beech); + await loads[1].answer(undefined); models.update('2026-01-20', reach); - expect([jobs.length, models.idle]).toEqual([1, true]); models.update('2026-07-01', reach); - expect(jobs.map(({ job }) => job.leafy)).toEqual([[{ model: 'beech' }], [{ model: 'beech' }]]); - }); - - it('takes a build from the device cache as the worker made it, and builds again for another version', async () => { - vi.stubGlobal('caches', new FakeCaches()); - const tier = (k: number) => ({ position: Float32Array.of(k, 0.5, 1 / 3), normal: Float32Array.of(0, 0, 1), color: Float32Array.of(0.1, 0.2, k), index: Uint32Array.of(0, 0, 0), trunk: k }); - // A summer crown with gaps in its tones and in its phases of their own, as a worker makes it. - const leafy = { tiers: [tier(1), tier(2), tier(3)], crown: 0.125, tones: [[1, 0, 0, 0, 1, 0, 0, 0, 1.5], undefined, undefined, [0.5, 0, 0, 0, 1, 0, 0, 0, 1]], own: [true, false, undefined, false] as boolean[] }; - const load = async (version: string) => { - const { models, specs, reach, beech } = trees(undefined, version); - models.update('2026-08-15', reach); - for (let i = 0; i < 50 && !jobs.length && !models.idle; i++) await new Promise(resolve => setTimeout(resolve, 2)); - jobs[0]?.answer({ leafy: [leafy], crowns: [] }); - // The engine saves what the workers built once the view is idle. - models.save(); - await new Promise(resolve => setTimeout(resolve, 20)); - models.update('2026-08-15', reach); - return { asked: jobs.length, spec: specs[beech] }; - }; - const made = await load('a'), again = await load('a'), other = await load('b'); - expect([made.asked, again.asked, other.asked, made.spec.tiers.length]).toEqual([1, 0, 1, 3]); - // toStrictEqual compares the classes of the typed arrays too. - expect(again.spec).toStrictEqual(made.spec); + models.update('2026-01-20', reach); + expect([loads.length, models.idle, models.nearIdle, specs[specs[beech].phases![0].model].shape?.crown]).toEqual([3, true, true, 0.2]); }); }); diff --git a/builder/web/src/lib/trailview/vegetation/tree-models.ts b/builder/web/src/lib/trailview/vegetation/tree-models.ts index 993ad288d3..e1b48614ea 100644 --- a/builder/web/src/lib/trailview/vegetation/tree-models.ts +++ b/builder/web/src/lib/trailview/vegetation/tree-models.ts @@ -1,9 +1,8 @@ import { dayOfYear } from '../ground/rules'; import type { Instances, ModelSpec } from '../instances/instances'; +import type { ModelFiles } from '../instances/model-files'; +import type { Packed } from '../models/model-file'; import { SEASONS } from '../rules/regions'; -import { CAPS, DeviceCache } from '../terrain/device-cache'; -import { modelOf, partsOf } from './model-entry'; -import type { BuiltLeafy, BuiltModel, ModelAnswer, ModelJob } from './model-worker'; import { phasesOn } from './phenology'; /** Trees stand within this many metres of the eye: the stamps of main roads reach 4 km. */ @@ -13,10 +12,6 @@ export const TREE_REACH_M = 4500; * tree of another height switches where it draws as large. Impostors follow to the reach. */ const TIERS_M = [60, 300, 700], TREE_SIZE_M = 25; -/** Workers that build the models side by side, a species or a crown per job: the trees show as each job lands. */ -const WORKERS = 3; -/** A worker ends after this long with no job, and the next build starts one again: a still view keeps no worker. */ -export const IDLE_MS = 10_000; /** An instance model of a species, the models of its phases that draw a crown of their own, and the regions whose trees take it. */ interface Entry { index: number; own: (number | undefined)[]; regions: Set } @@ -27,40 +22,32 @@ export function meets(a: Set, b: Set): boolean { for (const region of a) if (b.has(region)) return true; return false; } -/** A build: a species' summer crown, kept green all year or not, or a crown of a species in a phase. */ -interface Build { leafy: { model: string; evergreen: boolean }[]; crowns: { model: string; phase: number }[] } +/** A file to load: a species' summer crown, or a crown of a species in a phase. */ +interface Job { model: string; phase?: number } /** - * The tree models (model-worker.ts), one build per species for the forest, the shrubs and the - * plantings, of the species of the regions in reach. A tree draws the summer crown, which its phase on the date (phenology.ts) colours, or a - * crown of the phase's own, such as a bare one, once the workers have built it for a phase that the - * date has; until then the summer crown in the phase's colours. A free worker takes the next build: - * summer crowns before crowns of their own, each with the models of the near regions and then about the eye first. Built models - * stay, so a change of date builds only what it has not seen. A species that a pack keeps green all - * year stays in summer. A model with a tie to the ground's hue (draw.wgsl), such as a tree that stands - * as a shrub, is an instance model of its own on the same meshes. With the version of the model worker's code, - * a build comes from the device cache where it holds it; what the workers built goes there once the view is idle - * (`save`), so a load does not wait for the writes. + * The tree models (models/model-file.ts), one file per species for the forest, the shrubs and the plantings, of the + * species of the regions in reach. A tree draws the summer crown, which its phase on the date (phenology.ts) colours, or a + * crown of the phase's own, such as a bare one, once its file has arrived for a phase that the date has; until then the + * summer crown in the phase's colours. At most `loads` files load at once: summer crowns before crowns of their own, each + * with the models of the near regions and then about the eye first. Loaded models stay, so a change of date loads only + * what it has not seen. A file that failed (ModelFiles tried it again where that can help) counts as done. A species that a pack keeps green all year stays in summer. A model with a tie to the ground's hue + * (draw.wgsl), such as a tree that stands as a shrub, is an instance model of its own on the same meshes. */ export class TreeModels { /** Instance models by `model|evergreen|tie`. */ private readonly entries = new Map(); - /** The live workers, and the free ones among them with the timer that ends each. */ - private readonly workers = new Set(); - private readonly free = new Map>(); - /** The jobs that run, by id. */ - private readonly jobs = new Map(); - /** The built summer crowns by `model|evergreen`, the built crowns of a phase by `model|phase`, what is asked for (`leafy:` or `crown:` and the key), and the builds that wait for a worker. */ - private readonly leafy = new Map(); - private readonly crowns = new Map(); + /** The summer crowns by model, and the crowns of a phase by `model|phase`, that arrived. */ + private readonly leafy = new Map(); + private readonly crowns = new Map(); + /** The jobs asked for (`model` or `model|phase`), those that failed, and those that wait for a load. */ private readonly asked = new Set(); - /** What a failed job asked for: `ask` asks for it again, when the next job lands or the date's phases change. */ private readonly failed = new Set(); - private waiting: Build[] = []; + private waiting: Job[] = []; + private loading = 0; /** The phases that the date has. */ private phases = new Set(); private day?: number; - private next = 0; /** Whether the instances lack models that arrived, or the date's phases: the next update installs them, once a frame. */ private stale = false; /** Whether models or the date changed in the last update. */ @@ -68,35 +55,30 @@ export class TreeModels { /** The regions in reach at the last update, and the builds of a set of regions (`builds`). */ private wanted: Reach = { reach: new Set(), near: new Set() }; private buildsOf = new WeakMap, string[]>(); - private readonly cache?: Promise; - /** Builds whose device cache entry is being read, and the entries that the workers built and the cache lacks. */ - private reading = 0; - private readonly built: [string, Uint32Array[]][] = []; - /** `near` gives the models about the eye, each with its share of the trees there. An abort of `stop` ends the workers. */ - constructor(private readonly instances: Instances, private readonly wake: () => void, private readonly near: () => Map, private readonly stop: AbortSignal, version?: string) { - stop.addEventListener('abort', () => { - this.free.forEach(timer => clearTimeout(timer)); - this.workers.forEach(worker => worker.terminate()); - }); - if (version) this.cache = DeviceCache.open('trees', '', version, CAPS.trees).catch(() => undefined); - } + /** `near` gives the models about the eye, each with its share of the trees there. */ + constructor(private readonly instances: Instances, private readonly near: () => Map, private readonly files: ModelFiles, private readonly loads: number) {} get idle(): boolean { - return this.jobs.size === 0 && this.waiting.length === 0 && this.reading === 0 && !this.stale && this.builds().every(build => this.asked.has(`leafy:${build}`) || this.failed.has(`leafy:${build}`)); + return this.loading === 0 && this.waiting.length === 0 && !this.stale && this.builds().every(build => this.asked.has(build.split('|')[0])); } /** * Whether the trees of the near regions draw their final models: each has its summer crown, and the crown of its own of - * each of the date's phases that needs one is built and installed. + * each of the date's phases that needs one is installed. A file that failed counts as done. */ get nearIdle(): boolean { if (this.stale) return false; for (const [key, entry] of this.entries) { if (!meets(entry.regions, this.wanted.near)) continue; - const [model, evergreen] = key.split('|'), leafy = this.leafy.get(`${model}|${evergreen}`); - if (!leafy) return false; - for (const phase of this.phases) if (leafy.own[phase] && (!this.crowns.has(`${model}|${phase}`) || entry.own[phase] === undefined)) return false; + const [model, evergreen] = key.split('|'); + if (this.failed.has(model)) continue; + if (!this.leafy.has(model)) return false; + if (evergreen === '1') continue; + for (const phase of this.phases) { + const crown = `${model}|${phase}`; + if (this.ownIn(model, phase) && !this.failed.has(crown) && (!this.crowns.has(crown) || entry.own[phase] === undefined)) return false; + } } return true; } @@ -118,7 +100,7 @@ export class TreeModels { return entry.index; } - /** Takes the date and the regions in reach: the trees change phase at once, and the workers build what they lack. */ + /** Takes the date and the regions in reach: the trees change phase at once, and the files that they lack load. */ update(date: string, reach: Reach): void { this.newContent = false; const day = dayOfYear(date); @@ -134,7 +116,7 @@ export class TreeModels { } if (reach !== this.wanted) { this.wanted = reach; - if (this.builds().some(build => !this.asked.has(`leafy:${build}`) && !this.failed.has(`leafy:${build}`))) this.ask(); + if (this.builds().some(build => !this.asked.has(build.split('|')[0]))) this.ask(); } if (!this.stale) return; this.stale = false; @@ -142,150 +124,78 @@ export class TreeModels { this.install(); } - /** Takes a job's models; a job that failed is asked for again when the next job lands or the date's phases change. */ - private land(worker: Worker, { id, leafy, crowns, error }: ModelAnswer): void { - const job = this.jobs.get(id)!; - this.jobs.delete(id); - this.rest(worker); - if (error || !leafy || !crowns) { - for (const key of [...job.leafy.map(({ model, evergreen }) => `leafy:${model}|${evergreen ? 1 : 0}`), ...job.crowns.map(({ model, phase }) => `crown:${model}|${phase}`)]) { - this.asked.delete(key); - this.failed.add(key); - } - this.post(); - return void console.warn(`Tree models: ${error}`); - } - if (this.cache) this.built.push([keyOf(job), partsOf(leafy[0] ?? crowns[0])]); - this.take(job, leafy, crowns); - } - - /** Whether the workers built models that the device cache lacks. */ - get unsaved(): boolean { - return this.built.length > 0; - } - - /** Writes the models that the workers built to the device cache. */ - save(): void { - for (const [key, parts] of this.built.splice(0)) void this.cache?.then(cache => cache?.write(key, parts)).catch(() => {}); - } - - /** Keeps the models of a job, from a worker or from the device cache. */ - private take(job: Build, leafy: BuiltLeafy[], crowns: BuiltModel[]): void { - // A species kept green all year draws the summer crown in every phase. - job.leafy.forEach(({ model, evergreen }, i) => this.leafy.set(`${model}|${evergreen ? 1 : 0}`, evergreen ? { ...leafy[i], tones: [], own: [] } : leafy[i])); - job.crowns.forEach(({ model, phase }, i) => this.crowns.set(`${model}|${phase}`, crowns[i])); - this.stale = true; - this.ask(); - this.wake(); + /** Whether a species draws a crown of its own in a phase. */ + private ownIn(model: string, phase: number): boolean { + return !!this.files.models.trees[model]?.crowns[phase]; } /** Queues the summer crowns not yet asked for, and the crowns of their own that the date's phases need. */ private ask(): void { - this.failed.clear(); - const leafy = this.builds().filter(build => !this.asked.has(`leafy:${build}`)).map(build => { - this.asked.add(`leafy:${build}`); - const [model, evergreen] = build.split('|'); - return { model, evergreen: evergreen === '1' }; - }); - const crowns = [...this.leafy].flatMap(([build, { own }]) => [...this.phases].filter(phase => own[phase]).map(phase => `${build.split('|')[0]}|${phase}`)) - .filter(crown => !this.asked.has(`crown:${crown}`)).map(crown => { - this.asked.add(`crown:${crown}`); - const [model, phase] = crown.split('|'); - return { model, phase: Number(phase) }; - }); - const jobs = [...leafy.map(build => ({ leafy: [build], crowns: [] })), ...crowns.map(crown => ({ leafy: [], crowns: [crown] }))]; - // Each build reads the device cache at once, so a warm device takes all its models in a frame or two; a miss waits for a worker. - for (const job of jobs) { - if (!this.cache) { - this.waiting.push(job); - continue; - } - this.reading++; - void this.cache.then(cache => cache?.read(keyOf(job))).catch(() => undefined).then(parts => { - this.reading--; - if (this.stop.aborted) return; - let model: BuiltModel | BuiltLeafy | undefined; - try { - model = parts && modelOf(parts); - } catch { - model = undefined; - } - // A miss, or an entry that holds no model, goes to a worker. - if (!model) { - this.waiting.push(job); - return this.post(); - } - this.take(job, job.leafy.length ? [model as BuiltLeafy] : [], job.leafy.length ? [] : [model]); - }); + const leafy = [...new Set(this.builds().map(build => build.split('|')[0]))].filter(model => !this.asked.has(model)).map((model): Job => ({ model })); + // A species that every pack keeps green all year needs no crown of its own. + const seasonal = new Set(this.builds().filter(build => build.endsWith('|0')).map(build => build.split('|')[0])); + const crowns = [...this.leafy.keys()].filter(model => seasonal.has(model)).flatMap(model => [...this.phases].filter(phase => this.ownIn(model, phase)).map((phase): Job => ({ model, phase }))) + .filter(job => !this.asked.has(`${job.model}|${job.phase}`)); + for (const job of [...leafy, ...crowns]) { + this.asked.add(keyOf(job)); + this.waiting.push(job); } this.post(); } /** - * Gives the free workers the waiting builds: summer crowns first, each with the models of the near regions first, then - * those that have the most trees about the eye. + * Starts the waiting loads while fewer than `loads` run: summer crowns first, each with the models of the near regions + * first, then those that have the most trees about the eye. */ private post(): void { - if (this.stop.aborted || this.jobs.size >= WORKERS || !this.waiting.length) return; // Crowns of phases that the date lost: `ask()` queues them again if the date comes back. - for (const { model, phase } of this.waiting.flatMap(job => job.crowns)) if (!this.phases.has(phase)) this.asked.delete(`crown:${model}|${phase}`); - this.waiting = this.waiting.filter(job => job.crowns.every(({ phase }) => this.phases.has(phase))); + for (const job of this.waiting) if (job.phase !== undefined && !this.phases.has(job.phase)) this.asked.delete(keyOf(job)); + this.waiting = this.waiting.filter(job => job.phase === undefined || this.phases.has(job.phase)); + if (this.loading >= this.loads || !this.waiting.length) return; const near = this.near(), close = new Set(this.builds(this.wanted.near).map(build => build.split('|')[0])); - const share = (job: Build) => near.get((job.leafy[0] ?? job.crowns[0]).model) ?? 0, first = (job: Build) => +close.has((job.leafy[0] ?? job.crowns[0]).model); - this.waiting.sort((a, b) => b.leafy.length - a.leafy.length || first(b) - first(a) || share(b) - share(a)); - for (const job of this.waiting.splice(0, WORKERS - this.jobs.size)) { - const id = this.next++; - this.jobs.set(id, job); - this.worker().postMessage({ id, leafy: job.leafy.map(({ model }) => ({ model })), crowns: job.crowns } satisfies ModelJob); + const share = (job: Job) => near.get(job.model) ?? 0, first = (job: Job) => +close.has(job.model); + this.waiting.sort((a, b) => +(a.phase !== undefined) - +(b.phase !== undefined) || first(b) - first(a) || share(b) - share(a)); + for (const job of this.waiting.splice(0, this.loads - this.loading)) { + const entry = this.files.models.trees[job.model], url = job.phase === undefined ? entry?.summer : entry?.crowns[job.phase]; + this.loading++; + void (url ? this.files.load(url) : Promise.resolve(undefined)).then(models => this.land(job, models?.[0])); } } - /** A free worker, or a new one. */ - private worker(): Worker { - for (const [worker, timer] of this.free) { - clearTimeout(timer); - this.free.delete(worker); - return worker; - } - const worker = new Worker(new URL('./model-worker.ts', import.meta.url), { type: 'module' }); - worker.onmessage = ({ data }: MessageEvent) => this.land(worker, data); - this.workers.add(worker); - return worker; - } - - /** Frees a worker, which ends after IDLE_MS unless a job takes it. */ - private rest(worker: Worker): void { - this.free.set(worker, setTimeout(() => { - this.free.delete(worker); - this.workers.delete(worker); - worker.terminate(); - }, IDLE_MS)); + /** Keeps a file's model. A failed file can settle the species in reach, so it installs the models too. */ + private land(job: Job, model: Packed | undefined): void { + this.loading--; + if (!model) this.failed.add(keyOf(job)); + else if (job.phase === undefined) this.leafy.set(job.model, model); + else this.crowns.set(keyOf(job), model); + this.stale = true; + this.ask(); } /** * Gives each species that has its summer crown its models: a phase that needs a crown of its own - * draws it once built, else the summer crown in the phase's colours. A crown of its own that differs + * draws it once loaded, else the summer crown in the phase's colours. A crown of its own that differs * only in colour draws the summer crown's impostor. Crowns of phases that the date lacks leave the pools. */ private install(): void { // Crowns of their own take their instance models in entry and phase order once every species in reach - // has its summer crown, so the indexes never hang on which worker answers first. - const settled = this.builds().every(build => this.leafy.has(build)); + // has its summer crown, so the indexes never hang on which file arrives first. + const settled = this.builds().every(build => this.leafy.has(build.split('|')[0]) || this.failed.has(build.split('|')[0])); for (const [key, entry] of this.entries) { - const [model, evergreen, tie] = key.split('|'), leafy = this.leafy.get(`${model}|${evergreen}`); + const [model, evergreen, tie] = key.split('|'), leafy = this.leafy.get(model), tones = this.files.models.trees[model]?.tones ?? []; if (!leafy) continue; - const spec = (crown: BuiltModel): ModelSpec => ({ - tiers: crown.tiers.map((mesh, t) => ({ mesh, until: TIERS_M[t] })), impostor: { from: 2, until: TREE_REACH_M }, crown: crown.crown, tie: Number(tie), sized: TREE_SIZE_M, + const spec = ({ tiers, shape }: Packed): ModelSpec => ({ + tiers: tiers.map((mesh, t) => ({ mesh, until: TIERS_M[t] })), shape, impostor: { until: TREE_REACH_M }, tie: Number(tie), sized: TREE_SIZE_M, }); const own = SEASONS.map((_, phase) => { - if (!this.phases.has(phase) || !leafy.own[phase]) return undefined; + if (evergreen === '1' || !this.phases.has(phase) || !this.ownIn(model, phase)) return undefined; if (settled) entry.own[phase] ??= this.instances.add({ tiers: [] }); return this.crowns.get(`${model}|${phase}`); }); entry.own.forEach((index, phase) => index !== undefined && this.instances.replace(index, own[phase] ? spec(own[phase]) : { tiers: [] })); this.instances.replace(entry.index, { ...spec(leafy), phases: evergreen === '1' ? undefined : SEASONS.map((_, phase) => { - const tone = leafy.tones[phase], model = own[phase] && entry.own[phase]; + const tone = tones[phase] ?? undefined, model = own[phase] && entry.own[phase]; return model === undefined ? { model: entry.index, tone } : { model, impostor: tone ? entry.index : model, tone }; }), }); @@ -300,5 +210,4 @@ export class TreeModels { } } -/** A build's key in the device cache: each build makes one model, and both kinds of a species' summer crown are one. */ -const keyOf = ({ leafy, crowns }: Build) => (leafy.length ? `leafy/${leafy[0].model}` : `crown/${crowns[0].model}/${crowns[0].phase}`); +const keyOf = ({ model, phase }: Job) => (phase === undefined ? model : `${model}|${phase}`); diff --git a/builder/web/vite.config.ts b/builder/web/vite.config.ts index 5907d0b504..c9f83c8807 100644 --- a/builder/web/vite.config.ts +++ b/builder/web/vite.config.ts @@ -2,6 +2,7 @@ import { availableParallelism, loadavg } from "node:os"; import { svelte } from "@sveltejs/vite-plugin-svelte"; import { defineConfig, loadEnv } from "vite"; import { bundleVersions } from "./vite/bundle-version"; +import { modelFiles } from "./vite/model-files"; import { stampVersion } from "./vite/stamp-bundle"; import { thirdPartyLicenses } from "./vite/third-party-licenses"; import { plannerConfig } from "./src/lib/planner/config"; @@ -30,7 +31,7 @@ export default defineConfig(({ mode }) => { // base "./" keeps every asset URL relative, so a built page works under any sub-path. base: "./", // The licence notices ride along with the bundle, which is a distribution. - plugins: [svelte(), thirdPartyLicenses(), stampVersion(), bundleVersions()], + plugins: [svelte(), thirdPartyLicenses(), stampVersion(), bundleVersions(), modelFiles()], // Always a string, so a build without it drops the pretend device of `planner.ts` with its wasm. define: { "import.meta.env.VITE_FAKE_DEVICE": JSON.stringify(process.env.VITE_FAKE_DEVICE ?? "") }, resolve: { diff --git a/builder/web/vite/bundle-version.ts b/builder/web/vite/bundle-version.ts index 6513b673cd..5cd646a71c 100644 --- a/builder/web/vite/bundle-version.ts +++ b/builder/web/vite/bundle-version.ts @@ -1,7 +1,7 @@ // `virtual:NAME-version`: the version of the JavaScript that a worker runs to make what the device cache keeps, the // start of the SHA-256 of its entries bundled with every import inlined. `plan`: the plan worker's input preparation, // with the page decode that it imports, and the input walk; the cells also key by the hash of the bridge's wasm, which -// the bundle only names by URL. `tree`: the tree models and their entry layout. `house`: the house records. The stamp +// the bundle only names by URL. `house`: the house records. The stamp // version stays in stamp-bundle.ts: the data pipeline digests that file as the stamp bake's own (`stamp_sources`). import { createHash } from "node:crypto"; @@ -12,7 +12,6 @@ import { build, type Plugin } from "vite"; const ROOT = fileURLToPath(new URL("..", import.meta.url)); const ENTRIES: Record = { plan: ["src/lib/trailview/plan/plan-worker.ts", "src/lib/trailview/plan/inputs.ts"], - tree: ["src/lib/trailview/vegetation/model-worker.ts", "src/lib/trailview/vegetation/model-entry.ts"], house: ["src/lib/trailview/structures/house-worker.ts"], }; diff --git a/builder/web/vite/model-files.ts b/builder/web/vite/model-files.ts new file mode 100644 index 0000000000..edf2376765 --- /dev/null +++ b/builder/web/vite/model-files.ts @@ -0,0 +1,88 @@ +// `virtual:models`: the model files of TrailView (specs/trailview-models.md) and where each lies. The plugin bundles +// `models/bake.ts` for Node with every import inlined, runs it, and serves what it returns: a build emits each file as an +// asset with the hash of its content in its name; the dev server serves the files itself and makes them again when a +// module of the bundle changes, when a TrailView module is added or removed, or on any change after a bake that failed. + +import { createHash } from "node:crypto"; +import { mkdtemp, rm, writeFile } from "node:fs/promises"; +import { tmpdir } from "node:os"; +import path from "node:path"; +import { fileURLToPath, pathToFileURL } from "node:url"; +import { build, type Plugin } from "vite"; +import type { Baked } from "../src/lib/trailview/models/bake"; + +const ROOT = fileURLToPath(new URL("..", import.meta.url)); +const ENTRY = "src/lib/trailview/models/bake.ts"; +const ID = "virtual:models"; +/** Where the dev server serves the files. */ +const PREFIX = "/@trailview-models/"; +/** Marks a file name in the manifest's JSON, which the module replaces with the file's address. */ +const MARK = /"@@model-file:([\w-]+)@@"/g; +/** The modules that a change makes the files again for, after a bake that failed. */ +const SOURCES = path.join(ROOT, "src/lib/trailview"); + +/** The files and the models, and the files of the modules in the bundle. */ +async function bakeModels(): Promise<{ baked: Baked; modules: string[] }> { + const out = await build({ + configFile: false, root: ROOT, logLevel: "silent", ssr: { noExternal: true }, + build: { ssr: ENTRY, write: false, minify: false, rollupOptions: { output: { inlineDynamicImports: true } } }, + }); + const [chunk] = (Array.isArray(out) ? out[0] : out as { output: { code: string; moduleIds: string[] }[] }).output; + const dir = await mkdtemp(path.join(tmpdir(), "trailview-models-")); + try { + const file = path.join(dir, "bake.mjs"); + await writeFile(file, chunk.code); + const { bake } = await import(pathToFileURL(file).href) as { bake: () => Promise }; + return { baked: await bake(), modules: chunk.moduleIds }; + } finally { + await rm(dir, { recursive: true, force: true }); + } +} + +export function modelFiles(): Plugin { + let made: ReturnType | undefined, serving = false; + // A bake that fails leaves no files: the next request or change bakes again. + const bake = () => (made ??= bakeModels().catch(error => { + made = undefined; + throw error; + })); + const resolved = `\0${ID}`; + return { + name: "trailview-models", + configResolved: config => void (serving = config.command === "serve"), + resolveId: source => (source === ID ? resolved : undefined), + async load(id) { + if (id !== resolved) return undefined; + const { baked: { files, models } } = await bake(); + const json = JSON.stringify(models, (_, value) => (typeof value === "string" && files.has(value) ? `@@model-file:${value}@@` : value)); + return `export default ${json.replace(MARK, (_, name: string) => { + const bytes = files.get(name)!; + if (serving) return JSON.stringify(`${PREFIX}${name}?v=${createHash("sha256").update(bytes).digest("hex").slice(0, 12)}`); + return `import.meta.ROLLUP_FILE_URL_${this.emitFile({ type: "asset", name: `${name}.bin`, source: bytes })}`; + })};`; + }, + configureServer(server) { + server.middlewares.use(PREFIX, (request, response, next) => { + void bake().then(({ baked: { files } }) => { + const bytes = files.get((request.url ?? "").slice(1).split("?")[0]); + if (!bytes) return next(); + response.setHeader("Content-Type", "application/octet-stream"); + response.end(bytes); + }, next); + }); + // A changed module of the bake, or any added or removed TrailView module (a new tree or plant file of a glob) + // makes the files again. + const again = async (file: string, added: boolean) => { + const module = server.moduleGraph.getModuleById(resolved); + if (!module) return; + const modules = added ? undefined : await made?.then(({ modules }) => modules, () => undefined); + if (modules ? !modules.includes(path.resolve(file)) : !path.resolve(file).startsWith(SOURCES)) return; + made = undefined; + server.moduleGraph.invalidateModule(module); + server.ws.send({ type: "full-reload" }); + }; + server.watcher.on("change", file => void again(file, false)); + for (const event of ["add", "unlink"] as const) server.watcher.on(event, file => void again(file, true)); + }, + }; +} diff --git a/builder/web/vite/versions.d.ts b/builder/web/vite/versions.d.ts index 763595411d..8a916c21be 100644 --- a/builder/web/vite/versions.d.ts +++ b/builder/web/vite/versions.d.ts @@ -3,11 +3,13 @@ declare module "virtual:plan-version" { const version: string; export default version; } -declare module "virtual:tree-version" { - const version: string; - export default version; -} declare module "virtual:house-version" { const version: string; export default version; } + +// The model files of the build, from modelFiles of model-files.ts. +declare module "virtual:models" { + const models: import("../src/lib/trailview/models/model-file").Models; + export default models; +} diff --git a/docs/content/datenschutz.md b/docs/content/datenschutz.md index fa33a7135d..508157f650 100644 --- a/docs/content/datenschutz.md +++ b/docs/content/datenschutz.md @@ -226,7 +226,7 @@ wenn die Tabelle unten angibt, dass sie den Browser verlassen. | Daten | Zweck und Dauer | | --- | --- | | Selbst gespeicherte Skins mit Namen und Farben | Nur nach Betätigung von „Save“ im Farbeditor der Seite für den Kartenbereich; bis zur Löschung über die vorhandene Löschfunktion oder durch Löschen der Website-Daten. | -| Daten der 3D-Ansicht TrailView (Cache Storage `trailview-*`): Geländezellen, Hausdaten, Baummodelle und Stempel, die TrailView aus den Kartenkacheln berechnet | Gespeichert, wenn TrailView eine Region darstellt, auch für das Titelbild einer Route oder Fahrt, damit TrailView sie bei einem späteren Besuch nicht erneut berechnet. Jede Art hat eine Größengrenze, zusammen 296 MiB; darüber werden die am längsten nicht genutzten Einträge gelöscht. Die Daten einer anderen Kartenversion oder einer anderen Version von TrailView werden gelöscht, wenn TrailView seinen Speicher öffnet; bis dahin oder bis zum Löschen der Website-Daten. | +| Daten der 3D-Ansicht TrailView (Cache Storage `trailview-*`): Geländezellen, Hausdaten und Stempel, die TrailView aus den Kartenkacheln berechnet, sowie die Dateien der Bäume, Pflanzen, Tiere und kleinen Objekte, die es mit der Website herunterlädt | Gespeichert, wenn TrailView eine Region darstellt, auch für das Titelbild einer Route oder Fahrt, damit TrailView sie bei einem späteren Besuch nicht erneut berechnet oder herunterlädt. Jede Art hat eine Größengrenze, zusammen 280 MiB; darüber werden die am längsten nicht genutzten Einträge gelöscht. Die Daten einer anderen Kartenversion oder einer anderen Version von TrailView werden gelöscht, wenn TrailView seinen Speicher öffnet; die Dateien der Modelle bleiben, bis die Größengrenze sie löscht. Bis dahin oder bis zum Löschen der Website-Daten. | | Kartenzellen und Arbeitsdateien im OPFS | Für große Karten während Download und Zusammensetzen erforderlich. Die Zellen und die zusammengesetzte Karte werden nach dem Lauf gelöscht. Sortierdateien werden am Laufende entfernt; Dateien eines abgebrochenen Laufs spätestens beim Beginn des nächsten Laufs. | | Bibliothek des Routenplaners (IndexedDB `obc-library`): Routen, nicht gespeicherte Änderungen einer Route, Fahrten mit ihren Dateien und Fotos sowie Titelbilder | Der Routenplaner speichert eine Route, wenn „Save“ gewählt wird, und eine Fahrt beim Hinzufügen, damit die Bibliothek in diesem Browser bleibt. Nicht gespeicherte Änderungen einer Route behält er als Entwurf in diesem Browser, bis die Route gespeichert, „Discard changes“ gewählt, ihr Hinweis mit „ד geschlossen oder die Route gelöscht wird; auch das Speichern derselben Route in einem anderen Tab löscht den Entwurf. Eine Sicherungsdatei enthält keine Entwürfe. Im `sessionStorage` unter `obc-draft` behält ein Tab die Kennung der Route, deren Entwurf er schreibt, damit ein Neuladen die Änderungen fortsetzt; der Browser löscht sie mit dem Tab. Zu einem Foto, das einer Fahrt hinzugefügt wird, speichert er eine verkleinerte Kopie mit der Aufnahmezeit oder, wenn die Zeit keinen Ort auf der Fahrt ergibt, mit dem GPS-Ort des Fotos; die Originaldatei speichert er nicht. Eine selbst gespeicherte Sicherungsdatei enthält diese Kopien. Gespeichert bis zum Löschen, bis „Delete all“ auf der Seite „Settings“ gewählt wird oder bis die Website-Daten gelöscht werden. Zu jeder Route und Fahrt, die von einem Gerät kam oder an eines ging, speichert die Bibliothek auch die Kennung der Speicherkarte des Geräts. Sie verlässt den Browser nur in einer selbst gespeicherten Sicherungsdatei, als Suchanfrage nach Abschnitt 7.1 und bei der Synchronisation: Der Routenplaner sendet Routen über USB an das verbundene Gerät (Abschnitt 10), und bevor er eine Route sendet, sendet er ihre Wegpunkte an die Routenberechnung nach Abschnitt 7.1. | | Gemerktes Gerät (`obc-device`): Seriennummer des Geräts, Kennung seiner Speicherkarte, Zeitpunkt der letzten Synchronisation, Name, Objektkennung und Bereich der Karte auf der Speicherkarte, Kennungen und Versionen der Farben (Kartenstile) einer von diesem Browser gesendeten Karte, Anzahl der Routen und belegte Bytes auf der Speicherkarte, Anzahl der am selben Tag übernommenen Fahrten sowie die Kennungen der Fahrten auf dem Gerät, die die Bibliothek nicht übernimmt | Erst nach Betätigung von „Connect“ auf der Seite „Device“, damit die Synchronisation bei einem späteren Besuch mit diesem Gerät arbeitet. Jede Synchronisation aktualisiert die Angaben. Gespeichert, bis „Connect“ mit einem anderen Gerät betätigt wird oder eine Synchronisation eine andere Speicherkarte findet, die sie ersetzt, oder bis die Website-Daten gelöscht werden. „Delete all“ auf der Seite „Settings“ löscht sie nicht. | diff --git a/docs/content/privacy.md b/docs/content/privacy.md index c5f3404ee4..22ff31c208 100644 --- a/docs/content/privacy.md +++ b/docs/content/privacy.md @@ -215,7 +215,7 @@ the device that you use, except where the table below says that it leaves the br | Data | Purpose and duration | | --- | --- | | Skins you save yourself, with names and colors | Only after you press "Save" in the colour editor of the area page. Kept until you delete them with the delete function or delete the website data. | -| Data of the 3-D view TrailView (Cache Storage `trailview-*`): terrain cells, house records, tree models and stamps that TrailView calculates from the map tiles | Saved when TrailView draws a region, also for the cover image of a route or a ride, so that TrailView does not calculate them again at a later visit. Each kind has a size limit, together 296 MiB; past it, the entries that were used least recently are deleted. The data of another map release or of another version of TrailView is deleted when TrailView opens its store. Kept until then or until you delete the website data. | +| Data of the 3-D view TrailView (Cache Storage `trailview-*`): terrain cells, house records and stamps that TrailView calculates from the map tiles, and the files of the trees, plants, animals and small objects that it downloads with the website | Saved when TrailView draws a region, also for the cover image of a route or a ride, so that TrailView does not calculate or download them again at a later visit. Each kind has a size limit, together 280 MiB; past it, the entries that were used least recently are deleted. The data of another map release or of another version of TrailView is deleted when TrailView opens its store; the files of the models stay until the size limit deletes them. Kept until then or until you delete the website data. | | Map cells and working files in OPFS | Needed for large maps during download and assembly. The cells and the assembled map are deleted after the run. Sort files are removed at the end of the run. Files that a stopped run leaves are removed at the start of the next run at the latest. | | Library of the route planner (IndexedDB `obc-library`): routes, the changes of a route that you did not save, rides with their files and their photos, and cover images | The planner saves a route when you press "Save", and a ride when you add it, so that your library stays in this browser. It keeps the changes of a route that you did not save as a draft in this browser until you save the route, choose "Discard changes", dismiss its reminder with "×" or delete the route; a save of the same route in another tab also deletes the draft. A backup file does not hold drafts. In `sessionStorage` under `obc-draft`, a tab keeps the id of the route whose draft it writes, so that a reload continues the changes; the browser deletes it with the tab. For a photo that you add to a ride, it saves a smaller copy with the time of the photo, or with the GPS place of the photo when the time gives no place on the ride; it does not save the original file. A backup file that you save holds these copies. Kept until you delete it, use "Delete all" on the Settings page, or delete the website data. For each route and ride that came from a device or went to it, the library also keeps the id of the memory card of the device. It leaves the browser only in a backup file that you save, as a search request under section 7.1, and with the device sync: the route planner sends routes over USB to the device that you connected (section 10), and before it sends a route, it sends its waypoints to the route calculation under section 7.1. | | Remembered device (`obc-device`): the serial number of the device, the id of its memory card, the time of the last sync, the name, the object id and the area of the map on the card, the ids and the versions of the colours (map styles) of a map that this browser sent, the number of routes and the bytes on the card, the number of rides that came from it on the current day, and the ids of the rides on it that the library does not take | Only after you press "Connect" on the Device page, so that the sync works with this device at a later visit. Each sync updates it. Kept until you press "Connect" with another device, or a sync finds another memory card, which replaces it, or until you delete the website data. "Delete all" on the Settings page does not delete it. | diff --git a/docs/design/trailview.md b/docs/design/trailview.md index 98db64b641..41f10086b8 100644 --- a/docs/design/trailview.md +++ b/docs/design/trailview.md @@ -16,6 +16,7 @@ owns: - builder/web/vite/stamp-bundle.ts - builder/web/vite/stamp-version.d.ts - builder/web/vite/bundle-version.ts + - builder/web/vite/model-files.ts - builder/web/vite/versions.d.ts - builder/web/test-support/trailview/** - builder/web/test-support/fake-caches.ts diff --git a/specs/trailview-models.md b/specs/trailview-models.md new file mode 100644 index 0000000000..f70d5468ef --- /dev/null +++ b/specs/trailview-models.md @@ -0,0 +1,100 @@ +# TrailView model files + +TrailView draws each model of one fixed shape (a tree species in a season phase, a plant, a rock, an animal, +a bench or another small object) many times. The web build makes these models and writes them as model +files beside the bundle. The engine fetches a file and writes its bytes into the instance pools as they are. +This contract defines the bytes of a file, the manifest that names the files, and the budget that each +model keeps, whoever makes it. + +- Version: file `version` `1`. A reader refuses a file of another version or with another magic number. +- Writers: `builder/web/src/lib/trailview/models/bake.ts`, which `builder/web/vite/model-files.ts` runs + in the build. +- Readers: `decodeModels` in `builder/web/src/lib/trailview/models/model-file.ts`. +- Code authority: `models/model-file.ts` (the layout and `quantize`), `models/pack.ts` (the values that a + file holds of each model), `models/bake.ts` (the files and the budget). + +## Conventions + +- All numbers are little-endian. `u32` is an unsigned 32-bit integer, `f32` an IEEE 754 single. +- A model is 1 tall in model units, with its base at the origin and z up. +- "Coded" means the meshopt codec of the `meshoptimizer` package: `encodeVertexBuffer` with a stride + of 16 bytes, and `encodeIndexSequence` with 4-byte indices, which keeps the order of the indices. + +## 1. Files and the manifest + +The build emits each file as an asset with the hash of its content in its name, so a changed model +has a new address. The module `virtual:models` of the build is the manifest (`Models` in +`model-file.ts`): the address of each file, and what the engine needs before a file arrives. + +| File | Models, in order | Manifest entry | +| --- | --- | --- | +| A tree species' summer crown | 1, with 3 tiers | `trees[id].summer`, with `keepsHeading` and, per phase (winter, spring, summer, autumn), the colour matrix (3 × 3, columns first) that turns the summer crown into the phase's, or null | +| A tree species' crown of its own in a phase | 1, with 3 tiers | `trees[id].crowns[phase]`, null for a phase without one | +| A plant in a season | 3 shapes (seeds 1, 2, 3), 1 tier each | `plants[id].seasons[season]`, with the plant's `ground` | +| A breed of animal | 1 per coat | `breeds[name].file`, with `height` and `coats` | +| The objects | the rocks (3 slabs, 3 boulders), then the models of the kinds in the order of `POINT_MODELS` | `objects.file`, with `rocks` and per kind model the end of each tier in metres (`until`) and `casts` | + +## 2. File layout + +| Field | Type | Value | +| --- | --- | --- | +| magic | `u32` | `0x464d5654` (`TVMF`) | +| version | `u32` | `1` | +| models | `u32` | the count of models | +| heads | | one head per model, below | +| blobs | | per model, per tier and then for its proxy: the coded vertices, then the coded indices | + +A head: + +| Field | Type | Value | +| --- | --- | --- | +| extent | `f32` | the largest absolute coordinate of all its tiers | +| radius | `f32` | the radius of a sphere about (0, 0, 0.5) that holds all its tiers | +| spread | `f32` | the farthest that a vertex lies from the z axis | +| crown | `f32` | a tree's lowest crown point, NaN for a model without a crown | +| sinks | 8 × `f32` | a tree's sink table (`slope_sink` of `emit.wgsl`), zeros without a crown | +| flags | `u32` | bit 0: a tier has a trunk; bit 1: the model has a shadow proxy | +| tiers | `u32` | the count of mesh tiers, finest first, 3 at most | +| counts | 2 × `u32` per tier, and for the proxy | the vertex count and the index count | +| outlines | 256 × `f32` | per impostor frame (8 × 8) the trapezoid that holds the last tier: bottom, top, half widths | + +A blob is a `u32` byte length, then the bytes, padded with zeros to a multiple of 4. So a file is a +multiple of 4 bytes long. + +## 3. Vertices + +A vertex is 16 bytes, as the instance pipeline reads it: + +| Bytes | Type | Value | +| --- | --- | --- | +| 0–5 | 3 × snorm16 | the position over `extent` | +| 6–7 | int16 | for a vertex of the trunk, the trunk's top in model units × 32767; else 0 | +| 8–10 | 3 × snorm8 | the normal | +| 11 | int8 | 127 for a vertex that rises at dusk, else 0 | +| 12–14 | 3 × unorm8 | the colour in sRGB | +| 15 | unorm8 | 255 − 255 × its glow at night | + +Indices are three per triangle. The proxy is a mesh of 6 vertices in this format, packed over the extent as the head holds it (rounded to float32). + +## 4. Model budget + +Each model keeps the budget of its kind, and the build fails on a model that does not. A model that +appears in small numbers on screen may have several times the triangles of a tree's far tiers; trees and plants, +which fill the screen, keep their counts. `BUDGET` in `models/bake.ts` holds the numbers and says +where each comes from. + +| Model | Tier | Triangles, at most | Vertices, at most | +| --- | --- | --- | --- | +| Tree | 0 | 4,700 | 9,000 | +| Tree | 0, the mean of the summer crowns of all species | 3,400 | | +| Tree | 1 | 900 | 1,800 | +| Tree | 2 | 230 | 600 | +| Plant, each shape | 0 | 350 | 1,000 | +| Rock | 0 | 100 | 300 | +| Animal | 0 | 1,000 (the owner's choice: few on screen) | 3,000 | +| Animal, a second tier where it has one | 1 | 250 | 750 | +| Small object of a kind | 0 | 600 (the owner's choice: few on screen) | 1,800 | + +One view draws at most 256 instance models, each with at most 3 mesh tiers and an impostor. The +same files serve the desktop and the phone; a phone draws nearer tier ends through the budget table +of the engine.