|
| 1 | +/* |
| 2 | + * Copyright 2025 elementary, Inc. <https://elementary.io> |
| 3 | + * SPDX-License-Identifier: LGPL-3.0-or-later |
| 4 | + */ |
| 5 | + |
| 6 | +// based on shader from mutter |
| 7 | + |
| 8 | +uniform sampler2D tex; |
| 9 | +uniform float clip_radius; |
| 10 | +uniform vec2 actor_size; |
| 11 | + |
| 12 | +float rounded_rect_coverage (vec2 p) { |
| 13 | + float center_left = clip_radius + 1.5; |
| 14 | + float center_right = actor_size.x - clip_radius - 0.55; |
| 15 | + float center_x; |
| 16 | + |
| 17 | + if (p.x < center_left) |
| 18 | + center_x = center_left; |
| 19 | + else if (p.x >= center_right) |
| 20 | + center_x = center_right; |
| 21 | + else |
| 22 | + return 1.0; |
| 23 | + |
| 24 | + float center_top = clip_radius + 1.5; |
| 25 | + float center_bottom = actor_size.y - clip_radius - 0.55; |
| 26 | + float center_y; |
| 27 | + |
| 28 | + if (p.y < center_top) |
| 29 | + center_y = center_top; |
| 30 | + else if (p.y > center_bottom) |
| 31 | + center_y = center_bottom; |
| 32 | + else |
| 33 | + return 1.0; |
| 34 | + |
| 35 | + vec2 delta = p - vec2 (center_x, center_y); |
| 36 | + float dist_squared = dot (delta, delta); |
| 37 | + |
| 38 | + // Fully outside the circle |
| 39 | + float outer_radius = clip_radius + 0.5; |
| 40 | + if (dist_squared > (outer_radius * outer_radius)) |
| 41 | + return 0.0; |
| 42 | + |
| 43 | + // Fully inside the circle |
| 44 | + float inner_radius = clip_radius - 0.5; |
| 45 | + if (dist_squared <= (inner_radius * inner_radius)) |
| 46 | + return 1.0; |
| 47 | + |
| 48 | + // Only pixels on the edge of the curve need expensive antialiasing |
| 49 | + return smoothstep (outer_radius, inner_radius, sqrt (dist_squared)); |
| 50 | +} |
| 51 | + |
| 52 | +void main () { |
| 53 | + vec4 sample = texture2D (tex, cogl_tex_coord0_in.xy); |
| 54 | + |
| 55 | + vec2 texture_coord = cogl_tex_coord0_in.xy * actor_size; |
| 56 | + float res = rounded_rect_coverage (texture_coord); |
| 57 | + |
| 58 | + cogl_color_out = sample * cogl_color_in * res; |
| 59 | +} |
0 commit comments