rustial-renderer-wgpu 0.0.1

Pure WGPU renderer for the rustial 2.5D map engine
Documentation
// ---------------------------------------------------------------------------
// Fill-pattern shader — textured fill-layer rendering with repeating pattern.
//
// Extends the solid fill shader with a pattern texture sampled at per-vertex
// UVs computed in world space.  The pattern repeats seamlessly via the GPU
// sampler's Repeat address mode.
// ---------------------------------------------------------------------------

struct Uniforms {
    view_proj:   mat4x4<f32>,
    fog_color:   vec4<f32>,
    eye_pos:     vec4<f32>,
    fog_params:  vec4<f32>,   // (start, end, density, 0)
};

struct FillParams {
    fill_translate:    vec2<f32>,
    fill_opacity:      f32,
    fill_antialias:    f32,
    outline_color:     vec4<f32>,
};

struct FillPatternVertexInput {
    @location(0) position: vec3<f32>,
    @location(1) color:    vec4<f32>,
    @location(2) uv:       vec2<f32>,
};

struct FillPatternVertexOutput {
    @builtin(position) clip_position: vec4<f32>,
    @location(0) color:     vec4<f32>,
    @location(1) world_pos: vec3<f32>,
    @location(2) uv:        vec2<f32>,
};

@group(0) @binding(0)
var<uniform> u: Uniforms;

@group(0) @binding(1)
var<uniform> fp: FillParams;

@group(1) @binding(0)
var pattern_texture: texture_2d<f32>;

@group(1) @binding(1)
var pattern_sampler: sampler;

@vertex
fn vs_main(in: FillPatternVertexInput) -> FillPatternVertexOutput {
    var out: FillPatternVertexOutput;
    var clip = u.view_proj * vec4<f32>(in.position, 1.0);

    // Apply pixel-space translate (same as solid fill).
    clip.x = clip.x + fp.fill_translate.x * clip.w * 0.001;
    clip.y = clip.y + fp.fill_translate.y * clip.w * 0.001;

    out.clip_position = clip;
    out.color = in.color;
    out.world_pos = in.position;
    out.uv = in.uv;
    return out;
}

@fragment
fn fs_main(in: FillPatternVertexOutput) -> @location(0) vec4<f32> {
    // Sample the repeating pattern texture.
    let pattern_color = textureSample(pattern_texture, pattern_sampler, in.uv);

    // Ground-plane horizon fog (same as fill.wgsl).
    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 fog_mix = fog_t * 0.7;
    let blended_rgb = mix(pattern_color.rgb, u.fog_color.rgb, fog_mix);

    // Apply fill-layer opacity and pattern alpha.
    let alpha = pattern_color.a * fp.fill_opacity;
    return vec4<f32>(blended_rgb, alpha);
}