summaryrefslogtreecommitdiff
path: root/config/picom/shaders/glitch.glsl
blob: be28f787049acbc0d030c0ee87dd7e0f6c7fb0bb (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#version 330


int delta = 20;
float barh = 0.02;
float barw = 0.4;
int nbar = 16;

float maxoff = 5;
float minoff = 2;

in vec2 texcoord;             // texture coordinate of the fragment

uniform sampler2D tex;        // texture of the window
ivec2 window_size = textureSize(tex, 0);
ivec2 window_center = ivec2(window_size.x/2, window_size.y/2);

// Default window post-processing:
// 1) invert color
// 2) opacity / transparency
// 3) max-brightness clamping
// 4) rounded corners
vec4 default_post_processing(vec4 c);

uniform float time; // Time in miliseconds.

float alpha = round(time/delta); // Like time, but in seconds and resets to 

// Pseudo-random function (from original shader)
float random(float n) {
    return fract(sin(n) * 43758.5453f);
}

float get_box() {
    float n = random(alpha)*(nbar);

    for(int i=0;i<n;i++){
        float y = random(mod(alpha, 2048)+i) * window_size.y;
        float x = random(mod(alpha+128, 2048)+i) * window_size.x;
        float w = random(mod(alpha+64, 2048)+i) * barw*window_size.x;
        float h = random(mod(alpha+32, 2048)+i) * barh*window_size.y;
        if (texcoord.y > y && texcoord.y < y + h
         && texcoord.x > x && texcoord.x < x + w) {
            return i*w*h*y*x*n;
        }
    }
    return -1.0f;
}

float rand_offset(float b) {
    return (random(b*64) - 0.5) * (maxoff*2);
}

vec4 window_shader() {
    float b = get_box();

    if (b == -1.0) {
        vec4 c = texelFetch(tex, ivec2(texcoord), 0);
        return default_post_processing(c);
    }
    //b = random(mod(alpha, 2000));

    // Offsets in pixels for each color
    vec2 uvr = vec2(rand_offset(b*1), rand_offset(b*6));
    vec2 uvg = vec2(rand_offset(b*2),rand_offset(b*7));
    vec2 uvb = vec2(rand_offset(b*3),rand_offset(b*8));

    // Calculate offset coords
    uvr += texcoord;
    uvg += texcoord;
    uvb += texcoord;

    // Fetch colors using offset coords
    vec3 offset_color;
    offset_color.x = texelFetch(tex, ivec2(uvr), 0).x;
    offset_color.y = texelFetch(tex, ivec2(uvg), 0).y;
    offset_color.z = texelFetch(tex, ivec2(uvb), 0).z;
    
    // Set the new color
    vec4 c;
    c.w = texelFetch(tex, ivec2(uvr), 0).w;
    c.xyz = offset_color;

    return default_post_processing(c);
}