forge
Explore
Log in

Don't you dare go Hollow

niri

Clone
★ 0

0 watching0 forksRustGPL-3.012.1 MiB055a94dIvan Molodetskikh1 year ago2247 commits

clipped_surface.frag80 lines, 2.0 KiBHistoryRawDownload
1#version 100
2
3//_DEFINES_
4
5#if defined(EXTERNAL)
6#extension GL_OES_EGL_image_external : require
7#endif
8
9precision highp float;
10#if defined(EXTERNAL)
11uniform samplerExternalOES tex;
12#else
13uniform sampler2D tex;
14#endif
15
16uniform float alpha;
17varying vec2 v_coords;
18
19#if defined(DEBUG_FLAGS)
20uniform float tint;
21#endif
22
23uniform float niri_scale;
24
25uniform vec2 geo_size;
26uniform vec4 corner_radius;
27uniform mat3 input_to_geo;
28
29float rounding_alpha(vec2 coords, vec2 size) {
30 vec2 center;
31 float radius;
32
33 if (coords.x < corner_radius.x && coords.y < corner_radius.x) {
34 radius = corner_radius.x;
35 center = vec2(radius, radius);
36 } else if (size.x - corner_radius.y < coords.x && coords.y < corner_radius.y) {
37 radius = corner_radius.y;
38 center = vec2(size.x - radius, radius);
39 } else if (size.x - corner_radius.z < coords.x && size.y - corner_radius.z < coords.y) {
40 radius = corner_radius.z;
41 center = vec2(size.x - radius, size.y - radius);
42 } else if (coords.x < corner_radius.w && size.y - corner_radius.w < coords.y) {
43 radius = corner_radius.w;
44 center = vec2(radius, size.y - radius);
45 } else {
46 return 1.0;
47 }
48
49 float dist = distance(coords, center);
50 float half_px = 0.5 / niri_scale;
51 return 1.0 - smoothstep(radius - half_px, radius + half_px, dist);
52}
53
54void main() {
55 vec3 coords_geo = input_to_geo * vec3(v_coords, 1.0);
56
57 // Sample the texture.
58 vec4 color = texture2D(tex, v_coords);
59#if defined(NO_ALPHA)
60 color = vec4(color.rgb, 1.0);
61#endif
62
63 if (coords_geo.x < 0.0 || 1.0 < coords_geo.x || coords_geo.y < 0.0 || 1.0 < coords_geo.y) {
64 // Clip outside geometry.
65 color = vec4(0.0);
66 } else {
67 // Apply corner rounding inside geometry.
68 color = color * rounding_alpha(coords_geo.xy * geo_size, geo_size);
69 }
70
71 // Apply final alpha and tint.
72 color = color * alpha;
73
74#if defined(DEBUG_FLAGS)
75 if (tint == 1.0)
76 color = vec4(0.0, 0.2, 0.0, 0.2) + color * 0.8;
77#endif
78
79 gl_FragColor = color;
80}