rustial-renderer-bevy 0.0.1

Bevy Engine renderer for the rustial 2.5D map engine
// Dedicated hillshade overlay shader for terrain meshes.

#import bevy_pbr::forward_io::VertexOutput
#import bevy_pbr::mesh_bindings::mesh
#import bevy_pbr::mesh_functions
#import bevy_pbr::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 FogUniforms {
    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 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: FogUniforms;
@group(#{MATERIAL_BIND_GROUP}) @binding(1)
var hillshade_texture: texture_2d<f32>;
@group(#{MATERIAL_BIND_GROUP}) @binding(2)
var hillshade_sampler: sampler;
@group(#{MATERIAL_BIND_GROUP}) @binding(3)
var<uniform> terrain: TerrainUniforms;
@group(#{MATERIAL_BIND_GROUP}) @binding(4)
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);
}

@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 sampled_height = sample_height_bilinear(uv) * 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 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 fragment(in: VertexOutput) -> @location(0) vec4<f32> {
    let encoded = textureSample(hillshade_texture, hillshade_sampler, in.uv);
    let n = decode_normal(encoded.rgb);
    let slope = sqrt(max(1.0 - n.z * n.z, 0.0)) * max(fog.hillshade_light.z, 0.0);
    let opacity = clamp(fog.hillshade_light.w, 0.0, 1.0) * clamp(0.30 + 0.70 * slope, 0.0, 1.0);

    let key_light = hillshade_light_dir(fog.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(
        fog.hillshade_shadow.rgb,
        fog.hillshade_highlight.rgb,
        clamp(ndotl * 0.85 + 0.15, 0.0, 1.0),
    );
    let overlay = clamp(
        hillshade_rgb + fog.hillshade_accent.rgb * rim * 0.35 + vec3<f32>(fill * 0.08),
        vec3<f32>(0.0),
        vec3<f32>(1.0),
    );

    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_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(fog.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));
}