|
| 1 | +// The arrangement timeline (models/Timeline.lua): one draw of instanced |
| 2 | +// rounded rects, a section on the ruler or a block on its lane, into layer 1, |
| 3 | +// then the finishing pass adds the bar grid and the fixed playhead. Blocks |
| 4 | +// slide right to left: every position is a set bar, the playhead advances by |
| 5 | +// its speed times `inputs.age` between updates, so the strip scrolls evenly |
| 6 | +// on every display frame. |
| 7 | +// |
| 8 | +// values: [0] playhead (set bar), [1] bars per second, [2] lane rows, |
| 9 | +// [3] bars in view, [4] bars behind the playhead, [5] ruler height |
| 10 | +// as a fraction of the view, [6] display scale (pixels per point) |
| 11 | +// data: per instance row (-1 is the ruler), first bar, bars, colour |
| 12 | + |
| 13 | +// Family and section tints: the system palette in its dark appearance. |
| 14 | +constant float3 TIMELINE_COLOURS[10] = { |
| 15 | + float3(1.000, 0.216, 0.373), // drums: pink |
| 16 | + float3(1.000, 0.624, 0.039), // bass: orange |
| 17 | + float3(0.749, 0.353, 0.949), // chords: purple |
| 18 | + float3(0.353, 0.784, 0.980), // melody: cyan |
| 19 | + float3(1.000, 0.839, 0.039), // structure: yellow |
| 20 | + float3(0.039, 0.518, 1.000), // intro: blue |
| 21 | + float3(1.000, 0.839, 0.039), // build: yellow |
| 22 | + float3(1.000, 0.271, 0.227), // drop: red |
| 23 | + float3(0.251, 0.784, 0.878), // breakdown: teal |
| 24 | + float3(0.557, 0.557, 0.576), // outro: grey |
| 25 | +}; |
| 26 | +constant float TIMELINE_GAP = 1.0; // points between neighbouring blocks |
| 27 | +constant float TIMELINE_RADIUS = 2.5; // block corner radius in points |
| 28 | +constant float TIMELINE_RESTING = 0.45; // a block's opacity away from the playhead |
| 29 | +constant float TIMELINE_PAST = 0.5; // what has played dims further |
| 30 | +constant float TIMELINE_GRID = 0.06; // a bar line's opacity; phrase lines double it |
| 31 | + |
| 32 | +struct TimelineBlock { |
| 33 | + float4 position [[position]]; |
| 34 | + float2 pixel; |
| 35 | + float4 rect [[flat]]; // left, top, right, bottom in pixels |
| 36 | + float3 colour [[flat]]; |
| 37 | + float strength [[flat]]; |
| 38 | +}; |
| 39 | + |
| 40 | +static float timelinePlayhead(constant ShaderInputs &inputs) { |
| 41 | + return inputs.values[0] + inputs.values[1] * inputs.age; |
| 42 | +} |
| 43 | + |
| 44 | +vertex TimelineBlock timelineBlockVertex(uint vid [[vertex_id]], uint iid [[instance_id]], |
| 45 | + constant ShaderInputs &inputs [[buffer(0)]], constant float *data [[buffer(1)]]) { |
| 46 | + float row = data[iid * 4], start = data[iid * 4 + 1], length = data[iid * 4 + 2]; |
| 47 | + int colour = int(data[iid * 4 + 3]); |
| 48 | + float playhead = timelinePlayhead(inputs); |
| 49 | + float rows = max(inputs.values[2], 1.0), window = inputs.values[3], left = playhead - inputs.values[4]; |
| 50 | + float ruler = inputs.values[5], scale = inputs.values[6]; |
| 51 | + float2 size = inputs.size; |
| 52 | + float lane = (1.0 - ruler) / rows; |
| 53 | + float top = row < 0.0 ? 0.0 : ruler + row * lane; |
| 54 | + float bottom = row < 0.0 ? ruler : top + lane; |
| 55 | + float gap = TIMELINE_GAP * scale * 0.5; |
| 56 | + float4 rect = float4((start - left) / window * size.x + gap, top * size.y + gap, |
| 57 | + (start + length - left) / window * size.x - gap, bottom * size.y - gap); |
| 58 | + const uint corners[6] = {0, 1, 2, 2, 1, 3}; |
| 59 | + uint corner = corners[vid]; |
| 60 | + float2 p = float2((corner & 1) ? rect.z : rect.x, (corner & 2) ? rect.w : rect.y); |
| 61 | + bool playing = start <= playhead && playhead < start + length; |
| 62 | + TimelineBlock out; |
| 63 | + out.position = float4(p.x / size.x * 2.0 - 1.0, 1.0 - p.y / size.y * 2.0, 0.0, 1.0); |
| 64 | + out.pixel = p; |
| 65 | + out.rect = rect; |
| 66 | + out.colour = TIMELINE_COLOURS[colour]; |
| 67 | + // The playing block brightens; what has played dims. |
| 68 | + out.strength = playing ? 1.0 : (start + length <= playhead ? TIMELINE_RESTING * TIMELINE_PAST : TIMELINE_RESTING); |
| 69 | + return out; |
| 70 | +} |
| 71 | + |
| 72 | +fragment float4 timelineBlockFragment(TimelineBlock in [[stage_in]], constant ShaderInputs &inputs [[buffer(0)]]) { |
| 73 | + float2 centre = (in.rect.xy + in.rect.zw) * 0.5; |
| 74 | + float2 extent = max((in.rect.zw - in.rect.xy) * 0.5, float2(0.0)); |
| 75 | + float radius = min(TIMELINE_RADIUS * inputs.values[6], min(extent.x, extent.y)); |
| 76 | + float2 q = abs(in.pixel - centre) - extent + radius; |
| 77 | + float d = length(max(q, 0.0)) + min(max(q.x, q.y), 0.0) - radius; |
| 78 | + float alpha = saturate(0.5 - d) * in.strength; |
| 79 | + return float4(in.colour * alpha, alpha); |
| 80 | +} |
| 81 | + |
| 82 | +fragment float4 timeline(ShaderVertex in [[stage_in]], constant ShaderInputs &inputs [[buffer(0)]], |
| 83 | + texture2d<float> blocks [[texture(0)]]) { |
| 84 | + constexpr sampler s(filter::nearest, address::clamp_to_edge); |
| 85 | + float4 colour = blocks.sample(s, in.uv, level(0.0)); |
| 86 | + float window = inputs.values[3], behind = inputs.values[4], ruler = inputs.values[5]; |
| 87 | + float barsPerPixel = window / inputs.size.x; |
| 88 | + float bar = timelinePlayhead(inputs) - behind + in.uv.x * window; |
| 89 | + // Bar lines under the lanes, stronger on each eight-bar phrase. |
| 90 | + float fromLine = abs(fract(bar + 0.5) - 0.5) / barsPerPixel; |
| 91 | + float phrase = abs(fract(bar / 8.0 + 0.5) - 0.5) * 8.0 / barsPerPixel < 1.0 ? 2.0 : 1.0; |
| 92 | + float grid = in.uv.y > ruler ? saturate(1.0 - fromLine) * TIMELINE_GRID * phrase : 0.0; |
| 93 | + colour += float4(grid) * (1.0 - colour.a); |
| 94 | + // The fixed playhead. |
| 95 | + float head = saturate(1.5 * inputs.values[6] - abs(in.uv.x - behind / window) * inputs.size.x); |
| 96 | + return mix(colour, float4(1.0), head * 0.9); |
| 97 | +} |
0 commit comments