rustial-renderer-wgpu 0.0.1

Pure WGPU renderer for the rustial 2.5D map engine
Documentation
// Distinct hillshade overlay shader.

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>,   // (dir_rad, altitude_rad, exaggeration, opacity)
};

struct TerrainVertexInput {
    @location(0) position: vec3<f32>,
    @location(1) uv: vec2<f32>,
    @location(2) normal: vec3<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 hillshade_texture: texture_2d<f32>;
@group(1) @binding(1)
var hillshade_sampler: sampler;

fn decode_normal(rgb: vec3<f32>) -> vec3<f32> {
    let nxy = rgb.xy * 2.0 - vec2<f32>(1.0, 1.0);
    let nz = max(rgb.z, 0.001);
    return normalize(vec3<f32>(nxy.x, nxy.y, nz));
}

fn hillshade_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),
    ));
}

@vertex
fn vs_main(in: TerrainVertexInput) -> TerrainVertexOutput {
    var out: TerrainVertexOutput;
    out.clip_position = u.view_proj * vec4<f32>(in.position, 1.0);
    out.tex_coord = in.uv;
    out.world_pos = in.position;
    return out;
}

@fragment
fn fs_main(in: TerrainVertexOutput) -> @location(0) vec4<f32> {
    let encoded = textureSample(hillshade_texture, hillshade_sampler, in.tex_coord);
    let n = decode_normal(encoded.rgb);
    let slope = sqrt(max(1.0 - n.z * n.z, 0.0)) * max(u.hillshade_light.z, 0.0);
    let opacity = clamp(u.hillshade_light.w, 0.0, 1.0) * clamp(0.30 + 0.70 * slope, 0.0, 1.0);

    let key_light = hillshade_light_dir(u.hillshade_light);
    let fill_light = normalize(vec3<f32>(-key_light.y, key_light.x, max(0.35, key_light.z * 0.7)));
    let back_light = normalize(vec3<f32>(key_light.y * 0.35, -key_light.x * 0.35, 0.4));
    let ndotl = max(dot(n, key_light), 0.0);
    let fill = max(dot(n, fill_light), 0.0);
    let rim = pow(max(1.0 - dot(n, back_light), 0.0), 1.8) * slope;

    let hillshade_rgb = mix(
        u.hillshade_shadow.rgb,
        u.hillshade_highlight.rgb,
        clamp(ndotl * 0.85 + 0.15, 0.0, 1.0),
    );
    let overlay = clamp(
        hillshade_rgb + u.hillshade_accent.rgb * rim * 0.35 + vec3<f32>(fill * 0.08),
        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 atmospheric_fog = clamp(1.0 - exp2(-1.7 * fog_t * fog_t), 0.0, 1.0);
    let fog_rgb = clamp(u.fog_color.rgb * 1.02 + vec3<f32>(0.015, 0.02, 0.03), vec3<f32>(0.0), vec3<f32>(1.0));
    let faded_overlay = mix(overlay, fog_rgb, atmospheric_fog * 0.68);
    return vec4<f32>(faded_overlay, opacity * (1.0 - atmospheric_fog * 0.92));
}