struct Uniforms {
view_proj: mat4x4<f32>,
fog_color: vec4<f32>,
eye_pos: vec4<f32>,
fog_params: vec4<f32>,
hillshade_highlight: vec4<f32>,
hillshade_shadow: vec4<f32>,
hillshade_accent: vec4<f32>,
hillshade_light: vec4<f32>,
};
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;
}
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),
));
}
@fragment
fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
let n = normalize(in.normal);
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 lit = clamp(
in.color.rgb * (0.62 + 0.55 * ndotl + 0.10 * fill)
+ u.hillshade_accent.rgb * ridge * 0.06,
vec3<f32>(0.0),
vec3<f32>(1.0),
);
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 blended_rgb = mix(lit, u.fog_color.rgb, fog_t * 0.7);
let blended_a = in.color.a * (1.0 - fog_t * 0.7);
return vec4<f32>(blended_rgb, blended_a);
}