const EARTH_RADIUS: f32 = 6378137.0;
const DEG_TO_RAD: f32 = 0.017453292519943295;
const PROJECTION_WEB_MERCATOR: f32 = 0.0;
struct Uniforms {
view_proj: mat4x4<f32>,
fog_color: vec4<f32>,
eye_pos: vec4<f32>,
fog_params: vec4<f32>, // (start, end, density, 0)
hillshade_highlight: vec4<f32>,
hillshade_shadow: vec4<f32>,
hillshade_accent: vec4<f32>,
hillshade_light: vec4<f32>, // (dir_rad, altitude_rad, exaggeration, opacity)
ambient_color: vec4<f32>, // (r, g, b, lighting_enabled)
directional_dir: vec4<f32>, // (x, y, z, 0)
directional_color: vec4<f32>, // (r, g, b, 0)
};
struct TileUniforms {
geo_bounds: vec4<f32>, // (nw_lat, nw_lon, se_lat, se_lon)
scene_origin: vec4<f32>, // (x, y, z, projection_kind)
elev_params: vec4<f32>, // (exaggeration, skirt_base, min_elev, max_elev)
elev_region: vec4<f32>, // (u_min, v_min, u_max, v_max) in the source DEM texture
};
struct TerrainVertexInput {
@location(0) uv: vec2<f32>,
@location(1) skirt: f32,
};
struct TerrainVertexOutput {
@builtin(position) clip_position: vec4<f32>,
@location(0) tex_coord: vec2<f32>,
@location(1) world_pos: vec3<f32>,
};
@group(0) @binding(0)
var<uniform> u: Uniforms;
@group(1) @binding(0)
var terrain_texture: texture_2d<f32>;
@group(1) @binding(1)
var terrain_sampler: sampler;
@group(2) @binding(0)
var<uniform> tile: TileUniforms;
@group(2) @binding(1)
var height_texture: texture_2d<f32>;
fn sample_height_bilinear(uv: vec2<f32>) -> f32 {
let dims = vec2<f32>(textureDimensions(height_texture));
let dem_uv = mix(tile.elev_region.xy, tile.elev_region.zw, uv);
let clamped_uv = clamp(dem_uv, vec2<f32>(0.0), vec2<f32>(1.0));
let coord = clamped_uv * max(dims - vec2<f32>(1.0), vec2<f32>(0.0));
let base = vec2<i32>(floor(coord));
let next = min(base + vec2<i32>(1, 1), vec2<i32>(vec2<u32>(dims) - vec2<u32>(1u, 1u)));
let frac = coord - floor(coord);
let v00 = textureLoad(height_texture, base, 0).x;
let v10 = textureLoad(height_texture, vec2<i32>(next.x, base.y), 0).x;
let v01 = textureLoad(height_texture, vec2<i32>(base.x, next.y), 0).x;
let v11 = textureLoad(height_texture, next, 0).x;
let top = mix(v00, v10, frac.x);
let bot = mix(v01, v11, frac.x);
return mix(top, bot, frac.y);
}
fn project_planar(lat_deg: f32, lon_deg: f32, projection_kind: f32) -> vec2<f32> {
let lon_rad = lon_deg * DEG_TO_RAD;
if projection_kind == PROJECTION_WEB_MERCATOR {
let lat_clamped = clamp(lat_deg, -85.05112878, 85.05112878) * DEG_TO_RAD;
let x = EARTH_RADIUS * lon_rad;
let y = EARTH_RADIUS * log(tan(0.78539816339 + 0.5 * lat_clamped));
return vec2<f32>(x, y);
}
let lat_rad = lat_deg * DEG_TO_RAD;
return vec2<f32>(EARTH_RADIUS * lon_rad, EARTH_RADIUS * lat_rad);
}
fn grade_raster(rgb: vec3<f32>, amount: f32) -> vec3<f32> {
let luma = dot(rgb, vec3<f32>(0.2126, 0.7152, 0.0722));
let contrast = (rgb - vec3<f32>(0.5)) * (1.0 + 0.15 * amount) + vec3<f32>(0.5);
let saturated = vec3<f32>(luma) + (contrast - vec3<f32>(luma)) * (1.0 + 0.12 * amount);
return clamp(saturated, vec3<f32>(0.0), vec3<f32>(1.0));
}
/// Compute terrain normal from height-texture central differences.
/// This avoids the per-2×2-quad quantisation that `dpdx`/`dpdy`
/// screen-space derivatives suffer from at steep pitch angles.
fn compute_terrain_normal(uv: vec2<f32>, exag: f32) -> vec3<f32> {
let dims = vec2<f32>(textureDimensions(height_texture));
let region_size = tile.elev_region.zw - tile.elev_region.xy;
let safe_region = max(abs(region_size), vec2<f32>(0.001));
// One-texel step in mesh UV space.
let eps = vec2<f32>(1.0) / max(dims * safe_region, vec2<f32>(1.0));
let hL = sample_height_bilinear(uv - vec2<f32>(eps.x, 0.0));
let hR = sample_height_bilinear(uv + vec2<f32>(eps.x, 0.0));
let hD = sample_height_bilinear(uv - vec2<f32>(0.0, eps.y));
let hU = sample_height_bilinear(uv + vec2<f32>(0.0, eps.y));
// World-space tile extent (signed: x positive, y typically negative).
let nw = project_planar(tile.geo_bounds.x, tile.geo_bounds.y, tile.scene_origin.w);
let se = project_planar(tile.geo_bounds.z, tile.geo_bounds.w, tile.scene_origin.w);
let step = vec2<f32>(2.0 * eps.x, 2.0 * eps.y) * (se - nw);
let safe_step = select(step, vec2<f32>(1.0, -1.0), abs(step) < vec2<f32>(0.001));
var n = normalize(vec3<f32>(
-(hR - hL) * exag / safe_step.x,
-(hU - hD) * exag / safe_step.y,
1.0,
));
if n.z < 0.0 { n = -n; }
return n;
}
@vertex
fn vs_main(in: TerrainVertexInput) -> TerrainVertexOutput {
var out: TerrainVertexOutput;
let lat = mix(tile.geo_bounds.x, tile.geo_bounds.z, in.uv.y);
let lon = mix(tile.geo_bounds.y, tile.geo_bounds.w, in.uv.x);
let planar = project_planar(lat, lon, tile.scene_origin.w);
let raw_height = sample_height_bilinear(in.uv);
let clamped_height = clamp(raw_height, -500.0, 10000.0);
let sampled_height = clamped_height * tile.elev_params.x;
let displaced_z = select(sampled_height, tile.elev_params.y, in.skirt > 0.5) - tile.scene_origin.z;
let relative = vec3<f32>(
planar.x - tile.scene_origin.x,
planar.y - tile.scene_origin.y,
displaced_z,
);
out.clip_position = u.view_proj * vec4<f32>(relative, 1.0);
out.tex_coord = in.uv;
out.world_pos = relative;
return out;
}
fn terrain_light_dir(params: vec4<f32>) -> vec3<f32> {
let dir = params.x;
let altitude = params.y;
let cos_alt = cos(altitude);
return normalize(vec3<f32>(
-sin(dir) * cos_alt,
cos(dir) * cos_alt,
sin(altitude),
));
}
fn aerial_perspective(base: vec3<f32>, fog_rgb: vec3<f32>, fog_t: f32, slope: f32) -> vec3<f32> {
let haze = clamp(1.0 - exp2(-2.2 * fog_t * fog_t), 0.0, 1.0);
let lifted_fog = clamp(fog_rgb * 1.03 + vec3<f32>(0.015, 0.02, 0.03), vec3<f32>(0.0), vec3<f32>(1.0));
return mix(base, lifted_fog, haze * (0.82 + 0.18 * (1.0 - slope)));
}
@fragment
fn fs_main(in: TerrainVertexOutput) -> @location(0) vec4<f32> {
let base_color = textureSample(terrain_texture, terrain_sampler, in.tex_coord);
var n = compute_terrain_normal(in.tex_coord, tile.elev_params.x);
let slope = sqrt(max(1.0 - n.z * n.z, 0.0));
let light_dir = terrain_light_dir(u.hillshade_light);
let fill_dir = normalize(vec3<f32>(-light_dir.y, light_dir.x, max(0.25, light_dir.z * 0.65)));
let ndotl = max(dot(n, light_dir), 0.0);
let fill = max(dot(n, fill_dir), 0.0);
let ridge = pow(1.0 - clamp(n.z, 0.0, 1.0), 1.4);
let graded = grade_raster(base_color.rgb, 0.24 + 0.32 * slope);
let lit = graded * (0.72 + 0.52 * ndotl + 0.12 * fill)
+ u.hillshade_accent.rgb * ridge * 0.08
+ vec3<f32>(0.018, 0.02, 0.015) * slope;
let shaded = vec4<f32>(clamp(lit, vec3<f32>(0.0), vec3<f32>(1.0)), base_color.a);
let dx = in.world_pos.x - u.eye_pos.x;
let dy = in.world_pos.y - u.eye_pos.y;
let ground_dist = sqrt(dx * dx + dy * dy);
let fog_start = u.fog_params.x;
let fog_end = u.fog_params.y;
let density = u.fog_params.z;
let fog_linear = clamp((ground_dist - fog_start) / max(fog_end - fog_start, 0.001), 0.0, 1.0) * density;
let fog_t = clamp(1.0 - exp2(-(1.45 + 0.4 * slope) * fog_linear * fog_linear), 0.0, 1.0);
let blended_rgb = aerial_perspective(shaded.rgb, u.fog_color.rgb, fog_t, slope);
let blended_a = shaded.a * (1.0 - fog_t * 0.92);
return vec4<f32>(blended_rgb, blended_a);
}