Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/manual/whats-new/0.1.8.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 26 additions & 2 deletions docs/wear.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 <Model>'s <Recover>, a <MeshStandardMaterial shader> or a
Expand All @@ -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 <ExtendedMaterial base={<StandardMaterial/>} extension={<ShaderExtension {...wear}/>}> (one glTF
material by the base's `material`) or <SurfaceShader {...wear}>; 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
Expand Down
2 changes: 2 additions & 0 deletions sdk/raea/src/native-calls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, number>; max_side?: number; rays?: { sky?: number; cavity?: number } }; classes?: Record<string, string | { alpha: Record<string, string>; 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<string, number> } };
/** 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 */
Expand Down
3 changes: 3 additions & 0 deletions sdk/tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand Down Expand Up @@ -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/*"]
Expand Down
10 changes: 10 additions & 0 deletions sdk/wear/package.json
Original file line number Diff line number Diff line change
@@ -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" }
}
125 changes: 125 additions & 0 deletions sdk/wear/src/causes.ts
Original file line number Diff line number Diff line change
@@ -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<f32>) -> 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<f32>(textureDimensions(tex_c2));
let at = vec2<i32>(clamp(floor(uv * dims), vec2<f32>(0.0), dims - vec2<f32>(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>) -> f32 {
var q = fract(p * vec2<f32>(123.34, 456.21));
q += dot(q, q + 45.32);
return fract(q.x * q.y);
}
fn wear_vn2(p: vec2<f32>) -> 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<f32>(1.0, 0.0)), u.x), mix(wear_h21(i + vec2<f32>(0.0, 1.0)), wear_h21(i + vec2<f32>(1.0, 1.0)), u.x), u.y);
}
fn wear_fbm2(p: vec2<f32>) -> 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<f32>(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<f32>, h: f32, wall: f32 }

fn wear_geo(wp: vec3<f32>, nrm: vec3<f32>, 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<f32>;
if (length(side) > 0.3) {
let t = normalize(cross(U, side));
p = vec2<f32>(dot(wp, t), dot(wp, U));
} else {
let r = select(vec3<f32>(1.0, 0.0, 0.0), vec3<f32>(0.0, 0.0, 1.0), abs(U.x) > 0.9);
let e1 = normalize(cross(U, r));
p = vec2<f32>(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<f32>(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<f32>(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<f32>(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<f32>(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<f32>, rough: f32, metal: f32 }
`;
73 changes: 73 additions & 0 deletions sdk/wear/src/debug.ts
Original file line number Diff line number Diff line change
@@ -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 <ToneMapping mode="none"/> 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<f32>(c.sky)",
/** the year's sun: black in shade all year, yellow in the most */
sun: "vec3<f32>(1.0, 0.85, 0.2) * c.sun",
/** occlusion within 0.5 m */
cavity: "vec3<f32>(c.cav)",
/** nearness to a convex arris */
edge: "vec3<f32>(c.edge)",
/** run-off passing, in blue */
flow: "vec3<f32>(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<f32>(0.1 + 0.25 * c.sky), vec3<f32>(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<f32>(wear_h21(vec2<f32>(c.elem, 1.0)), wear_h21(vec2<f32>(c.elem, 2.0)), wear_h21(vec2<f32>(c.elem, 3.0)))",
/** the share of the whole sky seen */
open: "vec3<f32>(c.open)",
/** December's sun, in pale blue */
winter: "vec3<f32>(0.6, 0.8, 1.0) * c.dec",
/** driven snow's reach */
drift: "vec3<f32>(c.drift)",
/** where blown leaves and snow settle, in orange */
litter: "vec3<f32>(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<f32>(${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<f32> {
switch k {
case 2: { return vec3<f32>(0.75, 0.3, 0.08); }
case 3: { return vec3<f32>(0.2, 0.8, 0.55); }
case 4: { return vec3<f32>(0.95); }
default: { return vec3<f32>(0.45); }
}
}
fn wear_ramp(t: f32) -> vec3<f32> {
let x = clamp(t, 0.0, 1.0);
return clamp(vec3<f32>(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<f32>(0.0), vec3<f32>(1.0));
}
fn wear_class_colour(k: i32) -> vec3<f32> {
switch k {
${cases}
default: { return vec3<f32>(1.0, 0.0, 1.0); }
}
}
fn lit(in: VertexOutput, pbr: PbrInput, color: vec4<f32>) -> vec4<f32> {
var out = color;
@if(VERTEX_UVS_B) {
let c = wear_causes(in.uv_b);
out = vec4<f32>(${expr}, 1.0);
}
return out;
}
`;
}
Loading
Loading