Fix corner fringing + small frag cleanup

This commit is contained in:
Erik Reider 2023-01-06 14:44:19 +01:00
parent f934c74e97
commit 4160b16ca7
8 changed files with 43 additions and 24 deletions

View file

@ -18,13 +18,14 @@ float roundedBoxSDF(vec2 center, vec2 size, float radius) {
void main() { void main() {
vec2 center = gl_FragCoord.xy - position - half_size; vec2 center = gl_FragCoord.xy - position - half_size;
float distance = roundedBoxSDF(center, half_size - half_thickness, radius + half_thickness); float dist = roundedBoxSDF(center, half_size - half_thickness, radius + half_thickness);
float smoothedAlphaOuter = 1.0 - smoothstep(-1.0, 1.0, distance - half_thickness); float smoothedAlphaOuter = 1.0 - smoothstep(-1.0, 1.0, dist - half_thickness);
// Create an inner circle that isn't as anti-aliased as the outer ring // Create an inner circle that isn't as anti-aliased as the outer ring
float smoothedAlphaInner = 1.0 - smoothstep(-1.0, 0.5, distance + half_thickness); float smoothedAlphaInner = 1.0 - smoothstep(-1.0, 0.5, dist + half_thickness);
gl_FragColor = mix(vec4(0), v_color, smoothedAlphaOuter - smoothedAlphaInner); gl_FragColor = mix(vec4(0), v_color, smoothedAlphaOuter - smoothedAlphaInner);
if ((v_color.a == 1.0 && gl_FragColor.a <= 0.5) || gl_FragColor.a <= 0.01) { // Discards outside the curve and transparent pixels
if (dist > half_thickness || gl_FragColor.a == 0.0) {
discard; discard;
return; return;
} }

View file

@ -5,7 +5,7 @@ varying vec2 v_texcoord;
void main() { void main() {
gl_FragColor = v_color; gl_FragColor = v_color;
if (gl_FragColor.a <= 0.01){ if (gl_FragColor.a == 0.0) {
discard; discard;
} }
} }

View file

@ -7,13 +7,14 @@ uniform vec2 position;
uniform float radius; uniform float radius;
void main() { void main() {
vec2 half_size = size / 2.0; vec2 half_size = size * 0.5;
vec2 q = abs(gl_FragCoord.xy - position - half_size) - half_size + radius; vec2 q = abs(gl_FragCoord.xy - position - half_size) - half_size + radius;
float distance = min(max(q.x,q.y),0.0) + length(max(q,0.0)) - radius; float dist = min(max(q.x,q.y), 0.0) + length(max(q, 0.0)) - radius;
float smoothedAlpha = 1.0 - smoothstep(-1.0, 0.5, distance); float smoothedAlpha = 1.0 - smoothstep(-1.0, 0.5, dist);
gl_FragColor = mix(vec4(0), v_color, smoothedAlpha); gl_FragColor = mix(vec4(0), v_color, smoothedAlpha);
if (gl_FragColor.a <= 0.01){ // Discards outside the curve and transparent pixels
if (dist > 0.0 || gl_FragColor.a == 0.0) {
discard; discard;
} }
} }

View file

@ -8,11 +8,12 @@ uniform float radius;
void main() { void main() {
vec2 q = abs(gl_FragCoord.xy - position - size) - size + radius; vec2 q = abs(gl_FragCoord.xy - position - size) - size + radius;
float distance = min(max(q.x,q.y),0.0) + length(max(q,0.0)) - radius; float dist = min(max(q.x,q.y), 0.0) + length(max(q, 0.0)) - radius;
float smoothedAlpha = 1.0 - smoothstep(-1.0, 0.5, distance); float smoothedAlpha = 1.0 - smoothstep(-1.0, 0.5, dist);
gl_FragColor = mix(vec4(0), v_color, smoothedAlpha); gl_FragColor = mix(vec4(0), v_color, smoothedAlpha);
if (gl_FragColor.a <= 0.01){ // Discards outside the curve and transparent pixels
if (dist > 0.0 || gl_FragColor.a == 0.0) {
discard; discard;
} }
} }

View file

@ -8,11 +8,12 @@ uniform float radius;
void main() { void main() {
vec2 q = abs(gl_FragCoord.xy - position - vec2(0, size.y)) - size + radius; vec2 q = abs(gl_FragCoord.xy - position - vec2(0, size.y)) - size + radius;
float distance = min(max(q.x,q.y),0.0) + length(max(q,0.0)) - radius; float dist = min(max(q.x,q.y), 0.0) + length(max(q, 0.0)) - radius;
float smoothedAlpha = 1.0 - smoothstep(-1.0, 0.5, distance); float smoothedAlpha = 1.0 - smoothstep(-1.0, 0.5, dist);
gl_FragColor = mix(vec4(0), v_color, smoothedAlpha); gl_FragColor = mix(vec4(0), v_color, smoothedAlpha);
if (gl_FragColor.a <= 0.01){ // Discards outside the curve and transparent pixels
if (dist > 0.0 || gl_FragColor.a == 0.0) {
discard; discard;
} }
} }

View file

@ -29,13 +29,18 @@ void main() {
if (!has_titlebar || gl_FragCoord.y - position.y > radius) { if (!has_titlebar || gl_FragCoord.y - position.y > radius) {
vec2 corner_distance = min(gl_FragCoord.xy - position, size + position - gl_FragCoord.xy); vec2 corner_distance = min(gl_FragCoord.xy - position, size + position - gl_FragCoord.xy);
if (max(corner_distance.x, corner_distance.y) < radius) { if (max(corner_distance.x, corner_distance.y) < radius) {
float d = radius - distance(corner_distance, vec2(radius)); float dist = radius - distance(corner_distance, vec2(radius));
float smooth = smoothstep(-1.0f, 0.5f, d); float smooth = smoothstep(-1.0f, 0.5f, dist);
gl_FragColor = mix(vec4(0), gl_FragColor, smooth); gl_FragColor = mix(vec4(0), gl_FragColor, smooth);
// Discards pixels outside the curve
if (dist < 0.0) {
discard;
return;
}
} }
} }
if (gl_FragColor.a <= 0.01){ if (gl_FragColor.a == 0.0) {
discard; discard;
} }
} }

View file

@ -27,13 +27,18 @@ void main() {
if (!has_titlebar || gl_FragCoord.y - position.y > radius) { if (!has_titlebar || gl_FragCoord.y - position.y > radius) {
vec2 corner_distance = min(gl_FragCoord.xy - position, size + position - gl_FragCoord.xy); vec2 corner_distance = min(gl_FragCoord.xy - position, size + position - gl_FragCoord.xy);
if (max(corner_distance.x, corner_distance.y) < radius) { if (max(corner_distance.x, corner_distance.y) < radius) {
float d = radius - distance(corner_distance, vec2(radius)); float dist = radius - distance(corner_distance, vec2(radius));
float smooth = smoothstep(-1.0f, 0.5f, d); float smooth = smoothstep(-1.0f, 0.5f, dist);
gl_FragColor = mix(vec4(0), gl_FragColor, smooth); gl_FragColor = mix(vec4(0), gl_FragColor, smooth);
// Discards pixels outside the curve
if (dist < 0.0) {
discard;
return;
}
} }
} }
if (gl_FragColor.a <= 0.01){ if (gl_FragColor.a == 0.0) {
discard; discard;
} }
} }

View file

@ -26,13 +26,18 @@ void main() {
if (!has_titlebar || gl_FragCoord.y - position.y > radius) { if (!has_titlebar || gl_FragCoord.y - position.y > radius) {
vec2 corner_distance = min(gl_FragCoord.xy - position, size + position - gl_FragCoord.xy); vec2 corner_distance = min(gl_FragCoord.xy - position, size + position - gl_FragCoord.xy);
if (max(corner_distance.x, corner_distance.y) < radius) { if (max(corner_distance.x, corner_distance.y) < radius) {
float d = radius - distance(corner_distance, vec2(radius)); float dist = radius - distance(corner_distance, vec2(radius));
float smooth = smoothstep(-1.0f, 0.5f, d); float smooth = smoothstep(-1.0f, 0.5f, dist);
gl_FragColor = mix(vec4(0), gl_FragColor, smooth); gl_FragColor = mix(vec4(0), gl_FragColor, smooth);
// Discards pixels outside the curve
if (dist < 0.0) {
discard;
return;
}
} }
} }
if (gl_FragColor.a <= 0.01){ if (gl_FragColor.a == 0.0) {
discard; discard;
} }
} }