git clone https://garage44.eu/jeroen/cartopolis.gitcartopolis-agent · 2026-11-28T20:28:35Z
feat(map): light the surveyed lampposts at night The light pool `street_detail::update_lamp_lights` re-aims was fed only by the Overpass street-detail layer's lamps, and only those heads glowed. The lampposts a Dutch street actually has come from the BGT or a city's register (1,781 in Groningen), are merged into the furniture mesh, and had neither: at night they were dark boxes on a dark street. - `furniture::Tone::LampHead` marks the lamp form's head with vertex alpha `LAMP_HEAD_ALPHA`; the furniture material is opaque, so the alpha means nothing else on it. The furniture group's ground material carries `DETAIL_LAMP_HEADS`, and `ground_detail.wesl` lets its emissive through only on marked vertices. `update_lamp_glow` writes the night glow into that emissive beside the layer's own lamp material. - `WorldPlaces` keeps each surveyed lamp's head (world XZ, height over its foot, from `FurnitureInstance::lamp_head`), and the pool takes the nearest of both sources, the ground added at aim time, skipping a lamp within 3 m of one already lit so a lamp in both sources takes one slot. Pool sizes are unchanged (48 desktop, 16 balanced, 0 phone).
assets/shaders/ground_detail.wesl | 11 +++
crates/cartopolis/src/systems/map/ground_detail.rs | 5 ++
crates/cartopolis/src/systems/map/map_geometry.rs | 2 +
crates/cartopolis/src/systems/map/street_detail.rs | 78 ++++++++++++++++++----
crates/cartopolis/src/systems/map/world_places.rs | 23 +++++++
crates/geo/src/furniture.rs | 43 ++++++++++--
crates/geo/src/furniture/tests.rs | 43 ++++++++++++
7 files changed, 187 insertions(+), 18 deletions(-)
diff --git a/assets/shaders/ground_detail.wesl b/assets/shaders/ground_detail.wesl
index 72335fda..3b4ca521 100644
--- a/assets/shaders/ground_detail.wesl
+++ b/assets/shaders/ground_detail.wesl
@@ -696,6 +696,17 @@ fn fragment(in: VertexOutput, @builtin(front_facing) is_front: bool) -> Fragment
// (`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<f32>(
+ 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.
//
diff --git a/crates/cartopolis/src/systems/map/ground_detail.rs b/crates/cartopolis/src/systems/map/ground_detail.rs
index 879b00fb..d55dcb5c 100644
--- a/crates/cartopolis/src/systems/map/ground_detail.rs
+++ b/crates/cartopolis/src/systems/map/ground_detail.rs
@@ -107,6 +107,11 @@ pub const DETAIL_OPTED_OUT: Vec4 = Vec4::new(0.0, 0.0, 0.0, -1.0);
/// opted out — `w` is what every check reads — and `x` says which kind.
pub const DETAIL_FOLIAGE: Vec4 = Vec4::new(1.0, 0.0, 0.0, -1.0);
+/// [`DETAIL_OPTED_OUT`] for the street furniture, whose material's emissive lights
+/// only the lamp heads: `y` says the vertex alpha masks it
+/// (`furniture::LAMP_HEAD_ALPHA`, `ground_detail.wesl`).
+pub const DETAIL_LAMP_HEADS: Vec4 = Vec4::new(0.0, 1.0, 0.0, -1.0);
+
/// How much of the measured residual slope reaches the normal.
///
/// One, and it is written down rather than left implicit: the texture is a
diff --git a/crates/cartopolis/src/systems/map/map_geometry.rs b/crates/cartopolis/src/systems/map/map_geometry.rs
index 9f0d6e89..09cc4be0 100644
--- a/crates/cartopolis/src/systems/map/map_geometry.rs
+++ b/crates/cartopolis/src/systems/map/map_geometry.rs
@@ -5187,6 +5187,8 @@ fn ground_detail_for(group: SurfaceGroup) -> GroundDetailExtension {
return GroundDetailExtension {
params: if group == SurfaceGroup::Canopy {
crate::systems::ground_detail::DETAIL_FOLIAGE
+ } else if group == SurfaceGroup::Furniture {
+ crate::systems::ground_detail::DETAIL_LAMP_HEADS
} else {
crate::systems::ground_detail::DETAIL_OPTED_OUT
},
diff --git a/crates/cartopolis/src/systems/map/street_detail.rs b/crates/cartopolis/src/systems/map/street_detail.rs
index f4771f6c..3592b8d9 100644
--- a/crates/cartopolis/src/systems/map/street_detail.rs
+++ b/crates/cartopolis/src/systems/map/street_detail.rs
@@ -2255,6 +2255,9 @@ const LAMP_LUMENS: f32 = 6_000.0;
/// Light pool radius around the camera.
const LAMP_LIGHT_RANGE_M: f32 = 160.0;
+/// How near two lamps from different sources may stand and still be two.
+const LAMP_TWIN_M: f32 = 3.0;
+
/// How many real `PointLight`s the lamp layer may keep alive. These are the
/// first clustered lights in the scene, and on the web CPU clustering is the
/// single most expensive thing in the frame — the pool is a structural cap,
@@ -2275,28 +2278,47 @@ fn night_glow(dev: &crate::resources::DevPanelState) -> f32 {
((0.18 - elevation) / 0.33).clamp(0.0, 1.0)
}
-/// Drive the lamp heads' emissive with the night factor.
+/// Drive the lamp heads' emissive with the night factor: this layer's own heads,
+/// and the surveyed lampposts' (BGT, a city's register), which are parts of the
+/// merged furniture mesh. That mesh has one material, so its emissive is let
+/// through only on the vertices `furniture::LAMP_HEAD_ALPHA` marks
+/// (`ground_detail.wesl`, `LAMP_HEADS_MASKED`).
pub fn update_lamp_glow(
dev: Res<crate::resources::DevPanelState>,
sd: Res<StreetDetail>,
+ surfaces: Res<crate::systems::map_geometry::MapSurfaces>,
mut materials: ResMut<Assets<StandardMaterial>>,
+ mut ground: ResMut<Assets<crate::systems::ground_detail::GroundMaterial>>,
mut applied: Local<Option<f32>>,
) {
+ let glow = night_glow(&dev);
+ let c = LAMP_COLOR.to_linear();
+ let emissive = LinearRgba::rgb(
+ c.red * 14.0 * glow,
+ c.green * 14.0 * glow,
+ c.blue * 14.0 * glow,
+ );
+ // The furniture material is created with the first cell that has any, so
+ // it is checked every frame rather than behind the `applied` latch.
+ if let Some(handle) = surfaces.material(SurfaceGroup::Furniture) {
+ if ground
+ .get(handle)
+ .is_some_and(|m| m.base.emissive != emissive)
+ {
+ if let Some(mut mat) = ground.get_mut(handle) {
+ mat.base.emissive = emissive;
+ }
+ }
+ }
let Some(handle) = sd.lamp_material.as_ref() else {
return;
};
- let glow = night_glow(&dev);
if applied.is_some_and(|a| (a - glow).abs() < 0.01) {
return;
}
*applied = Some(glow);
if let Some(mut mat) = materials.get_mut(handle) {
- let c = LAMP_COLOR.to_linear();
- mat.emissive = LinearRgba::rgb(
- c.red * 14.0 * glow,
- c.green * 14.0 * glow,
- c.blue * 14.0 * glow,
- );
+ mat.emissive = emissive;
}
}
@@ -2320,6 +2342,8 @@ pub fn update_lamp_lights(
),
Without<FreeCamera>,
>,
+ places: Res<crate::systems::world_places::WorldPlaces>,
+ terrain: Res<crate::systems::terrain::TerrainField>,
time: Res<Time>,
mut last_run: Local<f32>,
) {
@@ -2366,16 +2390,42 @@ pub fn update_lamp_lights(
let cam_world = anchored_xz(cam_cell, cam_tf.translation);
- // Nearest lamps within range — a partial sort over the live set.
+ // Nearest lamps within range — this layer's, and the surveyed lampposts in
+ // the furniture (BGT, a city's register), which are most of a Dutch
+ // street's and whose heights are over their own foot until the ground is
+ // added here.
+ let ground = terrain.snapshot();
+ let in_range = |p: Vec3| {
+ let d2 = Vec2::new(p.x, p.z).distance_squared(cam_world);
+ (d2 < LAMP_LIGHT_RANGE_M * LAMP_LIGHT_RANGE_M).then_some((d2, p))
+ };
let mut near: Vec<(f32, Vec3)> = sd
.lamp_iter()
- .filter_map(|p| {
- let d2 = Vec2::new(p.x, p.z).distance_squared(cam_world);
- (d2 < LAMP_LIGHT_RANGE_M * LAMP_LIGHT_RANGE_M).then_some((d2, *p))
- })
+ .copied()
+ .chain(
+ places
+ .lamp_heads()
+ .map(|h| Vec3::new(h.x, ground.at(h.x, h.z) + h.y, h.z)),
+ )
+ .filter_map(in_range)
.collect();
near.sort_by(|a, b| a.0.total_cmp(&b.0));
- near.truncate(pool);
+ // A lamp OpenStreetMap maps and the survey measured is one lamp, a metre
+ // or two apart in the two sources; a second light on it would only be
+ // a slot taken from the next street. Quadratic, over at most a pool's
+ // worth of kept lamps.
+ let mut kept: Vec<(f32, Vec3)> = Vec::with_capacity(pool);
+ for (d2, p) in near {
+ if kept.len() == pool {
+ break;
+ }
+ if kept.iter().all(|(_, q)| {
+ Vec2::new(p.x, p.z).distance_squared(Vec2::new(q.x, q.z)) > LAMP_TWIN_M * LAMP_TWIN_M
+ }) {
+ kept.push((d2, p));
+ }
+ }
+ let near = kept;
let grid = Grid::new(WORLD_CELL_EDGE, WORLD_SWITCH_THRESHOLD);
let lit = if glow > 0.05 { near.len() } else { 0 };
diff --git a/crates/cartopolis/src/systems/map/world_places.rs b/crates/cartopolis/src/systems/map/world_places.rs
index 4d6ad75b..36e87064 100644
--- a/crates/cartopolis/src/systems/map/world_places.rs
+++ b/crates/cartopolis/src/systems/map/world_places.rs
@@ -364,6 +364,11 @@ pub struct WorldPlaces {
/// in anchored world XZ: what `systems::splashes` sprays, sounds and
/// splashes a swimmer at.
jets: HashMap<(i64, i64), Vec<Vec2>>,
+ /// The surveyed lampposts' heads per cell: anchored world XZ in `x`/`z`,
+ /// and in `y` the head's height over the lamp's own foot, since the ground
+ /// under it may land after the cell does. What `street_detail`'s light pool
+ /// aims at, beside that layer's own lamps.
+ lamps: HashMap<(i64, i64), Vec<Vec3>>,
/// The parked cars per cell, in anchored world XZ: what a "Drive this car"
/// prompt gets into (`systems::driving`).
cars: HashMap<(i64, i64), Vec<ParkedCar>>,
@@ -399,9 +404,13 @@ impl WorldPlaces {
let mut interactables: Vec<Interactable> = Vec::new();
let mut props: Vec<(Vec2, f32)> = Vec::new();
let mut jets: Vec<Vec2> = Vec::new();
+ let mut lamps: Vec<Vec3> = Vec::new();
for f in &places.furniture {
let pos = origin + Vec2::new(f.x, f.z);
+ if let Some([dx, up, dz]) = f.lamp_head() {
+ lamps.push(Vec3::new(pos.x + dx, up, pos.y + dz));
+ }
// A fountain standing in open water is a jet in a pond, not a basin on
// a square (`furniture::FurnitureInstance::over_water`). Neither half of
// the prop treatment survives the move: a collider out there is an
@@ -499,9 +508,20 @@ impl WorldPlaces {
} else {
self.jets.insert(cell, jets);
}
+ if lamps.is_empty() {
+ self.lamps.remove(&cell);
+ } else {
+ self.lamps.insert(cell, lamps);
+ }
self.revision = self.revision.wrapping_add(1);
}
+ /// Every surveyed lamppost's head across the loaded cells, as
+ /// [`Self::lamps`] holds them: world XZ, and the height over its foot.
+ pub fn lamp_heads(&self) -> impl Iterator<Item = Vec3> + '_ {
+ self.lamps.values().flatten().copied()
+ }
+
/// Every loaded fountain jet, anchored world XZ.
pub fn jets(&self) -> impl Iterator<Item = Vec2> + '_ {
self.jets.values().flatten().copied()
@@ -510,6 +530,7 @@ impl WorldPlaces {
pub fn remove_cell(&mut self, cell: &(i64, i64)) {
let had = self.cells.remove(cell).is_some()
| self.jets.remove(cell).is_some()
+ | self.lamps.remove(cell).is_some()
| self.props.remove(cell).is_some()
| self.addresses.remove(cell).is_some()
| self.paving.remove(cell).is_some()
@@ -526,11 +547,13 @@ impl WorldPlaces {
&& self.addresses.is_empty()
&& self.paving.is_empty()
&& self.jets.is_empty()
+ && self.lamps.is_empty()
{
return;
}
self.cells.clear();
self.jets.clear();
+ self.lamps.clear();
self.props.clear();
self.addresses.clear();
self.paving.clear();
diff --git a/crates/geo/src/furniture.rs b/crates/geo/src/furniture.rs
index f03df96a..0d0292ad 100644
--- a/crates/geo/src/furniture.rs
+++ b/crates/geo/src/furniture.rs
@@ -906,6 +906,36 @@ pub enum Tone {
/// this table's. The one part of the scheme that varies within a form, and the
/// reason is in that field's docs.
Paint,
+ /// A street light's head: [`Self::Roof`]'s colour by day, and the one part
+ /// of a furniture mesh that glows at night. It is marked in the vertex
+ /// alpha ([`LAMP_HEAD_ALPHA`]), because the whole layer is one material.
+ LampHead,
+}
+
+/// The vertex alpha a [`Tone::LampHead`] part carries, where every other part
+/// carries 1. `ground_detail.wesl` lets the furniture material's emissive
+/// through only below 0.75, so this is the whole of the night switch's reach:
+/// the material is opaque and the alpha means nothing else on it.
+pub const LAMP_HEAD_ALPHA: f32 = 0.5;
+
+/// Where a lamppost's head is, in the instance's own frame: metres right, up
+/// and forward of its foot, before yaw. The light that falls from it hangs
+/// under this point (`street_detail::update_lamp_lights`).
+pub const LAMP_HEAD_AT: [f32; 3] = [0.0, 3.90, 0.52];
+
+impl FurnitureInstance {
+ /// A lamppost's head relative to its foot, yaw applied: `[dx, up, dz]` in
+ /// the tile frame. `None` for anything that is not a lamp.
+ #[must_use]
+ pub fn lamp_head(&self) -> Option<[f32; 3]> {
+ if self.kind != FurnitureKind::Lamp {
+ return None;
+ }
+ let (sn, cs) = self.yaw.sin_cos();
+ let [x, y, z] = LAMP_HEAD_AT;
+ let [wx, wz] = place_ground(0.0, 0.0, (sn, cs), x, z);
+ Some([wx, y, wz])
+ }
}
impl Tone {
@@ -913,7 +943,7 @@ impl Tone {
match self {
Self::Wood => palette::FURNITURE_WOOD,
Self::Metal => palette::FURNITURE_METAL,
- Self::Roof => palette::FURNITURE_ROOF,
+ Self::Roof | Self::LampHead => palette::FURNITURE_ROOF,
Self::Stone => palette::FURNITURE_STONE,
Self::Brick => palette::QUAY_BRICK,
Self::Water => palette::WATER,
@@ -974,7 +1004,7 @@ pub(crate) const fn part(at: [f32; 3], half: [f32; 3], tone: Tone) -> Part {
/// is not implicitly promoted to `'static`, so the literals would be temporaries
/// rebuilt on every lookup, which is once per object per cell.
mod form {
- use super::{part, Part, Tone::*};
+ use super::{part, Part, Tone::*, LAMP_HEAD_AT};
pub const BENCH: [Part; 4] = [
part([0.0, 0.45, 0.0], [0.90, 0.03, 0.22], Wood),
@@ -1061,7 +1091,7 @@ mod form {
pub const LAMP: [Part; 3] = [
part([0.0, 2.00, 0.0], [0.06, 2.00, 0.06], Metal),
part([0.0, 3.98, 0.28], [0.05, 0.04, 0.28], Metal),
- part([0.0, 3.90, 0.52], [0.14, 0.06, 0.22], Roof),
+ part(LAMP_HEAD_AT, [0.14, 0.06, 0.22], LampHead),
];
/// A 2 m street rack of the common Dutch kind: two rails a wheel's width apart
/// at front-wheel height, on a post at each end. Long axis on x, like the
@@ -1443,7 +1473,12 @@ fn build_furniture_meshes_with(
Tone::Paint => paint_multiplier(item.paint),
tone => tone.multiplier(),
};
- let tint = [m[0] * item.luma, m[1] * item.luma, m[2] * item.luma, 1.0];
+ let alpha = if p.tone == Tone::LampHead {
+ LAMP_HEAD_ALPHA
+ } else {
+ 1.0
+ };
+ let tint = [m[0] * item.luma, m[1] * item.luma, m[2] * item.luma, alpha];
let raised;
let part = if up == 0.0 {
p
diff --git a/crates/geo/src/furniture/tests.rs b/crates/geo/src/furniture/tests.rs
index 27962805..e90d58a5 100644
--- a/crates/geo/src/furniture/tests.rs
+++ b/crates/geo/src/furniture/tests.rs
@@ -804,3 +804,46 @@ fn the_tones_are_muted_against_the_paper() {
);
}
}
+
+/// The head a lamp's light hangs under is the head that is drawn, turned with
+/// the post, and it is the only part of the mesh marked to glow.
+#[test]
+fn a_lamps_head_is_where_its_light_is_aimed() {
+ let lamp = FurnitureInstance {
+ x: 4.0,
+ z: -3.0,
+ yaw: 0.9,
+ kind: FurnitureKind::Lamp,
+ luma: 1.0,
+ paint: palette::FURNITURE,
+ over_water: false,
+ hull_only: false,
+ };
+ let meshes = build_furniture_meshes(&[lamp], None);
+ let mesh = &meshes[0];
+ let glowing: Vec<[f32; 3]> = mesh
+ .positions
+ .iter()
+ .zip(&mesh.colors)
+ .filter(|(_, c)| c[3] == LAMP_HEAD_ALPHA)
+ .map(|(p, _)| *p)
+ .collect();
+ // One box: 24 vertices, and nothing else in the lamp carries the mark.
+ assert_eq!(glowing.len(), 24);
+ let n = glowing.len() as f32;
+ let centre = glowing.iter().fold([0.0; 3], |a, p| {
+ [a[0] + p[0] / n, a[1] + p[1] / n, a[2] + p[2] / n]
+ });
+ let [dx, up, dz] = lamp.lamp_head().expect("a lamp has a head");
+ for (got, want) in centre.iter().zip([lamp.x + dx, up, lamp.z + dz]) {
+ assert!(
+ (got - want).abs() < 1e-4,
+ "head at {centre:?}, light aimed at {want}"
+ );
+ }
+ let bench = FurnitureInstance {
+ kind: FurnitureKind::Bench,
+ ..lamp
+ };
+ assert!(bench.lamp_head().is_none());
+}