rustial-renderer-wgpu 1.0.0

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

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>,
    hillshade_highlight:  vec4<f32>,
    hillshade_shadow:     vec4<f32>,
    hillshade_accent:     vec4<f32>,
    hillshade_light:      vec4<f32>,
};

struct TileUniforms {
    geo_bounds: vec4<f32>,
    scene_origin: vec4<f32>,
    elev_params: vec4<f32>,
};

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 hillshade_texture: texture_2d<f32>;
@group(1) @binding(1)
var hillshade_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 clamped_uv = clamp(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);
}

@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 sampled_height = sample_height_bilinear(in.uv) * 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 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),
    ));
}

@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));
}