Skip to content

Commit

Permalink
shaders/roundedcorners: base on kde-rounded-corners' shader
Browse files Browse the repository at this point in the history
  • Loading branch information
taj-ny committed Dec 9, 2024
1 parent dea38f3 commit ee0207a
Showing 1 changed file with 26 additions and 14 deletions.
40 changes: 26 additions & 14 deletions src/shaders/roundedcorners.glsl
Original file line number Diff line number Diff line change
@@ -1,30 +1,42 @@
// Based on KDE-Rounded-Corners' shader: https://github.com/matinlotfali/KDE-Rounded-Corners/blob/master/src/shaders/shapecorners.glsl

uniform float topCornerRadius;
uniform float bottomCornerRadius;
uniform float antialiasing;

uniform vec2 blurSize;
uniform float opacity;

vec4 shapeCorner(vec2 coord0, vec3 tex, vec2 start, float angle, float r) {
float diagonal_length = (abs(cos(angle)) > 0.1 && abs(sin(angle)) > 0.1) ? sqrt(2.0) : 1.0;
vec2 center = start + r * diagonal_length * vec2(cos(angle), sin(angle));
float distance_from_center = distance(coord0, center);

// I don't know exactly where the 0.25 comes from, in the original shader it's 0.5 but doesn't look perfect
float antialiasing = clamp(r - distance_from_center + 0.25, 0.0, 1.0);
return vec4(tex, mix(0.0, 1.0, antialiasing) * opacity);
}

vec4 roundedRectangle(vec2 fragCoord, vec3 texture)
{
if (topCornerRadius == 0 && bottomCornerRadius == 0) {
return vec4(texture, opacity);
}

vec2 halfblurSize = blurSize * 0.5;
vec2 p = fragCoord - halfblurSize;
float radius = 0.0;
if ((fragCoord.y <= bottomCornerRadius)
&& (fragCoord.x <= bottomCornerRadius || fragCoord.x >= blurSize.x - bottomCornerRadius)) {
radius = bottomCornerRadius;
p.y -= radius;
} else if ((fragCoord.y >= blurSize.y - topCornerRadius)
&& (fragCoord.x <= topCornerRadius || fragCoord.x >= blurSize.x - topCornerRadius)) {
radius = topCornerRadius;
p.y += radius;
if (fragCoord.y < bottomCornerRadius) {
if (fragCoord.x < bottomCornerRadius) {
return shapeCorner(fragCoord, texture, vec2(0.0, 0.0), radians(45.0), bottomCornerRadius);
} else if (fragCoord.x > blurSize.x - bottomCornerRadius) {
return shapeCorner(fragCoord, texture, vec2(blurSize.x, 0.0), radians(135.0), bottomCornerRadius);
}
}
else if (fragCoord.y > blurSize.y - topCornerRadius) {
if (fragCoord.x < topCornerRadius) {
return shapeCorner(fragCoord, texture, vec2(0.0, blurSize.y), radians(315.0), topCornerRadius);
} else if (fragCoord.x > blurSize.x - topCornerRadius) {
return shapeCorner(fragCoord, texture, vec2(blurSize.x, blurSize.y), radians(225.0), topCornerRadius);
}
}
float distance = length(max(abs(p) - (halfblurSize + vec2(0.0, radius)) + radius, 0.0)) - radius;

float s = smoothstep(0.0, antialiasing, distance);
return vec4(texture, mix(1.0, 0.0, s) * opacity);
return vec4(texture, opacity);
}

0 comments on commit ee0207a

Please sign in to comment.