#version 330 core

in vec2 v_uv;
in vec2 v_frag_pos;
in vec4 v_rect_bounds;   // (x0, y0, x1, y1)
in float v_radius;

uniform sampler2D u_image;

out vec4 frag_color;

void main() {
    vec2 center = (v_rect_bounds.xy + v_rect_bounds.zw) * 0.5;
    vec2 half_size = (v_rect_bounds.zw - v_rect_bounds.xy) * 0.5;

    // Signed distance to rounded rectangle.
    vec2 p = abs(v_frag_pos - center) - half_size + vec2(v_radius);
    float d = length(max(p, vec2(0.0))) + min(max(p.x, p.y), 0.0) - v_radius;

    // Anti-aliased edge (1px smooth transition).
    float alpha = 1.0 - smoothstep(-0.5, 0.5, d);

    vec4 tex = texture(u_image, v_uv);
    frag_color = vec4(tex.rgb, tex.a * alpha);
}
