Add crossfade_or_crop_next resize shader example
Browse files at this commit36a54615caf82d5509eeea2e096a16e6d6588affParent: 9004c831 changed file +44 -0
wiki/examples/resize_custom_shader.fragmodified+44 -0
| @@ -159,6 +159,50 @@ vec4 stretch_or_crop_next(vec3 coords_curr_geo, vec3 size_curr_geo) { | |||
| 159 | return color; | 159 | return color; |
| 160 | } | 160 | } |
| 161 | 161 | ||
| 162 | +// Example: cropped next texture if it's bigger than the current geometry, and | ||
| 163 | +// crossfade between previous and next texture otherwise. | ||
| 164 | +vec4 crossfade_or_crop_next(vec3 coords_curr_geo, vec3 size_curr_geo) { | ||
| 165 | + vec3 coords_next_geo = niri_curr_geo_to_next_geo * coords_curr_geo; | ||
| 166 | + vec3 coords_prev_geo = niri_curr_geo_to_prev_geo * coords_curr_geo; | ||
| 167 | + | ||
| 168 | + vec3 coords_crop = niri_geo_to_tex_next * coords_next_geo; | ||
| 169 | + vec3 coords_stretch = niri_geo_to_tex_next * coords_curr_geo; | ||
| 170 | + vec3 coords_stretch_prev = niri_geo_to_tex_prev * coords_curr_geo; | ||
| 171 | + | ||
| 172 | + // We can crop if the current window size is smaller than the next window | ||
| 173 | + // size. One way to tell is by comparing to 1.0 the X and Y scaling | ||
| 174 | + // coefficients in the current-to-next transformation matrix. | ||
| 175 | + bool can_crop_by_x = niri_curr_geo_to_next_geo[0][0] <= 1.0; | ||
| 176 | + bool can_crop_by_y = niri_curr_geo_to_next_geo[1][1] <= 1.0; | ||
| 177 | + bool crop = can_crop_by_x && can_crop_by_y; | ||
| 178 | + | ||
| 179 | + vec4 color; | ||
| 180 | + | ||
| 181 | + if (crop) { | ||
| 182 | + // However, when we crop, we also want to crop out anything outside the | ||
| 183 | + // current geometry. This is because the area of the shader is unspecified | ||
| 184 | + // and usually bigger than the current geometry, so if we don't fill pixels | ||
| 185 | + // outside with transparency, the texture will leak out. | ||
| 186 | + // | ||
| 187 | + // When crossfading, this is not an issue because the area outside will | ||
| 188 | + // correspond to client-side decoration shadows, which are already supposed | ||
| 189 | + // to be outside. | ||
| 190 | + if (coords_curr_geo.x < 0.0 || 1.0 < coords_curr_geo.x || | ||
| 191 | + coords_curr_geo.y < 0.0 || 1.0 < coords_curr_geo.y) { | ||
| 192 | + color = vec4(0.0); | ||
| 193 | + } else { | ||
| 194 | + color = texture2D(niri_tex_next, coords_crop.st); | ||
| 195 | + } | ||
| 196 | + } else { | ||
| 197 | + // If we can't crop, then crossfade. | ||
| 198 | + color = texture2D(niri_tex_next, coords_stretch.st); | ||
| 199 | + vec4 color_prev = texture2D(niri_tex_prev, coords_stretch_prev.st); | ||
| 200 | + color = mix(color_prev, color, niri_clamped_progress); | ||
| 201 | + } | ||
| 202 | + | ||
| 203 | + return color; | ||
| 204 | +} | ||
| 205 | + | ||
| 162 | // This is the function that you must define. | 206 | // This is the function that you must define. |
| 163 | vec4 resize_color(vec3 coords_curr_geo, vec3 size_curr_geo) { | 207 | vec4 resize_color(vec3 coords_curr_geo, vec3 size_curr_geo) { |
| 164 | // You can pick one of the example functions or write your own. | 208 | // You can pick one of the example functions or write your own. |