#version 450 layout(location = 0) in vec4 in_color; layout(location = 1) in vec4 in_borderColor; layout(location = 2) in vec2 in_local; layout(location = 3) in vec2 in_halfSize; layout(location = 4) in vec2 in_borderRound; // borderWidth, roundness layout(location = 0) out vec4 outColor; float RoundedBoxSDF(vec2 p, vec2 halfSize, float radius) { vec2 q = abs(p) - halfSize + radius; return length(max(q, 0.0)) + min(max(q.x, q.y), 0.0) - radius; } void main() { float borderWidth = in_borderRound.x; float radius = in_borderRound.y; float dist = RoundedBoxSDF(in_local, in_halfSize, radius); float fillAlpha = 1.0 - smoothstep(-1.0, 1.0, dist); float borderAlpha = 0.0; if (borderWidth > 0.0) { float borderDist = abs(dist + borderWidth * 0.5) - borderWidth * 0.5; borderAlpha = 1.0 - smoothstep(-1.0, 1.0, borderDist); } if (radius > 0.0 || borderWidth > 0.0) { vec4 fill = in_color * fillAlpha; vec4 result = mix(fill, in_borderColor, borderAlpha * in_borderColor.a); outColor = result; } else { outColor = in_color; } }