Branchwell

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

1// Original PulseCanvas designs 350–359. Cosmic phenomena drawn as vivid neon light, not
2// photographic space art: beams, shells, streams, loops and lensed arcs built from analytic
3// curves and a little noise. Every band drives its own structures; attacks make local events.
4
5// ---------------------------------------------------------------- shared helpers (cosm*)
6// Ridged noise: thin bright creases, the raw material of filaments.
7float cosmRidge(float2 p, int octaves) {
8 float sum = 0, amplitude = .5;
9 for (int i = 0; i < octaves; ++i) {
10 float n = 1 - abs(2 * noise2(p) - 1);
11 sum += amplitude * n * n;
12 p = float2(p.x * 1.6 - p.y * 1.2, p.x * 1.2 + p.y * 1.6) + 4.7;
13 amplitude *= .5;
14 }
15 return sum;
16}
17// Sparse twinkling specks, one jittered candidate per cell, with no seams at cell edges.
18float cosmStars(float2 p, float density, float t) {
19 float2 id = floor(p), local = fract(p) - .5, h = hash22(id + 31.7);
20 float on = step(1 - density, hash21(id + 9.1));
21 float2 offset = local - (h - .5) * .6;
22 float twinkle = .6 + .4 * sin(t * (1.1 + h.x * 2) + h.y * TAU);
23 return on * twinkle * exp(-dot(offset, offset) * 900);
24}
25// Distance in uv units to the nearest integer contour of a smooth field v.
26float cosmContour(float v, float px) {
27 return abs(fract(v + .5) - .5) * px / max(fwidth(v), 1e-5f);
28}
29// Lay a two-body composition along the canvas's long axis.
30float2 cosmLongAxis(float2 p, float2 extent) {
31 return extent.y > extent.x ? float2(p.y, -p.x) : p;
32}
33
34// ---------------------------------------------------------------- 350 Pulsar Beacon
35float3 pulsarBeacon(Ctx c, constant NativeFrame &f) {
36 float2 p = c.uv / c.size;
37 float spin = c.t * .55;
38 // Attacks shear thin horizontal slices sideways: a glitch flare through the beams.
39 float slice = floor(p.y * 28);
40 float shear = (noise2(float2(slice * 1.37, c.t * 5)) - .5) * c.hit * c.hit * .8;
41 float2 g = p + float2(shear, 0);
42 float r = length(g), a = atan2(g.y, g.x);
43 float lag = (spin - a) - PI * floor((spin - a) / PI); // time since a beam swept here
44 float offset = lag > PI * .5 ? lag - PI : lag; // signed angle to nearest beam
45 float width = .045 + .05 * c.b.x;
46 float cone = exp(-offset * offset / (width * width)) * exp(-r * .25);
47 float spine = glow(r * sin(offset), .004 + .005 * c.b.x) * exp(-r * .4);
48
49 // Nebula gas, lit by the sweep and fading behind it.
50 float2 drift = float2(c.t * .013, -c.t * .009);
51 float gas = fbm2(p * 1.25 + drift, 5);
52 float strands = cosmRidge(p * 2.3 + gas * 1.6 - drift * 1.5, 4);
53 float cloud = smoothstep(.3f, .78f, gas);
54 float after = (1 - exp(-lag * 30)) * exp(-lag * (2.6 - 1.4 * c.b.y));
55 float lit = after * (.3 + .7 * cloud) * (.35 + strands * 1.6);
56 float glint = pow(strands, 6.f) * cloud * (.2 + 3 * c.b.w);
57 float pulse = pow(.5 + .5 * cos(TAU * (r * 1.5 - c.t * .4)), 30.f) * cloud * (.15 + 1.6 * c.b.w);
58
59 // Magnetic loops share the beam axis and spin with it.
60 float2 m = rot(p, -spin);
61 float rm = max(length(m), 1e-3f);
62 float shell = rm * rm * rm / max(m.y * m.y, 1e-6f);
63 float lv = log2(shell) * 1.6;
64 float loopMask = smoothstep(1.2f, .7f, shell) * smoothstep(.03f, .08f, rm);
65 float flowing = .55 + .45 * sin(atan2(m.y, m.x) * 6 - c.t * 3);
66 float loops = glow(cosmContour(lv, c.px) / c.size, .005) * loopMask * flowing * (.25 + 1.8 * c.b.z);
67
68 float rp = length(p);
69 float star = glow(rp, .02) * 3 + glow(rp, .09) * .5;
70 float flare = (glow(p.y, .005 + .006 * c.hit) * exp(-abs(p.x) * (3.4 - 2.4 * c.hit))
71 + glow(p.x, .004) * exp(-abs(p.y) * 6)) * (.25 + 2.4 * c.hit);
72 float bars = c.hit * c.hit * smoothstep(.62f, .8f, noise2(float2(slice * 2.1, c.t * 6))) * exp(-r * .9) * .7;
73
74 float3 col = paint(.02 + gas * .2, f) * (.035 + .12 * gas * gas);
75 col += paint(.22 + strands * .15, f) * cloud * strands * .16;
76 col += paint(.42 + .38 * after, f) * lit * (.75 + .25 * strands);
77 col += paint(.75, f) * (glint + pulse * .6);
78 col += paint(.72, f) * cone * (.35 + 1.9 * c.b.x) * (.3 + .6 * cloud + strands * .7);
79 col += paint(.8, f) * spine * (.8 + 2.5 * c.b.x);
80 col += paint(.58, f) * loops * 1.4;
81 col += paint(.78, f) * (star + flare) + paint(.25, f) * bars;
82 return col;
83}
84NATIVE_EFFECT(pulsarBeacon)
85
86// ---------------------------------------------------------------- 351 Twin Suns
87float4 cosmSun(float2 d, float radius, float spin, float hue, float corona, float px, constant NativeFrame &f) {
88 float r = length(d) / radius;
89 float mu = sqrt(max(0.f, 1 - r * r));
90 float2 sp = rot(d / radius, spin);
91 float cells = noise2(sp * 5 + 3.3) * .6 + noise2(sp * 11 + 8.1) * .4;
92 float3 surface = paint(hue + .2 * mu + .12 * cells, f) * (.7 + 1.1 * mu) + paint(.95, f) * pow(mu, 3.f) * .5;
93 float disc = fill(r - 1, px / radius);
94 float a = atan2(d.y, d.x) + spin * .3;
95 float rays = pow(.5 + .5 * cos(a * 14 + 2 * sin(a * 5 + spin * 2)), 6.f) * exp(-max(r - 1, 0.f) * (2.6 - corona * 1.2));
96 float halo = glow(max(r - 1, 0.f), .25 + corona * .45) * (.3 + corona * 1.4);
97 float rim = glow(r - 1, .05);
98 float3 col = surface * disc + (paint(hue + .15, f) * (halo + rays * (.25 + corona * 1.5)) + paint(.9, f) * rim * .6) * (1 - disc);
99 return float4(col, disc);
100}
101
102float3 twinSuns(Ctx c, constant NativeFrame &f) {
103 float2 p = cosmLongAxis(c.uv / c.size, c.extent);
104 float phase = c.t * .3;
105 float2 orbit = float2(cos(phase), sin(phase) * .55);
106 float2 s1 = orbit * .42, s2 = -orbit * .74;
107 const float r1 = .23, r2 = .135;
108
109 // Circumbinary outflow: two trailing arms of glowing filaments that turn with the orbit.
110 float rho = length(p), th = atan2(p.y, p.x);
111 float spiral = 2 * (th - phase) + log(rho + .08) * 3.4;
112 float arms = pow(.5 + .5 * cos(spiral), 2.f);
113 float tex = fbm2(p * 2.1 + float2(c.t * .02, -c.t * .013), 4);
114 float threads = pow(.5 + .5 * cos(spiral * 5 + tex * 5), 10.f);
115 float gas = arms * exp(-rho * .3);
116 float3 col = paint(.02 + tex * .1, f) * (.04 + .1 * tex);
117 col += paint(.25 + tex * .35, f) * gas * (.06 + .2 * tex) * (.6 + 1.2 * c.b.y);
118 col += paint(.5 + .25 * tex, f) * gas * threads * (.22 + 1.5 * c.b.y);
119
120 // Tidal streams from the larger star to its companion, one strand per band.
121 float2 dv = s2 - s1; float len = length(dv);
122 float2 ax = dv / len, nv = float2(-ax.y, ax.x), rel = p - s1;
123 float u = dot(rel, ax) / len, v = dot(rel, nv), uc = clamp(u, 0.f, 1.f);
124 float ends = smoothstep(-.05f, .15f, u) * smoothstep(1.05f, .85f, u);
125 for (int k = 0; k < 4; ++k) {
126 float fk = float(k), level = c.b[k];
127 float bend = ((.12 + .06 * fk) * len + .04 * sin(TAU * uc * 1.5 + c.t * .8 + fk * 1.7)) * sin(PI * uc);
128 float dist = abs(v - bend);
129 float flow = pow(.5 + .5 * sin(u * 16 - c.t * 2.4 + fk * 1.9), 3.f);
130 float strand = glow(dist, .009 + .009 * level) * (.35 + .65 * flow) + glow(dist, .06) * .2;
131 col += paint(.55 + .08 * fk + .25 * flow, f) * strand * ends * (.35 + 1.8 * level);
132 }
133
134 // Attacks erupt a flare loop and an ejected blob from each star.
135 float burst = c.hit * c.hit;
136 float3 flares = 0;
137 for (int k = 0; k < 2; ++k) {
138 float2 s = k == 0 ? s1 : s2; float radius = k == 0 ? r1 : r2;
139 float fa = phase * (k == 0 ? 1.7 : -1.3) + float(k) * 2.4 + .8;
140 float2 dir = float2(cos(fa), sin(fa));
141 float2 site = s + dir * radius;
142 float loopArc = abs(length(p - site) - radius * (.2 + .9 * c.hit));
143 float outside = smoothstep(radius, radius * 1.05, length(p - s));
144 float blob = glow(length(p - s - dir * radius * (1.15 + 2.6 * (1 - c.hit))), .03 + .05 * (1 - c.hit));
145 flares += paint(.85, f) * (glow(loopArc, .008) * outside * 1.5 + blob) * burst;
146 }
147
148 // Stars, drawn back to front as the orbit turns.
149 float4 a = cosmSun(p - s1, r1, c.t * .2, .45, c.b.x, c.px, f);
150 float4 b = cosmSun(p - s2, r2, -c.t * .3, .7, c.b.z, c.px, f);
151 bool firstFront = sin(phase) < 0;
152 float4 back = firstFront ? b : a, front = firstFront ? a : b;
153 col = col * (1 - back.w) + back.rgb;
154 col += flares;
155 col = col * (1 - front.w) + front.rgb;
156 col += paint(.9, f) * cosmStars(p * 9, .25, c.t) * .5;
157 return col;
158}
159NATIVE_EFFECT(twinSuns)
160
161// ---------------------------------------------------------------- 352 Supernova Shell
162float3 supernovaShell(Ctx c, constant NativeFrame &f) {
163 float2 p = c.uv / c.size;
164 float r = length(p);
165 float2 dirv = p / max(r, 1e-4f);
166 const float R = .64;
167 float wob = noise2(dirv * 2.2 + 11.3) - .5;
168 float Ro = R * (1 + .14 * wob), Ri = Ro * .84;
169 float shell = (sqrt(max(Ro * Ro - r * r, 0.f)) - sqrt(max(Ri * Ri - r * r, 0.f))) / sqrt(Ro * Ro - Ri * Ri);
170
171 // Filaments drift outward through a seamless two-octave zoom.
172 float coarse = 0, fine = 0, wisp = 0;
173 for (int o = 0; o < 2; ++o) {
174 float2 z = octaveZoom(c.t * .03, o);
175 float2 q = p * z.x;
176 coarse += z.y * cosmRidge(q * 3.1 + 1.7, 3);
177 fine += z.y * cosmRidge(q * 7.3 - 4.1, 3);
178 wisp += z.y * fbm2(q * 2.4 + 9.5, 2);
179 }
180 coarse = pow(coarse / .875, 5.f);
181 fine = pow(fine / .875, 7.f);
182
183 // Hit: a fresh shock ring races out and lights the filaments it crosses.
184 float Rh = R * (.08 + 1.35 * (1 - c.hit)) * (1 + .1 * wob);
185 float shockBoost = 1 + 2.5 * c.hit * exp(-(r - Rh) * (r - Rh) / .008);
186 float newRing = glow(r - Rh, .012) * c.hit * c.hit * 2;
187
188 float inner = smoothstep(Ri, Ri * .3, r);
189 float2 kq = p * 8;
190 float2 kid = floor(kq), kl = fract(kq) - .5 - (hash22(kid + 4.4) - .5) * .6;
191 float knot = step(.6, hash21(kid + 1.9)) * exp(-dot(kl, kl) * 60) * smoothstep(.3f, .7f, shell);
192
193 float Rs = R * (1.15 + .1 * (noise2(dirv * 3.1 + 2.2) - .5));
194 float forward = glow(r - Rs, .006) * .9 + glow(r - Rs, .05) * .18;
195
196 float haze = fbm2(p * .9 + float2(4.1, c.t * .01), 3);
197 float3 col = paint(.95 + haze * .1, f) * (.03 + .08 * haze * haze);
198 col += paint(.1 + haze * .3, f) * smoothstep(.5f, .85f, haze) * .1;
199 col += paint(.8 + .1 * wob, f) * shell * (.07 + coarse * (.7 + 2.4 * c.b.x)) * shockBoost;
200 col += paint(.3 + .1 * fine, f) * shell * fine * (.45 + 2.6 * c.b.y) * shockBoost;
201 col += paint(.02 + .08 * wisp, f) * inner * pow(wisp, 3.f) * (.4 + 2.4 * c.b.z);
202 col += paint(.72, f) * knot * (.4 + 3 * c.b.w) * shockBoost;
203 col += paint(.05, f) * forward;
204 col += paint(.7, f) * (newRing + glow(r, .025) * 1.2 + glow(r, .08) * .2);
205 col += paint(.6, f) * cosmStars(p * 11, .2, c.t) * .4;
206 return col;
207}
208NATIVE_EFFECT(supernovaShell)
209
210// ---------------------------------------------------------------- 353 Galaxy Collision
211// Two-armed log spiral seen in its own (de-projected) plane: x = arm A, y = arm B, z = disc.
212float3 cosmSpiral(float2 q, float spin, float wind) {
213 float r = max(length(q), 1e-3f);
214 float phi = atan2(q.y, q.x) - wind * log(r) - spin;
215 float c1 = cos(phi);
216 return float3(pow(max(c1, 0.f), 6.f), pow(max(-c1, 0.f), 6.f), r);
217}
218
219float3 galaxyCollision(Ctx c, constant NativeFrame &f) {
220 float2 p = cosmLongAxis(c.uv / c.size, c.extent);
221 float orbit = c.t * .035;
222 float2 c1 = rot(float2(-.58, .14), orbit), c2 = rot(float2(.62, -.16), orbit);
223 float2 q1 = rot(p - c1, .5 + orbit); q1.y /= .6;
224 float2 q2 = rot(p - c2, -.9 + orbit); q2.y /= .5;
225 float3 g1 = cosmSpiral(q1 * .8, c.t * .16, 2.3), g2 = cosmSpiral(q2 * .9, -c.t * .13, -2.1);
226 float clumps = .45 + .55 * noise2(p * 9 + float2(c.t * .05, 0));
227
228 float disc1 = exp(-g1.z * 2.6), disc2 = exp(-g2.z * 3);
229 // Tidal tails: the outer arms keep going on the side facing away from the partner.
230 float2 away1 = normalize(c1 - c2);
231 float tailMask1 = smoothstep(-.2f, .5f, dot(normalize(p - c1 + 1e-4), away1));
232 float tailMask2 = smoothstep(-.2f, .5f, dot(normalize(p - c2 + 1e-4), -away1));
233 float tail1 = g1.x * exp(-g1.z * .75) * tailMask1 * smoothstep(.25f, .6f, g1.z);
234 float tail2 = g2.y * exp(-g2.z * .8) * tailMask2 * smoothstep(.25f, .6f, g2.z);
235
236 // A bridge of stars stretched between the pair.
237 float2 dv = c2 - c1; float len = length(dv);
238 float2 ax = dv / len, rel = p - c1;
239 float u = dot(rel, ax) / len, v = dot(rel, float2(-ax.y, ax.x));
240 float bridgeDist = abs(v - .16 * len * sin(PI * clamp(u, 0.f, 1.f)));
241 float bridge = glow(bridgeDist, .03) * smoothstep(-.05f, .2f, u) * smoothstep(1.05f, .8f, u);
242
243 float armA = g1.x * disc1, armB = g1.y * disc1, armC = (g2.x + g2.y) * disc2;
244 float tails = tail1 + tail2 + bridge * .6;
245
246 // Starburst knots sit on the arms; attacks blossom them with an expanding ring.
247 float2 kq = p * 7, kid = floor(kq), kl = (fract(kq) - .5 - (hash22(kid + 2.7) - .5) * .6) / 7;
248 float kd = length(kl);
249 float knotOn = step(.5, hash21(kid + 6.1));
250 float host = min(1.f, (armA + armB + armC) * 2 + tails * 2);
251 float knot = knotOn * host * (glow(kd, .006 + .01 * c.hit) * (.6 + 3 * c.hit)
252 + glow(kd - (.015 + .05 * (1 - c.hit)), .004) * c.hit * c.hit * 2);
253
254 float3 col = paint(0, f) * .07 + paint(.2, f) * exp(-min(length(p - c1), length(p - c2)) * 1.1) * .1;
255 col += paint(.75, f) * armA * clumps * (.5 + 2.4 * c.b.x);
256 col += paint(.52, f) * armB * clumps * (.5 + 2.4 * c.b.y);
257 col += paint(.3, f) * armC * clumps * (.55 + 2.4 * c.b.z);
258 col += paint(.45 + .25 * clumps, f) * tails * clumps * (.45 + 2.2 * c.b.w);
259 col += paint(.3, f) * (disc1 + disc2) * .3;
260 col += paint(.8, f) * (exp(-g1.z * g1.z * 90) * 1.8 + exp(-g2.z * g2.z * 110) * 1.6);
261 col += paint(.78, f) * knot;
262 col += paint(.7, f) * cosmStars(p * 10, .25, c.t) * .5;
263 return col;
264}
265NATIVE_EFFECT(galaxyCollision)
266
267// ---------------------------------------------------------------- 354 Solar Prominence
268float3 solarProminence(Ctx c, constant NativeFrame &f) {
269 float2 p = c.uv / c.size;
270 const float Rs = 2.2;
271 float2 C = float2(0, -.3 - .3 * max(c.extent.y - c.extent.x, 0.f) - Rs);
272 float2 d = p - C; float rr = length(d);
273 float h = rr - Rs; // height above the limb
274 float s = atan2(d.x, d.y) * Rs; // arc length along the limb
275 float above = smoothstep(-.004f, .02f, h);
276
277 // Granulated surface, foreshortened toward the limb.
278 float mu = sqrt(max(0.f, 1 - (rr / Rs) * (rr / Rs)));
279 float2 gq = float2(s, acos(clamp(rr / Rs, 0.f, 1.f)) * Rs) * 7;
280 float4 cell = voronoi(gq + float2(c.t * .05, 0), .9, c.t * .6);
281 float gran = mix(.6f, smoothstep(0.f, .45f, cell.y - cell.x), smoothstep(0.f, .3f, mu));
282 float plage = smoothstep(.55f, .8f, noise2(gq * .09 + 3.3)) * c.b.x;
283 float3 surface = paint(.5 + .25 * gran * mu + .15 * mu, f) * (.5 + 1.1 * mu) * (.7 + .4 * gran)
284 + paint(.9, f) * plage * gran * 1.2;
285 float disc = 1 - above;
286
287 float hp = max(h, 0.f);
288 float streamer = pow(noise2(float2(s * 1.4, hp * .3 - c.t * .04)), 2.f);
289 float corona = exp(-hp * 2.2) * (.3 + .9 * streamer) + exp(-hp * .45) * streamer * .3;
290 float spicule = pow(noise2(float2(s * 45, c.t * .7)), 4.f) * 5 * exp(-hp / (.03 + .06 * c.b.w)) * above;
291 float limb = glow(h, .012);
292 // A faint arcade of nested field arches high in the corona.
293 float sa = (floor(s / 1.1) + .5) * 1.1;
294 float2 ea = float2((s - sa) / .55, hp / (.8 + .2 * sin(sa * 3.1 + c.t * .1)));
295 float ela = length(ea);
296 float arcade = glow(cosmContour(ela * 5, c.px) / c.size, .0035) * smoothstep(.98f, .85f, ela) * smoothstep(.2f, .35f, ela);
297
298 float3 col = surface * disc;
299 col += (paint(0, f) * (.05 + .07 * streamer) + paint(.25 + .35 * streamer, f) * corona * .6) * above;
300 col += paint(.45, f) * arcade * above * (.2 + 1.3 * c.b.y);
301 col += paint(.85, f) * limb * .9 + paint(.3, f) * spicule * (.4 + 1.5 * c.b.w);
302
303 // Prominence loops: twisted triple-strand arches, each band lifting its own loops.
304 for (int k = 0; k < 6; ++k) {
305 float fk = float(k), level = c.b[k % 4];
306 float sk = (fk - 2.5) * .95 + .25 * sin(c.t * .05 + fk * 1.3);
307 float w = .2 + .16 * hash11(fk + 3.1);
308 float H = (.26 + .3 * hash11(fk + 8.7)) * (.6 + 1.2 * level) * (1 + .08 * sin(c.t * .3 + fk));
309 float tilt = .5 * (hash11(fk + 1.9) - .5);
310 float2 e = float2((s - sk - h * tilt) / w, h / H);
311 float el = length(e), along = atan2(e.y, e.x), scale = min(w, H);
312 float rope = 0;
313 for (int j = 0; j < 3; ++j) {
314 float off = .09 * sin(along * 5 + c.t * 1.1 + float(j) * 2.09 + fk);
315 rope += glow((el - 1 - off) * scale, .005 + .003 * level);
316 }
317 float flow = pow(.5 + .5 * sin(along * 7 - c.t * 2.2 + fk * 2), 4.f);
318 float halo = glow((el - 1) * scale, .035) * .3;
319 col += paint(.55 + .3 * flow + .05 * fk, f) * (rope * (.4 + .6 * flow) + halo) * above * (.3 + 1.5 * level);
320 }
321
322 // Attacks launch an ejection: a bright-fronted bubble rising off the limb.
323 float site = 1.1 * sin(c.t * .045);
324 float lift = 1 - c.hit;
325 float2 eb = float2(s - site, h - (.06 + lift * 1.1));
326 float rad = .08 + lift * .5;
327 float bubble = length(eb * float2(1, 1.15)) - rad;
328 float cme = glow(bubble, .02 + .03 * lift) * 1.4 + glow(length(eb + float2(0, rad * .5)), .04 + .06 * lift) * .9;
329 col += paint(.88, f) * cme * c.hit * c.hit * above;
330 return col;
331}
332NATIVE_EFFECT(solarProminence)
333
334// ---------------------------------------------------------------- 355 Quasar Jet
335float3 quasarJet(Ctx c, constant NativeFrame &f) {
336 float2 p = c.uv / c.size;
337 float ang = atan2(c.extent.y, c.extent.x) * .85 + .1 * sin(c.t * .05);
338 float2 q = rot(p, -ang);
339 float ax = abs(q.x), side = q.x < 0 ? -1.f : 1.f;
340 const float L = 1.15;
341 float reach = smoothstep(L + .15f, L - .25f, ax);
342 float wj = .022 + .06 * ax;
343 float flick = noise2(float2(ax * 7 - c.t * 6, side * 3.7));
344 float jetLevel = .6 + .4 * flick + c.b.w * 2.6 * flick * flick;
345 float jet = exp(-q.y * q.y / (wj * wj)) * reach / (1 + ax * .6) * jetLevel;
346 float spine = glow(q.y, .0035 + .002 * ax) * reach / (1 + ax) * jetLevel;
347
348 float helix = 0;
349 for (int j = 0; j < 2; ++j) {
350 float ph = ax * 13 - c.t * 2.2 + float(j) * PI + side;
351 helix += glow(q.y - wj * .85 * sin(ph), .004 + .002 * ax) * (.55 + .45 * cos(ph));
352 }
353 helix *= reach * (.3 + 1.8 * c.b.z);
354
355 float knots = 0;
356 for (int k = 0; k < 4; ++k) {
357 float ph = fract(c.t * .05 + float(k) * .25 + (side > 0 ? 0.f : .125f));
358 knots += glow(length(float2(ax - ph * L, q.y * 1.4)), .018 + .02 * ax) * sin(PI * ph);
359 }
360 float lx = .06 + (1 - c.hit) * L * .85;
361 float launch = glow(length(float2(ax - lx, q.y * 1.3)), .025 + .03 * lx) * c.hit * c.hit * 2.5;
362
363 // Radio lobes where the jets stall, with hot spots and glowing rims.
364 float2 lc = q - float2(side * L, 0);
365 float lt = fbm2(q * 2.3 + float2(-side * c.t * .04, c.t * .03), 4);
366 float lobeShape = length(lc * float2(.8, 1.2)) - (.34 + .26 * lt);
367 float strands = cosmRidge(lc * 3.4 + float2(c.t * .05, side * 2.3) + lt, 3) / .875;
368 float lobe = (smoothstep(.1f, -.25f, lobeShape) * .85 + exp(-max(lobeShape, 0.f) * 4) * .15) * (.15 + .85 * pow(strands, 3.f));
369 float lobeRim = glow(lobeShape, .012) * (.2 + .8 * lt);
370 float hotspot = glow(length(lc + float2(side * .12, 0)), .04);
371
372 float sheathW = .12 + .22 * ax / L;
373 float sheath = exp(-q.y * q.y / (sheathW * sheathW)) * reach;
374 float backflow = noise2(float2(ax * 4 + c.t * .5, q.y * 9));
375 float cocoon = sheath * (.3 + .7 * backflow) * (.2 + 1.3 * c.b.y);
376
377 float rp = length(p);
378 float hostGlow = exp(-length(rot(p, .4) * float2(1, 1.6)) * 2.2);
379 float haze = fbm2(p * 1.1 + float2(c.t * .01, 3), 4);
380
381 float3 col = paint(.02 + haze * .2, f) * (.05 + .14 * haze * haze);
382 col += paint(.3 + .5 * strands, f) * lobe * (.3 + 1.6 * c.b.x) + paint(.85, f) * (lobeRim * .3 + hotspot) * (.5 + 1.2 * c.b.x);
383 col += paint(.45, f) * cocoon;
384 col += paint(.6, f) * jet * .8 + paint(.95, f) * spine * 1.2;
385 col += paint(.8, f) * helix;
386 col += paint(.95, f) * (knots * 1.2 + launch);
387 col += paint(.5, f) * hostGlow * .22 + paint(.98, f) * (glow(rp, .022) * 3 + glow(rp, .1) * .25);
388 col += paint(.9, f) * cosmStars(p * 10, .22, c.t) * .5;
389 return col;
390}
391NATIVE_EFFECT(quasarJet)
392
393// ---------------------------------------------------------------- 356 Ring Plane Run
394float3 ringPlaneRun(Ctx c, constant NativeFrame &f) {
395 float2 uv = rot(c.uv / c.size, .1 * sin(c.t * .07));
396 // Low, gently tilted view across the ring plane; the planet lies far off to the upper left,
397 // so the ringlets sweep diagonally as long, nearly parallel arcs.
398 float den = max(1.1 - .32 * uv.y, .5f);
399 float2 P = uv / den;
400 const float2 C = float2(-9, 11);
401 const float Rref = 14;
402 float2 dp = P - C;
403 float sway = .9 * sin(c.t * .045); // the camera drifts slowly across the ringlets
404 float rho = length(dp) + sway, phi = atan2(dp.y, dp.x);
405 float flowArc = c.travel * 1.6 + c.t * .06;
406 float arc = phi * Rref + flowArc; // along-ring coordinate, streaming with travel
407
408 // Two shepherd gaps, each with moonlets that scallop the ringlet edges downstream.
409 float wake = 0, gapMask = 1, moonCover = 0;
410 float3 moonBody = 0, moonHalo = 0;
411 for (int g = 0; g < 2; ++g) {
412 float rg = g == 0 ? 13.55 : 14.92;
413 float drift = c.t * (g == 0 ? .11 : .08);
414 float local = arc - drift;
415 float idm = floor(local / 5 + .5);
416 float ds = local - idm * 5;
417 wake += .06 * sin(ds * 3) * exp(-max(ds, 0.f) * .35) * step(0.f, ds) * smoothstep(2.5f, 1.8f, ds)
418 * exp(-(rho - rg) * (rho - rg) * 5);
419 gapMask *= smoothstep(.05f, .13f, abs(rho - rg));
420 float phiM = (idm * 5 + drift - flowArc) / Rref;
421 float2 Pm = C + (rg - sway) * float2(cos(phiM), sin(phiM));
422 float uyM = 1.1 * Pm.y / (1 + .32 * Pm.y);
423 float denM = max(1.1 - .32 * uyM, .5f);
424 float radM = .075 * denM;
425 float2 nrm = (uv - float2(Pm.x * denM, uyM)) / radM;
426 float dm = length(nrm);
427 float body = fill(dm - 1, c.px / radM);
428 float lit = clamp(.35 - nrm.x * .5 + nrm.y * .6, 0.f, 1.f) * sqrt(max(0.f, 1 - dm * dm));
429 moonBody += paint(.55 + .4 * lit, f) * (.12 + 1.4 * lit) * body;
430 moonHalo += paint(.9, f) * glow(max(dm - 1, 0.f) * radM, .015) * .6;
431 moonCover = max(moonCover, body);
432 }
433 float rw = rho + wake;
434
435 // Ringlets: faint fills edged by glowing lines; each band lights its own family of lines.
436 float rr = rw * 5.5;
437 float n = floor(rr), m = floor(rr + .5);
438 float dens = hash11(n * 1.37 + 2.1);
439 float present = smoothstep(.08f, .2f, dens) * gapMask;
440 float lineDens = hash11(m * 2.31 + .7);
441 float level = c.b[int(m - 4 * floor(m / 4))];
442 float rim = glow(cosmContour(rr, c.px) / c.size, .003 + .003 * level) * smoothstep(.5f, .2f, fwidth(rr));
443 float fineLines = glow(cosmContour(rr * 3 + dens, c.px) / c.size, .0016) * smoothstep(.35f, .12f, fwidth(rr * 3));
444 float clumps = .3 + .7 * smoothstep(.2f, .8f, noise2(float2(arc * .9, rw * 7)));
445
446 // Attacks drive a spiral density wave outward across the plane.
447 float rhoF = 11.5 + (1 - c.hit) * 5.5;
448 float packet = exp(-(rho - rhoF) * (rho - rhoF) / .1) * c.hit;
449 float ripple = .5 + .5 * sin((rho - rhoF) * 32 - arc * .3);
450 float waveBoost = 1 + 2.6 * packet * ripple;
451
452 float2 gq = float2(arc, rw) * 2.5, gid = floor(gq);
453 float2 gl = fract(gq) - .5 - (hash22(gid + 1.3) - .5) * .5;
454 float grains = step(.55, hash21(gid + 4.2)) * exp(-dot(gl, gl) * 160) * (.5 + .5 * sin(c.t * 3 + hash21(gid) * TAU))
455 * (.3 + 2.4 * c.b.w);
456 float body = present * (.06 + .45 * dens * dens) * clumps * waveBoost;
457
458 float3 col = paint(0, f) * .045;
459 col += paint(.3 + .4 * dens, f) * body;
460 col += paint(.55 + .35 * lineDens, f) * rim * gapMask * (.15 + .85 * lineDens * lineDens) * (.4 + 2 * level) * waveBoost * (.4 + .6 * clumps);
461 col += paint(.7, f) * fineLines * present * (.2 + .5 * dens);
462 col += paint(.95, f) * (grains * present + packet * ripple * .3);
463 col = mix(col, moonBody, moonCover) + moonHalo;
464
465 // Planetshine from the inner rings warms the side nearest the planet.
466 col *= .7 + .5 * smoothstep(16.5f, 12.f, rho);
467 col = mix(col, paint(.45, f) * .18 + col * .6, smoothstep(.95f, .55f, den) * .6); // far side recedes into haze
468 return col;
469}
470NATIVE_EFFECT(ringPlaneRun)
471
472// ---------------------------------------------------------------- 357 Magnetosphere
473float3 magnetosphere(Ctx c, constant NativeFrame &f) {
474 float2 p = cosmLongAxis(c.uv / c.size, c.extent);
475 float2 q = rot(p - float2(-.15, 0), .18 * sin(c.t * .08));
476 const float Rp = .1;
477 // Dayside compressed by the wind (harder on attacks), nightside stretched into a tail.
478 float press = 1.45 + .4 * c.hit;
479 float xd = q.x * mix(press, 1 / (1 + 1.25 * max(q.x, 0.f)), smoothstep(-.1f, .1f, q.x));
480 float rd = length(float2(xd, q.y));
481 float shellL = rd * rd * rd / max(xd * xd, 1e-6f);
482 float lv = log2(shellL) * 2.4;
483 float id = floor(lv + .5);
484 float level = c.b[int(id - 4 * floor(id / 4))];
485 float lat = atan2(q.y, abs(xd));
486 float bounce = .55 + .45 * pow(.5 + .5 * sin(lat * 8 - c.t * 3 + id * 1.7), 3.f);
487 float xm = -.62 + .7 * q.y * q.y; // magnetopause
488 float inside = smoothstep(xm - .02, xm + .03, q.x) * smoothstep(Rp, Rp * 1.3, length(q));
489 float fieldD = cosmContour(lv, c.px) / c.size;
490 float crowd = smoothstep(.45f, .15f, fwidth(lv)); // fade lines where they bunch at the poles
491 float field = (glow(fieldD, .0045 + .003 * level) * bounce * crowd + glow(fieldD, .025) * .15 * crowd + .08 * (1 - crowd))
492 * inside * (.3 + 1.7 * level);
493
494 // Solar wind streamlines bending around the bow shock.
495 const float xb0 = -.86;
496 float body = 2.6 * .08 * log(1 + exp((q.x - xb0) / .08));
497 float y2 = q.y * q.y;
498 float eta = sign(q.y) * sqrt(max(y2 - body, 0.f));
499 float outside = smoothstep(0.f, .02f, y2 - body);
500 float wv = eta * 9;
501 float windD = cosmContour(wv, c.px) / c.size;
502 float dash = pow(.5 + .5 * sin(q.x * 12 - c.t * 4 + hash11(floor(wv + .5)) * TAU), 6.f);
503 float wind = (glow(windD, .003) * (.25 + dash * (.5 + 2.2 * c.b.w))) * outside * smoothstep(.45f, .15f, fwidth(wv));
504 float sheathMask = (1 - outside) * (1 - inside) * smoothstep(Rp, Rp * 1.3, length(q));
505 float sheath = fbm2(q * 4 + float2(-c.t * .3, 0), 3) * sheathMask;
506 float bowD = abs(q.x - xb0 - y2 / 2.6) / sqrt(1 + (2 * q.y / 2.6) * (2 * q.y / 2.6));
507 float mpD = abs(q.x - xm) / sqrt(1 + (1.4 * q.y) * (1.4 * q.y));
508
509 // Attacks fire an interplanetary shock front across the scene.
510 float xf = -2.8 + (1 - c.hit) * 5.4;
511 float shock = glow(q.x - xf - .25 * y2, .02) * c.hit * c.hit * 1.8;
512
513 float2 pole = float2(0, Rp * .9);
514 float aurora = glow(abs(length((q - pole) * float2(1, 3.2)) - .045), .006) + glow(abs(length((q + pole) * float2(1, 3.2)) - .045), .006);
515
516 float pr = length(q) / Rp;
517 float disc = fill(pr - 1, c.px / Rp);
518 float2 nrm = q / Rp;
519 float day = clamp(-nrm.x * .9 + .25, 0.f, 1.f) * sqrt(max(0.f, 1 - pr * pr));
520
521 float haze = fbm2(p * 1.4 + float2(-c.t * .04, 0), 3);
522 float3 col = paint(.02 + haze * .2, f) * (.05 + .12 * haze * haze);
523 col += paint(.35, f) * sheath * .55;
524 col += paint(.25 + .1 * id, f) * field * .9 + paint(.15, f) * wind;
525 col += paint(.8, f) * (glow(bowD, .006) * .9 + glow(bowD, .05) * .2) + paint(.6, f) * glow(mpD, .005) * inside * .4;
526 col += paint(.92, f) * shock + paint(.95, f) * aurora * (.3 + 2 * c.b.z);
527 col = mix(col, paint(.55 + .3 * day, f) * (.12 + 1.3 * day), disc) + paint(.9, f) * glow(max(pr - 1, 0.f) * Rp, .012) * .7;
528 col += paint(.9, f) * cosmStars(p * 9, .2, c.t) * .4;
529 return col;
530}
531NATIVE_EFFECT(magnetosphere)
532
533// ---------------------------------------------------------------- 358 Cosmic Web
534struct CosmCells { float d1; float d3; float edge; float2 a; float2 b; };
535CosmCells cosmCells(float2 p, float phase) {
536 float2 cell = floor(p), fp = fract(p);
537 float d1 = 8, d2 = 8, d3 = 8; float2 a = 0, b = 0, sa = 0, sb = 0;
538 for (int y = -1; y <= 1; ++y) for (int x = -1; x <= 1; ++x) {
539 float2 g = float2(x, y), id = cell + g, h = hash22(id);
540 float2 site = g + .5 + .4 * sin(phase * (.4 + .3 * h) + h * TAU);
541 float d = length(site - fp);
542 if (d < d1) { d3 = d2; d2 = d1; b = a; sb = sa; d1 = d; a = id; sa = site; }
543 else if (d < d2) { d3 = d2; d2 = d; b = id; sb = site; }
544 else if (d < d3) d3 = d;
545 }
546 CosmCells k;
547 k.d1 = d1; k.d3 = d3; k.a = a; k.b = b;
548 k.edge = abs(dot(fp - .5 * (sa + sb), normalize(sb - sa + 1e-5)));
549 return k;
550}
551
552float3 cosmicWeb(Ctx c, constant NativeFrame &f) {
553 float haze = fbm2(c.uv * .8 + float2(c.t * .01, 0), 3);
554 float3 col = paint(.02 + haze * .2, f) * (.04 + .12 * haze * haze);
555 for (int k = 0; k < 6; ++k) {
556 float2 s = layerSlot(k, 6, c.travel * .09);
557 float sc = layerScale(s.x, 2.4) / c.size;
558 float2 q = rot(c.uv * sc, s.y * 1.1) + hash22(float2(s.y, 7.3)) * 40;
559 q += (float2(noise2(q * .5), noise2(q * .5 + 9.1)) - .5) * .7;
560 CosmCells v = cosmCells(q, c.t * .25 + s.y);
561 float edge = v.edge / sc, node = (v.d3 - v.d1) / sc;
562 float pair = hash21(v.a + v.b);
563 int band = int(pair * 3.999);
564 float level = c.b[band];
565 float strength = fract(pair * 7.3); // some filaments thick and bright, some faint
566 float fade = layerFade(s.x) * (.3 + .7 * s.x) * (1 - c.fog * .5 * (1 - s.x));
567 float beads = .5 + .5 * noise2(q * 3.5 + s.y);
568 float pulseAlong = .6 + .4 * sin(v.d1 * 9 - c.t * 2 + hash21(v.a) * TAU);
569 float wide = (.0015 + .004 * s.x) * (.6 + strength);
570 float fil = (glow(edge, wide * (1 + 1.2 * level)) * (.3 + 2 * level) * beads * pulseAlong
571 + glow(edge, .03 * (.4 + s.x)) * .1) * (.3 + .9 * strength);
572 float lightUp = step(.45, hash21(v.a + 3.3));
573 float flare = 1 + 3.5 * c.hit * lightUp;
574 float hub = glow(node, .007 + .014 * s.x) * (.7 * flare) + glow(node, .06 * (.4 + s.x)) * .22 * flare;
575 col += (paint(.25 + s.y * .11 + float(band) * .1, f) * fil + paint(.75, f) * hub) * fade;
576 }
577 return col;
578}
579NATIVE_EFFECT(cosmicWeb)
580
581// ---------------------------------------------------------------- 359 Einstein Ring
582float3 cosmGalaxySource(float2 d, float spin, float hue, constant NativeFrame &f) {
583 float r = length(d);
584 float phi = atan2(d.y, d.x) - 2.6 * log(r + .02);
585 float arms = pow(.5 + .5 * cos(2 * phi - spin), 5.f);
586 float body = exp(-r * 6) * (.15 + .85 * arms) * 1.4 + exp(-r * r * 300) * 1.8;
587 return paint(hue + .3 * exp(-r * 6), f) * body;
588}
589
590float3 einsteinRing(Ctx c, constant NativeFrame &f) {
591 float2 th = c.uv / c.size;
592 float r = max(length(th), 1e-3f);
593 // Point lens plus a slowly turning external shear; mass breathes with the bass.
594 float thetaE = .56 * (1 + .3 * c.b.x);
595 float2 beta = th - thetaE * thetaE * th / (r * r);
596 float sa = c.t * .03;
597 float2 sh = float2(cos(2 * sa), sin(2 * sa)) * .1;
598 beta -= float2(sh.x * th.x + sh.y * th.y, sh.y * th.x - sh.x * th.y);
599 float centre = smoothstep(.06f, .22f, r);
600
601 float bg = fbm2(beta * 1.6 + float2(3, c.t * .01), 4);
602 float3 col = paint(1 - bg * .15, f) * (.035 + .05 * bg * bg) * centre;
603 col += paint(.1 + bg * .8, f) * pow(smoothstep(.4f, .9f, bg), 2.f) * .25 * centre;
604 for (int j = 0; j < 5; ++j) {
605 float fj = float(j);
606 float2 home = float2(cos(fj * 2.4 + 1), sin(fj * 2.4 + 1)) * (.1 + .36 * fract(fj * .618 + .2));
607 float2 sp = rot(home, c.t * (.04 + .015 * fj) * (j % 2 == 0 ? 1 : -1));
608 float2 d = rot(beta - sp, fj * 1.3 + c.t * .1);
609 d.y /= .6;
610 float level = c.b[j % 4];
611 col += cosmGalaxySource(d * (1.1 + fj * .12), c.t * .3 + fj, .05 + fj * .22, f) * (.4 + 1.9 * level) * centre;
612 }
613 // Attacks slide a bright new source behind the lens: a ring forms, then breaks into arcs.
614 float2 sh2 = rot(float2(-.08 + (1 - c.hit) * .9, .04), c.t * .07);
615 float flash = length(beta - sh2);
616 col += paint(.75, f) * (glow(flash, .03) * 2 + glow(flash, .1) * .4) * c.hit * c.hit * centre;
617 col += paint(.6, f) * cosmStars(beta * 9, .25, c.t) * .5 * centre;
618 col += paint(.9, f) * glow(r, .05) * .3;
619 return col;
620}
621NATIVE_EFFECT(einsteinRing)