summaryrefslogtreecommitdiff
path: root/addons/snake_shader/snake.post.frag
blob: 226e98defd25f1e373339e79de0dea22f6578dbf (plain) (blame)
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
#version 450
layout(location = 0) in vec2 uv;
layout(location = 0) out vec4 color;
layout(set = 2, binding = 0) uniform sampler2D board;
layout(std140, set = 3, binding = 0) uniform Clock { vec4 phase; } clock;

vec3 bright(vec2 at) {
    vec3 rgb = texture(board, at).rgb;
    return rgb * smoothstep(.16, .65, max(rgb.r, max(rgb.g, rgb.b)));
}
vec3 mapColor(vec3 rgb) {
    // Soft highlights with a cool shadow tint; retain the dark board floor.
    rgb *= vec3(.98, 1.02, 1.06);
    return clamp((rgb * (2.51 * rgb + .03)) / (rgb * (2.43 * rgb + .59) + .14), 0, 1);
}
void main() {
    vec2 size = vec2(textureSize(board, 0));
    vec2 pixel = uv * size;
    vec2 pitch = vec2(3, 4);
    vec2 sampleUV = (floor(pixel / pitch) + .5) * pitch / size;
    vec4 source = texture(board, sampleUV);
    vec3 bloom = vec3(0);
    vec2 stepUV = vec2(4) / size;
    for (int y = -1; y <= 1; y++) for (int x = -1; x <= 1; x++) {
        float weight = float((x == 0 ? 2 : 1) * (y == 0 ? 2 : 1));
        bloom += bright(sampleUV + vec2(x, y) * stepUV) * weight / 16;
    }
    vec3 rgb = mapColor(source.rgb + bloom * .24);
    vec3 mask = vec3(.65);
    mask[int(mod(floor(pixel.x), 3))] = 1.45;
    float scanline = mix(.5, 1, smoothstep(.15, .35, fract(pixel.y / 4)));
    float shimmer = 1 + .006 * sin(clock.phase.x * 18.849556 + pixel.y * .12);
    color = vec4(rgb * mask * scanline * shimmer, source.a);
}