diff --git a/builder/web/src/lib/trailview/README.md b/builder/web/src/lib/trailview/README.md index 6305831c24..175dad5ff3 100644 --- a/builder/web/src/lib/trailview/README.md +++ b/builder/web/src/lib/trailview/README.md @@ -38,10 +38,10 @@ 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). The build writes the fixed models as files (`bake.ts`, [contract](../../../../../specs/trailview-models.md)) | +| `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, crosses); the modelling kit of the soft low-poly style (`sculpt.ts`) and the herd animals, one file per breed (`animals.ts`, `animals/`). 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`) | +| `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 `showroom` address option, which stands a row of animals before the eye for the model review; the rocks' density and placement, which the ground also paints past their instances (`rocks.wgsl`) | | `light/` | The light keys and weather presets, the look at the eye, `shade.wgsl` (toon bands, haze, `finish()`), the sky with the moon, the stars and the Milky Way, and a solar eclipse where the moon covers the sun (`celestial.ts`, `eclipse.ts`, `night.ts`; `tools/trailview/sky.py` makes `milkyway.webp`, `stars.bin` and `moon.webp`), the object shadow maps near and far from the eye (`shadow.ts`) | | `resolve/` | The render targets, the jitter, TAA and `finish()` | | `plan/` | The ground plan with `ground=plan`: the cells about the eye and their inputs (`plan.ts`, `inputs.ts`), the build in the bridge of [`builder/ground-plan-wasm`](../../../../ground-plan-wasm/README.md), one copy per page (`bridge.ts`, `plan-worker.ts`, `pkg/`), the debug draw over the canvas (`debug.ts`, `draw.ts`, `debug.wgsl`) | diff --git a/builder/web/src/lib/trailview/dressing/dressing.ts b/builder/web/src/lib/trailview/dressing/dressing.ts index a6e75437bd..bcb11fb8be 100644 --- a/builder/web/src/lib/trailview/dressing/dressing.ts +++ b/builder/web/src/lib/trailview/dressing/dressing.ts @@ -7,20 +7,42 @@ import { Instances, VIEWS, type ModelSpec, type Producer, type Surfaces } from ' import type { ModelFiles } from '../instances/model-files'; import type { Packed } from '../models/model-file'; import { bits, named, type Rules } from '../rules/regions'; +import type { Reach } from '../vegetation/tree-models'; /** 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; +/** An animal draws its near tier to this many metres (specs/trailview-models.md). */ +const ANIMAL_NEAR_M = 150; /** 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. */ -const HERD_WORDS = 8, DRESSING_BYTES = 48; +const HERD_WORDS = 8, DRESSING_BYTES = 64; -interface Resources { gpu: Gpu; herds: GPUComputePipeline; rocks: GPUComputePipeline; groups: GPUBindGroup[]; params: GPUBuffer } +/** + * The showroom (`showroom=lat,lon,heading,entries[,tier]` in the address), the engine view of the model review: in place + * of the herds, a row of animals across the view toward `heading` (degrees from north) at (lat, lon), SHOW_SPACING + * times their heights apart, each facing right and turned SHOW_TURN toward the eye. An entry is a breed, which stands all its models, or + * `breed.k`, its model k. With `tier` 1 every animal draws its far tier at any distance. + */ +interface Showroom { lonLat: [number, number]; heading: number; entries: string[]; tier: number } +const SHOW_SPACING = 2.3, SHOW_TURN = 0.45; + +function showroom(): Showroom | undefined { + const value = globalThis.location ? new URLSearchParams(globalThis.location.search).get('showroom') : null; + if (!value) return undefined; + const [lat, lon, heading, entries, tier] = value.split(','); + if (!entries) return undefined; + return { lonLat: [Number(lon), Number(lat)], heading: (Number(heading) * Math.PI) / 180, entries: entries.split('|'), tier: Number(tier ?? 0) }; +} + +interface Resources { gpu: Gpu; herds: GPUComputePipeline; rocks: GPUComputePipeline; groups: GPUBindGroup[]; params: GPUBuffer; table: GPUBuffer } /** * 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. Their models come from - * model files (models/model-file.ts): the rocks from the objects file, each breed from a file of its own. + * model files (models/model-file.ts): the rocks from the objects file, each breed from a file of its own. A + * region's herds take their models when the region comes into reach: all breeds of all packs would not fit + * in the instances' models. */ export class Dressing implements Producer { private resources?: Resources; @@ -28,29 +50,66 @@ export class Dressing implements Producer { private readonly rockModel?: number; private herdSide = 0; private rockSide = 0; + private readonly show = showroom(); + /** The first entry of the showroom's animals in the table, and their count. */ + private showFirst = 0; + private showCount = 0; /** The files of the models. */ private readonly urls: string[] = []; + /** The first instance model of each breed that has its models, and the regions whose herds are in the table. */ + private readonly breeds = new Map(); + private readonly placed = new Set(); + private changed = false; + private updated = false; - constructor(rules: Rules, private readonly instances: Instances, private readonly files: ModelFiles) { + constructor(private readonly rules: Rules, private readonly instances: Instances, private readonly files: ModelFiles) { instances.produce(this); 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 = 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 = 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); + // The showroom's animals: an entry of a breed stands each of its models. + const row = (this.show?.entries ?? []).map(entry => entry.split('.')).filter(([name]) => this.breed(name) !== undefined) + .flatMap(([name, k]) => (k ? [Number(k)] : Array.from({ length: breedModels[name].coats * breedModels[name].poses }, (_, m) => m)).map(m => ({ name, model: this.breed(name)! + m }))); + this.showFirst = Math.max(1, rules.regions.length) * HERDS; + this.showCount = row.length; + this.table = new Uint32Array((this.showFirst + row.length) * HERD_WORDS); const floats = new Float32Array(this.table.buffer); - rules.regions.forEach((region, r) => { - region.herds.filter(herd => breeds.has(herd.model)).slice(0, HERDS).forEach((herd, k) => { - const at = (r * HERDS + k) * HERD_WORDS; - this.table.set([breeds.get(herd.model)!, 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); + if (this.show) { + const across = -this.show.heading, room = row.map(({ name }) => SHOW_SPACING * breedModels[name].height); + let offset = -room.reduce((sum, r) => sum + r, 0) / 2; + row.forEach(({ name, model }, k) => { + const at = (this.showFirst + k) * HERD_WORDS; + offset += room[k] / 2; + this.table[at] = model; + floats.set([across - Math.PI / 2 - SHOW_TURN, breedModels[name].height, offset * Math.cos(across), offset * Math.sin(across)], at + 2); + offset += room[k] / 2; }); + } + } + + /** The first instance model of a breed, which it adds on the first ask; undefined where the instances have no room. */ + private breed(name: string): number | undefined { + const entry = this.files.models.breeds[name]; + if (!entry) return undefined; + if (!this.breeds.has(name)) { + const near = this.show?.tier ? [] : [ANIMAL_NEAR_M]; + this.breeds.set(name, this.load(entry.file, entry.coats * entry.poses, ({ tiers, shape }) => ({ tiers: [...near, HERD_REACH_M].map((until, t) => ({ mesh: tiers[t + 1 - near.length], until })), shape }))); + } + return this.breeds.get(name); + } + + /** Writes the herds of region `r` into the table, in the order of its pack: those whose breed has its models. */ + private place(r: number): void { + this.placed.add(r); + this.changed = true; + const floats = new Float32Array(this.table.buffer), { breeds } = this.files.models; + // Only the first HERDS herds with a model draw, so only theirs take instance models. + this.rules.regions[r].herds.filter(herd => herd.model in breeds).slice(0, HERDS).filter(herd => this.breed(herd.model) !== undefined).forEach((herd, k) => { + const at = (r * HERDS + k) * HERD_WORDS, { coats, poses, height } = breeds[herd.model]; + this.table.set([this.breed(herd.model)!, coats | (poses << 16)], at); + floats.set([herd.groups, height, ...herd.size], at + 2); + this.table.set(bits(named(this.rules.vocabulary, herd.on)), at + 6); }); } @@ -65,9 +124,9 @@ export class Dressing implements Producer { return first; } - /** Whether the models have arrived, or failed. */ + /** Whether the models of the regions in reach have arrived, or failed. */ get idle(): boolean { - return this.files.done(...this.urls); + return this.updated && this.files.done(...this.urls); } async attach(gpu: Gpu, { surface, features }: Surfaces): Promise { @@ -75,10 +134,10 @@ export class Dressing implements Producer { const { device } = gpu; const herds = gpu.buffer('dressing', this.table.byteLength, GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST); const params = gpu.buffer('dressing', DRESSING_BYTES, GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST); - device.queue.writeBuffer(herds, 0, this.table); + this.changed = true; const [herdsOf, rocksOf] = await gpu.once(dressingPipelines); this.resources = { - gpu, herds: herdsOf, rocks: rocksOf, params, + gpu, herds: herdsOf, rocks: rocksOf, params, table: herds, groups: VIEWS.map(view => device.createBindGroup({ layout: herdsOf.getBindGroupLayout(0), entries: [...this.instances.emitResources(view), { binding: 8, resource: { buffer: params } }, { binding: 9, resource: { buffer: herds } }] })), }; this.surface = surface; @@ -88,10 +147,14 @@ export class Dressing implements Producer { private surface?: GPUBindGroup; private features?: GPUBindGroup; - /** The windows of herd cells and rock points about the eye at (lon, lat). */ - update(lonLat: [number, number]): void { + /** The windows of herd cells and rock points about the eye at (lon, lat), and the herds of the regions in reach. */ + update(lonLat: [number, number], { reach }: Reach): void { const r = this.resources; if (!r) return; + for (const region of reach) if (!this.placed.has(region) && this.rules.regions[region]) this.place(region); + if (this.changed) r.gpu.device.queue.writeBuffer(r.table, 0, this.table); + this.changed = false; + this.updated = true; const [ux, uy] = worldPixel(lonLat, UNITS), unit = anchorAt(...lonLat).unit; this.herdSide = 2 * Math.ceil(HERD_REACH_M / (HERD_CELL * unit)) + 2; this.rockSide = 2 * Math.ceil(ROCK_REACH_M / (ROCK_CELL * unit)) + 2; @@ -101,6 +164,11 @@ export class Dressing implements Producer { ints.set([Math.floor(ux / ROCK_CELL) - this.rockSide / 2, Math.floor(uy / ROCK_CELL) - this.rockSide / 2], 4); words[6] = this.rockModel ?? 0; floats[8] = HERD_REACH_M; + if (this.show) { + const [sx, sy] = worldPixel(this.show.lonLat, UNITS); + ints.set([Math.round(sx), Math.round(sy)], 12); + words.set([this.showCount, this.showFirst], 14); + } r.gpu.device.queue.writeBuffer(r.params, 0, params); } diff --git a/builder/web/src/lib/trailview/dressing/dressing.wgsl b/builder/web/src/lib/trailview/dressing/dressing.wgsl index 3669725517..9341f5c83c 100644 --- a/builder/web/src/lib/trailview/dressing/dressing.wgsl +++ b/builder/web/src/lib/trailview/dressing/dressing.wgsl @@ -24,13 +24,20 @@ struct Dressing { herd_reach: f32, unused2: f32, unused3: vec2f, + // The showroom of dressing.ts, which stands its animals in place of the herds: its middle in world units, its + // animals, and the first of their entries in `herds`. + show_origin: vec2i, + show_count: u32, + show_first: u32, } -// A herd of a region, as dressing.ts writes it: its first model and coats, its groups per eye's circle, -// its breed's height, its animals per group, and the materials that it grazes as bits of their codes. +// A herd of a region, as dressing.ts writes it: its first model, its coats (low 16 bits) and poses (high 16 bits), +// its groups per eye's circle, its breed's height, its animals per group, and the materials that it grazes as bits of +// their codes. Its models are each coat in each pose. A showroom entry holds its model, its yaw in `groups` and its +// offset from the showroom's middle in metres east and north in `size`. struct Herd { model: u32, - coats: u32, + looks: u32, groups: f32, height: f32, size: vec2f, @@ -58,6 +65,20 @@ var cell_region: u32; @compute @workgroup_size(64) fn herds_of(@builtin(workgroup_id) group: vec3u, @builtin(local_invocation_index) lane: u32) { + if (dressing.show_count > 0u) { + if (all(group.xy == vec2u(0u))) { + for (var i = lane; i < dressing.show_count; i += 64u) { + let h = herds[dressing.show_first + i]; + let at = dressing.show_origin + vec2i(round(vec2f(h.size.x, -h.size.y) / surface.unit)); + let s = lod_space(vec2f(at - surface.anchor)); + let ground = stand_height(s, at); + if (known(ground)) { + emit(h.model, s, ground, h.height, h.groups, 1.0, 1.0, 1.0); + } + } + } + return; + } let cell = dressing.herd_origin + vec2i(group.xy); let size = 1 << HERD_SHIFT; let corner = cell << vec2u(HERD_SHIFT); @@ -98,9 +119,12 @@ fn herds_of(@builtin(workgroup_id) group: vec3u, @builtin(local_invocation_index if (!known(ground)) { continue; } - // Most of a group wears the breed's first coat. - let coat = select(1u + mix_bits(a + 7u) % max(herd.coats - 1u, 1u), 0u, unit_float(mix_bits(a + 8u)) < 0.6 || herd.coats == 1u); - emit(herd.model + coat, s, ground, herd.height * (0.85 + 0.3 * unit_float(mix_bits(a + 9u))), + // Most of a group wears the breed's first coat; the poses come about as often each. + let coats = herd.looks & 0xffffu; + let poses = herd.looks >> 16u; + let coat = select(1u + mix_bits(a + 7u) % max(coats - 1u, 1u), 0u, unit_float(mix_bits(a + 8u)) < 0.6 || coats == 1u); + let pose = mix_bits(a + 12u) % poses; + emit(herd.model + coat * poses + pose, s, ground, herd.height * (0.85 + 0.3 * unit_float(mix_bits(a + 9u))), 6.2831853 * unit_float(mix_bits(a + 10u)), 1.0, 0.92 + 0.16 * unit_float(mix_bits(a + 11u)), 1.0); } } diff --git a/builder/web/src/lib/trailview/engine.ts b/builder/web/src/lib/trailview/engine.ts index 1ca8068405..4117e671f5 100644 --- a/builder/web/src/lib/trailview/engine.ts +++ b/builder/web/src/lib/trailview/engine.ts @@ -711,7 +711,7 @@ export class TrailView { this.forest.update(lonLat); this.plants.update(lonLat, this.extras.date, place.above, reach); this.rows.update(lonLat); - this.dressing.update(lonLat); + this.dressing.update(lonLat, reach); const group = this.groundLook.group; this.drawing = !!(eye && group); // A frame that cannot draw asks for no next frame: a capture would wait forever. diff --git a/builder/web/src/lib/trailview/models/animals.ts b/builder/web/src/lib/trailview/models/animals.ts index 91e7e704b1..cf5ec485e5 100644 --- a/builder/web/src/lib/trailview/models/animals.ts +++ b/builder/web/src/lib/trailview/models/animals.ts @@ -1,71 +1,18 @@ -// Grazing animals for the herds, a low-poly rework of the prototype's src/models/animals/: one -// four-legged build whose proportions, coats, horns and patches make the breed. 1 m tall at the withers' -// top, feet at the origin, z up, facing +y; the instance scales it to the breed's height. -import { rgb } from '../structures/mesh'; -import { Model, random, shade, type Vec } from './flat'; +// The animals of the herds: one file per breed in animals/, whose name is the breed's id, by which packs name it. A breed +// builds each of its coats in each of its poses with the modelling kit (sculpt.ts): 1 m tall at the withers' top when +// it stands, feet at the origin, z up, facing +y; the instance scales it to the breed's height. +import type { Sculpt } from './sculpt'; -interface Breed { +export interface Animal { + name: string; + latin: string; /** Metres to the top of the back, which the instance scales to. */ height: number; - /** The body's length and its half-width and half-depth, the legs' length, the neck's rise and the head's length, as shares of the height. */ - body: [number, number, number]; - leg: number; - neck: number; - head: number; - /** Coats by seed: the body, and the belly, legs and head. */ - coats: [string, string][]; - /** Patches of the second colour over the body, a share of its faces. */ - patches?: number; - horns?: 'cow' | 'long' | 'curl' | 'goat'; - hooves: string; + coats: Coat[]; + /** The count of poses; the herds draw each about as often. */ + poses: number; + build(s: Sculpt, coat: Coat, pose: number): void; } -/** The breeds that the packs of data/trailview/packs name as herd models. */ -export const BREEDS: Record = { - 'brown-cattle': { height: 1.4, body: [1.45, 0.3, 0.32], leg: 0.52, neck: 0.12, head: 0.36, coats: [['#7e6a58', '#a8957e'], ['#8e4a24', '#b87444'], ['#ae8250', '#d4ae78']], horns: 'cow', hooves: '#2c2420' }, - 'dairy-cattle': { height: 1.45, body: [1.5, 0.29, 0.32], leg: 0.55, neck: 0.1, head: 0.36, coats: [['#e9e6de', '#1e1c1b'], ['#e9e6de', '#1e1c1b']], patches: 0.45, hooves: '#2c2420' }, - 'simmental-cattle': { height: 1.45, body: [1.45, 0.3, 0.32], leg: 0.52, neck: 0.1, head: 0.36, coats: [['#ece6da', '#a85428'], ['#ece6da', '#c88e4c']], patches: 0.5, horns: 'cow', hooves: '#2c2420' }, - 'highland-cow': { height: 1.3, body: [1.4, 0.34, 0.36], leg: 0.46, neck: 0.1, head: 0.38, coats: [['#b3561f', '#e09048'], ['#4a2a1a', '#7a4a2c'], ['#d0ae74', '#efd6a2']], horns: 'long', hooves: '#2c2420' }, - sheep: { height: 0.95, body: [1.15, 0.36, 0.38], leg: 0.5, neck: 0.14, head: 0.3, coats: [['#efe7d6', '#2d2828'], ['#e6dcc6', '#2d2828']], horns: 'curl', hooves: '#2e211b' }, - goat: { height: 1.05, body: [1.1, 0.24, 0.27], leg: 0.6, neck: 0.22, head: 0.3, coats: [['#2e241e', '#ece4d4'], ['#2a211c', '#5a4434'], ['#87482a', '#e6dccb']], horns: 'goat', hooves: '#2c2420' }, - horse: { height: 1.6, body: [1.35, 0.22, 0.27], leg: 0.62, neck: 0.42, head: 0.42, coats: [['#6a3418', '#1a1512'], ['#8a4018', '#6e3014'], ['#b8b6b2', '#6e6c6a'], ['#221f1e', '#141212']], hooves: '#2a2624' }, -}; - -/** An animal of a breed in one of its coats. */ -export function animal(name: string, seed = 0): Model { - const b = BREEDS[name], [length, half, deep] = b.body, rand = random(seed + 3); - const [coat, other] = b.coats[seed % b.coats.length].map(h => rgb(h)), m = new Model(); - const back = 1, belly = b.leg, mid = (back + belly) / 2, y0 = -length / 2, y1 = length / 2; - // The body: an octagon swept from rump to chest; patches paint some of its faces in the second colour. - const ring = (y: number, k: number): Vec[] => Array.from({ length: 8 }, (_, i) => { - const a = (i / 8) * 2 * Math.PI; - return [Math.cos(a) * half * k, y, mid + Math.sin(a) * (back - belly) / 2 * k]; - }); - const rings = [ring(y0, 0.75), ring(y0 + 0.15 * length, 1), ring(y1 - 0.2 * length, 1), ring(y1, 0.8)]; - rings.slice(1).forEach((r, k) => r.forEach((p, i) => { - const q = rings[k], j = (i + 1) % 8, low = Math.sin(((i + 0.5) / 8) * 2 * Math.PI) < -0.5; - m.poly([q[i], q[j], r[j], p], b.patches && rand() < b.patches ? other : low && !b.patches ? shade(coat, 0.85) : coat); - })); - m.poly(rings[0], coat).poly(rings[3], coat); - // Neck and head: the neck rises from the chest, the head hangs forward from it. - const neck: Vec = [0, y1 + 0.05, back + b.neck], snout: Vec = [0, y1 + 0.05 + b.head, back + b.neck - 0.25 * b.head]; - const face = b.patches ? other : name === 'sheep' ? other : coat; - m.beam([0, y1 - 0.05, mid + 0.05], neck, deep * 1.1, coat, { sides: 6, w1: deep * 0.8 }); - m.beam(neck, snout, deep * 0.8, face, { sides: 6, w1: deep * 0.5, capped: true }); - for (const s of [-1, 1]) m.box([s * deep * 0.5, neck[1] + 0.03, neck[2] + 0.06], [0.14 * b.head + 0.05, 0.04, 0.06], face); - // Legs, hooves and tail. - for (const [x, y] of [[-1, y0 + 0.12], [1, y0 + 0.12], [-1, y1 - 0.12], [1, y1 - 0.12]]) { - m.beam([x * half * 0.55, y, mid], [x * half * 0.55, y, 0.06], 0.09 + 0.05 * half, name === 'sheep' || b.patches ? other : coat, { sides: 4, w1: 0.07 }); - m.box([x * half * 0.55, y, 0.03], [0.1, 0.12, 0.06], rgb(b.hooves)); - } - m.beam([0, y0, back - 0.05], [0, y0 - 0.06, belly - 0.05], 0.05, coat, { sides: 3 }); - // Horns. - const horn = rgb('#e2d6b8'), top = neck[2] + 0.1; - if (b.horns) for (const s of [-1, 1]) { - const at: Vec = [s * deep * 0.35, neck[1] + 0.02, top]; - const tip: Vec = b.horns === 'long' ? [s * (deep + 0.45), at[1] + 0.05, top + 0.12] : b.horns === 'goat' ? [s * 0.12, at[1] - 0.22, top + 0.2] - : b.horns === 'curl' ? [s * (deep * 0.6), at[1] - 0.05, top - 0.12] : [s * (deep * 0.5 + 0.12), at[1] + 0.04, top + 0.12]; - m.beam(at, tip, 0.06, b.horns === 'goat' ? rgb('#6b5c4a') : horn, { sides: 3, w1: 0.02 }); - } - return m; -} +export const ANIMALS = Object.fromEntries(Object.entries(import.meta.glob('./animals/*.ts', { eager: true, import: 'default' })) + .map(([path, animal]) => [path.split('/').pop()!.replace(/\.ts$/, ''), animal as Animal])); diff --git a/builder/web/src/lib/trailview/models/animals/body/cattle.ts b/builder/web/src/lib/trailview/models/animals/body/cattle.ts new file mode 100644 index 0000000000..83448554d1 --- /dev/null +++ b/builder/web/src/lib/trailview/models/animals/body/cattle.ts @@ -0,0 +1,29 @@ +// The body that the cattle breeds share. +import { shade } from '../../flat'; +import { alongY, bellied, both, type Paint, type Sculpt } from '../../sculpt'; +import { CREAM, ears, eyes, HOOF, HORN, NOSE, PINK, quadruped, type Body, type Coat } from './quadruped'; + +const BODY: Body = { + trunk: [[-0.66, 0.74, 0.12, 0.14, 0.14], [-0.56, 0.72, 0.25, 0.25, 0.24], [-0.3, 0.72, 0.31, 0.28, 0.31], [0.05, 0.71, 0.32, 0.28, 0.33], [0.35, 0.73, 0.29, 0.27, 0.3], [0.52, 0.75, 0.22, 0.22, 0.24], [0.6, 0.76, 0.12, 0.13, 0.14]], + neck: { at: [0.38, 0.78], length: 0.4, size: [[0.2, 0.2, 0.22], [0.135, 0.14, 0.15]], carry: [[24, 20], [-30, -60], [35, 25]] }, + head: { rows: [[0, 0.1, 0.1, 0.1], [0.07, 0.165, 0.15, 0.15], [0.2, 0.17, 0.15, 0.16], [0.34, 0.13, 0.11, 0.13], [0.44, 0.125, 0.1, 0.12], [0.5, 0.09, 0.07, 0.08]], nod: [-30, -72, -25] }, + front: { x: 0.17, y: 0.36, top: 0.62, thigh: 0.1, bone: 0.075 }, + hind: { x: 0.18, y: -0.42, top: 0.62, thigh: 0.115, bone: 0.075 }, +}; + +export interface Cattle { coat: number; trunk?: Paint; neck?: number; head?: Coat['head']; shin?: number; ear?: number; tuft?: number; horns?: boolean } + +/** A cow of the plain build: a breed gives its colours, and the paints of a patched one. */ +export function cattle(s: Sculpt, pose: number, c: Cattle): void { + const q = quadruped(s, BODY, pose, { + trunk: c.trunk ?? bellied(c.coat, shade(c.coat, 0.85)), neck: c.neck ?? c.coat, + head: c.head ?? (d => (d > 1 ? NOSE : d > 0.7 ? CREAM : c.coat)), + leg: () => [c.coat, c.shin ?? c.coat, HOOF], + }), h = q.head; + ears(s, h, [0.1, 0.12, 0.03], [0.0, 0.38, -0.07], 0.19, c.ear ?? c.coat); + if (c.horns ?? true) both(side => s.chain([h.p(0.06, side * 0.1, 0.12), h.p(0.05, side * 0.2, 0.2), h.p(0.07, side * 0.22, 0.3)], [0.06, 0.045, 0.015], HORN)); + eyes(s, h, [0.21, 0.165, 0.05]); + s.fine(() => s.chain([[0, -0.66, 0.9], [0, -0.75, 0.6], [0, -0.75, 0.42]].map(q.body), [0.04, 0.03, 0.03], c.coat)); + s.fine(() => s.chain([q.body([0, -0.75, 0.44]), q.body([0, -0.75, 0.28])], [0.08, 0.03], c.tuft ?? NOSE)); + if (!q.lies) s.fine(() => s.skin(alongY([[-0.25, 0.46, 0.11, 0.13, 0.13, -90], [-0.25, 0.36, 0.09, 0.11, 0.11, -90], [-0.25, 0.33, 0.05, 0.06, 0.06, -90]]), 6, PINK)); +} diff --git a/builder/web/src/lib/trailview/models/animals/body/deer.ts b/builder/web/src/lib/trailview/models/animals/body/deer.ts new file mode 100644 index 0000000000..70364f3e29 --- /dev/null +++ b/builder/web/src/lib/trailview/models/animals/body/deer.ts @@ -0,0 +1,23 @@ +// The body and the antlers that the deer share. +import { both, type Sculpt } from '../../sculpt'; +import type { Body, quadruped } from './quadruped'; + +export const DEER: Body = { + trunk: [[-0.54, 0.8, 0.1, 0.11, 0.11], [-0.46, 0.79, 0.21, 0.2, 0.2], [-0.2, 0.78, 0.25, 0.22, 0.23], [0.1, 0.77, 0.25, 0.22, 0.24], [0.32, 0.78, 0.22, 0.22, 0.23], [0.45, 0.8, 0.16, 0.17, 0.18], [0.52, 0.81, 0.08, 0.09, 0.1]], + neck: { at: [0.34, 0.85], length: 0.48, size: [[0.16, 0.18, 0.2], [0.11, 0.125, 0.13], [0.095, 0.105, 0.105]], carry: [[66, 56], [-30, -55], [68, 58]] }, + head: { rows: [[0, 0.075, 0.08, 0.08], [0.06, 0.125, 0.115, 0.115], [0.18, 0.13, 0.115, 0.12], [0.32, 0.09, 0.08, 0.09], [0.42, 0.08, 0.065, 0.075], [0.46, 0.055, 0.045, 0.05]], nod: [-22, -72, -18], set: [0.05, 0.08] }, + front: { x: 0.13, y: 0.3, top: 0.68, thigh: 0.08, bone: 0.05 }, + hind: { x: 0.135, y: -0.38, top: 0.68, thigh: 0.095, bone: 0.05 }, +}; + +/** + * Both antlers from the crown frame: the beam through rows of [back, up, out], and tines as [the row of the beam they + * leave, back, up, out] of their tips. Tier 1 keeps the beam and the first tine. + */ +export function antlers(s: Sculpt, k: ReturnType['crown']>, beam: number[][], tines: number[][], colour: number, w = 0.055): void { + both(side => { + const at = ([back, up, out]: number[]) => k.p(-back, side * out, up), width = (i: number) => w * (1 - (0.8 * i) / (beam.length - 1)); + s.chain(beam.map(at), beam.map((_, i) => width(i)), colour, 3); + tines.forEach(([from, ...tip], i) => (i && s.tier) || s.chain([at(beam[from]), at(tip)], [0.85 * width(from), 0.012], colour, 3)); + }); +} diff --git a/builder/web/src/lib/trailview/models/animals/body/quadruped.ts b/builder/web/src/lib/trailview/models/animals/body/quadruped.ts new file mode 100644 index 0000000000..04baf30091 --- /dev/null +++ b/builder/web/src/lib/trailview/models/animals/body/quadruped.ts @@ -0,0 +1,122 @@ +// The body that every herd animal shares: a trunk, a bent neck, a head and four legs from the numbers of a breed, in a +// pose. The breed adds what makes it itself (ears, horns, a tail) at the frames this returns. +import { rgb } from '../../../structures/mesh'; +import type { Vec } from '../../flat'; +import { alongY, axis, both, through, type Paint, type Sculpt } from '../../sculpt'; + +/** The poses: it stands with its head up, it grazes, it lies with its legs folded. */ +export const STAND = 0, GRAZE = 1, LIE = 2; +export const EYE = rgb('#1c1614'), HOOF = rgb('#2c2420'), NOSE = rgb('#3a2e2a'), HORN = rgb('#e2d6b8'), PINK = rgb('#d8a79a'), CREAM = rgb('#e4d8c2'); +export const X: Vec = [1, 0, 0]; + +/** A pair of legs: x and y of the feet, the top inside the trunk, the half-width of the thigh and of the bone. */ +export interface Leg { x: number; y: number; top: number; thigh: number; bone: number } + +export interface Body { + /** Rows of [y, z, half-width, up, down] from the rump to the chest. */ + trunk: number[][]; + sides?: number; + wool?: number; + neck: { + /** [y, z] of its root in the chest. */ + at: number[]; + length: number; + /** Rows of [half-width, up, down] from the root to the head, spread evenly. */ + size: number[][]; + /** For each pose, the pitch of the neck at its root and at the head. */ + carry: number[][]; + }; + head: { + /** Rows of [d, half-width, up, down] from the poll to the nose. */ + rows: number[][]; + /** The pitch of the head for each pose. */ + nod: number[]; + /** How far the poll sits behind and above the end of the neck. */ + set?: number[]; + }; + front: Leg; + hind: Leg; +} + +export interface Coat { + trunk: Paint | number; + neck: Paint | number; + /** From how far along the head a band ends (0 to 1; the front cap is 2) and how far up its facet looks. */ + head: (d: number, top: number) => number; + /** The thigh, the shin and the hoof; the front pair when `front`. */ + leg: (front: boolean) => number[]; +} + +/** How deep a lying trunk sinks into the grass. */ +const SINK = 0.03; +/** The longest leg that folds at its full length: a longer one would stand out over the lying trunk. */ +const LONGEST_FOLD = 0.64; +const mix = (rows: number[][], t: number) => { + const at = t * (rows.length - 1), i = Math.min(rows.length - 2, Math.floor(at)); + return rows[i].map((v, k) => v + (rows[i + 1][k] - v) * (at - i)); +}; + +/** + * Builds the body and returns its frames: `head` (an axis from the poll), `crown(d, v)` (an axis from there on the head + * that is level when the animal stands: horns hang from it), `neck(t, v)` (the point v above the neck at its share t), + * `body` (a point of the standing trunk in this pose), `lies`. + */ +export function quadruped(s: Sculpt, b: Body, pose: number, coat: Coat) { + const lies = pose === LIE, far = s.tier > 0; + const drop = lies ? Math.min(...b.trunk.slice(1, -1).map(([, z, , , down]) => z - down)) + SINK : 0; + const body = ([x, y, z]: number[]): Vec => [x, y, z - drop]; + s.skin(alongY(b.trunk.map(([y, z, ...size]) => [y, z - drop, ...size])), b.sides ?? 10, coat.trunk, b.wool); + + const n = far ? 2 : 3, [from, to] = b.neck.carry[pose], bones: Vec[] = [[0, b.neck.at[0], b.neck.at[1] - drop]]; + for (let i = 0; i < n; i++) { + const a = ((from + ((to - from) * (i + 0.5)) / n) * Math.PI) / 180, [, y, z] = bones[i]; + bones.push([0, y + (b.neck.length / n) * Math.cos(a), z + (b.neck.length / n) * Math.sin(a)]); + } + const rings = through(bones, bones.map((_, i) => mix(b.neck.size, i / n))); + s.skin(rings, 8, coat.neck); + const neck = (t: number, v = 0, x = 0): Vec => { + const i = Math.min(n - 1, Math.floor(t * n)), k = t * n - i, p = (rings[i].pitch + (rings[i + 1].pitch - rings[i].pitch) * k) * (Math.PI / 180); + return [x, bones[i][1] + (bones[i + 1][1] - bones[i][1]) * k - Math.sin(p) * v, bones[i][2] + (bones[i + 1][2] - bones[i][2]) * k + Math.cos(p) * v]; + }; + + const nod = b.head.nod[pose], [back, up] = b.head.set ?? [0.1, 0.02], head = axis(axis(bones[n], nod).p(-back, 0, up), nod); + const rows = b.head.rows.filter((_, i, all) => !far || i % 2 === 0 || i === all.length - 1), length = rows[rows.length - 1][0]; + s.skin(head.rings(rows), 8, (band, top) => coat.head(band < 0 ? 0 : band >= rows.length - 1 ? 2 : rows[band + 1][0] / length, top)); + + for (const front of [true, false]) { + const leg = front ? b.front : b.hind, { x, y, thigh, bone } = leg, top = lies ? Math.min(leg.top, LONGEST_FOLD) : leg.top, [upper, lower, hoof] = coat.leg(front), step = 1.13 * bone; + both(side => { + const at = (dx: number, py: number, z: number): Vec => [side * (x + dx), py, z]; + if (!lies) { + const hock = front ? 0 : -0.45 * bone; + const down = [[y, top, thigh, 1.2 * thigh, 1.2 * thigh], [y + hock, 0.56 * top, bone, 1.13 * bone, 1.13 * bone], [y + 0.3 * hock, step + 0.015, 0.87 * bone, 0.93 * bone, 0.93 * bone], + [y + 0.3 * hock, step, 1.07 * bone, 1.2 * bone, 1.13 * bone], [y + 0.3 * hock, 0, 1.13 * bone, 1.33 * bone, 1.2 * bone]].filter((_, i) => !far || i !== 2 && i !== 3); + s.skin(down.map(([py, z, w, fore, aft]) => ({ at: at(0, py, z), w, up: fore, down: aft, pitch: -90, e: 2 })), 6, far ? (band => [upper, upper, lower, lower][band + 1]) : (band => [upper, upper, lower, lower, hoof, hoof][band + 1])); + } else if (front) { + // The folded foreleg: the knee pokes out before the chest, the hoof lies by the elbow. + const fold = [[y - 0.1 * top, 1.6, 1.3, 1.5, 1.6], [y + 0.42 * top, 1.4, 1.2, 1.3, 1.4], [y + 0.5 * top, 1.2, 0.7, 0.8, 0.8]].filter((_, i) => !far || i !== 1); + s.skin(through(fold.map(([py, z]) => at(0, py, z * bone)), fold.map(row => row.slice(2).map(v => v * bone))), 6, lower); + s.fine(() => s.skin(through([at(1.2 * bone, y + 0.02 * top, 0.9 * bone), at(1.2 * bone, y + 0.2 * top, 0.9 * bone)], [[bone, bone, 0.9 * bone], [1.1 * bone, 1.1 * bone, 0.9 * bone]]), 6, hoof)); + } else { + // The haunch on the flank, and the shank along the grass to a hoof beside the belly. + s.skin(through([at(thigh, y - 0.3 * top, 0.3 * top), at(1.1 * thigh, y + 0.02 * top, 0.33 * top), at(thigh, y + 0.36 * top, 0.24 * top)], [[0.6 * thigh, 0.14 * top, 0.14 * top], [1.1 * thigh, 0.33 * top, 0.33 * top], [0.6 * thigh, 0.16 * top, 0.16 * top]]), 6, upper); + s.fine(() => s.skin(through([at(1.6 * thigh, y - 0.26 * top, 1.2 * bone), at(1.8 * thigh, y + 0.45 * top, 1.1 * bone), at(1.8 * thigh, y + 0.62 * top, 1.1 * bone)], [[1.1 * bone, 1.2 * bone, 1.2 * bone], [0.95 * bone, bone, bone], [1.1 * bone, 1.1 * bone, 1.1 * bone]]), 6, band => (band > 0 ? hoof : lower))); + } + }); + } + return { head, neck, body, lies, crown: (d: number, v: number) => axis(head.p(d, 0, v), nod - b.head.nod[STAND]) }; +} + +/** + * A paint from a map of the pelt: one word for each band, its letters the facets from low on the right flank over the + * back to low on the left flank. The belly, and a letter with no colour, take `base`. + */ +export const pelt = (map: string[], colours: Record, base: number): Paint => (band, _, around) => + colours[map[Math.max(0, Math.min(map.length - 1, band))][Math.round(around * 10 + 1) % 10]] ?? base; + +type Head = ReturnType; +/** Both eyes at [d, x, v] of the head: fine details. */ +export const eyes = (s: Sculpt, h: Head, [d, x, v]: number[], size = 0.05) => s.fine(() => both(side => s.box(h.p(d, side * x, v), [0.6 * size, size, size], EYE))); +/** Both ears, leaves from [d, x, v] of the head to their tips. */ +export const ears = (s: Sculpt, h: Head, [d, x, v]: number[], [td, tx, tv]: number[], width: number, colour: number) => + both(side => s.leaf(h.p(d, side * x, v), h.p(td, side * tx, tv), width, 0.6 * width, h.dir, colour)); diff --git a/builder/web/src/lib/trailview/models/animals/brown-cattle.ts b/builder/web/src/lib/trailview/models/animals/brown-cattle.ts new file mode 100644 index 0000000000..f736715af5 --- /dev/null +++ b/builder/web/src/lib/trailview/models/animals/brown-cattle.ts @@ -0,0 +1,12 @@ +import { rgb } from '../../structures/mesh'; +import type { Animal } from '../animals'; +import { cattle } from './body/cattle'; + +export default { + name: 'Brown cattle', + latin: 'Bos taurus', + height: 1.4, + coats: ['#9a7a5c', '#8e4a24', '#ae8250'].map(rgb), + poses: 3, + build: (s, coat, pose) => cattle(s, pose, { coat }), +} satisfies Animal; diff --git a/builder/web/src/lib/trailview/models/animals/chamois.ts b/builder/web/src/lib/trailview/models/animals/chamois.ts new file mode 100644 index 0000000000..d681dbd78d --- /dev/null +++ b/builder/web/src/lib/trailview/models/animals/chamois.ts @@ -0,0 +1,35 @@ +import { rgb } from '../../structures/mesh'; +import type { Animal } from '../animals'; +import { shade } from '../flat'; +import { both } from '../sculpt'; +import { ears, eyes, HOOF, NOSE, quadruped, X, type Body } from './body/quadruped'; + +const BODY: Body = { + trunk: [[-0.5, 0.79, 0.1, 0.11, 0.11], [-0.42, 0.78, 0.21, 0.2, 0.21], [-0.18, 0.77, 0.26, 0.23, 0.26], [0.1, 0.77, 0.26, 0.23, 0.27], [0.31, 0.78, 0.23, 0.22, 0.24], [0.43, 0.8, 0.16, 0.17, 0.18], [0.5, 0.81, 0.08, 0.09, 0.1]], + neck: { at: [0.32, 0.86], length: 0.32, size: [[0.16, 0.18, 0.19], [0.115, 0.125, 0.13]], carry: [[66, 58], [-35, -60], [66, 56]] }, + head: { rows: [[0, 0.08, 0.085, 0.085], [0.06, 0.135, 0.125, 0.125], [0.18, 0.14, 0.125, 0.13], [0.31, 0.1, 0.09, 0.1], [0.39, 0.09, 0.075, 0.085], [0.43, 0.06, 0.05, 0.055]], nod: [-24, -75, -20], set: [0.05, 0.09] }, + front: { x: 0.13, y: 0.29, top: 0.62, thigh: 0.08, bone: 0.052 }, + hind: { x: 0.135, y: -0.35, top: 0.62, thigh: 0.092, bone: 0.052 }, +}; +const HORN = rgb('#231d1a'), FACE = rgb('#eadfc8'); + +export default { + name: 'Chamois', + latin: 'Rupicapra rupicapra', + height: 0.78, + // The dark coat of winter, the brown of autumn, the fawn of summer. + coats: ['#5c463a', '#6e5038', '#9c7448'].map(hex => rgb(hex)), + poses: 3, + build(s, coat, pose) { + const dark = shade(coat, 0.5); + const q = quadruped(s, BODY, pose, { + trunk: (band, top) => (band < 0 ? FACE : top < -0.6 ? shade(FACE, 0.9) : top > 0.9 ? dark : coat), neck: (_, top) => (top < -0.6 ? FACE : coat), + // A pale face with a dark stripe from the ear through the eye to the nose. + head: (d, top) => (d > 1 ? NOSE : d < 0.2 ? coat : Math.abs(top) < 0.3 ? dark : FACE), leg: () => [coat, shade(coat, 0.6), HOOF], + }), h = q.head, k = q.crown(0.07, 0.1); + ears(s, h, [0.1, 0.1, 0.03], [0.02, 0.24, 0.16], 0.09, coat); + eyes(s, h, [0.19, 0.125, 0.045], 0.045); + both(side => s.chain([k.p(0, side * 0.045, 0), k.p(0.03, side * 0.06, 0.2), k.p(-0.02, side * 0.075, 0.28), k.p(-0.08, side * 0.085, 0.24)], [0.048, 0.04, 0.032, 0.012], HORN)); + s.leaf(q.body([0, -0.44, 0.9]), q.body([0, -0.55, 0.82]), 0.08, 0.05, X, dark); + }, +} satisfies Animal; diff --git a/builder/web/src/lib/trailview/models/animals/dairy-cattle.ts b/builder/web/src/lib/trailview/models/animals/dairy-cattle.ts new file mode 100644 index 0000000000..7fc30fd06f --- /dev/null +++ b/builder/web/src/lib/trailview/models/animals/dairy-cattle.ts @@ -0,0 +1,19 @@ +import { rgb } from '../../structures/mesh'; +import type { Animal } from '../animals'; +import { cattle } from './body/cattle'; +import { pelt, PINK } from './body/quadruped'; + +const WHITE = rgb('#e9e6de'), BLACK = rgb('#1e1c1b'); + +export default { + name: 'Dairy cattle', + latin: 'Bos taurus', + height: 1.45, + // A black fore and rump with a white girth; a black saddle with a white rump. + coats: [['..BBBB..', '..BBBBB.', '...BBBB.', '........', '.BBBBBB.', '.BBBBBB.'], ['........', '...BB...', '.BBBBBB.', '.BBBBB..', '....BBB.', '.BBBBBB.']], + poses: 3, + build: (s, map, pose) => cattle(s, pose, { + coat: WHITE, trunk: pelt(map, { B: BLACK }, WHITE), neck: BLACK, ear: BLACK, tuft: WHITE, horns: false, + head: (d, top) => (d > 0.9 ? PINK : top > 0.5 && d > 0.3 ? WHITE : BLACK), + }), +} satisfies Animal; diff --git a/builder/web/src/lib/trailview/models/animals/donkey.ts b/builder/web/src/lib/trailview/models/animals/donkey.ts new file mode 100644 index 0000000000..f6653b4ea7 --- /dev/null +++ b/builder/web/src/lib/trailview/models/animals/donkey.ts @@ -0,0 +1,36 @@ +import { rgb } from '../../structures/mesh'; +import type { Animal } from '../animals'; +import { shade } from '../flat'; +import { ears, eyes, quadruped, type Body } from './body/quadruped'; + +const BODY: Body = { + trunk: [[-0.6, 0.78, 0.1, 0.12, 0.12], [-0.52, 0.76, 0.23, 0.22, 0.22], [-0.28, 0.75, 0.28, 0.24, 0.26], [0.04, 0.74, 0.29, 0.24, 0.28], [0.3, 0.76, 0.25, 0.23, 0.25], [0.45, 0.78, 0.19, 0.2, 0.21], [0.52, 0.79, 0.1, 0.11, 0.12]], + neck: { at: [0.36, 0.82], length: 0.44, size: [[0.17, 0.2, 0.22], [0.12, 0.15, 0.16], [0.105, 0.13, 0.13]], carry: [[42, 30], [-28, -52], [48, 36]] }, + head: { rows: [[0, 0.085, 0.09, 0.09], [0.07, 0.14, 0.135, 0.135], [0.21, 0.145, 0.135, 0.145], [0.37, 0.115, 0.1, 0.12], [0.48, 0.11, 0.09, 0.11], [0.53, 0.08, 0.06, 0.075]], nod: [-42, -70, -35], set: [0.08, 0.04] }, + front: { x: 0.14, y: 0.33, top: 0.66, thigh: 0.085, bone: 0.055 }, + hind: { x: 0.15, y: -0.42, top: 0.66, thigh: 0.1, bone: 0.055 }, +}; +const PALE = rgb('#ddd4c4'); + +export default { + name: 'Donkey', + latin: 'Equus asinus', + height: 1.1, + coats: ['#9a958e', '#8a705a', '#6c6058'].map(hex => rgb(hex)), + poses: 3, + build(s, coat, pose) { + const dark = shade(coat, 0.45); + const q = quadruped(s, BODY, pose, { + // A pale belly, and the dark stripe of the back. + trunk: (_, top) => (top < -0.6 ? PALE : top > 0.9 ? shade(coat, 0.7) : coat), neck: coat, + head: d => (d > 1 ? shade(PALE, 0.75) : d > 0.8 ? PALE : coat), leg: () => [coat, coat, rgb('#2a2624')], + }), h = q.head; + // Long ears, with a pale ring round each eye. + ears(s, h, [0.06, 0.08, 0.1], [-0.06, 0.17, 0.42], 0.13, coat); + s.fine(() => [-1, 1].forEach(side => s.box(h.p(0.21, side * 0.14, 0.05), [0.03, 0.085, 0.085], PALE))); + eyes(s, h, [0.21, 0.15, 0.05]); + s.chain([q.neck(0.15, 0.2), q.neck(0.55, 0.17), q.neck(0.98, 0.15)], [0.06, 0.07, 0.06], dark); + s.fine(() => s.chain([[0, -0.6, 0.9], [0, -0.7, 0.7], [0, -0.71, 0.48]].map(q.body), [0.045, 0.04, 0.04], coat)); + s.chain([q.body([0, -0.71, 0.5]), q.body([0, -0.7, 0.28])], [0.09, 0.04], dark); + }, +} satisfies Animal; diff --git a/builder/web/src/lib/trailview/models/animals/dromedary.ts b/builder/web/src/lib/trailview/models/animals/dromedary.ts new file mode 100644 index 0000000000..c56ab846fa --- /dev/null +++ b/builder/web/src/lib/trailview/models/animals/dromedary.ts @@ -0,0 +1,30 @@ +import { rgb } from '../../structures/mesh'; +import type { Animal } from '../animals'; +import { shade } from '../flat'; +import { alongY, bellied } from '../sculpt'; +import { ears, eyes, quadruped, type Body } from './body/quadruped'; + +const BODY: Body = { + trunk: [[-0.6, 0.88, 0.09, 0.1, 0.1], [-0.52, 0.86, 0.2, 0.15, 0.18], [-0.28, 0.85, 0.25, 0.17, 0.22], [0.04, 0.84, 0.26, 0.17, 0.25], [0.32, 0.85, 0.23, 0.16, 0.25], [0.48, 0.85, 0.17, 0.14, 0.2], [0.56, 0.85, 0.09, 0.09, 0.12]], + neck: { at: [0.42, 0.78], length: 0.86, size: [[0.15, 0.17, 0.2], [0.1, 0.11, 0.13], [0.085, 0.09, 0.1], [0.08, 0.085, 0.09]], carry: [[8, 92], [-50, -20], [30, 95]] }, + head: { rows: [[0, 0.075, 0.075, 0.075], [0.06, 0.125, 0.11, 0.11], [0.18, 0.125, 0.11, 0.115], [0.32, 0.1, 0.075, 0.1], [0.43, 0.095, 0.065, 0.095], [0.47, 0.065, 0.045, 0.065]], nod: [-12, -50, -10], set: [0.1, -0.02] }, + front: { x: 0.14, y: 0.36, top: 0.8, thigh: 0.085, bone: 0.052 }, + hind: { x: 0.14, y: -0.42, top: 0.8, thigh: 0.1, bone: 0.052 }, +}; + +export default { + name: 'Dromedary', + latin: 'Camelus dromedarius', + height: 1.85, + coats: ['#c9a877', '#b08d60', '#dcc6a0'].map(hex => rgb(hex)), + poses: 3, + build(s, coat, pose) { + const dark = shade(coat, 0.8); + const q = quadruped(s, BODY, pose, { trunk: bellied(coat, shade(coat, 0.88)), neck: coat, head: d => (d > 1 ? shade(coat, 0.6) : coat), leg: () => [coat, coat, shade(coat, 0.7)] }), h = q.head; + const [, , top] = q.body([0, 0, 0.98]); + s.skin(alongY([[-0.46, top - 0.02, 0.1, 0.05, 0.1], [-0.3, top + 0.03, 0.2, 0.15, 0.12], [-0.1, top + 0.05, 0.22, 0.21, 0.14], [0.1, top + 0.03, 0.18, 0.14, 0.12], [0.26, top - 0.02, 0.09, 0.05, 0.1]]), 8, (_, up) => (up > 0.9 ? dark : coat)); + ears(s, h, [0.06, 0.09, 0.05], [0.0, 0.17, 0.12], 0.07, coat); + eyes(s, h, [0.17, 0.11, 0.04], 0.045); + s.fine(() => s.chain([[0, -0.6, 0.94], [0, -0.68, 0.74], [0, -0.67, 0.56]].map(q.body), [0.04, 0.035, 0.05], dark)); + }, +} satisfies Animal; diff --git a/builder/web/src/lib/trailview/models/animals/goat.ts b/builder/web/src/lib/trailview/models/animals/goat.ts new file mode 100644 index 0000000000..5c12efdd3c --- /dev/null +++ b/builder/web/src/lib/trailview/models/animals/goat.ts @@ -0,0 +1,41 @@ +import { rgb } from '../../structures/mesh'; +import type { Animal } from '../animals'; +import { both } from '../sculpt'; +import { ears, eyes, HOOF, NOSE, quadruped, X, type Body } from './body/quadruped'; + +const BODY: Body = { + trunk: [[-0.52, 0.76, 0.11, 0.11, 0.11], [-0.44, 0.75, 0.22, 0.22, 0.22], [-0.2, 0.74, 0.28, 0.25, 0.28], [0.1, 0.74, 0.28, 0.25, 0.29], [0.33, 0.75, 0.24, 0.24, 0.26], [0.45, 0.77, 0.16, 0.18, 0.19], [0.52, 0.78, 0.08, 0.09, 0.1]], + neck: { at: [0.35, 0.83], length: 0.33, size: [[0.16, 0.18, 0.19], [0.12, 0.13, 0.13]], carry: [[58, 50], [-35, -60], [60, 50]] }, + head: { rows: [[0, 0.085, 0.085, 0.085], [0.07, 0.145, 0.135, 0.135], [0.2, 0.15, 0.135, 0.14], [0.34, 0.105, 0.095, 0.105], [0.43, 0.095, 0.085, 0.095], [0.47, 0.065, 0.055, 0.06]], nod: [-30, -75, -25], set: [0.06, 0.1] }, + front: { x: 0.14, y: 0.31, top: 0.58, thigh: 0.08, bone: 0.056 }, + hind: { x: 0.14, y: -0.37, top: 0.58, thigh: 0.092, bone: 0.056 }, +}; +const HORN = rgb('#8a7a64'); + +interface Coat { fore: string; rear: string; under: string; shin: string; muzzle: string } + +export default { + name: 'Goat', + latin: 'Capra hircus', + height: 0.8, + // A black neck with a white rear; a dark brown; a chamois brown with dark legs; a white. + coats: [ + { fore: '#2e241e', rear: '#ece4d4', under: '', shin: '', muzzle: '#2e241e' }, + { fore: '#4a3a2e', rear: '#4a3a2e', under: '#7a6048', shin: '#33271f', muzzle: '#8a6e54' }, + { fore: '#87482a', rear: '#87482a', under: '#e6dccb', shin: '#2e241e', muzzle: '#e6dccb' }, + { fore: '#ece4d4', rear: '#ece4d4', under: '#e0d6c2', shin: '#ece4d4', muzzle: '#e2c8bc' }, + ] as Coat[], + poses: 3, + build(s, coat, pose) { + const [fore, rear] = [coat.fore, coat.rear].map(hex => rgb(hex)), under = coat.under ? rgb(coat.under) : 0, muzzle = rgb(coat.muzzle); + const q = quadruped(s, BODY, pose, { + trunk: (band, top) => (under && top < -0.6 ? under : band < 3 ? rear : fore), neck: fore, head: d => (d > 1 ? NOSE : d > 0.7 ? muzzle : fore), + leg: front => { const c = front ? fore : rear; return [c, coat.shin ? rgb(coat.shin) : c, HOOF]; }, + }), h = q.head, k = q.crown(0.05, 0.1); + ears(s, h, [0.11, 0.1, 0.04], [0.07, 0.34, -0.01], 0.11, fore); + both(side => s.chain([k.p(0, side * 0.07, 0), k.p(-0.03, side * 0.09, 0.14), k.p(-0.12, side * 0.11, 0.24), k.p(-0.23, side * 0.14, 0.27)], [0.06, 0.05, 0.035, 0.012], HORN)); + eyes(s, h, [0.2, 0.135, 0.05]); + s.leaf(h.p(0.35, 0, -0.08), h.p(0.32, 0, -0.3), 0.08, 0.05, X, muzzle); + s.leaf(q.body([0, -0.44, 0.9]), q.body([0, -0.6, 1.08]), 0.09, 0.05, X, rear); + }, +} satisfies Animal; diff --git a/builder/web/src/lib/trailview/models/animals/highland-cow.ts b/builder/web/src/lib/trailview/models/animals/highland-cow.ts new file mode 100644 index 0000000000..6bd0d051f0 --- /dev/null +++ b/builder/web/src/lib/trailview/models/animals/highland-cow.ts @@ -0,0 +1,32 @@ +import { rgb } from '../../structures/mesh'; +import type { Animal } from '../animals'; +import { shade } from '../flat'; +import { bellied, both } from '../sculpt'; +import { ears, HOOF, HORN, NOSE, quadruped, type Body } from './body/quadruped'; + +// A low, wide trunk under long hair that hangs to the knees. +const BODY: Body = { + trunk: [[-0.64, 0.7, 0.14, 0.16, 0.16], [-0.54, 0.68, 0.3, 0.29, 0.3], [-0.28, 0.67, 0.37, 0.32, 0.37], [0.06, 0.67, 0.38, 0.33, 0.39], [0.36, 0.69, 0.35, 0.31, 0.36], [0.54, 0.71, 0.26, 0.25, 0.28], [0.62, 0.72, 0.14, 0.15, 0.16]], + neck: { at: [0.4, 0.74], length: 0.36, size: [[0.25, 0.24, 0.27], [0.18, 0.18, 0.2]], carry: [[18, 12], [-30, -55], [28, 18]] }, + head: { rows: [[0, 0.12, 0.11, 0.11], [0.07, 0.19, 0.17, 0.17], [0.2, 0.19, 0.17, 0.18], [0.33, 0.15, 0.12, 0.14], [0.42, 0.14, 0.11, 0.13], [0.48, 0.1, 0.08, 0.09]], nod: [-34, -72, -28] }, + front: { x: 0.19, y: 0.38, top: 0.56, thigh: 0.12, bone: 0.085 }, + hind: { x: 0.2, y: -0.42, top: 0.56, thigh: 0.135, bone: 0.085 }, +}; + +export default { + name: 'Highland cow', + latin: 'Bos taurus', + height: 1.2, + coats: [['#b3561f', '#e09048'], ['#4a2a1a', '#7a4a2c'], ['#d0ae74', '#efd6a2']].map(pair => pair.map(hex => rgb(hex))), + poses: 3, + build(s, [coat, light], pose) { + const q = quadruped(s, BODY, pose, { trunk: bellied(coat, shade(coat, 0.8)), neck: coat, head: d => (d > 1 ? NOSE : d > 0.8 ? light : coat), leg: () => [coat, coat, HOOF] }), h = q.head; + // The fringe over the eyes, and hair that hangs from the neck and the flanks. + s.skin(h.rings([[-0.04, 0.12, 0.08, 0.06], [0.1, 0.2, 0.14, 0.05], [0.27, 0.15, 0.16, 0.03], [0.34, 0.07, 0.16, 0.02]].map(([d, w, up, down]) => [d, w, up + 0.06, down - 0.06])), 8, light); + s.fine(() => s.leaf(q.neck(0.25, -0.2), q.neck(0.6, -0.42), 0.26, 0.12, [1, 0, 0], shade(coat, 0.9))); + ears(s, h, [0.08, 0.16, 0.0], [0.03, 0.42, -0.08], 0.15, coat); + const k = q.crown(0.07, 0.13); + both(side => s.chain([k.p(0, side * 0.1, 0), k.p(0.02, side * 0.34, 0.03), k.p(0.07, side * 0.56, 0.17), k.p(0.1, side * 0.62, 0.4)], [0.07, 0.06, 0.045, 0.012], HORN)); + s.fine(() => s.chain([[0, -0.64, 0.86], [0, -0.74, 0.55], [0, -0.74, 0.22]].map(q.body), [0.05, 0.05, 0.1], coat)); + }, +} satisfies Animal; diff --git a/builder/web/src/lib/trailview/models/animals/horse.ts b/builder/web/src/lib/trailview/models/animals/horse.ts new file mode 100644 index 0000000000..5c6c303189 --- /dev/null +++ b/builder/web/src/lib/trailview/models/animals/horse.ts @@ -0,0 +1,33 @@ +import { rgb } from '../../structures/mesh'; +import type { Animal } from '../animals'; +import { shade } from '../flat'; +import { bellied } from '../sculpt'; +import { ears, eyes, quadruped, type Body } from './body/quadruped'; + +const BODY: Body = { + trunk: [[-0.58, 0.8, 0.11, 0.13, 0.13], [-0.5, 0.78, 0.24, 0.23, 0.23], [-0.28, 0.77, 0.28, 0.24, 0.25], [0.02, 0.75, 0.28, 0.23, 0.26], [0.28, 0.77, 0.26, 0.24, 0.26], [0.43, 0.79, 0.2, 0.21, 0.23], [0.51, 0.8, 0.11, 0.12, 0.14]], + neck: { at: [0.33, 0.82], length: 0.58, size: [[0.18, 0.22, 0.26], [0.12, 0.16, 0.17], [0.095, 0.125, 0.115]], carry: [[52, 34], [-25, -50], [55, 40]] }, + head: { rows: [[0, 0.08, 0.09, 0.09], [0.07, 0.135, 0.13, 0.13], [0.22, 0.14, 0.13, 0.14], [0.4, 0.1, 0.09, 0.11], [0.53, 0.095, 0.08, 0.1], [0.58, 0.07, 0.055, 0.065]], nod: [-48, -70, -40], set: [0.08, 0.04] }, + front: { x: 0.14, y: 0.31, top: 0.7, thigh: 0.09, bone: 0.058 }, + hind: { x: 0.15, y: -0.41, top: 0.7, thigh: 0.105, bone: 0.058 }, +}; + +interface Coat { coat: string; hair: string; shin: string; blaze?: boolean } + +export default { + name: 'Horse', + latin: 'Equus caballus', + height: 1.55, + // A bay with a blaze, a chestnut, a grey, a black. + coats: [{ coat: '#7a3e1e', hair: '#1a1512', shin: '#2e1e16', blaze: true }, { coat: '#8a4018', hair: '#6e3014', shin: '#8a4018' }, { coat: '#b8b6b2', hair: '#6e6c6a', shin: '#8e8c8a' }, { coat: '#4a4441', hair: '#1c1a19', shin: '#4a4441' }] as Coat[], + poses: 3, + build(s, c, pose) { + const [coat, hair, shin] = [c.coat, c.hair, c.shin].map(hex => rgb(hex)), dark = shade(coat, 0.6); + const q = quadruped(s, BODY, pose, { trunk: bellied(coat, shade(coat, 0.85)), neck: coat, head: (d, top) => (c.blaze && top > 0.9 && d > 0.3 ? rgb('#ece6da') : d > 0.85 ? dark : coat), leg: () => [coat, shin, rgb('#2a2624')] }), h = q.head; + ears(s, h, [0.05, 0.07, 0.1], [0.02, 0.1, 0.27], 0.09, coat); + eyes(s, h, [0.22, 0.135, 0.05]); + // The mane on the crest, the forelock, the tail. + s.chain([q.neck(0.12, 0.2), q.neck(0.5, 0.17), q.neck(0.95, 0.15), h.p(0.14, 0, 0.15)], [0.09, 0.11, 0.1, 0.06], hair); + s.chain([[0, -0.56, 0.9], [0, -0.7, 0.74], [0, -0.74, 0.42], [0, -0.7, 0.22]].map(q.body), [0.07, 0.12, 0.13, 0.05], hair); + }, +} satisfies Animal; diff --git a/builder/web/src/lib/trailview/models/animals/ibex.ts b/builder/web/src/lib/trailview/models/animals/ibex.ts new file mode 100644 index 0000000000..d8a49423de --- /dev/null +++ b/builder/web/src/lib/trailview/models/animals/ibex.ts @@ -0,0 +1,41 @@ +import { rgb } from '../../structures/mesh'; +import type { Animal } from '../animals'; +import { shade } from '../flat'; +import { bellied, both } from '../sculpt'; +import { ears, eyes, HOOF, NOSE, quadruped, X, type Body } from './body/quadruped'; + +const BODY: Body = { + trunk: [[-0.56, 0.74, 0.12, 0.13, 0.13], [-0.47, 0.73, 0.25, 0.24, 0.24], [-0.22, 0.72, 0.31, 0.27, 0.3], [0.1, 0.72, 0.32, 0.28, 0.31], [0.36, 0.74, 0.29, 0.26, 0.29], [0.5, 0.76, 0.2, 0.2, 0.22], [0.57, 0.77, 0.1, 0.11, 0.12]], + neck: { at: [0.38, 0.82], length: 0.3, size: [[0.21, 0.22, 0.24], [0.15, 0.16, 0.17]], carry: [[52, 42], [-35, -60], [56, 46]] }, + head: { rows: [[0, 0.095, 0.095, 0.095], [0.07, 0.155, 0.14, 0.14], [0.2, 0.155, 0.14, 0.145], [0.33, 0.115, 0.1, 0.115], [0.42, 0.105, 0.085, 0.1], [0.46, 0.07, 0.06, 0.065]], nod: [-28, -75, -22], set: [0.06, 0.1] }, + front: { x: 0.16, y: 0.34, top: 0.58, thigh: 0.095, bone: 0.065 }, + hind: { x: 0.165, y: -0.4, top: 0.58, thigh: 0.11, bone: 0.065 }, +}; +const HORN = rgb('#6e6152'), RIDGE = rgb('#8c7e6a'); +// The sweep of a horn in the level frame of the crown: [back, up, out, width]. +const SWEEP = [[0, 0, 0.06, 0.09], [0.04, 0.24, 0.09, 0.085], [0.15, 0.45, 0.13, 0.075], [0.31, 0.6, 0.17, 0.06], [0.5, 0.66, 0.21, 0.045], [0.66, 0.6, 0.24, 0.02]]; + +export default { + name: 'Alpine ibex', + latin: 'Capra ibex', + height: 0.9, + coats: ['#8f7a62', '#7a6652', '#a08a6c'].map(hex => rgb(hex)), + poses: 3, + build(s, coat, pose) { + const dark = shade(coat, 0.55), pale = rgb('#d8cdb8'); + const q = quadruped(s, BODY, pose, { trunk: bellied(coat, pale), neck: coat, head: d => (d > 1 ? NOSE : d > 0.7 ? shade(coat, 1.2) : coat), leg: () => [coat, shade(coat, 0.7), HOOF] }), h = q.head, k = q.crown(0.06, 0.11); + ears(s, h, [0.12, 0.12, 0.0], [0.08, 0.33, -0.05], 0.1, coat); + eyes(s, h, [0.2, 0.14, 0.05]); + both(side => { + const at = SWEEP.map(([back, up, out]) => k.p(-back, side * out, up)); + s.chain(at, SWEEP.map(row => row[3]), HORN); + // The knots on the front of the horn. + s.fine(() => [1, 2, 3, 4].forEach(i => [0.15, 0.6].forEach(t => { + const [back, up, out, w] = SWEEP[i].map((v, n) => v + (SWEEP[i + 1][n] - v) * t), turn = Math.atan2(SWEEP[i + 1][1] - SWEEP[i][1], SWEEP[i + 1][0] - SWEEP[i][0]); + s.box(k.p(-back + 0.5 * w * Math.sin(turn), side * out, up + 0.5 * w * Math.cos(turn)), [1.25 * w, 0.045, 0.045], RIDGE); + }))); + }); + s.leaf(h.p(0.33, 0, -0.08), h.p(0.31, 0, -0.24), 0.07, 0.05, X, dark); + s.leaf(q.body([0, -0.48, 0.88]), q.body([0, -0.6, 0.8]), 0.1, 0.05, X, dark); + }, +} satisfies Animal; diff --git a/builder/web/src/lib/trailview/models/animals/pig.ts b/builder/web/src/lib/trailview/models/animals/pig.ts new file mode 100644 index 0000000000..cb2617912d --- /dev/null +++ b/builder/web/src/lib/trailview/models/animals/pig.ts @@ -0,0 +1,31 @@ +import { rgb } from '../../structures/mesh'; +import type { Animal } from '../animals'; +import { shade } from '../flat'; +import { bellied, both } from '../sculpt'; +import { eyes, quadruped, type Body } from './body/quadruped'; + +// Lean and long: an arched back over slim legs, a long head that the pig carries low. +const BODY: Body = { + trunk: [[-0.62, 0.64, 0.12, 0.14, 0.14], [-0.53, 0.65, 0.26, 0.27, 0.29], [-0.28, 0.67, 0.31, 0.31, 0.35], [0.06, 0.68, 0.31, 0.32, 0.36], [0.36, 0.67, 0.29, 0.3, 0.34], [0.54, 0.65, 0.23, 0.25, 0.27], [0.62, 0.64, 0.13, 0.15, 0.15]], + neck: { at: [0.44, 0.64], length: 0.28, size: [[0.24, 0.27, 0.24], [0.18, 0.2, 0.18]], carry: [[-4, -8], [-30, -42], [6, 2]] }, + head: { rows: [[0, 0.12, 0.12, 0.11], [0.06, 0.18, 0.17, 0.15], [0.18, 0.165, 0.15, 0.14], [0.32, 0.1, 0.08, 0.095], [0.45, 0.085, 0.07, 0.085], [0.48, 0.08, 0.07, 0.08]], nod: [-20, -42, -12], set: [0.1, 0.03] }, + front: { x: 0.16, y: 0.4, top: 0.5, thigh: 0.09, bone: 0.055 }, + hind: { x: 0.165, y: -0.42, top: 0.5, thigh: 0.11, bone: 0.055 }, +}; +const SNOUT = rgb('#b89a94'); + +export default { + name: 'Iberian pig', + latin: 'Sus scrofa domesticus', + height: 0.75, + // Black, slate, and the dark red of the retinto. + coats: ['#575250', '#6e6864', '#84583f'].map(hex => rgb(hex)), + poses: 3, + build(s, coat, pose) { + const q = quadruped(s, BODY, pose, { trunk: bellied(coat, shade(coat, 0.85)), neck: coat, head: d => (d > 0.95 ? SNOUT : coat), leg: () => [coat, coat, shade(coat, 0.5)] }), h = q.head; + // Ears that flop forward over the eyes. + both(side => s.leaf(h.p(0.05, side * 0.11, 0.14), h.p(0.3, side * 0.22, 0.07), 0.2, 0.07, [0, 0, 1], shade(coat, 0.8))); + eyes(s, h, [0.22, 0.135, 0.03], 0.04); + s.fine(() => s.chain([[0, -0.62, 0.72], [0, -0.7, 0.66], [0, -0.7, 0.54]].map(q.body), [0.04, 0.035, 0.02], coat)); + }, +} satisfies Animal; diff --git a/builder/web/src/lib/trailview/models/animals/red-deer.ts b/builder/web/src/lib/trailview/models/animals/red-deer.ts new file mode 100644 index 0000000000..a31929423a --- /dev/null +++ b/builder/web/src/lib/trailview/models/animals/red-deer.ts @@ -0,0 +1,33 @@ +import { rgb } from '../../structures/mesh'; +import type { Animal } from '../animals'; +import { shade } from '../flat'; +import { antlers, DEER } from './body/deer'; +import { ears, eyes, HOOF, NOSE, quadruped, X } from './body/quadruped'; + +const RUMP = rgb('#e2d2b0'), ANTLER = rgb('#a89274'); + +interface Coat { coat: string; stag?: boolean } + +export default { + name: 'Red deer', + latin: 'Cervus elaphus', + height: 1.2, + // A stag among two hinds. + coats: [{ coat: '#96582e', stag: true }, { coat: '#a2673a' }, { coat: '#8c5a34' }] as Coat[], + poses: 3, + build(s, c, pose) { + const coat = rgb(c.coat), dark = shade(coat, 0.7); + const q = quadruped(s, DEER, pose, { + trunk: (band, top) => (band < 1 ? RUMP : top < -0.6 ? shade(coat, 1.25) : coat), neck: c.stag ? dark : coat, + head: d => (d > 1 ? NOSE : d > 0.75 ? shade(coat, 1.3) : coat), leg: () => [coat, shade(coat, 0.85), HOOF], + }), h = q.head; + ears(s, h, [0.08, 0.1, 0.04], [0.0, 0.32, 0.14], 0.13, coat); + eyes(s, h, [0.19, 0.125, 0.045], 0.045); + if (c.stag) { + antlers(s, q.crown(0.06, 0.1), [[0, 0, 0.06], [0.12, 0.22, 0.19], [0.26, 0.44, 0.3], [0.26, 0.68, 0.3]], [[0, -0.2, 0.1, 0.1], [1, -0.1, 0.38, 0.28], [2, 0.42, 0.6, 0.36], [2, 0.12, 0.64, 0.2]], ANTLER); + // The mane of the neck. + s.fine(() => s.leaf(q.neck(0.1, -0.15), q.neck(0.8, -0.17), 0.16, 0.14, X, dark)); + } + s.leaf(q.body([0, -0.5, 0.9]), q.body([0, -0.6, 0.78]), 0.09, 0.05, X, RUMP); + }, +} satisfies Animal; diff --git a/builder/web/src/lib/trailview/models/animals/reindeer.ts b/builder/web/src/lib/trailview/models/animals/reindeer.ts new file mode 100644 index 0000000000..6034f7ad46 --- /dev/null +++ b/builder/web/src/lib/trailview/models/animals/reindeer.ts @@ -0,0 +1,37 @@ +import { rgb } from '../../structures/mesh'; +import type { Animal } from '../animals'; +import { shade } from '../flat'; +import { antlers, DEER } from './body/deer'; +import { ears, eyes, HOOF, NOSE, quadruped, X, type Body } from './body/quadruped'; + +// A stocky deer on shorter legs that carries its head low. +const BODY: Body = { + ...DEER, + trunk: DEER.trunk.map(([y, z, w, up, down]) => [1.05 * y, z - 0.03, 1.16 * w, up + 0.03, 1.15 * down]), + neck: { at: [0.36, 0.82], length: 0.38, size: [[0.19, 0.2, 0.24], [0.14, 0.15, 0.19], [0.12, 0.13, 0.15]], carry: [[34, 22], [-35, -55], [40, 28]] }, + head: { rows: [[0, 0.085, 0.085, 0.085], [0.06, 0.14, 0.125, 0.125], [0.18, 0.14, 0.125, 0.13], [0.31, 0.11, 0.09, 0.105], [0.4, 0.105, 0.08, 0.095], [0.45, 0.075, 0.055, 0.065]], nod: [-30, -72, -24], set: [0.07, 0.05] }, + front: { x: 0.15, y: 0.32, top: 0.62, thigh: 0.085, bone: 0.055 }, + hind: { x: 0.155, y: -0.4, top: 0.62, thigh: 0.1, bone: 0.055 }, +}; +const PALE = rgb('#ddd5c4'), ANTLER = rgb('#b9a88c'); + +export default { + name: 'Reindeer', + latin: 'Rangifer tarandus', + height: 1.1, + coats: ['#6e5e50', '#857361', '#a39584'].map(hex => rgb(hex)), + poses: 3, + build(s, coat, pose) { + const dark = shade(coat, 0.8); + const q = quadruped(s, BODY, pose, { + trunk: (band, top) => (band < 1 ? PALE : top < -0.6 ? shade(PALE, 0.9) : band > 3 ? shade(coat, 1.15) : coat), neck: PALE, + head: d => (d > 1 ? NOSE : d > 0.75 ? PALE : coat), leg: () => [coat, dark, HOOF], + }), h = q.head; + ears(s, h, [0.09, 0.12, 0.03], [0.04, 0.3, 0.06], 0.1, coat); + eyes(s, h, [0.19, 0.14, 0.045], 0.045); + antlers(s, q.crown(0.06, 0.1), [[0, 0, 0.07], [0.22, 0.24, 0.2], [0.36, 0.52, 0.3], [0.2, 0.78, 0.34], [0.02, 0.86, 0.32]], [[0, -0.2, 0.1, 0.05], [3, 0.28, 0.96, 0.4], [1, 0.34, 0.2, 0.26], [2, 0.5, 0.56, 0.36]], ANTLER, 0.05); + // The mane under the neck. + s.leaf(q.neck(0.15, -0.2), q.neck(0.7, -0.34), 0.2, 0.14, X, PALE); + s.leaf(q.body([0, -0.52, 0.9]), q.body([0, -0.62, 0.8]), 0.09, 0.05, X, PALE); + }, +} satisfies Animal; diff --git a/builder/web/src/lib/trailview/models/animals/sheep.ts b/builder/web/src/lib/trailview/models/animals/sheep.ts new file mode 100644 index 0000000000..4cf700cb2b --- /dev/null +++ b/builder/web/src/lib/trailview/models/animals/sheep.ts @@ -0,0 +1,32 @@ +import { rgb } from '../../structures/mesh'; +import type { Animal } from '../animals'; +import { shade } from '../flat'; +import { axis, bellied } from '../sculpt'; +import { ears, eyes, quadruped, type Body } from './body/quadruped'; + +// Few, large lumps of wool: a trunk through 5 rings. +const BODY: Body = { + trunk: [[-0.56, 0.71, 0.17, 0.16, 0.16], [-0.4, 0.7, 0.35, 0.31, 0.33], [-0.03, 0.69, 0.39, 0.33, 0.36], [0.34, 0.71, 0.34, 0.3, 0.33], [0.5, 0.74, 0.17, 0.16, 0.17]], + wool: 0.05, + neck: { at: [0.34, 0.8], length: 0.3, size: [[0.21, 0.21, 0.21], [0.16, 0.16, 0.16]], carry: [[36, 30], [-30, -55], [42, 34]] }, + head: { rows: [[0, 0.08, 0.08, 0.08], [0.06, 0.13, 0.12, 0.12], [0.18, 0.135, 0.12, 0.12], [0.3, 0.1, 0.09, 0.1], [0.38, 0.09, 0.075, 0.085], [0.42, 0.06, 0.05, 0.055]], nod: [-35, -75, -28], set: [0.09, 0.03] }, + front: { x: 0.17, y: 0.3, top: 0.55, thigh: 0.07, bone: 0.052 }, + hind: { x: 0.17, y: -0.36, top: 0.55, thigh: 0.075, bone: 0.052 }, +}; + +export default { + name: 'Sheep', + latin: 'Ovis aries', + height: 0.8, + // Wool and face: two with a black face, a white one, a brown one. + coats: [['#efe7d6', '#2d2828'], ['#e6dcc6', '#2d2828'], ['#efe9dc', '#dccdb6'], ['#6a5444', '#3a2c26']].map(pair => pair.map(hex => rgb(hex))), + poses: 3, + build(s, [wool, face], pose) { + const q = quadruped(s, BODY, pose, { trunk: bellied(wool, shade(wool, 0.88)), neck: wool, head: () => face, leg: () => [wool, face, shade(face, 0.6)] }), h = q.head; + const cap = axis(h.p(0.09, 0, 0.11), 0); + s.skin(cap.rings([[-0.09, 0.06, 0.04, 0.04], [0, 0.13, 0.08, 0.05], [0.08, 0.07, 0.04, 0.04]]), 8, wool); + ears(s, h, [0.1, 0.1, 0.04], [0.08, 0.31, -0.03], 0.11, face); + eyes(s, h, [0.2, 0.13, 0.04], 0.04); + s.chain([[0, -0.54, 0.82], [0, -0.64, 0.7], [0, -0.66, 0.6]].map(q.body), [0.1, 0.1, 0.04], wool, 5); + }, +} satisfies Animal; diff --git a/builder/web/src/lib/trailview/models/animals/simmental-cattle.ts b/builder/web/src/lib/trailview/models/animals/simmental-cattle.ts new file mode 100644 index 0000000000..4e7a9f99a9 --- /dev/null +++ b/builder/web/src/lib/trailview/models/animals/simmental-cattle.ts @@ -0,0 +1,17 @@ +import { rgb } from '../../structures/mesh'; +import type { Animal } from '../animals'; +import { cattle } from './body/cattle'; +import { pelt, PINK } from './body/quadruped'; + +const WHITE = rgb('#ece6da'); +// A red back over a white flank and a white chest. +const MAP = ['.RRRRRR.', 'RRRRRRRR', '..RRRRRR', '.RRRRRR.', 'RRRRRRRR', '.RRRRRR.']; + +export default { + name: 'Simmental cattle', + latin: 'Bos taurus', + height: 1.45, + coats: ['#a85428', '#c88e4c'].map(rgb), + poses: 3, + build: (s, red, pose) => cattle(s, pose, { coat: red, trunk: pelt(MAP, { R: red }, WHITE), shin: WHITE, tuft: WHITE, head: (d, top) => (d > 0.9 ? PINK : d < 0.2 && top < 0.5 ? red : WHITE) }), +} satisfies Animal; diff --git a/builder/web/src/lib/trailview/models/bake.ts b/builder/web/src/lib/trailview/models/bake.ts index 530e6483bf..82f12f2d8f 100644 --- a/builder/web/src/lib/trailview/models/bake.ts +++ b/builder/web/src/lib/trailview/models/bake.ts @@ -3,13 +3,14 @@ import { KINDS } from '../kinds'; import { SEASONS, type Season } from '../rules/regions'; import { SUMMER } from '../vegetation/phenology'; -import { animal, BREEDS } from './animals'; +import { ANIMALS } 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 { mesh, Sculpt } from './sculpt'; import { lit, simplified, simplifierReady } from './simplify'; import { fits, tone } from './tone'; import type { Grown, Lod } from './trees'; @@ -36,8 +37,8 @@ const BUDGET: Record = { }; 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 } +/** What the build serves: each file by name, and the models with the names of their files; and the budget, for the review page. */ +export interface Baked { files: Map; models: Models; budget: typeof BUDGET } export async function bake(): Promise { await simplifierReady; @@ -74,7 +75,15 @@ export async function bake(): Promise { 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))]))) }; + // A breed's models, each coat in each pose, with a near and a far tier. + for (const [name, animal] of Object.entries(ANIMALS)) { + const built = animal.coats.flatMap(coat => Array.from({ length: animal.poses }, (_, pose) => pack([0, 1].map(tier => { + const s = new Sculpt(tier); + animal.build(s, coat, pose); + return mesh(s.model); + })))); + models.breeds[name] = { height: animal.height, coats: animal.coats.length, poses: animal.poses, file: await file('animal', `animal-${name}`, built) }; + } 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())); @@ -86,7 +95,7 @@ export async function bake(): Promise { 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 }; + return { files, models, budget: BUDGET }; } /** A crown of a species' own in a season: bare crowns grow each tier from the tree, the others simplify tier 0. */ diff --git a/builder/web/src/lib/trailview/models/flat.ts b/builder/web/src/lib/trailview/models/flat.ts index 1719a31953..0e50b8d566 100644 --- a/builder/web/src/lib/trailview/models/flat.ts +++ b/builder/web/src/lib/trailview/models/flat.ts @@ -23,6 +23,10 @@ export class Model { /** How much each triangle glows at night, 0 to 1, and whether it rises from its foot as the night falls, as a flame. */ readonly glows: number[] = []; readonly rises: boolean[] = []; + /** The polygon of each triangle: the triangles of one `poly` share it. */ + readonly faces: number[] = []; + private polygons = 0; + private open?: number; get triangles(): number { return this.colours.length; @@ -33,6 +37,7 @@ export class Model { this.colours.push(colour); this.glows.push(0); this.rises.push(false); + this.faces.push(this.open ?? this.polygons++); return this; } @@ -45,7 +50,9 @@ export class Model { /** A convex polygon, as a fan. */ poly(corners: Vec[], colour: number): this { + this.open = this.polygons++; for (let i = 1; i + 1 < corners.length; i++) this.tri(corners[0], corners[i], corners[i + 1], colour); + this.open = undefined; return this; } @@ -54,6 +61,8 @@ export class Model { this.colours.push(...other.colours); this.glows.push(...other.glows); this.rises.push(...other.rises); + this.faces.push(...other.faces.map(face => face + this.polygons)); + this.polygons += other.polygons; return this; } diff --git a/builder/web/src/lib/trailview/models/model-file.ts b/builder/web/src/lib/trailview/models/model-file.ts index 4240c429ea..9a466946e9 100644 --- a/builder/web/src/lib/trailview/models/model-file.ts +++ b/builder/web/src/lib/trailview/models/model-file.ts @@ -67,8 +67,8 @@ export function quantize(mesh: FloatMesh, extent: number): Uint8Array } -/** 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 } +/** A breed: its height in metres to the top of the back, its counts of coats and poses, and the file of its models, each coat in each pose. */ +export interface BreedEntry { height: number; coats: number; poses: number; file: string } /** * The models of the build (`virtual:models`): what the engine knows before it fetches a file. The objects file holds the diff --git a/builder/web/src/lib/trailview/models/sculpt.test.ts b/builder/web/src/lib/trailview/models/sculpt.test.ts new file mode 100644 index 0000000000..ebf9587732 --- /dev/null +++ b/builder/web/src/lib/trailview/models/sculpt.test.ts @@ -0,0 +1,30 @@ +import { describe, expect, it } from 'vitest'; +import { alongY, mesh, Sculpt } from './sculpt'; + +describe('the modelling kit', () => { + it('turns every face of a skin away from its axis, and keeps each facet as one normal with its corners once', () => { + const s = new Sculpt().skin(alongY([[-1, 0, 0.3, 0.4, 0.2], [0, 0, 0.5, 0.6, 0.3], [1, 0, 0.2, 0.2, 0.2]]), 8, 0x808080); + const { position: p, normal: n, index } = mesh(s.model); + for (let t = 0; t < index.length; t += 3) { + const [a, b, c] = [index[t], index[t + 1], index[t + 2]]; + const mid = [0, 1, 2].map(d => (p[3 * a + d] + p[3 * b + d] + p[3 * c + d]) / 3); + // Away from the y axis on the sides, along it at the caps. + const out = Math.abs(mid[1]) > 0.99 ? [0, mid[1], 0] : [mid[0], 0, mid[2]]; + expect(n[3 * a] * out[0] + n[3 * a + 1] * out[1] + n[3 * a + 2] * out[2]).toBeGreaterThan(0); + } + // 2 bands of 8 quads with 4 corners each, and 2 caps of 8 corners. + expect(p.length / 3).toBe(2 * 8 * 4 + 2 * 8); + expect(index.length / 3).toBe(2 * 8 * 2 + 2 * 6); + }); + + it('builds the far tier with fewer sides and without the fine details', () => { + const [near, far] = [0, 1].map(tier => { + const s = new Sculpt(tier).skin(alongY([[-1, 0, 0.3, 0.3, 0.3], [1, 0, 0.3, 0.3, 0.3]]), 10, 0); + const body = s.model.triangles; + s.fine(() => s.box([0, 0, 0], [0.1, 0.1, 0.1], 0)); + return { body, details: s.model.triangles - body }; + }); + expect(far.body).toBeLessThan(near.body); + expect([near.details, far.details]).toEqual([12, 0]); + }); +}); diff --git a/builder/web/src/lib/trailview/models/sculpt.ts b/builder/web/src/lib/trailview/models/sculpt.ts new file mode 100644 index 0000000000..64ce2d1bb3 --- /dev/null +++ b/builder/web/src/lib/trailview/models/sculpt.ts @@ -0,0 +1,157 @@ +// The modelling kit of the soft low-poly style (docs/content: the model style guide): round skins lofted through rings, +// flat leaves and chains of bars, in flat faces of one colour each. Every face turns away from a point inside its part, +// so no part needs a hand-made winding. A sculpt has a tier: tier 1 builds the same parts with fewer sides and leaves +// out the fine details, for the far tier of a model. +import { geometry, Model, type Vec } from './flat'; +import type { FloatMesh } from './model-file'; + +/** A ring of a skin: its middle, its half-width, its reach above and below the middle, the pitch of its axis above +y in degrees, and how square it is (2 is an ellipse). */ +export interface Ring { at: Vec; w: number; up: number; down: number; pitch: number; e: number } +/** + * The colour of a face from its band (−1 and the band count are the end caps), from how far up (−1 to 1) its facet looks, + * and from its place around the ring: 0 at +x, 0.25 at the top, 0.5 at −x. + */ +export type Paint = (band: number, top: number, around: number) => number; + +export const sub = (a: Vec, b: Vec): Vec => [a[0] - b[0], a[1] - b[1], a[2] - b[2]]; +export const along = (a: Vec, d: Vec, k: number): Vec => [a[0] + d[0] * k, a[1] + d[1] * k, a[2] + d[2] * k]; +const cross = (a: Vec, b: Vec): Vec => [a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0]]; +const unit = (a: Vec): Vec => along([0, 0, 0], a, 1 / (Math.hypot(...a) || 1)); +const bend = (v: number, e: number) => Math.sign(v) * Math.abs(v) ** (2 / e); + +/** A paint of one colour for each band; the caps take the first and the last. */ +export const bands = (...colours: number[]): Paint => band => colours[Math.max(0, Math.min(colours.length - 1, band + 1))]; +/** A paint with a belly: the facets that look down take `under`. */ +export const bellied = (coat: number, under: number): Paint => (_, top) => (top < -0.6 ? under : coat); +/** The same part at both sides: `f` takes −1 and 1. */ +export const both = (f: (s: number) => void) => [-1, 1].forEach(f); + +/** The rings of a part along y from rows of [y, z, half-width, up, down, pitch]. */ +export const alongY = (rows: number[][], e = 2): Ring[] => rows.map(([y, z, w, up, down, pitch = 0]) => ({ at: [0, y, z], w, up, down, pitch, e })); + +/** + * A frame along an axis from `at`, pitched `pitch` degrees above +y: `p(d, x, v)` is the point d along the axis, x to the + * side and v above it; `rings` takes rows of [d, half-width, up, down]. A head, a neck or a tail hangs from one. + */ +export function axis(at: Vec, pitch: number) { + const c = Math.cos((pitch * Math.PI) / 180), s = Math.sin((pitch * Math.PI) / 180); + const p = (d: number, x = 0, v = 0): Vec => [at[0] + x, at[1] + d * c - v * s, at[2] + d * s + v * c]; + return { p, dir: [0, c, s] as Vec, rings: (rows: number[][], e = 2): Ring[] => rows.map(([d, w, up, down]) => ({ at: p(d), w, up, down, pitch, e })) }; +} + +/** + * The rings of a part through points of one bend in the y-z plane, each ring square to the path, from rows of + * [half-width, up, down]: a bent neck, a folded leg. + */ +export function through(points: Vec[], rows: number[][], e = 2): Ring[] { + return points.map((at, i) => { + const a = points[Math.max(0, i - 1)], b = points[Math.min(points.length - 1, i + 1)], [w, up, down] = rows[i]; + return { at, w, up, down, pitch: (Math.atan2(b[2] - a[2], b[1] - a[1]) * 180) / Math.PI, e }; + }); +} + +/** A model in the making, at a tier: 0 near, 1 far. */ +export class Sculpt { + readonly model = new Model(); + constructor(readonly tier = 0) {} + + /** Sides of a part that has n at tier 0: 4 keep a round part the same at both sides, 3 are enough for a thin one. */ + sides(n: number): number { + return this.tier ? (n < 8 ? 3 : 4) : n; + } + + /** Fine details, which only tier 0 builds. */ + fine(f: () => void): this { + if (!this.tier) f(); + return this; + } + + /** A convex face whose normal points away from `inside`. */ + face(corners: Vec[], colour: number, inside: Vec): this { + const n = cross(sub(corners[1], corners[0]), sub(corners[2], corners[0])); + const mid = corners.reduce((sum, p) => along(sum, p, 1 / corners.length), [0, 0, 0] as Vec), out = sub(mid, inside); + this.model.poly(n[0] * out[0] + n[1] * out[1] + n[2] * out[2] < 0 ? [...corners].reverse() : corners, colour); + return this; + } + + /** + * A skin of `sides` facets (fewer at tier 1) through the rings, its ends closed; a ring of few sides grows to keep the + * area of its ellipse. `lump` pushes every second point out and the others in, as wool. + */ + skin(rings: Ring[], sides: number, paint: Paint | number, lump = 0): this { + const colour: Paint = typeof paint === 'number' ? () => paint : paint, last = rings.length - 1, n = this.sides(sides), full = Math.sqrt(Math.PI / ((n / 2) * Math.sin((2 * Math.PI) / n))); + const points = rings.map((r, i) => Array.from({ length: n }, (_, k): Vec => { + const t = (2 * Math.PI * (k + 0.5)) / n, p = (r.pitch * Math.PI) / 180, swell = full * (i > 0 && i < last ? 1 + lump * ((i + k) % 2 ? 1 : -1) : 1); + const s = bend(Math.sin(t), r.e), v = s * (s > 0 ? r.up : r.down) * swell; + return [r.at[0] + bend(Math.cos(t), r.e) * r.w * swell, r.at[1] - Math.sin(p) * v, r.at[2] + Math.cos(p) * v]; + })); + for (let i = 0; i < last; i++) for (let k = 0; k < n; k++) { + const j = (k + 1) % n; + this.face([points[i][k], points[i][j], points[i + 1][j], points[i + 1][k]], colour(i, Math.sin((2 * Math.PI * (k + 1)) / n), ((k + 1) / n) % 1), along(rings[i].at, sub(rings[i + 1].at, rings[i].at), 0.5)); + } + this.face(points[0], colour(-1, 0, 0), rings[1].at); + return this.face(points[last], colour(last, 0, 0), rings[last - 1].at); + } + + /** A flat diamond from a to its tip b, `width` across and `thick` along n: an ear, a beard, a short tail. */ + leaf(a: Vec, b: Vec, width: number, thick: number, n: Vec, colour: number): this { + const d = sub(b, a), s = unit(cross(d, n)), c = along(a, d, 0.4), t = unit(n); + const q = [along(c, s, width / 2), along(c, t, thick / 2), along(c, s, -width / 2), along(c, t, -thick / 2)]; + for (let k = 0; k < 4; k++) for (const end of [a, b]) this.face([end, q[k], q[(k + 1) % 4]], colour, c); + return this; + } + + /** Bars through the points, each `widths` across at its point, of `sides` sides: a horn, a tail. Tier 1 has fewer sides and goes through every second point and the last. */ + chain(points: Vec[], widths: number[], colour: number, sides = 4): this { + const keep = points.map((_, i) => i).filter(i => !this.tier || i % 2 === 0 || i === points.length - 1); + for (let k = 0; k + 1 < keep.length; k++) { + const a = points[keep[k]], b = points[keep[k + 1]], first = this.model.triangles; + this.model.beam(a, b, widths[keep[k]], colour, { sides: this.sides(sides), w1: widths[keep[k + 1]], capped: true }); + this.outward(first, along(a, sub(b, a), 0.5)); + } + return this; + } + + /** Turns each triangle from `first` on away from `inside`: Model.beam winds its start cap inward. */ + private outward(first: number, inside: Vec): void { + const p = this.model.points; + for (let i = 9 * first; i < p.length; i += 9) { + const a: Vec = [p[i], p[i + 1], p[i + 2]], b: Vec = [p[i + 3], p[i + 4], p[i + 5]], c: Vec = [p[i + 6], p[i + 7], p[i + 8]]; + const n = cross(sub(b, a), sub(c, a)), out = sub(along(along(a, b, 1), c, 1).map(v => v / 3) as Vec, inside); + if (n[0] * out[0] + n[1] * out[1] + n[2] * out[2] < 0) p.splice(i + 3, 6, ...c, ...b); + } + } + + /** A box about its middle: an eye, a nostril. */ + box(at: Vec, size: Vec, colour: number): this { + this.model.box(at, size, colour); + return this; + } +} + +/** + * The mesh of a model as the instances read it: each polygon lit flat by one normal (Newell's, so a lofted quad that is + * not quite planar draws as one facet), its corners kept once. + */ +export function mesh(model: Model): FloatMesh { + const flat = geometry(model), p = model.points, normals = new Map(); + model.faces.forEach((face, t) => { + const n = normals.get(face) ?? [0, 0, 0], i = 9 * t; + const c = cross([p[i + 3] - p[i], p[i + 4] - p[i + 1], p[i + 5] - p[i + 2]], [p[i + 6] - p[i], p[i + 7] - p[i + 1], p[i + 8] - p[i + 2]]); + normals.set(face, along(n, c, 1)); + }); + const at = new Map(), out = { position: [] as number[], normal: [] as number[], color: [] as number[], index: [] as number[] }; + for (let v = 0; v < flat.position.length / 3; v++) { + const position = [0, 1, 2].map(d => flat.position[3 * v + d]), normal = unit(normals.get(model.faces[Math.floor(v / 3)])!), color = [0, 1, 2].map(d => flat.color[3 * v + d]); + const key = [...position, ...normal, ...color].join(); + let k = at.get(key); + if (k === undefined) { + at.set(key, (k = out.position.length / 3)); + out.position.push(...position); + out.normal.push(...normal); + out.color.push(...color); + } + out.index.push(k); + } + return out; +} diff --git a/builder/web/tests/browser/trailview/models.mjs b/builder/web/tests/browser/trailview/models.mjs new file mode 100644 index 0000000000..2a52575bda --- /dev/null +++ b/builder/web/tests/browser/trailview/models.mjs @@ -0,0 +1,66 @@ +// The model review page: each breed's models in the engine's light, near at tier 0 and close up at tier 1, with the +// triangles and vertices of each tier against the budget, and the models of another build beside them. +// +// node builder/web/tests/browser/trailview/models.mjs [--out DIR] [--before DIR] [--only a,b] [SERVED] +// +// The page must be built (`box.sh` or `npm run build:trailview`), from the same checkout. SERVED are the data +// options of shoot.mjs (--tiles, --ground, --features); the place is a flat meadow of the Engelberg box. --before names +// the folder of another run: its `-near.png` stands beside each breed (today's model, for a change of models). +// Writes DIR/index.html and one PNG per breed and tier; the showroom of dressing.ts stands the animals. + +import { spawnSync } from 'node:child_process'; +import { copyFileSync, existsSync, mkdirSync, writeFileSync } from 'node:fs'; +import { join } from 'node:path'; +import { parseArgs } from 'node:util'; +import { bakeModels } from '../../../vite/model-files.ts'; +import { decodeModels } from '../../../src/lib/trailview/models/model-file.ts'; + +const { values } = parseArgs({ + options: { + out: { type: 'string', default: new URL('../../../../../.artifacts/trailview-models', import.meta.url).pathname }, + before: { type: 'string' }, + only: { type: 'string' }, + tiles: { type: 'string' }, + ground: { type: 'string' }, + features: { type: 'string' }, + }, +}); +/** The showroom's middle, the eye's heading toward it, and the page; SHOW_SPACING of dressing.ts. */ +const PLACE = [46.820416, 8.403045], HEADING = 160, WIDTH = 1600, HEIGHT = 640, SPACING = 2.3; +const { baked } = await bakeModels(); +const BUDGET = baked.budget.animal; +const only = values.only?.split(','); +const breeds = Object.entries(baked.models.breeds).filter(([name]) => !only || only.includes(name)); +mkdirSync(values.out, { recursive: true }); + +// The eye stands far enough before the row to see all of it. +const views = breeds.flatMap(([name, { height, coats, poses }]) => { + const distance = Math.max(6, (0.5 * coats * poses * SPACING * height) / 1.1), a = (HEADING * Math.PI) / 180; + const lat = PLACE[0] - (distance * Math.cos(a)) / 111320, lon = PLACE[1] - (distance * Math.sin(a)) / (111320 * Math.cos((PLACE[0] * Math.PI) / 180)); + const view = (tier) => `?lat=${lat.toFixed(7)}&lon=${lon.toFixed(7)}&heading=${HEADING}&pitch=-6&eye=1.6&date=2026-08-15&time=15:00&weather=sunny&showroom=${PLACE.join(',')},${HEADING},${name},${tier}`; + return [{ name: `${name}-near`, view: view(0) }, { name: `${name}-tier1`, view: view(1) }]; +}); +writeFileSync(join(values.out, 'views.json'), JSON.stringify(views)); +const served = ['tiles', 'ground', 'features'].flatMap(key => (values[key] ? [`--${key}`, values[key]] : [])); +const shot = spawnSync(process.execPath, [new URL('./shoot.mjs', import.meta.url).pathname, '--gpu', '--views', join(values.out, 'views.json'), '--out', values.out, '--width', `${WIDTH}`, '--height', `${HEIGHT}`, ...served], { stdio: 'inherit' }); +if (shot.status !== 0) process.exit(shot.status ?? 1); + +const rows = []; +for (const [name, { height, coats, poses, file }] of breeds) { + const models = await decodeModels(baked.files.get(file)); + const counts = models.map(({ tiers }, k) => `${k}: coat ${Math.floor(k / poses)}, pose ${k % poses}${tiers.map(({ vertices, index }, t) => { + const tris = index.length / 3, verts = vertices.length / 16, over = tris > BUDGET[t][0] || verts > BUDGET[t][1]; + return `${tris} / ${BUDGET[t][0]}${verts} / ${BUDGET[t][1]}`; + }).join('')}`).join(''); + const before = values.before && join(values.before, `${name}-near.png`); + if (before && existsSync(before)) copyFileSync(before, join(values.out, `${name}-before.png`)); + rows.push(`

${name}

${height} m to the back, ${coats} coats × ${poses} poses, ${baked.files.get(file).length} bytes

+${before && existsSync(before) ? `
Before
` : ''} +
Tier 0, to 150 m
+
Tier 1, to 1,500 m, drawn close
+${counts}
ModelTier 0 trianglesTier 0 verticesTier 1 trianglesTier 1 vertices
`); +} +writeFileSync(join(values.out, 'index.html'), `TrailView models + +

TrailView models

${rows.join('\n')}`); +console.log(join(values.out, 'index.html')); diff --git a/builder/web/vite/model-files.ts b/builder/web/vite/model-files.ts index edf2376765..98c48ef318 100644 --- a/builder/web/vite/model-files.ts +++ b/builder/web/vite/model-files.ts @@ -22,7 +22,7 @@ const MARK = /"@@model-file:([\w-]+)@@"/g; 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[] }> { +export 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 } } }, diff --git a/data/trailview/packs/alps.json b/data/trailview/packs/alps.json index 86f360168d..046dae11e6 100644 --- a/data/trailview/packs/alps.json +++ b/data/trailview/packs/alps.json @@ -35,8 +35,8 @@ "simmental-cattle": { "groups": 1, "size": [3, 12], "on": ["pasture", "grassland"] }, "alpine-sheep": { "model": "sheep", "latin": "Ovis aries (White Alpine)", "groups": 1, "size": [10, 40], "on": ["rough-grass", "heath"] }, "goat": { "groups": 1, "size": [3, 10], "on": ["rough-grass", "scrub", "heath"] }, - "chamois": { "model": "goat", "latin": "Rupicapra rupicapra", "groups": 1, "size": [1, 5], "on": ["scree", "bare-rock"] }, - "alpine-ibex": { "model": "goat", "latin": "Capra ibex", "groups": 1, "size": [2, 6], "on": ["bare-rock", "scree"] } + "chamois": { "latin": "Rupicapra rupicapra", "groups": 1, "size": [1, 5], "on": ["scree", "bare-rock"] }, + "alpine-ibex": { "model": "ibex", "latin": "Capra ibex", "groups": 1, "size": [2, 6], "on": ["bare-rock", "scree"] } }, "rows": { "orchard": "hawthorn" }, "style": { "si": "black-forest-farmhouse", "it-21": "slate-stone-house", "it-23": "slate-stone-house", "it-25": "slate-stone-house", "ch-ti": "slate-stone-house", "fr-pac": "slate-stone-house", "de-by": "bavarian-chalet", "at": "bavarian-chalet", "*": "chalet" } diff --git a/data/trailview/packs/apennines.json b/data/trailview/packs/apennines.json index 64d1e61d1d..fa202f0744 100644 --- a/data/trailview/packs/apennines.json +++ b/data/trailview/packs/apennines.json @@ -36,7 +36,7 @@ "cattle": { "model": "highland-cow", "latin": "Bos taurus (Marchigiana)", "groups": 2, "size": [4, 15], "on": ["pasture", "grassland", "arable"] }, "horse": { "groups": 2, "size": [3, 12], "on": ["pasture", "grassland", "arable", "rough-grass", "wet-grassland"] }, "goat": { "groups": 1, "size": [3, 10], "on": ["rough-grass", "wet-grassland", "grassland", "scrub", "sclerophyllous-scrub", "bare-rock", "scree", "sparse"] }, - "pyrenean-chamois": { "model": "goat", "latin": "Rupicapra pyrenaica", "groups": 1, "size": [3, 12], "on": ["bare-rock", "scree", "sparse", "rough-grass", "wet-grassland", "grassland"] } + "pyrenean-chamois": { "model": "chamois", "latin": "Rupicapra pyrenaica", "groups": 1, "size": [3, 12], "on": ["bare-rock", "scree", "sparse", "rough-grass", "wet-grassland", "grassland"] } }, "style": { "it-45": "slate-stone-house", "it-52": "slate-stone-house", "*": "stone-house" } } diff --git a/data/trailview/packs/caledon.json b/data/trailview/packs/caledon.json index f5218b376e..f3a4b35c99 100644 --- a/data/trailview/packs/caledon.json +++ b/data/trailview/packs/caledon.json @@ -36,11 +36,11 @@ "foxglove": { "model": "alpine-flowers", "latin": "Digitalis purpurea", "on": ["grass", "shrub"], "height": [0.8, 1.5], "share": 0.03, "clumped": true } }, "herds": { - "red-deer": { "model": "goat", "latin": "Cervus elaphus", "groups": 3, "size": [4, 25], "on": ["heath", "rough-grass", "wet-grassland", "grassland", "bog"] }, + "red-deer": { "latin": "Cervus elaphus", "groups": 3, "size": [4, 25], "on": ["heath", "rough-grass", "wet-grassland", "grassland", "bog"] }, "sheep": { "groups": 4, "size": [3, 20], "on": ["pasture", "grassland", "arable", "rough-grass", "wet-grassland", "heath"] }, "highland-cow": { "groups": 1, "size": [2, 8], "on": ["pasture", "grassland", "arable", "rough-grass", "wet-grassland"] }, "aberdeen-angus": { "model": "highland-cow", "latin": "Bos taurus (Aberdeen Angus)", "groups": 2, "size": [5, 20], "on": ["pasture", "grassland", "arable"] }, - "highland-pony": { "model": "highland-cow", "latin": "Equus caballus (Highland)", "groups": 1, "size": [1, 4], "on": ["pasture", "grassland", "arable"] } + "highland-pony": { "model": "horse", "latin": "Equus caballus (Highland)", "groups": 1, "size": [1, 4], "on": ["pasture", "grassland", "arable"] } }, "style": "highland-cottage" } diff --git a/data/trailview/packs/canaries-east.json b/data/trailview/packs/canaries-east.json index fde4ca1a79..f197ac61bc 100644 --- a/data/trailview/packs/canaries-east.json +++ b/data/trailview/packs/canaries-east.json @@ -19,7 +19,7 @@ }, "herds": { "goat": { "groups": 2, "size": [5, 25], "on": ["rough-grass", "wet-grassland", "grassland", "scrub", "sclerophyllous-scrub", "bare-rock", "scree", "sparse"] }, - "dromedary": { "model": "highland-cow", "latin": "Camelus dromedarius", "groups": 1, "size": [2, 8], "on": ["scrub", "sclerophyllous-scrub", "rough-grass", "wet-grassland", "grassland"] } + "dromedary": { "latin": "Camelus dromedarius", "groups": 1, "size": [2, 8], "on": ["scrub", "sclerophyllous-scrub", "rough-grass", "wet-grassland", "grassland"] } }, "style": "canarian-house" } diff --git a/data/trailview/packs/corsica.json b/data/trailview/packs/corsica.json index 4e21ffeb27..cc0205928a 100644 --- a/data/trailview/packs/corsica.json +++ b/data/trailview/packs/corsica.json @@ -27,7 +27,7 @@ }, "herds": { "cattle": { "model": "brown-cattle", "groups": 2, "size": [3, 10], "on": ["pasture", "grassland", "arable", "rough-grass", "wet-grassland", "scrub", "sclerophyllous-scrub"] }, - "iberian-pig": { "model": "goat", "latin": "Sus scrofa domesticus", "groups": 2, "size": [3, 10], "on": ["rough-grass", "wet-grassland", "grassland", "scrub", "sclerophyllous-scrub"] }, + "iberian-pig": { "model": "pig", "latin": "Sus scrofa domesticus", "groups": 2, "size": [3, 10], "on": ["rough-grass", "wet-grassland", "grassland", "scrub", "sclerophyllous-scrub"] }, "goat": { "groups": 2, "size": [5, 20], "on": ["rough-grass", "wet-grassland", "grassland", "scrub", "sclerophyllous-scrub", "bare-rock", "scree", "sparse"] }, "sheep": { "groups": 2, "size": [10, 40], "on": ["pasture", "grassland", "arable", "rough-grass", "wet-grassland"] }, "horse": { "groups": 1, "size": [2, 8], "on": ["pasture", "grassland", "arable", "rough-grass", "wet-grassland"] }, diff --git a/data/trailview/packs/fjell.json b/data/trailview/packs/fjell.json index e96b72feef..a218d04c7b 100644 --- a/data/trailview/packs/fjell.json +++ b/data/trailview/packs/fjell.json @@ -24,7 +24,7 @@ "grass": { "off": ["#moor"] } }, "herds": { - "reindeer": { "model": "goat", "latin": "Rangifer tarandus", "groups": 3, "size": [3, 12], "on": ["heath", "rough-grass", "wet-grassland", "grassland", "bog"] }, + "reindeer": { "latin": "Rangifer tarandus", "groups": 3, "size": [3, 12], "on": ["heath", "rough-grass", "wet-grassland", "grassland", "bog"] }, "sheep": { "groups": 3, "size": [2, 6], "on": ["pasture", "grassland", "arable", "rough-grass", "wet-grassland", "heath"] } }, "style": "falu-red-house", diff --git a/data/trailview/packs/iberia.json b/data/trailview/packs/iberia.json index 1f93640690..a70801626d 100644 --- a/data/trailview/packs/iberia.json +++ b/data/trailview/packs/iberia.json @@ -23,7 +23,7 @@ }, "herds": { "sheep": { "groups": 2, "size": [15, 60], "on": ["pasture", "grassland", "arable"] }, - "iberian-pig": { "model": "goat", "latin": "Sus scrofa domesticus", "groups": 1, "size": [4, 15], "on": ["rough-grass", "wet-grassland", "grassland"] }, + "iberian-pig": { "model": "pig", "latin": "Sus scrofa domesticus", "groups": 1, "size": [4, 15], "on": ["rough-grass", "wet-grassland", "grassland"] }, "cattle": { "model": "brown-cattle", "groups": 1, "size": [4, 15], "on": ["rough-grass", "wet-grassland", "grassland"] }, "goat": { "groups": 1, "size": [8, 30], "on": ["scrub", "sclerophyllous-scrub", "bare-rock", "scree", "sparse"] } }, diff --git a/data/trailview/packs/iberian-conifer.json b/data/trailview/packs/iberian-conifer.json index 84f30de2ab..c42b6e936c 100644 --- a/data/trailview/packs/iberian-conifer.json +++ b/data/trailview/packs/iberian-conifer.json @@ -34,7 +34,7 @@ "goat": { "groups": 1, "size": [8, 30], "on": ["rough-grass", "wet-grassland", "grassland", "scrub", "sclerophyllous-scrub", "bare-rock", "scree", "sparse"] }, "cattle": { "model": "brown-cattle", "groups": 2, "size": [4, 15], "on": ["pasture", "grassland", "arable", "rough-grass", "wet-grassland"] }, "horse": { "groups": 1, "size": [3, 10], "on": ["rough-grass", "wet-grassland", "grassland", "heath", "pasture", "arable"] }, - "iberian-ibex": { "model": "goat", "latin": "Capra pyrenaica", "groups": 1, "size": [3, 12], "on": ["bare-rock", "scree", "sparse"] } + "iberian-ibex": { "model": "ibex", "latin": "Capra pyrenaica", "groups": 1, "size": [3, 12], "on": ["bare-rock", "scree", "sparse"] } }, "rows": { "orchard": "hawthorn" }, "style": "village-house" diff --git a/data/trailview/packs/iceland.json b/data/trailview/packs/iceland.json index be10c1f1d6..748ba5a58a 100644 --- a/data/trailview/packs/iceland.json +++ b/data/trailview/packs/iceland.json @@ -26,7 +26,7 @@ }, "herds": { "icelandic-sheep": { "model": "sheep", "latin": "Ovis aries (Icelandic)", "groups": 5, "size": [2, 4], "on": ["pasture", "grassland", "arable", "rough-grass", "wet-grassland", "heath", "bog"] }, - "icelandic-horse": { "model": "highland-cow", "latin": "Equus caballus (Icelandic)", "groups": 2, "size": [3, 12], "on": ["pasture", "grassland", "arable", "rough-grass", "wet-grassland"] }, + "icelandic-horse": { "model": "horse", "latin": "Equus caballus (Icelandic)", "groups": 2, "size": [3, 12], "on": ["pasture", "grassland", "arable", "rough-grass", "wet-grassland"] }, "icelandic-cattle": { "model": "highland-cow", "latin": "Bos taurus (Icelandic)", "groups": 1, "size": [3, 10], "on": ["pasture", "grassland", "arable"] } }, "style": "cottage" diff --git a/data/trailview/packs/illyria.json b/data/trailview/packs/illyria.json index 5edcb07ea8..37c3f297b0 100644 --- a/data/trailview/packs/illyria.json +++ b/data/trailview/packs/illyria.json @@ -44,7 +44,7 @@ "sheep": { "groups": 3, "size": [10, 60], "on": ["pasture", "grassland", "arable", "rough-grass", "wet-grassland"] }, "goat": { "groups": 3, "size": [4, 20], "on": ["rough-grass", "wet-grassland", "grassland", "scrub", "sclerophyllous-scrub", "bare-rock", "scree", "sparse"] }, "cattle": { "model": "highland-cow", "latin": "Bos taurus (Busha)", "groups": 1, "size": [2, 8], "on": ["pasture", "grassland", "arable"] }, - "donkey": { "model": "highland-cow", "latin": "Equus asinus", "groups": 1, "size": [1, 3], "on": ["rough-grass", "wet-grassland", "grassland", "pasture", "arable"] } + "donkey": { "latin": "Equus asinus", "groups": 1, "size": [1, 3], "on": ["rough-grass", "wet-grassland", "grassland", "pasture", "arable"] } }, "rows": { "orchard": "almond" }, "style": { "al": "village-house", "*": "stone-house" } diff --git a/data/trailview/packs/kola.json b/data/trailview/packs/kola.json index e22c4c2c44..d8edb1472b 100644 --- a/data/trailview/packs/kola.json +++ b/data/trailview/packs/kola.json @@ -23,7 +23,7 @@ "lyme-grass": { "model": "esparto", "latin": "Leymus arenarius", "on": ["bare"], "height": [0.5, 1.0], "share": 0.03, "clumped": true } }, "herds": { - "reindeer": { "model": "goat", "latin": "Rangifer tarandus", "groups": 3, "size": [3, 12], "on": ["heath", "rough-grass", "wet-grassland", "grassland", "bog"] }, + "reindeer": { "latin": "Rangifer tarandus", "groups": 3, "size": [3, 12], "on": ["heath", "rough-grass", "wet-grassland", "grassland", "bog"] }, "sheep": { "groups": 1, "size": [2, 6], "on": ["pasture", "grassland", "arable", "rough-grass", "wet-grassland"] } }, "style": "wood-cabin" diff --git a/data/trailview/packs/norway-coast.json b/data/trailview/packs/norway-coast.json index 2a774f1b1e..c62da7ddad 100644 --- a/data/trailview/packs/norway-coast.json +++ b/data/trailview/packs/norway-coast.json @@ -34,7 +34,7 @@ "old-norwegian-sheep": { "model": "sheep", "latin": "Ovis aries (Old Norwegian)", "groups": 2, "size": [3, 12], "on": ["heath", "rough-grass", "wet-grassland", "grassland"] }, "goat": { "groups": 1, "size": [4, 15], "on": ["rough-grass", "wet-grassland", "grassland", "scrub", "sclerophyllous-scrub"] }, "norwegian-red-cattle": { "model": "highland-cow", "latin": "Bos taurus (Norwegian Red)", "groups": 2, "size": [3, 10], "on": ["pasture", "grassland", "arable"] }, - "fjord-horse": { "model": "highland-cow", "latin": "Equus caballus (Fjord)", "groups": 1, "size": [1, 4], "on": ["pasture", "grassland", "arable"] } + "fjord-horse": { "model": "horse", "latin": "Equus caballus (Fjord)", "groups": 1, "size": [1, 4], "on": ["pasture", "grassland", "arable"] } }, "style": "wood-cabin", "houses": "cottage" diff --git a/data/trailview/packs/nw-iberia.json b/data/trailview/packs/nw-iberia.json index b55d2dbf0a..3b72711cd6 100644 --- a/data/trailview/packs/nw-iberia.json +++ b/data/trailview/packs/nw-iberia.json @@ -42,7 +42,7 @@ "sheep": { "groups": 3, "size": [15, 60], "on": ["pasture", "grassland", "arable", "rough-grass", "wet-grassland", "heath"] }, "goat": { "groups": 1, "size": [5, 20], "on": ["rough-grass", "wet-grassland", "grassland", "scrub", "sclerophyllous-scrub", "bare-rock", "scree", "sparse"] }, "horse": { "groups": 1, "size": [3, 10], "on": ["heath", "rough-grass", "wet-grassland", "grassland"] }, - "iberian-ibex": { "model": "goat", "latin": "Capra pyrenaica", "groups": 1, "size": [3, 12], "on": ["bare-rock", "scree", "sparse"] } + "iberian-ibex": { "model": "ibex", "latin": "Capra pyrenaica", "groups": 1, "size": [3, 12], "on": ["bare-rock", "scree", "sparse"] } }, "rows": { "orchard": "hawthorn" }, "style": { "es-cl": "slate-stone-house", "es-ga": "slate-stone-house", "pt-04": "slate-stone-house", "es-ex": "village-house", "es-cm": "village-house", "pt-05": "village-house", "pt-12": "village-house", "*": "stone-house" } diff --git a/data/trailview/packs/pyrenees.json b/data/trailview/packs/pyrenees.json index 130bcbb70d..73913b03c9 100644 --- a/data/trailview/packs/pyrenees.json +++ b/data/trailview/packs/pyrenees.json @@ -41,7 +41,7 @@ "sheep": { "groups": 3, "size": [10, 40], "on": ["pasture", "grassland", "arable", "rough-grass", "wet-grassland", "heath"] }, "horse": { "groups": 2, "size": [3, 10], "on": ["rough-grass", "wet-grassland", "grassland", "heath", "pasture", "arable"] }, "goat": { "groups": 1, "size": [3, 10], "on": ["rough-grass", "wet-grassland", "grassland", "scrub", "sclerophyllous-scrub", "bare-rock", "scree", "sparse"] }, - "pyrenean-chamois": { "model": "goat", "latin": "Rupicapra pyrenaica", "groups": 1, "size": [2, 8], "on": ["bare-rock", "scree", "sparse", "rough-grass", "wet-grassland", "grassland"] } + "pyrenean-chamois": { "model": "chamois", "latin": "Rupicapra pyrenaica", "groups": 1, "size": [2, 8], "on": ["bare-rock", "scree", "sparse", "rough-grass", "wet-grassland", "grassland"] } }, "rows": { "orchard": "hawthorn" }, "style": "slate-stone-house" diff --git a/data/trailview/packs/south-apennines.json b/data/trailview/packs/south-apennines.json index 9ac73641ac..491d5a97c8 100644 --- a/data/trailview/packs/south-apennines.json +++ b/data/trailview/packs/south-apennines.json @@ -42,7 +42,7 @@ "goat": { "groups": 2, "size": [4, 15], "on": ["rough-grass", "wet-grassland", "grassland", "scrub", "sclerophyllous-scrub", "bare-rock", "scree", "sparse"] }, "cattle": { "model": "highland-cow", "latin": "Bos taurus (Podolica)", "groups": 2, "size": [4, 15], "on": ["pasture", "grassland", "arable", "rough-grass", "wet-grassland"] }, "horse": { "groups": 2, "size": [3, 12], "on": ["pasture", "grassland", "arable", "rough-grass", "wet-grassland"] }, - "iberian-pig": { "model": "goat", "latin": "Sus scrofa domesticus", "groups": 1, "size": [3, 10], "on": ["rough-grass", "wet-grassland", "grassland", "scrub", "sclerophyllous-scrub"] } + "iberian-pig": { "model": "pig", "latin": "Sus scrofa domesticus", "groups": 1, "size": [3, 10], "on": ["rough-grass", "wet-grassland", "grassland", "scrub", "sclerophyllous-scrub"] } }, "style": "stone-house" } diff --git a/data/trailview/packs/sw-iberia.json b/data/trailview/packs/sw-iberia.json index 81b45964fa..6d441fa8c1 100644 --- a/data/trailview/packs/sw-iberia.json +++ b/data/trailview/packs/sw-iberia.json @@ -30,7 +30,7 @@ }, "herds": { "cattle": { "model": "brown-cattle", "groups": 2, "size": [5, 25], "on": ["pasture", "grassland", "arable", "rough-grass", "wet-grassland"] }, - "iberian-pig": { "model": "goat", "latin": "Sus scrofa domesticus", "groups": 1, "size": [4, 15], "on": ["rough-grass", "wet-grassland", "grassland"] }, + "iberian-pig": { "model": "pig", "latin": "Sus scrofa domesticus", "groups": 1, "size": [4, 15], "on": ["rough-grass", "wet-grassland", "grassland"] }, "sheep": { "groups": 2, "size": [15, 60], "on": ["pasture", "grassland", "arable", "rough-grass", "wet-grassland"] }, "goat": { "groups": 1, "size": [8, 30], "on": ["scrub", "sclerophyllous-scrub", "bare-rock", "scree", "sparse"] }, "horse": { "groups": 1, "size": [3, 12], "on": ["pasture", "grassland", "arable"] } diff --git a/data/trailview/packs/tyrrhenian.json b/data/trailview/packs/tyrrhenian.json index 75369ab253..d9708213a8 100644 --- a/data/trailview/packs/tyrrhenian.json +++ b/data/trailview/packs/tyrrhenian.json @@ -43,8 +43,8 @@ "goat": { "groups": 2, "size": [5, 25], "on": ["rough-grass", "wet-grassland", "grassland", "scrub", "sclerophyllous-scrub", "bare-rock", "scree", "sparse"] }, "cattle": { "model": "brown-cattle", "groups": 1, "size": [3, 12], "on": ["pasture", "grassland", "arable", "rough-grass", "wet-grassland"] }, "horse": { "groups": 1, "size": [2, 6], "on": ["pasture", "grassland", "arable"] }, - "donkey": { "model": "highland-cow", "latin": "Equus asinus", "groups": 1, "size": [1, 4], "on": ["rough-grass", "wet-grassland", "grassland", "pasture", "arable"] }, - "iberian-pig": { "model": "goat", "latin": "Sus scrofa domesticus", "groups": 1, "size": [3, 10], "on": ["rough-grass", "wet-grassland", "grassland", "scrub", "sclerophyllous-scrub"] } + "donkey": { "latin": "Equus asinus", "groups": 1, "size": [1, 4], "on": ["rough-grass", "wet-grassland", "grassland", "pasture", "arable"] }, + "iberian-pig": { "model": "pig", "latin": "Sus scrofa domesticus", "groups": 1, "size": [3, 10], "on": ["rough-grass", "wet-grassland", "grassland", "scrub", "sclerophyllous-scrub"] } }, "rows": { "orchard": "almond" }, "style": { "mt": "canarian-house", "it-75": "canarian-house", "*": "village-house" } diff --git a/docs/assets/trailview-models/lineup.jpg b/docs/assets/trailview-models/lineup.jpg new file mode 100644 index 0000000000..ba83a1c6bd Binary files /dev/null and b/docs/assets/trailview-models/lineup.jpg differ diff --git a/docs/assets/trailview-models/poses.jpg b/docs/assets/trailview-models/poses.jpg new file mode 100644 index 0000000000..1fddd6b1cc Binary files /dev/null and b/docs/assets/trailview-models/poses.jpg differ diff --git a/docs/assets/trailview-models/tiers.jpg b/docs/assets/trailview-models/tiers.jpg new file mode 100644 index 0000000000..b7172211b5 Binary files /dev/null and b/docs/assets/trailview-models/tiers.jpg differ diff --git a/docs/content/nav.json b/docs/content/nav.json index 4475895049..abf437cdbb 100644 --- a/docs/content/nav.json +++ b/docs/content/nav.json @@ -19,6 +19,7 @@ { "path": "software/packer-routing", "title": "Packer & routing" }, { "path": "software/terrain", "title": "Terrain & elevation" }, { "path": "software/trailview-roads", "title": "TrailView roads" }, + { "path": "software/trailview-models", "title": "TrailView models" }, { "path": "software/companion-link", "title": "The companion link" }, { "path": "software/trips", "title": "Trips and rides" }, { "path": "software/ride-replay", "title": "Ride replay" }, diff --git a/docs/content/software/trailview-models.md b/docs/content/software/trailview-models.md new file mode 100644 index 0000000000..e9fd22938c --- /dev/null +++ b/docs/content/software/trailview-models.md @@ -0,0 +1,70 @@ +--- +title: How TrailView models look +description: The one style of every TrailView model, and why we make each model in code with one kit. +--- + +# How TrailView models look + +TrailView draws many small models: animals, trees, plants, rocks, benches and other objects. All of +them have one style, "soft low-poly". This page says what the style is and why we keep it. The bytes +of a model file and the triangle budget of each kind are in the +[model contract](src:specs/trailview-models.md). + +![A row of the herd animals on a meadow in TrailView: cattle, sheep, goats, a horse, a donkey, a pig, a dromedary, deer, reindeer, ibex and chamois](../../assets/trailview-models/lineup.jpg) + +## The style + +A model is low in detail and looks like a model from a game. It is friendly and never odd. A rider +sees it for a moment, at the side of a trail, so its shape must read at once. + +- **Round, faceted shapes.** A body is a skin through rings of 8 to 12 sides. Each face has one flat + colour and one normal, so the facets show and the light gives the shape. +- **Clear outlines.** A model reads from the side and from above. Legs, ears, horns and tails stand + out from the body. A detail that a rider cannot see at 10 m does not get a triangle. +- **Friendly proportions.** An animal has a slightly large head with eyes and a pale muzzle, short + sturdy legs and a round body. The proportions come from the real species; the style only rounds + and softens them. +- **Few colours.** A coat has two or three colours in large areas: a back, a belly, a face, patches + with clear edges. Many small colour changes look like noise at a distance. + +## Why we make every model in code + +We make all models ourselves, in TypeScript, with one shared kit +([`sculpt.ts`](src:builder/web/src/lib/trailview/models/sculpt.ts)). We do not ship models from +asset packs or from image-to-3-D tools, even with a free licence. A model from another source has +another style, and two sources never agree. Packs and photos are only references. + +The kit has few parts: + +- a **skin** through rings, with a separate reach above and below the middle, a pitch and a + squareness, for bodies, necks, heads and legs; +- a **leaf**, a flat diamond, for ears, beards and short tails; +- a **chain** of bars for horns, antlers and tails. + +Each face of a part turns away from a point inside the part, so no model needs a hand-made winding +order. The animals share one more layer, a four-legged body +([`quadruped.ts`](src:builder/web/src/lib/trailview/models/animals/body/quadruped.ts)) that a +breed fills with its numbers. + +## Tiers: one build, two levels of detail + +Each model builds its near tier and its far tier from the same code. The far tier uses fewer sides +and leaves out fine details such as eyes and udders. So the outline and the colour areas stay the +same when an animal moves from one tier to the other. For an animal, the near tier ends at 150 m. +Past it, an animal is only a few pixels tall. + +![The cattle of TrailView in the near tier (left) and the far tier (right), drawn at the same size](../../assets/trailview-models/tiers.jpg) + +## Poses and coats + +An animal has two or three poses: it stands, it grazes, or it lies. A herd mixes them, so a group +looks alive. A breed has one to four coats, and most animals of a group wear the first coat. + +![A brown cow standing, grazing and lying](../../assets/trailview-models/poses.jpg) + +## A new model + +A new model or a change to a model is a change of look. The review page +(`builder/web/tests/browser/trailview/models.mjs`) shows each model in TrailView's own light, near +and far, with its triangle and vertex counts against the budget, and the old model beside it. The +owner approves the pictures before the change merges. diff --git a/specs/trailview-models.md b/specs/trailview-models.md index f70d5468ef..e97947ebf9 100644 --- a/specs/trailview-models.md +++ b/specs/trailview-models.md @@ -31,7 +31,7 @@ has a new address. The module `virtual:models` of the build is the manifest (`Mo | 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` | +| A breed of animal | 1 per coat and pose, coat by coat (model `coat × poses + pose`), 2 tiers each | `breeds[name].file`, with `height`, `coats` and `poses` | | 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 @@ -92,7 +92,7 @@ where each comes from. | 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 | +| Animal | 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