git clone https://garage44.eu/jeroen/cartopolis.gitcartopolis-agent · 2026-11-28T20:28:43Z
feat(map): sink a laid street's joints with a parallax march A brick, paver, tile, sett or slab street was drawn with its joints printed on a flat sheet: the bevel tilt lights a stone's edges but hides nothing, so from a walker's eye the pattern read as a picture of stones. `ground_detail.wesl` now marches each joint pixel's view ray down into the joints' height field (eight steps and a secant) and draws the look at the point it hits, so the stone beyond a joint hides its far side. The ground is horizontal, so the step across it is `V.xz / V.y` and no tangent frame is needed (tangents on the ground meshes were declined in `surface-finish-and-occlusion.md`). Depth is not written, so the depth prepass and the shadows still see the plain surface. - The five lattices move into `laid_cell`, which the look and the march both read, so the march finds the joints the colour draws. - A face pixel is the top of the field and marches nothing; the march also fades out with the stones' sharpness (`pav_blur`), so it costs nothing past a few pixels a stone. The view ray is held at a rise of 0.2 so a centimetre's joint cannot throw the pattern along the street. - `the_far_tone_is_the_shaders` now reads `paving_look`'s arms, not the first `case 2u` in the file, which is `laid_cell`'s.
assets/shaders/ground_detail.wesl | 232 ++++++++++++++++++++++++++++++--------
crates/geo/src/paving.rs | 3 +
2 files changed, 190 insertions(+), 45 deletions(-)
diff --git a/assets/shaders/ground_detail.wesl b/assets/shaders/ground_detail.wesl
index 3b4ca521..1ad27f46 100644
--- a/assets/shaders/ground_detail.wesl
+++ b/assets/shaders/ground_detail.wesl
@@ -465,6 +465,178 @@ fn gutter(look_in: PavingLook, p: vec2<f32>, seam_m: f32, tarmac: bool, metres_p
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<f32>,
+ f: vec2<f32>,
+ size: vec2<f32>,
+ edge: f32,
+ joint: f32,
+ unit: f32,
+}
+
+fn laid_cell(pattern: u32, p: vec2<f32>) -> 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<f32>(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<f32>(f.x * 0.5, f.y); }
+ case 1u: { s.cell = c - vec2<f32>(1.0, 0.0); s.edge = min(1.0 - f.x, min(f.y, 1.0 - f.y)); s.f = vec2<f32>(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<f32>(f.x, f.y * 0.5); }
+ default: { s.cell = c - vec2<f32>(0.0, 1.0); s.edge = min(1.0 - f.y, min(f.x, 1.0 - f.x)); s.f = vec2<f32>(f.x, 0.5 + f.y * 0.5); }
+ }
+ s.size = select(vec2<f32>(0.1, 0.2), vec2<f32>(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<f32>(p.x / 0.2 + 0.5 * course, p.y / 0.1);
+ s.cell = floor(q);
+ s.f = fract(q);
+ s.size = vec2<f32>(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<f32>(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<f32>(row, 7.0));
+ let q = vec2<f32>(p.x / 0.125 + shift, p.y / 0.125);
+ s.cell = floor(q);
+ s.f = fract(q);
+ s.size = vec2<f32>(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>) -> 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<f32>, view: vec3<f32>, metres_per_px: f32) -> vec2<f32> {
+ 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.
@@ -486,67 +658,35 @@ fn paving_look(pattern: u32, p: vec2<f32>, metres_per_px: f32, sharpest_px: f32)
}
case 2u: {
// Poured concrete in 2.5 m slabs with sawn joints.
- let q = p / 2.5;
- let f = fract(q);
- let edge = min(min(f.x, 1.0 - f.x), min(f.y, 1.0 - f.y));
- look = stone(pav_hash(floor(q)), f, vec2<f32>(2.5, 2.5), edge, 0.006, 0.1, pav_blur(2.5, metres_per_px), 0.99);
+ 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<f32>(0.0);
look.tone *= 1.0 + 0.06 * (pav_noise(p / 0.7) - 0.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));
- var id = c;
- var edge = 1.0;
- var g = f;
- switch d {
- case 0u: { edge = min(f.x, min(f.y, 1.0 - f.y)); g = vec2<f32>(f.x * 0.5, f.y); }
- case 1u: { id = c - vec2<f32>(1.0, 0.0); edge = min(1.0 - f.x, min(f.y, 1.0 - f.y)); g = vec2<f32>(0.5 + f.x * 0.5, f.y); }
- case 2u: { edge = min(f.y, min(f.x, 1.0 - f.x)); g = vec2<f32>(f.x, f.y * 0.5); }
- default: { id = c - vec2<f32>(0.0, 1.0); edge = min(1.0 - f.y, min(f.x, 1.0 - f.x)); g = vec2<f32>(f.x, 0.5 + f.y * 0.5); }
- }
- look = stone(pav_hash(id), g, select(vec2<f32>(0.1, 0.2), vec2<f32>(0.2, 0.1), d < 2u), edge, 0.08, 0.32, pav_blur(0.1, metres_per_px), 0.96);
+ 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(id + vec2<f32>(17.0, 3.0)) - 0.5;
+ let hue = pav_hash(s.cell + vec2<f32>(17.0, 3.0)) - 0.5;
look.tone *= vec3<f32>(1.0 + 0.1 * hue, 1.0, 1.0 - 0.12 * hue);
}
case 4u: {
- // Concrete pavers, 20 × 10 cm, each course half a stone over.
- let course = floor(p.y / 0.1);
- let q = vec2<f32>(p.x / 0.2 + 0.5 * course, p.y / 0.1);
- let f = fract(q);
- let edge = min(min(f.x, 1.0 - f.x) * 2.0, min(f.y, 1.0 - f.y));
- look = stone(pav_hash(floor(q)), f, vec2<f32>(0.2, 0.1), edge, 0.08, 0.14, pav_blur(0.1, metres_per_px), 0.97);
+ 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: {
- // Pavement tiles, 30 × 30 cm, butted close.
- let q = p / 0.3125;
- let f = fract(q);
- let edge = min(min(f.x, 1.0 - f.x), min(f.y, 1.0 - f.y));
- look = stone(pav_hash(floor(q)), f, vec2<f32>(0.3125, 0.3125), edge, 0.05, 0.2, pav_blur(0.3125, metres_per_px), 0.97);
+ 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: {
- // 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<f32>(row, 7.0));
- let q = vec2<f32>(p.x / 0.125 + shift, p.y / 0.125);
- let f = fract(q);
- let edge = min(min(f.x, 1.0 - f.x), min(f.y, 1.0 - f.y));
- look = stone(pav_hash(floor(q)), f, vec2<f32>(0.125, 0.125), edge, 0.14, 0.4, pav_blur(0.125, metres_per_px), 0.93);
+ 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 += (f - vec2<f32>(0.5, 0.5)) * 0.25 * (1.0 - pav_blur(0.125, metres_per_px));
+ look.tilt += (s.f - vec2<f32>(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.
@@ -850,7 +990,9 @@ fn fragment(in: VertexOutput, @builtin(front_facing) is_front: bool) -> Fragment
}
if paving_code > 0.5 {
- let look = paving_look(u32(round(paving_code)), in.world_position.xz, metres_per_px, sharpest_px);
+ 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<f32>(
mix(pbr_input.material.base_color.rgb * look.tone, look.fill.rgb, look.fill.a),
pbr_input.material.base_color.a,
diff --git a/crates/geo/src/paving.rs b/crates/geo/src/paving.rs
index 7cd7cc86..e782edb5 100644
--- a/crates/geo/src/paving.rs
+++ b/crates/geo/src/paving.rs
@@ -856,6 +856,9 @@ mod tests {
#[test]
fn the_far_tone_is_the_shaders() {
let wgsl = include_str!("../../../assets/shaders/ground_detail.wesl");
+ // The arms of `paving_look`, not the lattices' (`laid_cell`) or the
+ // joints' depths, which switch on the same numbers.
+ let wgsl = &wgsl[wgsl.find("fn paving_look(").expect("paving_look")..];
for (p, name) in [
(pattern::CONCRETE, "case 2u"),
(pattern::BRICK, "case 3u"),