// Sub-metre ground: the half-metre tier's slope, applied to the normal. // // `systems::terrain`'s field is 3.125 m and the meshes that stand on it carry a // vertex every 6 m. That is what gives a street a *gradient*; it is not what gives // it a kerb. AHN measures the ground at half a metre and `terrain`'s fine tier // already fetches it, but half-metre geometry over a z14 cell is nine million quads // and is never going to be drawn. // // So the sub-metre half of the ground lives here, in the normal, exactly as // `water.wgsl`'s waves do and for the same reason: a 12 cm kerb, the camber of a // carriageway, the lip of a quay and a rut across a verge are all *shading*, not // silhouette. Nothing is displaced, nothing is subdivided, and the cost is one // texture fetch per ground fragment. // // # What the texture holds, and why it is a residual // // `terrain::encode_slope` writes the tier's slope **minus a box blur of itself at // `MAX_GROUND_EDGE_M`** — the slope the mesh underneath is already drawn with. Add // the raw slope instead and every gradient is applied twice, once in the vertices // and once here, and a hillside shades as if it were half again as steep. // // # It is in render space, and that is a moving target // // `detail.xy` is the box's centre in **render** space, not the anchored world frame: // under `big_space` the origin jumps a whole `WORLD_CELL_EDGE` as the camera crosses // a grid cell. `ground_detail::refresh_ground_detail` rewrites it whenever the box // re-centres or the origin moves — which is rare, twice a minute on foot at worst — // rather than every frame, because unlike `building_windows` there are hundreds of // ground materials and not one. // // # The map's water is lit as water, not painted // // Past the streamed ring the only water is the clipmap tile's own pixels, and the // painted colour there used to be matched to the lit surface inside the ring by // hand (`WATER_ALBEDO`, `palette::WATER`) — at one time of day, and drifting at // every other. The tile now carries **which texels are water** in its alpha // (`tile_loader::TilePayload::decode`, 255 = land, so an image source with no mask // reads as land and is untouched), and those texels take the surface's deep body // colour, its far-field roughness and its reflectance from `water_body` / // `water_sea`, then the same fresnel and stand-in sky out of `water_common`. Past // the ring the surface has already retired every wave it can resolve, so a flat // sheet with that roughness *is* what it would draw there. 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::STANDARD_MATERIAL_FLAGS_BASE_COLOR_TEXTURE_BIT, }; import bevy_pbr::render::mesh_view_bindings as view_bindings; import super::water_common::{ water_reflected, water_transmitted, sky_fallback, with_clouds, cloud_mirror_fade, }; import super::cloud_field as cf; import super::hex_grid; import super::map_light; import bevy_pbr::render::mesh_bindings::mesh; @if(BINDLESS) import bevy_render::bindless::{bindless_samplers_filtering, bindless_textures_2d}; // `ground_clouds` below is a `binding_array`; bevy's own bindless modules // enable the extension the same way. @if(BINDLESS) enable wgpu_binding_array; // The material's four vectors, one buffer (`ground_detail::GroundUniform`): // // detail — x, z: the detail box's centre in render-space XZ. z: 1 / its // half-extent, so `(world_position.xz - centre) * inv_half` is −1..1 across it. // w: strength, 0 when there is no tier to read (the layer off, outside the // Netherlands, still fetching), which is what makes this shader identical to the // plain one at no cost. // lod — the clipmap level's ring, for the cross-fade below: xy its centre in // render-space XZ, z 1 / its half-extent, w the mip bias to reach at its edge. // Zero on every ground surface that is not a clipmap tile, which is the off // switch. // water_body — the water surface's far field, for a clipmap tile's water texels // (`water::WaterSeaState::far_water`). rgb: the deep body colour — the surface's // albedo less what depth takes out of it, linear. w: its `reflectance`. // water_sea — x: perceptual roughness with every wave retired. y: how clear the // sky is (1 = cloudless), for the stand-in sky. z: 1 on a clipmap tile, 0 // everywhere else — the off switch, since the vector fills and the roads share // this material and carry no mask. w: 1 on a road ribbon (`RoadDepthExtension` // sets it), 0 on every ground surface. // far — the cross-fade to the painted map: x the camera distance it starts at, // y where it is complete, metres; z 1 when the painted twin (106) is bound. // tile — the clipmap tile's key for the hex grid (`hex_grid.wesl`): x, y, 2^z, and w // the layer's strength, 0 off and on every surface that is not a tile. struct GroundUniform { detail: vec4, lod: vec4, water_body: vec4, water_sea: vec4, far: vec4, tile: vec4, } // Bindless where the platform has it, which is what lets every clipmap tile — // each a material of its own, with its own basemap — share one bind group and be // batched into one draw (`ground_detail::GroundUniform`). The same resources // either way: the uniforms, the slope tier (`detail_slope`), the tile's own // basemap bound a second time for the cross-fade (`lod_basemap`), and the ground // cloud deck (`sky::CLOUD_FIELD_BUFFER`) the water texels mirror exactly as the // streamed surface inside the ring does. // One entry per material, field i naming bindless index 100 + i. @if(BINDLESS) struct GroundIndices { material: u32, // 100 slope: u32, // 101 slope_sampler: u32, // 102 basemap: u32, // 103 basemap_sampler: u32, // 104 clouds: u32, // 105 painted: u32, // 106 painted_sampler: u32, // 107 } @if(BINDLESS) { @group(constants::MATERIAL_BIND_GROUP) @binding(109) var ground_indices: array; @group(constants::MATERIAL_BIND_GROUP) @binding(110) var ground_array: array; @group(constants::MATERIAL_BIND_GROUP) @binding(111) var ground_clouds: binding_array; } @else { @group(constants::MATERIAL_BIND_GROUP) @binding(100) var ground_single: GroundUniform; @group(constants::MATERIAL_BIND_GROUP) @binding(101) var detail_slope: texture_2d; @group(constants::MATERIAL_BIND_GROUP) @binding(102) var detail_sampler: sampler; @group(constants::MATERIAL_BIND_GROUP) @binding(103) var lod_basemap: texture_2d; @group(constants::MATERIAL_BIND_GROUP) @binding(104) var lod_basemap_sampler: sampler; @group(constants::MATERIAL_BIND_GROUP) @binding(105) var clouds_single: cf::Deck; @group(constants::MATERIAL_BIND_GROUP) @binding(106) var painted_basemap: texture_2d; @group(constants::MATERIAL_BIND_GROUP) @binding(107) var painted_sampler: sampler; } // Metres of rise per metre of run a fully-saturated texel stands for. Must equal // `terrain::DETAIL_SLOPE_RANGE`. const SLOPE_RANGE: f32 = 1.0; // Where the box starts fading out, as a fraction of its half-extent. The tier's own // rim blend (`TerrainSnapshot::rim_weight`) does the same job for point queries; a // hard edge here would draw a square of texture around the camera. const RIM_START: f32 = 0.80; // Where the level-to-level cross-fade starts, as a fraction of the ring's // half-extent. 0.65 is a little over the outermost ring and a half of tiles at the // finest level's radius of 4 — wide enough that the softening reads as distance // rather than as a second edge just inside the first, narrow enough that most of the // ring keeps the sharpness it was rasterised at. const LOD_BLEND_START: f32 = 0.65; // ─── Paving ────────────────────────────────────────────────────────────────── // // What a surveyed surface is made of, drawn at the scale of its stones: the // pattern number arrives in the vertex alpha (`paving::pattern`), the stones // are laid on a lattice in world metres, and every pattern converges on its own // mean colour below a pixel. Nothing is stored. The lattices divide the // floating origin's 10,000 m jump (0.1, 0.125, 0.3125, 2.5 m all do), and the // per-stone hash repeats every 80 stones, which divides each jump's index step // — so the pavement is the same pavement either side of one. struct PavingLook { tone: vec3, tilt: vec2, roughness: f32, /// How much of this point is the joint and the worn edges either side of /// it, where the dirt collects; faded with the stones, 0 with no stones. joint: f32, /// 1 − the stones' blur, and 0 on a pattern with no stones — which is what /// says `lived_in` applies. crisp: f32, /// What fills the joints, as a colour of its own (rgb, linear) laid over the /// stone by `a` — sand on red brick is sand-coloured, not a darker red. fill: vec4, } /// How strongly asphalt's aggregate tilts the normal, for the surveyed asphalt /// and the road ribbons alike, so the two read as one material where they meet. const ASPHALT_RELIEF: f32 = 0.6; fn pav_hash(i: vec2) -> f32 { let w = i - floor(i / 80.0) * 80.0; return fract(sin(dot(w, vec2(127.1, 311.7))) * 43758.5453); } fn pav_noise(p: vec2) -> f32 { let i = floor(p); let f = fract(p); let u = f * f * (3.0 - 2.0 * f); return mix( mix(pav_hash(i), pav_hash(i + vec2(1.0, 0.0)), u.x), mix(pav_hash(i + vec2(0.0, 1.0)), pav_hash(i + vec2(1.0, 1.0)), u.x), u.y, ); } /// How far below a pixel a lattice of `unit` metres is, as 0 (crisp) to 1 (gone). fn pav_blur(unit: f32, metres_per_px: f32) -> f32 { return clamp(metres_per_px / unit * 3.0 - 0.3, 0.0, 1.0); } /// A loose or bound aggregate in relief: the slope of the same grain noise the /// tone draws at `unit` metres, as a tilt — so a low sun picks the stones out /// of a carriageway rather than only their colour. Gone below a pixel with the /// grain itself, since a slope that survives there is a sparkle. fn grain_tilt(p: vec2, unit: f32, strength: f32, metres_per_px: f32) -> vec2 { let k = 1.0 - pav_blur(unit, metres_per_px); if k <= 0.0 { return vec2(0.0, 0.0); } let q = p / unit; let h0 = pav_noise(q); let slope = vec2(pav_noise(q + vec2(0.25, 0.0)) - h0, pav_noise(q + vec2(0.0, 0.25)) - h0); return -slope * strength * k; } /// How much of a stone's edge bevel one point is on: 0 on the flat and in the /// joint, rising to 1 halfway down the chamfer — a parabola over `[a, a + w]` /// metres from the joint's centre, so the tilt has no step at either end. fn chamfer(d: f32, a: f32, w: f32) -> f32 { let t = clamp((d - a) / w, 0.0, 1.0); return 4.0 * t * (1.0 - t); } /// Bevelled edges are what a laid stone is up close: the face is flat and its /// edges roll down into the joint, so under a low sun every stone carries a lit /// edge and a dark one. Each axis tilts away from its nearer side, on a chamfer /// `BEVEL_M` wide starting at the joint's edge. const BEVEL_M: f32 = 0.012; /// A stone's face: darker in its joints, its own tint, a faintly domed top and /// bevelled edges — `f` is where in the stone this is (0–1 both ways), `size` /// the stone in metres along those two axes, `edge` how far from its nearest /// joint as a share of the stone, `joint` the joint's share. fn stone(id: f32, f: vec2, size: vec2, edge: f32, joint: f32, spread: f32, blur: f32, mean: f32) -> PavingLook { let crisp = 1.0 - blur; let in_joint = 1.0 - smoothstep(joint * 0.5, joint, edge); let tint = 1.0 + spread * (id - 0.5); let tone = mix(mix(tint, 0.62, in_joint), mean, blur); var look: PavingLook; look.tone = vec3(tone); // The bevel is a centimetre, so it goes well before the stone does: gone // once a stone is ~6 px, where it would only be a crawling edge. let bevel_k = clamp(1.0 - blur * 6.0, 0.0, 1.0); let half_joint = joint * 0.75 * min(size.x, size.y); let d = min(f, vec2(1.0, 1.0) - f) * size; let away = select(vec2(1.0, 1.0), vec2(-1.0, -1.0), f < vec2(0.5, 0.5)); let bevel = away * vec2(chamfer(d.x, half_joint, BEVEL_M), chamfer(d.y, half_joint, BEVEL_M)); // No two stones were laid alike: each sits a little off level, rocked by // the bed settling under it, and each has its edges worn by a different // amount — a crisp new one beside a rounded old one. let rock = (vec2(fract(id * 7.31), fract(id * 3.17)) - vec2(0.5, 0.5)) * 0.1; let worn = mix(0.35, 1.2, fract(id * 5.71)); look.tilt = ((f - vec2(0.5, 0.5)) * 0.12 + rock) * crisp * (1.0 - in_joint) + bevel * 0.8 * worn * bevel_k; look.roughness = 0.85; look.joint = (1.0 - smoothstep(joint * 0.5, joint * 2.5, edge)) * crisp; look.crisp = crisp; return look; } /// What a street does to its stones once they are laid — they are not as clean /// as the pattern draws them. Stains a metre across that darken more than they /// lighten, joints filled with sand and grit (and moss where nobody walks) that /// take the colour of what is in them and soften the edges they fill, and on a /// footpath the odd flattened disc of chewing gum. /// /// Every lattice here divides the floating origin's 10,000 m jump into a /// multiple of `pav_hash`'s 80 (1.0, 0.625, 1.25, 0.3125 and 0.25 m all do), so /// the dirt stays where it was across one. Each term fades on its own scale. /// Joint sand and the moss that takes over a quiet joint, linear. const SAND: vec3 = vec3(0.30, 0.26, 0.19); const MOSS: vec3 = vec3(0.10, 0.14, 0.05); fn lived_in(look_in: PavingLook, pattern: u32, p: vec2, metres_per_px: f32, fresh: f32) -> PavingLook { var look = look_in; // A relaid patch has not had the years to collect it yet. let stain_fade = (1.0 - pav_blur(1.0, metres_per_px)) * (1.0 - 0.75 * fresh); let blotch = smoothstep(0.55, 0.85, pav_noise(p + vec2(31.0, 17.0))); let mottle = pav_noise(p / 0.3125) - 0.5; // 0.15 is about the blotch's mean, so the stains darken a street by a few // percent before they fade into its measured colour. Dirt is grey-green. // Half what it was once the stones stopped fading three metres out: seen // across a whole square, a metre blotch at 35 % read as spilt oil. let grime = (0.15 * mottle - 0.18 * (blotch - 0.15)) * stain_fade; look.tone *= (1.0 + grime) * mix(vec3(1.0), vec3(0.94, 0.96, 0.92), blotch * stain_fade); let fill = pav_noise(p / 0.3125 + vec2(5.0, 71.0)); let moss = smoothstep(0.6, 0.8, pav_noise(p / 1.25 + vec2(13.0, 3.0))); let grit = mix(SAND, MOSS, moss) * (0.8 + 0.4 * fill); look.fill = vec4(grit, look.joint * mix(0.35, 0.85, fill) * (1.0 - 0.6 * fresh)); look.tilt *= mix(1.0, 0.35, smoothstep(0.45, 0.8, fill)); if pattern == 4u || pattern == 5u { let cell = floor(p / 0.25); let h = pav_hash(cell + vec2(91.0, 47.0)); if h > 0.96 { let centre = (cell + vec2(0.2, 0.2) + 0.6 * vec2(fract(h * 13.7), fract(h * 31.1))) * 0.25; let r = 0.008 + 0.01 * fract(h * 7.9); let gum = 1.0 - smoothstep(r * 0.7, r, length(p - centre)); look.tone *= 1.0 - 0.45 * gum * (1.0 - pav_blur(0.02, metres_per_px)); } } return look; } /// The street at the scale it is walked rather than the scale of its stones. /// Every stone above is varied and every stain is a metre across, so a square /// a hundred metres wide was still one carpet: nothing in it changed over the /// ten metres between one lamppost and the next. A real one is lighter here and /// duller there, and has been dug up and relaid in places — a trench, a cable, /// a drain — where the stones are of another batch and have not yet taken on /// the street's dirt, or, in asphalt, a darker, smoother patch with a seam of /// sealant round it. /// /// All of it averages out — the clouds are zero-mean, the patches a fifth of /// the cells and a few per cent of the ground — so where `FLAT_GROUND_OFF_M` /// hands the ground to the basemap, which paints each pattern's mean /// (`paving::pattern_mean`), the colour does not step, only the mottling goes. /// It fades on a pixel's *sharp* side, between one and three metres, because a /// blotch five metres wide is still five metres across a grazing view. The lattices (12.5, 6.25, 5 and /// 3.125 m) divide 125, so with `pav_hash`'s 80 they divide the floating /// origin's 10,000 m jump like the stones' do. struct StreetScale { /// Multiplies the surface's colour. tone: vec3, /// Multiplies its roughness. roughness: f32, /// 1 inside a relaid patch, 0 elsewhere and on patterns that are not laid. fresh: f32, } /// The street-scale variation at `p`; `laid` is 0 for no patches (loose /// ground, a road ribbon), 1 for laid stones, 2 for asphalt. fn street_scale(p: vec2, laid: u32, metres_per_px: f32, sharpest_px: f32) -> StreetScale { var s: StreetScale; let k = 1.0 - smoothstep(1.0, 3.0, sharpest_px); let broad = pav_noise(p / 12.5 + vec2(3.0, 61.0)) - 0.5; let near = pav_noise(p / 5.0 + vec2(47.0, 11.0)) - 0.5; let warm = pav_noise(p / 12.5 + vec2(29.0, 5.0)) - 0.5; let v = (0.45 * broad + 0.22 * near) * k; s.tone = vec3(1.0 + v) * (vec3(1.0) + vec3(0.08, 0.0, -0.08) * warm * k); s.roughness = 1.0 + 0.16 * (pav_noise(p / 3.125 + vec2(7.0, 41.0)) - 0.5) * k; s.fresh = 0.0; if laid == 0u || k <= 0.0 { return s; } let cell = floor(p / 6.25); let h = pav_hash(cell + vec2(53.0, 29.0)); if h > 0.8 { let f = fract(p / 6.25); let lo = vec2(0.05 + 0.3 * fract(h * 11.3), 0.05 + 0.3 * fract(h * 17.1)); let hi = vec2(0.6 + 0.35 * fract(h * 23.9), 0.6 + 0.35 * fract(h * 5.3)); // Metres inside the patch's nearest edge: negative outside it. let d = min(f - lo, hi - f) * 6.25; let inside_m = min(d.x, d.y); let aa = max(metres_per_px, 0.002); let fresh = smoothstep(-aa, aa, inside_m) * k; s.fresh = fresh; let batch = fract(h * 41.7) - 0.5; if laid == 1u { // Another batch of stone: a shade off in hue, and a little lighter // for being clean. s.tone *= mix(vec3(1.0), vec3(1.14 + 0.25 * batch, 1.14 + 0.08 * batch, 1.14 - 0.2 * batch), fresh); } else { // Fresh asphalt is darker and closer-textured than the worn road // round it, and the joint is sealed with a strip of bitumen. let seam = (1.0 - smoothstep(0.03, 0.03 + aa, abs(inside_m))) * k; s.tone *= mix(vec3(1.0), vec3(0.8), fresh) * (1.0 - 0.45 * seam); s.roughness *= mix(1.0, 0.85, fresh); } } return s; } /// The edge of a road, where the camber sends the water and the sweeper does /// not reach: a gutter of dust and grit a hand or two wide, moss in it where it /// stays damp, the odd green tuft in a seam, and on the tarmac the net of /// cracks old asphalt breaks into along its edge, where the foundation gives /// first. Texture, not placement — nothing here claims a weed at a particular /// spot (`streets-that-look-lived-in.md`). /// /// `seam_m` is metres to the nearest seam: the carriageway's edge on the /// tarmac, either edge of the kerb band on the band. `tarmac` says which, since /// a concrete kerb band does not crack the way asphalt does. Every lattice /// divides 125, so with `pav_hash`'s 80 they hold across the floating origin's /// jump. /// /// The dust is a colour of its own laid over the surface, not a multiplier: it /// lightens dark tarmac and darkens the pale band, which is what dust does. const DUST: vec3 = vec3(0.24, 0.215, 0.17); /// `ribbon::ROAD_CASING_M`: how wide a kerb band is. const KERB_BAND_M: f32 = 0.75; const TUFT: vec3 = vec3(0.07, 0.16, 0.025); /// Greener than the moss in a joint (`MOSS`): a gutter stays wet for days. const GUTTER_MOSS: vec3 = vec3(0.08, 0.15, 0.04); /// Asphalt's crazing: the edges of an irregular net of cells `unit` metres /// across, as metres to the nearest edge. fn craze_m(p: vec2, unit: f32) -> f32 { let q = p / unit; let c = floor(q); var f1 = 9.0; var f2 = 9.0; for (var j = -1; j <= 1; j++) { for (var i = -1; i <= 1; i++) { let cell = c + vec2(f32(i), f32(j)); let h = vec2(pav_hash(cell + vec2(3.0, 7.0)), pav_hash(cell + vec2(61.0, 29.0))); let d = length(cell + 0.15 + 0.7 * h - q); if d < f1 { f2 = f1; f1 = d; } else if d < f2 { f2 = d; } } } return 0.5 * (f2 - f1) * unit; } fn gutter(look_in: PavingLook, p: vec2, seam_m: f32, tarmac: bool, metres_per_px: f32) -> PavingLook { var look = look_in; let k = 1.0 - pav_blur(0.5, metres_per_px); if k <= 0.0 || seam_m > 1.5 { return look; } let aa = max(metres_per_px, 0.002); // How wide the dirty band is wanders along the road: swept here, silted // up there. let reach = 0.15 + 0.35 * pav_noise(p / 3.125 + vec2(19.0, 7.0)); let band = 1.0 - smoothstep(0.0, reach, seam_m); let mottle = pav_noise(p / 0.25 + vec2(3.0, 59.0)); let dust = band * mix(0.35, 0.85, mottle) * k; // Moss where the gutter stays damp: patches a metre or so long, broken up // at the scale of a hand. let damp = smoothstep(0.25, 0.5, pav_noise(p / 1.25 + vec2(37.0, 23.0))); let clumps = smoothstep(0.25, 0.55, pav_noise(p / 0.125 + vec2(11.0, 5.0))); let moss = sqrt(band) * damp * clumps * k; // A tuft in the seam itself: a few centimetres of green right at the // edge, every metre or two. let in_seam = 1.0 - smoothstep(0.03, 0.12, seam_m); let tufts = in_seam * smoothstep(0.5, 0.68, pav_noise(p / 0.625 + vec2(43.0, 67.0))) * smoothstep(0.3, 0.55, mottle) * (1.0 - pav_blur(0.1, metres_per_px)); var crack = 0.0; if tarmac { // Crazed only within a metre or so of the edge, and only on stretches // that have started to go. let near = 1.0 - smoothstep(0.3, 1.5, seam_m); let aged = smoothstep(0.35, 0.55, pav_noise(p / 6.25 + vec2(71.0, 31.0))); let fade = near * aged * (1.0 - pav_blur(0.03, metres_per_px)); if fade > 0.0 { let jitter = (vec2(pav_noise(p / 0.0625), pav_noise(p / 0.0625 + vec2(5.0, 9.0))) - 0.5) * 0.05; let d = craze_m(p + jitter, 0.625); let width = mix(0.005, 0.012, near); crack = (1.0 - smoothstep(width, width + aa, d)) * fade; } } look.tone *= 1.0 - 0.7 * crack; let green = max(moss * 0.9, tufts); let fill = mix(DUST, mix(GUTTER_MOSS, TUFT, tufts), green / max(dust + green, 1.0e-3)); look.fill = vec4(fill, max(dust, green)); look.roughness = mix(look.roughness, 1.0, dust); look.tilt *= 1.0 - 0.7 * dust; return look; } /// Where in its stone a point of a laid pattern (2–6) is, for the stones' /// look and for the joints' depth alike, so the march below finds the joints /// the colour draws. `cell` names the stone, `f` is where in it (0–1 both /// ways), `size` the stone in metres along those axes, `edge` how far from its /// nearest joint as a share of the stone, `joint` the joint's share, and `unit` /// the lattice the stones fade on. struct LaidCell { cell: vec2, f: vec2, size: vec2, edge: f32, joint: f32, unit: f32, } fn laid_cell(pattern: u32, p: vec2) -> LaidCell { var s: LaidCell; switch pattern { case 2u: { // Poured concrete in 2.5 m slabs with sawn joints. let q = p / 2.5; s.cell = floor(q); s.f = fract(q); s.size = vec2(2.5, 2.5); s.edge = min(min(s.f.x, 1.0 - s.f.x), min(s.f.y, 1.0 - s.f.y)); s.joint = 0.006; s.unit = 2.5; } case 3u: { // Clay brick in a 90° herringbone, 20 × 10 cm. Each 10 cm square is // half a brick: with d = (x + y) mod 4, d = 0/1 are the left/right of // a brick lying along x, d = 2/3 the bottom/top of one along z — // which tiles the plane, and is the bond most Dutch brick streets // are laid in. let q = p / 0.1; let c = floor(q); let f = fract(q); let d = u32(c.x + c.y - 4.0 * floor((c.x + c.y) / 4.0)); s.cell = c; switch d { case 0u: { s.edge = min(f.x, min(f.y, 1.0 - f.y)); s.f = vec2(f.x * 0.5, f.y); } case 1u: { s.cell = c - vec2(1.0, 0.0); s.edge = min(1.0 - f.x, min(f.y, 1.0 - f.y)); s.f = vec2(0.5 + f.x * 0.5, f.y); } case 2u: { s.edge = min(f.y, min(f.x, 1.0 - f.x)); s.f = vec2(f.x, f.y * 0.5); } default: { s.cell = c - vec2(0.0, 1.0); s.edge = min(1.0 - f.y, min(f.x, 1.0 - f.x)); s.f = vec2(f.x, 0.5 + f.y * 0.5); } } s.size = select(vec2(0.1, 0.2), vec2(0.2, 0.1), d < 2u); s.joint = 0.08; s.unit = 0.1; } case 4u: { // Concrete pavers, 20 × 10 cm, each course half a stone over. let course = floor(p.y / 0.1); let q = vec2(p.x / 0.2 + 0.5 * course, p.y / 0.1); s.cell = floor(q); s.f = fract(q); s.size = vec2(0.2, 0.1); s.edge = min(min(s.f.x, 1.0 - s.f.x) * 2.0, min(s.f.y, 1.0 - s.f.y)); s.joint = 0.08; s.unit = 0.1; } case 5u: { // Pavement tiles, 30 × 30 cm, butted close. let q = p / 0.3125; s.cell = floor(q); s.f = fract(q); s.size = vec2(0.3125, 0.3125); s.edge = min(min(s.f.x, 1.0 - s.f.x), min(s.f.y, 1.0 - s.f.y)); s.joint = 0.05; s.unit = 0.3125; } default: { // Natural stone setts, 12.5 cm, each row shifted by its own amount, // wide sandy joints and a strongly varied face. let row = floor(p.y / 0.125); let shift = pav_hash(vec2(row, 7.0)); let q = vec2(p.x / 0.125 + shift, p.y / 0.125); s.cell = floor(q); s.f = fract(q); s.size = vec2(0.125, 0.125); s.edge = min(min(s.f.x, 1.0 - s.f.x), min(s.f.y, 1.0 - s.f.y)); s.joint = 0.14; s.unit = 0.125; } } return s; } // ─── Joints in depth ───────────────────────────────────────────────────────── // // A laid street's joints are grooves, and the stones' edges hide part of each // groove from a camera that is not straight overhead: from a walker's eye the // far side of every joint is lost behind the stone in front of it, and the // pattern reads as stones lying *on* something rather than printed on it. The // normal's tilt above lights the bevel; it cannot hide anything. // // So the point a pixel shows is found by marching its view ray down into the // joints' height field (parallax occlusion mapping) and the look is drawn there. // The ground is horizontal, so the ray's step across it is `V.xz / V.y` and no // tangent frame is needed — which matters, because no ground mesh carries // tangents and adding them to the largest uploads was declined // (`surface-finish-and-occlusion.md`). Only the point moves: depth is not // written, so the depth prepass and the shadows see the plain surface, and the // lattices still divide the floating origin's jump because the shift is // centimetres. /// How deep a pattern's joints are, metres. A brick street's sand sits a /// centimetre or so under the stones; a slab's sawn joint is a few millimetres. fn joint_depth_m(pattern: u32) -> f32 { switch pattern { case 2u: { return 0.004; } case 3u: { return 0.012; } case 4u: { return 0.010; } case 5u: { return 0.004; } default: { return 0.018; } } } /// How far below the stones' face `p` is, as a share of the joint's depth: 0 /// on a face, 1 in the joint's bottom, and the bevel rolling between them. fn joint_profile(pattern: u32, p: vec2) -> f32 { let s = laid_cell(pattern, p); // Metres from the joint's centre, along the nearer axis of the stone. let d = s.edge * min(s.size.x, s.size.y); let half_joint = s.joint * 0.75 * min(s.size.x, s.size.y); return 1.0 - smoothstep(half_joint, half_joint + BEVEL_M, d); } /// Linear steps through the joint's depth. Eight resolve a centimetre's /// groove at a walker's grazing angle without stepping; the secant after them /// takes the hit between two steps. const JOINT_STEPS: i32 = 8; /// The view ray is not followed flatter than this, or at the horizon a joint /// a centimetre deep would throw the pattern metres along the street. const JOINT_MIN_RISE: f32 = 0.2; /// The point a pixel at `p` sees once the joints are grooves, for `view` the /// unit vector from the surface to the camera. Faded in with the stones' /// sharpness: below a few pixels a stone has no joint worth hiding, and the /// march costs nothing there. fn joint_parallax(pattern: u32, p: vec2, view: vec3, metres_per_px: f32) -> vec2 { if pattern < 2u || pattern > 6u { return p; } let k = 1.0 - clamp(pav_blur(laid_cell(pattern, p).unit, metres_per_px) * 3.0, 0.0, 1.0); // A face is the top of the field: a ray that enters there has already hit // it, which is most pixels, and they march nothing. let h0 = joint_profile(pattern, p); if k <= 0.0 || h0 <= 0.0 { return p; } // Across the ground per unit of depth, from where the ray enters the face // down to the joint's bottom, away from the camera. let reach = -view.xz / max(view.y, JOINT_MIN_RISE) * joint_depth_m(pattern) * k; let step = 1.0 / f32(JOINT_STEPS); var t_prev = 0.0; var gap_prev = -h0; var t = 0.0; for (var i = 1; i <= JOINT_STEPS; i++) { t = f32(i) * step; let gap = t - joint_profile(pattern, p + reach * t); if gap >= 0.0 { // Between the last step above the surface and this one below it. let w = clamp(gap_prev / min(gap_prev - gap, -1.0e-6), 0.0, 1.0); return p + reach * mix(t_prev, t, w); } t_prev = t; gap_prev = gap; } return p + reach; } /// `paving::pattern`: 1 asphalt, 2 concrete slabs, 3 brick in herringbone, /// 4 concrete pavers in a stretcher bond, 5 pavement tiles, 6 stone setts, /// 7 grass pavers, 8 gravel and sand. fn paving_look(pattern: u32, p: vec2, metres_per_px: f32, sharpest_px: f32) -> PavingLook { var look: PavingLook; look.tone = vec3(1.0); look.tilt = vec2(0.0); look.roughness = 0.9; let laid = select(select(0u, 1u, pattern >= 2u && pattern <= 6u), 2u, pattern == 1u); let street = street_scale(p, laid, metres_per_px, sharpest_px); switch pattern { case 1u: { // Asphalt: a fine aggregate, and the patches of a road resurfaced // piece by piece. let grain = (pav_noise(p / 0.025) - 0.5) * (1.0 - pav_blur(0.025, metres_per_px)); let patches = pav_noise(p / 2.44) - 0.5; look.tone = vec3(1.0 + 0.16 * grain + 0.12 * patches); look.tilt = grain_tilt(p, 0.025, ASPHALT_RELIEF, metres_per_px); } case 2u: { // Poured concrete in 2.5 m slabs with sawn joints. let s = laid_cell(pattern, p); look = stone(pav_hash(s.cell), s.f, s.size, s.edge, s.joint, 0.1, pav_blur(s.unit, metres_per_px), 0.99); look.tilt = vec2(0.0); look.tone *= 1.0 + 0.06 * (pav_noise(p / 0.7) - 0.5); } case 3u: { let s = laid_cell(pattern, p); look = stone(pav_hash(s.cell), s.f, s.size, s.edge, s.joint, 0.32, pav_blur(s.unit, metres_per_px), 0.96); // Clay is not one colour: some bricks fire browner, some purpler. let hue = pav_hash(s.cell + vec2(17.0, 3.0)) - 0.5; look.tone *= vec3(1.0 + 0.1 * hue, 1.0, 1.0 - 0.12 * hue); } case 4u: { let s = laid_cell(pattern, p); look = stone(pav_hash(s.cell), s.f, s.size, s.edge, s.joint, 0.14, pav_blur(s.unit, metres_per_px), 0.97); } case 5u: { let s = laid_cell(pattern, p); look = stone(pav_hash(s.cell), s.f, s.size, s.edge, s.joint, 0.2, pav_blur(s.unit, metres_per_px), 0.97); // Pavement tiles weather: a faint stain here and there, larger than a tile. look.tone *= 1.0 + 0.1 * (pav_noise(p / 1.7) - 0.5) * (1.0 - pav_blur(1.7, metres_per_px)); look.tilt *= 0.5; } case 6u: { let s = laid_cell(pattern, p); look = stone(pav_hash(s.cell), s.f, s.size, s.edge, s.joint, 0.4, pav_blur(s.unit, metres_per_px), 0.93); look.roughness = 0.75; // A sett is hand-dressed stone, rounder than a pressed paver. look.tilt += (s.f - vec2(0.5, 0.5)) * 0.25 * (1.0 - pav_blur(s.unit, metres_per_px)); } case 7u: { // Grass pavers: a 40 cm concrete grid with grass in its cells. let f = fract(p / 0.4); let hole = f32(abs(f.x - 0.5) < 0.32 && abs(f.y - 0.5) < 0.32); let blur = pav_blur(0.4, metres_per_px); let grass = vec3(0.45, 0.85, 0.35); look.tone = mix(mix(vec3(1.05), grass, hole), mix(vec3(1.05), grass, 0.4), blur); } case 8u: { // Gravel, shell and sand: loose grains. let grain = (pav_noise(p / 0.03) - 0.5) * (1.0 - pav_blur(0.03, metres_per_px)); look.tone = vec3(1.0 + 0.3 * grain + 0.08 * (pav_noise(p / 1.5) - 0.5)); look.tilt = grain_tilt(p, 0.03, 0.8, metres_per_px); } default: {} } if look.crisp > 0.0 { look = lived_in(look, pattern, p, metres_per_px, street.fresh); } look.tone *= street.tone; look.roughness *= street.roughness; return look; } // ─── Foliage ───────────────────────────────────────────────────────────────── // // A crown is an 18-vertex ellipsoid (`scatter::build_vegetation_meshes`), and lit // as one it read as a green ball: one smooth gradient from the lit side to the // shade. A real crown is clumps of leaves with gaps between them, and the gaps // are the darkest thing in it, because the sky and the sun reach them only // through the clumps. So the crown's surface becomes a density field in three // lattices — boughs, clumps and sprays of leaves — whose slope tilts the normal // and whose low points are occluded, from the ambient and from the sun alike. // Nothing is stored or uploaded. The lattices divide the floating origin's // 10,000 m jump and the hash wraps every 80 cells in x and z, as the paving's // do, so a crown is the same crown either side of one. fn fol_hash(i: vec3) -> f32 { let w = vec3(i.x - floor(i.x / 80.0) * 80.0, i.y, i.z - floor(i.z / 80.0) * 80.0); return fract(sin(dot(w, vec3(127.1, 311.7, 74.7))) * 43758.5453); } fn fol_noise(p: vec3) -> f32 { let i = floor(p); let f = fract(p); let u = f * f * (3.0 - 2.0 * f); let near = mix( mix(fol_hash(i), fol_hash(i + vec3(1.0, 0.0, 0.0)), u.x), mix(fol_hash(i + vec3(0.0, 1.0, 0.0)), fol_hash(i + vec3(1.0, 1.0, 0.0)), u.x), u.y, ); let far = mix( mix(fol_hash(i + vec3(0.0, 0.0, 1.0)), fol_hash(i + vec3(1.0, 0.0, 1.0)), u.x), mix(fol_hash(i + vec3(0.0, 1.0, 1.0)), fol_hash(i + vec3(1.0, 1.0, 1.0)), u.x), u.y, ); return mix(near, far, u.z); } /// One lattice's height and slope, the slope by forward differences a third of /// a cell long — short enough to follow a clump, long enough not to alias. fn fol_octave(q: vec3) -> vec4 { let h = fol_noise(q); let e = 0.33; let g = vec3( fol_noise(q + vec3(e, 0.0, 0.0)) - h, fol_noise(q + vec3(0.0, e, 0.0)) - h, fol_noise(q + vec3(0.0, 0.0, e)) - h, ) / e; return vec4(g, h); } // Lattices in metres: boughs, clumps, sprays. Each divides 10,000 m into a whole // multiple of 80 cells. const BOUGH_M: f32 = 1.25; const CLUMP_M: f32 = 0.5; const SPRAY_M: f32 = 0.125; struct FoliageLook { /// The colour multiplier: a clump's sunny face a little over 1, a gap well /// under, converging on 1 below a pixel. tone: f32, /// Added to the normal: minus the density's slope, so a clump faces out. tilt: vec3, /// How much of the sky and the sun reach this point, 0..1. open: f32, } fn foliage(p: vec3, metres_per_px: f32) -> FoliageLook { var look: FoliageLook; look.tone = 1.0; look.tilt = vec3(0.0); look.open = 1.0; let k_bough = 1.0 - pav_blur(BOUGH_M, metres_per_px); if k_bough <= 0.0 { return look; } let k_clump = 1.0 - pav_blur(CLUMP_M, metres_per_px); let k_spray = 1.0 - pav_blur(SPRAY_M, metres_per_px); let bough = fol_octave(p / BOUGH_M); var d = (bough.w - 0.5) * 0.9 * k_bough; var slope = bough.xyz * 0.35 * k_bough; if k_clump > 0.0 { let clump = fol_octave(p / CLUMP_M); d += (clump.w - 0.5) * 0.8 * k_clump; slope += clump.xyz * 0.45 * k_clump; } if k_spray > 0.0 { // Height only: a slope at this scale is a sparkle by the time it is // sharp enough to see. d += (fol_noise(p / SPRAY_M) - 0.5) * 0.4 * k_spray; } // 0 deep in a gap, 1 on the face of a clump. The whole look fades with the // coarsest lattice, so a crown past a few hundred metres keeps the colour // the far map was matched to (`palette::CANOPY`, `vegetation`'s merge). let face = smoothstep(-0.45, 0.25, d); let k = max(k_bough, k_clump); look.tone = mix(1.0, mix(0.5, 1.06, face), k); look.open = mix(1.0, mix(0.35, 1.0, face), k); look.tilt = -slope; return look; } @fragment fn fragment(in: VertexOutput, @builtin(front_facing) is_front: bool) -> FragmentOutput { var pbr_input = pbr_input_from_standard_material(in, is_front); @if(BINDLESS) let slot = mesh[in.instance_index].material_and_lightmap_bind_group_slot & 0xffffu; @if(BINDLESS) let ground = ground_array[ground_indices[slot].material]; @else let ground = ground_single; let detail = ground.detail; let lod = ground.lod; let water_body = ground.water_body; let water_sea = ground.water_sea; let far = ground.far; // How much of this texel is water, read out of the tile's alpha **before** // `alpha_discard` flattens it to 1 for an opaque material. 255 is land, so a // mask-less image and every surface that is not a tile answer 0. var water = 0.0; if water_sea.z > 0.0 { water = 1.0 - pbr_input.material.base_color.a; } // …and on a surveyed surface the same alpha carries what it is paved in // (`paving::pattern`, as `1 + pattern`): 1.0, which every other surface // carries, is none. Read before `alpha_discard` for the same reason. let paving_code = select(0.0, pbr_input.material.base_color.a - 1.0, water_sea.z <= 0.0); // The street furniture (`ground_detail::DETAIL_LAMP_HEADS`) is one material // for every object in a cell, so the night glow `street_detail::update_lamp_glow` // writes into its emissive lights only the lamp heads, which carry // `furniture::LAMP_HEAD_ALPHA` where every other part carries 1. if detail.w < 0.0 && detail.y > 0.5 { let head = select(0.0, 1.0, pbr_input.material.base_color.a < 0.75); pbr_input.material.emissive = vec4( pbr_input.material.emissive.rgb * head, pbr_input.material.emissive.a, ); } // How many metres one pixel spans here — taken outside any branch, since a // derivative in non-uniform control flow is undefined. // // A grazing view smears the ground along the line of sight and not across // it: at eye height ten metres off, a pixel is five times longer than it is // wide. Fading the stones on the long side alone took them out of the // picture three metres from the feet (Grote Markt, 2026-10-29), so they // fade on the side's geometric mean — the pixel's area — which keeps them // twice as far and still goes before they are small enough to crawl. let along_px = max(length(dpdx(in.world_position.xz)), length(dpdy(in.world_position.xz))); let sharpest_px = min(length(dpdx(in.world_position.xz)), length(dpdy(in.world_position.xz))); let metres_per_px = sqrt(along_px * sharpest_px); // A crown is not ground and is seen side-on as often as from above, so its // footprint is taken in three dimensions. let crown_px = length(fwidth(in.world_position.xyz)); // Where this texel is in Web Mercator, for the sea's sheen (`map_light`): // the tile's key and its UV, as the hex grid finds it; none on a surface // with no key or no UV. A derivative, so here. var merc = vec2(-1.0); @if(VERTEX_UVS_A) if ground.tile.z > 0.5 { merc = (ground.tile.xy + in.uv) / ground.tile.z; } let merc_px = length(fwidth(merc)); // Metres to the nearer side of a road ribbon, whose `u` runs 0→1 straight // across it (`SurfaceMesh::uvs`). The ribbon's width is not passed in, so it // is read off how fast `u` changes over the ground: solve the 2×2 the two // screen derivatives make for `u`'s gradient in world XZ, whose length is // one over the width. A kerb band's `u` also runs 0→1 across it, from the // carriageway out (`ribbon::kerb_band`), over a known width; a junction // stub's is 0 throughout, and gets no seam. Meaningless on anything but a // road, which is the only branch that reads it. var seam_m = 1.0e4; @if(VERTEX_UVS_A) { let dx = dpdx(in.world_position.xz); let dy = dpdy(in.world_position.xz); let du = vec2(dpdx(in.uv.x), dpdy(in.uv.x)); let det = dx.x * dy.y - dx.y * dy.x; let across = min(in.uv.x, 1.0 - in.uv.x); if abs(det) > 1.0e-12 && (abs(du.x) + abs(du.y)) > 0.0 { let g = vec2(dy.y * du.x - dx.y * du.y, dx.x * du.y - dy.x * du.x) / det; seam_m = across / max(length(g), 1.0e-4); } if (pbr_input.material.flags & STANDARD_MATERIAL_FLAGS_BASE_COLOR_TEXTURE_BIT) == 0u && (abs(du.x) + abs(du.y)) > 0.0 { seam_m = across * KERB_BAND_M; } } pbr_input.material.base_color = alpha_discard(pbr_input.material.flags, pbr_input.material.alpha_cutoff, pbr_input.material.base_color); // A clipmap level meets the coarser one beneath it along the edge of its own // ring, and across that edge the texel density halves in one step — a hard square // line of sharpness drawn across the ground, which no amount of filtering removes // because it is a difference in the *source*, not in the sampling rate. // // The levels above `vector_tiles::MAX_SOURCE_ZOOM` are magnified renders of the // same z14 body, so a child's own mip 1 is, texel for texel, its parent's // density. Ramping a mip bias to 1.0 across the outer band of the ring therefore // lands the child's edge on exactly the sharpness the parent carries just beyond // it, and the step becomes a gradient. Nothing is blended between two textures and // nothing is made transparent: the tile stays one opaque quad, sampled softer // towards its rim. // // The branch is on a uniform-buffer value, so the sample below stays in uniform // control flow — and every ground surface that is not a clipmap tile carries // `lod.w == 0` and pays one comparison. // // **`VERTEX_UVS_A` is not optional here.** `VertexOutput::uv` exists only when // the mesh carries UV0, and several groups that share this material do not — // `tessellate_surfaces` leaves `uvs` empty for the flat colour fills. Reading // `in.uv` unguarded does not fail on the tiles, which have it; it fails to // compile the *specialisation* the fills need, which is a pipeline that never // builds and a shader error at the first draw of a grass polygon. @if(VERTEX_UVS_A) if lod.w > 0.0 { let ring = (in.world_position.xz - lod.xy) * lod.z; // Chebyshev, because the ring is a square of tiles and not a disc: this is // exactly 1.0 along the whole outer boundary, which is where the parent // takes over. let edge = max(abs(ring.x), abs(ring.y)); let t = smoothstep(LOD_BLEND_START, 1.0, edge); @if(BINDLESS) let soft = textureSampleBias( bindless_textures_2d[ground_indices[slot].basemap], bindless_samplers_filtering[ground_indices[slot].basemap_sampler], in.uv, lod.w * t, ); @else let soft = textureSampleBias(lod_basemap, lod_basemap_sampler, in.uv, lod.w * t); // `mix`, not an assignment: at `t = 0` the inner ring must be bit-for-bit // what it was, and this fetch and bevy's own are not obliged to agree in the // last bit. Only the colour is taken — `alpha_discard` has already run // against the alpha above, and re-deciding it here from a softer sample // would move a cutout that has already been made. pbr_input.material.base_color = vec4( mix(pbr_input.material.base_color.rgb, soft.rgb, t), pbr_input.material.base_color.a, ); } // From today's ground to the painted map (`vector_tiles::painted`), on distance // from the camera and not on its height, so what is near keeps today's style in // a tilted view too. A tile in the band where the two meet carries itself drawn // a second time in the painted style; every other surface carries `far.z == 0`. // Water keeps its mask from the tile's own alpha, read above. @if(VERTEX_UVS_A) if far.z > 0.0 { let d = length(in.world_position.xyz - view_bindings::view.world_position.xyz); let t = smoothstep(far.x, far.y, d); @if(BINDLESS) let painted = textureSample( bindless_textures_2d[ground_indices[slot].painted], bindless_samplers_filtering[ground_indices[slot].painted_sampler], in.uv, ); @else let painted = textureSample(painted_basemap, painted_sampler, in.uv); pbr_input.material.base_color = vec4( mix(pbr_input.material.base_color.rgb, painted.rgb, t), pbr_input.material.base_color.a, ); } // The optional hex grid, over land only (`hex_grid.wesl`). Evaluated outside any // branch, because it takes a derivative; `tile.w` is 0 wherever it is off. @if(VERTEX_UVS_A) { let hex = hex_grid::coverage( ground.tile, in.uv, length(in.world_position.xyz - view_bindings::view.world_position.xyz), water, ); pbr_input.material.base_color = vec4( mix(pbr_input.material.base_color.rgb, hex_grid::LINE, hex), pbr_input.material.base_color.a, ); } if paving_code > 0.5 { let pattern = u32(round(paving_code)); let seen = joint_parallax(pattern, in.world_position.xz, pbr_input.V, metres_per_px); let look = paving_look(pattern, seen, metres_per_px, sharpest_px); pbr_input.material.base_color = vec4( mix(pbr_input.material.base_color.rgb * look.tone, look.fill.rgb, look.fill.a), pbr_input.material.base_color.a, ); let n = normalize(pbr_input.N + vec3(look.tilt.x, 0.0, look.tilt.y)); pbr_input.N = n; pbr_input.world_normal = n; pbr_input.material.perceptual_roughness = look.roughness; } else if water_sea.w > 0.5 { // A road ribbon: its texture carries the markings, the wheel paths and // a grain coarser than the stones, so the aggregate's relief is added // here exactly as the surveyed asphalt beside it gets it. // // The same broad variation as the surveyed asphalt beside it, so the two // do not change character at the seam; no patches, which would cut // across the markings at angles no repair crew would. Then the gutter. let street = street_scale(in.world_position.xz, 0u, metres_per_px, sharpest_px); var edge: PavingLook; edge.tone = street.tone; edge.tilt = grain_tilt(in.world_position.xz, 0.025, ASPHALT_RELIEF, metres_per_px); edge.roughness = pbr_input.material.perceptual_roughness * street.roughness; edge.fill = vec4(0.0); // A road's carriageway samples its generated texture; its kerb band is // flat-coloured (`road_base_material`). let tarmac = (pbr_input.material.flags & STANDARD_MATERIAL_FLAGS_BASE_COLOR_TEXTURE_BIT) != 0u; edge = gutter(edge, in.world_position.xz, seam_m, tarmac, metres_per_px); let n = normalize(pbr_input.N + vec3(edge.tilt.x, 0.0, edge.tilt.y)); pbr_input.N = n; pbr_input.world_normal = n; pbr_input.material.base_color = vec4( mix(pbr_input.material.base_color.rgb * edge.tone, edge.fill.rgb, edge.fill.a), pbr_input.material.base_color.a, ); pbr_input.material.perceptual_roughness = edge.roughness; } else if detail.w < 0.0 && detail.x > 0.5 { // A tree's crown (`ground_detail::DETAIL_FOLIAGE`). let look = foliage(in.world_position.xyz, crown_px); // The underside of a crown is lit by the ground and by little else: // the sky is behind the whole depth of it. let under = mix(0.5, 1.0, smoothstep(-0.9, 0.4, pbr_input.N.y)); let n = normalize(pbr_input.N + look.tilt); pbr_input.N = n; pbr_input.material.base_color = vec4( pbr_input.material.base_color.rgb * look.tone, pbr_input.material.base_color.a, ); pbr_input.diffuse_occlusion *= look.open * under; pbr_input.specular_occlusion *= look.open * under; // A gap is in the crown's own shade, whichever way the sun is. pbr_input.directional_shadow_factor *= mix(0.4, 1.0, look.open); // Leaves have a waxy sheen the bark and the ground do not. pbr_input.material.perceptual_roughness = 0.7; } if detail.w > 0.0 { let d = (in.world_position.xz - detail.xy) * detail.z; let rim = max(abs(d.x), abs(d.y)); // Water is level; the ground's kerbs and ruts are not in it. let w = detail.w * (1.0 - smoothstep(RIM_START, 1.0, rim)) * (1.0 - water); if w > 0.0 { let uv = d * 0.5 + vec2(0.5, 0.5); @if(BINDLESS) let slope = textureSampleLevel( bindless_textures_2d[ground_indices[slot].slope], bindless_samplers_filtering[ground_indices[slot].slope_sampler], uv, 0.0, ); @else let slope = textureSampleLevel(detail_slope, detail_sampler, uv, 0.0); let s = slope.xy * SLOPE_RANGE * w; // The ground's own normal already carries the coarse slope, and the // texture is the residual, so this is an addition rather than a // replacement — `water.wgsl` replaces because its surface is flat by // construction and this one is not. let n = normalize(pbr_input.N + vec3(-s.x, 0.0, -s.y)); pbr_input.N = n; pbr_input.world_normal = n; } } // The water texels become the surface's far field: its deep body colour // behind the same fresnel, its roughness with every wave retired, its // reflectance. Blended by the mask, so a coastline's mip-softened edge is a // blend rather than a stair. // // The tile's own paint is kept for the map light below: from far the sea is // the painted map's sea, which is what the globe's tiles past this one draw, // and lit as the body colour it came out darker and drew the clipmap's edge // across the sea as a straight seam (mac-1, 25 km over the North Sea). let paint = pbr_input.material.base_color.rgb; var reflected = 0.0; if water > 0.0 { // **Water is level, whatever the tile under it is doing.** A relief tile's // normal follows the terrain field at its own quad size, and beside a dike // that is a slope spread over tens of metres of open water. Matte paint // never showed it; a glossy surface reflecting the sky turned it into a // dark band on one side of the Afsluitdijk and a bright one on the other // (Deck, 2026-09-26). The mask-weighted blend keeps a coastline's land // half on the ground's own normal. let up = normalize(mix(pbr_input.N, vec3(0.0, 1.0, 0.0), water)); pbr_input.N = up; pbr_input.world_normal = up; reflected = water_reflected(dot(pbr_input.N, pbr_input.V)); pbr_input.material.base_color = vec4( mix( pbr_input.material.base_color.rgb, water_body.rgb * water_transmitted(reflected), water, ), pbr_input.material.base_color.a, ); pbr_input.material.perceptual_roughness = mix(pbr_input.material.perceptual_roughness, water_sea.x, water); pbr_input.material.reflectance = mix(pbr_input.material.reflectance, vec3(water_body.w), water); } // The clouds' shadows (`cloud_field::sun_shadow`): the sun alone, through // the deck this material already binds for its water's mirror. Built field // by field, never as a whole struct load — see the mirror below for why. { @if(BINDLESS) let deck_slot = ground_indices[slot].clouds; @if(BINDLESS) let shadow_deck = cf::Deck( ground_clouds[deck_slot].shape, ground_clouds[deck_slot].motion, ground_clouds[deck_slot].sun_dir, ground_clouds[deck_slot].sun_color, ground_clouds[deck_slot].sky_color, ground_clouds[deck_slot].place, ground_clouds[deck_slot].haze_color, ground_clouds[deck_slot].light, ); @else let shadow_deck = cf::Deck( clouds_single.shape, clouds_single.motion, clouds_single.sun_dir, clouds_single.sun_color, clouds_single.sky_color, clouds_single.place, clouds_single.haze_color, clouds_single.light, ); pbr_input.directional_shadow_factor *= cf::sun_shadow( shadow_deck, in.world_position.xyz - view_bindings::view.world_position.xyz, ); } var out: FragmentOutput; out.color = apply_pbr_lighting(pbr_input); // From far, the painted map in its own colours, whatever the sun's angle. out.color = map_light::apply( out.color, mix(pbr_input.material.base_color.rgb, paint, water), pbr_input.material.emissive.rgb, normalize(pbr_input.N), length(in.world_position.xyz - view_bindings::view.world_position.xyz), ); // …and the sun on the sea, which that takes away. out.color = map_light::sea_sheen( out.color, normalize(pbr_input.N), pbr_input.V, water, length(in.world_position.xyz - view_bindings::view.world_position.xyz), merc, merc_px, ); // The same stand-in sky the surface reflects where there is no environment // map, weighted by the mask; the land part of a coastal texel keeps all of // bevy's ambient. @if(!ENVIRONMENT_MAP && DISTANCE_FOG) if water > 0.0 { out.color = vec4( sky_fallback( out.color.rgb, pbr_input, view_bindings::fog.base_color.rgb, water_sea.y, reflect(-pbr_input.V, pbr_input.N).y, reflected, water, 1.0 - water, ), out.color.a, ); } // And the clouds in it, as the surface inside the ring mirrors them // (`water.wgsl`): without this a lake's clouds would stop at the ring's edge. // The pixel's footprint on the deck is taken out here, for every ground pixel, // because a derivative is not allowed inside the water-only branch — the mask // varies from texel to texel. It is a handful of arithmetic and one load. // Field by field, never `let clouds = ground_clouds[i]` whole (`cf::Deck`): // naga's Metal writer turns a whole load out of a `binding_array` of buffers // into a pointer-to-struct conversion Metal refuses, the opaque pipeline fails // to build, and the renderer halts on a black window (macOS, 2026-10-15). @if(BINDLESS) let cloud_slot = ground_indices[slot].clouds; @if(BINDLESS) let deck_shape = ground_clouds[cloud_slot].shape; @else let deck_shape = clouds_single.shape; let offset = in.world_position.xyz - view_bindings::view.world_position.xyz; let mirror_ray = reflect(-pbr_input.V, pbr_input.N); let spread = length(fwidth(cf::mirror_hit(deck_shape.w, offset, mirror_ray))); // The same cut-off the surface applies, to the same weight, so it cannot // draw a line along the ring's edge. let cloud_fade = cloud_mirror_fade(reflected * water); if cloud_fade > 0.0 { @if(BINDLESS) let deck = cf::Deck( deck_shape, ground_clouds[cloud_slot].motion, ground_clouds[cloud_slot].sun_dir, ground_clouds[cloud_slot].sun_color, ground_clouds[cloud_slot].sky_color, ground_clouds[cloud_slot].place, ground_clouds[cloud_slot].haze_color, ground_clouds[cloud_slot].light, ); @else let deck = cf::Deck( deck_shape, clouds_single.motion, clouds_single.sun_dir, clouds_single.sun_color, clouds_single.sky_color, clouds_single.place, clouds_single.haze_color, clouds_single.light, ); var cloud = cf::mirrored( deck, offset, mirror_ray, length(offset), pbr_input.material.perceptual_roughness, spread, ); cloud.a *= cloud_fade; out.color = vec4( with_clouds(out.color.rgb, pbr_input, cloud, water_sea.y, reflected, water), out.color.a, ); } out.color = main_pass_post_lighting_processing(pbr_input, out.color); // The underground layer looks through the ground (`systems::sewers`). A // screen door, not a blend: the ground stays opaque and keeps writing depth, // so the ground stack's depth ladder is untouched and nothing has to be // sorted — a pixel is either ground or a hole. Every sheet of the stack draws // the same pattern at the same pixels, so a hole goes through all of them at // once rather than opening onto the clipmap level underneath. // // At the very end, after every derivative and texture sample, because a // `discard` before them would put them in non-uniform control flow. Only in // this variant (`ground_detail::GroundKey`): the statement's mere presence // costs early depth testing. @if(GROUND_SEE_THROUGH) { let horizontal = length(in.world_position.xz - view_bindings::view.world_position.xz); let open = SEE_THROUGH * (1.0 - smoothstep(SEE_THROUGH_NEAR_M, SEE_THROUGH_FAR_M, horizontal)); let cell = vec2(in.position.xy) % vec2(4u); if bayer4(cell.x, cell.y) < open { discard; } } return out; } // How much of the ground opens near the camera. Over half, because the // pipes are the point; under all of it, because a street you cannot see is // a street you cannot place them in. @if(GROUND_SEE_THROUGH) const SEE_THROUGH: f32 = 0.6; // Horizontal metres from the camera over which the ground closes again, so // the far city reads as a city and the opening reads as looking down. @if(GROUND_SEE_THROUGH) const SEE_THROUGH_NEAR_M: f32 = 250.0; @if(GROUND_SEE_THROUGH) const SEE_THROUGH_FAR_M: f32 = 450.0; // A 4×4 ordered-dither threshold in [0, 1): the pattern with the most even // spread of holes at every density. @if(GROUND_SEE_THROUGH) fn bayer4(x: u32, y: u32) -> f32 { var m = array( 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 (m[y * 4u + x] + 0.5) / 16.0; }