struct HudUniforms {
    projection: mat4x4<f32>,
    color: vec4<f32>,
    outline_color: vec4<f32>,
    outline_width: f32,
    depth: f32,
    _padding: vec2<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>,
    @location(2) color: vec4<f32>,
}

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

@vertex
fn vs_main(vertex: VertexInput) -> VertexOutput {
    var output: VertexOutput;

    output.position = uniforms.projection * vec4<f32>(vertex.position.xy, vertex.position.z, 1.0);
    output.position.z = uniforms.depth;
    output.tex_coords = vertex.tex_coords;
    output.vertex_color = vertex.color;
    return output;
}

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

    let screen_px_distance = fwidth(distance);
    let smoothing = screen_px_distance * 0.5;

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

    let base_color = uniforms.color * input.vertex_color;

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

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