-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
shaders/roundedcorners: base on kde-rounded-corners' shader
- Loading branch information
Showing
1 changed file
with
26 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |