niri
Clone
https://git.tahinli.com/Tahinli/niri.git
0 watching0 forksRustGPL-3.012.1 MiB9d8f4f8Ivan Molodetskikh1 month ago2997 commits
| 1 | // Your shader must contain one function (see the bottom of this file). |
| 2 | // |
| 3 | // It should not contain any uniform definitions or anything else, as niri |
| 4 | // provides them for you. |
| 5 | // |
| 6 | // All symbols defined by niri will have a niri_ prefix, so don't use it for |
| 7 | // your own variables and functions. |
| 8 | |
| 9 | // The function that you must define looks like this: |
| 10 | vec4 close_color(vec3 coords_geo, vec3 size_geo) { |
| 11 | vec4 color = /* ...compute the color... */; |
| 12 | return color; |
| 13 | } |
| 14 | |
| 15 | // It takes as input: |
| 16 | // |
| 17 | // * coords_geo: coordinates of the current pixel relative to the window |
| 18 | // geometry. |
| 19 | // |
| 20 | // These are homogeneous (the Z component is equal to 1) and scaled in such a |
| 21 | // way that the 0 to 1 coordinates lie within the window geometry. Pixels |
| 22 | // outside the window geometry will have coordinates below 0 or above 1. |
| 23 | // |
| 24 | // The window geometry is its "visible bounds" from the user's perspective. |
| 25 | // |
| 26 | // The shader runs over the full screen area, so you must expect and handle |
| 27 | // coordinates outside the [0, 1] range. If the window is scrolled off-screen, |
| 28 | // all of the coordinates to the shader can fall outside the [0, 1] range. |
| 29 | // |
| 30 | // * size_geo: size of the window geometry in logical pixels. |
| 31 | // |
| 32 | // It is homogeneous (the Z component is equal to 1). |
| 33 | // |
| 34 | // The function must return the color of the pixel (with premultiplied alpha). |
| 35 | // The pixel color will be further processed by niri (for example, to apply the |
| 36 | // final opacity from window rules). |
| 37 | |
| 38 | // Now let's go over the uniforms that niri defines. |
| 39 | // |
| 40 | // You should only rely on the uniforms documented here. Any other uniforms can |
| 41 | // change or be removed without notice. |
| 42 | |
| 43 | // The window texture. |
| 44 | uniform sampler2D niri_tex; |
| 45 | |
| 46 | // Matrix that converts geometry coordinates into the window texture |
| 47 | // coordinates. |
| 48 | // |
| 49 | // The window texture can and will go outside the geometry (for client-side |
| 50 | // decoration shadows for example), which is why this matrix is necessary. |
| 51 | uniform mat3 niri_geo_to_tex; |
| 52 | |
| 53 | |
| 54 | // Unclamped progress of the animation. |
| 55 | // |
| 56 | // Goes from 0 to 1 but may overshoot and oscillate. |
| 57 | uniform float niri_progress; |
| 58 | |
| 59 | // Clamped progress of the animation. |
| 60 | // |
| 61 | // Goes from 0 to 1, but will stop at 1 as soon as it first reaches 1. Will not |
| 62 | // overshoot or oscillate. |
| 63 | uniform float niri_clamped_progress; |
| 64 | |
| 65 | // Random float in [0; 1), consistent for the duration of the animation. |
| 66 | uniform float niri_random_seed; |
| 67 | |
| 68 | // Now let's look at some examples. You can copy everything below this line |
| 69 | // into your custom-shader to experiment. |
| 70 | |
| 71 | // Example: fill the current geometry with a solid vertical gradient and |
| 72 | // gradually make transparent. |
| 73 | vec4 solid_gradient(vec3 coords_geo, vec3 size_geo) { |
| 74 | vec4 color = vec4(0.0); |
| 75 | |
| 76 | // Paint only the area inside the current geometry. |
| 77 | if (0.0 <= coords_geo.x && coords_geo.x <= 1.0 |
| 78 | && 0.0 <= coords_geo.y && coords_geo.y <= 1.0) |
| 79 | { |
| 80 | vec4 from = vec4(1.0, 0.0, 0.0, 1.0); |
| 81 | vec4 to = vec4(0.0, 1.0, 0.0, 1.0); |
| 82 | color = mix(from, to, coords_geo.y); |
| 83 | } |
| 84 | |
| 85 | // Make it transparent. |
| 86 | color *= (1.0 - niri_clamped_progress); |
| 87 | |
| 88 | return color; |
| 89 | } |
| 90 | |
| 91 | // Example: gradually scale down and make transparent, equivalent to the |
| 92 | // default closing animation. |
| 93 | vec4 default_close(vec3 coords_geo, vec3 size_geo) { |
| 94 | // Scale down the window. |
| 95 | float scale = max(0.0, ((1.0 - niri_clamped_progress) / 5.0 + 0.8)); |
| 96 | coords_geo = vec3((coords_geo.xy - vec2(0.5)) / scale + vec2(0.5), 1.0); |
| 97 | |
| 98 | // Get color from the window texture. |
| 99 | vec3 coords_tex = niri_geo_to_tex * coords_geo; |
| 100 | vec4 color = texture2D(niri_tex, coords_tex.st); |
| 101 | |
| 102 | // Make the window transparent. |
| 103 | color *= (1.0 - niri_clamped_progress); |
| 104 | |
| 105 | return color; |
| 106 | } |
| 107 | |
| 108 | // Example: make the window 'fall down' with slight rotation. |
| 109 | vec4 fall_and_rotate(vec3 coords_geo, vec3 size_geo) { |
| 110 | // For this shader, set animation curve to linear for best results. |
| 111 | |
| 112 | // Simulate an accelerated fall: square the (linear) progress. |
| 113 | float progress = niri_clamped_progress * niri_clamped_progress; |
| 114 | |
| 115 | // Get our rotation pivot point coordinates at the bottom center of the window. |
| 116 | vec2 coords = (coords_geo.xy - vec2(0.5, 1.0)) * size_geo.xy; |
| 117 | |
| 118 | // Move the window down to simulate a fall. |
| 119 | coords.y -= progress * 200.0; |
| 120 | |
| 121 | // Randomize rotation direction and maximum angle. |
| 122 | float random = (niri_random_seed - 0.5) / 2.0; |
| 123 | random = sign(random) - random; |
| 124 | float max_angle = 0.05 * random; |
| 125 | |
| 126 | // Rotate the window around our pivot point. |
| 127 | float angle = progress * max_angle; |
| 128 | mat2 rotate = mat2(cos(angle), -sin(angle), sin(angle), cos(angle)); |
| 129 | coords = rotate * coords; |
| 130 | |
| 131 | // Transform the coordinates back. |
| 132 | coords_geo = vec3(coords / size_geo.xy + vec2(0.5, 1.0), 1.0); |
| 133 | |
| 134 | // Sample the window texture. |
| 135 | vec3 coords_tex = niri_geo_to_tex * coords_geo; |
| 136 | vec4 color = texture2D(niri_tex, coords_tex.st); |
| 137 | |
| 138 | // Multiply by alpha to fade out. |
| 139 | return color * (1.0 - niri_clamped_progress); |
| 140 | } |
| 141 | |
| 142 | // This is the function that you must define. |
| 143 | vec4 close_color(vec3 coords_geo, vec3 size_geo) { |
| 144 | // You can pick one of the example functions or write your own. |
| 145 | return fall_and_rotate(coords_geo, size_geo); |
| 146 | } |
| 147 |