library-map: clip terrain relief to land so ocean renders as water
- Commit
e380f4bab3837ef25fd24aac4c7047625ab5f4a2- Author
- vayun-mathur <[email protected]>
- When
- 2026-09-20 01:03 UTC (2 months ago)
- Parents
60aaa87
Diff
library/map/src/main/rust/src/tess/terrain.rs
diff --git a/library/map/src/main/rust/src/tess/terrain.rs b/library/map/src/main/rust/src/tess/terrain.rsindex 04fe3f94c2..9b527bf942 100644--- a/library/map/src/main/rust/src/tess/terrain.rs+++ b/library/map/src/main/rust/src/tess/terrain.rs@@ -127,10 +127,37 @@ fn edge_ring(dim: usize) -> Vec<usize> { /// sample, appended after the grid vertices, and one wall quad per edge segment, appended after /// the grid indices — so the grid prefix of both buffers is the untouched triangulation above. pub fn tessellate(hm: &Heightmap, ground_width_m: f64, out_v: &mut Vec<f32>, out_i: &mut Vec<u32>) {+ tessellate_masked(hm, ground_width_m, None, out_v, out_i);+}++/// [`tessellate`], but drawing the grid only where `land` says the sample is over land.+///+/// `land` is a `dim * dim` row-major mask (the same order the grid vertices are emitted in): a cell+/// contributes its two triangles only when all four of its corners are land, and a skirt wall only+/// when both of its edge samples are. `None` means "all land" — the exact output [`tessellate`]+/// gives, so the no-mask callers and their goldens are unchanged.+///+/// This is what keeps the earth-coloured relief off the sea: the archive attaches a flat sea-level+/// heightmap to coastal tiles that straddle the shore, and without this the whole tile's grid drew,+/// painting shaded earth over open water that has no fill to cover it back (the sea is the clear+/// colour, not a polygon). The mask is built from the tile's coastline land polygons — see+/// [`crate::tile::geometry::terrain`]. Every grid vertex is still emitted whatever the mask says, so+/// the row-major vertex order the skirts and callers index into is unchanged; only triangles are+/// withheld.+pub fn tessellate_masked(+ hm: &Heightmap,+ ground_width_m: f64,+ land: Option<&[bool]>,+ out_v: &mut Vec<f32>,+ out_i: &mut Vec<u32>,+) { let dim = hm.dim as usize; if dim < 2 { return; }+ // A cell (its four corners) or a skirt segment (its two ends) is drawn only when every sample+ // it touches is land. `None` admits everything, so the un-masked path is bit-identical.+ let is_land = |i: usize| land.map_or(true, |m| m.get(i).copied().unwrap_or(false)); // Tile-normalised height per metre; a degenerate (polar) tile with zero width flattens rather // than dividing by zero, matching how buildings handle the same edge. let factor = if ground_width_m > 0.0 {@@ -195,6 +222,14 @@ pub fn tessellate(hm: &Heightmap, ground_width_m: f64, out_v: &mut Vec<f32>, out let b = a + 1; let c = a + dim32; let d = c + 1;+ // Withhold a cell whose footprint reaches the sea: all four corners must be land, so a+ // coastal cell drops out and the open water below shows the clear colour instead of+ // shaded earth. A land-only tile keeps every cell (the mask is all-true or absent).+ let cell = row * dim + col;+ if !(is_land(cell) && is_land(cell + 1) && is_land(cell + dim) && is_land(cell + dim + 1))+ {+ continue;+ } // Two triangles per cell, consistently wound; culling is off in the pipeline so the // winding only ever decides nothing visible, but it is kept regular for readability. out_i.extend_from_slice(&[a, b, d, a, d, c]);@@ -226,8 +261,16 @@ pub fn tessellate(hm: &Heightmap, ground_width_m: f64, out_v: &mut Vec<f32>, out } let n = ring.len() as u32; for k in 0..n {- let t0 = base + ring[k as usize] as u32;- let t1 = base + ring[((k + 1) % n) as usize] as u32;+ let e0 = ring[k as usize];+ let e1 = ring[((k + 1) % n) as usize];+ // A skirt only walls a real land edge: over the sea the grid is gone, so its skirt would+ // be a lone earth-coloured wall hanging in open water under tilt. Drop it when either end+ // is not land.+ if !(is_land(e0) && is_land(e1)) {+ continue;+ }+ let t0 = base + e0 as u32;+ let t1 = base + e1 as u32; let s0 = skirt_base + k; let s1 = skirt_base + (k + 1) % n; // One wall quad per edge segment, wound like the grid cells; culling is off so the
library/map/src/main/rust/src/tile/geometry/terrain.rs
diff --git a/library/map/src/main/rust/src/tile/geometry/terrain.rs b/library/map/src/main/rust/src/tile/geometry/terrain.rsindex fd3bda8784..3c2ecea9db 100644--- a/library/map/src/main/rust/src/tile/geometry/terrain.rs+++ b/library/map/src/main/rust/src/tile/geometry/terrain.rs@@ -2,6 +2,7 @@ use super::mesh::{TerrainMesh, EARTH_CIRCUMFERENCE_M}; use crate::tess::terrain; use tilecodec::mamaps::body::{Body, Heightmap};+use tilecodec::mamaps::dict::{LAYER_LANDTYPE, NONE}; /// The DEM-displaced ground grid for this tile, or an empty mesh when the tile carries no /// heightmap.@@ -11,16 +12,98 @@ use tilecodec::mamaps::body::{Body, Heightmap}; /// a metre of relief reads the same on screen as a metre across — the same tile-local unit the /// building heights use. A tile with no heightmap (open ocean, off-DEM coverage) returns an empty /// mesh and keeps drawing its flat `earth` fill instead.+///+/// The grid is clipped to the tile's land: a coastal tile carries a flat sea-level heightmap over+/// the water half, and drawing it there would bury the open sea — which is the clear colour, not a+/// fill, so nothing repaints over it — under shaded earth. [`land_mask`] marks which samples sit on+/// land (inside the coastline `earth` base), and [`terrain::tessellate_masked`] withholds the cells+/// and skirts that reach the sea. pub(crate) fn terrain_mesh(tile: &Body, ground_width_m: f64) -> TerrainMesh { let Some(heightmap) = &tile.heightmap else { return TerrainMesh::default(); }; let mut vertices = Vec::new(); let mut indices = Vec::new();- terrain::tessellate(heightmap, ground_width_m, &mut vertices, &mut indices);+ let land = land_mask(tile, heightmap.dim as usize);+ terrain::tessellate_masked(heightmap, ground_width_m, Some(&land), &mut vertices, &mut indices); TerrainMesh { vertices, indices } } +/// A `dim * dim` land mask over the tile's heightmap grid, row-major from the top-left — the same+/// order [`terrain::tessellate_masked`] emits vertices in.+///+/// `true` where the sample sits on land, from the tile's coastline land polygons: the `landtype`+/// features with kind [`NONE`], which is what the `stream_prepared` land product writes for the+/// mainland and islands and what the style's kind-less `earth` arm paints. Inland water is *not*+/// carved out — a lake sits on land the coastline product still calls land, and its own `water`+/// fill paints the blue over the relief — so the mask carves out only the sea, which is the case+/// with nothing to cover the grid.+///+/// A tile with no `NONE` land base at all is open sea (or off the land product): every sample reads+/// as water, so the grid drops out completely.+fn land_mask(tile: &Body, dim: usize) -> Vec<bool> {+ let extent = tile.extent.max(1) as f32;+ // The coastline land rings, in tile-local 0..1 — the space the grid vertices live in.+ let mut rings: Vec<Vec<(f32, f32)>> = Vec::new();+ if let Some(layer) = tile.layer(LAYER_LANDTYPE) {+ for feature in &layer.features {+ if feature.kind != NONE {+ continue;+ }+ for part in layer.parts_of(feature) {+ let ring: Vec<(f32, f32)> = layer+ .points(part)+ .iter()+ .map(|&(px, py)| (px as f32 / extent, py as f32 / extent))+ .collect();+ if ring.len() >= 3 {+ rings.push(ring);+ }+ }+ }+ }+ let mut mask = vec![false; dim * dim];+ if rings.is_empty() || dim < 2 {+ return mask;+ }+ let step = 1.0 / (dim as f32 - 1.0);+ // Nudge the sample off the tile edge: a land-only tile's ring runs exactly along the boundary,+ // and an even-odd test on the fence is a coin toss. A hair inside keeps the edge vertices land+ // (so the full grid draws) without pulling a genuine sea sample across the coastline.+ let eps = step * 1e-3;+ for row in 0..dim {+ let v = (row as f32 * step).clamp(eps, 1.0 - eps);+ for col in 0..dim {+ let u = (col as f32 * step).clamp(eps, 1.0 - eps);+ mask[row * dim + col] = point_in_rings(u, v, &rings);+ }+ }+ mask+}++/// Even-odd ray cast: is `(u, v)` inside the land the `rings` bound?+///+/// One counter across every ring so holes fall out for free — a point inside an exterior and its+/// hole crosses twice and reads as outside — which is how an enclosed sea within a land polygon+/// stays sea. Winding is irrelevant to the test, so it does not matter that the land product and+/// this pipeline disagree on it.+fn point_in_rings(u: f32, v: f32, rings: &[Vec<(f32, f32)>]) -> bool {+ let mut inside = false;+ for ring in rings {+ let n = ring.len();+ let mut j = n - 1;+ for i in 0..n {+ let (xi, yi) = ring[i];+ let (xj, yj) = ring[j];+ if (yi > v) != (yj > v) && u < (xj - xi) * (v - yi) / (yj - yi) + xi {+ inside = !inside;+ }+ j = i;+ }+ }+ inside+}+ /// Bilinearly sample a tile's ground elevation at tile-local `(u, v)` in 0..1, in metres /// above sea level. ///