swayfx/sway/desktop/fx_renderer/shaders/quad_round.frag

40 lines
1.2 KiB
GLSL
Raw Normal View History

2023-04-04 10:16:54 +10:00
#define SOURCE_QUAD_ROUND 1
#define SOURCE_QUAD_ROUND_TOP_LEFT 2
#define SOURCE_QUAD_ROUND_TOP_RIGHT 3
2023-04-28 08:50:36 +10:00
#define SOURCE_QUAD_ROUND_BOTTOM_RIGHT 4
#define SOURCE_QUAD_ROUND_BOTTOM_LEFT 5
2023-04-04 10:16:54 +10:00
#if !defined(SOURCE)
#error "Missing shader preamble"
#endif
precision mediump float;
varying vec4 v_color;
varying vec2 v_texcoord;
uniform vec2 size;
uniform vec2 position;
uniform float radius;
2023-04-04 10:16:54 +10:00
vec2 getCornerDist() {
#if SOURCE == SOURCE_QUAD_ROUND
vec2 half_size = size * 0.5;
2023-04-04 10:16:54 +10:00
return abs(gl_FragCoord.xy - position - half_size) - half_size + radius;
#elif SOURCE == SOURCE_QUAD_ROUND_TOP_LEFT
return abs(gl_FragCoord.xy - position - size) - size + radius;
#elif SOURCE == SOURCE_QUAD_ROUND_TOP_RIGHT
return abs(gl_FragCoord.xy - position - vec2(0, size.y)) - size + radius;
2023-04-28 08:50:36 +10:00
#elif SOURCE == SOURCE_QUAD_ROUND_BOTTOM_RIGHT
return abs(gl_FragCoord.xy - position) - size + radius;
#elif SOURCE == SOURCE_QUAD_ROUND_BOTTOM_LEFT
return abs(gl_FragCoord.xy - position - vec2(size.x, 0)) - size + radius;
2023-04-04 10:16:54 +10:00
#endif
}
void main() {
vec2 q = getCornerDist();
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, dist);
gl_FragColor = mix(vec4(0), v_color, smoothedAlpha);
}