// Fill-extrusion shader with directional lighting and fog.
struct Uniforms {
view_proj: mat4x4<f32>,
fog_color: vec4<f32>,
eye_pos: vec4<f32>,
fog_params: vec4<f32>, // (start, end, density, 0)
};
struct VertexInput {
@location(0) position: vec3<f32>,
@location(1) normal: vec3<f32>,
@location(2) color: vec4<f32>,
};
struct VertexOutput {
@builtin(position) clip_position: vec4<f32>,
@location(0) normal: vec3<f32>,
@location(1) color: vec4<f32>,
@location(2) world_pos: vec3<f32>,
};
@group(0) @binding(0)
var<uniform> u: Uniforms;
@vertex
fn vs_main(in: VertexInput) -> VertexOutput {
var out: VertexOutput;
out.clip_position = u.view_proj * vec4<f32>(in.position, 1.0);
out.normal = in.normal;
out.color = in.color;
out.world_pos = in.position;
return out;
}
@fragment
fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
// --- Directional light ---
// Fixed sun direction: upper-left, 45° altitude.
let sun_dir = normalize(vec3<f32>(-0.5, 0.5, 0.707));
let n = normalize(in.normal);
// Lambertian diffuse.
let ndotl = max(dot(n, sun_dir), 0.0);
// Soft fill light from opposite side to reduce harsh shadows.
let fill_dir = normalize(vec3<f32>(sun_dir.y, -sun_dir.x, max(0.25, sun_dir.z * 0.6)));
let fill = max(dot(n, fill_dir), 0.0);
// Ambient + diffuse + fill.
let lit = clamp(
in.color.rgb * (0.55 + 0.50 * ndotl + 0.12 * fill),
vec3<f32>(0.0),
vec3<f32>(1.0),
);
// --- Horizon fog ---
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_t = clamp(
(ground_dist - fog_start) / max(fog_end - fog_start, 0.001),
0.0, 1.0,
) * density;
let fog_mix = fog_t * 0.7;
let blended_rgb = mix(lit, u.fog_color.rgb, fog_mix);
let blended_a = in.color.a * (1.0 - fog_mix);
return vec4<f32>(blended_rgb, blended_a);
}