rustial-renderer-bevy 0.0.1

Bevy Engine renderer for the rustial 2.5D map engine
#import bevy_pbr::{
    mesh_bindings::mesh,
    mesh_functions,
    forward_io::{Vertex, VertexOutput},
    view_transformations::position_world_to_clip,
}

const EARTH_RADIUS: f32 = 6378137.0;
const DEG_TO_RAD: f32 = 0.017453292519943295;
const PROJECTION_WEB_MERCATOR: f32 = 0.0;

struct TileFogUniforms {
    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 MaterialFlags {
    has_texture: f32,
    fade_opacity: f32,
    _pad: vec2<f32>,
};

struct TerrainUniforms {
    geo_bounds: vec4<f32>,
    scene_origin: vec4<f32>,
    elev_params: vec4<f32>,
    elev_region: vec4<f32>,
    options: vec4<f32>,
};

@group(#{MATERIAL_BIND_GROUP}) @binding(0)
var<uniform> fog: TileFogUniforms;
@group(#{MATERIAL_BIND_GROUP}) @binding(1)
var tile_texture: texture_2d<f32>;
@group(#{MATERIAL_BIND_GROUP}) @binding(2)
var tile_sampler: sampler;
@group(#{MATERIAL_BIND_GROUP}) @binding(3)
var<uniform> flags: MaterialFlags;
@group(#{MATERIAL_BIND_GROUP}) @binding(4)
var<uniform> terrain: TerrainUniforms;
@group(#{MATERIAL_BIND_GROUP}) @binding(5)
var height_texture: texture_2d<f32>;

fn sample_height_bilinear(uv: vec2<f32>) -> f32 {
    let dims_u = textureDimensions(height_texture);
    let dims = vec2<f32>(dims_u);
    let max_coord = vec2<i32>(max(vec2<u32>(1u, 1u), dims_u) - vec2<u32>(1u, 1u));
    let dem_uv = mix(terrain.elev_region.xy, terrain.elev_region.zw, uv);
    let clamped_uv = clamp(dem_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), max_coord);
    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);
}

/// Compute terrain normal from height-texture central differences.
/// This avoids the per-2×2-quad quantisation that `dpdx`/`dpdy`
/// screen-space derivatives suffer from at steep pitch angles.
fn compute_terrain_normal(uv: vec2<f32>, exag: f32) -> vec3<f32> {
    let dims = vec2<f32>(textureDimensions(height_texture));
    let region_size = terrain.elev_region.zw - terrain.elev_region.xy;
    let safe_region = max(abs(region_size), vec2<f32>(0.001));
    // One-texel step in mesh UV space.
    let eps = vec2<f32>(1.0) / max(dims * safe_region, vec2<f32>(1.0));

    let hL = sample_height_bilinear(uv - vec2<f32>(eps.x, 0.0));
    let hR = sample_height_bilinear(uv + vec2<f32>(eps.x, 0.0));
    let hD = sample_height_bilinear(uv - vec2<f32>(0.0, eps.y));
    let hU = sample_height_bilinear(uv + vec2<f32>(0.0, eps.y));

    // World-space tile extent (signed: x positive, y typically negative).
    let nw = project_planar(terrain.geo_bounds.x, terrain.geo_bounds.y, terrain.scene_origin.w);
    let se = project_planar(terrain.geo_bounds.z, terrain.geo_bounds.w, terrain.scene_origin.w);
    let step = vec2<f32>(2.0 * eps.x, 2.0 * eps.y) * (se - nw);
    let safe_step = select(step, vec2<f32>(1.0, -1.0), abs(step) < vec2<f32>(0.001));

    var n = normalize(vec3<f32>(
        -(hR - hL) * exag / safe_step.x,
        -(hU - hD) * exag / safe_step.y,
        1.0,
    ));
    if n.z < 0.0 { n = -n; }
    return n;
}

@vertex
fn vertex(vertex: Vertex) -> VertexOutput {
    var out: VertexOutput;
    let world_from_local = mesh_functions::get_world_from_local(vertex.instance_index);

    if terrain.options.x > 0.5 {
        let uv = vertex.uv;
        let skirt = vertex.position.z;
        let lat = mix(terrain.geo_bounds.x, terrain.geo_bounds.z, uv.y);
        let lon = mix(terrain.geo_bounds.y, terrain.geo_bounds.w, uv.x);
        let planar = project_planar(lat, lon, terrain.scene_origin.w);
        let raw_height = sample_height_bilinear(uv);
        // Clamp extreme ocean/no-data values that would punch geometry
        // far below the ground plane, creating visual holes.
        let clamped_height = clamp(raw_height, -500.0, 10000.0);
        let sampled_height = clamped_height * terrain.elev_params.x;
        let displaced_z = select(sampled_height, terrain.elev_params.y, skirt > 0.5) - terrain.scene_origin.z;
        let local_position = vec3<f32>(
            planar.x - terrain.scene_origin.x,
            planar.y - terrain.scene_origin.y,
            displaced_z,
        );
        out.world_position = mesh_functions::mesh_position_local_to_world(
            world_from_local,
            vec4<f32>(local_position, 1.0),
        );
        out.position = position_world_to_clip(out.world_position.xyz);
        out.world_normal = vec3<f32>(0.0, 0.0, 1.0);
        out.uv = uv;
        return out;
    }

    out.world_normal = mesh_functions::mesh_normal_local_to_world(vertex.normal, vertex.instance_index);
    out.world_position = mesh_functions::mesh_position_local_to_world(world_from_local, vec4<f32>(vertex.position, 1.0));
    out.position = position_world_to_clip(out.world_position.xyz);
    out.uv = vertex.uv;
    return out;
}

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.14 * amount) + vec3<f32>(0.5);
    let saturated = vec3<f32>(luma) + (contrast - vec3<f32>(luma)) * (1.0 + 0.12 * amount);
    return clamp(saturated, vec3<f32>(0.0), vec3<f32>(1.0));
}

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

fn aerial_perspective(base: vec3<f32>, fog_rgb: vec3<f32>, fog_t: f32, slope: f32) -> vec3<f32> {
    let haze = clamp(1.0 - exp2(-2.2 * fog_t * fog_t), 0.0, 1.0);
    let lifted_fog = clamp(fog_rgb * 1.03 + vec3<f32>(0.015, 0.02, 0.03), vec3<f32>(0.0), vec3<f32>(1.0));
    return mix(base, lifted_fog, haze * (0.82 + 0.18 * (1.0 - slope)));
}

@fragment
fn fragment(in: VertexOutput) -> @location(0) vec4<f32> {
    var base = vec4<f32>(0.58, 0.64, 0.56, 1.0);
    if flags.has_texture > 0.5 {
        base = textureSample(tile_texture, tile_sampler, in.uv);
    }

    let dx = in.world_position.x - fog.eye_pos.x;
    let dy = in.world_position.y - fog.eye_pos.y;
    let ground_dist = sqrt(dx * dx + dy * dy);
    let fog_start = fog.fog_params.x;
    let fog_end   = fog.fog_params.y;
    let density   = fog.fog_params.z;
    let fog_linear = clamp(
        (ground_dist - fog_start) / max(fog_end - fog_start, 0.001),
        0.0,
        1.0,
    ) * density;
    let fog_t = clamp(1.0 - exp2(-1.45 * fog_linear * fog_linear), 0.0, 1.0);

    let near_detail = 1.0 - clamp(ground_dist / max(fog_start * 1.1, 1.0), 0.0, 1.0);
    var lit = grade_raster(base.rgb, 0.35 + 0.65 * near_detail);
    var slope = 0.0;

    if terrain.options.x > 0.5 {
        var n = compute_terrain_normal(in.uv, terrain.elev_params.x);
        slope = sqrt(max(1.0 - n.z * n.z, 0.0));
        let light_dir = terrain_light_dir(fog.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 graded = grade_raster(base.rgb, 0.24 + 0.32 * slope);
        lit = clamp(
            graded * (0.72 + 0.52 * ndotl + 0.12 * fill)
                + fog.hillshade_accent.rgb * ridge * 0.08
                + vec3<f32>(0.018, 0.02, 0.015) * slope,
            vec3<f32>(0.0),
            vec3<f32>(1.0),
        );
    } else {
        let raw_n = in.world_normal;
        let len_sq = dot(raw_n, raw_n);
        if len_sq > 0.5 {
            let n = raw_n * inverseSqrt(len_sq);
            slope = sqrt(max(1.0 - n.z * n.z, 0.0));
            if slope > 0.01 {
                let light_dir = terrain_light_dir(fog.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 graded = grade_raster(base.rgb, 0.24 + 0.32 * slope);
                lit = clamp(
                    graded * (0.72 + 0.52 * ndotl + 0.12 * fill)
                        + fog.hillshade_accent.rgb * ridge * 0.08
                        + vec3<f32>(0.018, 0.02, 0.015) * slope,
                    vec3<f32>(0.0),
                    vec3<f32>(1.0),
                );
            }
        }
    }

    let blended_rgb = aerial_perspective(lit, fog.fog_color.rgb, fog_t, slope);
    return vec4<f32>(blended_rgb, base.a * (1.0 - fog_t * 0.92) * flags.fade_opacity);
}