git clone https://garage44.eu/jeroen/cartopolis.git.. /crates/materials/src/building_windows.rs
//! The LoD2.2 buildings' material: the window pattern on their walls, and the
//! constants its defaults are built from. The facade packing and the clock that
//! drives it are `cartopolis::systems::building_windows`.
use bevy::mesh::{MeshVertexAttribute, MeshVertexBufferLayoutRef};
use bevy::pbr::{
ExtendedMaterial, MaterialExtension, MaterialExtensionKey, MaterialExtensionPipeline,
};
use bevy::prelude::*;
use bevy::render::render_resource::{
AsBindGroup, RenderPipelineDescriptor, ShaderType, SpecializedMeshPipelineError, VertexFormat,
};
use bevy::render::storage::ShaderBuffer;
use bevy::shader::ShaderRef;
/// The LoD2.2 buildings' material: the same `StandardMaterial` the lane always
/// used, plus the window pattern.
pub type BuildingMaterial = ExtendedMaterial<StandardMaterial, BuildingWindows>;
/// Column pitch and floor pitch, in metres.
///
/// **Both must divide `cartopolis::grid::WORLD_CELL_EDGE` exactly**, and this is not a taste:
/// the shader reads `world_position`, which jumps by a whole cell edge when
/// big_space recentres the floating origin, and a lattice that does not divide
/// the jump slides the whole city's windows sideways as you fly. 10,000 / 2.5 =
/// 4,000 and 10,000 / 3.125 = 3,200, both whole, and both divisible by the
/// shader's `HASH_PERIOD` of 32 — which is the other half of the same
/// requirement, since the per-window hash is taken on the index.
///
/// 2.5 m between windows and 3.125 m between floors are both close enough to the
/// real thing (`osm_buildings::STOREY_HEIGHT` is 3.0) that nobody will measure
/// the difference off a facade.
pub const WINDOW_LATTICE: Vec2 = Vec2::new(2.5, 3.125);
/// Pane size, metres. Narrower and shorter than the lattice, so the wall shows
/// between the windows rather than the windows being the wall.
pub const WINDOW_SIZE: Vec2 = Vec2::new(1.35, 1.55);
/// Emissive gain at full night.
///
/// **Measured down from 1.6 on 2026-08-26**, which lit Groningen to the horizon
/// like a stadium. Windows are a warm point in a dark street, not a light source;
/// bloom is on, and at the old value every facade in the city bloomed into a
/// single sheet. The capture that showed it is the reason this is a number and
/// not a guess.
const NIGHT_GAIN: f32 = 0.62;
/// A window's colour by day, as a multiplier on the wall's own (untextured)
/// colour.
///
/// It was 0.80, deliberately mild, while the facades were flat cartographic
/// tones and a dark grid was the loudest thing on the street. Once the walls
/// carried brick and render (2026-09-30) a pane that mild read as a bricked-up
/// opening at street level; glass is dark, and what lightens it is the sky it
/// reflects, which the glass roughness and reflectance supply.
const DAY_TINT: f32 = 0.35;
/// No windows below this height above the world's zero.
///
/// A fragment this low is a plinth, a garden wall or the side of a shed, and
/// lighting those reads as a mistake rather than as a window. It is measured
/// from the world's ground rather than each building's base for the reason the
/// shader documents — it cannot see the base.
const FIRST_SILL_M: f32 = 2.2;
/// The lit colour, linear RGB. Warm, and the same hue
/// `sky::update_window_glow` uses for the other lane, so a street that straddles
/// the 3DBAG coverage edge does not change colour across it.
const LIT_COLOR: Vec3 = Vec3::new(1.0, 0.72, 0.34);
/// Size of the block the *occupancy* hash is taken on, in metres, and how many
/// blocks it repeats over.
///
/// A per-window hash alone gives every building in the city the same fraction of
/// lit windows, which is what made the first night capture read as decoration
/// rather than as a city: real streets have whole dark buildings standing between
/// the lit ones — an office after hours, an empty flat, a warehouse. This is a
/// second, coarser hash on roughly one building's footprint that decides how
/// *lively* the block is, and the window hash is then tested against it.
///
/// 25 m divides `WORLD_CELL_EDGE` into 400 steps and 400 is 25 periods of 16, so
/// it survives a floating-origin jump on the same terms the window lattice does —
/// see [`WINDOW_LATTICE`], and do not change either number without redoing that
/// arithmetic.
pub const BLOCK_M: f32 = 25.0;
/// How many blocks the occupancy hash repeats over. See [`BLOCK_M`].
pub const BLOCK_PERIOD: f32 = 16.0;
/// Which variant of the building shader a material compiles.
///
/// One flag: [`BuildingKey::cutaway`], the variant that can `discard` — the
/// ghosted building's walls (`systems::cutaway`) and the screen-door holes in
/// scaffold netting and site fences. A *variant* rather than a uniform branch
/// for the reason `ground_detail::GroundKey` gives: a fragment shader that
/// contains `discard` loses early depth testing for every draw it is used in,
/// whether or not it ever takes the branch, and this one is the most expensive
/// fragment shader on the largest geometry in the app. So the shells compile
/// without it except while somebody is indoors, and the netting, which always
/// needs it, is meshed onto the other material (`lod22`'s inside pieces).
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct BuildingKey {
pub cutaway: bool,
}
impl From<&BuildingWindows> for BuildingKey {
fn from(e: &BuildingWindows) -> Self {
Self {
cutaway: e.screen_door || e.ghost.x > 0.5,
}
}
}
/// Uniforms for `shaders/building_windows.wesl`.
#[derive(Asset, AsBindGroup, Reflect, Clone, Debug)]
// One uniform buffer, not one per field: see the shader's `Facade` for the
// browser limit seven of them broke.
#[uniform(100, BuildingWindowsGpu)]
#[bind_group_data(BuildingKey)]
pub struct BuildingWindows {
/// x: column pitch (m). y: floor pitch (m). z: pane width. w: pane height.
pub lattice: Vec4,
/// x: night factor, 0 by day → 1 after dusk. y: emissive gain at full night.
/// z: the daylight darkening. w: the first sill's height.
pub glow: Vec4,
/// x: occupancy block size (m). y: how many blocks it repeats over.
/// z, w: the fraction of windows lit in the deadest and the liveliest block.
pub block: Vec4,
/// rgb: the lit colour, linear. a unused.
pub lit: Vec4,
/// x: how many of [`Self::ghost_ring`]'s points are live, 0 for nothing
/// ghosted. y, z, w unused.
///
/// The ghosted building is the one the avatar is standing in: its **walls**
/// are discarded so you can see the room and the avatar behind them, and
/// `systems::building_select` draws a translucent shell where they were. The
/// roof is kept — see the shader.
///
/// A uniform rather than a per-vertex key for the reason this whole module
/// exists: four bytes a vertex on the largest geometry in the app, carried
/// forever, to serve a state that is off almost all of the time.
pub ghost: Vec4,
/// The ghost ring's bounding box in render-space XZ — `(min x, min z, max x,
/// max z)`. The broad phase, and not optional: without it every wall fragment
/// in the city walks the polygon while anything is ghosted.
pub ghost_bounds: Vec4,
/// The ghost ring in render-space XZ, two points per element (`xy` then `zw`).
///
/// **Render space, so it is rewritten every frame** — under `big_space` the
/// origin jumps a whole `WORLD_CELL_EDGE` as the camera crosses a grid cell,
/// which is the trap this file's `WINDOW_LATTICE` note is entirely about. A
/// ring is 32 floats and there is one material; paying for it once a frame is
/// cheaper than any arrangement that makes the shader solve it.
pub ghost_ring: [Vec4; GHOST_RING_VEC4S],
/// The cloud deck ([`CLOUD_FIELD_BUFFER`](crate::sky::CLOUD_FIELD_BUFFER)),
/// for the clouds' shadows on the walls and roofs (`cloud_field::sun_shadow`)
/// — the one buffer the ground's materials bind for theirs.
#[storage(102, read_only, visibility(fragment))]
pub clouds: Handle<ShaderBuffer>,
/// The graffiti sheet the walls of narrow streets are painted from
/// ([`GRAFFITI_SHEET`]; `cartopolis::systems::graffiti`).
#[texture(103)]
#[sampler(104)]
pub graffiti: Handle<Image>,
/// Always compile the [`BuildingKey::cutaway`] variant: set on the material
/// the scaffold netting and site fences are drawn with, whose holes are
/// `discard`s. Every other building material compiles it only while
/// [`Self::ghost`] names a building.
pub screen_door: bool,
}
/// [`BuildingWindows`]' uniforms as `building_windows.wesl`'s `Facade` lays
/// them out, in that order.
#[derive(ShaderType, Clone, Copy, Debug)]
pub struct BuildingWindowsGpu {
pub lattice: Vec4,
pub glow: Vec4,
pub lit: Vec4,
pub block: Vec4,
pub ghost: Vec4,
pub ghost_bounds: Vec4,
pub ghost_ring: [Vec4; GHOST_RING_VEC4S],
}
/// What `#[uniform(100, BuildingWindowsGpu)]` binds.
impl From<&BuildingWindows> for BuildingWindowsGpu {
fn from(m: &BuildingWindows) -> Self {
Self {
lattice: m.lattice,
glow: m.glow,
lit: m.lit,
block: m.block,
ghost: m.ghost,
ghost_bounds: m.ghost_bounds,
ghost_ring: m.ghost_ring,
}
}
}
/// The graffiti sheet (`assets/textures/graffiti.png`), at a fixed id for the
/// reason [`CLOUD_FIELD_BUFFER`](crate::sky::CLOUD_FIELD_BUFFER) has one: every
/// building material names it in its `Default`, and a texture that does not
/// exist is a material that never prepares. `cartopolis::systems::graffiti`
/// puts a clear texel there at startup and the sheet over it once it loads.
pub const GRAFFITI_SHEET: Handle<Image> =
bevy::asset::uuid_handle!("5b1f6c2e-8d43-4a7b-9e0c-3f2a71d4c8b6");
/// How many `vec4`s the ghost ring is packed into — two points each, so 32
/// points. See [`BuildingWindows::ghost_ring`]; the shader's `GHOST_MAX_POINTS`
/// is the same number and they must agree.
pub const GHOST_RING_VEC4S: usize = 16;
/// Ring points the shader can hold. A BAG outline simplified at 10 cm is about
/// nine of them.
pub const GHOST_MAX_POINTS: usize = GHOST_RING_VEC4S * 2;
impl Default for BuildingWindows {
fn default() -> Self {
Self {
lattice: Vec4::new(
WINDOW_LATTICE.x,
WINDOW_LATTICE.y,
WINDOW_SIZE.x,
WINDOW_SIZE.y,
),
glow: Vec4::new(0.0, NIGHT_GAIN, DAY_TINT, FIRST_SILL_M),
lit: Vec4::new(LIT_COLOR.x, LIT_COLOR.y, LIT_COLOR.z, 0.0),
// A dead block lights one window in twelve — a stairwell, somebody
// working late — and the liveliest lights half. Neither end is zero
// or one: a wholly black building reads as missing, and a wholly lit
// one is the thing this exists to stop.
block: Vec4::new(BLOCK_M, BLOCK_PERIOD, 0.08, 0.5),
ghost: Vec4::ZERO,
ghost_bounds: Vec4::ZERO,
ghost_ring: [Vec4::ZERO; GHOST_RING_VEC4S],
// Never `Handle::default()`, which names no asset: a material whose
// storage buffer does not exist never prepares, and draws nothing.
clouds: crate::sky::CLOUD_FIELD_BUFFER,
graffiti: GRAFFITI_SHEET,
screen_door: false,
}
}
}
impl MaterialExtension for BuildingWindows {
fn vertex_shader() -> ShaderRef {
"shaders/building_vertex.wesl".into()
}
fn fragment_shader() -> ShaderRef {
"shaders/building_windows.wesl".into()
}
fn specialize(
_pipeline: &MaterialExtensionPipeline,
descriptor: &mut RenderPipelineDescriptor,
layout: &MeshVertexBufferLayoutRef,
key: MaterialExtensionKey<Self>,
) -> Result<(), SpecializedMeshPipelineError> {
if key.bind_group_data.cutaway {
if let Some(fragment) = descriptor.fragment.as_mut() {
fragment.shader_defs.push("BUILDING_CUTAWAY".into());
}
}
// The shadow pass is bevy's prepass pipeline, whose vertex inputs sit at
// other locations; `building_fade::specialize` says why touching it is a
// pipeline that never validates. It reads the position, which is
// unchanged — which is also why the buildings can be in the camera's
// depth prepass at all. The cutaway variant cuts holes, so it stays out
// of that one's depth (`keep_holes_out_of_view_prepass`).
if crate::ground_detail::keep_holes_out_of_view_prepass(
descriptor,
key.mesh_key,
key.bind_group_data.cutaway,
) {
return Ok(());
}
if !layout.0.contains(ATTRIBUTE_FACADE_CODE) {
// Built the old way: four-float colour with the code in its alpha,
// which bevy's own layout already describes.
return Ok(());
}
let mut wanted = vec![
Mesh::ATTRIBUTE_POSITION.at_shader_location(0),
Mesh::ATTRIBUTE_NORMAL.at_shader_location(1),
];
if layout.0.contains(Mesh::ATTRIBUTE_UV_0) {
wanted.push(Mesh::ATTRIBUTE_UV_0.at_shader_location(2));
}
// A wall's measured window lattice, on a cell the city model measured
// (`MeshData::with_windows`); bevy sets `VERTEX_UVS_B` for it.
if layout.0.contains(Mesh::ATTRIBUTE_UV_1) {
wanted.push(Mesh::ATTRIBUTE_UV_1.at_shader_location(3));
}
wanted.push(Mesh::ATTRIBUTE_COLOR.at_shader_location(5));
wanted.push(ATTRIBUTE_FACADE_CODE.at_shader_location(FACADE_SHADER_LOCATION));
let vertex = layout.0.get_layout(&wanted)?;
descriptor.vertex.buffers = vec![vertex];
descriptor
.vertex
.shader_defs
.push("BUILDING_FACADE_CODE".into());
Ok(())
}
}
/// The facade code — occupancy, era, shop and eaves, packed into one float
/// (`cartopolis::systems::building_windows::Facade`) — as its own attribute.
///
/// It used to ride in the vertex colour's alpha, which is what kept the colour
/// at four floats (sixteen bytes) on the largest meshes in the app: the code
/// runs far past 1.0, so no normalised byte can hold it. Split out, the colour
/// is four bytes and the code four more — eight bytes a vertex back.
/// `shaders/building_vertex.wesl` puts the two back together.
///
/// The id is arbitrary but must not collide with bevy's own (below 10),
/// `building_fade::ATTRIBUTE_BUILDING_BASE` or `road_depth::ATTRIBUTE_ROAD_RUNG`.
pub const ATTRIBUTE_FACADE_CODE: MeshVertexAttribute =
MeshVertexAttribute::new("FacadeCode", 812_774_916, VertexFormat::Float32);
/// Where the vertex stage reads [`ATTRIBUTE_FACADE_CODE`]. Bevy's own attributes
/// occupy 0–7.
const FACADE_SHADER_LOCATION: u32 = 8;