summaryrefslogtreecommitdiff
path: root/examples/spheres/shaders/trail.vert
blob: 7f9f35c9f9df9ff0f2f9e14f5ecf7fac08d7c227 (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
#version 450
#extension GL_GOOGLE_include_directive : require
// System 0's trails: per body, a ribbon through its last positions,
// `radius` pixels wide at the newest and tapering to nothing at the oldest,
// fading as it goes. Six vertices per segment; segments past what has been
// filled, or across a jump, collapse to nothing.
layout(location = 0) out vec4 color;

#include "SkyView.glsl"

layout(set = 0, binding = 0) readonly buffer Trail { vec2 trail[]; };

// Match layouts.zig and main.zig's palette.
const uint TRAIL = 1024u;
const vec3 palette[3] = vec3[](vec3(0.40, 0.85, 1.0), vec3(1.0, 0.45, 0.82), vec3(1.0, 0.80, 0.35));

void main() {
    uint segment = uint(gl_VertexIndex) / 6u;
    uint corner = uint(gl_VertexIndex) % 6u;
    uint body = segment / (TRAIL - 1u);
    uint age = segment % (TRAIL - 1u);
    if (age + 1u >= u.filled) {
        gl_Position = vec4(0.0, 0.0, 2.0, 1.0);
        color = vec4(0.0);
        return;
    }
    // Corners 0, 3, 5 at the newer end, 1, 2, 4 at the older; 0, 1, 3 on
    // one side, 2, 4, 5 on the other.
    bool older = corner == 1u || corner == 2u || corner == 4u;
    bool side = corner == 2u || corner == 4u || corner == 5u;
    uint a = (u.head + TRAIL - age) % TRAIL;
    uint b = (u.head + TRAIL - age - 1u) % TRAIL;
    // A jump (system 0 stepping into a snapshot) breaks the trail.
    if (distance(trail[body * TRAIL + a], trail[body * TRAIL + b]) > u.gap) {
        gl_Position = vec4(0.0, 0.0, 2.0, 1.0);
        color = vec4(0.0);
        return;
    }
    vec2 pa = (trail[body * TRAIL + a] - u.center) / u.per_pixel;
    vec2 pb = (trail[body * TRAIL + b] - u.center) / u.per_pixel;
    vec2 along = pb - pa;
    float len = length(along);
    vec2 normal = len > 1e-4 ? vec2(-along.y, along.x) / len : vec2(0.0, 1.0);
    float fade = 1.0 - float(older ? age + 1u : age) / float(u.filled);
    vec2 at = (older ? pb : pa) + normal * (side ? 1.0 : -1.0) * u.radius * fade;
    gl_Position = vec4(at * 2.0 / u.size, 0.0, 1.0);
    color = vec4(palette[body], 1.0) * u.alpha * fade * fade;
}