garage44.eu/jeroen/cartopolis

logclone: git clone https://garage44.eu/jeroen/cartopolis.git

cartopolis-agent · 2026-11-28T19:25:58Z

feat(buildings): weather a wall by its century, and grow moss on the oldest

Every building before 1920 was one era to the facade shader, so a
warehouse of 1635 weathered exactly like a house of 1910. The BAG year
now also gives an antiquity (from 1880, 1800, 1650), carried in the
three era codes no era used, so the facade alpha gains no bit. The
shader reads era 5-7 as era 1 for everything else, raises the damp line
and streaks by 0.15 a step, and on walls before 1880 grows a green film
of algae and moss over the lower storey and dark run-off streaks down
the face, three times as much on a north wall as on a south one.

A test pins the year grading and that it survives the alpha channel
with the GPU's interpolation wobble.
assets/shaders/building_windows.wesl               | 54 ++++++++++++--
 .../cartopolis/src/systems/map/building_windows.rs | 82 +++++++++++++++++++++-
 crates/cartopolis/src/systems/map/lod22.rs         |  1 +
 3 files changed, 130 insertions(+), 7 deletions(-)

diff --git a/assets/shaders/building_windows.wesl b/assets/shaders/building_windows.wesl
index b9d19c45..d9f49267 100644
--- a/assets/shaders/building_windows.wesl
+++ b/assets/shaders/building_windows.wesl
@@ -577,9 +577,12 @@ fn wall_relief(
 /// 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.
-fn era_weathering(era: u32) -> f32 {
+///
+/// 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; }
+        case 1u: { return 0.6 + 0.15 * f32(antiquity); }
         case 2u: { return 0.4; }
         case 3u: { return 0.45; }
         case 4u: { return 0.15; }
@@ -655,6 +658,30 @@ fn weathered(
     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.4, 0.0, 0.85);
+    var out = mix(c, c * vec3<f32>(0.5, 0.72, 0.38), film);
+    let run = smoothstep(0.58, 0.88, value_noise(vec2<f32>(u / 0.3, v / 5.0) + vec2<f32>(37.0, 4.0)));
+    out = mix(out, out * vec3<f32>(0.6, 0.68, 0.52), run * moss * mix(0.4, 0.8, north) * fade);
+    return out;
+}
+
 /// Height of a facade's plinth — the darker, harder-wearing course it stands on.
 const PLINTH_M: f32 = 0.5;
 
@@ -1506,7 +1533,11 @@ fn fragment(in: VertexOutput, @builtin(front_facing) is_front: bool) -> Fragment
     let occupancy_raw = select(occupancy_packed, 0.0, neglected);
     let code = u32(code_f);
     let eave = f32((code >> 4u) & 511u) * 0.2;
-    let era = (code >> 1u) & 7u;
+    // 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`).
@@ -1708,7 +1739,7 @@ fn fragment(in: VertexOutput, @builtin(front_facing) is_front: bool) -> Fragment
         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) * mix(0.5, 1.3, hash2(bkey + vec2<f32>(53.0, 17.0)));
+        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)),
@@ -1728,6 +1759,21 @@ fn fragment(in: VertexOutput, @builtin(front_facing) is_front: bool) -> Fragment
                 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 {
diff --git a/crates/cartopolis/src/systems/map/building_windows.rs b/crates/cartopolis/src/systems/map/building_windows.rs
index df09ce88..d0d68925 100644
--- a/crates/cartopolis/src/systems/map/building_windows.rs
+++ b/crates/cartopolis/src/systems/map/building_windows.rs
@@ -47,7 +47,8 @@ use cartopolis_materials::building_windows::{BLOCK_M, BLOCK_PERIOD, WINDOW_LATTI
 ///
 /// `alpha = occupancy + 4 × code`, occupancy quantised to sixteenths in [0, 4),
 /// and `code = shop | era << 1 | eave << 4 | material << 13 | graffiti << 15`, eave
-/// in 20 cm steps (0 = unknown, up to 102 m), material two bits (0 = unmeasured)
+/// in 20 cm steps (0 = unknown, up to 102 m), era 5–7 being era 1 at an
+/// [`antiquity`] of 1–3, material two bits (0 = unmeasured)
 /// and graffiti one, set per wall face. The largest alpha is 2¹⁸, and a sixteenth
 /// of that is still exact in an `f32`'s 24 bits. **Code 0 is "nothing known"**, and `1.0` —
 /// what every building off the BAG, and the OSM lane, has always carried —
@@ -79,6 +80,9 @@ pub struct Facade {
     /// This wall stands on a narrow street and is painted (`systems::graffiti`).
     /// Per wall face, not per building: set on the faces it holds for.
     pub graffiti: bool,
+    /// How far before 1880 it was built ([`antiquity`]), 0 to 3: era 1 spans
+    /// centuries, and a warehouse of 1650 has had them to weather in.
+    pub antiquity: u8,
 }
 
 /// The shader's code for a measured roof covering
@@ -168,6 +172,20 @@ pub fn era(year: Option<u16>) -> u8 {
     }
 }
 
+/// How far before 1880 a construction year is, for the weathering: 0 since
+/// 1880 (or unknown), 1 from 1800, 2 from 1650, 3 before that. Era 1 alone
+/// holds the whole of it, so it rides in the era's three unused codes: the
+/// shader reads era 5–7 as era 1 at antiquity 1–3 ([`pack_facade`]).
+#[must_use]
+pub fn antiquity(year: Option<u16>) -> u8 {
+    match year {
+        Some(y) if y < 1650 => 3,
+        Some(y) if y < 1800 => 2,
+        Some(y) if y < 1880 => 1,
+        _ => 0,
+    }
+}
+
 const EAVE_STEP_M: f32 = 0.2;
 const EAVE_MAX_STEPS: u32 = 511;
 /// The occupancy values that carry a [`Condition`]. See [`Facade`].
@@ -185,8 +203,14 @@ pub fn pack_facade(f: Facade) -> f32 {
     let eave = f.eave_m.map_or(0, |e| {
         ((e / EAVE_STEP_M).round().max(1.0) as u32).min(EAVE_MAX_STEPS)
     });
+    // Only era 1 has an antiquity, and it takes the codes no era uses.
+    let era = if f.era == 1 && f.antiquity > 0 {
+        4 + f.antiquity.min(3)
+    } else {
+        f.era.min(4)
+    };
     let code = u32::from(f.shop)
-        | (u32::from(f.era.min(7)) << 1)
+        | (u32::from(era) << 1)
         | (eave << 4)
         | (u32::from(f.material.min(3)) << 13)
         | (u32::from(f.graffiti) << 15);
@@ -207,10 +231,12 @@ fn unpack_facade(a: f32) -> Facade {
     } else {
         (raw, Condition::Kept)
     };
+    let era = ((code >> 1) & 7) as u8;
     Facade {
         occupancy,
         shop: code & 1 == 1,
-        era: ((code >> 1) & 7) as u8,
+        era: if era > 4 { 1 } else { era },
+        antiquity: era.saturating_sub(4),
         eave_m: (eave > 0).then(|| eave as f32 * EAVE_STEP_M),
         condition,
         material: ((code >> 13) & 3) as u8,
@@ -687,6 +713,7 @@ mod tests {
                     // The largest code there is: every bit of it set.
                     material: 3,
                     graffiti: true,
+                    antiquity: 0,
                 };
                 let a = super::pack_facade(f);
                 for wobble in [-0.02, 0.0, 0.02] {
@@ -727,6 +754,7 @@ mod tests {
             condition: super::Condition::Kept,
             material: 3,
             graffiti: false,
+            antiquity: 0,
         };
         for covering in 0..=10u8 {
             let a = super::pack_roof(f, covering);
@@ -750,6 +778,51 @@ mod tests {
         assert_eq!(super::unpack_facade(super::pack_roof(f, 0)).eave_m, None);
     }
 
+    /// A building's age reaches the shader in the era's spare codes: a warehouse
+    /// of 1650 and a house of 1910 are both era 1, and only the antiquity tells
+    /// them apart. Every other era, and an unknown year, carries none.
+    #[test]
+    fn a_buildings_age_survives_the_alpha_channel() {
+        use super::{antiquity, era};
+        for (year, want) in [
+            (Some(1590), 3),
+            (Some(1649), 3),
+            (Some(1650), 2),
+            (Some(1799), 2),
+            (Some(1800), 1),
+            (Some(1879), 1),
+            (Some(1880), 0),
+            (Some(1910), 0),
+            (Some(1965), 0),
+            (Some(2021), 0),
+            (None, 0),
+        ] {
+            assert_eq!(antiquity(year), want, "{year:?}");
+            let f = super::Facade {
+                occupancy: 0.75,
+                era: era(year),
+                eave_m: Some(14.0),
+                antiquity: antiquity(year),
+                ..Default::default()
+            };
+            for wobble in [-0.02, 0.0, 0.02] {
+                let back = super::unpack_facade(super::pack_facade(f) + wobble);
+                assert_eq!(
+                    (back.era, back.antiquity),
+                    (f.era, want),
+                    "{year:?} at {wobble}"
+                );
+            }
+        }
+        // An antiquity on a later era is not a thing; it packs as the era alone.
+        let late = super::Facade {
+            era: 3,
+            antiquity: 2,
+            ..Default::default()
+        };
+        assert_eq!(super::unpack_facade(super::pack_facade(late)).antiquity, 0);
+    }
+
     #[test]
     fn a_facade_survives_the_alpha_channel() {
         for f in [
@@ -761,6 +834,7 @@ mod tests {
                 condition: super::Condition::Kept,
                 material: 1,
                 graffiti: false,
+                antiquity: 0,
             },
             super::Facade {
                 occupancy: 0.25,
@@ -770,6 +844,7 @@ mod tests {
                 condition: super::Condition::Kept,
                 material: 0,
                 graffiti: false,
+                antiquity: 0,
             },
             super::Facade {
                 occupancy: 0.5,
@@ -779,6 +854,7 @@ mod tests {
                 condition: super::Condition::Kept,
                 material: 2,
                 graffiti: false,
+                antiquity: 0,
             },
         ] {
             let back = super::unpack_facade(super::pack_facade(f));
diff --git a/crates/cartopolis/src/systems/map/lod22.rs b/crates/cartopolis/src/systems/map/lod22.rs
index 49be8ba3..5aacbeec 100644
--- a/crates/cartopolis/src/systems/map/lod22.rs
+++ b/crates/cartopolis/src/systems/map/lod22.rs
@@ -2271,6 +2271,7 @@ fn build_lod22_from_body(
             }),
             material: walled.map_or(0, |f| crate::systems::building_windows::wall_material(f.m)),
             graffiti: false,
+            antiquity: crate::systems::building_windows::antiquity(fact.and_then(|f| f.year)),
         };
         let lit = crate::systems::building_windows::pack_facade(facade);
         // A roof's vertices carry the occupancy and, where measured, the