#version 450 layout(location = 0) in vec2 v_local; layout(location = 1) in vec2 v_half; layout(location = 2) in vec2 v_uv; layout(location = 3) flat in vec4 v_params; layout(location = 4) in vec4 v_color; layout(set = 0, binding = 0) uniform sampler2D atlas; layout(push_constant) uniform PushConstants { vec2 viewport; float text_gamma; // < 1 bolder, > 1 thinner (settings: text weight) float inv_atlas; } pc; layout(location = 0) out vec4 out_color; #define KIND_RECT 0.0 #define KIND_GLYPH 1.0 #define KIND_TRIANGLE 2.0 #define KIND_CIRCLE 3.0 float sd_round_box(vec2 p, vec2 half_size, float radius) { vec2 q = abs(p) - half_size + radius; return length(max(q, 0.0)) + min(max(q.x, q.y), 0.0) - radius; } // NOTE: distance to the line a->b, positive on the side away from the inside point float sd_edge(vec2 p, vec2 a, vec2 b, vec2 inside) { vec2 n = normalize(vec2(b.y - a.y, a.x - b.x)); float s = sign(dot(inside - a, n)); return -s * dot(p - a, n); } // right pointing triangle filling the rect, exact inside, close enough outside for aa float sd_triangle(vec2 p, vec2 h) { vec2 a = vec2(-h.x, -h.y); vec2 b = vec2(-h.x, h.y); vec2 c = vec2( h.x, 0.0); vec2 m = (a + b + c) / 3.0; return max(max(sd_edge(p, a, b, m), sd_edge(p, b, c, m)), sd_edge(p, c, a, m)); } void main() { float kind = v_params.w; if(kind == KIND_GLYPH) { float coverage = texture(atlas, v_uv).r; coverage = pow(coverage, pc.text_gamma); out_color = v_color * coverage; return; } float radius = min(v_params.x, min(v_half.x, v_half.y)); float border = v_params.y; float softness = max(v_params.z, 0.5); float d; if(kind == KIND_TRIANGLE) d = sd_triangle(v_local, v_half); else if(kind == KIND_CIRCLE) d = length(v_local) - min(v_half.x, v_half.y); else d = sd_round_box(v_local, v_half, radius); float alpha = 1.0 - smoothstep(-softness, softness, d); if(border > 0.0) { alpha *= smoothstep(-softness, softness, d + border); } out_color = v_color * alpha; }