aboutsummaryrefslogtreecommitdiffstats
path: root/desktop/modules/appearance/shatter.frag
blob: 4c9e40dfd6047505b492932ee05d5702524fa362 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Copyright (C) 2026 Danilo M. <danix@danix.xyz>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2 as
// published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// The old wallpaper breaking into Voronoi shards that shrink, slide away from
// where the blow landed and fade, each on its own delay. A shard only moves
// inside the room its shrinking leaves, so a pixel needs one cell lookup to
// find it, rather than a search of the neighbours. Compiled with:
// qsb --qt6 -o shatter.frag.qsb shatter.frag
#version 440
layout(location = 0) in vec2 qt_TexCoord0;
layout(location = 0) out vec4 fragColor;
layout(std140, binding = 0) uniform buf {
    mat4 qt_Matrix;
    float qt_Opacity;
    float progress;
    float aspect;
    vec2 origin;
};
layout(binding = 1) uniform sampler2D source;

vec2 hash2(vec2 p) {
    return fract(sin(vec2(dot(p, vec2(127.1, 311.7)), dot(p, vec2(269.5, 183.3)))) * 43758.5453);
}

// The nearest seed to p: its cell in xy, its position in zw.
vec4 cell(vec2 p) {
    vec2 i = floor(p), f = fract(p);
    float best = 9.0;
    vec4 r = vec4(0.0);
    for (int y = -1; y <= 1; y++) {
        for (int x = -1; x <= 1; x++) {
            vec2 g = vec2(float(x), float(y));
            vec2 s = g + hash2(i + g);
            float d = dot(s - f, s - f);
            if (d < best) { best = d; r = vec4(i + g, i + s); }
        }
    }
    return r;
}

void main() {
    const float n = 6.0;
    vec2 asp = vec2(aspect, 1.0);
    vec2 p = qt_TexCoord0 * asp * n;
    vec4 c = cell(p);
    vec2 seed = c.zw;

    vec2 away = seed / n - origin * asp;
    float d = clamp(length(away) / length(max(origin, 1.0 - origin) * asp), 0.0, 1.0);
    float l = clamp((progress - 0.6 * mix(d, hash2(c.xy).x, 0.3)) / 0.4, 0.0, 1.0);

    float s = 1.0 - 0.85 * l;
    vec2 q = seed + (p - seed - normalize(away + 1e-4) * (1.0 - s) * 0.4) / s;
    if (cell(q).xy != c.xy) { fragColor = vec4(0.0); return; }

    float a = 1.0 - smoothstep(0.4, 1.0, l);
    fragColor = vec4(texture(source, q / (asp * n)).rgb * a, a) * qt_Opacity;
}