rustial-renderer-wgpu 0.0.1

Pure WGPU renderer for the rustial 2.5D map engine
Documentation
// Terrain data shader for renderer-owned depth / coordinate buffers.

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) relative_pos: vec3<f32>,
};

@group(0) @binding(0)
var<uniform> u: Uniforms;
@group(1) @binding(0)
var<uniform> tile: TileUniforms;
@group(1) @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 raw_height = sample_height_bilinear(in.uv);
    let clamped_height = clamp(raw_height, -500.0, 10000.0);
    let sampled_height = clamped_height * 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.relative_pos = relative;
    return out;
}

@fragment
fn fs_main(in: TerrainVertexOutput) -> @location(0) vec4<f32> {
    return vec4<f32>(in.relative_pos, 1.0);
}