Branchwell

PulseCanvas / app · pulsecanvas.ai / Sources / PulseCanvas / Resources / NativeDance.metal

1// PulseCanvas native designs: dancing and club visuals (scenes 167-183). Original
2// procedural code; see NativeCommon.metal. Every design is a pure function of its
3// uniforms. Choreography runs on procedural time (c.t) at zero Chaos; above zero the
4// music clock (c.m) adds tempo, bands enlarge specific moves and lights, and attacks
5// trigger local accents (a jump, a flick, a ripple) that ease out with the envelope.
6
7// ---------------------------------------------------------------- shared helpers
8float dnBeat(Ctx c, float rate, float music) { return c.t * rate + c.m * music; }
9// Attack envelope normalised by Chaos (0...1); only used through dnAccent.
10float dnHit(Ctx c, constant NativeFrame &f) { return saturate(c.hit / max(f.camera.y, .05f)); }
11// Smooth accent for moving geometry: zero at the attack onset and at rest, peaking as
12// the envelope decays, so attacks never make shapes jump. Scales with Chaos.
13float dnAccent(Ctx c, constant NativeFrame &f) { float h = dnHit(c, f); return 4 * h * (1 - h) * saturate(f.camera.y * 1.7f); }
14
15float2 dnMul(float2 a, float2 b) { return float2(a.x * b.x - a.y * b.y, a.x * b.y + a.y * b.x); }
16float2 dnPolar(float a) { return float2(cos(a), sin(a)); }
17float dnSq(float x) { return x * x; }
18// Camera (focal length 2) pitched down over a ground plane so the horizon sits at
19// screen height `horizon` (keep it above the canvas). Returns world x, forward z and
20// the ray parameter t (world units per uv unit across the screen).
21constant float DN_FOCAL = 2;
22
23// Projects a point (x, height, z) given relative to the dnFloor camera position. Returns
24// screen xy and a depth z such that a world length L appears L / z long on screen.
25
26// Soft cone of light from src along dir.
27
28
29// ---------------------------------------------------------------- 167 Groove Tiles
30// An illuminated glass dance floor seen at an angle. Tiles glow from beneath in moving
31// patterns (checker, diagonals, ripples, spirals) that crossfade over time; band groups
32// light blocks of tiles; a ring of tiles ripples out from a wandering spot on the music
33// clock and attacks light it up with a spark at its origin, while mirror-ball specks
34// and soft pools of shadow sweep across the glass. Zoom speed slides the floor.
35
36
37// ---------------------------------------------------------------- 168 Mirror Ball
38// A big faceted mirror ball turns high in a hazy room. Each square facet reflects the
39// coloured spotlights aimed at it and twinkles; hundreds of reflected specks sweep the
40// walls. Bass brightens the beams, highs make facets sparkle, attacks bring out extra
41// specks and light a ring of specks flung outward on the music clock. Zoom speed pans
42// the room and turns the ball.
43
44
45// ---------------------------------------------------------------- 169 Drone Ballet
46// Two hundred and fifty-six light drones perform above dark water, flowing between
47// formations (sphere, helix bundle, rippling sheet, nested rings, torus knot). Groups
48// of drones morph on staggered schedules and arc out of each other's way, each drone a
49// glowing point with a soft halo and a short trail, mirrored as shimmering streaks on
50// the water above a faint far skyline. Bands brighten drone rows and sweep colour
51// across the swarm, attacks light a ripple of brightness and displacement through it,
52// the music clock speeds the morphs and Zoom speed orbits and dollies the camera.
53constant int DRONE_U = 32, DRONE_V = 8, DRONE_PU = 4, DRONE_PV = 2;
54
55struct DroneSwarm { int from, to; float morph, t, rippleR, accent, bass, beat; float2 spin; };
56// A patch of drones morphs together: its blend and the arc it lifts along while
57// changing formation are shared, so each patch travels its own path.
58struct DronePatch { float blend; float3 lift; };
59
60// World position (relative to the swarm centre, already spun) of the drone or patch
61// centre at grid coordinate (u, v).
62
63
64// ---------------------------------------------------------------- 170 Ribbon Gymnast
65// Rhythmic-gymnastics ribbons loop, spiral and snake through the air over a dark stage.
66// Each ribbon is the recent path of its stick tip (a sum of rotating phasors whose
67// shape morphs between circles, figure eights and loops), drawn as a twisting band
68// with a bright front and a darker back. Bass widens the loops; attacks whip the
69// tail into a coil. Zoom speed drifts the camera.
70
71
72// ---------------------------------------------------------------- 171 Laser Haze
73// A club laser show: rows of emitters below and above fan thin beams through drifting
74// haze, sweeping, fanning and converging into cones, with hot spots where beams strike
75// the ceiling, above a dark stage edge with a soft footlight glow. Bands widen each
76// emitter's fan, attacks flick fans to a new angle and Zoom speed pans along the rig.
77
78
79// ---------------------------------------------------------------- 172 Moving Heads
80// A concert rig: moving-head fixtures hang from a truss and swing their cones across
81// the stage in choreographed sweeps, projecting rotating gobo patterns onto the floor
82// through haze, with a chasing row of stage-edge lights. Bass widens the cones, highs
83// twinkle the gobos and attacks sweep every head toward centre stage in unison.
84
85
86// ---------------------------------------------------------------- 173 Conga Orbs
87// A conga line of glossy orbs hops along a snaking, looping path across a polished
88// dance floor, each orb squashing on landing and stretching as it springs, with a
89// shadow, a mirror reflection and a glowing track joining the line. Bass raises the
90// hops, highs sparkle the highlights, spinning flips ripple down the line with the
91// music clock and attacks add a hop and light the track. Zoom speed dollies the camera.
92
93
94// ---------------------------------------------------------------- 174 Headspin
95// Breaking moves as pure light painting: four lights carried by an unseen dancer trace
96// two-tone trails that cycle through spin figures every few seconds (stacked head-spin
97// rings with epicycloid loops, windmill circles, figure-eight sweeps, low floor sweeps),
98// crossfading between figures while trails build up and decay. Every so often the spin
99// slows to a freeze and the lights flare. Sparks skid off the floor. Bass brightens the
100// trails and attacks flare the lights and light a ring on the floor.
101struct SpinLimbs { float3 p[4]; };
102// One figure for a foot or hand light, split into the part that flips sign for the
103// opposite light (odd in the spin phasor) and the part both share (even). xy are screen
104// offsets from the spin centre, z is nearness to the camera.
105struct SpinPart { float3 odd, even; };
106
107
108// Feet (0, 1) sit opposite each other on the spin, hands (2, 3) a quarter turn away.
109
110// The spin slows to a near freeze every 24 seconds: rate = TAU * (1.25 - .95 * freeze).
111constant float SPIN_FREEZE = TAU / 24;
112
113
114// ---------------------------------------------------------------- 175 Waltz Pairs
115// A ballroom seen from above: pairs of glowing partners turn around each other with
116// soft trails while the couples travel counter-clockwise around a polished parquet
117// floor that mirrors the chandeliers. The turn accelerates on beat one of each bar of
118// three; bass widens the turns and attacks spin a couple out. Zoom speed slowly turns
119// and dollies the overhead camera.
120
121
122// ---------------------------------------------------------------- 176 Pigment Burst
123// Slow-motion bursts of coloured powder bloom against a dark backdrop, like high-speed
124// photographs of thrown pigment. Six plumes launch on a staggered schedule from points
125// along the lower frame, each billowing outward (self-similar noise that expands with
126// the cloud), curling, lit from one side with a glowing rim, and fading while newer
127// plumes take over, so the frame is always full; overlapping plumes mix their colours.
128// Bass swells and speeds the youngest plume, attacks push it outward, highs add
129// glittering grains and Zoom speed drifts the camera through the clouds.
130float3 pigmentBurst(Ctx c, constant NativeFrame &f) {
131 float zoom = 1 + .08 * sin(c.travel * .08);
132 float2 uv = c.uv / zoom + float2(sin(c.travel * .05) * .35, sin(c.travel * .037) * .15);
133 float3 color = deep(.4 + uv.y * .05, f) * .035 + paint(.5, f) * .05 * exp(-length(uv * float2(.6, 1)) * 1.2);
134 // Fine pigment dust hangs everywhere from earlier bursts.
135 float dust = fbm2(uv * .9 + float2(c.t * .02, -c.t * .015), 3);
136 color += paint(dust * 1.2 + .1, f) * (.035 + .05 * dust);
137 const int plumes = 6;
138 const float life = 7.5;
139 float clock = (c.t + c.m * 1.6) / life;
140 float accent = dnAccent(c, f);
141 float2 light = normalize(float2(-.6, .8));
142 float density = 0;
143 // Oldest plume first so younger plumes billow in front.
144 int oldest = (plumes - 1 - int(floor(fract(clock) * plumes))) % plumes;
145 for (int m = 0; m < plumes; ++m) {
146 int k = (oldest - m + plumes) % plumes;
147 float phase = clock + float(k) / plumes;
148 float age = fract(phase);
149 float generation = floor(phase);
150 float3 seed = hash33(float3(generation, float(k), 7.1));
151 float young = exp(-age * 5);
152 float grow = 1 - exp(-age * (3.2 + 2.5 * c.b.x * young));
153 float2 launch = float2(((float(k) + seed.x) / plumes * 2 - 1) * .9 * c.extent.x, c.extent.y * (seed.y * 1.1 - .85));
154 float2 dir = normalize(float2(-launch.x * .3 + (seed.z - .5) * .9, .8 - launch.y * .4));
155 float2 center = launch + dir * grow * (.45 + .35 * seed.y) * c.extent.y;
156 float R = (.2 + .8 * grow) * c.size * (1 + .45 * c.b.x * young + .35 * accent * young) * 1.1;
157 float2 q = rot(uv - center, (seed.z - .5) * 1.6 * age + seed.x * 6) / R;
158 float r = length(q);
159 if (r > 1.7) continue;
160 float fade = smoothstep(0.f, .06f, age) * pow(1 - age, 1.3f);
161 // Billows: noise that expands with the plume and drifts outward as it grows.
162 float2 nq = q * 1.7 * (1 - .35 * age) + seed.xy * 20;
163 float billow = fbm2(nq, 5);
164 float bump = (billow - fbm2(nq + light * .07, 5)) * 5;
165 float edge = r + (billow - .5) * 1.25 + (noise2(nq * 3.3) - .5) * .3;
166 float body = smoothstep(1.f, .72f, edge);
167 float thick = smoothstep(1.f, .2f, edge);
168 float2 lq = q + light * .16;
169 float lit = fbm2(lq * 1.7 * (1 - .35 * age) + seed.xy * 20, 3);
170 float toward = smoothstep(1.f, .2f, length(lq) + (lit - .5) * 1.25);
171 float shade = saturate(.5 + (thick - toward) * 2.2 + dot(q, light) * .25 + bump);
172 float rim = smoothstep(.6f, 1.f, edge) * body;
173 float plumeBand = c.b[k % 4];
174 float hue = seed.x * .9 + float(k) * .17 + plumeBand * .2;
175 float3 tint = paint(hue, f) * (1 + plumeBand * .6);
176 float3 plume = mix(deep(hue, f) * .45, tint, shade) + mix(tint, paint(.95, f), .3) * rim * .4;
177 plume *= (.75 + .45 * billow) * (.7 + .4 * thick);
178 color += tint * smoothstep(1.7f, .6f, r) * fade * .05;
179 float alpha = body * fade * .95;
180 color = mix(color, plume, alpha);
181 density = max(density, alpha);
182 }
183 // Glittering grains caught in the clouds.
184 float2 g = uv * 95;
185 float2 gid = floor(g);
186 float gh = hash21(gid);
187 float grain = step(.93f, gh) * pow(.5 + .5 * sin(c.t * (5 + gh * 7) + gh * 60), 10.f);
188 float dotMask = smoothstep(.35f, .1f, length(fract(g) - .5));
189 color += paint(.9 + gh * .1, f) * grain * dotMask * density * (.35 + c.b.w * 3 + c.hit * 2.5);
190 return color;
191}
192
193// ---------------------------------------------------------------- 177 Pirouette Bloom
194// Spinning tulle skirts seen from above, abstracted into flowers: layered frills turn
195// and their pleats flare wider the faster each bloom spins, sequins glint on the hems
196// and a rosette of sequins glows at each heart, every bloom in its own spotlight pool.
197// Bass flares the pleats, attacks pop the sequins and Zoom speed drifts the camera.
198
199
200// ---------------------------------------------------------------- 178 Stretch Membrane
201// One taut, glossy stretch-fabric sheet fills the frame. Unseen performers press into
202// it from behind: seven knee-, shoulder- and palm-sized pushes glide and twist in slow
203// phrases as low, flat-topped swells, with soft broad wrinkles pulled between
204// neighbouring pushes. Everything is shaded from the sheet's own normals: a fine weave
205// everywhere, a warm grazing light from one side and a cool one from the other, long
206// anisotropic stretch highlights along the tension around each push and a coloured
207// rim where the fabric bends at its base. Bass deepens the pushes, attacks raise a
208// small sharp push, highs ripple the sheen and Zoom speed slides the pushes and lights.
209float3 stretchMembrane(Ctx c, constant NativeFrame &f) {
210 float2 p = c.uv;
211 float slide = c.travel * .07;
212 float phrase = dnBeat(c, .3, .8);
213 float depthBoost = 1 + c.b.x * .6;
214 const int pushes = 7;
215 float2 centers[pushes];
216 float amps[pushes];
217 float2 grad = 0.f;
218 float height = 0, rim = 0;
219 float3 tension = 0.f; // structure tensor (xx, yy, xy) of the stretch directions
220 for (int k = 0; k < pushes; ++k) {
221 float fk = float(k);
222 float3 seed = hash33(float3(fk, 3.1, 7.7));
223 float2 center = float2(sin(phrase * (.8 + seed.x * .6) + fk * 2.3 + slide) * .82 * c.extent.x,
224 sin(phrase * (.65 + seed.y * .5) + fk * 1.7 + slide * .6) * .78 * c.extent.y);
225 float press = .35 + .65 * smoothstep(.1f, .8f, .5 + .5 * sin(phrase * 1.2 + fk * 1.9));
226 float size = (.2 + .22 * seed.z) * c.size;
227 float2 axes = float2(size * (1 + .6 * seed.x), size);
228 float twist = c.t * (seed.y - .5) * .8 + fk * 1.3;
229 float A = press * (.7 + .3 * seed.y) * depthBoost * (1 + c.b[k % 4] * 1.2) * size * .62;
230 // A low swell with a flat top that eases out slowly into the sheet.
231 float2 q = rot(p - center, -twist) / axes;
232 float q2 = dot(q, q);
233 float g = 1 / (1 + q2 * q2);
234 height += A * g;
235 float2 slope = rot(-4 * A * g * g * q2 * q / axes, twist);
236 grad += slope;
237 float w = dot(slope, slope);
238 float2 dir = slope * rsqrt(max(w, 1e-8f));
239 tension += float3(dir.x * dir.x, dir.y * dir.y, dir.x * dir.y) * w;
240 rim += exp(-dnSq((sqrt(q2) - 1.15) / .45)) * press;
241 centers[k] = center;
242 amps[k] = A / size;
243 }
244 // Soft, broad wrinkles pulled taut between neighbouring pushes.
245 for (int k = 0; k < pushes; ++k) {
246 int j = (k + 1) % pushes;
247 float2 a = centers[k], b = centers[j];
248 float2 ab = b - a;
249 float len = length(ab);
250 float strength = smoothstep(1.7f, .5f, len) * min(amps[k], amps[j]);
251 if (strength < 1e-4) continue;
252 float h = saturate(dot(p - a, ab) / max(len * len, 1e-4f));
253 float2 along = ab / max(len, 1e-4f), across = float2(-along.y, along.x);
254 float side = dot(p - a - ab * h, across);
255 float width = .16 + .1 * len;
256 float env = sin(PI * h) * exp(-side * side / (width * width)) * strength;
257 float wave = side * 55 + h * 3;
258 float wrinkle = .006 * (1 + c.b.y * 1.6);
259 height += wrinkle * cos(wave) * env;
260 grad += across * (-wrinkle * 55 * sin(wave) * env);
261 tension += float3(along.x * along.x, along.y * along.y, along.x * along.y) * env * env * .2;
262 }
263 // A small sharp push springs up where the music clock points when an attack lands.
264 float2 hitAt = float2(sin(c.m * .9 + 1) * .6 * c.extent.x, cos(c.m * .7) * .5 * c.extent.y);
265 float2 hq = (p - hitAt) / (.1 * c.size);
266 float hinv = 1 / (1 + dot(hq, hq));
267 float hA = dnAccent(c, f) * .1 * c.size * .5;
268 height += hA * hinv * hinv;
269 grad += -4 * hA * hinv * hinv * hinv * hq / (.1 * c.size);
270 // The woven surface: a rib and a fine weave tilt the normals everywhere.
271 float2 warp = normalize(float2(1, .35)), weft = float2(-warp.y, warp.x);
272 float shimmer = 1 + c.b.w * .8 * sin(dot(p, float2(23, 9)) - c.t * 7);
273 float fineWeave = saturate(1.6f - c.px * 420 * .9f);
274 float ribFade = saturate(1.6f - c.px * 150 * .9f);
275 float2 weave = (warp * cos(dot(p, warp) * 420) + weft * cos(dot(p, weft) * 420)) * .05 * fineWeave
276 + warp * cos(dot(p, warp) * 150 + sin(dot(p, weft) * 3) * 2) * .03 * ribFade;
277 float3 n = normalize(float3(-(grad + weave * shimmer), 1));
278 // Stretch direction: the dominant eigenvector of the tension tensor. Fibres run across
279 // it, so highlights streak along the stretch.
280 float stretchAngle = .5 * atan2(2 * tension.z, tension.x - tension.y);
281 float2 stretchDir = float2(cos(stretchAngle), sin(stretchAngle));
282 float stretchAmount = saturate(sqrt(dnSq(tension.x - tension.y) + 4 * tension.z * tension.z) * 5) * smoothstep(.04f, .22f, length(grad));
283 float3 fibre = float3(-stretchDir.y, stretchDir.x, 0);
284 fibre = normalize(fibre - n * dot(fibre, n));
285 float3 view = float3(0, 0, 1);
286 float swing = sin(c.travel * .05) * .25 + sin(c.t * .07) * .15;
287 float3 warmL = normalize(float3(-cos(swing) * .93, .2 + sin(swing) * .3, .36));
288 float3 coolL = normalize(float3(cos(swing) * .93, -.15 + sin(swing) * .2, .36));
289 float3 warmT = paint(.75 + c.b.x * .2, f), coolT = paint(.12 + c.b.z * .25, f) * 1.5;
290 float across = saturate(.5 - .5 * p.x / c.extent.x + .15 * sin(swing * 2));
291 float3 lights[2] = {warmL, coolL};
292 float3 tints[2] = {warmT, coolT};
293 float reach[2] = {.2 + .8 * across, .2 + .8 * (1 - across)};
294 float3 color = deep(.5, f) * .04;
295 for (int i = 0; i < 2; ++i) {
296 float3 L = lights[i];
297 float3 hv = normalize(L + view);
298 float diffuse = max(dot(n, L), 0.f);
299 float sheen = pow(max(dot(n, hv), 0.f), 5.f);
300 float spec = pow(max(dot(n, hv), 0.f), 45.f);
301 float th = dot(fibre, hv);
302 float aniso = pow(sqrt(max(1 - th * th, 0.f)), 140.f) * stretchAmount;
303 color += tints[i] * (diffuse * diffuse * .55 + sheen * .12 + spec * .2 + aniso * 1.5) * reach[i];
304 }
305 // A coloured rim where the fabric bends at the base of each push, and a faint glow
306 // where it is stretched thinnest.
307 color += mix(coolT, warmT, across) * rim * .07 * (1 + .5 * stretchAmount);
308 color += paint(.9, f) * dnSq(saturate(height * 9)) * .12;
309 color += paint(.9, f) * hinv * hinv * c.hit * .45;
310 return color;
311}
312
313// ---------------------------------------------------------------- 179 Kinetic Mirrors
314// A large kinetic wall of small square mirrors on pivots, seen at a slight angle. The
315// tiles tilt in travelling waves (diagonals, rings, spirals), so each reflects a
316// different part of a colourful stage behind the viewer (drifting colour spots, sweeping
317// beams, pin lights) and shimmering waves of colour roll across the wall between thin
318// dark gaps and bevelled edges. Bands drive the different wave patterns, attacks send a
319// ripple of tilts out from a point, highs add glints and Zoom speed pans along the wall.
320float3 mirrorStage(float3 d, Ctx c, constant NativeFrame &f) {
321 float3 color = deep(.4 + d.y * .2, f) * .07 + paint(.3, f) * .025 * (1 + d.y);
322 for (int k = 0; k < 5; ++k) {
323 float fk = float(k);
324 float3 spot = normalize(float3(sin(c.t * (.17 + fk * .03) + fk * 1.3) * .9, sin(c.t * (.13 + fk * .02) + fk * 2.1) * .45 + .05, -1));
325 float a = max(dot(d, spot), 0.f);
326 color += paint(fk * .21 + .05, f) * (pow(a, 34.f) * .75 + pow(a, 400.f) * 2.5);
327 }
328 float az = atan2(d.x, -d.z);
329 float beams = pow(.5 + .5 * cos(az * 7 - c.t * 1.1 + d.y * 2), 24.f) * smoothstep(-.3f, .5f, d.y);
330 color += paint(.55 + .2 * sin(az * 2 + c.t * .2), f) * beams * .45;
331 float2 pin = float2(az * 9, d.y * 9);
332 float2 pid = floor(pin);
333 float ph = hash21(pid);
334 float twinkle = pow(.5 + .5 * sin(c.t * (2 + ph * 4) + ph * 30), 6.f);
335 color += paint(.9, f) * exp(-dot(fract(pin) - .5, fract(pin) - .5) * 60) * step(.7f, ph) * twinkle * (.6 + c.b.w * 3);
336 return color;
337}
338float3 kineticMirrors(Ctx c, constant NativeFrame &f) {
339 float yaw = .3 + .05 * sin(c.t * .07);
340 float3 rd = normalize(float3(c.uv, 2.2));
341 float3 N0 = float3(-sin(yaw), 0, -cos(yaw));
342 float3 T = float3(cos(yaw), 0, -sin(yaw));
343 const float D = 3;
344 float t = dot(float3(0, 0, D), N0) / dot(rd, N0);
345 float3 hit = rd * t;
346 float2 w = float2(dot(hit - float3(0, 0, D), T), hit.y) + float2(c.travel * .15, 0);
347 float tile = .16 * c.size;
348 float2 cell = w / tile;
349 float2 id = floor(cell), lq = fract(cell) - .5;
350 float2 wc = (id + .5) * tile;
351 // Travelling tilt waves; each band drives its own pattern.
352 float2 ringC = float2(sin(c.t * .13) * 1.5 + c.travel * .15, cos(c.t * .11) * .6);
353 float2 rv = wc - ringC;
354 float rr = length(rv);
355 float diag = sin(dot(wc, float2(1.4, .9)) * 2.2 - c.t * 1.7 - c.m * 2.2);
356 float ringW = sin(rr * 4.5 - c.t * 2.3 - c.m * 2.8);
357 float spiral = sin(atan2(rv.y, rv.x) * 3 + rr * 3.2 - c.t * 1.5 - c.m * 1.8);
358 float mixA = .5 + .5 * sin(c.t * .09), mixB = .5 + .5 * sin(c.t * .07 + 2);
359 float wDiag = .45 * mixA + c.b.x * .6, wRing = .45 * (1 - mixA) + c.b.y * .6, wSpiral = .35 * mixB + c.b.z * .6;
360 float2 tilt = float2(diag * wDiag + ringW * wRing * rv.x / max(rr, .2f) + spiral * wSpiral * .7,
361 diag * wDiag * .6 + ringW * wRing * rv.y / max(rr, .2f) + spiral * wSpiral * .7 * cos(rr * 2)) * .3;
362 // Attack ripple from a point on the wall, riding the music clock.
363 float2 hitC = float2(sin(c.m * .8) * 1.2 + c.travel * .15, cos(c.m * .6) * .5);
364 float2 hv = wc - hitC;
365 float hr = length(hv);
366 float front = fract(c.m * .45) * 3;
367 float ripple = exp(-dnSq((hr - front) / .25)) * (1 - front / 3);
368 tilt += hv / max(hr, .05f) * ripple * dnAccent(c, f) * .45;
369 // Bevelled tile edges and dark gaps.
370 float edge = max(abs(lq.x), abs(lq.y));
371 float bevel = smoothstep(.36f, .46f, edge);
372 float2 lean = tilt + sign(lq) * step(abs(lq.yx), abs(lq)) * bevel * .5;
373 float3 n = normalize(N0 + T * lean.x + float3(0, 1, 0) * lean.y);
374 float3 r = reflect(rd, n);
375 float3 color = mirrorStage(r, c, f);
376 color *= .75 + .25 * smoothstep(.5f, .2f, edge);
377 color += paint(.9, f) * c.hit * ripple * 1.2;
378 float px = c.px * t * 1.2 / tile;
379 float gap = smoothstep(.47f - px, .47f + px, edge);
380 color = mix(color, deep(.3, f) * .02, gap * .9);
381 return color * (1 - .35 * smoothstep(3.f, 8.f, t) * c.fog * 2);
382}
383
384// ---------------------------------------------------------------- 180 Fire Dance
385// Fire poi on a night beach, the performers implied only by the light: flaming heads
386// trace circles, flowers and figure eights with flickering fire trails, shedding sparks
387// while embers rise and the sand glows. Bass enlarges the flames, attacks flare the poi heads and light bursts
388// of sparks around them, and highs make the embers flicker. Zoom speed pans the beach.
389float3 fireDance(Ctx c, constant NativeFrame &f) {
390 float2 uv = c.uv;
391 float pan = c.travel * .1;
392 float seaY = .1 * c.extent.y, shoreY = -.22 * c.extent.y;
393 float2 moon = float2(.6 * c.extent.x - sin(pan * .3) * .1, .65 * c.extent.y);
394 float3 color;
395 if (uv.y > seaY) {
396 color = deep(.15 + uv.y * .05, f) * .05 + paint(.3, f) * .07 * exp(-(uv.y - seaY) * 3);
397 float2 star = uv * 90;
398 color += paint(.9, f) * pow(hash21(floor(star)), 90.f) * smoothstep(.3f, .1f, length(fract(star) - .5)) * 1.5;
399 color += paint(.95, f) * (fill(length(uv - moon) - .07, c.px) * .6 + exp(-length(uv - moon) * 6) * .12);
400 } else if (uv.y > shoreY) {
401 float depth = seaY - uv.y;
402 float glint = pow(noise2(float2(uv.x * 4 + pan, depth * 60 / (depth + .1) - c.t * .6)), 6.f);
403 color = deep(.2, f) * .05 + paint(.6, f) * glint * .35 * exp(-depth * 3);
404 color += paint(.9, f) * glint * exp(-abs(uv.x - moon.x) * 4) * (.5 + c.b.z * 1.5);
405 color += paint(.7, f) * .1 * exp(-dnSq((uv.y - shoreY) / .012)) * (.6 + .4 * sin(uv.x * 9 + c.t * .7));
406 } else {
407 float grain = noise2(uv * 140) * .5 + noise2(uv * 9 + 3) * .5;
408 color = deep(.55, f) * (.045 + .03 * grain) + paint(.35, f) * .025;
409 }
410 const float H = .95;
411 float feetY = shoreY - .42 * min(c.extent.y, 1.4f);
412 const float sp = 1.45;
413 float slot = floor((uv.x + pan) / sp + .5);
414 float flameScale = c.size * (1 + c.b.x * .55);
415 float3 fire = 0.f, sparks = 0.f;
416 const int N = 18;
417 const float span = .36;
418 float fades[N + 1];
419 for (int i = 0; i <= N; ++i) fades[i] = 1.2 * log(max(1 - float(i) / float(N), 1e-3f));
420 for (int k = -1; k <= 1; ++k) {
421 float id = slot + float(k);
422 float seed = hash11(id * 2.9 + 1);
423 float2 feet = float2(id * sp - pan, feetY);
424 if (abs(uv.x - feet.x) > 1.05) continue;
425 float omega = TAU * (1 + .2 * seed) * (seed > .5 ? 1.f : -1.f);
426 float mode = .5 + .5 * sin(c.t * .15 + seed * 6);
427 float extension = mix(1.f, .35f, mode), flower = mode * .55;
428 float tauNow = c.t + c.m * 3 / TAU;
429 for (int side = 0; side < 2; ++side) {
430 float sgn = side == 0 ? -1.f : 1.f;
431 float2 hc = feet + float2(.3 * sgn, .64) * H;
432 float poiBand = c.b[(side + int(seed * 3.99)) % 4];
433 float poiScale = flameScale * (1 + poiBand * .8);
434 // The poi head is a hand circle plus a tether made of two rotating phasors.
435 float2 A = dnPolar(omega * tauNow * sgn), B = float2(A.x, -A.y);
436 B = dnMul(dnMul(B, B), B);
437 float2 A0 = A, B0 = B;
438 const float2 lead = float2(.8775826, .4794255);
439 float2 prev = hc + .12 * A + .36 * (extension * dnMul(A, lead) + flower * B);
440 float2 head = prev;
441 fire += paint(.97, f) * exp(-length(uv - head) / (.016 * poiScale)) * 1.4;
442 fire += paint(.6, f) * exp(-length(uv - head) / (.12 * poiScale)) * (.16 + c.hit * .5);
443 if (length(uv - hc) > 1.1) continue;
444 // Flame trail: the recent path of the poi head.
445 float2 rA = dnPolar(-omega * span / float(N) * sgn), rB = float2(rA.x, -rA.y);
446 rB = dnMul(dnMul(rB, rB), rB);
447 float score = -1e4, sAt = 1, dAt = 9, dMin = 1e3, sMin = 1;
448 float prevW = .018 * poiScale;
449 for (int i = 1; i <= N; ++i) {
450 float s = float(i) / float(N);
451 A = dnMul(A, rA); B = dnMul(B, rB);
452 float2 pt = hc + .12 * A + .36 * (extension * dnMul(A, lead) + flower * B) + float2(0, s * s * .07);
453 float2 pa = uv - prev, ba = pt - prev;
454 float hh = saturate(dot(pa, ba) / max(dot(ba, ba), 1e-7f));
455 float d = length(pa - ba * hh);
456 float curW = (.018 + .04 * s) * poiScale;
457 float dw = d / mix(prevW, curW, hh);
458 float sc = -dw * dw * 1.3 + mix(fades[i - 1], fades[i], hh);
459 if (sc > score) { score = sc; sAt = (float(i - 1) + hh) / float(N); dAt = dw; }
460 if (d < dMin) { dMin = d; sMin = (float(i - 1) + hh) / float(N); }
461 prev = pt;
462 prevW = curW;
463 }
464 float glowBest = exp(-dAt * dAt * 1.3) * pow(1 - sAt, 1.2f);
465 float flick = noise2(float2(sAt * 9 - c.t * 7 + id * 3 + sgn, dAt * 2 + c.t * 3));
466 float tongues = noise2(uv * float2(16, 9) + float2(id * 5 + sgn * 3, -c.t * 6));
467 float heat = glowBest * (.55 + .9 * flick);
468 fire += paint(mix(.98f, .25f, saturate(sAt * 1.1 + dAt * .3)), f) * heat * (1.5 + poiBand * 1.2);
469 float aura = exp(-dMin / (.055 * poiScale)) * dnSq(1 - sMin) * smoothstep(.35f, .8f, tongues);
470 fire += paint(.3 + .3 * (1 - sMin), f) * aura * .55;
471 // Sparks shed from the poi path, and a burst from the head on attacks.
472 for (int k2 = 0; k2 < 4; ++k2) {
473 float hk = hash11(id * 13 + float(k2) * 3.7 + sgn);
474 float age = fract(c.t * 1.2 + hk);
475 float2 back = dnPolar(-omega * age * .6 * sgn), backB = float2(back.x, -back.y);
476 float2 Aa = dnMul(A0, back), Ba = dnMul(B0, dnMul(dnMul(backB, backB), backB));
477 float2 origin = hc + .12 * Aa + .36 * (extension * dnMul(Aa, lead) + flower * Ba);
478 float2 vel = (float2(hk, hash11(hk * 9)) - .5) * .5 + float2(0, .25);
479 float2 a = origin + vel * age - float2(0, .7 * age * age);
480 float2 b = a - (vel - float2(0, 1.4 * age)) * .03;
481 sparks += paint(.85, f) * exp(-sdSegment(uv, a, b) / .003) * dnSq(1 - age) * .9;
482 }
483 for (int k2 = 0; k2 < 5; ++k2) {
484 float hk = hash11(id * 7 + float(k2) * 5.1 + sgn * 3);
485 float2 dir = dnPolar(hk * TAU + float(k2));
486 float age = fract(c.m * .8 + hk);
487 float2 a = head + dir * age * (.35 + .25 * hk) - float2(0, .3 * age * age);
488 sparks += paint(.9, f) * exp(-sdSegment(uv, a, a - dir * .04) / .004) * c.hit * 2.2 * (1 - age * .6) * smoothstep(0.f, .15f, age) * (1 - smoothstep(.75f, 1.f, age));
489 }
490 }
491 color += paint(.6, f) * exp(-length((uv - feet) * float2(1, 3.5)) * 2.2) * .35 * (uv.y < shoreY ? 1.f : .4f) * (1 + c.b.x * 2.5);
492 }
493 // Embers rising over the whole beach.
494 float2 eq = (uv + float2(pan * .5, -c.t * .12)) * 11;
495 float2 eid = floor(eq);
496 float2 eh = hash22(eid + 5);
497 float2 ep = fract(eq) - .5 - (eh - .5) * .6 + .15 * float2(sin(c.t * 2 + eh.x * 9), 0);
498 float flickE = .55 + .45 * sin(c.t * (6 + eh.y * 8) + eh.x * 20) * (1 + c.b.w * 2.5);
499 float ember = exp(-dot(ep, ep) * 260 / (1 + c.b.w * 1.5)) * step(.6f - c.b.w * .2f, eh.x) * flickE * smoothstep(.9f, -.4f, uv.y / c.extent.y);
500 return color + fire + sparks + paint(.7 + eh.y * .25, f) * ember * .9;
501}
502
503// ---------------------------------------------------------------- 181 Chiffon Swirl
504// A long-exposure photograph of a twirling chiffon skirt without the dancer: four sheer
505// layers flare around an invisible vertical axis in spiral folds, translucent where they
506// overlap, rim-lit, with fine fabric grain and luminous hems, under a single spotlight
507// in stage haze. The spin speeds up and slows in phrases and the skirt flares with it;
508// bass flares the swirl wider, attacks flick a hem outward, highs make the hems glitter
509// and Zoom speed slowly dollies around the swirl.
510struct ChiffonLayer { float3 color; float alpha; };
511
512
513// ---------------------------------------------------------------- 182 Fountain Show
514// A musical fountain at night: a row of jets throws columns and arcs of streaky water
515// lit from below in shifting palette colours, with mist, splashes and reflections in
516// the pool in front of a city skyline. Jets choreograph procedurally; bands drive the
517// heights of their jet groups and attacks shoot the central jets high. Zoom speed pans.
518float3 fountainJets(float2 p, float waterY, float pan, Ctx c, constant NativeFrame &f) {
519 float3 light = 0.f;
520 const float sp = .34;
521 float slot = floor((p.x + pan) / sp + .5);
522 float arcs = smoothstep(.25f, .75f, .5 + .5 * sin(c.t * .13));
523 float modeA = .5 + .5 * sin(c.t * .07), modeB = .5 + .5 * sin(c.t * .11 + 2);
524 float tall = .8 + .55 * saturate(c.extent.y - 1);
525 float y = p.y - waterY;
526 for (int k = -3; k <= 3; ++k) {
527 float j = slot + float(k);
528 float xj = j * sp - pan;
529 float dx = p.x - xj;
530 int group = int(j - floor(j / 4) * 4);
531 float wave = .5 + .35 * sin(c.t * 1.5 + c.m * 2.4 - j * .6);
532 float alternate = .45 + .35 * sin(c.t * 2.1 + c.m * 3) * (fmod(abs(j), 2.f) < 1 ? 1.f : -1.f);
533 float pulse = .55 + .3 * sin(c.t * 1.1 + c.m * 1.8);
534 float H = mix(mix(wave, alternate, modeA), pulse, modeB * .5);
535 float central = exp(-dnSq(xj / .5));
536 H = (H + c.b[group] * .5 + dnAccent(c, f) * .9 * central) * tall;
537 float3 tint = paint(j * .13 + c.t * .05, f) * (1 + c.hit * central);
538 if (abs(dx) < .2 && y > -.03 && y < H + .25) {
539 float frac = saturate(y / max(H, .05f));
540 float w = (.012 + .03 * frac * frac) * c.size;
541 float core = exp(-dnSq(dx / w));
542 float streak = noise2(float2(dx / w * 1.3 + j * 7, y * 10 - c.t * 7));
543 float column = core * (.45 + .8 * streak) * smoothstep(H + .12f, H * .75f, y);
544 float crown = exp(-dnSq((y - H) / (.06 + .08 * frac)) - dnSq(dx / (w * 3))) * .5;
545 light += tint * (column + crown) * (1.5 - .7 * frac) * (1 - arcs * .5);
546 light += paint(.95, f) * core * core * .35 * (1 - frac) * (1 - arcs * .5);
547 }
548 for (int side = 0; side < 2; ++side) {
549 float dir = side == 0 ? -1.f : 1.f;
550 float reach = (.42 + .2 * sin(c.t * .4 + j)) * (.7 + .3 * H);
551 float u = dx * dir / reach;
552 if (u < 0 || u > 1.08) continue;
553 float apex = H * .75;
554 float yc = 4 * apex * u * (1 - u);
555 float slope = 4 * apex * (1 - 2 * u) / reach;
556 float dist = (y - yc) / sqrt(1 + slope * slope);
557 float w = (.006 + .018 * u) * c.size;
558 float drops = noise2(float2(u * 26 - c.t * 6 + j * 3, dist * 70));
559 float stream = exp(-dnSq(dist / w)) * (.35 + .9 * drops) * smoothstep(1.08f, .95f, u);
560 light += tint * stream * arcs * 1.3;
561 float2 splash = float2(dx - dir * reach, y);
562 light += tint * exp(-dot(splash, splash) / .002) * arcs * .5;
563 }
564 light += tint * exp(-dnSq(dx / .14) - y * y / .003) * .35;
565 }
566 return light;
567}
568float3 fountainShow(Ctx c, constant NativeFrame &f) {
569 float2 uv = c.uv;
570 float pan = c.travel * .06;
571 float waterY = -.32 * c.extent.y;
572 float3 color;
573 if (uv.y > waterY) {
574 color = mix(paint(.15, f) * .1, deep(.1, f) * .05, smoothstep(waterY, c.extent.y, uv.y));
575 float clouds = fbm2(float2(uv.x * .9 + pan * .1 + c.t * .01, uv.y * 2.2), 4);
576 color += paint(.35, f) * smoothstep(.45f, .8f, clouds) * .1 * smoothstep(waterY, waterY + .6f, uv.y);
577 float2 star = uv * 80;
578 color += paint(.9, f) * pow(hash21(floor(star)), 60.f) * smoothstep(.35f, .1f, length(fract(star) - .5)) * smoothstep(waterY + .4f, waterY + 1.f, uv.y);
579 float x = uv.x + pan * .3;
580 float bw = .16;
581 float bid = floor(x / bw);
582 float hb = hash11(bid * 1.7 + 3);
583 float top = waterY + .05 + (.12 + .55 * hb * hb) * min(c.extent.y, 1.4f);
584 if (uv.y < top) {
585 float2 win = float2(fract(x / .026), fract((uv.y - waterY) / .036));
586 float2 wid = float2(floor(x / .026), floor((uv.y - waterY) / .036));
587 float on = step(.62f, hash21(wid + bid)) * step(.25f, win.x) * step(win.x, .75f) * step(.3f, win.y) * step(win.y, .7f);
588 color = deep(.3, f) * .035 + paint(.75 + hash21(wid) * .25, f) * on * .3;
589 }
590 float bokeh = pow(noise2(float2(x * 14, 3)), 4.f) * exp(-(uv.y - waterY) * 30);
591 color += paint(.8, f) * bokeh * .4;
592 color += fountainJets(uv, waterY, pan, c, f);
593 color += paint(.4, f) * exp(-(uv.y - waterY) / .06) * .08;
594 } else {
595 float depth = waterY - uv.y;
596 float ripple = sin(depth * 70 / (depth + .15) + c.t * 2 + uv.x * 3) * .012 * min(depth * 6, 1.f);
597 float2 mirror = float2(uv.x + ripple, waterY + depth * .9);
598 color = deep(.25, f) * .04 + fountainJets(mirror, waterY, pan, c, f) * .45 * exp(-depth * 1.5);
599 color += paint(.3, f) * .04 * (.5 + .5 * sin(depth * 40 - c.t + uv.x * 5));
600 float rimY = -c.extent.y + .07;
601 color = mix(color, deep(.4, f) * .08 + paint(.5, f) * .06, smoothstep(rimY + .01f, rimY - .01f, uv.y));
602 }
603 return color;
604}
605
606// ---------------------------------------------------------------- 183 Glowstick Rave
607// A dark rave: dozens of glowsticks wave in circles and figure eights, held by a crowd
608// you never see, leaving short light trails in drifting smoke. Each stick's motion is
609// procedural; bands energize their stick groups and attacks throw some sticks spinning
610// up into the air. Zoom speed drifts the view through the crowd.
611float3 glowstickRave(Ctx c, constant NativeFrame &f) {
612 float2 uv = c.uv;
613 float pan = c.travel * .08;
614 float smoke = fbm2(uv * 1.2 + float2(c.t * .04 + pan * .3, c.t * .02), 3);
615 float3 color = deep(.4 + uv.y * .05, f) * (.03 + .05 * smoke);
616 float throwArc = dnAccent(c, f);
617 float3 sticks = 0.f;
618 for (int layer = 0; layer < 2; ++layer) {
619 float fl = float(layer);
620 float cell = layer == 0 ? .26 : .42;
621 float2 q = (uv + float2(pan * (.6 + fl * .7), 0)) / cell + fl * float2(.37, .51);
622 float2 base = floor(q);
623 float w = (layer == 0 ? .04 : .05) * c.size;
624 for (int j = -2; j <= 1; ++j) for (int i = -1; i <= 1; ++i) {
625 float2 id = base + float2(i, j);
626 float2 h = hash22(id + fl * 17);
627 float h3 = hash21(id * 1.7 + fl * 5 + 3);
628 if (h.x < (layer == 0 ? .5 : .3)) continue;
629 float band = c.b[int(h3 * 3.99)];
630 float dirSpin = h3 > .5 ? 1.f : -1.f;
631 float omega = (2.6 + 2 * h.y) * dirSpin;
632 float theta = omega * c.t + c.m * 2.2 * dirSpin + h.x * TAU;
633 float A = .28 * (1 + band * .7);
634 float eight = smoothstep(.3f, .7f, h.y);
635 bool thrown = hash21(id + fl * 3 + 11) > .78;
636 float lift = thrown ? throwArc * 1.7 : 0.f;
637 float spinUp = thrown ? TAU * 1.5 * throwArc : 0.f;
638 float2 hand = id + .5 + (h - .5) * .35 + float2(0, lift);
639 bool twirl = h.y > .8;
640 float3 tint = paint(h3 * 1.1 + fl * .2, f);
641 float energy = (1 + band * 1.6 + (thrown ? c.hit * 2 : 0.f)) * (layer == 0 ? .3f : .6f);
642 float dHand = length(q - hand);
643 sticks += tint * exp(-dHand / .45) * .05 * (.5 + smoke) * energy;
644 if (dHand > A + .7) continue;
645 float2 e = dnPolar(theta), step = dnPolar(-.1 * dirSpin);
646 float stickLen = layer == 0 ? .3f : .36f;
647 for (int g = 0; g < 4; ++g) {
648 float2 pos = hand + A * mix(e, float2(e.y, e.x * e.y), eight);
649 float ang = PI * .5 + .7 * e.y + spinUp;
650 float2 dir = dnPolar(ang) * stickLen;
651 if (twirl) dir = dnMul(dir, dnMul(e, e));
652 float d = sdSegment(q, pos - dir, pos + dir);
653 float ghost = g == 0 ? 1.f : dnSq(1 - float(g) / 4) * .55;
654 float core = 1 - smoothstep(w * .45f, w, d);
655 float halo = exp(-d / (w * 2.2)) * .3;
656 sticks += (tint * (halo + core * .8) + paint(.95, f) * core * .6) * ghost * energy;
657 e = dnMul(e, step);
658 }
659 }
660 }
661 return color + sticks;
662}
663
664#define DANCE_EFFECTS(X) X(pigmentBurst) X(stretchMembrane) X(kineticMirrors) X(fireDance) X(fountainShow) X(glowstickRave)
665DANCE_EFFECTS(NATIVE_EFFECT)