summaryrefslogtreecommitdiff
path: root/addons/snake_shader/snake.frag
blob: 14896f2fe62b22e8c5e9c2e0fffdf9747a219a89 (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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#version 450
layout(location = 0) in vec2 uv;
layout(location = 0) out vec4 color;
layout(std140, set = 3, binding = 0) uniform Board {
    vec4 status; // dead, focused, target width, target height
    vec4 points; // head xy, food xy
    vec4 grid; // board size, hovered cell including walls
    vec4 motion; // seconds, tile pixels, board offset xy
    uvec4 rows[128];
} board;
float box(vec2 p, float size, float radius) {
    vec2 d = abs(p) - size + radius;
    return length(max(d, 0)) + min(max(d.x, d.y), 0) - radius;
}
void main() {
    vec2 p = (uv * board.status.zw - board.motion.zw) / board.motion.y;
    vec3 bg = vec3(.018, .028, .043);
    float dim = board.status.y < .5 ? .6 : 1;
    if (any(lessThan(p, vec2(0))) || any(greaterThanEqual(p, board.grid.xy + 2))) {
        color = vec4(bg * dim, 1);
        return;
    }
    ivec2 cell = ivec2(floor(p)) - 1;
    vec2 local = fract(p) - .5;
    vec3 ink = vec3(.10, .16, .23);
    float dist = box(local, .43, .10);
    bool inside = cell.x >= 0 && cell.x < int(board.grid.x) && cell.y >= 0 && cell.y < int(board.grid.y);
    if (inside) {
        dist = 1;
        if ((board.rows[cell.y][cell.x / 32] & (1u << uint(cell.x % 32))) != 0u) {
            ink = board.status.x > .5 ? vec3(.85, .12, .20) : vec3(.12, .78, .43);
            dist = box(local, .44, .16);
            if (all(equal(cell, ivec2(board.points.xy)))) ink *= 1.18;
        } else if (all(equal(cell, ivec2(board.points.zw)))) {
            ink = vec3(1, .42, .10) * (1 + .035 * sin(board.motion.x * 1.5707963));
            dist = length(local) - .26;
        } else {
            bg += vec3(.008, .014, .018) * (1 - smoothstep(.44, .49, max(abs(local.x), abs(local.y))));
        }
    }
    float aa = max(fwidth(dist), .002);
    float coverage = 1 - smoothstep(-aa, aa, dist);
    vec3 rgb = mix(bg + ink * .10 * exp(-max(dist, 0) * 8), ink, coverage);
    if (board.grid.z >= 0) {
        vec2 delta = p - (board.grid.zw + .5);
        float halo = exp(-dot(delta, delta) * 2.5);
        float rim = 1 - smoothstep(.015, .07, abs(box(local, .45, .09)));
        bool hovered = all(equal(ivec2(floor(p)), ivec2(board.grid.zw)));
        rgb += vec3(.12, .30, .36) * halo + (hovered ? vec3(.14, .32, .40) * rim : vec3(0));
    }
    color = vec4(rgb * dim, 1);
}