From 8581fac81105d500bad5929797a023ec1b227b6d Mon Sep 17 00:00:00 2001 From: shinyobjectz Date: Thu, 19 Nov 2026 08:36:34 -0800 Subject: [PATCH] raea/wear: a recipe per material class (WR5) wearBake bakes a model's causes once (the native call, now typed), and wearShader draws them as wear by material class: one WESL function per class (plaster, brick, sandstone, granite, wood, iron, zinc, copper, slate, felt, glass, enamel, brass, ground), the stains, rising damp and splash they share, and WR9's season and weather over them, from uniforms (age: years, soot, upkeep; weather: day, wet, snow, storm, ice). A debug mode draws any of 14 causes in false colour from a lit hook. A mesh without the bake's second UV set is left as it was. The bake (raea-sys-weathering 6) writes each texel's class into causes2's spare alpha, read by textureLoad, so one shader serves every material; dilation copies class and element into the gutters instead of averaging two into a third; a primitive with no UV set gets the atlas's as TEXCOORD_0 too, since Bevy's standard surface reads the first wherever there's a second. Proof: bun test in sdk/wear (12), cargo test -p raea-sys-weathering (11), and a street front of all 14 classes baked and rendered by day, in rain, snow and autumn, and cause by cause. --- docs/manual/whats-new/0.1.8.md | 4 + docs/wear.feature | 28 +++- sdk/raea/src/native-calls.ts | 2 + sdk/tsconfig.base.json | 3 + sdk/wear/package.json | 10 ++ sdk/wear/src/causes.ts | 125 ++++++++++++++++ sdk/wear/src/debug.ts | 73 ++++++++++ sdk/wear/src/index.ts | 199 +++++++++++++++++++++++++ sdk/wear/src/recipes.ts | 251 ++++++++++++++++++++++++++++++++ sdk/wear/test/wear.test.ts | 116 +++++++++++++++ sdk/wear/tsconfig.json | 1 + systems/weathering/src/lib.rs | 10 +- systems/weathering/src/maps.rs | 15 +- systems/weathering/src/rays.rs | 2 +- systems/weathering/src/scene.rs | 16 +- systems/weathering/src/tests.rs | 30 ++++ 16 files changed, 873 insertions(+), 12 deletions(-) create mode 100644 sdk/wear/package.json create mode 100644 sdk/wear/src/causes.ts create mode 100644 sdk/wear/src/debug.ts create mode 100644 sdk/wear/src/index.ts create mode 100644 sdk/wear/src/recipes.ts create mode 100644 sdk/wear/test/wear.test.ts create mode 100644 sdk/wear/tsconfig.json diff --git a/docs/manual/whats-new/0.1.8.md b/docs/manual/whats-new/0.1.8.md index 56a3bbed1..77d964a79 100644 --- a/docs/manual/whats-new/0.1.8.md +++ b/docs/manual/whats-new/0.1.8.md @@ -84,6 +84,10 @@ frame, on Windows too. item's Controls tab moves them on the live picture. - `@shared/` imports render. A composition that imports `@shared/components/NAME` from its workspace's `shared/` folder renders from the terminal as it does in the app. +- raea/wear weathers a model by what weathers it. `wearBake` (and `raea wear bake`) works out once where rain + reaches and runs, where the sun falls, what people touch and how high each point stands over the street. + `wearShader` draws soot, stains, damp, rust, peeling paint and worn edges by material (plaster, brick, stone, wood, + iron and nine more), with the season's rain, snow, dust or leaves over them. - `raea check` reads a video without FFmpeg. Where FFmpeg isn't installed, raea's own readers stand in, held to FFmpeg's answers. - `raea gen`: a fal step sends the resolution and aspect ratio its model takes, an image edit's mask reaches the diff --git a/docs/wear.feature b/docs/wear.feature index 3a8a5ae21..bf6cb20f8 100644 --- a/docs/wear.feature +++ b/docs/wear.feature @@ -46,7 +46,7 @@ Feature: raea/wear, weathering that has a cause | map | r | g | b | a | | causes0 | sky: rain's reach, n.d over the rain's cone | sun: share of the year's sun path in sight | cavity: occlusion within 0.5 m | edge: nearness to a convex arris | | causes1 | flow: run-off passing (WR2) | deposit: what it left (WR2) | deposit kind (WR2) | touch (WR3) | - | causes2 | ground: height over the walk below, 0-4 m | element: the chart's own number | open: share of the whole sky seen | spare | + | causes2 | ground: height over the walk below, 0-4 m | element: the chart's own number | open: share of the whole sky seen | class: 16 x (its place + 1) (WR5) | | causes3 | winter sun: share of the December path in sight (where snow lingers) | drift: driven snow's reach, n.d over its cone | litter: where blown leaves and snow settle (concave, at the walk, in the lee) | spare | And the year's sun is what fades and dries (it acts all year); the winter sun only decides where snow and ice outlast a thaw, so it has its own channel rather than a season in the request @@ -195,7 +195,7 @@ Feature: raea/wear, weathering that has a cause Rule: 2. The materials - @needs:WR4 + @needs:WR4 @done Scenario: WR5. raea/wear: a recipe per material Given `wearShader({ classes, weather, age })` from raea/wear Then it gives `{ wesl, uniforms, textures }` for a 's , a or a @@ -221,6 +221,30 @@ Feature: raea/wear, weathering that has a cause | raea/wear | sdk/wear/src/recipes.ts | 420 | one WESL function per class | | raea/wear | sdk/wear/src/debug.ts | 60 | a cause in false colour | | raea/wear | sdk/wear/test/wear.test.ts | 120 | the table, the shader's text, the uniforms | + And as built (11-19) + | crate | file | lines | job | + | raea/wear | sdk/wear/src/index.ts | 199 | wearBake, wearShader, WEAR_CLASSES, seasonOf, the surface hook | + | raea/wear | sdk/wear/src/causes.ts | 125 | the maps read (element and class by textureLoad), noise, the face's frame, the marks every class shares | + | raea/wear | sdk/wear/src/recipes.ts | 251 | a function per class (14), the shared stains, damp and splash, WR9's weather over them | + | raea/wear | sdk/wear/src/debug.ts | 73 | a cause in false colour from a `lit` hook (14 causes, class too) | + | raea/wear | sdk/wear/test/wear.test.ts | 116 | the table against the bake's, the shader's text, the uniforms, debug | + | raea-sys-weathering | src/rays.rs, maps.rs, scene.rs | +30 | causes2's alpha is the class (16 x (place + 1)); gutters copy class and element; TEXCOORD_0 where a primitive had none; version 6 | + | raea | sdk/raea/src/native-calls.ts | +2 | wear.bake typed (tools/native-types.sh --write) | + And its shape: `wearShader({ baked, classes?, age: { years, soot, upkeep }, weather: { day, wet, snow, storm, ice, + damp?, dust?, leaves?, melt? }, up?, debug? })`; `baked` is wearBake's answer (its maps and density), `classes` + limits the recipes compiled, and the season's damp, dust and leaves follow from `day` unless given. Authors put + it on } extension={}> (one glTF + material by the base's `material`) or ; a mesh without the bake's TEXCOORD_1 is left + as it was (`@if(VERTEX_UVS_B)`) + And the class comes from the bake (causes2's alpha, read by textureLoad), so one shader serves every material and + COLOR_0 alpha codes alike; Bevy's standard surface reads the first UV set wherever there's a second, so the bake + gives a primitive with none the atlas's as both + And proof: `bun test` in sdk/wear (12 tests); `cargo test -p raea-sys-weathering` (11, one new: each texel's class + and the gutters' a real one); a street front of all 14 classes made in code, baked at 16 texels a metre and + rendered on the Mac (11-19): the class view names every part right; after 80 sooty years the cornice is + streaked, the sills drip, the plinth and the brick's foot carry splash and a damp tide, the south door is + faded, the railing rusted; rain, snow with a storm and autumn render; an unbaked sphere under the same shader is + unchanged. The corner house with stills of every cause and condition is WR7's @needs:WR5,WR3 Scenario: WR6. Crumbling diff --git a/sdk/raea/src/native-calls.ts b/sdk/raea/src/native-calls.ts index 205f7745b..cb4b6c869 100644 --- a/sdk/raea/src/native-calls.ts +++ b/sdk/raea/src/native-calls.ts @@ -304,6 +304,8 @@ export interface NativeCalls { "venc.finish": { args: { id: number }; answer: { packets: { data: string; key: boolean; pts: number; dur: number }[]; description: string | null } }; /** a VideoToolbox H.264 encoder */ "venc.new": { args: { width: number; height: number; fps: number; kbps: number }; answer: { id: number } }; + /** a .glb's weathering causes baked once and kept (raea/wear): the model with a second UV set, and four maps over it (sky, sun, cavity, edge; run-off, deposit, touch; height over the ground, element, open sky; December's sun, driven snow, litter) */ + "wear.bake": { args: { src: string; env?: { up?: [number, number, number]; north?: [number, number, number]; rain?: { dir?: [number, number, number]; spread?: number }; sun?: { lat?: number }; snow?: { dir?: [number, number, number]; spread?: number }; walk?: string[] | { y: number }; density?: number | Record; max_side?: number; rays?: { sky?: number; cavity?: number } }; classes?: Record; default?: string }> }; answer: { key: string; file: string; maps: { causes0: string; causes1: string; causes2: string; causes3: string }; spots: string; size: [number, number]; density: number; charts: number; tris: number; ms: Record } }; /** SHA-1, SHA-256, SHA-384 or SHA-512 of base64 bytes, base64 */ "web.digest": { args: { algo: string; data: string }; answer: string }; /** n random bytes (the OS's), base64 */ diff --git a/sdk/tsconfig.base.json b/sdk/tsconfig.base.json index 7d3cd833c..184b2770b 100644 --- a/sdk/tsconfig.base.json +++ b/sdk/tsconfig.base.json @@ -71,6 +71,7 @@ "raea/tone": ["./audio/src/tone.tsx"], "raea/words": ["./video/src/words.tsx"], "raea/world": ["./app/src/world.ts"], + "raea/wear": ["./wear/src/index.ts"], "raea/views": ["./dev_tools/src/views.tsx"], "raea/window": ["./app/src/window.ts"], "raea/plugins/*": ["./plugins/src/*.gen"], @@ -111,6 +112,8 @@ "@raea/web/*": ["./web/src/*"], "@raea/room": ["./room/src/index.ts"], "@raea/room/*": ["./room/src/*"], + "@raea/wear": ["./wear/src/index.ts"], + "@raea/wear/*": ["./wear/src/*"], "@raea/rune": ["./rune/src/index.ts"], "@raea/plugins": ["./plugins/src/index.ts"], "@raea/plugins/*": ["./plugins/src/*"] diff --git a/sdk/wear/package.json b/sdk/wear/package.json new file mode 100644 index 000000000..0ffccf6aa --- /dev/null +++ b/sdk/wear/package.json @@ -0,0 +1,10 @@ +{ + "name": "@raea/wear", + "license": "FSL-1.1-ALv2", + "private": true, + "version": "0.1.0", + "description": "raea's weathering (docs/wear.feature WR5): a model's causes baked once, and a recipe per material class that draws them as wear over the season's weather. Authors import it as raea/wear.", + "type": "module", + "main": "src/index.ts", + "exports": { ".": "./src/index.ts" } +} diff --git a/sdk/wear/src/causes.ts b/sdk/wear/src/causes.ts new file mode 100644 index 000000000..0d0bef06b --- /dev/null +++ b/sdk/wear/src/causes.ts @@ -0,0 +1,125 @@ +// The WESL that reads the bake's four cause maps (docs/wear.feature WR1-WR3, WR9) through the model's second UV set, +// and the marks every class shares: what years of rain, soot and neglect left at a point, the same in every season. +// causes0 = sky (rain's reach), sun (the year's sun), cavity, edge (convex arris) +// causes1 = flow (run-off passing), deposit (what it left), deposit kind (60 x: 1 grime, 2 rust, 3 copper, 4 zinc), touch +// causes2 = ground (height over the walk, 0-4 m), element (the chart's number), open (the whole sky seen), class +// causes3 = winter sun (December's), drift (driven snow), litter (where blown leaves and snow settle) +// The maps load as colour (sRGB) textures, so a colour channel is re-encoded to read the byte the bake wrote; alpha is +// stored linear. Element and class are numbers, read with textureLoad so a filtered read never blends two into a third. + +/** The uniforms the module reads: `up` (the bake's up), `age` (years since the last repair, soot, upkeep, the bake's + * texels a metre), `wx` (wet, snow, storm, ice) and `season` (damp, dust, leaves, melt). */ +export const CAUSES = /* wgsl */ ` +fn wear_enc(l: f32) -> f32 { return select(1.055 * pow(max(l, 0.0), 1.0 / 2.4) - 0.055, 12.92 * l, l <= 0.0031308); } + +struct WearCauses { + sky: f32, sun: f32, cav: f32, edge: f32, + flow: f32, dep: f32, kind: i32, touch: f32, + ground: f32, elem: f32, open: f32, cls: i32, + dec: f32, drift: f32, litter: f32 +} + +fn wear_causes(uv: vec2) -> WearCauses { + let a = textureSample(tex_c0, samp_c0, uv); + let f = textureSample(tex_c1, samp_c0, uv); + let b = textureSample(tex_c2, samp_c0, uv); + let d = textureSample(tex_c3, samp_c0, uv); + let dims = vec2(textureDimensions(tex_c2)); + let at = vec2(clamp(floor(uv * dims), vec2(0.0), dims - vec2(1.0))); + let x = textureLoad(tex_c2, at, 0); + let kind = i32(round(wear_enc(f.b) * 255.0 / 60.0)); + let cls = i32(round(x.a * 255.0 / 16.0)) - 1; + return WearCauses( + wear_enc(a.r), wear_enc(a.g), wear_enc(a.b), a.a, + wear_enc(f.r), wear_enc(f.g), kind, f.a, + wear_enc(b.r) * 4.0, round(wear_enc(x.g) * 255.0), wear_enc(b.b), cls, + wear_enc(d.r), wear_enc(d.g), wear_enc(d.b) + ); +} + +// value noise over the face, in metres +fn wear_h21(p: vec2) -> f32 { + var q = fract(p * vec2(123.34, 456.21)); + q += dot(q, q + 45.32); + return fract(q.x * q.y); +} +fn wear_vn2(p: vec2) -> f32 { + let i = floor(p); + let f = fract(p); + let u = f * f * (3.0 - 2.0 * f); + return mix(mix(wear_h21(i), wear_h21(i + vec2(1.0, 0.0)), u.x), mix(wear_h21(i + vec2(0.0, 1.0)), wear_h21(i + vec2(1.0, 1.0)), u.x), u.y); +} +fn wear_fbm2(p: vec2) -> f32 { + var v = 0.0; + var a = 0.5; + var q = p; + for (var i = 0; i < 4; i++) { + v += a * wear_vn2(q); + q = q * 2.03 + vec2(1.7, 9.2); + a *= 0.5; + } + return v; +} + +// where the point is: how far its face looks up, its place on the face in metres (along a wall and up it; two +// horizontal axes on a level face), its height over the walk, and how upright the face is +struct WearGeo { up: f32, p: vec2, h: f32, wall: f32 } + +fn wear_geo(wp: vec3, nrm: vec3, c: WearCauses) -> WearGeo { + let U = normalize(u_up()); + let n = normalize(nrm); + let up = dot(n, U); + let side = n - U * up; + var p: vec2; + if (length(side) > 0.3) { + let t = normalize(cross(U, side)); + p = vec2(dot(wp, t), dot(wp, U)); + } else { + let r = select(vec3(1.0, 0.0, 0.0), vec3(0.0, 0.0, 1.0), abs(U.x) > 0.9); + let e1 = normalize(cross(U, r)); + p = vec2(dot(wp, e1), dot(wp, cross(U, e1))); + } + return WearGeo(up, p, c.ground, 1.0 - smoothstep(0.5, 0.8, abs(up))); +} + +// what years of weather left, by cause: the same in every season +struct WearMarks { + age: f32, upkeep: f32, wear: f32, + wash: f32, shelter: f32, tuck: f32, crust: f32, + tear: f32, stain: f32, damp: f32, salt: f32, splash: f32, + low: f32, chip: f32 +} + +fn wear_marks(c: WearCauses, g: WearGeo) -> WearMarks { + let years = max(u_age().x, 0.0); + // the marks come on fast in the first decades and then hold (soot crusts and paint fails within 20-30 years) + let age = 1.0 - exp(-years / 25.0); + // one draw per element (a front, a ledge), so neighbours differ: a house kept up beside one let go + let upkeep = clamp(u_age().z + (wear_h21(vec2(c.elem, 7.0)) - 0.5) * 0.7, 0.0, 1.0); + let wear = age * (1.0 - 0.7 * upkeep); + let wash = smoothstep(0.35, 0.85, c.sky); + let shelter = 1.0 - smoothstep(0.08, 0.5, c.sky); + let air = smoothstep(0.04, 0.3, c.open); + // tucked: less sky than a plain wall sees (about half), so under a cornice, in a reveal or a court + let tuck = 1.0 - smoothstep(0.08, 0.3, c.open); + // soot lies in streaks: run-off cleans narrow bands and leaves the rest + let streaks = wear_fbm2(vec2(g.p.x * 2.1 + c.elem * 0.37, g.p.y * 0.11)); + let mottle = 0.35 + 0.9 * smoothstep(0.3, 0.75, streaks) * (0.85 + 0.3 * wear_vn2(g.p * 6.0)); + let crust = clamp(u_age().y * air * (0.12 + 0.25 * shelter + 0.5 * tuck + 0.3 * c.cav) * mottle * age * (1.0 - 0.6 * upkeep), 0.0, 0.85); + let tear = smoothstep(0.6, 0.9, c.flow); + let stain = smoothstep(0.04, 0.45, c.dep) * age; + // rising damp to a tide line 0.6-1.2 m up, higher where the sun never dries it, its salts on top + let tide = mix(0.6, 1.2, clamp(0.55 * (1.0 - c.sun) + 0.25 * wear_vn2(vec2(g.p.x * 0.7, 3.0)) + 0.2 * (1.0 - upkeep), 0.0, 1.0)); + let damp = (1.0 - smoothstep(tide - 0.2, tide, g.h)) * age * (0.5 + 0.5 * (1.0 - upkeep)) * g.wall; + let salt = smoothstep(tide - 0.12, tide - 0.03, g.h) * (1.0 - smoothstep(tide - 0.01, tide + 0.06, g.h)) * age * g.wall; + let splash = (1.0 - smoothstep(0.06, 0.42, g.h)) * (0.6 + 0.4 * wear_vn2(g.p * 13.0)) * age * g.wall; + let low = 1.0 - smoothstep(1.4, 2.2, g.h); + // chips need a real arris, so a bake of 12 texels a metre or finer (at 4 an edge spreads 25 cm); people and carts + // reach below 2.2 m + let chip = step(12.0, u_age().w) * smoothstep(0.75, 0.9, c.edge) * low * step(0.68 - 0.2 * (1.0 - upkeep), wear_vn2(g.p * 16.0 + vec2(c.elem * 9.0, 0.0))) * step(0.05, age); + return WearMarks(age, upkeep, wear, wash, shelter, tuck, crust, tear, stain, damp, salt, splash, low, chip); +} + +// a surface's own colour, roughness and metalness as the recipes change them +struct WearSurf { col: vec3, rough: f32, metal: f32 } +`; diff --git a/sdk/wear/src/debug.ts b/sdk/wear/src/debug.ts new file mode 100644 index 000000000..462afba1e --- /dev/null +++ b/sdk/wear/src/debug.ts @@ -0,0 +1,73 @@ +// A cause in false colour (docs/wear.feature WR5): the shader's `lit` hook returns the cause's own colour in place of +// the lit one, so light and exposure don't change it. Render with and no fog to read the +// values exactly; a mesh without the bake's second UV set keeps its lit colour. + +/** Each cause the debug mode shows, as the colour it's drawn in (WESL over `c: WearCauses`). */ +export const DEBUG_CAUSES = { + /** rain's reach: black where none comes, white where it all does */ + sky: "vec3(c.sky)", + /** the year's sun: black in shade all year, yellow in the most */ + sun: "vec3(1.0, 0.85, 0.2) * c.sun", + /** occlusion within 0.5 m */ + cavity: "vec3(c.cav)", + /** nearness to a convex arris */ + edge: "vec3(c.edge)", + /** run-off passing, in blue */ + flow: "vec3(0.1, 0.4, 1.0) * c.flow", + /** what the run-off left, in the colour of what it left: grey grime, rust, green copper, white zinc */ + deposit: "wear_kind_colour(c.kind) * c.dep", + /** hands, feet and wheels, in amber over the sky term in grey */ + touch: "mix(vec3(0.1 + 0.25 * c.sky), vec3(1.0, 0.62, 0.08), c.touch)", + /** height over the walk: blue at the walk, through green and yellow, red at 4 m and over */ + ground: "wear_ramp(c.ground / 4.0)", + /** each element its own colour (a front, a ledge: what an upkeep draw covers) */ + element: "vec3(wear_h21(vec2(c.elem, 1.0)), wear_h21(vec2(c.elem, 2.0)), wear_h21(vec2(c.elem, 3.0)))", + /** the share of the whole sky seen */ + open: "vec3(c.open)", + /** December's sun, in pale blue */ + winter: "vec3(0.6, 0.8, 1.0) * c.dec", + /** driven snow's reach */ + drift: "vec3(c.drift)", + /** where blown leaves and snow settle, in orange */ + litter: "vec3(1.0, 0.5, 0.1) * c.litter", + /** the material class, by WEAR_CLASSES' colour; magenta where the bake found none */ + class: "wear_class_colour(c.cls)", +} as const; + +export type WearCause = keyof typeof DEBUG_CAUSES; + +/** The `lit` hook drawing `cause`; `palette` is each class's colour in WEAR_CLASSES' order. */ +export function debugHook(cause: WearCause, palette: [number, number, number][]): string { + const expr = DEBUG_CAUSES[cause]; + if (expr === undefined) throw new TypeError(`wearShader: debug is one of ${Object.keys(DEBUG_CAUSES).join(", ")}, got ${JSON.stringify(cause)}`); + const v = (c: [number, number, number]) => `vec3(${c.map((x) => x.toFixed(3)).join(", ")})`; + const cases = palette.map((c, i) => ` case ${i}: { return ${v(c)}; }`).join("\n"); + return /* wgsl */ ` +fn wear_kind_colour(k: i32) -> vec3 { + switch k { + case 2: { return vec3(0.75, 0.3, 0.08); } + case 3: { return vec3(0.2, 0.8, 0.55); } + case 4: { return vec3(0.95); } + default: { return vec3(0.45); } + } +} +fn wear_ramp(t: f32) -> vec3 { + let x = clamp(t, 0.0, 1.0); + return clamp(vec3(1.5 - abs(4.0 * x - 3.0), 1.5 - abs(4.0 * x - 2.0), 1.5 - abs(4.0 * x - 1.0)), vec3(0.0), vec3(1.0)); +} +fn wear_class_colour(k: i32) -> vec3 { + switch k { +${cases} + default: { return vec3(1.0, 0.0, 1.0); } + } +} +fn lit(in: VertexOutput, pbr: PbrInput, color: vec4) -> vec4 { + var out = color; + @if(VERTEX_UVS_B) { + let c = wear_causes(in.uv_b); + out = vec4(${expr}, 1.0); + } + return out; +} +`; +} diff --git a/sdk/wear/src/index.ts b/sdk/wear/src/index.ts new file mode 100644 index 000000000..405585cd4 --- /dev/null +++ b/sdk/wear/src/index.ts @@ -0,0 +1,199 @@ +// raea/wear (docs/wear.feature WR5): a model weathered by what weathers it. `wearBake` bakes a .glb's causes once +// (rain's reach, the sun, run-off and what it leaves, touch, height over the walk; raea-sys-weathering, kept in the +// composition's asset store), and `wearShader` gives the shader that draws them on each surface by its material class. +// +// const baked = wearBake({ src: staticFile("house.glb"), classes: { facade: "plaster", rail: "iron", "*": "brick" } }); +// const wear = wearShader({ baked, age: { years: 60, soot: 0.8 }, weather: { day: 300, wet: 0.4 } }); +// +// } extension={} /> +// +// +// The same spec goes on one glTF material ( as the base), a +// , or a over the whole scene. A surface without the +// bake's second UV set is drawn as it was. +import { hasNative, native, type NativeCalls } from "@raea/app/native"; +import { CAUSES } from "./causes"; +import { debugHook, DEBUG_CAUSES, type WearCause } from "./debug"; +import { HELPERS, RECIPES, SHARED, WEATHER } from "./recipes"; + +export { DEBUG_CAUSES, type WearCause } from "./debug"; + +/** The material classes, in the bake's order (causes2's alpha is 16 x (the place + 1)), each with its colour in the + * debug view and what the causes do to it. */ +export const WEAR_CLASSES = [ + { name: "plaster", colour: [0.95, 0.9, 0.75], marks: "washed clean and chalky; soot grey where sheltered, darkest under cornices; dark tears from sill ends; splash to 0.4 m and a damp tide at 0.6-1.2 m; blown at the foot; chipped arrises low down; a grease band" }, + { name: "brick", colour: [0.75, 0.3, 0.2], marks: "mortar washed out; soot black where sheltered; efflorescence under ledges; a tide line, green on north faces; spalled faces" }, + { name: "sandstone", colour: [0.9, 0.75, 0.45], marks: "washed light; a black crust where sheltered; black and white streaks under ledges; rounded arrises" }, + { name: "granite", colour: [0.6, 0.6, 0.62], marks: "little: soot where sheltered" }, + { name: "wood", colour: [0.55, 0.35, 0.15], marks: "sun-faded and alligatored on the faces the sun reaches; peeling at bottom rails; rot at the foot; worn to the wood where held and kicked" }, + { name: "iron", colour: [0.25, 0.3, 0.4], marks: "paint flaking at joints and arrises; rust at the foot and on the stone below; bright where held" }, + { name: "zinc", colour: [0.7, 0.8, 0.85], marks: "a pale patina where rain-washed; white streaks below" }, + { name: "copper", colour: [0.3, 0.7, 0.55], marks: "verdigris where rain-washed, brown-black where sheltered; green streaks below" }, + { name: "slate", colour: [0.35, 0.35, 0.45], marks: "lichen where the sun misses; soot in the laps" }, + { name: "felt", colour: [0.15, 0.15, 0.15], marks: "greyed by the sun; washed where the rain runs" }, + { name: "glass", colour: [0.5, 0.85, 1.0], marks: "a grime film where sheltered, thickest at the bottom and corners" }, + { name: "enamel", colour: [0.2, 0.3, 0.9], marks: "rust bled from its screws; chipped to black iron at holes and edges" }, + { name: "brass", colour: [0.9, 0.7, 0.2], marks: "dull brown, more where rain-washed; bright where touched" }, + { name: "ground", colour: [0.4, 0.55, 0.3], marks: "dirt in corners and at the wall's foot; moss where the sun misses; polished where feet pass" }, +] as const satisfies readonly { name: string; colour: [number, number, number]; marks: string }[]; + +export type WearClass = (typeof WEAR_CLASSES)[number]["name"]; +export type WearBakeRequest = NativeCalls["wear.bake"]["args"]; +export type WearBaked = NativeCalls["wear.bake"]["answer"]; + +const bakes = new Map(); + +/** The bake of `req.src` under its env and classes: the model with TEXCOORD_1 and the cause maps, made once and kept + * in the composition's asset store (the same request comes back from it). Needs the raea engine. */ +export function wearBake(req: WearBakeRequest): WearBaked { + const key = JSON.stringify(req); + let b = bakes.get(key); + if (!b) { + if (!hasNative()) throw new Error("wearBake: baking needs the raea engine (raea render)"); + b = native("wear.bake", req); + if (!b || typeof b.file !== "string") throw new Error(`wearBake: the bake gave nothing for ${req.src}`); + bakes.set(key, b); + } + return b; +} + +/** What years of weather left: the same in every season. */ +export interface WearAge { + /** years since the last repair (0 is new; the marks come on over 20-30 years and then hold) */ + years?: number; + /** how sooty the air was, 0..1 (a coal-burning city near 1) */ + soot?: number; + /** how well kept, 0..1, around which each element (a front, a ledge) draws its own */ + upkeep?: number; +} + +/** The day and its weather (docs/wear.feature WR9): drawn over the causes, never baked. The season's own share of + * damp, dust and leaves follows from `day` unless given. */ +export interface WearWeather { + /** the day of the year, 1-366 (172 by default: midsummer) */ + day?: number; + /** rain on the surfaces now, 0..1 */ + wet?: number; + /** snow lying, 0..1 */ + snow?: number; + /** driven snow plastered on what the storm reaches, 0..1 */ + storm?: number; + /** frost, 0..1 */ + ice?: number; + /** spring's damp and algae, 0..1 */ + damp?: number; + /** summer's dust, 0..1 */ + dust?: number; + /** autumn's fallen leaves, 0..1 */ + leaves?: number; + /** December's sun thinning snow between falls, 0..1 */ + melt?: number; +} + +export interface WearShaderOptions { + /** wearBake's answer: its maps are the shader's textures, its density decides whether edges can chip */ + baked: Pick; + /** the classes to draw (the rest are drawn as they are); all of them by default */ + classes?: readonly WearClass[]; + age?: WearAge; + weather?: WearWeather; + /** the bake's up (its env.up), [0, 1, 0] by default */ + up?: [number, number, number]; + /** one cause in false colour in place of the weathered surface */ + debug?: WearCause; +} + +// a bump over the year's days a..b, 1 at its middle (b may pass 366 into the next year) +const bump = (day: number, a: number, b: number) => { + const d = day < a ? day + 366 : day; + if (d < a || d > b) return 0; + return Math.sin(((d - a) / (b - a)) * Math.PI); +}; + +/** The season's damp, dust, leaves and melt for a day, with what the weather gives over them. */ +export function seasonOf(w: WearWeather = {}): { damp: number; dust: number; leaves: number; melt: number } { + const day = w.day ?? 172; + if (!(day >= 1 && day <= 366)) throw new RangeError(`wearShader: weather.day is 1-366, got ${day}`); + const wet = w.wet ?? 0; + return { + damp: w.damp ?? 0.8 * bump(day, 50, 150), + dust: w.dust ?? 0.8 * bump(day, 140, 260) * (1 - wet), + leaves: w.leaves ?? bump(day, 270, 345), + melt: w.melt ?? ((w.snow ?? 0) > 0 ? 0.5 : 0), + }; +} + +const unit = (owner: string, v: number | undefined, d: number) => { + const x = v ?? d; + if (!(Number.isFinite(x) && x >= 0 && x <= 1)) throw new RangeError(`wearShader: ${owner} is 0..1, got ${v}`); + return x; +}; + +/** The shader that draws the bake's causes as wear, by material class: `{ wesl, uniforms, textures }` for a + * (raea/pbr), a or a . */ +export function wearShader(o: WearShaderOptions): { wesl: string; uniforms: Record; textures: Record } { + const maps = o?.baked?.maps; + if (!maps || ["causes0", "causes1", "causes2", "causes3"].some((k) => typeof (maps as Record)[k] !== "string")) throw new TypeError("wearShader: baked is wearBake's answer, with its four maps"); + const names = WEAR_CLASSES.map((c) => c.name) as readonly string[]; + const want = o.classes ?? names; + for (const c of want) if (!names.includes(c)) throw new TypeError(`wearShader: no class ${JSON.stringify(c)} (${names.join(", ")})`); + const years = o.age?.years ?? 30; + if (!(Number.isFinite(years) && years >= 0)) throw new RangeError(`wearShader: age.years is 0 or more, got ${years}`); + const w = o.weather ?? {}; + const s = seasonOf(w); + const density = o.baked.density; + const uniforms = { + up: o.up ?? [0, 1, 0], + age: [years, unit("age.soot", o.age?.soot, 0.5), unit("age.upkeep", o.age?.upkeep, 0.5), typeof density === "number" ? density : 4], + wx: [unit("weather.wet", w.wet, 0), unit("weather.snow", w.snow, 0), unit("weather.storm", w.storm, 0), unit("weather.ice", w.ice, 0)], + season: [unit("weather.damp", s.damp, 0), unit("weather.dust", s.dust, 0), unit("weather.leaves", s.leaves, 0), unit("weather.melt", s.melt, 0)], + }; + const sampler = { filter: "linear", mipmap: "none", wrap: "clamp" } as const; + const textures = { c0: { src: maps.causes0, sampler }, c1: { src: maps.causes1, sampler }, c2: { src: maps.causes2, sampler }, c3: { src: maps.causes3, sampler } }; + if (o.debug !== undefined) { + if (!(o.debug in DEBUG_CAUSES)) throw new TypeError(`wearShader: debug is one of ${Object.keys(DEBUG_CAUSES).join(", ")}, got ${JSON.stringify(o.debug)}`); + return { wesl: CAUSES + debugHook(o.debug, WEAR_CLASSES.map((c) => [...c.colour])), uniforms, textures }; + } + return { wesl: CAUSES + HELPERS + recipes(want) + SHARED + WEATHER + SURFACE, uniforms, textures }; +} + +/** The recipes of `want` and the switch that picks one by the texel's class. */ +function recipes(want: readonly string[]): string { + let out = ""; + const cases: string[] = []; + WEAR_CLASSES.forEach((c, i) => { + if (!want.includes(c.name)) return; + out += `\nfn wear_${c.name}(s: ptr, c: WearCauses, g: WearGeo, m: WearMarks) {${RECIPES[c.name]}\n}\n`; + cases.push(` case ${i}: { wear_${c.name}(s, c, g, m); }`); + }); + return `${out} +fn wear_class(s: ptr, c: WearCauses, g: WearGeo, m: WearMarks) -> bool { + switch c.cls { +${cases.join("\n")} + default: { return false; } + } + return true; +} +`; +} + +const SURFACE = /* wgsl */ ` +fn wear_surface(uv: vec2, pbr: ptr) { + let c = wear_causes(uv); + let g = wear_geo((*pbr).world_position.xyz, (*pbr).world_normal, c); + let m = wear_marks(c, g); + var s = WearSurf((*pbr).material.base_color.rgb, (*pbr).material.perceptual_roughness, (*pbr).material.metallic); + if (!wear_class(&s, c, g, m)) { return; } + wear_shared(&s, c, g, m); + wear_weather(&s, c, g, m); + (*pbr).material.base_color = vec4(s.col, (*pbr).material.base_color.a); + (*pbr).material.perceptual_roughness = clamp(s.rough, 0.04, 1.0); + (*pbr).material.metallic = clamp(s.metal, 0.0, 1.0); +} + +fn surface(in: VertexOutput, pbr: ptr) { + @if(VERTEX_UVS_B) { + wear_surface(in.uv_b, pbr); + } +} +`; diff --git a/sdk/wear/src/recipes.ts b/sdk/wear/src/recipes.ts new file mode 100644 index 000000000..5b041869b --- /dev/null +++ b/sdk/wear/src/recipes.ts @@ -0,0 +1,251 @@ +// One WESL function per material class, answering each cause as docs/wear.feature WR5's table has it, then what every +// class shares (stains, rising damp, splash) and the season and the day's weather over them (WR9), which nothing bakes. +// Each recipe changes the surface's own colour, roughness and metalness, so a model keeps its textures and the marks +// lie over them. First tried on the game project's city tile (its components/wear/look.ts), and moved here. +// Notes on causes: Bonazza (NABU MedShip 2019) for black crusts on sheltered stone and rain-washed stone staying +// light; the rest are the marks a street of 19th-century fronts shows, by the cause each map measures. + +/** A recipe's WESL: `fn wear_(s: ptr, c: WearCauses, g: WearGeo, m: WearMarks)`. */ +export const RECIPES: Record = { + plaster: /* wgsl */ ` + // rain-washed: soot rinsed off, the paint chalked paler and flatter + (*s).col = mix((*s).col, (*s).col * 1.06 + vec3(0.02), m.wash * 0.35 * m.age); + (*s).col = mix((*s).col, vec3(dot((*s).col, vec3(0.3, 0.45, 0.25))) * 1.05, (0.15 * c.sun + 0.1 * m.wash) * m.wear); + (*s).rough = mix((*s).rough, 0.95, m.wash * 0.4 * m.age); + // sheltered: soot grey, darkest tucked under cornices + (*s).col = mix((*s).col, (*s).col * vec3(0.3, 0.29, 0.27), m.crust); + // the foot: render blown where the damp and the frost work, the brick behind it showing (worst where let go) + let blowN = wear_fbm2(g.p * vec2(0.9, 1.4) + vec2(c.elem * 0.31, 5.0)); + let blowAt = 0.74 - 0.2 * (1.0 - m.upkeep) - 0.12 * (1.0 - smoothstep(0.15, 0.9, g.h)); + let foot = (1.0 - smoothstep(0.7, 1.3, g.h)) * g.wall * step(0.2, m.wear); + let blown = smoothstep(blowAt, blowAt + 0.015, blowN) * foot; + let lip = smoothstep(blowAt - 0.04, blowAt, blowN) * (1.0 - blown) * foot; + (*s).col = mix((*s).col, wear_bond(g.p) * mix(1.0, 0.55, m.crust), blown); + (*s).col = (*s).col * (1.0 - 0.45 * lip); + // chipped arrises low down: the lime coat knocked off to the brick + (*s).col = mix((*s).col, mix(vec3(0.78, 0.76, 0.7), wear_bond(g.p), 0.5), m.chip * (1.0 - blown)); + // a grease band where shoulders and hubs rub + (*s).col = mix((*s).col, (*s).col * vec3(0.5, 0.48, 0.45), c.touch * 0.6 * g.wall); + (*s).rough = mix((*s).rough, 0.55, c.touch * 0.5);`, + + brick: /* wgsl */ ` + // rain-washed: the mortar washed out of the joints, so they read as dark lines + let j = wear_bond_joint(g.p) * g.wall; + (*s).col = mix((*s).col, (*s).col * 0.55, j * m.wash * m.age * 0.6); + // sheltered: soot black + (*s).col = mix((*s).col, vec3(0.04, 0.035, 0.03), clamp(m.crust * 1.2, 0.0, 0.9)); + // under a ledge: efflorescence, the salts the soaked water leaves as it dries + let eff = smoothstep(0.35, 0.75, c.flow) * (1.0 - m.wash) * smoothstep(0.45, 0.7, wear_fbm2(g.p * vec2(3.0, 1.2))) * m.age; + (*s).col = mix((*s).col, vec3(0.82, 0.8, 0.76), eff * 0.6); + // the ground band: green on faces the sun misses + let algae = (1.0 - c.sun) * (1.0 - smoothstep(0.3, 1.5, g.h)) * smoothstep(0.4, 0.7, wear_fbm2(g.p * 1.7)) * m.age * g.wall; + (*s).col = mix((*s).col, vec3(0.16, 0.22, 0.1), algae * 0.6); + // spalled faces: frost in damp brick takes the fired face off, the softer core showing paler and rough + let spall = step(0.74 - 0.3 * m.damp - 0.15 * (1.0 - m.upkeep), wear_vn2(g.p * vec2(4.0, 12.9) + vec2(c.elem, 0.0))) * max(m.low, c.edge) * step(0.1, m.wear); + (*s).col = mix((*s).col, (*s).col * vec3(1.2, 1.02, 0.9) + vec3(0.03), spall * 0.8); + (*s).rough = mix((*s).rough, 0.98, max(spall, m.wash * 0.3));`, + + sandstone: /* wgsl */ ` + // rain-washed: the stone washed light + (*s).col = mix((*s).col, (*s).col * 1.18 + vec3(0.03), m.wash * 0.6 * m.age); + // sheltered: the black gypsum crust + (*s).col = mix((*s).col, vec3(0.05, 0.045, 0.04), clamp(m.crust * 1.3, 0.0, 0.95)); + // under a ledge: the run-off's paths washed white between the black (the deposit's grime is the shared stain) + (*s).col = mix((*s).col, (*s).col * 1.25 + vec3(0.04), m.tear * (1.0 - m.wash) * 0.5 * m.age); + // rounded arrises: worn back and pale + (*s).col = mix((*s).col, (*s).col * 1.12, c.edge * 0.5 * m.age); + (*s).rough = mix((*s).rough, 0.97, c.edge * m.age);`, + + granite: /* wgsl */ ` + // granite barely weathers: some soot where it's sheltered, a cleaner face where the rain reaches + (*s).col = mix((*s).col, vec3(0.06, 0.055, 0.05), m.crust * 0.6); + (*s).col = mix((*s).col, (*s).col * 1.08, m.wash * 0.3 * m.age);`, + + wood: /* wgsl */ ` + // painted wood: sun-faded on the faces the sun reaches (south and west), chalked, and alligatored there + (*s).col = mix((*s).col, vec3(dot((*s).col, vec3(0.33))) * 1.25 + vec3(0.04), c.sun * 0.45 * m.wear); + let cells = abs(wear_vn2(g.p * vec2(14.0, 9.0)) - 0.5); + (*s).col = (*s).col * (1.0 - 0.45 * (1.0 - smoothstep(0.015, 0.05, cells)) * smoothstep(0.3, 0.8, c.sun) * m.wear); + // under a ledge and at the bottom rails: peeling where the run-off soaks the timber, the grey wood showing + let wetWood = clamp(c.flow + (1.0 - smoothstep(0.04, 0.5, g.h)) * g.wall, 0.0, 1.0); + let peel = smoothstep(0.5, 0.56, wear_fbm2(g.p * vec2(9.0, 4.0)) + 0.2 * wetWood * m.wear) * wetWood * step(0.1, m.wear); + (*s).col = mix((*s).col, vec3(0.3, 0.27, 0.22) * (0.8 + 0.3 * wear_vn2(vec2(g.p.x * 30.0, g.p.y * 2.0))), max(peel, m.chip)); + // rot at the foot + let rot = (1.0 - smoothstep(0.02, 0.25, g.h)) * m.wear * g.wall; + (*s).col = mix((*s).col, vec3(0.12, 0.08, 0.05), rot * 0.8); + (*s).rough = mix((*s).rough, 0.95, max(rot, peel)); + // worn to the wood where held and kicked, in patches where boots and hands land + let worn = c.touch * smoothstep(0.42, 0.58, wear_fbm2(g.p * vec2(9.0, 4.0)) + 0.12 * (c.touch - 0.5)); + (*s).col = mix((*s).col, vec3(0.15, 0.1, 0.065) * (0.8 + 0.3 * wear_vn2(vec2(g.p.x * 40.0, g.p.y * 4.0))), worn * 0.9); + (*s).col = mix((*s).col, (*s).col * 0.7, c.touch * 0.35); + (*s).rough = mix((*s).rough, 0.42, c.touch * 0.7);`, + + iron: /* wgsl */ ` + // painted iron: the paint flaking at joints and arrises and where water stands, rust at the foot (its run-off + // stains the stone below: the deposit's kind, read on the classes under it) + let edgy = c.edge * step(12.0, u_age().w); + let footRust = 1.0 - smoothstep(0.05, 0.4, g.h); + let rust = 0.8 * clamp((0.35 * m.wash + 0.6 * edgy + 0.5 * c.cav + 0.6 * footRust) * smoothstep(0.4, 0.8, wear_fbm2(g.p * 4.0) + 0.25 * (1.0 - m.upkeep)), 0.0, 1.0) * m.wear; + (*s).col = mix((*s).col, vec3(0.11, 0.045, 0.02) * (0.7 + 0.5 * wear_vn2(g.p * 21.0)), rust); + (*s).rough = mix((*s).rough, 0.95, rust); + (*s).metal = mix((*s).metal, 0.0, rust); + // bright where held + (*s).col = mix((*s).col, vec3(0.2, 0.19, 0.18), c.touch); + (*s).metal = mix((*s).metal, 0.9, c.touch * 0.8); + (*s).rough = mix((*s).rough, 0.32, c.touch);`, + + zinc: /* wgsl */ ` + // rain-washed: the pale blue-grey carbonate patina (its white run-off is the deposit's, on what's below) + let pat = m.wash * m.age * smoothstep(0.3, 0.7, wear_vn2(g.p * 3.0) + 0.3); + (*s).col = mix((*s).col, vec3(0.62, 0.64, 0.63), pat * 0.7); + (*s).metal = mix((*s).metal, 0.2, pat); + (*s).rough = mix((*s).rough, 0.8, pat);`, + + copper: /* wgsl */ ` + // rain-washed: verdigris, slower than the rest (decades); sheltered copper goes brown-black instead + let slow = 1.0 - exp(-max(u_age().x, 0.0) / 40.0); + let green = m.wash * smoothstep(0.1, 0.8, slow) * (0.7 + 0.3 * wear_vn2(g.p * 5.0)); + (*s).col = mix((*s).col, vec3(0.18, 0.09, 0.05), m.age * (1.0 - green) * 0.8); + (*s).col = mix((*s).col, vec3(0.25, 0.52, 0.42), green); + (*s).metal = mix((*s).metal, 0.0, max(green, m.age * 0.5)); + (*s).rough = mix((*s).rough, 0.85, green);`, + + slate: /* wgsl */ ` + // lichen on the faces the sun misses, soot in the laps + let lichen = (1.0 - c.sun) * smoothstep(0.55, 0.75, wear_fbm2(g.p * 2.5 + vec2(c.elem * 0.13, 1.0))) * m.age; + (*s).col = mix((*s).col, vec3(0.2, 0.22, 0.13), lichen * 0.6); + (*s).col = mix((*s).col, vec3(0.03), m.crust * 0.5);`, + + felt: /* wgsl */ ` + // roofing felt: greyed by the sun, its grit washed off where the rain runs + (*s).col = mix((*s).col, vec3(dot((*s).col, vec3(0.33))) * 1.2 + vec3(0.03), c.sun * 0.4 * m.wear); + (*s).col = mix((*s).col, (*s).col * 0.8, m.tear * m.age * 0.5);`, + + glass: /* wgsl */ ` + // sheltered: a grime film, thickest in the corners and at the bottom where the frame holds the dirt + let film = clamp(m.shelter * 0.5 + c.cav * 0.8 + (1.0 - m.wash) * 0.2, 0.0, 1.0) * m.age * (1.0 - 0.6 * m.upkeep); + (*s).col = mix((*s).col, (*s).col * vec3(0.62, 0.6, 0.55), film * 0.7); + (*s).rough = mix((*s).rough, 0.55, film * 0.6);`, + + enamel: /* wgsl */ ` + // rust bled from its screws, in short tails below them + let col = floor(g.p.x * 6.0); + let screw = step(0.8, wear_h21(vec2(col, c.elem))); + let tail = screw * (1.0 - smoothstep(0.0, 0.04, abs(fract(g.p.x * 6.0) - 0.5) / 6.0)) * smoothstep(0.3, 0.9, wear_vn2(vec2(col, g.p.y * 3.0))); + (*s).col = mix((*s).col, vec3(0.3, 0.12, 0.04), tail * m.wear * 0.8); + // chipped to black iron at its holes and edges + let chipped = smoothstep(0.65, 0.9, c.edge) * step(0.62 - 0.3 * m.wear, wear_vn2(g.p * 30.0)) * step(0.05, m.wear); + (*s).col = mix((*s).col, vec3(0.03, 0.03, 0.035), chipped); + (*s).rough = mix((*s).rough, 0.7, chipped);`, + + brass: /* wgsl */ ` + // tarnished dull brown, more where the rain reaches; bright where hands touch it + let tarnish = (0.5 + 0.5 * m.wash) * m.age * (1.0 - 0.8 * m.upkeep); + (*s).col = mix((*s).col, vec3(0.3, 0.22, 0.1), tarnish * 0.8); + (*s).rough = mix((*s).rough, 0.7, tarnish); + (*s).col = mix((*s).col, vec3(0.85, 0.68, 0.35), c.touch); + (*s).metal = mix((*s).metal, 1.0, c.touch); + (*s).rough = mix((*s).rough, 0.25, c.touch);`, + + ground: /* wgsl */ ` + // the street: dirt where the sweepings and the wind leave it (the wall's foot, the gutter, every corner), moss in + // the joints the sun never dries, paving polished where feet pass + let corner = smoothstep(0.08, 0.4, c.cav) * m.age; + (*s).col = mix((*s).col, vec3(0.085, 0.075, 0.06) * (0.7 + 0.6 * wear_vn2(g.p * 5.0)), corner * 0.75); + let moss = (1.0 - c.sun) * smoothstep(0.5, 0.75, wear_fbm2(g.p * 1.5)) * m.wear; + (*s).col = mix((*s).col, vec3(0.11, 0.14, 0.06), moss * 0.55); + (*s).col = mix((*s).col, (*s).col * 1.12 + vec3(0.015), c.touch * 0.5); + (*s).rough = mix((*s).rough, 0.5, c.touch * 0.6);`, +}; + +/** Shared helpers some recipes call: a brick bond in metres on a face, and its joints (named apart from the recipes' + * own wear_). */ +export const HELPERS = /* wgsl */ ` +fn wear_bond_joint(p: vec2) -> f32 { + let row = floor(p.y / 0.0775); + let bx = p.x / 0.25 + select(0.0, 0.5, i32(row) % 2 == 1); + let f = vec2(fract(bx), fract(p.y / 0.0775)); + return max(1.0 - smoothstep(0.0, 0.05, min(f.x, 1.0 - f.x)), 1.0 - smoothstep(0.0, 0.14, min(f.y, 1.0 - f.y))); +} +fn wear_bond(p: vec2) -> vec3 { + let row = floor(p.y / 0.0775); + let bx = p.x / 0.25 + select(0.0, 0.5, i32(row) % 2 == 1); + let tone = 0.8 + 0.35 * wear_h21(vec2(floor(bx), row)); + return mix(vec3(0.42, 0.2, 0.13) * tone, vec3(0.3, 0.29, 0.27), wear_bond_joint(p)); +} +// autumn's fallen leaves: lime and plane in ochre and brown, a few red +fn wear_leaf(p: vec2) -> vec3 { + let k = wear_vn2(p * 9.0); + let base = mix(vec3(0.42, 0.26, 0.07), vec3(0.2, 0.11, 0.045), smoothstep(0.3, 0.7, k)); + return mix(base, vec3(0.36, 0.1, 0.035), smoothstep(0.78, 0.92, wear_vn2(p * 23.0))); +} +`; + +/** What every class shares after its own recipe: the run-off's stains on what it reaches (not the metals that made + * them), rising damp and its salts in what draws water up, and the splash-back over the walk. */ +export const SHARED = /* wgsl */ ` +fn wear_shared(s: ptr, c: WearCauses, g: WearGeo, m: WearMarks) { + let metal = c.cls == 5 || c.cls == 6 || c.cls == 7 || c.cls == 12; + var stainCol = vec3(0.045, 0.04, 0.035); + if (c.kind == 2) { stainCol = vec3(0.24, 0.1, 0.035); } else if (c.kind == 3) { stainCol = vec3(0.1, 0.32, 0.24); } else if (c.kind == 4) { stainCol = vec3(0.66, 0.66, 0.64); } + if (!metal && c.kind > 0) { (*s).col = mix((*s).col, stainCol, m.stain * select(0.7, 0.85, c.kind >= 2)); } + // plaster, brick, sandstone and granite draw the ground's water up + let porous = select(0.0, 1.0, c.cls >= 0 && c.cls <= 3); + (*s).col = mix((*s).col, (*s).col * vec3(0.6, 0.63, 0.58), m.damp * 0.8 * porous); + (*s).col = mix((*s).col, vec3(0.8, 0.79, 0.74), m.salt * 0.3 * porous * smoothstep(0.4, 0.75, wear_vn2(vec2(g.p.x * 1.3, g.p.y * 3.0)))); + if (c.cls != 13 && c.cls != 10) { (*s).col = mix((*s).col, (*s).col * 0.62, m.splash * 0.7); } + (*s).rough = mix((*s).rough, 0.97, m.crust * 0.5); +} +`; + +/** WR9: the season and the day's weather over the causes, never baked: rain wetting what it reaches, streaming down + * the run-off's paths and puddling in the walk's hollows; snow lying on what looks up and is open, banked where + * litter settles, thinned by December's sun, trodden off by touch, plastered by a storm by drift; frost where the + * winter sun misses; spring's damp and algae; summer's dust; autumn's leaves. */ +export const WEATHER = /* wgsl */ ` +fn wear_weather(s: ptr, c: WearCauses, g: WearGeo, m: WearMarks) { + let wet = u_wx().x; + let snow = u_wx().y; + let storm = u_wx().z; + let ice = u_wx().w; + let damp = u_season().x; + let dust = u_season().y; + let leaves = u_season().z; + let melt = u_season().w; + let up = smoothstep(0.55, 0.85, g.up); + // spring: damp lingers on north faces and plinths; green algae where the run-off and the shade meet + (*s).col = (*s).col * (1.0 - 0.25 * damp * (1.0 - c.sun) * (0.5 + 0.5 * m.low) * g.wall); + (*s).col = mix((*s).col, vec3(0.14, 0.2, 0.08), damp * smoothstep(0.4, 0.8, c.flow) * (1.0 - c.sun) * g.wall * 0.5); + // summer's dust on sills, ledges and treads + (*s).col = mix((*s).col, vec3(0.47, 0.43, 0.37), dust * (1.0 - wet) * up * (0.3 + 0.5 * c.sky) * 0.45); + // rain: only what it reaches is wet, the run-off's paths stream and shine, the walk's hollows hold puddles + let rained = wet * smoothstep(0.08, 0.45, c.sky); + let rill = wet * smoothstep(0.5, 0.85, c.flow) * g.wall; + (*s).col = (*s).col * (1.0 - 0.35 * rained) * (1.0 - 0.3 * rill); + (*s).rough = mix((*s).rough, (*s).rough * 0.55, rained); + (*s).rough = mix((*s).rough, 0.1, rill); + let level = smoothstep(0.9, 0.98, g.up); + let pud = wet * level * smoothstep(0.1, 0.4, c.sky) * smoothstep(0.62, 0.74, wear_fbm2(g.p * 0.7 + vec2(3.0, 9.0)) + 0.3 * c.litter + 0.25 * c.cav); + (*s).col = mix((*s).col, (*s).col * 0.5, pud); + (*s).rough = mix((*s).rough, 0.03, pud); + // autumn's leaves where blown things settle, dark once rained on + let leaf = leaves * up * smoothstep(0.12, 0.45, c.litter + 0.35 * wear_fbm2(g.p * 4.0) - 0.1); + (*s).col = mix((*s).col, wear_leaf(g.p) * mix(1.0, 0.55, wet), leaf * 0.9); + // frost on what the winter sun misses + let frost = ice * (1.0 - c.dec) * smoothstep(0.02, 0.3, c.open) * (0.6 + 0.4 * wear_vn2(g.p * 40.0)); + (*s).col = mix((*s).col, vec3(0.82, 0.86, 0.9), frost * 0.5); + (*s).rough = mix((*s).rough, 0.9, frost * 0.5); + // snow lying on what looks up and is open above, banked where litter settles, plastered on what a storm drives at + let cover = smoothstep(0.3, 0.6, wear_fbm2(g.p * 1.9) * 0.7 + 0.45 * snow); + let lies = snow * up * cover * smoothstep(0.02, 0.25, c.open) * (1.0 - 0.8 * c.dec * melt) * (1.0 - c.touch); + let bank = snow * up * smoothstep(0.05, 0.35, c.litter); + let flake = smoothstep(0.35, 0.65, wear_fbm2(vec2(g.p.x, g.p.y) * 3.1) + 0.45 * c.drift - 0.2); + let plastered = storm * (1.0 - up) * smoothstep(0.2, 0.6, c.drift) * flake; + let sn = clamp(max(max(lies, bank), plastered), 0.0, 1.0); + // old snow in the lee greys with the soot + let snowCol = mix(vec3(0.92, 0.93, 0.95), vec3(0.62, 0.61, 0.6), u_age().y * c.litter * 0.5); + (*s).col = mix((*s).col, snowCol, sn); + (*s).rough = mix((*s).rough, 0.85, sn); + (*s).metal = mix((*s).metal, 0.0, sn); +} +`; diff --git a/sdk/wear/test/wear.test.ts b/sdk/wear/test/wear.test.ts new file mode 100644 index 000000000..1d78dc6c9 --- /dev/null +++ b/sdk/wear/test/wear.test.ts @@ -0,0 +1,116 @@ +import { describe, expect, test } from "bun:test"; +import { readFileSync } from "node:fs"; +import { surfaceShader } from "@raea/render/3d/materials"; +import { DEBUG_CAUSES, seasonOf, WEAR_CLASSES, wearShader, type WearCause } from "../src/index"; +import { RECIPES } from "../src/recipes"; + +const baked = { maps: { causes0: "/s/causes0.png", causes1: "/s/causes1.png", causes2: "/s/causes2.png", causes3: "/s/causes3.png" }, density: 16 }; +const ROOT = new URL("../../..", import.meta.url).pathname; + +describe("the class table", () => { + test("is the bake's, in its order, so causes2's code names the same class", () => { + const rs = readFileSync(`${ROOT}systems/weathering/src/scene.rs`, "utf8"); + const table = rs.slice(rs.indexOf("const NAMES"), rs.indexOf("];", rs.indexOf("const NAMES"))); + const names = [...table.matchAll(/\("([a-z]+)", Class::/g)].map((m) => m[1]); + expect(names.length).toBe(14); + expect(WEAR_CLASSES.map((c) => c.name)).toEqual(names as never); + // the code the shader decodes: 16 x (place + 1) + expect(rs).toContain("16 * (NAMES.iter().position(|(_, c)| *c == self).unwrap_or(0) as u8 + 1)"); + }); + test("every class has a recipe and every recipe a class", () => { + expect(Object.keys(RECIPES).sort()).toEqual(WEAR_CLASSES.map((c) => c.name).sort()); + }); +}); + +describe("the shader", () => { + const sh = wearShader({ baked, age: { years: 60, soot: 0.9, upkeep: 0.3 }, weather: { day: 300, wet: 0.4 } }); + + test("is a surface hook raea's shader check takes, with the four maps sampled linear and clamped", () => { + const spec = surfaceShader("MeshStandardMaterial", sh); + expect(spec.wesl).toContain("fn surface(in: VertexOutput, pbr: ptr)"); + expect(Object.keys(sh.textures)).toEqual(["c0", "c1", "c2", "c3"]); + expect(sh.textures.c2.src).toBe("/s/causes2.png"); + expect(sh.textures.c0.sampler).toEqual({ filter: "linear", mipmap: "none", wrap: "clamp" }); + // a mesh the bake didn't give a second UV set is left as it was + expect(sh.wesl).toMatch(/@if\(VERTEX_UVS_B\) \{\s+wear_surface\(in\.uv_b, pbr\);/); + }); + + test("reads only the uniforms and textures it's given", () => { + const used = new Set([...sh.wesl.matchAll(/\bu_([a-z]+)\(\)/g)].map((m) => m[1])); + expect([...used].sort()).toEqual(Object.keys(sh.uniforms).sort()); + const tex = new Set([...sh.wesl.matchAll(/\btex_([a-z0-9]+)\b/g)].map((m) => m[1])); + expect([...tex].sort()).toEqual(Object.keys(sh.textures).sort()); + expect(Object.keys(sh.uniforms).length).toBeLessThanOrEqual(16); + }); + + test("has one function per class and a case for each, by the bake's number", () => { + WEAR_CLASSES.forEach((c, i) => { + expect(sh.wesl).toContain(`fn wear_${c.name}(s: ptr, c: WearCauses, g: WearGeo, m: WearMarks)`); + expect(sh.wesl).toContain(`case ${i}: { wear_${c.name}(s, c, g, m); }`); + }); + }); + + test("takes only the classes asked for, the rest drawn as they are", () => { + const iron = wearShader({ baked, classes: ["iron"] }); + expect(iron.wesl).toContain("case 5: { wear_iron(s, c, g, m); }"); + expect(iron.wesl).not.toContain("fn wear_plaster("); + expect(iron.wesl).toContain("default: { return false; }"); + expect(() => wearShader({ baked, classes: ["marble" as never] })).toThrow(/no class "marble"/); + }); + + test("declares each function once", () => { + const fns = [...sh.wesl.matchAll(/\bfn\s+([A-Za-z_][A-Za-z0-9_]*)/g)].map((m) => m[1]); + expect(fns.filter((f, i) => fns.indexOf(f) !== i)).toEqual([]); + const d = wearShader({ baked, debug: "class" }).wesl; + const dfns = [...d.matchAll(/\bfn\s+([A-Za-z_][A-Za-z0-9_]*)/g)].map((m) => m[1]); + expect(dfns.filter((f, i) => dfns.indexOf(f) !== i)).toEqual([]); + }); + + test("never names a WGSL reserved word", () => { + const reserved = ["class", "filter", "type", "match", "mod", "module", "self", "set", "get", "new", "from", "target", "common", "enum", "union", "export", "final", "move", "pass", "precise", "static", "this", "typedef", "using", "where", "yield"]; + const idents = new Set([...sh.wesl.matchAll(/\b(?:let|var|fn)\s+([A-Za-z_][A-Za-z0-9_]*)|\b([A-Za-z_][A-Za-z0-9_]*)\s*:\s*(?:f32|i32|u32|bool|vec[234]|ptr|Wear)/g)].map((m) => m[1] ?? m[2])); + for (const r of reserved) expect(idents.has(r)).toBe(false); + }); +}); + +describe("the uniforms", () => { + test("carry age, the day's weather and the season's own share", () => { + const sh = wearShader({ baked, age: { years: 60, soot: 0.9, upkeep: 0.3 }, weather: { day: 300, wet: 0.4, snow: 0.2 }, up: [0, 0, 1] }); + expect(sh.uniforms.up).toEqual([0, 0, 1]); + expect(sh.uniforms.age).toEqual([60, 0.9, 0.3, 16]); + expect(sh.uniforms.wx).toEqual([0.4, 0.2, 0, 0]); + const [damp, dust, leaves, melt] = sh.uniforms.season; + expect(leaves).toBeGreaterThan(0.85); + expect(damp).toBe(0); + expect(dust).toBe(0); + expect(melt).toBe(0.5); + }); + test("the season follows the day: spring's damp, summer's dust, autumn's leaves", () => { + expect(seasonOf({ day: 100 }).damp).toBeGreaterThan(0.7); + expect(seasonOf({ day: 200 }).dust).toBeGreaterThan(0.7); + expect(seasonOf({ day: 200, wet: 1 }).dust).toBe(0); + expect(seasonOf({ day: 20 })).toEqual({ damp: 0, dust: 0, leaves: 0, melt: 0 }); + expect(seasonOf({ day: 300, leaves: 0.1 }).leaves).toBe(0.1); + }); + test("refuse what isn't a share or a day", () => { + expect(() => wearShader({ baked, age: { soot: 2 } })).toThrow(/age.soot is 0..1/); + expect(() => wearShader({ baked, age: { years: -1 } })).toThrow(/age.years/); + expect(() => wearShader({ baked, weather: { day: 400 } })).toThrow(/weather.day/); + expect(() => wearShader({ baked: { density: 4 } as never })).toThrow(/four maps/); + }); +}); + +describe("the debug mode", () => { + test("draws each cause from a lit hook alone", () => { + for (const cause of Object.keys(DEBUG_CAUSES) as WearCause[]) { + const d = wearShader({ baked, debug: cause }); + surfaceShader("MeshStandardMaterial", d); + expect(d.wesl).toContain("fn lit(in: VertexOutput, pbr: PbrInput, color: vec4) -> vec4"); + expect(d.wesl).not.toContain("fn surface("); + expect(d.wesl).toContain(`out = vec4(${DEBUG_CAUSES[cause]}, 1.0);`); + } + // each class its own colour, in the table's order + expect(wearShader({ baked, debug: "class" }).wesl).toContain("case 5: { return vec3(0.250, 0.300, 0.400); }"); + expect(() => wearShader({ baked, debug: "rain" as never })).toThrow(/debug is one of/); + }); +}); diff --git a/sdk/wear/tsconfig.json b/sdk/wear/tsconfig.json new file mode 100644 index 000000000..a65b75207 --- /dev/null +++ b/sdk/wear/tsconfig.json @@ -0,0 +1 @@ +{ "extends": "../tsconfig.base.json", "include": ["src"] } diff --git a/systems/weathering/src/lib.rs b/systems/weathering/src/lib.rs index 3328d73f8..41892c0ed 100644 --- a/systems/weathering/src/lib.rs +++ b/systems/weathering/src/lib.rs @@ -5,12 +5,13 @@ //! raea/wear's recipes read the maps, one WESL function per material class. //! //! in: a .glb (static meshes), the environment (`Env::of`), the material classes (`scene::Classes::of`) -//! out: in the store under the request's content key: model.glb (TEXCOORD_1 added, charts.rs), causes0-2.png, +//! out: in the store under the request's content key: model.glb (TEXCOORD_1 added, charts.rs), causes0-3.png, //! meta.json; the same request gives the same bytes //! //! causes0 = (sky: rain reach, sun: share of the sun's path seen, cavity: occlusion within CAVITY_REACH, edge: //! nearness to a convex arris); causes1 = (flow, deposit, deposit kind, touch); causes2 = (ground: height over the -//! walking surface, 0-4 m; element: the chart's own number; open: the share of the whole sky seen; spare). +//! walking surface, 0-4 m; element: the chart's own number; open: the share of the whole sky seen; class: the +//! triangle's material class as `Class::code`); causes3 = (winter sun, drift, litter, spare). mod charts; mod flow; @@ -34,7 +35,7 @@ pub use scene::{Class, Classes}; type R = Result; /// Bumped when a bake's bytes would change for the same request. -const VERSION: &str = "raea-sys-weathering 4"; +const VERSION: &str = "raea-sys-weathering 6"; /// The world the bake's causes come from. #[derive(Clone, Debug)] @@ -175,7 +176,8 @@ pub fn bake(store: &Path, src: &Path, req: &Value) -> R { let covered = at.covered(); let mut files = Map::new(); for (name, mut m) in [("causes0", c0), ("causes1", c1), ("causes2", c2), ("causes3", c3)] { - maps::dilate(&mut m, &mut covered.clone(), at.w, at.h, charts::GUTTER); + let exact: &[usize] = if name == "causes2" { &[1, 3] } else { &[] }; + maps::dilate(&mut m, &mut covered.clone(), at.w, at.h, charts::GUTTER, exact); let p = dir.join(format!("{name}.png")); maps::png(&p, &m, at.w, at.h)?; files.insert(name.into(), json!(p.display().to_string())); diff --git a/systems/weathering/src/maps.rs b/systems/weathering/src/maps.rs index b43218ee1..bed97c7fb 100644 --- a/systems/weathering/src/maps.rs +++ b/systems/weathering/src/maps.rs @@ -6,7 +6,9 @@ use std::path::Path; use super::R; -pub(crate) fn dilate(m: &mut [[u8; 4]], covered: &mut [bool], w: u32, h: u32, passes: u32) { +/// The channels in `exact` aren't averaged: an empty texel takes its first covered neighbour's (causes2's element and +/// class are numbers, and a mean of two is a third). +pub(crate) fn dilate(m: &mut [[u8; 4]], covered: &mut [bool], w: u32, h: u32, passes: u32, exact: &[usize]) { let (w, h) = (w as i64, h as i64); for _ in 0..passes { let mut fill: Vec<(usize, [u8; 4])> = Vec::new(); @@ -16,7 +18,7 @@ pub(crate) fn dilate(m: &mut [[u8; 4]], covered: &mut [bool], w: u32, h: u32, pa if covered[i] { continue; } - let (mut s, mut k) = ([0u32; 4], 0u32); + let (mut s, mut k, mut first) = ([0u32; 4], 0u32, [0u8; 4]); for dy in -1..=1 { for dx in -1..=1 { let (xx, yy) = (x + dx, y + dy); @@ -25,6 +27,9 @@ pub(crate) fn dilate(m: &mut [[u8; 4]], covered: &mut [bool], w: u32, h: u32, pa } let j = (yy * w + xx) as usize; if covered[j] { + if k == 0 { + first = m[j]; + } for c in 0..4 { s[c] += m[j][c] as u32; } @@ -33,7 +38,11 @@ pub(crate) fn dilate(m: &mut [[u8; 4]], covered: &mut [bool], w: u32, h: u32, pa } } if k > 0 { - fill.push((i, s.map(|v| ((v + k / 2) / k) as u8))); + let mut v = s.map(|v| ((v + k / 2) / k) as u8); + for &c in exact { + v[c] = first[c]; + } + fill.push((i, v)); } } } diff --git a/systems/weathering/src/rays.rs b/systems/weathering/src/rays.rs index 908ca193f..d6bac3e1d 100644 --- a/systems/weathering/src/rays.rs +++ b/systems/weathering/src/rays.rs @@ -221,7 +221,7 @@ pub(crate) fn causes(sc: &Scene, at: &Atlas, env: &Env, mesh: &TriMesh, c0: &mut let ground = ground(sc, mesh, env, t.tri, p, n); let element = (crate::rays::hash(at.chart_of[t.tri as usize].wrapping_add(17)) & 0xff) as u8; let c3 = [byte(x.december), byte(x.drift), byte(x.litter), 255]; - ([byte(sky), byte(x.sun), byte(occl), byte(edge)], [byte(ground / GROUND_RANGE), element, byte(open), 255], c3) + ([byte(sky), byte(x.sun), byte(occl), byte(edge)], [byte(ground / GROUND_RANGE), element, byte(open), sc.class[t.tri as usize].code()], c3) }) .collect(); for (t, (a, b, c)) in at.texels.iter().zip(vals) { diff --git a/systems/weathering/src/scene.rs b/systems/weathering/src/scene.rs index 8929e38f4..4c5356444 100644 --- a/systems/weathering/src/scene.rs +++ b/systems/weathering/src/scene.rs @@ -1,7 +1,8 @@ //! glTF to triangles with a class each, and back with TEXCOORD_1. Read: every primitive of every node's mesh (a mesh //! two nodes use is baked twice, as two meshes, since its two places weather apart), its positions put in the world //! by the node's transform. Written: each primitive's vertices split where its charts meet (charts.rs), every -//! attribute copied element by element in the new order (whatever its type), TEXCOORD_1 added, and the binary +//! attribute copied element by element in the new order (whatever its type), TEXCOORD_1 added (and as TEXCOORD_0 where +//! there's none, since Bevy's standard surface reads the first UV set wherever the second is), and the binary //! rebuilt with only what is still used. Refused: skins, morph targets, .gltf with outside files, sparse accessors, //! modes other than triangles, and a material that already reads TEXCOORD_1 (a lightmapped model). @@ -55,6 +56,11 @@ impl Class { pub fn name(self) -> &'static str { NAMES.iter().find(|(_, c)| *c == self).map(|(n, _)| *n).unwrap_or("plaster") } + /// causes2's alpha (raea/wear reads it with textureLoad): 16 x (its place in NAMES + 1), so 0 is a texel no class + /// covered and a byte off by a few still names the class. + pub fn code(self) -> u8 { + 16 * (NAMES.iter().position(|(_, c)| *c == self).unwrap_or(0) as u8 + 1) + } } /// By material name, a class or {alpha: {CODE: class}, default}: COLOR_0's alpha (0-255) picks the class, the @@ -290,7 +296,13 @@ pub(crate) fn write(sc: &Scene, at: &Atlas) -> R> { attrs.insert(name.clone(), json!(push(&nb, Some(34962), acc))); } let uvb: Vec = uv.iter().flat_map(|q| q.iter().flat_map(|f| f.to_le_bytes())).collect(); - attrs.insert("TEXCOORD_1".into(), json!(push(&uvb, Some(34962), json!({"componentType": 5126, "type": "VEC2", "count": uv.len()})))); + let tc1 = push(&uvb, Some(34962), json!({"componentType": 5126, "type": "VEC2", "count": uv.len()})); + attrs.insert("TEXCOORD_1".into(), json!(tc1)); + // Bevy's standard surface reads the first UV set wherever there's a second, so a primitive with none gets the + // atlas's as its first too (no texture of its own reads it) + if !attrs.contains_key("TEXCOORD_0") { + attrs.insert("TEXCOORD_0".into(), json!(tc1)); + } let ib: Vec = idx.iter().flat_map(|i| i.to_le_bytes()).collect(); let ia = push(&ib, Some(34963), json!({"componentType": 5125, "type": "SCALAR", "count": idx.len()})); let mut np = p.clone(); diff --git a/systems/weathering/src/tests.rs b/systems/weathering/src/tests.rs index bf309b0c7..28afd8c4e 100644 --- a/systems/weathering/src/tests.rs +++ b/systems/weathering/src/tests.rs @@ -218,12 +218,42 @@ fn a_bake_is_the_same_bytes_twice_and_adds_texcoord_1() { for m in sc.glb.json["meshes"].as_array().unwrap() { for p in m["primitives"].as_array().unwrap() { assert!(p["attributes"].get("TEXCOORD_1").is_some(), "a primitive without TEXCOORD_1"); + assert_eq!(p["attributes"]["TEXCOORD_0"], p["attributes"]["TEXCOORD_1"], "a primitive with no UV set of its own gets the atlas's as its first"); assert!(p["attributes"].get("NORMAL").is_some(), "NORMAL was dropped"); } } let _ = std::fs::remove_dir_all(&tmp); } +#[test] +fn each_texel_carries_its_class_and_the_gutters_a_real_one() { + let cl = Classes::of(&json!({"ground": "ground", "slab": "iron", "*": "plaster"})).unwrap(); + let e = env(json!({"density": 4, "rays": {"sky": 8, "cavity": 4}})); + let sc = scene::read(&glb(front()), &cl).unwrap(); + let mesh = rays::mesh_of(&sc); + let at = charts::build(&sc, &e, &mesh).unwrap(); + let n = (at.w * at.h) as usize; + let (mut c0, mut c2, mut c3) = (vec![[0u8; 4]; n], vec![[0u8; 4]; n], vec![[0u8; 4]; n]); + rays::causes(&sc, &at, &e, &mesh, &mut c0, &mut c2, &mut c3); + let codes = [scene::Class::Ground.code(), scene::Class::Iron.code(), scene::Class::Plaster.code()]; + assert_eq!(codes, [224, 96, 16], "the codes the shader decodes"); + for t in &at.texels { + assert_eq!(c2[t.y as usize * at.w as usize + t.x as usize][3], sc.class[t.tri as usize].code(), "texel {},{}", t.x, t.y); + } + // the gutters copy a neighbour's class and element rather than averaging two into a third + let mut covered = at.covered(); + let before = covered.iter().filter(|c| **c).count(); + maps::dilate(&mut c2, &mut covered, at.w, at.h, charts::GUTTER, &[1, 3]); + assert!(covered.iter().filter(|c| **c).count() > before, "dilation filled no gutter"); + let elements: std::collections::HashSet = at.texels.iter().map(|t| c2[t.y as usize * at.w as usize + t.x as usize][1]).collect(); + for (i, px) in c2.iter().enumerate() { + if covered[i] { + assert!(codes.contains(&px[3]), "texel {i} has class code {}", px[3]); + assert!(elements.contains(&px[1]), "texel {i} has element {}, no chart's", px[1]); + } + } +} + /// A bake of a real file, run by hand: RAEA_WEAR_SRC (a .glb), RAEA_WEAR_REQ (a request .json), RAEA_WEAR_STORE. /// cargo test --release -p raea-sys-weathering -- --ignored bake_a_file --nocapture #[test]