// Tile shader for rustial WGPU renderer.
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) uv: vec2<f32>,
@location(2) opacity: f32,
};
struct VertexOutput {
@builtin(position) clip_position: vec4<f32>,
@location(0) tex_coord: vec2<f32>,
@location(1) world_pos: vec3<f32>,
@location(2) opacity: f32,
};
@group(0) @binding(0)
var<uniform> u: Uniforms;
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.12 * amount) + vec3<f32>(0.5);
let saturated = vec3<f32>(luma) + (contrast - vec3<f32>(luma)) * (1.0 + 0.10 * amount);
return clamp(saturated, vec3<f32>(0.0), vec3<f32>(1.0));
}
@vertex
fn vs_main(in: VertexInput) -> VertexOutput {
var out: VertexOutput;
out.clip_position = u.view_proj * vec4<f32>(in.position, 1.0);
out.tex_coord = in.uv;
out.world_pos = in.position;
out.opacity = in.opacity;
return out;
}
@group(1) @binding(0)
var tile_texture: texture_2d<f32>;
@group(1) @binding(1)
var tile_sampler: sampler;
@fragment
fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
let base = textureSample(tile_texture, tile_sampler, in.tex_coord);
// Horizon fade: dissolve far tiles into the background.
// Use ground-plane (XY) distance only -- altitude would make all
// flat tiles equidistant from a high camera.
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 near_detail = 1.0 - clamp(ground_dist / max(fog_start * 1.1, 1.0), 0.0, 1.0);
let styled_rgb = grade_raster(base.rgb, 0.35 + 0.65 * near_detail);
let blended_rgb = mix(styled_rgb, u.fog_color.rgb, fog_t);
let blended_a = base.a * (1.0 - fog_t) * in.opacity;
return vec4<f32>(blended_rgb, blended_a);
}