summaryrefslogtreecommitdiff
path: root/examples/spheres/shaders/link.vert
blob: d4582e46bc8bfce825884f912b31cd8c24f97706 (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
#version 450
#extension GL_GOOGLE_include_directive : require
// The FM pair, drawn: from the modulating body to its carrier, a ribbon
// as wide and bright as the index is high, shading from the
// modulator's colour to the carrier's. Six vertices for each of the six
// ordered pairs; uncoupled pairs collapse to nothing.
layout(location = 0) out vec4 color;
layout(location = 1) out vec2 along;
layout(location = 2) flat out float time;

#include "SkyView.glsl"

layout(set = 0, binding = 0) readonly buffer Bodies { vec4 bodies[]; };
// layouts.voices_*: coupling rows now at 9..11.
layout(set = 0, binding = 1) readonly buffer Voices { vec4 voices[]; };

// Match main.zig's palette.
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));
// Pairs (carrier, modulator).
const ivec2 pairs[6] = ivec2[](ivec2(0, 1), ivec2(0, 2), ivec2(1, 0), ivec2(1, 2), ivec2(2, 0), ivec2(2, 1));

void main() {
    ivec2 pair = pairs[uint(gl_VertexIndex) / 6u];
    uint corner = uint(gl_VertexIndex) % 6u;
    float amount = voices[9 + pair.x][pair.y];
    time = u.time;
    if (amount < 0.02) {
        gl_Position = vec4(0.0, 0.0, 2.0, 1.0);
        color = vec4(0.0);
        along = vec2(0.0);
        return;
    }
    // Corners 0, 3, 5 at the modulator, 1, 2, 4 at the carrier; 0, 1, 3 on
    // one side.
    bool far_end = corner == 1u || corner == 2u || corner == 4u;
    bool side = corner == 2u || corner == 4u || corner == 5u;
    vec2 from = (bodies[pair.y].xy - u.center) / u.per_pixel;
    vec2 to = (bodies[pair.x].xy - u.center) / u.per_pixel;
    vec2 d = to - from;
    float len = max(length(d), 1e-3);
    vec2 normal = vec2(-d.y, d.x) / len;
    float width = u.radius * (0.4 + min(amount, 1.6));
    vec2 at = (far_end ? to : from) + normal * (side ? width : -width);
    gl_Position = vec4(at * 2.0 / u.size, 0.0, 1.0);
    along = vec2(far_end ? len : 0.0, side ? 1.0 : -1.0);
    color = vec4(mix(palette[pair.y], palette[pair.x], far_end ? 1.0 : 0.0), 1.0) * u.alpha * clamp(amount, 0.0, 1.0);
}