struct HudUniforms {
    projection: mat4x4<f32>,
    color: vec4<f32>,
    outline_color: vec4<f32>,
    outline_width: f32,
    smoothing: f32,
}

@group(0) @binding(0) var<uniform> uniforms: HudUniforms;
@group(1) @binding(0) var font_texture: texture_2d<f32>;
@group(1) @binding(1) var font_sampler: sampler;

struct VertexInput {
    @location(0) position: vec3<f32>,
    @location(1) tex_coords: vec2<f32>,
}

struct VertexOutput {
    @builtin(position) position: vec4<f32>,
    @location(0) tex_coords: vec2<f32>,
}

@vertex
fn vs_main(vertex: VertexInput) -> VertexOutput {
    var output: VertexOutput;
    output.position = uniforms.projection * vec4<f32>(vertex.position, 1.0);
    output.tex_coords = vertex.tex_coords;
    return output;
}

@fragment
fn fs_main(input: VertexOutput) -> @location(0) vec4<f32> {
    let distance = textureSample(font_texture, font_sampler, input.tex_coords).r;

    let alpha_base = smoothstep(0.5 - uniforms.smoothing, 0.5 + uniforms.smoothing, distance);

    if uniforms.outline_width > 0.0 {
        let outline_threshold = 0.5 - uniforms.outline_width;
        let alpha_outline = smoothstep(outline_threshold - uniforms.smoothing, outline_threshold + uniforms.smoothing, distance);

        let outline_factor = alpha_outline - alpha_base;
        let final_color = mix(uniforms.outline_color, uniforms.color, alpha_base / alpha_outline);
        return vec4<f32>(final_color.rgb, final_color.a * alpha_outline);
    } else {
        return vec4<f32>(uniforms.color.rgb, uniforms.color.a * alpha_base);
    }
}
