Branchwell

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

Raw 12.8 KB on 0e4fe45e403a1b6b5a57aa68addd681995d37722
1// Particle, orbital and constellation designs. Original PulseCanvas procedural code.
2// Every effect receives Ctx from NativeCommon.metal; audio terms are zero at zero Chaos.
3
4// ------------------------------------------------------------------ 41 Photon Rapids
5// Light rushes past from an off-centre vanishing point that drifts and curves, so the
6// streaks swirl like rapids. The music clock speeds the current, bands light their own
7// streak families and attacks stretch every streak.
8
9
10// ------------------------------------------------------------------ 50 Magnetic Ballet
11// Field lines of four magnetic poles waltzing across the canvas, drawn as glowing lines
12// over an iron-filing texture. Each pole's strength follows a band, so the lines bend and
13// gather with the music; attacks brighten the filings.
14float3 magneticBallet(Ctx c, constant NativeFrame &f) {
15 float2 p = c.uv / c.size;
16 float stream = 0, potential = 0;
17 float2 field = 0.f;
18 for (int i = 0; i < 5; ++i) {
19 float fi = float(i);
20 float2 pole = float2(sin(c.t * (.13 + fi * .03) + fi * 2.4 + c.m * .1) * c.extent.x * .85, cos(c.t * (.11 + fi * .025) + fi * 1.7) * c.extent.y * .8);
21 // Whole-number charges keep every field line continuous; bands move the poles.
22 float charge = fmod(fi, 2.f) < 1 ? 1. : -1.;
23 pole += float2(sin(c.m * .7 + fi * 2), cos(c.m * .6 + fi)) * c.b[i % 4] * .25;
24 float2 d = p - pole;
25 float r2 = dot(d, d) + .002;
26 stream += charge * atan2(d.y, d.x);
27 potential += charge * .5 * log(r2);
28 field += charge * d / r2;
29 }
30 float lines = stream * 3 / PI + c.travel * .2;
31 float strength = length(field);
32 // |grad stream| = |field|; using it avoids derivative spikes at the atan2 branch cuts.
33 float w = 3 / PI * strength * c.px / c.size;
34 float fieldLine = 1 - smoothstep(.0f, max(w * 1.5, .06f), abs(fract(lines) - .5) - .02);
35 float equipotential = 1 - smoothstep(.0f, max(2 * strength * c.px * 1.5, .04f), abs(fract(potential * 2) - .5) - .45);
36 float2 direction = field / max(strength, 1e-4f);
37 // Iron filings: short dashes aligned with the local field.
38 float2 grid = p * 60;
39 float2 cell = floor(grid), local = fract(grid) - .5;
40 float2 jitter = hash22(cell) - .5;
41 float2 q = local - jitter * .3;
42 float along = dot(q, direction), across = dot(q, float2(-direction.y, direction.x));
43 float filing = (1 - smoothstep(.05f, .09f, abs(across))) * (1 - smoothstep(.28f, .36f, abs(along))) * step(.25, hash21(cell + 3));
44 float3 color = deep(.4, f) * .06;
45 color += paint(.2 + potential * .05, f) * filing * clamp(strength * .15, 0.f, 1.f) * (.5 + c.hit * 1.5);
46 color += paint(.55 + stream * .02, f) * fieldLine * clamp(strength * .2, .25f, 1.f) * 1.1;
47 color += paint(.85, f) * equipotential * .12;
48 color += paint(.95, f) * exp(-1 / max(strength * .02, 1e-3f)) * .4;
49 return color;
50}
51
52// ------------------------------------------------------------------ 52 Diamond Undertow
53// Schools of faceted diamond shards ride a curling undertow in several depth layers.
54// Each shard aligns with the current; bands tumble their own shards and attacks make
55// the facets flash.
56
57
58// ------------------------------------------------------------------ 54 Gyroscopic Garden
59// A low glide over a garden of brass gyroscopes on pedestals. Each has three nested
60// gimbals spinning on different axes; bands spin their own gyroscopes faster and attacks
61// glint across the rings.
62
63
64// ------------------------------------------------------------------ 55 Bubble Choir
65// Clouds of soap bubbles float toward the viewer in layers. Thin-film colors swirl over
66// every skin; each bubble swells with its own band and attacks make the skins wobble.
67
68
69// ------------------------------------------------------------------ 57 Echo Lanterns
70// Hundreds of paper sky lanterns rise over a still lake; the water mirrors them. Each
71// flame flickers with its own band, attacks flare nearby lanterns, and Zoom speed lifts
72// the camera through the swarm.
73
74
75// ------------------------------------------------------------------ 86 Plexus Constellations
76// Stars drifting in deep layers join into constellations whenever they come close. Loud
77// passages raise the joining distance so the web grows; stars twinkle on their bands.
78float2 plexusPoint(float2 cell, float layer, Ctx c) {
79 float2 seed = hash22(cell + layer * 17);
80 return cell + .5 + .4 * float2(sin(c.t * (.2 + seed.x * .3) + seed.y * 9), cos(c.t * (.17 + seed.y * .3) + seed.x * 9));
81}
82float3 plexusConstellations(Ctx c, constant NativeFrame &f) {
83 float3 color = deep(.45 + c.uv.y * .1, f) * .06 + paint(.4, f) * .04 * fbm2(c.uv * 1.2 + c.t * .01, 4);
84 const int count = 3;
85 for (int k = 0; k < count; ++k) {
86 float2 slot = layerSlot(k, count, c.travel * 0.1);
87 float phase = slot.x, id = slot.y, fade = layerFade(phase);
88 float scale = layerScale(phase, 1.8) * 3.2 / c.size;
89 float2 p = c.uv * scale + id * 9.1;
90 float2 base = floor(p);
91 float2 points[9];
92 int n = 0;
93 for (int y = -1; y <= 1; ++y) for (int x = -1; x <= 1; ++x) points[n++] = plexusPoint(base + float2(x, y), id, c);
94 float reach = 1.05 + c.e * .35 + c.b.x * .2;
95 float lines = 0, stars = 0;
96 for (int i = 0; i < 9; ++i) {
97 float2 a = points[i];
98 float band = c.b[int(hash21(floor(a) + id) * 3.99)];
99 stars += exp(-length(p - a) / (.035 * (1 + band * .8 + c.hit * .7))) * (.6 + band + c.hit * 1.2);
100 for (int j = i + 1; j < 9; ++j) {
101 float2 b = points[j];
102 float span = length(a - b);
103 if (span > reach) continue;
104 float strength = pow(1 - span / reach, 1.2f);
105 float d = sdSegment(p, a, b);
106 lines += (exp(-d / (.01 + c.px * scale)) + exp(-d / .06) * .15) * strength;
107 }
108 }
109 float tone = mix(.25, 1., phase) * fade;
110 color += paint(.35 + id * .07, f) * lines * 1.1 * tone + paint(.9, f) * stars * 1.3 * tone;
111 }
112 return color;
113}
114
115// ------------------------------------------------------------------ 87 Murmuration
116// A starling murmuration rolls across a dusk sky: thousands of birds forming one folding
117// cloud. Bass thickens the flock, attacks snap it into a turn, highs scatter the edges.
118
119
120// ------------------------------------------------------------------ 88 Ring Shepherds
121// Skimming a planetary ring at a grazing angle. Thousands of ringlets, gaps and spiral
122// density waves sweep past, with the planet's limb filling the sky. Ringlets brighten on
123// their bands; attacks launch wakes from the shepherd moons.
124float3 ringShepherds(Ctx c, constant NativeFrame &f) {
125 float2 planetCenter = float2(-c.extent.x * .35, c.extent.y * .25 + 2.75);
126 float planetRadius = 2.2;
127 float3 color = deep(.4, f) * .03 + paint(.9, f) * pow(hash21(floor(c.uv * 140)), 70.f);
128 float planetDistance = length(c.uv - planetCenter);
129 if (planetDistance < planetRadius) {
130 float2 n2 = (c.uv - planetCenter) / planetRadius;
131 float3 n = float3(n2, sqrt(max(0.f, 1 - dot(n2, n2))));
132 float light = max(dot(n, normalize(float3(.6, -.3, .7))), 0.f);
133 float bands = fbm2(float2(n.y * 6, c.t * .02), 4);
134 color = paint(.3 + bands * .5, f) * (.04 + light * .9) + paint(.7, f) * pow(1 - n.z, 4.f) * .3;
135 }
136 if (c.uv.y < .25 * c.extent.y) {
137 float3 g = groundView(c.uv, c.extent, 1., -.75 * c.extent.y);
138 // The camera orbits the planet, so ring structure streams past along the azimuth.
139 float2 fromPlanet = rot(float2(g.x * 4 + 60, g.y * 4 - 120), c.travel * .03 + c.t * .006);
140 float radius = length(fromPlanet) - 85;
141 float azimuth = atan2(fromPlanet.y, fromPlanet.x) * 60;
142 float ringlets = fbm2(float2(radius * 1.3, 0), 5);
143 ringlets = mix(ringlets, .5 + .5 * sin(radius * 9), .25);
144 float gap = 1 - smoothstep(.01f, .035f, abs(fract(radius * .07) - .5));
145 float band = c.b[int(fract(radius * .11) * 3.99)];
146 float wake = 0;
147 for (int i = 0; i < 2; ++i) {
148 float moonRadius = 46 + float(i) * 9;
149 float moonAzimuth = c.t * (.01 + float(i) * .004) + float(i) * 3;
150 float dr = radius - moonRadius;
151 float phase = dr * 6 - (azimuth - moonAzimuth) * 60 * sign(dr);
152 wake += sin(phase - c.m * 3) * exp(-abs(dr) * .35) * (.4 + c.hit * 1.2);
153 }
154 float density = clamp(ringlets * (1 - gap) + wake * .15, 0.f, 1.f);
155 float grain = .75 + .25 * noise2(float2(radius * 6, azimuth * 3));
156 float clumps = .7 + .3 * smoothstep(.3f, .8f, noise2(float2(radius * .8, azimuth * .6)));
157 float spokes = 1 - .35 * smoothstep(.55f, .8f, noise2(float2(azimuth * .9, radius * .05)));
158 grain *= clumps * spokes;
159 float3 ring = paint(.2 + ringlets * .6, f) * density * grain * (.6 + band * 1.4);
160 ring += paint(.95, f) * pow(density, 6.f) * .3;
161 float coverage = smoothstep(.25f * c.extent.y, .15f * c.extent.y, c.uv.y);
162 float fade = exp(-g.z * .03) * smoothstep(0.f, .2f, .25f * c.extent.y - c.uv.y);
163 color = mix(color, ring * fade + color * (1 - density) * .4, coverage);
164 }
165 return color;
166}
167
168// ------------------------------------------------------------------ 89 Pendulum Wave
169// Rows of pendulums with slowly increasing periods swing in and out of phase, forming
170// snakes, standing waves and chaos. Bass widens the swings, each row's bobs glow on
171// their band and the music clock advances the pattern.
172
173
174// ------------------------------------------------------------------ 90 Firework Garden
175// Fireworks bloom over a dark garden and mirror in a pond. Shells launch on a steady
176// schedule; louder music launches more and larger shells, bands color their bursts and
177// attacks crackle the sparks.
178
179
180// ------------------------------------------------------------------ 91 Event Horizon
181// A black hole bends a starfield and its own accretion disk into the classic lensed
182// halo. The disk spins faster inside; bands heat their own radii, attacks launch flares
183// that orbit, and Zoom speed slowly circles the camera.
184float3 eventHorizon(Ctx c, constant NativeFrame &f) {
185 float orbit = c.travel * .12 + c.t * .015;
186 float3 ro = float3(sin(orbit) * 9, 1.1 + sin(c.t * .03) * .4, -cos(orbit) * 9);
187 float3 target = float3(1.6, 0, 0);
188 target.xz = rot(target.xz, -orbit);
189 float3 rd = cameraRay(c.uv / c.size, target - ro, float3(0, 1, 0), 1.4);
190 float3 p = ro, v = rd;
191 float3 color = 0.f;
192 float transmission = 1;
193 float h2 = dot(cross(p, v), cross(p, v));
194 for (int i = 0; i < 90; ++i) {
195 float r = length(p);
196 if (r < 1) { transmission = 0; break; }
197 float stepSize = .06 * r;
198 float3 previous = p;
199 float3 acceleration = -1.5 * h2 * p / pow(r, 5.f);
200 v += acceleration * stepSize;
201 p += v * stepSize;
202 if (previous.y * p.y < 0) {
203 float3 crossing = mix(previous, p, previous.y / (previous.y - p.y));
204 float radius = length(crossing.xz);
205 if (radius > 2.4 && radius < 9) {
206 float angle = atan2(crossing.z, crossing.x);
207 float spin = c.t * 2.2 / pow(radius, 1.5f) * 3 + c.m * .8 / radius;
208 float swirl = fbm2(float2(angle * 3 + spin * 3 + log(radius) * 4, radius * 1.5), 4);
209 float band = c.b[int(clamp((9 - radius) / 6.6 * 3.99, 0.f, 3.99f))];
210 float heat = pow(2.4 / radius, 1.6f);
211 float flare = c.hit * exp(-pow(fract((angle - spin * 4) / TAU) - .5, 2.f) * 80) * step(3.f, radius) * step(radius, 5.f);
212 float density = smoothstep(2.4f, 3.f, radius) * smoothstep(9.f, 6.f, radius) * (.35 + .9 * swirl) * (1 + band * 1.3);
213 float doppler = 1 + .5 * sin(angle - orbit);
214 float3 emission = mix(paint(.4, f), paint(.95, f), heat) * (heat * 2.5 + .3) * doppler + paint(.98, f) * flare * 3;
215 color += emission * density * transmission;
216 transmission *= 1 - clamp(density * .85, 0.f, .95f);
217 }
218 }
219 if (r > 30) break;
220 }
221 float3 direction = normalize(v);
222 float2 sky = float2(atan2(direction.z, direction.x), asin(clamp(direction.y, -1.f, 1.f)));
223 float stars = pow(hash21(floor(sky * 180)), 60.f) * 2 + pow(hash21(floor(sky * 90 + 3)), 40.f);
224 float3 background = paint(.9, f) * stars + deep(.4, f) * .05 * fbm2(sky * 3, 4);
225 color += background * transmission;
226 return color;
227}
228
229// ------------------------------------------------------------------ 99 Neon Calligraphy
230// Neon tubes write sweeping calligraphic strokes across the canvas, layered at different
231// depths over a wet floor that reflects them. Each tube draws on, lingers and fades;
232// bands brighten their tubes and attacks make them buzz.
233
234
235#define PARTICLE_EFFECTS(X) X(magneticBallet) X(plexusConstellations) X(ringShepherds) X(eventHorizon)
236PARTICLE_EFFECTS(NATIVE_EFFECT)