// STAGE: the soloist. One member of the cast, large, centred, held.
//
// Every video needs a shot where you can actually see what the song is made of.
// This is that shot: the protagonist at full size, breathing on the bar, with
// its own echoes behind it.

export const soloist = {
    name: 'Soloist',
    family: 'minimal',
    kind: 'fragment',
    consumes: ['cast', 'ink', 'staging'],
    texture: 0.5,
    traits: ['shape', 'camera', 'style'],

    params: {
        size:     { type: 'float', range: [0.25, 0.85], default: 0.45, uniform: 'u_size' },
        echoes:   { type: 'int',   range: [0, 6],       default: 2,    uniform: 'u_echoes', bias: 'density' },
        echoStep: { type: 'float', range: [0.05, 0.5],  default: 0.18, uniform: 'u_echoStep' },
        turn:     { type: 'float', range: [0, 0.8],     default: 0.12, uniform: 'u_turn', bias: 'motion', rate: true },
        offset:   { type: 'vec2',  range: [-0.4, 0.4],  default: [0, 0], uniform: 'u_offset' },
        palette:  { type: 'palette', count: 4 },
    },

    reactive: {
        size: { feature: 'beat', amount: 0.1, response: 'spike' },
    },

    shader: `
vec4 scene(vec2 uv, vec2 p) {
    p = sigCamera(p);
    p = sigFolded(p);
    float t = u_time * u_turn + u_seed;
    // Even a close-up obeys the song's sense of scale — pulled toward it rather
    // than replaced by it, since a soloist that shrinks to a dot stops being one.
    float size0 = u_size * mix(1.0, stageScale(), 0.5);

    vec3 col = mix(pal(0) * 0.14, pal(1) * 0.18, length(p) * 0.5);

    // Echoes first, so the soloist reads in front of them.
    for (int i = 6; i >= 1; i--) {
        if (i > u_echoes) continue;
        float fi = float(i);
        float size = size0 * (1.0 + fi * u_echoStep);
        vec2 q = rot(t * (1.0 - fi * 0.15)) * (p - u_offset) / max(size, 1e-3);
        float d = castMain(q) * size;
        float a = inkMask(d, uv) * (0.5 / fi);
        col = mix(col, pal(i + 1), a);
    }

    vec2 q = rot(t) * (p - u_offset) / max(size0, 1e-3);
    float d = castMain(q) * size0;
    col = mix(col, pal(2), inkMask(d, uv));
    col += pal(3) * 0.3 * (1.0 - smoothstep(0.0, size0 * 1.5, abs(d))) * 0.5;

    return vec4(inkValue(col), 1.0);
}
`,
};

export default soloist;
