// Geometric family: a field of extruded blocks seen isometrically, rising and
// falling on the low end. A solid, lit surface with a light direction — three
// faces per block, and the whole grid reads as a landscape rather than a chart.
//
// Floating Geometry is bodies adrift in space with nothing under them. This is
// the opposite proposition: everything is on one grid, everything touches the
// ground, and the only freedom any block has is its height. The plan of each
// block is the track's signature form, so a hexagonal video gets a honeycomb of
// columns rather than a chessboard of them.

export const isometricBlocks = {
    name: 'Isometric Blocks',
    family: 'geometric',
    kind: 'fragment',
    texture: 0.6,
    traits: ['shape', 'camera', 'style'],

    params: {
        grid:    { type: 'float', range: [2, 10],      default: 4.5,  uniform: 'u_grid', bias: 'density', slowAxis: true },
        height:  { type: 'float', range: [0.1, 0.7],   default: 0.35, uniform: 'u_height', bias: 'energy' },
        plan:    { type: 'float', range: [0.2, 0.5],   default: 0.4,  uniform: 'u_plan' },
        tilt:    { type: 'float', range: [0.35, 0.75], default: 0.55, uniform: 'u_isoTilt' },
        light:   { type: 'float', range: [0, 1.4],     default: 0.65, uniform: 'u_light' },
        wave:    { type: 'float', range: [0, 1],       default: 0.5,  uniform: 'u_wave' },
        roll:    { type: 'float', range: [0.01, 0.08], default: 0.035, uniform: 'u_roll', bias: 'motion', rate: true },
        palette: { type: 'palette', count: 5 },
    },

    // The low end deliberately does NOT drive the block heights. Measured at
    // aggressive settings that read as 4 flashes a second: every tower in the
    // frame grows and shrinks together, which is a whole-frame luminance cycle
    // on the kick and exactly what the flash ceiling exists to stop. The beat
    // lands on a quarter of the tops instead — same reading, local swing.
    reactive: {
        light: { feature: 'loudness', amount: 0.25, response: 'smooth' },
    },

    shader: `
// How tall the block at this cell stands, in screen units. A travelling wave
// plus a fixed per-cell character, so the field has a rhythm running through it
// and a skyline that is still recognisable between beats.
float columnHeight(vec2 cell, float t) {
    float own = hash12(cell + u_seed);
    float wave = sin(dot(cell, vec2(0.7, 0.5)) - t * 1.1) * 0.5 + 0.5;
    // Squared, so most of the field lies flat and the tall ones stand out as
    // individual towers. A linear height makes every column tall enough to
    // occlude its neighbours, and the field turns into vertical stripes.
    return u_height * pow(mix(own, wave, u_wave), 2.4);
}

vec4 scene(vec2 uv, vec2 p) {
    float t = u_time * u_roll + u_seed;
    p = sigCamera(p);

    float tilt = max(u_isoTilt, 0.1);
    float cw = 1.0 / u_grid;                        // one cell across, on screen

    vec3 col = mix(pal(0) * 0.06, pal(1) * 0.14, sat(p.y * 0.4 + 0.5));

    // A block standing on row r covers screen rows ABOVE r's ground line, so a
    // pixel can only belong to a row at or behind it. Walking a fixed number of
    // rows from the back forward and overwriting as we go is the painter's
    // algorithm, and it is the whole of the depth sorting an isometric field
    // needs — no sorting, no z buffer, just the order of the loop.
    float colX = floor(p.x / cw);
    float fx = (p.x / cw - colX - 0.5) * cw;        // screen offset within the column

    // Alternate columns are staggered half a row, which is what interlocks the
    // field into a lattice instead of leaving it a set of independent stacks.
    float stagger = mod(colX, 2.0) * 0.5;
    float rowHere = floor(p.y / (tilt * cw) - stagger);

    // Rows FURTHER AWAY stand higher up the frame, so a pixel can only belong
    // to a block whose ground line is at or below it — row <= rowHere. Counting
    // down from there draws the far ones first and lets the near ones paint
    // over them.
    // lint: fixed-cost — eight rows of depth, back to front
    for (int j = 7; j >= 0; j--) {
        float row = rowHere - float(7 - j);
        vec2 cell = vec2(colX, row);

        float groundY = (row + stagger) * tilt * cw; // where this cell meets the ground
        float h = columnHeight(cell, t);
        float dy = p.y - groundY;                   // height up the block face

        // Side wall: the plan swept up from the ground line to the top face.
        float inX = smoothstep(u_plan * cw, u_plan * cw * 0.85, abs(fx));
        float wall = inX * step(0.0, dy) * step(dy, h);

        float lit = 0.5 + 0.5 * sat(fx / (u_plan * cw) * 0.8 + 0.5);
        // The wall darkens toward the ground, which is the only shading cue
        // that tells a tall block from a short one at a glance.
        // Neither face takes the loudness. Anything that multiplies a face
        // colour multiplies most of the frame, and a field of blocks brightening
        // together on the kick measured at 5 flashes a second. The light level
        // is expressed on the EDGES, which are a few percent of the image.
        vec3 side = mix(pal(0) * 0.5, pal(2) * 0.8, lit) * 0.75;
        side *= 0.45 + 0.55 * sat(dy / max(h, 1e-3));
        col = mix(col, side, wall);

        // Top face: the track's signature form, lying flat, lifted by h and
        // squashed by the tilt — so every block in the field is the same
        // silhouette as every other subject in the video.
        vec2 q = vec2(fx, (p.y - groundY - h) / tilt);
        float dTop = sigShape(q / (u_plan * cw)) * (u_plan * cw);
        float topMask = smoothstep(0.004, -0.004, dTop);

        vec3 top = mix(pal(2), pal(3), sat(h / max(u_height, 1e-3))) * 0.8;
        col = mix(col, top, topMask);
        // A quarter of the blocks answer the beat, chosen by cell rather than
        // by anything global, so the field has a pulse without the frame having
        // a flash.
        float picked = step(0.75, hash12(cell * 3.17 + 5.0));
        col += pal(3) * topMask * picked * u_beat * 0.45;
        col += pal(4) * sigEdge(dTop) * (0.2 + u_light * 1.2) * step(0.0, dy);
    }

    col *= 0.75 + 0.25 * exp(-dot(p, p) * 0.3);
    col += sigGrain(uv);
    return vec4(col, 1.0);
}
`,
};

export default isometricBlocks;
