git clone https://garage44.eu/jeroen/cartopolis.git.. /assets/shaders/building_windows.wesl
// Windows on the 3DBAG buildings — a pattern in the wall, not quads on it.
//
// `osm_buildings` builds real window quads and has since long before this. It
// cannot help here, because `osm_buildings` *despawns* its own detail patch over
// every cell 3DBAG covers (`lod22_covers`) — so over the Netherlands, which is
// the whole point of that dataset, the buildings you actually see had no windows
// at all, by day or by night.
//
// Quads were the wrong answer at this scale and the arithmetic is not close. A
// z14 LoD2.2 cell is thousands of buildings and ~240k triangles; four floors of
// windows on each is tens of megabytes of extra mesh per cell, on the lane that
// already owns the largest assets in the app and the documented device-loss
// churn (`docs/notes/upload-bursts.md`). So nothing is stored: the windows are
// computed per fragment from the wall itself, which costs no vertex, no upload
// and no residency, and reaches every building rather than a patch around the
// camera. `systems::grass` made the same call for the same reason ("generated,
// not scattered"), as did `globe_ocean` ("a BRDF split, not water").
//
// Three things are load-bearing:
//
// * **A wall is found by its normal, not by a flag.** The source tags each
// triangle roof/wall, but that tag is not in the mesh and putting it there
// would cost a vertex attribute on the biggest geometry lane we have. A
// near-horizontal normal is a roof and a near-vertical one is a wall, which is
// what the tag means anyway.
// * **The lattice must survive the floating origin.** `world_position` under
// big_space jumps by a whole `WORLD_CELL_EDGE` (10,000 m) as the camera crosses
// a grid cell — the trap `water.wgsl` avoids with UV0 and `grass.wgsl` with
// `MeshTag`. Neither is available here (a shared vertex belongs to two walls
// with different tangents, so no per-vertex coordinate can serve both), so the
// lattice is chosen to *divide* 10,000 instead: `lattice.x` and `lattice.y`
// both do, and the per-window hash is taken modulo `HASH_PERIOD`, which divides
// the resulting index step. The pattern is then bit-identical either side of a
// jump. Change either pitch without checking that and every window in the city
// changes brightness when you fly 10 km.
// * **The horizontal coordinate comes from the wall's own tangent.** Two
// coplanar triangles of one wall share a normal, so they share a tangent and
// the columns line up across the seam between them. Adjacent walls of a
// building meet at a corner where nothing needs to line up.
//
// Heights are measured from each building's own base, which `lod22` packs into
// two spare bytes a vertex and `building_vertex.wesl` hands over as the normal's
// length (`height_code`). A mesh without it — the OSM lane — still measures
// from the world's zero, which is its foot only with the terrain layer off; on
// Dutch relief that puts its ground-floor band and plinth a metre or two out.
import bevy_pbr::render::{
forward_io::{VertexOutput, FragmentOutput},
pbr_fragment::pbr_input_from_standard_material,
pbr_functions::{alpha_discard, apply_pbr_lighting, main_pass_post_lighting_processing},
pbr_types::{PbrInput, STANDARD_MATERIAL_FLAGS_UNLIT_BIT},
};
import super::ground_bounce;
import super::cloud_field as cf;
import super::graffiti_cells as gc;
import bevy_pbr::render::mesh_view_bindings as view_bindings;
// x: column pitch (m). y: floor pitch (m). z: window width (m). w: window height (m).
// → `facade.lattice`, in `Facade` below.
// x: night factor, 0 by day → 1 after dusk. y: emissive gain at full night.
// z: how much a window darkens the facade by day. w: lowest wall height (m) that
// carries windows at all — below it a fragment is a garden wall or a shed.
// → `facade.glow`, in `Facade` below.
// rgb: the lit colour, linear. a unused.
// → `facade.lit`, in `Facade` below.
// x: occupancy block size (m). y: blocks per hash period. z, w: the fraction of
// windows lit in the deadest and the liveliest block.
// → `facade.block`, in `Facade` below.
// ─── The ghosted building ────────────────────────────────────────────────────
//
// One building's walls are taken out of this pass, so you can see into it —
// which is what happens when the avatar walks inside one. `systems::interiors`
// has already built the room behind the wall, and `building_select` draws a
// translucent shell in the wall's place; this is the half that has to *stop*
// drawing, because a merged cell mesh has no per-building entity to hide.
//
// It is a polygon test on a uniform rather than a per-vertex key, and the reason
// is the one the module note gives for the windows: a vertex attribute on this
// lane costs four bytes on the largest geometry in the app, permanently, to serve
// a state that is off almost always. The ring is rewritten each frame in render
// space, so the floating origin is somebody else's problem.
//
// x: how many ring points are live (0 = nothing ghosted, and then this whole
// block is two instructions). y: 1 when the roof goes as well as the walls — the
// cutaway (`systems::cutaway`). z, w unused.
// → `facade.ghost`, in `Facade` below.
// The ring's bounding box in render-space XZ: min x, min z, max x, max z. The
// broad phase — without it every wall fragment in the city walks the polygon.
// → `facade.ghost_bounds`, in `Facade` below.
// The ring, two points per element (xy then zw), in render-space XZ.
// → `facade.ghost_ring`, in `Facade` below.
// The seven above as one uniform, not seven: WebGPU allows 12 uniform buffers
// per shader stage, bevy's view bindings spend five and `StandardMaterial` one,
// so seven more failed this pipeline in every browser. `BuildingWindowsGpu` in
// `building_windows.rs` is the Rust side and must keep this order.
struct Facade {
lattice: vec4<f32>,
glow: vec4<f32>,
lit: vec4<f32>,
block: vec4<f32>,
ghost: vec4<f32>,
ghost_bounds: vec4<f32>,
ghost_ring: array<vec4<f32>, 16>,
}
@group(constants::MATERIAL_BIND_GROUP) @binding(100) var<uniform> facade: Facade;
// The cloud deck, for the clouds' shadows (`cloud_field::sun_shadow`).
@group(constants::MATERIAL_BIND_GROUP) @binding(102) var<storage, read> clouds: cf::Deck;
// Real graffiti from Groningen, cut out of Commons photographs
// (`tools/graffiti/gen.ts`; its cells in `graffiti_cells.wesl`).
@group(constants::MATERIAL_BIND_GROUP) @binding(103) var graffiti_sheet: texture_2d<f32>;
@group(constants::MATERIAL_BIND_GROUP) @binding(104) var graffiti_sampler: sampler;
/// Ring points this shader can hold. A BAG outline simplified at 10 cm is about
/// nine points; anything longer is simplified again on the CPU before it is sent,
/// which moves a tint boundary by centimetres and nothing else.
const GHOST_MAX_POINTS: i32 = 32;
/// A normal this far off horizontal is a wall. Roofs — including the steep
/// pitched ones 3DBAG actually carries — stay above it and get nothing.
const WALL_NORMAL_Y: f32 = 0.42;
/// The window hash repeats on this many columns and floors. It has to divide the
/// index step a floating-origin jump produces (10,000 / pitch), which is what
/// keeps the pattern stable — see the module note. 32 divides both 4,000 and
/// 3,200, and 32 windows is ~80 m, wider than almost any single facade.
const HASH_PERIOD: f32 = 32.0;
/// Roughness of a pane, against the wall's own (0.95 — as matte as the parameter
/// goes).
///
/// Not near zero. A perfectly smooth pane mirrors the sky, and with no
/// screen-space reflection behind it that arrives as a flat block of sky colour
/// with a hard edge on every window in the city. At this value the pane takes a
/// long specular highlight and a sheen of the environment map, which is what
/// glass does at the distances anybody looks at these buildings from.
const GLASS_ROUGHNESS: f32 = 0.14;
/// Reflectance of a pane, against bevy's 0.5 dielectric default (~4% at normal
/// incidence). Glass sits nearer 8%.
const GLASS_REFLECTANCE: f32 = 0.72;
/// How much of the sky a pane's reflection still gets when it is aimed level
/// across a street, at the facades opposite, and the reflection's elevation
/// (its direction's y) over which that opens back to the full sky. A street of
/// four-storey houses, seen from across it, hides the sky up to about 30°.
const GLASS_STREET_OPEN: f32 = 0.3;
const GLASS_SKY_FROM: f32 = 0.05;
const GLASS_SKY_TO: f32 = 0.55;
/// Peak-to-peak spread of the wall's own roughness between lattice cells, around
/// whatever the material asks for (0.95 on both building lanes).
///
/// Small on purpose. At this end of the parameter the specular lobe is broad
/// either way, so ±0.05 is a change in how a wall *catches* a low sun and not a
/// change in what it is made of — which is the whole ask. Widening it to where
/// you can see it per cell turns a facade into a chequerboard, because the cell
/// is a hard-edged lattice and nothing here blurs across one.
const WALL_FINISH_RANGE: f32 = 0.10;
/// Cheap 2D value hash. Not for cryptography and not for a large domain — it is
/// asked for one number per window per frame.
fn hash2(p: vec2<f32>) -> f32 {
let h = dot(p, vec2<f32>(127.1, 311.7));
return fract(sin(h) * 43758.5453123);
}
// ─── Roofs ───────────────────────────────────────────────────────────────────
//
// The roof's *colour* is measured (atlas's `roofs` dataset, baked into the
// vertex colour by `lod22`); what it is made of is drawn here, the same way the
// windows are: from the fragment's own position and normal, nothing stored. A
// pitched roof is courses of tiles, a flat one is a finish chosen by its
// measured tone. Every pattern is normalised to a mean of 1 and converges on 1
// below a pixel, so from a distance a roof is exactly the colour atlas measured
// and up close it has the texture that colour is the average of.
//
// Known limit, shared with the windows: a pitched roof's course and column
// lattice runs along the roof's own slope, which is not an axis, so a
// floating-origin jump (10 km of flight) shifts the pattern's phase on it. The
// tiles are then drawn one course over from where they were — a one-frame change
// in which tile is dark, at a distance where nobody can count tiles.
/// A normal closer to vertical than this is a flat roof — `lod22`'s
/// `FLAT_ROOF_NORMAL_Y`, which picks the measured flat colour by the same test.
const FLAT_ROOF_Y: f32 = 0.90;
/// Tile courses: the exposed length of one course down the slope, and the
/// width of one tile across it. A Dutch pantile shows ~0.30 × 0.21 m.
const TILE_COURSE_M: f32 = 0.30;
const TILE_WIDTH_M: f32 = 0.22;
/// The mean of the tile pattern below. The measured colour is the lit face of
/// a tile — atlas drops the darkest quarter of a roof's pixels, which is where
/// the laps' shadows went — so the pattern is not renormalised: up close the
/// faces are the measured colour and the laps darker, and at range the roof
/// converges on this share of it, which is what the photo's own average is.
const TILE_MEAN: f32 = 0.88;
/// Width of one bitumen roll.
const ROLL_M: f32 = 1.0;
/// The roof coverings atlas measures from a city model
/// (`building_windows::RoofCovering`; 0 is "not measured"). Those not named
/// here are drawn as the unmeasured roof is.
const ROOF_GLAZED: u32 = 2u;
const ROOF_CONCRETE: u32 = 3u;
const ROOF_SLATE: u32 = 4u;
const ROOF_METAL: u32 = 5u;
const ROOF_SOLAR: u32 = 6u;
/// One slate's exposed width and course.
const SLATE_M: vec2<f32> = vec2<f32>(0.25, 0.2);
/// The slate pattern's mean, as `TILE_MEAN` is the tiles'.
const SLATE_MEAN: f32 = 0.9;
/// Standing seams this far apart.
const SEAM_M: f32 = 0.5;
/// One solar panel, across and down the slope. Both divide the floating
/// origin's 10,000 m jump, so a flat roof's panels, laid along the world axes,
/// do not shift with it.
const SOLAR_PANEL_M: vec2<f32> = vec2<f32>(1.0, 1.6);
const SOLAR_MEAN: f32 = 0.95;
/// A panel's aluminium frame, linear.
const SOLAR_FRAME: vec3<f32> = vec3<f32>(0.45, 0.47, 0.5);
/// A field of solar panels at a point: (tone of the cells, how much frame).
/// Along the slope on a pitched roof (`u`, `down`), along the world axes on a
/// flat one, which records no direction for them.
fn roof_panels(xz: vec2<f32>, ny: f32, u: f32, down: f32) -> vec2<f32> {
let p = select(vec2<f32>(u, down), xz, ny >= FLAT_ROOF_Y) / SOLAR_PANEL_M;
let f = fract(p);
let edge = min(
min(f.x, 1.0 - f.x) * SOLAR_PANEL_M.x,
min(f.y, 1.0 - f.y) * SOLAR_PANEL_M.y,
);
let frame = 1.0 - smoothstep(0.015, 0.03, edge);
// Six cells across and ten down, their joins a faint lighter grid.
let c = fract(f * vec2<f32>(6.0, 10.0));
let join = 1.0 - smoothstep(0.0, 0.05, min(min(c.x, 1.0 - c.x), min(c.y, 1.0 - c.y)));
return vec2<f32>(1.0 + 0.12 * join, frame);
}
/// The scale of a gravel ballast's grain, and of the weathering, repairs and
/// ponding that make one flat roof blotchy at a distance. Both divide the
/// floating origin's 10,000 m jump into a multiple of the 256 `hash_wrapped`
/// repeats on (124,928 and 2,048 steps), so a flat roof — whose lattices run
/// along the world axes — is bit-identical either side of one.
const GRAIN_M: f32 = 0.0800461;
const PATCH_M: f32 = 4.8828125;
// ─── Walls ───────────────────────────────────────────────────────────────────
//
// The rest of a building, on the terms the roofs above are drawn on: what the
// shader is told about it arrives packed in the vertex colour's alpha, and what
// a wall is made of is read off its colour — the era palette already chose red
// brick for a 1900 terrace and pale render for a 2005 block, and a measured
// colour says the same thing. Every pattern converges on the wall's own colour
// below a pixel.
/// A shop's ground floor, and the storey everything else is divided into.
const SHOP_FLOOR_M: f32 = 4.2;
/// The least wall a window keeps between its head and the eaves.
const EAVE_CLEAR_M: f32 = 0.3;
const STOREY_M: f32 = 3.0;
/// With the eaves known, the ground floor's windows start at a real sill.
const GROUND_SILL_M: f32 = 0.7;
/// A shop window, with the fascia over it.
const SHOP_PANE_M: vec2<f32> = vec2<f32>(2.3, 2.9);
/// A Dutch waal-format brick laid with its joint: 210 + 10 mm long, 50 + 12.5 mm
/// a course. Below a pixel from a few tens of metres, which the blur handles.
const BRICK_LENGTH_M: f32 = 0.22;
const BRICK_COURSE_M: f32 = 0.0625;
/// The share of a brick wall that is mortar, for keeping its mean the wall's
/// colour: bed joints are 0.2 of a course, head joints 0.045 of a brick.
const MORTAR_SHARE: f32 = 0.24;
/// The two mortars a Dutch wall is pointed in, linear: a grey cement and a
/// sandy lime. The joint was one 0.42 grey — near white in sRGB — and with a
/// joint a fifth of every course the walls up close read as tiling.
const CEMENT: vec3<f32> = vec3<f32>(0.22, 0.215, 0.2);
const LIME: vec3<f32> = vec3<f32>(0.3, 0.26, 0.19);
/// What a joint averages to once the recess has shaded it (~0.6 of its
/// colour), for keeping the wall's mean its own colour.
const MORTAR: vec3<f32> = vec3<f32>(0.155, 0.143, 0.118);
/// The shadow a gutter and the eaves' overhang throw down the wall.
const GUTTER_M: f32 = 0.35;
/// Window size in metres for an era (`building_windows::era`): tall narrow
/// sashes before 1920, squarer between the wars and after, long horizontal
/// bands in the system-built decades, large panes since.
fn pane_for_era(era: u32, fallback: vec2<f32>) -> vec2<f32> {
switch era {
case 1u: { return vec2<f32>(0.95, 1.85); }
case 2u: { return vec2<f32>(1.4, 1.5); }
case 3u: { return vec2<f32>(2.35, 1.2); }
case 4u: { return vec2<f32>(1.9, 2.1); }
default: { return fallback; }
}
}
/// What atlas measured this building's walls to be made of
/// (`building_windows::Facade::material`): 0 unknown, 1 brick, 2 render, 3
/// panel. Set once per fragment from the packed code, before any wall is drawn;
/// private to the invocation, so it needs threading through nothing.
var<private> wall_material: u32 = 0u;
/// What a wall of colour `c` (linear) is made of, as (brick, concrete, render)
/// weights summing to 1: brick where it is a saturated warm colour, concrete
/// panels where it is a mid grey, render where it is pale — or, where atlas
/// measured it ([`wall_material`]), what it measured, whatever the colour.
fn wall_kinds(c: vec3<f32>) -> vec3<f32> {
switch wall_material {
case 1u: { return vec3<f32>(1.0, 0.0, 0.0); }
case 2u: { return vec3<f32>(0.0, 0.0, 1.0); }
case 3u: { return vec3<f32>(0.0, 1.0, 0.0); }
default: {}
}
let luma = dot(c, vec3<f32>(0.2126, 0.7152, 0.0722));
let hi = max(c.r, max(c.g, c.b));
let sat = (hi - min(c.r, min(c.g, c.b))) / max(hi, 1e-4);
let brick_w = smoothstep(0.18, 0.32, sat) * (1.0 - smoothstep(0.3, 0.45, luma)) * f32(c.r >= c.b);
let pale_w = smoothstep(0.28, 0.42, luma) * (1.0 - brick_w);
return vec3<f32>(brick_w, (1.0 - brick_w) * (1.0 - pale_w), pale_w);
}
/// What the wall is made of, drawn over its colour `c` (linear) — see
/// [`wall_kinds`]. `brick` is the brick lattice, `floor_f` the continuous
/// floor coordinate, `brick_px`/`px` how much of each lattice a pixel spans.
fn wall_finish(
c: vec3<f32>,
u: f32,
v: f32,
floor_f: f32,
brick: vec2<f32>,
brick_px: f32,
px: f32,
) -> vec3<f32> {
let kinds = wall_kinds(c);
let brick_w = kinds.x;
let concrete_w = kinds.y;
let pale_w = kinds.z;
// Brick: stretcher bond, each course half a brick over, every brick its own
// firing. The face is darkened by just enough that face and mortar together
// average to `c`, so the colour a wall was given is the colour it reads as.
let course = floor(brick.y);
let along = brick.x + 0.5 * course;
let fb = vec2<f32>(fract(along), fract(brick.y));
let id = vec2<f32>(floor(along), course);
// A joint is as wide as the mason left it: each bed a millimetre or two
// thicker or thinner than the last and wandering along its length, each
// head joint its own. The other readers of the lattice keep the nominal
// shares, which these differ from by a few millimetres at most.
let bed = 0.2 + 0.06 * (hash_wrapped(vec2<f32>(course, 5.0)) - 0.5)
+ 0.05 * (value_noise(vec2<f32>(u / 0.4, course)) - 0.5);
let head = 0.045 + 0.025 * (hash_wrapped(id + vec2<f32>(29.0, 7.0)) - 0.5);
let mortar = max(f32(fb.y < bed), f32(fb.x < head));
let fired = 0.88 + 0.24 * hash_wrapped(id + vec2<f32>(5.0, 3.0));
// …and not only lighter or darker: a ramp through what one kiln load
// gives — terracotta, brown, a purple that went darker in the hot part of
// the kiln — and about one in twenty-five dark and glazed.
let pick = hash_wrapped(id + vec2<f32>(17.0, 41.0));
let terracotta = vec3<f32>(1.1, 0.96, 0.84);
let brown = vec3<f32>(0.95, 0.94, 0.92);
let purple = vec3<f32>(0.88, 0.8, 0.92);
// Divided by the ramp's mean, (T + 2B + P) / 4, so it does not move the wall.
let ramp = select(mix(terracotta, brown, pick * 2.0), mix(brown, purple, pick * 2.0 - 1.0), pick > 0.5)
/ vec3<f32>(0.97, 0.91, 0.9);
let burnt = f32(hash_wrapped(id + vec2<f32>(71.0, 13.0)) > 0.96);
let firing = ramp * fired * mix(1.0, 0.55, burnt);
let face = max((c - MORTAR_SHARE * MORTAR) / (1.0 - MORTAR_SHARE), c * 0.3);
// Faded out earlier than the windows are: a course is 6 cm, and at a few
// pixels a course the bond beats against the pixel grid into moiré rings
// across the whole wall (seen on the Deck at 20–40 m, 2026-09-30).
let brick_blur = clamp(brick_px * 3.0 - 0.3, 0.0, 1.0);
// The joint is recessed, so the brick over it shades it — deepest right
// under the brick, from the sky as much as from the sun, which is why this
// reads in shade where the relief in `wall_relief` does not. A head joint,
// open to the sky above it, less so.
let recess = select(0.7, mix(0.85, 0.35, smoothstep(0.2, 1.0, fb.y / bed)), fb.y < bed);
// Mortar is not one mix: a grey cement on one stretch of wall and a sandy
// lime on the next, where it was repointed or built in another year, and
// within either darker where rain has carried dirt into it.
let lime = smoothstep(0.35, 0.65, value_noise(vec2<f32>(u / 7.0, v / 5.0) + vec2<f32>(13.0, 29.0)));
let mix_col = mix(CEMENT, LIME, lime);
let dirt = mix(1.05, 0.8, value_noise(vec2<f32>(u, v) / 0.6));
// …and it is a sand, not a paint: a grain of a few millimetres, gone well
// before the joint is.
let grain_k = 1.0 - clamp(brick_px * 30.0 - 0.3, 0.0, 1.0);
let grain = 1.0 + 0.3 * (value_noise(vec2<f32>(u, v) / 0.004) - 0.5) * grain_k;
let bricked = mix(mix(face * firing, mix_col * recess * dirt * grain, mortar), c, brick_blur);
// Concrete: panel joints a window bay apart and at every floor, and the
// streaks rain leaves running down from each sill.
let jx = abs(fract(u / 2.5 + 0.5) - 0.5) * 2.5;
let jy = abs(fract(floor_f + 0.5) - 0.5) * STOREY_M;
let joint = (1.0 - smoothstep(0.0, 0.03, min(jx, jy))) * (1.0 - clamp(px * 1.6 - 0.35, 0.0, 1.0));
let streak = value_noise(vec2<f32>(u / 0.7, v / 5.0)) - 0.5;
let panelled = c * (1.0 + 0.18 * streak) * mix(1.0, 0.55, joint);
// Render: a plaster finish, faintly uneven.
let rendered = c * (1.0 + 0.1 * (value_noise(vec2<f32>(u, v) / 1.3) - 0.5));
return bricked * brick_w + panelled * concrete_w + rendered * pale_w;
}
/// [`wall_finish`], with a large block's pale wall clad instead of plastered
/// ([`is_cladding`]). The other two kinds are drawn as they were.
fn wall_finish_clad(
c: vec3<f32>,
u: f32,
v: f32,
floor_f: f32,
brick: vec2<f32>,
brick_px: f32,
px: f32,
along: f32,
era: u32,
eave: f32,
slab: vec2<f32>,
slab_px: f32,
) -> vec3<f32> {
let base = wall_finish(c, u, v, floor_f, brick, brick_px, px);
if !is_cladding(era, eave) {
return base;
}
let pale_w = wall_kinds(c).z;
let rendered = c * (1.0 + 0.1 * (value_noise(vec2<f32>(u, v) / 1.3) - 0.5));
return base + (cladding(c, along, v, floor_f, era, slab, slab_px, px) - rendered) * pale_w;
}
/// How a brick wall's roughness varies over its lattice, as a factor on the
/// wall's own: 1 in the joints, 0.8–0.95 on a brick's face, its own value per
/// brick and a little grain across it. Gone with the bricks' blur.
fn brick_sheen(brick: vec2<f32>, brick_px: f32, brick_w: f32) -> f32 {
let course = floor(brick.y);
let along = brick.x + 0.5 * course;
let fb = vec2<f32>(fract(along), fract(brick.y));
let mortar = max(f32(fb.y < 0.2), f32(fb.x < 0.045));
let skin = 0.8 + 0.15 * hash_wrapped(vec2<f32>(floor(along), course) + vec2<f32>(43.0, 89.0));
let grain = 1.0 + 0.06 * (value_noise(brick * vec2<f32>(6.0, 2.0)) - 0.5);
let face = mix(skin * grain, 1.0, mortar);
let k = brick_w * (1.0 - clamp(brick_px * 3.0 - 0.3, 0.0, 1.0));
return mix(1.0, face, k);
}
/// A natural-stone cladding slab, laid in stack bond with open joints — the
/// usual skin of a pale office or apartment block since 1990.
const SLAB_M: vec2<f32> = vec2<f32>(1.2, 0.6);
/// Whether a pale wall is a large post-war block's cladding rather than a
/// house's plaster: built since 1960 and three storeys or more to the eaves.
/// Plaster stays on what is older or lower, which is where it is in this
/// country; the register says nothing about a facade's material, so this is
/// the era palette's own reasoning carried one step further.
fn is_cladding(era: u32, eave: f32) -> bool {
return era >= 3u && eave >= 9.0;
}
/// A pale block's skin, drawn over its colour `c`: since 1990 stone slabs
/// ([`SLAB_M`]), each cut from its own part of the block — lighter or darker,
/// warmer or cooler, with a faint bed running through it — and dark open
/// joints; between 1960 and 1990 storey-high precast panels, a window bay
/// wide, their washed aggregate a little speckled and streaked under each
/// joint. `slab` is the slab lattice and `slab_px` how much of it a pixel spans.
fn cladding(c: vec3<f32>, along: f32, v: f32, floor_f: f32, era: u32, slab: vec2<f32>, slab_px: f32, px: f32) -> vec3<f32> {
if era >= 4u {
let id = floor(slab);
let f = slab - id;
let d = min(f, vec2<f32>(1.0) - f) * SLAB_M;
let joint = 1.0 - smoothstep(0.003, 0.007, min(d.x, d.y));
let blur = clamp(slab_px * 3.0 - 0.3, 0.0, 1.0);
let h = hash_wrapped(id + vec2<f32>(37.0, 19.0));
let g = hash_wrapped(id + vec2<f32>(3.0, 77.0)) - 0.5;
let cut = vec3<f32>(0.92 + 0.16 * h) * vec3<f32>(1.0 + 0.05 * g, 1.0, 1.0 - 0.06 * g);
// The stone's bed, running along the slab, faded with the slab itself.
let bed = 1.0 + 0.06 * (value_noise(vec2<f32>(along / 0.35, v / 0.04) + id * 3.1) - 0.5);
let laid = mix(c * cut * bed, c * 0.45, joint);
return mix(laid, c, blur);
}
// Precast: the concrete branch's panels, finer in grain.
let jx = abs(fract(along / 2.4 + 0.5) - 0.5) * 2.4;
let jy = abs(fract(floor_f + 0.5) - 0.5) * STOREY_M;
let joint = (1.0 - smoothstep(0.0, 0.02, min(jx, jy))) * (1.0 - clamp(px * 1.6 - 0.35, 0.0, 1.0));
let panel = hash_wrapped(floor(vec2<f32>(along / 2.4, floor_f)) + vec2<f32>(9.0, 51.0)) - 0.5;
let streak = value_noise(vec2<f32>(along / 0.5, v / 4.0)) - 0.5;
return c * (1.0 + 0.08 * panel + 0.12 * streak) * mix(1.0, 0.5, joint);
}
/// A wall at the scale of a few metres: the brick of a later repair, a
/// lighter patch where the sun bleached it, a darker one where the rain runs.
/// Every brick is varied, but until this a wall was the same colour from one
/// end to the other, which is what made a terrace read as printed card at any
/// distance past its bricks. Zero-mean, and faded on the wall's *sharper* axis
/// (`wall_px`, metres) between half a metre and one and a half, so it holds on
/// a wall seen down a street and is gone before a whole facade is a few pixels.
fn wall_drift(u: f32, v: f32, wall_px: f32) -> vec3<f32> {
let k = 1.0 - smoothstep(0.5, 1.5, wall_px);
let broad = value_noise(vec2<f32>(u / 4.0, v / 3.0) + vec2<f32>(91.0, 7.0)) - 0.5;
let near = value_noise(vec2<f32>(u / 1.3, v / 1.1) + vec2<f32>(23.0, 57.0)) - 0.5;
let warm = value_noise(vec2<f32>(u / 6.0, v / 6.0) + vec2<f32>(5.0, 83.0)) - 0.5;
let tone = 1.0 + (0.16 * broad + 0.08 * near) * k;
return vec3<f32>(tone) * (vec3<f32>(1.0) + vec3<f32>(0.05, 0.0, -0.05) * warm * k);
}
/// One side of a raised edge, as a slope from 0 to 1 and back: a parabola over
/// `[a, a + w]`, so the tilt has no step at either end of the edge.
fn arris(x: f32, a: f32, w: f32) -> f32 {
let t = clamp((x - a) / w, 0.0, 1.0);
return 4.0 * t * (1.0 - t);
}
/// The wall's relief, on the lattices [`wall_finish`] colours, as a tilt of its
/// normal — (along the wall, up) in the units `frame_bevel` uses, i.e. minus the
/// surface's slope. Nothing is displaced and nothing is stored: under a low sun
/// a brick wall reads as brick because each course's top arris catches the light
/// and its lower one does not, and that is shading, not silhouette. `kinds` is
/// [`wall_kinds`] of the wall's own colour, `m_px` how many metres one pixel
/// spans on it.
///
/// Every term fades out on its own lattice's blur, and earlier than the colour
/// does: a tilt that survives at a few pixels a course is a highlight crawling
/// over the wall as the camera moves, which is worse than no relief at all.
fn wall_relief(
kinds: vec3<f32>,
u: f32,
v: f32,
floor_f: f32,
brick: vec2<f32>,
brick_px: f32,
m_px: f32,
) -> vec2<f32> {
var tilt = vec2<f32>(0.0, 0.0);
// Brick: the face stands a few millimetres proud of a recessed joint, with
// rounded arrises — rising out of the joint, falling into the next one. The
// joint shares are `wall_finish`'s (0.045 of a brick, 0.2 of a course); an
// arris is ~13 mm either way — wider than a real one's rounding, because at
// 8 mm it was a pixel line in sun even from 3 m (mac-2, 2026-10-20). Each
// brick is also laid a hair off the plane.
let brick_k = kinds.x * (1.0 - clamp(brick_px * 5.0 - 0.3, 0.0, 1.0));
if brick_k > 0.0 {
let course = floor(brick.y);
let along = brick.x + 0.5 * course;
let fb = vec2<f32>(fract(along), fract(brick.y));
let laid = hash_wrapped(vec2<f32>(floor(along), course) + vec2<f32>(11.0, 29.0));
let x = arris(fb.x, 1.0 - 0.06, 0.06) - arris(fb.x, 0.045, 0.06);
let y = arris(fb.y, 1.0 - 0.2, 0.2) - arris(fb.y, 0.2, 0.2);
let face = f32(fb.x > 0.045 && fb.y > 0.2);
let jitter = vec2<f32>(fract(laid * 7.3) - 0.5, fract(laid * 13.1) - 0.5) * 0.08 * face;
// Some bricks are crisp and some weathered round, and where the joint
// was repointed flush the arrises are half lost in it.
let worn = mix(0.35, 1.2, fract(laid * 5.71));
let flush = mix(1.0, 0.4, smoothstep(0.5, 0.8, value_noise(vec2<f32>(u, v) / 0.31 + vec2<f32>(7.0, 3.0))));
tilt += (vec2<f32>(x, y) * worn * flush + jitter) * brick_k;
}
// Concrete: the panel joints are sawn grooves, 3 cm across, whose walls face
// into the joint.
let groove_k = kinds.y * (1.0 - clamp(m_px / 0.03 * 3.0 - 0.3, 0.0, 1.0));
if groove_k > 0.0 {
let gx = (fract(u / 2.5 + 0.5) - 0.5) * 2.5;
let gy = (fract(floor_f + 0.5) - 0.5) * STOREY_M;
let in_x = 1.0 - smoothstep(0.012, 0.018, abs(gx));
let in_y = 1.0 - smoothstep(0.012, 0.018, abs(gy));
tilt += vec2<f32>(-sign(gx) * in_x, -sign(gy) * in_y) * 0.6 * groove_k;
}
// Render: a hand-floated plaster is not a plane, and a raking light finds the
// waves in it. Slope from a finite difference of the noise, a few percent.
let render_k = kinds.z * (1.0 - clamp(m_px / 0.4 * 3.0 - 0.3, 0.0, 1.0));
if render_k > 0.0 {
let p = vec2<f32>(u, v) / 0.4;
let h0 = value_noise(p);
let slope = vec2<f32>(value_noise(p + vec2<f32>(0.1, 0.0)) - h0, value_noise(p + vec2<f32>(0.0, 0.1)) - h0);
tilt -= slope * 1.2 * render_k;
}
return tilt;
}
/// How weathered a kept wall of this era is, 0 to 1, before the building's own
/// draw: the older the brick, the more of a damp line and rain streaks; the
/// system-built decades' concrete stains as badly as anything older. Unknown
/// (era 0) takes a middling value rather than none, so it does not read as new.
///
/// Before 1880 it goes on climbing with the antiquity, by the century: a
/// warehouse of 1650 has stood through three more of them than a house of 1910.
fn era_weathering(era: u32, antiquity: u32) -> f32 {
switch era {
case 1u: { return 0.6 + 0.15 * f32(antiquity); }
case 2u: { return 0.4; }
case 3u: { return 0.45; }
case 4u: { return 0.15; }
default: { return 0.3; }
}
}
/// [`grimed`]'s two marks at a kept building's strength `age` (0–1): a damp
/// line rising off the ground, and streaks where rain runs down the wall. `v`
/// is metres above the building's own base, so the line starts at its foot.
///
/// The damp line has a floor of its own: every wall standing on a pavement
/// takes the rain splashing back off it, however new, so even this decade's
/// building carries half of it.
///
/// It has to read as the same wall in different weather, not as a course of
/// different brick, and a band with an even top did the second (judged on
/// mac-2, 2026-10-20). So the line is ragged at three scales and soft, and the
/// wet is patchy inside it. And it is **mostly in the mortar**: lime mortar is
/// what drinks, holds the dirt and grows the green, so on a brick wall (`brick`
/// is the brick lattice, `brick_w` how much of one this is, fading with
/// distance) the joints go dark and the bricks only deepen a little, each by
/// its own amount. Wet brick is the dry one deepened — darker *and* more
/// saturated — rather than dimmed. A wall with no joints takes the face's
/// share alone, a little more of it.
fn weathered(
c: vec3<f32>,
u: f32,
v: f32,
age: f32,
fade: f32,
brick: vec2<f32>,
brick_w: f32,
) -> vec3<f32> {
let wet = clamp(age * 1.8, 0.6, 1.0);
let line = 0.55 * value_noise(vec2<f32>(u / 2.9, 3.5))
+ 0.3 * value_noise(vec2<f32>(u / 0.8, 9.5))
+ 0.15 * value_noise(vec2<f32>(u / 0.23, 17.5));
let rise = mix(0.35, 1.2, line) * mix(0.6, 1.0, wet);
let patchy = value_noise(vec2<f32>(u / 0.35, v / 0.25) + vec2<f32>(3.0, 11.0)) - 0.5;
// Fully wet over the lower third, then giving out raggedly: soft enough not
// to be a course of other brick, full enough below to be seen at all — the
// version that faded from the ground up could not be (deck-1, 2026-10-20).
let reach = v + 0.3 * rise * patchy;
let damp = (1.0 - smoothstep(rise * 0.35, rise, reach)) * wet;
let course = floor(brick.y);
let along = brick.x + 0.5 * course;
let fb = vec2<f32>(fract(along), fract(brick.y));
// `wall_finish`'s joint shares.
let joint = max(f32(fb.y < 0.2), f32(fb.x < 0.045)) * brick_w;
let thirst = hash_wrapped(vec2<f32>(floor(along), course) + vec2<f32>(23.0, 61.0));
let face = damp * mix(0.3, mix(0.05, 0.35, thirst), brick_w) * (1.0 - joint);
// Darker, and deeper in hue: each channel loses more the further it sits
// below the brightest. Not `pow(c, 1.35)`, which says the same thing and is
// undefined at 0 under Metal's fast maths.
let peak = max(max(c.r, c.g), max(c.b, 1e-4));
let soaked = c * (0.55 + 0.25 * c / peak);
var out = mix(c, soaked, face);
// The joints: wet, dirty, and green where they stay wet longest.
let algae = smoothstep(0.5, 0.75, value_noise(vec2<f32>(u / 0.9, v / 0.6) + vec2<f32>(41.0, 5.0)));
let grime = mix(vec3<f32>(0.55, 0.53, 0.48), vec3<f32>(0.42, 0.52, 0.34), algae);
out = mix(out, out * grime, damp * joint);
// …and where the water evaporates it leaves its salt in them: a faint pale
// tide along the top of the wet, broken where the line is.
// Squared by hand: `pow` of a negative base is undefined in WGSL, and below
// the line this one is negative — the NaN it made took the whole wall's
// colour, windows and all, on Metal (mac-2, 2026-10-20).
let off = (reach - rise) / 0.07;
let tide = exp(-off * off) * wet * joint
* smoothstep(0.35, 0.65, value_noise(vec2<f32>(u / 0.5, 29.5)));
out = mix(out, vec3<f32>(0.72, 0.7, 0.66), 0.25 * tide);
let streak = smoothstep(0.6, 0.9, value_noise(vec2<f32>(u / 0.45, v / 7.0) + vec2<f32>(19.0, 0.0)));
return out * (1.0 - 0.25 * age * streak * fade);
}
/// The green a wall grows over the centuries, at a kept building's `moss`
/// (0–1, nothing on a wall built since 1880): a film of algae and moss in
/// patches over its lower storey, climbing higher the older it is, and dark
/// green run-off streaks down the whole face. `north` is how squarely the wall
/// faces north (0–1): a wall the sun never dries grows several times more than
/// one it bakes, which is what makes it read as growth rather than as paint.
/// Multiplied in rather than painted over, so the brick and its joints show
/// through the green, as they do through the real thing.
fn mossed(c: vec3<f32>, u: f32, v: f32, moss: f32, north: f32, fade: f32) -> vec3<f32> {
if moss <= 0.0 {
return c;
}
let shade = mix(0.3, 1.0, north);
let top = 1.2 + 3.0 * moss;
let reach = v + 0.8 * (value_noise(vec2<f32>(u / 1.7, 7.5)) - 0.5) * top;
let low = 1.0 - smoothstep(top * 0.3, top, reach);
let patches = smoothstep(0.62 - 0.3 * moss, 0.8, value_noise(vec2<f32>(u / 1.1, v / 0.8) + vec2<f32>(71.0, 13.0)));
let film = clamp(low * patches * moss * shade * 1.6, 0.0, 0.9);
// Half the brick tinted, half the moss's own olive: a tint alone only
// darkened a dark red wall, and read as damp rather than as green
// (mac-2, 2026-11-28).
let green = mix(c * vec3<f32>(0.55, 0.75, 0.4), vec3<f32>(0.1, 0.14, 0.06), 0.5);
var out = mix(c, green, film);
let run = smoothstep(0.55, 0.85, value_noise(vec2<f32>(u / 0.3, v / 5.0) + vec2<f32>(37.0, 4.0)));
out = mix(out, out * vec3<f32>(0.55, 0.62, 0.48), run * moss * mix(0.5, 1.0, north) * fade);
// And centuries of city air over the whole face.
return out * (1.0 - 0.12 * moss);
}
/// Height of a facade's plinth — the darker, harder-wearing course it stands on.
const PLINTH_M: f32 = 0.5;
/// A plinth at the foot of a building from before 1960 whose eaves are known,
/// with a thin ledge along its top; and on a building from before 1920 a stone
/// cornice under the eaves. `dv` is how many metres of wall one pixel spans.
///
/// Before 1960 only, since `v` started at each building's real foot
/// (2026-10-20): until then the band mostly sat under the pavement, and once it
/// stood on every wall it read as a stripe painted along the whole street.
fn base_and_cornice(c: vec3<f32>, v: f32, eave: f32, era: u32, dv: f32) -> vec3<f32> {
if eave <= 0.0 {
return c;
}
let crisp = 1.0 - clamp(dv * 12.0 - 0.5, 0.0, 1.0);
let old = f32(era == 1u || era == 2u);
let plinth = f32(v < PLINTH_M - 0.04) * old;
let ledge = f32(v >= PLINTH_M - 0.04 && v < PLINTH_M) * old;
var out = c * mix(1.0, 0.72, plinth) * mix(1.0, 1.3, ledge * crisp);
if era == 1u && eave > 6.0 {
let cornice = f32(v > eave - 0.7 && v < eave - 0.32);
let shadow = f32(v > eave - 0.8 && v <= eave - 0.7);
out = mix(out, SILL_COLOUR * 1.25, cornice * crisp);
out = out * mix(1.0, 0.6, shadow * crisp);
}
return out;
}
/// The eaves' shadow: a dark band under the gutter, fading out below a pixel.
fn gutter_shade(v: f32, eave: f32, gutter_px: f32) -> f32 {
if eave <= 0.0 {
return 1.0;
}
let below = eave - v;
let band = step(-0.05, below) * (1.0 - smoothstep(0.6 * GUTTER_M, GUTTER_M, below));
return mix(1.0, 0.5, band * (1.0 - clamp(gutter_px * 1.6 - 0.35, 0.0, 1.0)));
}
// ─── Doors ───────────────────────────────────────────────────────────────────
/// `building_windows::DOOR_STEP_M` and `DOOR_CLEAR_M`, and `lod22`'s leaf size.
const DOOR_STEP_M: f32 = 0.2;
const DOOR_CLEAR_M: f32 = 1.0;
const DOOR_W: f32 = 1.1;
const DOOR_H: f32 = 2.1;
/// A doorway part (`lod22::push_doorway`). The surround, reveals and step have
/// UV0.y = -0.5 and keep their plain colour; the leaf has UV0 = (x + 10 × style,
/// -1 - y) and is drawn here: panels, glass and a handle. Style 3 is a window's
/// pane in a designed facade (`lod22_facade`): all glass, no handle.
fn door_surface(pbr_input: ptr<function, PbrInput>, uv: vec2<f32>) {
if uv.y > -0.75 {
// UV0.x = 1 is a scaffold's galvanised tube (`scaffolding`).
let metal = f32(uv.x > 0.5 && uv.x < 1.5);
(*pbr_input).material.perceptual_roughness = mix(0.7, 0.45, metal);
(*pbr_input).material.metallic = 0.7 * metal;
return;
}
let style = floor(uv.x / 10.0);
let x = uv.x - 10.0 * style;
let y = -uv.y - 1.0;
let rim = 0.11;
var glass = 0.0;
var tone = 1.0;
if style < 0.5 {
// Panelled, with a glazed transom: two raised panels below a lock rail,
// split by a muntin, each with a darker bevel round it.
let lower = x > rim && x < DOOR_W - rim && y > 0.25 && y < 1.2
&& abs(x - DOOR_W * 0.5) > 0.05;
if lower {
let ex = min(x - rim, DOOR_W - rim - x);
let ey = min(y - 0.25, 1.2 - y);
let edge = min(min(ex, ey), abs(x - DOOR_W * 0.5) - 0.05);
tone = select(1.1, 0.72, edge < 0.035);
}
glass = f32(x > rim && x < DOOR_W - rim && y > 1.35 && y < DOOR_H - 0.13);
} else if style < 1.5 {
// A glass door in a slim frame.
glass = f32(x > 0.07 && x < DOOR_W - 0.07 && y > 0.07 && y < DOOR_H - 0.07);
} else if style < 2.5 {
// Flush, with a tall glass strip on the lock side.
glass = f32(x > DOOR_W * 0.6 && x < DOOR_W - 0.13 && y > 0.3 && y < DOOR_H - 0.3);
} else {
// A window's pane: glass edge to edge; its frame is geometry.
glass = 1.0;
}
let c = (*pbr_input).material.base_color.rgb;
var col = mix(c * tone, vec3<f32>(0.02, 0.025, 0.03), glass);
var metal = 0.0;
// The handle, on the lock side at hand height.
if style < 2.5 && length(vec2<f32>(x - (DOOR_W - 0.13), y - 1.05)) < 0.035 {
col = vec3<f32>(0.55, 0.4, 0.15);
metal = 1.0;
}
(*pbr_input).material.base_color = vec4<f32>(col, (*pbr_input).material.base_color.a);
(*pbr_input).material.metallic = metal;
(*pbr_input).material.perceptual_roughness = mix(mix(0.5, GLASS_ROUGHNESS, glass), 0.3, metal);
(*pbr_input).material.reflectance = mix(
(*pbr_input).material.reflectance,
vec3<f32>(GLASS_REFLECTANCE),
glass,
);
let street = street_reflection((*pbr_input).V, (*pbr_input).N, glass);
(*pbr_input).specular_occlusion *= street;
(*pbr_input).diffuse_occlusion *= street;
}
/// What a pane mirrors from the street is the other side of the street. The
/// environment map holds only sky, so a reflection aimed across the street
/// fetched the bright horizon haze, and every ground-floor window in the city
/// read as one milky grey sheet. The facades opposite are far darker than that
/// haze, so a ray aimed below the rooftops is occluded, opening back to the
/// full sky as it climbs. The factor, by `glass`.
///
/// For `diffuse_occlusion` as well as `specular_occlusion`, because the veil
/// was mostly not the environment map: bevy's `AmbientLight` carries its own
/// specular lobe (`EnvBRDFApprox`), occluded only by the diffuse term, and at a
/// grazing view it laid ~0.05 of linear grey over a pane that was black
/// inside (mac-2, 2026-11-06, interior forced to 0 and the specular occlusion
/// to 0). The room behind the glass dims with it, which is right too: it is
/// lit by the same street.
fn street_reflection(v: vec3<f32>, n: vec3<f32>, glass: f32) -> f32 {
let mirrored = reflect(-v, n);
let open_sky = smoothstep(GLASS_SKY_FROM, GLASS_SKY_TO, mirrored.y);
return mix(1.0, mix(GLASS_STREET_OPEN, 1.0, open_sky), glass);
}
// ─── Windows, up close ───────────────────────────────────────────────────────
/// A room seen through a window by day: nearly black.
const GLASS_BASE: vec3<f32> = vec3<f32>(0.03, 0.034, 0.04);
/// How far the glass sits back from the face of the wall: a frame in a
/// half-brick reveal.
const REVEAL_M: f32 = 0.12;
/// The room behind a pane, by day, found the way a ray finds it: from where
/// the view meets the glass (`pg`, pane-local metres) along the view's
/// direction in the wall's frame (`d`: along, up, into the wall), to the first
/// of the room's two side walls, its floor, its ceiling or its back wall. The
/// room is the window's bay wide (`room_w`), the storey high, from `floor_y`
/// to `ceil_y` around the pane's centre, and three to six metres deep by the
/// window's own draw `r`. Nothing is stored and nothing is drawn but this
/// fragment: it is the old "interior mapping" trick, and what it buys is that
/// a window has a room in it that moves as you walk past.
///
/// Lit by the window it is seen through, so it darkens with depth, and kept as
/// dark as a room seen from a sunny street is — `GLASS_BASE` is its far end.
fn room_behind(pg: vec2<f32>, d: vec3<f32>, room_w: f32, floor_y: f32, ceil_y: f32, r: f32) -> vec3<f32> {
let depth = 3.0 + 3.0 * fract(r * 5.17);
let big = 1e4;
let tx = select(big, (sign(d.x) * room_w * 0.5 - pg.x) / d.x, abs(d.x) > 1e-4);
let ty = select(big, (select(floor_y, ceil_y, d.y > 0.0) - pg.y) / d.y, abs(d.y) > 1e-4);
let tz = depth / max(d.z, 1e-3);
let t = max(min(tx, min(ty, tz)), 0.0);
let hit = pg + d.xy * t;
let z = d.z * t;
// What each surface is: a painted wall in one of a few colours, darker on
// the sides that face the window less; a wooden or carpeted floor; a pale
// ceiling. A cabinet or sofa line along the foot of the back wall.
let paint_pick = fract(r * 23.9);
var paint = vec3<f32>(0.62, 0.6, 0.55);
if paint_pick > 0.75 {
paint = vec3<f32>(0.55, 0.5, 0.4);
} else if paint_pick > 0.5 {
paint = vec3<f32>(0.45, 0.5, 0.52);
}
let floor_col = select(vec3<f32>(0.3, 0.19, 0.1), vec3<f32>(0.25, 0.24, 0.23), fract(r * 31.7) > 0.6);
var col = paint;
if tz <= tx && tz <= ty {
let furniture = f32(hit.y < floor_y + 0.85 && abs(hit.x - (fract(r * 3.3) - 0.5) * room_w * 0.5) < room_w * 0.3);
col = mix(paint, vec3<f32>(0.18, 0.14, 0.12), furniture * f32(fract(r * 11.1) > 0.35));
} else if ty < tx {
col = select(floor_col, vec3<f32>(0.75, 0.74, 0.72), d.y > 0.0);
} else {
col = paint * 0.75;
}
// Daylight from the window, falling off into the room. At 0.16 the room
// was there and could not be seen through the sky the glass reflects
// (mac-2, 2026-10-29); this is about what a room off a bright street
// shows from across a pavement.
let light = 0.34 * exp(-z * 0.32) + 0.05;
return max(col * light, GLASS_BASE);
}
/// What is in the window of a room, drawn from the window's own hash `r`: net
/// curtains or drapes at the sides of about a third of them, a blind part-way
/// down in one in five, and the rest the room behind (`room`).
fn room_through_glass(pm: vec2<f32>, hs: vec2<f32>, r: f32, room: vec3<f32>) -> vec3<f32> {
let pick = fract(r * 13.7);
let fabric_pick = fract(r * 29.3);
var fabric = vec3<f32>(0.34, 0.3, 0.24);
if fabric_pick > 0.6 {
fabric = vec3<f32>(0.2, 0.05, 0.04);
} else if fabric_pick > 0.35 {
fabric = vec3<f32>(0.3, 0.3, 0.3);
}
if pick < 0.35 {
let curtain = f32(abs(pm.x) > hs.x * 0.6);
return mix(room, fabric * 0.5, curtain);
}
if pick < 0.55 {
let drop = mix(0.25, 0.7, fract(r * 7.1));
let blind = f32(pm.y > hs.y * (1.0 - 2.0 * drop));
let slat = 0.8 + 0.2 * f32(fract(pm.y / 0.045) > 0.3);
return mix(room, vec3<f32>(0.42, 0.4, 0.36) * 0.55 * slat, blind);
}
return room;
}
/// A shop by day, through its window: lit, and busy — racks and displays at
/// no particular spacing, brighter under the ceiling lights.
fn shop_through_glass(pm: vec2<f32>, hs: vec2<f32>, along: f32) -> vec3<f32> {
let racks = 0.7 + 0.6 * value_noise(vec2<f32>(along / 0.55, 3.0));
let lights = mix(0.9, 1.5, smoothstep(0.4, 0.95, pm.y / max(hs.y, 0.1)));
return vec3<f32>(0.1, 0.09, 0.075) * racks * lights;
}
// ─── Scaffold netting ────────────────────────────────────────────────────────
/// The netting's mesh pitch, metres.
const NET_MESH_M: f32 = 0.06;
/// Width of one roll of netting; the rolls overlap at a darker seam.
const NET_ROLL_M: f32 = 2.5;
/// Debris netting on a scaffold: a woven sheet, its rolls lapped at seams and
/// sagging between the ties at each lift. The holes show the shadow behind, so
/// up close it is a fabric and from across the street a pale sheet over the
/// facade — which is the whole point of drawing it.
fn netting(pbr_input: ptr<function, PbrInput>, u: f32, v: f32, weave: vec2<f32>, weave_px: f32) -> f32 {
let g = abs(fract(weave) - 0.5);
let line = min(0.5 - g.x, 0.5 - g.y);
let strand = 1.0 - smoothstep(0.1, 0.16, line);
let near = 1.0 - clamp(weave_px * 1.6 - 0.35, 0.0, 1.0);
let seam_d = abs(fract(u / NET_ROLL_M + 0.5) - 0.5) * NET_ROLL_M;
let seam = 1.0 - smoothstep(0.03, 0.07, seam_d);
// Each lift's sheet sags a little away from its tie line.
let sag = 0.9 + 0.1 * smoothstep(0.0, 0.5, abs(fract(v / 2.0) - 0.5));
let c = (*pbr_input).material.base_color;
(*pbr_input).material.base_color = vec4<f32>(c.rgb * sag * mix(1.0, 0.8, seam), c.a);
(*pbr_input).material.perceptual_roughness = 0.95;
(*pbr_input).material.metallic = 0.0;
// How much of the surface the net covers: its strands up close, its mean
// at range, and solid where two rolls overlap.
return max(mix(NET_COVER, strand, near), seam);
}
/// The share of a scaffold's netting that is net rather than hole.
const NET_COVER: f32 = 0.4;
/// The share of a site fence's mesh panel that is wire, at range.
const FENCE_COVER: f32 = 0.18;
/// A site fence's welded mesh (`scaffolding::push_fence`, UV0.x = 3): wires
/// every 7.5 cm across and 22.5 cm up. Returns the wire's coverage.
fn fence_mesh(pbr_input: ptr<function, PbrInput>, wire: vec2<f32>, wire_px: f32) -> f32 {
let g = abs(fract(wire) - 0.5);
let d = 0.5 - g;
let on_wire = max(1.0 - smoothstep(0.03, 0.06, d.x), 1.0 - smoothstep(0.01, 0.025, d.y));
let near = 1.0 - clamp(wire_px * 1.6 - 0.35, 0.0, 1.0);
(*pbr_input).material.perceptual_roughness = 0.5;
(*pbr_input).material.metallic = 0.7;
return mix(FENCE_COVER, on_wire, near);
}
/// The share of a model's clear glass (`lod22::CLEAR_GLASS_UV`) drawn, of the
/// screen door's sixteen: enough to read as a tinted pane, few enough that the
/// sky and the roof behind it show through.
const GLASS_COVER: f32 = 0.4;
/// Clear glass on a model part: smooth and a little metallic, so it catches
/// the sky, with the part's own tint. Returns its coverage.
fn clear_glass(pbr_input: ptr<function, PbrInput>) -> f32 {
(*pbr_input).material.perceptual_roughness = 0.08;
(*pbr_input).material.metallic = 0.3;
return GLASS_COVER;
}
/// Screen-door transparency: whether a fragment of a surface with this
/// coverage is a hole here, on a 4×4 ordered dither. The building material is
/// opaque, and this variant writes no depth in a handset's depth prepass
/// (`keep_holes_out_of_view_prepass`), so a discarded fragment simply shows
/// whatever lies behind it.
fn see_through(coverage: f32, frag: vec2<f32>) -> bool {
let p = vec2<u32>(frag) % vec2<u32>(4u, 4u);
var bayer = array<f32, 16>(
0.0, 8.0, 2.0, 10.0,
12.0, 4.0, 14.0, 6.0,
3.0, 11.0, 1.0, 9.0,
15.0, 7.0, 13.0, 5.0,
);
return coverage <= (bayer[p.y * 4u + p.x] + 0.5) / 16.0;
}
// ─── Neglect ─────────────────────────────────────────────────────────────────
//
// A building the register lists as out of use, or as waiting for demolition
// (`building_windows::Condition`, carried in the occupancy's unused top). It is
// dark at night, its ground floor is boarded, some of the windows above it too,
// a shop's window is whitewashed from the inside the way an empty shop in the
// Netherlands says so, and the walls carry the grime nobody is washing off. The
// register's word only: no other building is aged by this.
/// Weathered plywood over an opening, in pane-local metres: 1.22 m sheets with
/// a darker seam between them and a grain running up them.
fn boarded(pm: vec2<f32>, hs: vec2<f32>, r: f32) -> vec3<f32> {
// Pale yellow-brown plywood, a little greyed by the rain — never as grey as
// glass, or a boarded window reads as a dark pane from the street.
let age = fract(r * 5.3) * 0.35;
let ply = mix(vec3<f32>(0.46, 0.33, 0.16), vec3<f32>(0.3, 0.27, 0.22), age);
let grain = 0.9 + 0.1 * value_noise(vec2<f32>(pm.x / 0.04, pm.y / 0.9));
let seam_y = abs(fract((pm.y + hs.y) / 1.22 + 0.5) - 0.5) * 1.22;
let seam = mix(0.6, 1.0, smoothstep(0.004, 0.012, seam_y));
return ply * grain * seam;
}
/// What boarded windows average to at range.
const BOARD_MEAN: vec3<f32> = vec3<f32>(0.4, 0.3, 0.17);
/// A shop window whitewashed from the inside: swirls of chalk on the glass.
fn whitewashed(pm: vec2<f32>, along: f32) -> vec3<f32> {
let swirl = value_noise(vec2<f32>(along / 0.3, pm.y / 0.3))
+ 0.5 * value_noise(vec2<f32>(along / 0.11, pm.y / 0.11));
return mix(vec3<f32>(0.6, 0.6, 0.56), vec3<f32>(0.4, 0.4, 0.37), swirl * 0.5);
}
/// A neglected wall's grime: damp rising from the foot in a ragged line, and
/// dirt streaked down it by the rain. `neglect` is 1 vacant, 2 condemned;
/// `fade` takes the streaks out below a pixel.
fn grimed(c: vec3<f32>, u: f32, v: f32, neglect: f32, fade: f32) -> vec3<f32> {
let rise = (0.5 + 0.5 * value_noise(vec2<f32>(u / 1.3, 0.5))) * mix(0.8, 1.5, neglect - 1.0);
let damp = 1.0 - smoothstep(rise * 0.5, rise, v);
var out = mix(c, c * vec3<f32>(0.5, 0.56, 0.46), damp * 0.75);
let streak = smoothstep(0.55, 0.9, value_noise(vec2<f32>(u / 0.45, v / 7.0)));
out = out * (1.0 - (0.2 + 0.1 * neglect) * streak * fade);
// …and the whole wall a little duller than a kept one.
return out * mix(0.92, 0.85, neglect - 1.0);
}
// ─── Graffiti ────────────────────────────────────────────────────────────────
//
// A wall on a narrow street (`systems::graffiti`, bit 15 of the code) is
// painted along its foot the way a steeg is: pieces and tags of real Groningen
// graffiti off a sheet cut from Commons photographs (`graffiti_sheet`), some
// faded and some rolled over in a patch of other paint, and over all of it
// layer on layer of drawn tags, mostly black and white, small and large, a few
// dripping: sprayed, with a ragged edge and a mist of overspray. The brick and
// its joints show through all of it (the caller multiplies the wall's own
// texture back in).
//
// Which stretch of a wall carries what is a hash, keyed to the stretch's own
// block of the city so that two alleys do not repeat each other; it is
// decoration, not a survey of anybody's paint (the module note says who asked
// for it).
/// Pieces, and the patches painted over them, are laid one to a stretch this long.
const SPRAY_STRETCH_M: f32 = 3.4;
/// As high as somebody reaches with a can, standing on something.
const SPRAY_TOP_M: f32 = 3.3;
/// A rattle can's colour, linear RGB: loud, because that is what sells.
fn spray_colour(k: f32) -> vec3<f32> {
switch u32(floor(fract(k) * 9.0)) {
case 0u: { return vec3<f32>(0.62, 0.02, 0.32); }
case 1u: { return vec3<f32>(0.02, 0.12, 0.65); }
case 2u: { return vec3<f32>(0.85, 0.55, 0.01); }
case 3u: { return vec3<f32>(0.70, 0.03, 0.02); }
case 4u: { return vec3<f32>(0.03, 0.40, 0.08); }
case 5u: { return vec3<f32>(0.22, 0.04, 0.50); }
case 6u: { return vec3<f32>(0.01, 0.45, 0.55); }
case 7u: { return vec3<f32>(0.85, 0.30, 0.02); }
default: { return vec3<f32>(0.55, 0.40, 0.75); }
}
}
/// A tag's ink: black most of all, then white, chrome, and now and then a colour.
fn tag_ink(k: f32, c: f32) -> vec3<f32> {
if k < 0.5 {
return vec3<f32>(0.012);
} else if k < 0.68 {
return vec3<f32>(0.78, 0.78, 0.76);
} else if k < 0.8 {
return vec3<f32>(0.5, 0.5, 0.52);
}
return spray_colour(c);
}
fn sd_capsule(p: vec2<f32>, a: vec2<f32>, b: vec2<f32>) -> f32 {
let pa = p - a;
let ba = b - a;
let h = clamp(dot(pa, ba) / max(dot(ba, ba), 1e-6), 0.0, 1.0);
return length(pa - ba * h);
}
/// `a` laid over `b`, both colour and coverage.
fn spray_over(b: vec4<f32>, a: vec4<f32>) -> vec4<f32> {
return vec4<f32>(mix(b.rgb, a.rgb, a.a), a.a + b.a * (1.0 - a.a));
}
/// How much paint a stroke leaves `d` metres from its line, `w` its half
/// width: a solid core with a ragged edge (`grain`, noise in 0–1), and a faint
/// mist of overspray past it.
fn spray_cover(d: f32, w: f32, px: f32, grain: f32) -> f32 {
let aa = max(px, 0.003);
let edge = w * (1.0 + 0.35 * (grain - 0.5));
let core = 1.0 - smoothstep(edge - aa, edge + aa, d);
let mist = 0.22 * (1.0 - smoothstep(edge, edge + 1.5 * w + 0.01 + aa, d));
return max(core, mist);
}
/// The seed of the stretch `x` falls in: its index along the wall, and the
/// block of the city its centre stands in. `dir` is the world direction `x`
/// grows in. The block is taken the way the windows' occupancy is
/// (`facade.block`), so it survives a floating-origin jump.
fn spray_seed(xz: vec2<f32>, dir: vec2<f32>, x: f32, len: f32) -> f32 {
let k = floor(x / len);
let centre = xz + dir * ((k + 0.5) * len - x);
let b = floor(centre / facade.block.x);
let key = b - floor(b / facade.block.y) * facade.block.y;
return k + 61.0 * floor(hash2(key + vec2<f32>(71.0, 29.0)) * 97.0);
}
/// One letter's skeleton, for a drawn tag: a stem and two strokes off it,
/// `size` metres. Enough to read as a handstyle from across a steeg.
fn letter_sd(p: vec2<f32>, size: vec2<f32>, seed: f32) -> f32 {
let r1 = hash2(vec2<f32>(seed, 1.0));
let r2 = hash2(vec2<f32>(seed, 2.0));
let r3 = hash2(vec2<f32>(seed, 3.0));
let stem = mix(0.15, 0.4, r1);
let top = vec2<f32>(mix(0.55, 0.95, r2), mix(0.55, 0.95, r3));
let low = vec2<f32>(mix(0.6, 0.95, r3), mix(0.05, 0.4, r1));
var d = sd_capsule(p, vec2<f32>(stem, 0.1) * size, vec2<f32>(stem + (r2 - 0.5) * 0.3, 0.9) * size);
d = min(d, sd_capsule(p, vec2<f32>(stem, 0.85) * size, top * size));
d = min(d, sd_capsule(p, mix(vec2<f32>(stem, 0.5), top, 0.4 * r3) * size, low * size));
return d;
}
/// Cell `cell` of the graffiti sheet laid on the wall `size` metres, `p` metres
/// from its lower left corner: colour and coverage. `dx`, `dy` are how far
/// (along, up) the wall one pixel steps across and down the screen, for the mip
/// — this runs in a branch, where the sampler cannot take its own derivatives.
fn sheet_at(cell: u32, p: vec2<f32>, size: vec2<f32>, dx: vec2<f32>, dy: vec2<f32>) -> vec4<f32> {
let q = p / size;
if q.x < 0.0 || q.y < 0.0 || q.x > 1.0 || q.y > 1.0 {
return vec4<f32>(0.0);
}
// A function-scope copy: a `const` array cannot be indexed at run time.
var cells = gc::CELLS;
let fill = cells[cell].xy;
let corner = vec2<f32>(f32(cell % 4u), f32(cell / 4u));
// Centred in its cell; the texture's rows run down, the wall's up.
let inside = vec2<f32>(0.5 + (q.x - 0.5) * fill.x, 0.5 - (q.y - 0.5) * fill.y);
let uv = (corner + inside) / gc::GRID;
let per_m = vec2<f32>(fill.x / size.x, -fill.y / size.y) / gc::GRID;
return textureSampleGrad(graffiti_sheet, graffiti_sampler, uv, dx * per_m, dy * per_m);
}
/// A cell of the sheet's at its own width (and a little either way), its
/// height from the cut-out's shape. `lift` is where its foot may be.
fn sheet_piece(cell: u32, x: f32, v: f32, seed: f32, room: f32, lift: vec2<f32>, dx: vec2<f32>, dy: vec2<f32>) -> vec4<f32> {
var cells = gc::CELLS;
let c = cells[cell];
let w = min(c.z * mix(0.8, 1.15, hash2(vec2<f32>(seed, 81.0))), room - 0.2);
let h = w * c.y / c.x;
let x0 = 0.1 + hash2(vec2<f32>(seed, 82.0)) * max(room - w - 0.2, 0.0);
let y0 = mix(lift.x, lift.y, hash2(vec2<f32>(seed, 83.0)));
let s = sheet_at(cell, vec2<f32>(x - x0, v - y0), vec2<f32>(w, h), dx, dy);
// Some of it older than the rest.
return vec4<f32>(s.rgb, s.a * mix(0.7, 1.0, hash2(vec2<f32>(seed, 84.0))));
}
/// A stretch of wall somebody rolled over to hide what was on it, in a paint
/// that never quite matched: soft-edged, a little patchy.
fn spray_buff(x: f32, v: f32, seed: f32, wall: vec3<f32>) -> vec4<f32> {
let a = 0.1 + hash2(vec2<f32>(seed, 70.0)) * 1.2;
let b = a + mix(1.2, 2.4, hash2(vec2<f32>(seed, 71.0)));
let top = mix(1.6, 2.6, hash2(vec2<f32>(seed, 72.0)))
+ 0.12 * (value_noise(vec2<f32>(x / 0.4, seed)) - 0.5);
let soft = 0.04;
let inside = smoothstep(a - soft, a + soft, x) * (1.0 - smoothstep(b - soft, b + soft, x))
* smoothstep(0.15, 0.3, v) * (1.0 - smoothstep(top - soft, top + soft, v));
let k = hash2(vec2<f32>(seed, 73.0));
// The wall's own colour, darker or greyer, or a flat grey.
var c = wall * 0.75;
if k < 0.35 {
c = vec3<f32>(0.16, 0.16, 0.17);
} else if k < 0.6 {
c = mix(wall, vec3<f32>(0.2, 0.19, 0.18), 0.6);
}
c = c * mix(0.9, 1.05, value_noise(vec2<f32>(x / 0.5, v / 0.3) + vec2<f32>(seed, 5.0)));
return vec4<f32>(c, inside * 0.95);
}
/// A tag in a stretch `room` metres long: a name, in thin capitals or one
/// looping line, leaning on, sometimes underlined, sometimes dripping. `size` and `rise`
/// bound its height and where its foot is on the wall.
fn spray_tag(
x: f32,
v: f32,
seed: f32,
px: f32,
grain: f32,
room: f32,
size: vec2<f32>,
rise: vec2<f32>,
) -> vec4<f32> {
let h = mix(size.x, size.y, hash2(vec2<f32>(seed, 41.0)));
// Most tags are letters — a handstyle of thin, slanted capitals, each its
// own height — and the rest a name in one looping line.
let printed = hash2(vec2<f32>(seed, 39.0)) < 0.65;
let letters = 3.0 + floor(hash2(vec2<f32>(seed, 48.0)) * 4.0);
let lw = h * mix(0.45, 0.7, hash2(vec2<f32>(seed, 38.0)));
let w = select(h * mix(2.2, 4.0, hash2(vec2<f32>(seed, 40.0))), letters * lw * 0.9, printed);
let x0 = 0.05 + hash2(vec2<f32>(seed, 42.0)) * max(room - w - 0.1, 0.0);
let y0 = mix(rise.x, rise.y, hash2(vec2<f32>(seed, 43.0)));
let p = vec2<f32>(x - x0, v - y0);
if p.x < -0.25 * h - 0.05 || p.x > w + 0.45 * h + 0.05 || p.y < -0.8 * h - 0.05 || p.y > h + 0.05 {
return vec4<f32>(0.0);
}
let lean = 0.3 * h;
var d = 1e3;
if printed {
let slanted = vec2<f32>(p.x - p.y * 0.3, p.y);
for (var i = 0; i < 6; i++) {
if f32(i) >= letters {
break;
}
let fi = f32(i);
let tall = mix(0.7, 1.0, hash2(vec2<f32>(seed, 60.0 + fi)));
let q = slanted - vec2<f32>(fi * lw * 0.9, (1.0 - tall) * 0.5 * h);
d = min(d, letter_sd(q, vec2<f32>(lw, h * tall), seed * 5.0 + fi));
}
} else {
// A point running along while it circles, fast enough that it runs
// back on itself — one loop a letter, each its own height.
let turn = letters * 6.2831853;
let r = 1.8 * w / turn;
let phase = hash2(vec2<f32>(seed, 49.0)) * 6.2831853;
var prev = vec2<f32>(0.0);
for (var i = 0; i <= 22; i++) {
let t = f32(i) / 22.0;
let a = turn * t + phase;
let tall = mix(0.5, 1.0, hash2(vec2<f32>(seed, 50.0 + floor(t * letters))));
let y = h * (0.5 + 0.5 * tall * cos(a));
let next = vec2<f32>(t * w - r * sin(a) + (y / h - 0.5) * lean, y);
if i > 0 {
d = min(d, sd_capsule(p, prev, next));
}
prev = next;
}
}
if hash2(vec2<f32>(seed, 46.0)) < 0.35 {
d = min(d, sd_capsule(p, vec2<f32>(-0.05 * w, -0.15 * h), vec2<f32>(w, -0.05 * h)));
}
let width = h * mix(0.04, 0.09, hash2(vec2<f32>(seed, 44.0)));
// Where the can lingered the paint ran.
if hash2(vec2<f32>(seed, 51.0)) < 0.4 {
for (var j = 0; j < 3; j++) {
let fj = f32(j);
let dx = w * hash2(vec2<f32>(seed, 52.0 + fj));
let fall = h * mix(0.15, 0.8, hash2(vec2<f32>(seed, 55.0 + fj)));
d = min(d, sd_capsule(p, vec2<f32>(dx, 0.1 * h), vec2<f32>(dx, -fall)) + 0.4 * width);
}
}
// Past a pixel a line this thin is a fraction of one, not a blur of it.
let m = spray_cover(d, width, px, grain) * clamp(2.0 * width / px, 0.0, 1.0);
let ink = tag_ink(hash2(vec2<f32>(seed, 45.0)), hash2(vec2<f32>(seed, 47.0)));
return vec4<f32>(ink, m * mix(0.6, 0.95, hash2(vec2<f32>(seed, 53.0))));
}
/// One lattice of tags: `len` metres a stretch, slid `offset` along, a tag in
/// `odds` of them.
fn tag_layer(
xz: vec2<f32>,
dir: vec2<f32>,
x: f32,
v: f32,
px: f32,
grain: f32,
len: f32,
offset: f32,
odds: f32,
size: vec2<f32>,
rise: vec2<f32>,
) -> vec4<f32> {
let tx = x + offset;
let s = spray_seed(xz, dir, tx, len) + offset * 13.0;
if hash2(vec2<f32>(s, 9.0)) >= odds {
return vec4<f32>(0.0);
}
return spray_tag(tx - floor(tx / len) * len, v, s, px, grain, len, size, rise);
}
/// Everything sprayed at `x` metres along a wall (left to right as you face
/// it) and `v` up it, over a wall of colour `wall`. `xz`, `dir` as
/// [`spray_seed`]'s.
fn graffiti(
xz: vec2<f32>,
dir: vec2<f32>,
x: f32,
v: f32,
px: f32,
dx: vec2<f32>,
dy: vec2<f32>,
wall: vec3<f32>,
) -> vec4<f32> {
if v > SPRAY_TOP_M || v < 0.1 {
return vec4<f32>(0.0);
}
// Spray's grain, gone to its average once a pixel spans it.
let grain = mix(value_noise(vec2<f32>(x, v) / 0.015), 0.5, clamp(px / 0.015, 0.0, 1.0));
var out = vec4<f32>(0.0);
// A piece off the sheet on most stretches, and on some of them a patch
// rolled over it.
let ps = spray_seed(xz, dir, x, SPRAY_STRETCH_M);
let lx = x - floor(x / SPRAY_STRETCH_M) * SPRAY_STRETCH_M;
if hash2(vec2<f32>(ps, 7.0)) < 0.7 {
let cell = min(u32(hash2(vec2<f32>(ps, 6.0)) * f32(gc::PIECES)), gc::PIECES - 1u);
out = sheet_piece(cell, lx, v, ps, SPRAY_STRETCH_M, vec2<f32>(0.15, 0.6), dx, dy);
}
if hash2(vec2<f32>(ps, 8.0)) < 0.2 {
out = spray_over(out, spray_buff(lx, v, ps, wall));
}
// The sheet's own tags, on a lattice slid half a stretch along.
let tx = x + 0.5 * SPRAY_STRETCH_M;
let tlen = 2.3;
let ts = spray_seed(xz, dir, tx, tlen) + 7.0;
if hash2(vec2<f32>(ts, 9.0)) < 0.5 {
let cell = gc::PIECES + min(u32(hash2(vec2<f32>(ts, 6.0)) * f32(gc::TAGS)), gc::TAGS - 1u);
let t = sheet_piece(cell, tx - floor(tx / tlen) * tlen, v, ts, tlen, vec2<f32>(0.5, 1.6), dx, dy);
out = spray_over(out, t);
}
// Then the drawn tags, four lattices of them slid against the pieces and
// each other, so they run over the pieces' edges and into every gap: the
// middle-sized ones at eye height, the small ones anywhere, and a few big
// ones high up, out of the next writer's reach.
out = spray_over(out, tag_layer(xz, dir, x, v, px, grain, 2.7, 1.1, 0.45, vec2<f32>(0.3, 0.6), vec2<f32>(1.9, 2.7)));
out = spray_over(out, tag_layer(xz, dir, x, v, px, grain, 1.9, 1.7, 0.7, vec2<f32>(0.18, 0.42), vec2<f32>(0.7, 2.0)));
out = spray_over(out, tag_layer(xz, dir, x, v, px, grain, 1.3, 0.4, 0.6, vec2<f32>(0.1, 0.25), vec2<f32>(0.3, 2.4)));
out = spray_over(out, tag_layer(xz, dir, x, v, px, grain, 1.1, 0.75, 0.5, vec2<f32>(0.08, 0.2), vec2<f32>(0.9, 1.9)));
return out;
}
/// How far to tilt the normal on a window frame's bevel, as (along the wall,
/// up): towards the glass on the inner 40 % of the frame.
fn frame_bevel(pm: vec2<f32>, hs: vec2<f32>, era: u32, shop: bool) -> vec2<f32> {
let fw = select(0.065, 0.05, era >= 3u || shop);
let inner = hs - vec2<f32>(fw, fw);
let a = abs(pm);
let into = a - inner;
let bx = f32(into.x > 0.0 && into.x < fw * 0.4 && a.y < hs.y);
let by = f32(into.y > 0.0 && into.y < fw * 0.4 && a.x < hs.x);
return vec2<f32>(-sign(pm.x) * bx, -sign(pm.y) * by) * 0.6;
}
/// Stone sills, and the lintel tone over an old window.
const SILL_COLOUR: vec3<f32> = vec3<f32>(0.42, 0.41, 0.38);
/// A window's frame: white-painted timber on an old building, brown on a
/// system-built one, dark aluminium on a new one.
fn frame_colour(era: u32) -> vec3<f32> {
switch era {
case 1u, 2u: { return vec3<f32>(0.66, 0.64, 0.58); }
case 3u: { return vec3<f32>(0.07, 0.05, 0.035); }
default: { return vec3<f32>(0.04, 0.042, 0.045); }
}
}
fn lintel_tone(era: u32) -> f32 {
return select(0.78, 1.35, era == 1u);
}
/// A band over the opening: a stone lintel on an old house, a soldier course
/// of darker brick between the wars, none after.
fn lintel(pm: vec2<f32>, hs: vec2<f32>, era: u32) -> f32 {
if era != 1u && era != 2u {
return 0.0;
}
return f32(pm.y > hs.y && pm.y < hs.y + 0.17 && abs(pm.x) < hs.x + 0.08);
}
/// What a point of a window is, given its offset `pm` from the pane's centre and
/// the pane's half size `hs`, both in metres. x: unused. y: frame or glazing
/// bar (1) versus glass (0). z: the glass's shading from the reveal — the
/// opening is set back in the wall, so its top and one side lie in the shadow
/// of the wall around it. w: the sill below it.
fn window_look(pm: vec2<f32>, hs: vec2<f32>, era: u32, shop: bool) -> vec4<f32> {
let a = abs(pm);
let fw = select(0.065, 0.05, era >= 3u || shop);
var bar = f32(a.x > hs.x - fw || a.y > hs.y - fw);
let bw = 0.028;
// Glazing bars by era: a sash window's meeting rail and the bars of its
// upper sash; a transom with a top light between the wars; a transom and
// a mullion every 0.9 m in a system-built band; one mullion in a wide new
// pane; a shop window's transom high up.
switch era {
case 1u: {
let rail = abs(pm.y - hs.y * 0.05) < 0.035;
let upper = pm.y > hs.y * 0.05;
bar = max(bar, f32(rail || (upper && (abs(pm.x) < bw || abs(pm.y - hs.y * 0.55) < bw))));
}
case 2u: {
let transom = abs(pm.y - hs.y * 0.42) < 0.04;
bar = max(bar, f32(transom || (pm.y > hs.y * 0.42 && abs(pm.x) < bw)));
}
case 3u: {
let transom = abs(pm.y - hs.y * 0.5) < 0.035;
let mullion = abs(fract((pm.x + hs.x) / 0.9 + 0.5) - 0.5) * 0.9 < 0.025;
bar = max(bar, f32(transom || mullion));
}
default: {
bar = max(bar, f32(hs.x > 0.7 && abs(pm.x) < 0.035));
}
}
if shop {
bar = max(bar, f32(abs(pm.y - hs.y * 0.62) < 0.04));
}
let inner = hs - vec2<f32>(fw, fw);
let reveal_top = smoothstep(inner.y - 0.14, inner.y, pm.y);
let reveal_side = smoothstep(inner.x - 0.1, inner.x, -pm.x);
let shade = 1.0 - 0.45 * max(reveal_top, 0.6 * reveal_side);
let sill = f32(pm.y < -hs.y && pm.y > -hs.y - 0.07 && a.x < hs.x + 0.06 && !shop);
return vec4<f32>(0.0, bar, shade, sill);
}
/// `hash2` over a lattice index wrapped into a small domain first: `sin` of
/// the index of a 6 cm grain ten kilometres out has no fractional bits left.
fn hash_wrapped(i: vec2<f32>) -> f32 {
return hash2(i - floor(i / 256.0) * 256.0);
}
/// Smooth value noise in [0, 1].
fn value_noise(p: vec2<f32>) -> f32 {
let i = floor(p);
let f = fract(p);
let w = f * f * (3.0 - 2.0 * f);
return mix(
mix(hash_wrapped(i), hash_wrapped(i + vec2<f32>(1.0, 0.0)), w.x),
mix(hash_wrapped(i + vec2<f32>(0.0, 1.0)), hash_wrapped(i + vec2<f32>(1.0, 1.0)), w.x),
w.y,
);
}
/// One ring point, out of the packed pairs.
fn ghost_point(i: i32) -> vec2<f32> {
let v = facade.ghost_ring[i / 2];
return select(v.xy, v.zw, (i & 1) == 1);
}
/// Is this fragment on the wall of the ghosted building?
///
/// Even-odd, the same test `collision::Footprint::contains` runs on the CPU over
/// the same ring — so what you can see through is what you can walk through.
fn in_ghost(p: vec2<f32>) -> bool {
let n = min(i32(facade.ghost.x), GHOST_MAX_POINTS);
if n < 3 {
return false;
}
if p.x < facade.ghost_bounds.x || p.x > facade.ghost_bounds.z
|| p.y < facade.ghost_bounds.y || p.y > facade.ghost_bounds.w {
return false;
}
var inside = false;
var j = n - 1;
for (var i = 0; i < n; i = i + 1) {
let a = ghost_point(i);
let b = ghost_point(j);
if (a.y > p.y) != (b.y > p.y) {
let x = a.x + (p.y - a.y) / (b.y - a.y) * (b.x - a.x);
if p.x < x {
inside = !inside;
}
}
j = i;
}
return inside;
}
@fragment
fn fragment(in: VertexOutput, @builtin(front_facing) is_front: bool) -> FragmentOutput {
var pbr_input = pbr_input_from_standard_material(in, is_front);
// The vertex stage hands over this point's height above its building's own
// base as the world normal's length (`building_vertex.wesl::height_code`).
// Taken off here, before anything lights with it: bevy keeps
// `world_normal` unnormalised and offsets the shadow lookup along it.
let height_code = length(in.world_normal);
pbr_input.world_normal = normalize(pbr_input.world_normal);
// A unit normal is a mesh that carries no base (the OSM lane, a mesh built
// before the base was packed), which keeps measuring from the world's zero.
let knows_base = height_code > 1.5;
let n = normalize(in.world_normal);
// `abs`, because a wall's normal may point either way and back faces are
// culled by the material rather than by this test.
//
// The second half is defensive rather than a fix for anything observed. A
// triangle soup generalised from real survey data does contain degenerate
// triangles; their normal is zero, `normalize` of that is NaN, and every
// comparison against a NaN is false — so such a fragment would slip past this
// test into a garbage tangent. It was *suspected* of drawing the thin vertical
// streaks on the gable walls in the 2026-08-26 night captures and it is not
// the cause of those (see `blur` below); the guard is kept because the failure
// mode is real and silent, not because it fixed that.
let horiz_len = length(vec2<f32>(n.x, n.z));
let is_wall = abs(n.y) < WALL_NORMAL_Y && horiz_len > 1e-3;
// The ghosted building loses its walls, and in a cutaway (y = 1, which is
// every ghost since `systems::cutaway`) its roof as well. Keeping the roof
// was right while the inside was one empty room with a glass box round it —
// without the roof that was a hole in the street with a floor in it. Now the
// inside is every storey up to the one being looked at, with its walls, and
// a follow camera above and behind the avatar can only see into it from
// above: with the roof on it was looking at the underside of the roof.
// `is_wall` is already the right question and is already answered.
//
// Only in the cutaway variant (`BuildingKey`): a shader holding a `discard`
// loses early depth testing on every draw, taken or not, so the shells
// compile without one while nobody is indoors.
@if(BUILDING_CUTAWAY)
if (is_wall || facade.ghost.y > 0.5) && in_ghost(in.world_position.xz) {
discard;
}
// Computed **outside** the branch on purpose: `fwidth` below is a derivative,
// and a derivative taken in non-uniform control flow is undefined — naga
// rejects some of those outright and the rest read neighbouring fragments
// that took the other path. The cost is a few ALU ops on roof fragments.
//
// `max` rather than the raw length, so a degenerate normal divides by the
// floor instead of producing an infinity that then propagates into `fwidth`.
let horiz = vec2<f32>(n.x, n.z) / max(horiz_len, 1e-3);
let tangent = vec2<f32>(-horiz.y, horiz.x);
let u = dot(in.world_position.xz, tangent);
// Up the wall from its own foot, so the storeys, the plinth, the damp line
// and the eaves sit where they do on this building rather than on the
// world's zero — which was its foot only with the terrain layer off.
let v = select(in.world_position.y, (height_code - 4.0) * 64.0, knows_base);
// What the vertex colour's alpha says about this building — its night
// occupancy, a shop, its era and its eaves (`building_windows::Facade`
// packs it; the tests there pin the layout). Alpha 1.0 is code 0, which is
// "nothing known", and every branch below falls back to the old look on it.
// The 1/32 is the interpolation margin `building_windows::Facade` describes.
let packed = pbr_input.material.base_color.a;
let code_f = max(floor((packed + 1.0 / 32.0) / 4.0), 0.0);
let occupancy_packed = max(packed - 4.0 * code_f, 0.0);
// 3 is vacant and 3.5 condemned, past anything a kept building is lit by.
let neglect = select(0.0, select(1.0, 2.0, occupancy_packed > 3.25), occupancy_packed > 2.75);
let neglected = neglect > 0.5;
let occupancy_raw = select(occupancy_packed, 0.0, neglected);
let code = u32(code_f);
let eave = f32((code >> 4u) & 511u) * 0.2;
// Era 5–7 is era 1 built before 1880, 1800 and 1650 (`building_windows::antiquity`):
// everything else here asks only for the era.
let era_code = (code >> 1u) & 7u;
let era = select(era_code, 1u, era_code > 4u);
let antiquity = select(0u, era_code - 4u, era_code > 4u);
wall_material = (code >> 13u) & 3u;
// A wall on a narrow street (`systems::graffiti`). Walls only, and only a
// laid-out face: a roof's covering uses this bit too (`pack_roof`).
let sprayed_bit = ((code >> 15u) & 1u) == 1u;
// A shop front needs a ground floor to stand in.
let shop = (code & 1u) == 1u && eave > SHOP_FLOOR_M + 2.0;
// The floors. Known eaves are divided into whole storeys of about three
// metres — the rule `interiors` builds its storeys by — and a shop's ground
// floor is its own taller storey under them. Unknown keeps the one pitch
// every building used to share.
var ground_h = facade.lattice.y;
var storey_h = facade.lattice.y;
if eave > 0.0 {
let rest = select(eave, eave - SHOP_FLOOR_M, shop);
storey_h = rest / max(1.0, round(rest / STOREY_M));
ground_h = select(storey_h, SHOP_FLOOR_M, shop);
}
// The wall's measured window lattice, where the city model was read
// (`building_windows::WallFace::windows`): a pane's size, the storey and
// the height of the lowest row seen. Its storeys are laid so a row's
// centre is where that row was; the ground floor is what lies under the
// lowest of them, a storey or more as fits.
@if(VERTEX_UVS_B)
let win_b = in.uv_b;
@else
let win_b = vec2<f32>(0.0, 0.0);
let measured = win_b.x > 0.5;
var measured_pane = vec2<f32>(0.0, 0.0);
// The height above which no row was seen: a parapet, an attic, a sign.
var measured_top = 1.0e9;
if measured {
let wx = u32(round(win_b.x));
let wy = u32(round(win_b.y));
measured_pane = vec2<f32>(f32(wx % 512u), f32(wx / 512u)) / 100.0;
let st = f32(wy % 512u) / 100.0;
let z0 = f32((wy / 512u) % 2048u) / 100.0;
let rows = wy / 1048576u;
if st >= 2.0 {
storey_h = st;
}
if rows > 0u {
measured_top = z0 + (f32(rows) - 0.5) * storey_h;
}
// The floor line under the lowest row seen, then down a storey at a
// time while a whole storey still fits under it.
var g = z0 - 0.5 * storey_h;
g = g - max(floor((g - 2.0) / storey_h), 0.0) * storey_h;
if g < 1.5 {
g = g + storey_h;
}
ground_h = g;
}
// A continuous floor coordinate: 0–1 is the ground floor, then one a storey.
// Measured from y = 0, which is the building's base with the terrain layer
// off — the limit the module note describes.
let floor_f = select(v / ground_h, 1.0 + (v - ground_h) / storey_h, v >= ground_h);
// Index of the window this fragment is in, and where inside it we are.
// The wall face's own layout, where `lod22` gave one (`WallFaces`): UV0.x
// metres along the face from its corner, UV0.y an integer carrying the bay
// width that fits the face and where its door is. Negative UV0.y is a
// doorway part, 0 is no layout (the OSM lane) and keeps the world lattice.
@if(VERTEX_UVS_A)
let face_uv = in.uv;
@else
let face_uv = vec2<f32>(0.0, 0.0);
let door_part = face_uv.y < -0.25;
let laid_out = face_uv.y > 0.5;
let face_code = u32(round(max(face_uv.y, 0.0)));
var bay = facade.lattice.x;
var along = u;
var door_u = -1000.0;
if laid_out {
bay = f32(face_code % 512u) / 100.0;
along = face_uv.x;
let door_step = face_code / 512u;
if door_step > 0u {
door_u = f32(door_step) * DOOR_STEP_M;
}
}
let sprayed = sprayed_bit && laid_out && is_wall;
// A face too short for a window says so with a 1 cm bay.
let windowless = laid_out && bay < 0.5;
// An imported model's walls carry the windowless marker with no layout
// behind it (`WINDOWLESS_UV`: x is 0 on every vertex), so its cladding
// slabs run along the world lattice instead of collapsing into bands.
if windowless && face_uv.x == 0.0 {
along = u;
}
let cell = vec2<f32>(along / max(bay, 0.5), floor_f);
// How much of the lattice one pixel spans. Where that reaches a whole window
// the grid is below the sampling rate and point-sampling it aliases — into
// vertical streaks on a wall seen edge-on, and into a crawling sparkle at
// distance.
let px = max(fwidth(cell.x), fwidth(cell.y));
// The roofs' lattices, and how much of each one a pixel spans — outside the
// branch for the same reason as `px`. `horiz` points down a pitched roof's
// slope (its normal leans that way); dividing by the normal's vertical part
// turns a horizontal distance into one measured along the roof.
let down = dot(in.world_position.xz, horiz) / max(abs(n.y), 0.3);
let tile = vec2<f32>(u / TILE_WIDTH_M, down / TILE_COURSE_M);
let tile_px = max(fwidth(tile.x), fwidth(tile.y));
let grain = in.world_position.xz / GRAIN_M;
let grain_px = max(fwidth(grain.x), fwidth(grain.y));
let blotch_uv = in.world_position.xz / PATCH_M;
let blotch_px = max(fwidth(blotch_uv.x), fwidth(blotch_uv.y));
// Laid by hand, not printed: a course rises and falls a few millimetres
// over a metre or so, and each course's bond sits a little off the half
// brick it was meant to be. Shifting the lattice itself, here, moves every
// reader of it — the colour, the relief, the damp in the joints — alike.
let course_v = v / BRICK_COURSE_M
+ 0.1 * (value_noise(vec2<f32>(u / 0.9, v / 2.0) + vec2<f32>(61.0, 3.0)) - 0.5);
let brick = vec2<f32>(
u / BRICK_LENGTH_M + 0.12 * (hash_wrapped(vec2<f32>(floor(course_v), 7.0)) - 0.5),
course_v,
);
// On the geometric mean of the two axes, not the larger: a wall seen down
// a street is squeezed along it and not up it, and fading on the squeezed
// axis alone took the brick off every wall but the one beside you — the
// street's paving had the same fault (`ground_detail.wesl`, 2026-10-29).
let brick_px = sqrt(fwidth(brick.x) * fwidth(brick.y));
// Metres a pixel spans on the wall, on its sharper axis, for the variation
// a few metres across that holds long after a course has gone.
let wall_px = min(fwidth(u), fwidth(v));
// Which way `along` runs against `u` (and so against the wall's tangent):
// a laid-out face counts from its own corner, which may be either end. A
// derivative, so taken here in uniform control flow.
let along_sign = select(
-1.0,
1.0,
dot(vec2<f32>(dpdx(along), dpdy(along)), vec2<f32>(dpdx(u), dpdy(u))) >= 0.0,
);
// Graffiti's frame (`graffiti`): metres along the wall left to right as
// you face it, the world direction that runs, and metres a pixel spans.
// Derivatives, so here.
let spray_right = select(-1.0, 1.0, dpdx(along) >= 0.0);
let spray_x = along * spray_right;
let spray_dir = tangent * along_sign * spray_right;
let spray_px = max(fwidth(spray_x), fwidth(v));
let spray_dx = vec2<f32>(dpdx(spray_x), dpdx(v));
let spray_dy = vec2<f32>(dpdy(spray_x), dpdy(v));
// A pale block's cladding slabs, laid from the face's own corner.
let slab = vec2<f32>(along, v) / SLAB_M;
let slab_px = sqrt(fwidth(slab.x) * fwidth(slab.y));
let gutter_px = fwidth(v) / GUTTER_M;
let weave = vec2<f32>(u, v) / NET_MESH_M;
let weave_px = max(fwidth(weave.x), fwidth(weave.y));
let wire = vec2<f32>(u / 0.075, v / 0.225);
let wire_px = max(fwidth(wire.x), fwidth(wire.y));
if door_part {
door_surface(&pbr_input, face_uv);
// A scaffold's debris netting (`scaffolding`, UV0.x = 2), a site
// fence's mesh panel (UV0.x = 3) and a model's clear glass (UV0.x = 4,
// `lod22::CLEAR_GLASS_UV`): all are see-through.
if face_uv.y > -0.75 && face_uv.x > 1.5 {
var cover = 1.0;
if face_uv.x > 3.5 {
cover = clear_glass(&pbr_input);
} else if face_uv.x > 2.5 {
cover = fence_mesh(&pbr_input, wire, wire_px);
} else {
cover = netting(&pbr_input, u, v, weave, weave_px);
}
// The netting is meshed onto the screen-door material, which always
// compiles the cutaway variant (`lod22`'s inside pieces); a panel
// somehow drawn on a shell material is opaque rather than wrong.
@if(BUILDING_CUTAWAY)
if see_through(cover, in.position.xy) {
discard;
}
}
} else if is_wall {
let idx = floor(cell);
let f = cell - idx;
// The wall itself first: what it is built of, then the eaves' shadow,
// then — below — the windows cut into it. The glass is drawn over the
// colour as it was, not over the brick: a pane is not bricked.
let plain = pbr_input.material.base_color.rgb;
pbr_input.material.base_color = vec4<f32>(
wall_finish_clad(
pbr_input.material.base_color.rgb, u, v, floor_f, brick, brick_px, px,
along, era, eave, slab, slab_px,
)
* gutter_shade(v, eave, gutter_px)
* wall_drift(u, v, wall_px),
pbr_input.material.base_color.a,
);
pbr_input.material.base_color = vec4<f32>(
base_and_cornice(pbr_input.material.base_color.rgb, v, eave, era, gutter_px * GUTTER_M),
pbr_input.material.base_color.a,
);
// The block this wall stands in, for the draws that belong to a building
// rather than to a window (`liveliness` below, and the weathering here).
let bidx = floor(in.world_position.xz / facade.block.x);
let bkey = bidx - floor(bidx / facade.block.y) * facade.block.y;
// A kept wall weathers by its age as well: a damp line and rain streaks,
// at the era's strength, more or less by the block's own draw.
let weather = era_weathering(era, antiquity) * mix(0.5, 1.3, hash2(bkey + vec2<f32>(53.0, 17.0)));
if neglected {
pbr_input.material.base_color = vec4<f32>(
grimed(pbr_input.material.base_color.rgb, along, v, neglect, 1.0 - clamp(px * 0.8, 0.0, 1.0)),
pbr_input.material.base_color.a,
);
} else {
pbr_input.material.base_color = vec4<f32>(
weathered(
pbr_input.material.base_color.rgb,
along,
v,
weather,
1.0 - clamp(px * 0.8, 0.0, 1.0),
brick,
wall_kinds(plain).x * (1.0 - clamp(brick_px * 3.0 - 0.3, 0.0, 1.0)),
),
pbr_input.material.base_color.a,
);
}
// …and a wall from before 1880 has grown green on it, by the century.
// Its own draw, narrower than the weather's, so that no 1650 wall comes
// out cleaner than a 1850 one beside it.
let moss = f32(antiquity) / 3.0 * mix(0.75, 1.15, hash2(bkey + vec2<f32>(9.0, 87.0)));
pbr_input.material.base_color = vec4<f32>(
mossed(
pbr_input.material.base_color.rgb,
along,
v,
moss,
clamp(-pbr_input.world_normal.z, 0.0, 1.0),
1.0 - clamp(px * 0.8, 0.0, 1.0),
),
pbr_input.material.base_color.a,
);
// Graffiti on a wall along a narrow street, under the relief so the
// brick shows through the paint, and kept off the door.
if sprayed && abs(along - door_u) > DOOR_CLEAR_M {
let wall_now = pbr_input.material.base_color.rgb;
let paint = graffiti(
in.world_position.xz,
spray_dir,
spray_x,
v,
spray_px,
spray_dx,
spray_dy,
plain,
);
// Paint lies on the brick, not in front of it: the wall's own
// light and dark — its bricks, its joints, its damp — come
// through, as a share of how far it strays from its plain tone.
let lum = vec3<f32>(0.2126, 0.7152, 0.0722);
let showing = clamp(dot(wall_now, lum) / max(dot(plain, lum), 1e-3), 0.55, 1.3);
// A tag is gone long before a wall is; a piece holds on as colour.
let keep = 1.0 - smoothstep(0.08, 0.3, spray_px);
pbr_input.material.base_color = vec4<f32>(
mix(wall_now, paint.rgb * mix(1.0, showing, 0.75), paint.a * keep),
pbr_input.material.base_color.a,
);
}
// What the wall is built of, in relief — `gutter_px * GUTTER_M` is
// `fwidth(v)`, metres a pixel up the wall.
var relief = wall_relief(wall_kinds(plain), u, v, floor_f, brick, brick_px, gutter_px * GUTTER_M);
// A stone slab's edges face into its open joint, as a sawn panel's do.
if era >= 4u && is_cladding(era, eave) {
let sf = fract(slab);
let sd = (sf - vec2<f32>(0.5, 0.5)) * SLAB_M;
let edge = SLAB_M * 0.5 - abs(sd);
let into = vec2<f32>(1.0 - smoothstep(0.004, 0.012, edge.x), 1.0 - smoothstep(0.004, 0.012, edge.y));
let k = wall_kinds(plain).z * (1.0 - clamp(slab_px * 5.0 - 0.3, 0.0, 1.0));
relief += vec2<f32>(sign(sd.x) * into.x, sign(sd.y) * into.y) * 0.5 * k;
}
// The pane, sized by the building's era and centred in its cell. Written
// as a half-width test either side of the middle so the window keeps its
// real size as the pitch changes.
let on_ground = idx.y < 0.5;
let floor_h = select(storey_h, ground_h, on_ground);
var pane_m = pane_for_era(era, facade.lattice.zw);
if measured {
pane_m = measured_pane;
}
var centre = vec2<f32>(0.5, 0.5);
if shop && on_ground {
pane_m = SHOP_PANE_M;
centre.y = 0.42;
// The fascia over the shop window: a dark band for the sign.
let fascia = f32(f.y > 0.82 && f.y < 0.95) * (1.0 - clamp(px * 1.6 - 0.35, 0.0, 1.0));
pbr_input.material.base_color = vec4<f32>(
pbr_input.material.base_color.rgb * mix(1.0, 0.45, fascia),
pbr_input.material.base_color.a,
);
}
let half = min(vec2<f32>(pane_m.x / max(bay, 0.5), pane_m.y / floor_h) * 0.5, vec2<f32>(0.47, 0.45));
// No window where the door is: the ground-floor bays whose pane would
// come within DOOR_CLEAR_M of the door's centre.
let clear_of_door = !(on_ground
&& abs((idx.x + 0.5) * bay - door_u) < pane_m.x * 0.5 + DOOR_CLEAR_M);
let d = abs(f - centre);
let inside = f32(d.x < half.x && d.y < half.y);
// Modulo *before* hashing, so a floating-origin jump — which moves `idx`
// by an exact multiple of HASH_PERIOD — cannot change the answer.
let key = idx - floor(idx / HASH_PERIOD) * HASH_PERIOD;
let r = hash2(key);
// How lively this block is. A second, coarser hash on roughly one
// building's footprint — without it every facade in the city carries the
// same fraction of lit windows, which is what made the first night
// capture read as decoration rather than as a city. Real streets have
// whole dark buildings standing between the lit ones.
//
// `bkey` (above) is taken on the horizontal position only, so all four walls of a building
// agree, and moduloed on its own period for the floating-origin reason
// the window lattice is.
let liveliness = hash2(bkey + vec2<f32>(37.0, 91.0));
// …scaled by what the building is actually *for*. The vertex colour's
// alpha carries `osm_buildings::night_lit_scale` of the BAG's
// `gebruiksdoel` for this building — free, because the material is opaque
// and that channel was a constant 1.0 (the same spare-channel trick
// `globe_ocean` plays with its sea mask, and read here for the same
// reason: before `alpha_discard`, which flattens alpha on an opaque
// material). 1.0 means "nothing known", so a building outside the BAG
// coverage — or a whole city outside the Netherlands — keeps the purely
// hashed liveliness above and looks exactly as it did.
//
// Clamped rather than trusted: the scale is authored above 1.0 for homes,
// and a lit fraction past 1 would light every window on a facade.
let occupancy = clamp(occupancy_raw, 0.0, 2.0);
let lit_fraction = clamp(mix(facade.block.z, facade.block.w, liveliness) * occupancy, 0.0, 1.0);
// Lit if this window's own draw came in under the block's fraction, then
// ramped so the ones that are on are not all on equally.
var brightness = 0.0;
if r < lit_fraction {
brightness = mix(0.45, 1.25, r / max(lit_fraction, 1e-4));
}
// Nothing below the first sill: a fragment that low is a plinth, a garden
// wall or a shed side, and lighting those reads as a mistake rather than
// as a window.
//
// Where the eaves are known the building is known to be a building, and
// its ground floor has windows like any other; a shop's is the window.
let sill = select(facade.glow.w, GROUND_SILL_M, eave >= 4.0);
// No window the eaves cut through: a storey that only partly fits
// under them is a parapet or an attic, and a pane drawn there reads as
// broken off by the roof (seen on the Grote Markt, 2026-11-19).
let floor_bottom = select(ground_h + (idx.y - 1.0) * storey_h, 0.0, on_ground);
let pane_top = floor_bottom + (centre.y + half.y) * floor_h;
let under_eaves = eave <= 0.0 || pane_top <= eave - EAVE_CLEAR_M;
let tall_enough = select(f32(v > sill), 1.0, shop && on_ground)
* f32(clear_of_door && !windowless && under_eaves)
* f32(v < measured_top);
// Below a pixel, converge on the pattern's own **mean** rather than fading
// it out. Fading out is the usual answer and it is the wrong one here: the
// windows that are sub-pixel are the whole lit horizon, and removing them
// leaves the far half of the city black. Converging keeps the light and
// loses only the grid, which is what a real city does at that distance.
//
// It is worth being clear what this does *not* do, because two captures
// were spent on it. The thin vertical streaks on a gable wall seen nearly
// edge-on are **not** aliasing and this does not remove them: at 40 m a
// window is ~30 px tall, and at ~10° incidence its 1.35 m width projects
// to ~4 px, so a 4×30 px sliver is the correct projection of a real
// window and not an artefact. This exists for genuine sub-pixel range,
// where it stops the grid crawling as the camera moves — which no still
// capture can show.
let blur = clamp(px * 1.6 - 0.35, 0.0, 1.0);
// Fraction of the wall the panes cover — the mean of `inside`.
let pane_area = 4.0 * half.x * half.y;
let mean_bright = lit_fraction * 0.85;
let pane = mix(inside, pane_area, blur) * tall_enough;
let emissive = mix(inside * brightness, pane_area * mean_bright, blur) * tall_enough;
// By day a window is dark glass in the wall around it — the wall's own
// colour, darkened, rather than its brick darkened, which read as a
// bricked-up opening once the walls had a texture. The glass roughness
// and reflectance below are what put the sky in it.
// The window itself, up close: frame, glazing bars, the shadow of the
// reveal, a sill under it and a lintel over it. Faded out with the
// same `blur`, so at range only the glass coverage above is left.
let metres = vec2<f32>(max(bay, 0.5), floor_h);
let pm = (f - centre) * metres;
let hs = half * metres;
let detail = tall_enough * (1.0 - blur);
let shop_front = shop && on_ground;
// **Depth.** The view, in the wall's frame: along the face (the way
// `pm.x` runs), up, and into the wall. The glass is `REVEAL_M` back
// from the face, so the point of glass this fragment shows is where
// the view meets that plane (`pg`) — the frame and its bars move
// against the opening as you walk past — and where that falls outside
// the opening, what is seen is the reveal itself: its soffit, its sides
// or the sill. Then the room, from the glass on.
let n_raw = vec3<f32>(horiz.x, 0.0, horiz.y);
let n_out = select(-n_raw, n_raw, dot(pbr_input.V, n_raw) >= 0.0);
let into = -pbr_input.V;
let t3w = vec3<f32>(tangent.x, 0.0, tangent.y) * along_sign;
let dview = vec3<f32>(dot(into, t3w), into.y, max(dot(into, -n_out), 0.05));
let pg = mix(pm, pm + dview.xy * (REVEAL_M / dview.z), detail);
let over = abs(pg) - hs;
let reveal = f32((over.x > 0.0 || over.y > 0.0) && inside > 0.5) * detail;
// Which face of the reveal: the one the ray left the opening through
// first, i.e. with the larger overshoot for its rate of travel.
let by_x = over.x / max(abs(dview.x), 1e-3) > over.y / max(abs(dview.y), 1e-3);
let reveal_col = select(
select(plain * 0.45, SILL_COLOUR, pg.y < 0.0),
plain * 0.7,
by_x,
);
let look = window_look(pg, hs, era, shop_front);
let look_wall = window_look(pm, hs, era, shop_front);
let frame_col = frame_colour(era);
let room = room_behind(
pg,
dview,
metres.x,
-centre.y * floor_h,
(1.0 - centre.y) * floor_h,
r,
);
// Behind the glass. Not the wall's colour darkened, which tinted every
// pane in a brick street brick-red: a room, seen through glass, with
// what hangs in the window — curtains, a blind — or, in a shop, the
// lit shop. The sky it reflects is the lighting's job
// (`GLASS_REFLECTANCE`), and it only shows over something this dark.
var interior = select(
room_through_glass(pg, hs, r, mix(GLASS_BASE, room, detail)),
shop_through_glass(pg, hs, along),
shop_front,
) * mix(look.z, 1.0, detail * 0.5);
// A neglected building: an empty room behind the glass, a whitewashed
// shop window, and boards over the ground floor and over half the windows
// above it — all of them once it is waiting to come down.
let boards = neglected && !shop_front
&& (on_ground || fract(r * 17.3) < select(0.5, 1.0, neglect > 1.5));
let board_k = f32(boards);
if neglected {
interior = select(GLASS_BASE * 0.7, whitewashed(pm, along), shop_front) * look.z;
}
var wall_col = pbr_input.material.base_color.rgb;
// Sill and lintel sit on the wall around the opening.
wall_col = mix(wall_col, SILL_COLOUR, look_wall.w * detail);
wall_col = mix(wall_col, wall_col * lintel_tone(era), lintel(pm, hs, era) * detail);
var glazed = mix(interior, frame_col, look.y);
// At range the pane is only its coverage, the way it always was.
glazed = mix(glazed, mix(GLASS_BASE, plain * facade.glow.z, 0.3), blur);
// Boards cover the frame and the glass alike.
glazed = mix(glazed, mix(boarded(pm, hs, r), BOARD_MEAN, blur), board_k);
// …and where the view falls on the reveal, that is what is seen.
glazed = mix(glazed, reveal_col, reveal * (1.0 - board_k));
// The glass's share of the opening: none where it is boarded, or where
// the reveal stands in front of it.
let glass = pane * (1.0 - board_k) * (1.0 - reveal);
// A frame's inner edge is bevelled down to the glass: tilt the normal
// there so it catches the light like a moulding rather than a stripe.
let bevel = frame_bevel(pg, hs, era, shop_front) * detail * glass;
// No two panes are glazed in quite one plane: each sits a degree or two
// off the wall, the way a city's windows each catch a different piece
// of sky rather than one sheet of it across a whole elevation. Gone with
// the window detail, since a per-pane glint below a pixel crawls.
let skew = (vec2<f32>(fract(r * 3.71), fract(r * 9.13)) - vec2<f32>(0.5, 0.5)) * 0.07;
// The wall's own relief stops at the opening: a pane is not bricked.
let tilt = bevel + relief * (1.0 - pane) + skew * glass * (1.0 - blur);
let t3 = vec3<f32>(tangent.x, 0.0, tangent.y);
pbr_input.N = normalize(pbr_input.N + t3 * tilt.x + vec3<f32>(0.0, tilt.y, 0.0));
pbr_input.material.base_color = vec4<f32>(
mix(wall_col, glazed, pane),
pbr_input.material.base_color.a,
);
// …and at night it is lit from inside. The emissive is added rather than
// replacing the surface, so the wall keeps taking the moon and the street.
pbr_input.material.emissive += vec4<f32>(
facade.lit.rgb * (emissive * (1.0 - reveal) * facade.glow.x * facade.glow.y),
0.0,
);
// Glass is not plaster, and until this every square metre of every
// building in the country carried one roughness. That is the flatness you
// see on a facade at any hour: a window took the sun exactly as dully as
// the wall around it, so a street of windows had no highlights in it at
// all and the whole elevation read as one printed sheet.
//
// `pane` and not `inside`, deliberately — `pane` is the same
// distance-blurred coverage the colour uses, so at range the windows
// converge on the wall's own finish instead of leaving a grid of glints
// sparkling across the far half of the city. That is the identical
// argument the `blur` comment above makes for the emissive, and getting
// it wrong here would undo it there.
//
// Reflectance rises with it: glass reflects appreciably more at normal
// incidence than a rendered or brick wall, and it is that — not the
// roughness alone — that puts the sky into the upper floors of a building
// seen from across a square.
// The *wall* between the windows is not one finish either, and until this
// it was: one `perceptual_roughness: 0.95` on every square metre of every
// building in the country, so a whole street took the sun identically and
// the elevations read as printed card. Brick, render, stone panels and
// painted plaster are not the same material and do not need to be told
// apart by name to stop looking like one sheet — they need the highlight
// to break up at facade scale.
//
// It reuses `r`, the lattice cell's own draw, rather than hashing again:
// this is the largest geometry lane in the app and the question the whole
// module answers is "what can a facade have for free". One multiply and a
// `fract` decorrelate it from the lit-window decision it shares a hash
// with, which is enough for a roughness nobody is meant to read as a
// pattern. The cell is roughly a window bay, which is about the scale real
// facades change finish at — a panel, a bay, a stone course.
//
// Faded out by `blur` for the same reason the emissive and the glass
// roughness are: a per-cell value that survives below a pixel is a
// crawling sparkle across the far half of the city.
let finish = fract(r * 7.13) - 0.5;
pbr_input.material.perceptual_roughness = clamp(
pbr_input.material.perceptual_roughness + WALL_FINISH_RANGE * finish * (1.0 - blur),
0.0,
1.0,
);
// Mortar is as matte as anything gets; a fired brick face is not, and
// no two came out of the kiln with the same skin. Multiplied in, so it
// composes with what else writes the roughness.
pbr_input.material.perceptual_roughness *= brick_sheen(brick, brick_px, wall_kinds(plain).x);
pbr_input.material.perceptual_roughness = mix(
pbr_input.material.perceptual_roughness,
GLASS_ROUGHNESS,
glass,
);
pbr_input.material.reflectance = mix(
pbr_input.material.reflectance,
vec3<f32>(GLASS_REFLECTANCE),
glass,
);
let street = street_reflection(pbr_input.V, pbr_input.N, glass);
pbr_input.specular_occlusion *= street;
pbr_input.diffuse_occlusion *= street;
} else if n.y > 0.0 && !door_part {
var tone = 1.0;
// What atlas measured this face to be covered with
// (`building_windows::RoofCovering`), 0 when nothing was: a roof's
// vertices carry it where a wall's carry its eaves and material.
let roof_kind = (code >> 13u) & 15u;
if roof_kind == ROOF_SOLAR {
// Panels, whatever the slope: a frame of bare aluminium round each,
// and its cells in a faint grid.
let panel = roof_panels(in.world_position.xz, n.y, u, down);
let panel_px = tile_px * TILE_COURSE_M / SOLAR_PANEL_M.y;
let blur = clamp(panel_px * 2.5 - 0.3, 0.0, 1.0);
tone = mix(panel.x, SOLAR_MEAN, blur);
pbr_input.material.base_color = vec4<f32>(
mix(pbr_input.material.base_color.rgb, SOLAR_FRAME, panel.y * (1.0 - blur)),
pbr_input.material.base_color.a,
);
pbr_input.material.perceptual_roughness = 0.25;
pbr_input.material.reflectance = vec3<f32>(0.6);
} else if n.y < FLAT_ROOF_Y && roof_kind == ROOF_SLATE {
// Slates: small, flat, rectangular, each laid half over the joint
// below, with a thin dark gap and a sharp lap shadow.
let s = vec2<f32>(tile.x * TILE_WIDTH_M / SLATE_M.x, tile.y * TILE_COURSE_M / SLATE_M.y);
let s_px = tile_px * TILE_COURSE_M / SLATE_M.y;
let course = floor(s.y);
let across = s.x + 0.5 * course;
let f = fract(s.y);
let g = fract(across);
let lap = mix(0.55, 1.0, smoothstep(0.0, 0.12, f));
let gap = mix(0.6, 1.0, smoothstep(0.0, 0.04, min(g, 1.0 - g)));
let each = 0.92 + 0.16 * hash_wrapped(vec2<f32>(floor(across), course) + vec2<f32>(31.0, 5.0));
let blur = clamp(s_px * 2.5 - 0.3, 0.0, 1.0);
tone = mix(lap * gap * each, SLATE_MEAN, blur);
pbr_input.material.perceptual_roughness *= 0.9;
} else if n.y < FLAT_ROOF_Y && roof_kind == ROOF_METAL {
// Standing seams down the slope: sheet between, a raised bead at
// each seam catching the light.
let seam_x = tile.x * TILE_WIDTH_M / SEAM_M;
let s_px = tile_px * TILE_WIDTH_M / SEAM_M;
let d = abs(fract(seam_x + 0.5) - 0.5) * SEAM_M;
let bead = 1.0 - smoothstep(0.0, 0.025, d);
let blur = clamp(s_px * 2.5 - 0.3, 0.0, 1.0);
tone = mix(1.0 + 0.25 * bead, 1.0 + 0.25 * 0.05, blur);
pbr_input.material.perceptual_roughness = 0.35;
pbr_input.material.metallic = 0.6;
} else if n.y < FLAT_ROOF_Y {
// A pitched roof: courses of tiles, each course half a tile over
// from the one above. `f` runs down the slope through one course,
// so f ≈ 0 is where the course above laps over it and throws its
// shadow; `g` runs across one tile, and its edges are the gaps.
let course = floor(tile.y);
let across = tile.x + 0.5 * course;
let col = floor(across);
let f = fract(tile.y);
let g = fract(across);
let lap = mix(0.3, 1.0, smoothstep(0.0, 0.25, f));
let gap = mix(0.75, 1.0, smoothstep(0.0, 0.08, min(g, 1.0 - g)));
// Every tile is its own firing: a few percent either way.
let fired = 0.9 + 0.2 * hash_wrapped(vec2<f32>(col, course) + vec2<f32>(13.0, 7.0));
let blur = clamp(tile_px * 2.5 - 0.3, 0.0, 1.0);
tone = mix(lap * gap * fired, TILE_MEAN, blur);
// …and the courses stand proud of the plane: a tile's face is a
// little flatter than the roof it lies on, so it tilts the normal
// back up the slope, most at the course's lower edge.
let down3 = normalize(vec3<f32>(horiz.x, 0.0, horiz.y));
let tilt = 0.35 * (smoothstep(0.0, 0.25, f) - 0.5) * (1.0 - blur);
// A Dutch pantile is an S across its width — one roll and one
// trough a tile — so across the courses each tile's face turns from
// the light and back. The slope of a sine over the tile's width.
let across3 = vec3<f32>(tangent.x, 0.0, tangent.y);
// A concrete tile is flatter across than a clay pantile; a glazed
// one is the same shape with a skin that shines.
let flatter = select(1.0, 0.4, roof_kind == ROOF_CONCRETE);
let wave = 0.25 * flatter * cos(6.2831853 * g) * (1.0 - blur);
pbr_input.N = normalize(pbr_input.N - down3 * tilt - across3 * wave);
if roof_kind == ROOF_GLAZED {
pbr_input.material.perceptual_roughness *= 0.75;
}
} else {
// A flat roof: its finish follows from its measured tone, which is
// what atlas's material classes are cut from too — dark is bitumen,
// mid is gravel ballast, pale is membrane, and green is sedum.
let c = pbr_input.material.base_color.rgb;
let luma = dot(c, vec3<f32>(0.2126, 0.7152, 0.0722));
let blur = clamp(grain_px * 1.6 - 0.35, 0.0, 1.0);
let fine = value_noise(grain) - 0.5;
let coarse = value_noise(grain * 0.07) - 0.5;
// Blotches a few metres across: the one scale of a flat roof that
// still reads from a few hundred metres up, so it keeps its own blur.
let blotch = (value_noise(blotch_uv) - 0.5 + 0.5 * (value_noise(blotch_uv * 2.0) - 0.5))
* (1.0 - clamp(blotch_px * 1.6 - 0.35, 0.0, 1.0));
// The seams between rolls, along whichever world axis — a flat roof
// records no direction for them. A thin raised bead, blurred out
// with the grain.
let seam_d = abs(fract(in.world_position.x / ROLL_M + 0.5) - 0.5) * ROLL_M;
let seam = 1.0 - smoothstep(0.0, 0.03, seam_d);
let dark = 1.0 - smoothstep(0.035, 0.07, luma);
let pale = smoothstep(0.25, 0.4, luma);
let green = smoothstep(1.05, 1.25, c.g / max(max(c.r, c.b), 1e-4));
let gravel = (1.0 - dark) * (1.0 - pale) * (1.0 - green);
let finish = gravel * 0.45 * fine
+ green * (0.6 * fine + 0.3 * coarse)
+ dark * (0.12 * seam + 0.1 * coarse)
+ pale * (0.08 * seam - 0.1 * coarse);
tone = 1.0 + finish * (1.0 - blur)
+ blotch * (0.35 * gravel + 0.3 * dark - 0.25 * pale + 0.3 * green);
// A membrane and wet-looking bitumen carry a sheen; ballast does not.
pbr_input.material.perceptual_roughness = mix(
pbr_input.material.perceptual_roughness,
0.6,
max(dark, pale) * 0.5,
);
}
pbr_input.material.base_color = vec4<f32>(
pbr_input.material.base_color.rgb * tone,
pbr_input.material.base_color.a,
);
}
pbr_input.material.base_color = alpha_discard(pbr_input.material.flags, pbr_input.material.alpha_cutoff, pbr_input.material.base_color);
// The clouds' shadows: the sun alone, as on the ground beside the walls.
// Built field by field — a whole struct load from a bound buffer is what
// the Adreno 840 reads wrong (`cloud_field.wesl`'s `Deck`).
pbr_input.directional_shadow_factor *= cf::sun_shadow(
cf::Deck(
clouds.shape,
clouds.motion,
clouds.sun_dir,
clouds.sun_color,
clouds.sky_color,
clouds.place,
clouds.haze_color,
clouds.light,
),
in.world_position.xyz - view_bindings::view.world_position.xyz,
);
var out: FragmentOutput;
if (pbr_input.material.flags & STANDARD_MATERIAL_FLAGS_UNLIT_BIT) == 0u {
out.color = apply_pbr_lighting(pbr_input);
// The street's light thrown back up (`ground_bounce`): what fills a wall in
// shade. The atmosphere's environment map already holds it — the sunlit
// ground below its horizon — so only a run without one adds it.
@if(!ENVIRONMENT_MAP)
{
let diffuse = pbr_input.material.base_color.rgb * (1.0 - pbr_input.material.metallic);
out.color = vec4<f32>(
out.color.rgb + ground_bounce::radiance(diffuse * pbr_input.diffuse_occlusion, pbr_input.N),
out.color.a,
);
}
} else {
out.color = pbr_input.material.base_color;
}
out.color = main_pass_post_lighting_processing(pbr_input, out.color);
return out;
}