Branchwell

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

1// Original PulseCanvas designs 300–309: tunnels with new structures. None of these is a
2// centred polar tunnel with a pattern on its wall; each has its own geometry (a bending path,
3// an off-axis camera, two bores, a coil, pleats, open sky, a vertical shaft, veils, pipes
4// seen from outside, a twisting ribbon). Original procedural code; see the prelude.
5
6// ---------------------------------------------------------------- shared helpers
7float boreAttack(Ctx c) { return smoothstep(.04f, .7f, c.hit); }
8float boreBand(Ctx c, float id) { return c.b[min(3, int(id - 4 * floor(id / 4)))]; }
9float boreWrap(float x, float period) { return x - period * floor(x / period); }
10float boreSq(float x) { return x * x; }
11float boreTwinkle(float seed, float t) { return pow(.5f + .5f * sin(t * (4 + 5 * seed) + seed * 37), 6.f); }
12// Exit distance from inside a cylinder of radius r around the z axis (o = origin in the cross-section).
13float boreCylinder(float2 o, float3 rd, float r) {
14 float a = max(dot(rd.xy, rd.xy), 1e-6f), b = dot(o, rd.xy), k = dot(o, o) - r * r;
15 return (-b + sqrt(max(b * b - a * k, 0.f))) / a;
16}
17
18// ---------------------------------------------------------------- 300 Serpent Bore
19// A scaled tube whose centreline winds through space. The camera rides the bends, so the
20// far end swings around the frame while the walls sweep past. Overlapping glowing plates
21// line the wall: bass lifts rows of plates, low-mids sweep a spiral sheen, high-mids light
22// four dorsal stripes, highs glint the rims and attacks roll a lit wave toward the viewer.
23float2 boreSerpentPath(float z) {
24 return float2(1.9 * sin(z * .23) + .9 * sin(z * .41 + 1.3), 1.4 * sin(z * .19 + 2.1) + .8 * sin(z * .37 + .4));
25}
26
27float3 serpentBore(Ctx c, constant NativeFrame &f) {
28 float camZ = c.travel * .85;
29 // The view follows the path's recent heading, so the bends ahead swing the far end around the frame.
30 float3 eye = float3(boreSerpentPath(camZ), camZ);
31 float3 heading = float3(boreSerpentPath(camZ + .4) - boreSerpentPath(camZ - 1.6), 2);
32 float roll = .4 * sin(c.t * .06);
33 float3 rd = cameraRay(c.uv, heading, float3(sin(roll), cos(roll), 0), 1.2);
34 float t = .02;
35 for (int i = 0; i < 56; ++i) {
36 float3 p = eye + rd * t;
37 float d = 1 - length(p.xy - boreSerpentPath(p.z));
38 if (d < .001 + t * .002 || t > 42) break;
39 t += d * .65;
40 }
41 float3 p = eye + rd * t;
42 float2 q = p.xy - boreSerpentPath(p.z);
43 const float columns = 22;
44 float density = 2.7 / c.size;
45 float2 g = float2(atan2(q.y, q.x) * columns / TAU, p.z * density);
46 // Staggered plates; the row further away lies on top, so every visible rim faces the viewer.
47 float base = floor(g.y), cover = 9, lift = 0;
48 float4 s = 0;
49 for (int k = 1; k >= -1; --k) {
50 float row = base + float(k), shift = .5 * boreWrap(row, 2);
51 float col = floor(g.x - shift + .5);
52 float rowLift = c.b.x * boreSq(boreSq(.5 + .5 * sin(row * .55 - c.t * 1.6)));
53 float2 local = g - float2(col + shift, row);
54 float d = length(local * float2(1, 1.08)) - (.72 + .1 * rowLift);
55 s = float4(d, local.y, col, row); lift = rowLift;
56 if (d < 0) break;
57 cover = min(cover, d);
58 }
59 float colId = boreWrap(s.z, columns), row = s.w;
60 float seed = hash21(float2(colId, row));
61 float foot = t * c.px * density * 1.4 + .01;
62 float inner = clamp(-s.x / .72, 0.f, 1.f);
63 float rim = 1 - smoothstep(0.f, .08f + foot, -s.x);
64 float shade = 1 - .7 * exp(-max(cover, 0.f) * 8);
65 float sheen = .3 + .7 * boreSq(1 - inner);
66 float hue = row * .031 + .22 * sin(colId * TAU / columns) + seed * .08;
67 float spiral = c.b.y * pow(.5f + .5f * sin(colId * TAU / columns * 2 + row * .21 - c.t * .8), 6.f);
68 float stripe = c.b.z * step(boreWrap(colId, 4), .5f);
69 float wave = c.hit * exp(-boreSq((row / density - camZ - 1.2 - 9 * boreAttack(c)) / .8));
70 float3 plate = paint(hue, f) * (.12 + .88 * sheen * sheen) * shade * (.62 + lift * 1.6 + spiral * 2 + stripe * 1.3 + wave * 2.2);
71 plate += paint(hue + .3, f) * rim * shade * (.5 + lift * 1.2 + wave * 1.8);
72 plate += paint(.92, f) * rim * c.b.w * boreTwinkle(seed, c.t) * 2.5;
73 float3 haze = paint(.8 + .1 * sin(c.t * .1), f) * .4;
74 return mix(haze, plate / (1 + t * .08), exp(-t * (.05 + c.fog * .04)));
75}
76NATIVE_EFFECT(serpentBore)
77
78// ---------------------------------------------------------------- 301 Ringworld Drift
79// Inside a vast cylinder habitat, standing just above its floor and looking down its axis.
80// Terraced bands ring the habitat: each one hugs the floor below, curves up both sides and
81// closes overhead, so they nest as eccentric arches around a vanishing point near the floor.
82// The axial sun line hangs overhead. Each terrace's lamps pulse with its band, and attacks
83// send light climbing up both sides of the nearer terraces to meet overhead.
84float3 ringworldDrift(Ctx c, constant NativeFrame &f) {
85 float stand = -PI * .5 + .45 * sin(c.t * .045) + .15 * sin(c.t * .11);
86 float2 radial = float2(cos(stand), sin(stand)), side = float2(-radial.y, radial.x);
87 float2 eye = radial * .84;
88 float3 rd = cameraRay(c.uv, float3(-radial * .62 + side * .1 * sin(c.t * .07), 1), float3(-radial, 0), 1.05);
89 float t = min(boreCylinder(eye, rd, 1), 90.f);
90 float2 q = eye + rd.xy * t;
91 float camZ = c.travel * 1.8, z = t * rd.z + camZ;
92 float angle = atan2(q.y, q.x);
93 float foot = t * c.px / 1.05;
94 float graze = max(abs(dot(rd.xy, q)), .03f);
95 // Terraces along the axis: a lit face rising to a bright lip, then a shadowed step.
96 float span = 1.7 * c.size, u = z / span, terrace = floor(u), fr = u - terrace;
97 float footU = foot / graze / span;
98 float seed = hash11(terrace + 11), band = boreBand(c, terrace);
99 float face = smoothstep(0.f, .08f + footU, fr) * (1 - smoothstep(.86f - footU, .9f, fr));
100 float lip = glow(fr - .9, .018 + footU);
101 // Each terrace is divided into fields around the habitat; some glow as panels of light.
102 const float sectors = 18;
103 float sector = angle * sectors / TAU, sectorId = boreWrap(floor(sector), sectors);
104 float seam = glow(abs(fract(sector) - .5) - .5, .02 + foot * sectors / TAU / max(graze, .15f));
105 float panelSeed = hash21(float2(terrace, sectorId));
106 float panel = smoothstep(.3f, .65f, panelSeed) * (.6 + band * 2.2 * boreSq(.5 + .5 * sin(sectorId * 2.1 + terrace - c.t * 1.4)));
107 float hue = .32 + .26 * sin(terrace * .7) + panelSeed * .25;
108 float3 col = mix(deep(hue + .35, f), paint(hue, f), .3 + .7 * fr) * face * (.12 + (.3 + .7 * fr * fr) * panel) * (1 - seam * .75) + deep(.5, f) * .06;
109 // Lamps around each lip, pulsing with the terrace's band.
110 const float lamps = 56;
111 float around = angle * lamps / TAU, lampId = floor(around + .5);
112 float arc = (around - lampId) * TAU / lamps, gapZ = (fr - .9) * span;
113 float w = .035 + foot;
114 float lampGlow = exp(-(arc * arc + gapZ * gapZ) / (w * w)) * boreSq(.035 / w);
115 float pulse = boreSq(.5 + .5 * sin(boreWrap(lampId, lamps) * .9 + terrace * 1.7 - c.t * 1.8));
116 float nearFade = smoothstep(.3f, 1.8f, t); // lips fade before they sweep past the camera
117 col += (paint(.7 + seed * .2, f) * lip * (.45 + band * 1.6) + paint(.92, f) * lampGlow * (.8 + band * 6 * pulse)) * nearFade;
118 // Attack: light climbs both sides of the nearer terraces from the floor to meet overhead.
119 float h = boreAttack(c);
120 float fromFloor = abs(atan2(sin(angle - stand), cos(angle - stand)));
121 float climb = c.hit * exp(-boreSq((fromFloor - (1 - h) * PI) / .35)) * smoothstep(14.f, 3.f, z - camZ);
122 col += paint(.9, f) * climb * (lip * 3 + face * .7);
123 col = mix(paint(.6, f) * .5, col, exp(-t * (.03 + c.fog * .03)));
124 // The axial sun line, with low-mid surges running along it.
125 float s0 = -dot(eye, rd.xy) / max(dot(rd.xy, rd.xy), 1e-6f);
126 float axis = length(eye + rd.xy * s0), sunZ = s0 * rd.z + camZ;
127 float sun = glow(axis, .025 + s0 * c.px * .6) * step(0.f, s0);
128 float surge = .5 + .3 * sin(sunZ * .25 - c.t * .3) + c.b.y * 3 * pow(.5f + .5f * sin(sunZ * .6 - c.t * 2.4), 6.f);
129 return col + paint(.95, f) * sun * surge * exp(-s0 * .02);
130}
131NATIVE_EFFECT(ringworldDrift)
132
133// ---------------------------------------------------------------- 302 Twin Bore
134// Two bores side by side, each with its own vanishing point and ring cadence, separated by a
135// glowing translucent membrane. Bass and low-mids drive the left bore's rings and ribs, the
136// high-mids and highs drive the right bore's slats and sparks, and attacks ripple the membrane.
137float3 twinBore(Ctx c, constant NativeFrame &f) {
138 float spread = clamp(.36f * c.extent.x, .34f, .62f);
139 float2 drift = .06 * float2(sin(c.t * .13), cos(c.t * .11));
140 float2 vl = float2(-spread, .05 * sin(c.t * .09)) + drift, vr = float2(spread, -.05 * sin(c.t * .09)) + drift;
141 float2 mid = (vl + vr) * .5, across = normalize(vr - vl);
142 float2 dl = c.uv - vl, dr = c.uv - vr;
143 float rl = max(length(dl), .015f), rr = max(length(dr), .015f);
144 // Left bore: thick filled rings on a slow cadence, ribs along the bore.
145 float zl = .8 / rl, al = atan2(dl.y, dl.x);
146 float ringL = (zl + c.travel * 1.3) * 1.3 / c.size, idL = floor(ringL + .5);
147 float swell = c.b.x * boreSq(.5 + .5 * sin(idL * 1.1 - c.t * 1.8));
148 float eL = abs(fract(ringL + .5) - .5), aaL = fwidth(ringL);
149 float bandL = 1 - smoothstep(.1f + .16f * swell, .14f + .16f * swell + aaL, eL);
150 float ribs = glow(sin(al * 5 + zl * .15), .08) * (.15 + c.b.y * 2);
151 float3 left = paint(.55 + .12 * sin(al) + ringL * .045, f) * (.1 + bandL * (.55 + swell * 1.8) + exp(-eL * eL * 30) * .25)
152 + paint(.8, f) * ribs;
153 left = mix(paint(.85, f) * .8, left, exp(-zl * .07));
154 // Right bore: dense segmented rings of slats with sparks at the joints.
155 float zr = .8 / rr, ar = atan2(dr.y, dr.x);
156 float ringR = (zr + c.travel * 1.3) * 2.4 / c.size, idR = floor(ringR + .5);
157 float eR = abs(fract(ringR + .5) - .5), aaR = fwidth(ringR);
158 float slot = ar * 14 / TAU + idR * .5, slotId = boreWrap(floor(slot + .5), 14);
159 float slat = smoothstep(.12f, .3f, abs(fract(slot) - .5)) * (1 - smoothstep(.12f, .2f + aaR, eR));
160 float lift = c.b.z * boreSq(.5 + .5 * sin(idR * .7 + slotId * .9 - c.t * 2.2));
161 float spark = exp(-(boreSq(fract(slot + .5) - .5) + eR * eR) * 60) * c.b.w * boreTwinkle(hash21(float2(slotId, idR)), c.t) * 4;
162 float3 right = paint(.22 + .1 * sin(ar * 2) + ringR * .03, f) * (.09 + slat * (.5 + lift * 2.5)) + paint(.95, f) * spark;
163 right = mix(paint(.4, f) * .8, right, exp(-zr * .07));
164 // The membrane: the bisecting sheet, rippled by attacks as a wave running toward the viewer.
165 float zs = .8 / max(length(float2(spread, dot(c.uv - mid, float2(-across.y, across.x)))), .02f);
166 float front = .8 + 7 * boreAttack(c);
167 float rippleEnvelope = c.hit * exp(-boreSq((zs - front) / 1.6));
168 float bend = .14 * rippleEnvelope * sin(zs * 3 - c.t * 3) + .03 * sin(c.uv.y * 4 + c.t * .5);
169 float mx = dot(c.uv - mid, across) - bend * min(1 / zs, 1.5f);
170 float wm = .012 + .05 / zs;
171 float sheet = exp(-abs(mx) / (wm * 3));
172 float3 col = mix(left, right, smoothstep(-c.px * 1.5f, c.px * 1.5f, mx));
173 col = mix(col, (left + right) * .5, sheet * .4);
174 col += filmColor(zs * .15 + c.uv.y * .4 + c.t * .04, f) * (glow(mx, wm) * .9 + sheet * .25) * (1 + rippleEnvelope * 3);
175 return col;
176}
177NATIVE_EFFECT(twinBore)
178
179// ---------------------------------------------------------------- 303 Coil Run
180// Flying off-axis inside a giant helical coil of flat light ribbon. Gaps between the turns
181// open onto a deep nebula on a wider cylinder, which drifts past with its own parallax. Bass
182// breathes the coil pitch in travelling waves, low-mids light the centre lane, high-mids the
183// edge dashes, highs sparkle the nebula, and attacks chase a bright pulse along the ribbon.
184float3 coilRun(Ctx c, constant NativeFrame &f) {
185 float orbit = c.t * .04;
186 float2 eye = .55 * float2(cos(orbit), sin(orbit));
187 float roll = .3 * sin(c.t * .05);
188 float3 rd = cameraRay(c.uv, float3(-eye * .35, 1), float3(-sin(roll), cos(roll), 0), 1.15);
189 float camZ = c.travel * 1.6;
190 float t = boreCylinder(eye, rd, 1);
191 float2 q = eye + rd.xy * t;
192 float zw = t * rd.z + camZ;
193 float pitch = 1.5 * c.size, duty = .6;
194 float breath = c.b.x * .3 * sin(zw * .55 - c.t * 1.4);
195 float phase = zw / pitch + breath - atan2(q.y, q.x) / TAU;
196 float u = fract(phase);
197 float foot = t * c.px / 1.15;
198 float aa = foot / max(abs(dot(rd.xy, q)), .03f) / pitch + foot / TAU + .004;
199 float mask = smoothstep(0.f, aa, u) * (1 - smoothstep(duty - aa, duty, u));
200 mask = mix(mask, duty, smoothstep(.15f, .5f, aa));
201 float across = u / duty, along = zw / pitch + breath - u;
202 float edge = glow(min(u, duty - u), .012 + aa);
203 float lane = glow(across - .5, .03 + aa) * (.2 + c.b.y * 2.2);
204 float dash = pow(.5f + .5f * cos(along * TAU * 6 - c.t * 3), 8.f) * edge * (.3 + c.b.z * 3);
205 float h = boreAttack(c);
206 float chase = c.hit * exp(-boreSq((along - camZ / pitch - .3 - (1 - h) * 7) / .16));
207 float hue = along * .19 + across * .12;
208 float rails = glow(abs(across - .5) - .32, .02 + aa) * .5;
209 float3 ribbon = mix(deep(hue + .3, f), paint(hue, f), .4 + .6 * sin(across * PI)) * (.2 + .22 * sin(across * PI)) + paint(hue + .35, f) * (edge * .6 + rails)
210 + paint(.9, f) * (lane + dash + chase * 3);
211 // Nebula on an outer cylinder seen through the gaps.
212 float t2 = boreCylinder(eye, rd, 4);
213 float2 q2 = eye + rd.xy * t2;
214 float z2 = t2 * rd.z + camZ;
215 float n = fbm3(float3(q2 * .45, z2 * .12 + c.t * .02), 3);
216 float3 nebula = paint(n * 1.4 + .25, f) * (.05 + .9 * n * n) + deep(.3 + n, f) * .12;
217 float2 starCell = float2(floor((atan2(q2.y, q2.x) + PI) / TAU * 90), floor(z2 * 3));
218 float starSeed = hash21(starCell);
219 float star = step(.86f, starSeed) * (.25 + c.b.w * 4 * boreTwinkle(starSeed, c.t));
220 nebula += paint(.85, f) * star * exp(-boreSq(length(fract(float2((atan2(q2.y, q2.x) + PI) / TAU * 90, z2 * 3)) - .5) * 4));
221 float3 col = mix(nebula, ribbon, mask * exp(-t * .03));
222 return col;
223}
224NATIVE_EFFECT(coilRun)
225
226// ---------------------------------------------------------------- 304 Bellows Passage
227// A pleated rectangular bellows. Each pleat is real geometry: the wall folds in to a lit ridge
228// and out to a shadowed valley, and the faces that turn away hide behind the ridges. Bass
229// squeezes the pleats in waves rolling toward the viewer, low-mids smoulder in the valleys,
230// high-mids light the corner creases, highs spark along the ridges and attacks flash a band
231// of ridges.
232float boreBellowsMetric(float2 d) {
233 float2 a = abs(d) * float2(.82, 1.18);
234 a *= a; a *= a;
235 return sqrt(sqrt(a.x + a.y));
236}
237
238float3 bellowsPassage(Ctx c, constant NativeFrame &f) {
239 float2 sway = .1 * float2(sin(c.t * .13), cos(c.t * .11));
240 float2 dir = (c.uv - sway) / 1.1;
241 float s = max(boreBellowsMetric(dir), 1e-4f);
242 float camZ = c.travel * 1.1;
243 float pitch = .42 * c.size, depthA = .24;
244 float slope = depthA * 2 * (1 / pitch + .2) + s;
245 // March to the first wall crossing; the Lipschitz step never skips a fold.
246 float z = 1 / s;
247 for (int i = 0; i < 32; ++i) {
248 if (z > 34) break;
249 float zw = z + camZ;
250 float u = zw / pitch + c.b.x * .4 * sin(zw * .45 + c.t * 1.8);
251 float wall = 1 + depthA * (1 - abs(2 * fract(u) - 1));
252 z += max(wall - z * s, 0.f) / slope;
253 }
254 float zw = z + camZ;
255 float u = zw / pitch + c.b.x * .4 * sin(zw * .45 + c.t * 1.8), fu = fract(u), ridgeId = floor(u + .5);
256 float dw = depthA * (fu < .5 ? 2.f : -2.f) / pitch;
257 float lambert = clamp((s - dw) / sqrt((1 + dw * dw) * (1 + s * s)), 0.f, 1.f);
258 float aa = fwidth(u);
259 float ramp = clamp(fu * 2 - 1, 0.f, 1.f), facing = smoothstep(.46f, .54f, fu);
260 float ridge = 1 - smoothstep(0.f, .04f + aa * 1.5f, abs(fu - round(fu)));
261 float valley = exp(-boreSq((fu - .5) / .07));
262 float2 e = abs(dir) * float2(.82, 1.18) / s;
263 float crease = glow(e.x - e.y, .025 + aa * .2) * (.15 + c.b.z * 2.5) * facing;
264 float around = atan2(dir.y * 1.18, dir.x * .82);
265 float sparks = pow(.5f + .5f * cos(around * 24 + ridgeId * 1.7 - c.t * 2), 12.f) * ridge * c.b.w * 3;
266 float h = boreAttack(c);
267 float flash = c.hit * exp(-boreSq((zw - camZ - 1.5 - h * 10) / 1.3));
268 float hue = .12 + .1 * sin(ridgeId * .45) + .06 * sin(around * 2);
269 float3 face = mix(deep(hue + .1, f), paint(hue, f), ramp) * (.1 + .6 * ramp) * (.35 + .65 * lambert);
270 float3 col = mix(deep(hue + .1, f) * .3, face, facing);
271 float valleyId = floor(u);
272 col += paint(.3, f) * valley * c.b.y * 2.5 * (.4 + .6 * boreSq(sin(valleyId * .9 - c.t * 1.5)));
273 col += paint(.8, f) * ridge * (.45 + flash * 4) + paint(.7, f) * crease + paint(.95, f) * sparks;
274 return mix(paint(.6, f) * .45, col, exp(-z * (.06 + c.fog * .04)));
275}
276NATIVE_EFFECT(bellowsPassage)
277
278// ---------------------------------------------------------------- 305 Branching Bore
279// A bore that forks every few lengths. The camera follows one branch, swinging left and
280// right at alternate forks; the branch not taken glows hot as it peels away to the side.
281// Junction rims light with their band, ribs and grooves take bass and low-mids, and attacks
282// send a ring racing down the branch ahead.
283float2 boreBranchMain(float z) {
284 float k = floor(z / 6), u = z / 6 - k, s = u * u * (3 - 2 * u);
285 return float2(1.4 * (boreWrap(k, 2) < .5 ? s : 1 - s), .5 * sin(z * .17));
286}
287float2 boreBranchSide(float z, float k) {
288 float u = max(z / 6 - k, 0.f), grow = u < 1 ? u * u : 2 * u - 1;
289 float away = boreWrap(k, 2) < .5 ? -1 : 1;
290 return float2(1.4 * (away < 0 ? 0 : 1) + away * 1.5 * grow, .5 * sin(z * .17) + .45 * grow * (hash11(k) - .5));
291}
292// x = interior field of the union, y = taken branch, z = branch not taken, w = its fork id.
293float4 boreBranchField(float3 p) {
294 float k = floor(p.z / 6);
295 float main = 1 - length(p.xy - boreBranchMain(p.z));
296 float sideA = 1 - length(p.xy - boreBranchSide(p.z, k));
297 float sideB = 1 - length(p.xy - boreBranchSide(p.z, k - 1)) - max(0.f, (k - 1) * 6 - p.z) * 3;
298 float side = max(sideA, sideB), fork = sideA > sideB ? k : k - 1;
299 return float4(smax(main, side, .25), main, side, fork);
300}
301
302float3 branchingBore(Ctx c, constant NativeFrame &f) {
303 float camZ = c.travel * 1.6;
304 float3 eye = float3(boreBranchMain(camZ), camZ), ahead = float3(boreBranchMain(camZ + 2.5), camZ + 2.5);
305 float roll = .25 * sin(c.t * .06);
306 float3 rd = cameraRay(c.uv, ahead - eye, float3(sin(roll), cos(roll), 0), 1.15);
307 float t = .02;
308 float4 g = 0;
309 for (int i = 0; i < 60; ++i) {
310 g = boreBranchField(eye + rd * t);
311 if (g.x < .001 + t * .0015 || t > 40) break;
312 t += g.x * .75;
313 }
314 float3 p = eye + rd * t;
315 float takenSide = smoothstep(-.03f, .03f, g.y - g.z);
316 float fork = g.w, uSide = p.z / 6 - fork;
317 float2 q = p.xy - (takenSide > .5 ? boreBranchMain(p.z) : boreBranchSide(p.z, fork));
318 float around = atan2(q.y, q.x);
319 float rib = p.z * 1.7 / c.size, ribId = floor(rib + .5), eRib = abs(fract(rib + .5) - .5);
320 float foot = t * c.px * 1.7 / c.size + .01;
321 float ribs = (1 - smoothstep(.06f, .1f + foot, eRib)) * (.4 + c.b.x * 2.5 * boreSq(.5 + .5 * sin(ribId * .8 - c.t * 1.7)));
322 float veins = glow(sin(around * 4 + 1.3 * sin(p.z * .7 + around * 2) + p.z * .3), .12) * (.16 + c.b.y * 1.8);
323 float h = boreAttack(c);
324 float ring = c.hit * exp(-boreSq((p.z - camZ - 3.2 - (1 - h) * 18) / .5)) * takenSide;
325 // The branch not taken burns hotter the deeper it runs.
326 float heat = (1 - takenSide) * smoothstep(-.1f, 1.4f, uSide);
327 float3 wall = paint(.08 + .07 * sin(around * 3 + p.z * .4), f) * (.26 + .12 * cos(around - 1));
328 wall += paint(.35 + heat * .3, f) * ribs * (.5 + heat) + paint(.55, f) * veins + paint(.92, f) * ring * 3;
329 wall += paint(.4 + .15 * sin(p.z * .6), f) * heat * (.4 + c.b.w * 1.2 * boreTwinkle(hash11(fork + 7), c.t));
330 float split = length(boreBranchMain(p.z) - boreBranchSide(p.z, fork));
331 float rim = glow(g.y - g.z, .04) * smoothstep(.35f, 1.f, split) * smoothstep(2.2f, 1.f, uSide);
332 wall += paint(.65 + .2 * sin(fork * 1.7), f) * rim * (.5 + boreBand(c, fork) * 4);
333 return mix(paint(.45, f) * .4, wall / (1 + t * .04), exp(-t * (.045 + c.fog * .04)));
334}
335NATIVE_EFFECT(branchingBore)
336
337// ---------------------------------------------------------------- 306 Skyward Shaft
338// Looking up a square shaft while rising through it. Four walls of lit apertures converge on
339// a bright square of sky. Each wall's windows pulse with their own band, floor slabs mark the
340// storeys, and attacks drop rings of light down the shaft toward the viewer.
341float3 skywardShaft(Ctx c, constant NativeFrame &f) {
342 float roll = c.t * .035;
343 float2 eye = float2(.38 * sin(c.t * .07), .3 * cos(c.t * .09));
344 float3 rd = cameraRay(c.uv, float3(-eye * .25 + .06 * float2(sin(c.t * .11), cos(c.t * .13)), 1), float3(-sin(roll), cos(roll), 0), 1);
345 float2 safe = float2(abs(rd.x) < 1e-5 ? 1e-5 : rd.x, abs(rd.y) < 1e-5 ? 1e-5 : rd.y);
346 float2 lim = (sign(safe) - eye) / safe;
347 bool xWall = lim.x < lim.y;
348 float t = min(lim.x, lim.y);
349 float2 hit = eye + rd.xy * t;
350 float height = t * rd.z, top = 12;
351 float wallId = xWall ? (rd.x > 0 ? 0 : 1) : (rd.y > 0 ? 2 : 3);
352 float acrossW = xWall ? hit.y : hit.x;
353 float zw = height + c.travel * 1.5;
354 float foot = t * c.px;
355 float2 cellSize = float2(.5, .62) * c.size;
356 float2 g = float2(acrossW + 1, zw) / cellSize, cell = floor(g), local = (fract(g) - .5) * cellSize;
357 float seed = hash31(float3(cell, wallId));
358 float box = sdBox2(local, cellSize * float2(.3, .3));
359 float window = 1 - smoothstep(-foot, foot, box);
360 float frame = glow(box, .008 + foot);
361 float band = c.b[int(wallId)];
362 float lit = step(.4f, seed) * (.25 + .75 * boreSq(.5 + .5 * sin(c.t * .6 + seed * 9)));
363 float pulse = band * boreSq(.5 + .5 * sin(cell.y * .7 - c.t * 2 + seed * 3));
364 float slab = 1 - smoothstep(.03f, .05f + foot / cellSize.y, abs(fract(g.y / 5 + .1) - .1));
365 float h = boreAttack(c);
366 float ring = c.hit * exp(-boreSq((height - 1.2 - h * (top - 3)) / .6));
367 float spill = exp(-(top - height) * .18);
368 float3 wall = deep(.35 + wallId * .08, f) * (.1 + .6 * spill) + paint(.45, f) * frame * .35 + deep(.6, f) * window * .12;
369 wall += paint(.6 + seed * .35, f) * window * (lit * (.6 + pulse * 3.5) + ring * 2);
370 wall += paint(.8, f) * slab * (.4 + spill) + paint(.92, f) * ring * 1.5;
371 float rimLine = glow(top - height, .05 + foot);
372 // Sky beyond the opening.
373 float2 skyPoint = eye + rd.xy * (top + 8) / max(rd.z, .05f);
374 float cloud = fbm2(skyPoint * .35 + float2(c.t * .02, 0), 3);
375 float3 sky = paint(.9, f) * 1.3 + paint(.6 + cloud * .3, f) * cloud * .9;
376 float3 col = height > top ? sky : wall * exp(-height * c.fog * .03) + paint(.95, f) * rimLine * 1.5;
377 return col;
378}
379NATIVE_EFFECT(skywardShaft)
380
381// ---------------------------------------------------------------- 307 Veil Passage
382// Flying through a stack of translucent liquid veils. Each veil has an organic hole and the
383// camera threads them one after another along a weaving path. Bass ripples the veils and
384// bends the holes, low-mids light their folds, high-mids widen the rim halos, highs shimmer
385// the surfaces, and attacks flare the hole rims in a wave running away from the viewer.
386float2 boreVeilPath(float id) { return float2(.42 * sin(id * .9) + .2 * sin(id * .37 + 1), .32 * cos(id * .7 + .5)); }
387
388float3 veilPassage(Ctx c, constant NativeFrame &f) {
389 const int count = 7;
390 float advance = c.travel * .11;
391 float2 camera = boreVeilPath(float(count) - advance * float(count));
392 float3 col = deep(.6, f) * .2 + paint(.9, f) * .3 * exp(-length(c.uv) * 1.5);
393 float h = boreAttack(c);
394 for (int k = 0; k < count; ++k) {
395 float2 s = layerSlot(k, count, advance);
396 float sc = layerScale(s.x, 3.2) / c.size, fade = layerFade(s.x);
397 float2 p = c.uv * sc - (boreVeilPath(s.y) - camera);
398 float a = atan2(p.y, p.x), r = length(p);
399 float radius = 1.05 + .16 * sin(3 * a + s.y * 1.7 + c.t * .25) + .09 * sin(5 * a - s.y + c.t * .4)
400 + .1 * c.b.x * sin(7 * a + c.t * 2 + s.y);
401 float d = r - radius, aa = c.px * sc * 1.5;
402 float sheet = smoothstep(-aa, aa, d);
403 float ripple = c.b.x * sin(d * 9 - c.t * 2.5) * exp(-max(d, 0.f) * 1.2);
404 float thick = s.y * .17 + .12 * sin(p.x * .9 + s.y + sin(p.y * 1.1 + c.t * .3)) + .1 * sin(r * 2 - c.t * .6) + ripple * .2;
405 float sheen = pow(.5f + .5f * sin(a * 2 + r * 1.5 + s.y * 2.3 + c.t * .2), 3.f);
406 float2 glint = p * 6 + s.y * 3.1; float glintSeed = hash21(floor(glint) + s.y * 17);
407 float shimmer = step(.72f, glintSeed) * exp(-boreSq(length(fract(glint) - .5) * 5)) * c.b.w * 4 * boreTwinkle(glintSeed, c.t);
408 float lightness = (.1 + .45 * exp(-max(d, 0.f) * 2)) * (.45 + .55 * s.x);
409 float3 veil = filmColor(thick, f) * lightness * (1 + ripple * .7 + sheen * (.3 + c.b.y * 2)) + paint(.9, f) * shimmer * lightness;
410 float flare = c.hit * exp(-boreSq((s.x - (1 - h * .85)) / .14));
411 float rim = glow(d, .03 + .04 * c.b.z) * (.8 + flare * 4) + glow(d, .2) * .15;
412 col = mix(col, veil, sheet * .6 * fade);
413 col += paint(.75 + s.y * .1, f) * rim * fade;
414 }
415 return col;
416}
417NATIVE_EFFECT(veilPassage)
418
419// ---------------------------------------------------------------- 308 Glass Pipeworks
420// Flying through a three-dimensional lattice of glass pipes seen from outside. The glass shows
421// as bright rims and luminous fluid pulses through the cores: pipes along the flight carry the
422// bass, cross pipes the low-mids and the risers the high-mids; highs fizz bubbles and attacks
423// push bright slugs of fluid through the network.
424float boreLattice(float x) { return x - 4 * floor(x * .25 + .5); }
425
426float3 glassPipeworks(Ctx c, constant NativeFrame &f) {
427 float camZ = c.travel * 1.9;
428 float3 eye = float3(2 + .45 * sin(camZ * .12), 2 + .35 * sin(camZ * .15 + 1.2), camZ);
429 float yaw = .35 * sin(c.t * .05), pitch = .22 * sin(c.t * .043 + 1), roll = .3 * sin(c.t * .031);
430 float3 rd = cameraRay(c.uv, float3(sin(yaw), sin(pitch), 1), float3(sin(roll), cos(roll), 0), 1.15);
431 float h = boreAttack(c), slugShift = (1 - h) * 12;
432 float3 fluid = 0;
433 float glassLight = 0, transmit = 1, t = .1;
434 // Sphere-trace between pipes; at each pipe, shade it analytically and pass straight through.
435 for (int i = 0; i < 34; ++i) {
436 float3 p = eye + rd * t;
437 float2 cellZ = floor(p.xy * .25 + .5), cellX = floor(p.yz * .25 + .5), cellY = floor((p.xz - float2(0, 2)) * .25 + .5);
438 // Low-discrepancy pipe seeds (plastic-number sequence): cheap and evenly spread.
439 float3 seeds = fract(float3(dot(cellZ, float2(.7549, .5698)) + .13, dot(cellX, float2(.5698, .7549)) + .41, dot(cellY, float2(.6180, .3820)) + .77));
440 float2 offset = .7 * (float2(seeds.x, fract(seeds.x * 7.13)) - .5);
441 float3 lattice = float3(boreLattice(p.x), boreLattice(p.y), boreLattice(p.z));
442 float lz2 = boreLattice(p.z - 2);
443 float2 qz = lattice.xy - offset, qx = lattice.yz, qy = float2(lattice.x, lz2);
444 float3 radius = (.2 + .14 * seeds) * step(.38f, seeds);
445 float3 gap = select(float3(9), float3(length(qz), length(qx), length(qy)) - radius, radius > 0);
446 float d = min(gap.x, min(gap.y, gap.z));
447 // Pipes in neighbouring cells sit at least this far beyond the cell walls.
448 float3 wall = 2 - float3(max(abs(lattice.x), abs(lattice.y)), max(abs(lattice.y), abs(lattice.z)), max(abs(lattice.x), abs(lz2)));
449 float reach = min(d, min(wall.x, min(wall.y, wall.z)) + 1.25);
450 if (d < .1) {
451 int k = gap.x <= d ? 0 : (gap.y <= d ? 1 : 2);
452 float2 q = k == 0 ? qz : (k == 1 ? qx : qy);
453 float2 v = k == 0 ? rd.xy : (k == 1 ? rd.yz : rd.xz);
454 float vv = max(dot(v, v), 1e-4f), r = radius[k];
455 float mid = -dot(q, v) / vv, miss = max(dot(q, q) - mid * mid * vv, 0.f);
456 // A near miss skips past the pipe's closest approach instead of crawling along it.
457 if (miss >= r * r || mid < 0) { t += min(max(mid, d), min(.8f, max(reach, .05f))) + .02; continue; }
458 float exitAt = mid + sqrt(max(r * r - miss, 0.f) / vv);
459 float coreR = r * .62, chord = 2 * sqrt(max(coreR * coreR - miss, 0.f) / vv);
460 float3 pm = p + rd * max(mid, 0.f);
461 float along = k == 0 ? pm.z : (k == 1 ? pm.x : pm.y), flow = k == 0 || seeds[k] > .6 ? 1 : -1;
462 float wave = .5 + .5 * sin(along * 1.3 * flow - c.t * 2.2 + seeds[k] * 20);
463 float pulse = wave * wave; pulse *= pulse * wave;
464 float fizz = .5 + .5 * sin(along * 9 + seeds[k] * 40 - c.t * 5);
465 fizz *= fizz; fizz *= fizz; fizz *= fizz;
466 float level = .25 + pulse * (.4 + c.b[k] * 3.5) + fizz * fizz * c.b.w * 3;
467 if (c.hit > .01) {
468 float x = along * flow - seeds[k] * 20 - slugShift;
469 x -= 20 * floor(x / 20 + .5);
470 level += c.hit * exp(-x * x * 2) * 6;
471 }
472 float fade = exp(-t * (.07 + c.fog * .05)) * transmit;
473 float lit = min(chord, 1.2f) * 2.2 + exp(-miss / (coreR * coreR)) * .35;
474 fluid[k] += lit * level * fade;
475 float edge = sqrt(miss) / r;
476 edge *= edge; edge *= edge;
477 glassLight += (edge * edge * 1.1 + .07) * fade;
478 transmit *= .8;
479 t += max(exitAt, 0.f) + .03;
480 if (transmit < .25) break;
481 } else {
482 t += max(reach, .02f) * .9;
483 }
484 if (t > 30) break;
485 }
486 float3 back = deep(.4 + .1 * rd.y, f) * .14 + paint(.55, f) * .05 * (1 + rd.y);
487 return back + paint(.3, f) * fluid.x * 1.2 + paint(.52, f) * fluid.y + paint(.72, f) * fluid.z + paint(.9, f) * glassLight * .85;
488}
489NATIVE_EFFECT(glassPipeworks)
490
491// ---------------------------------------------------------------- 309 Möbius Ride
492// Riding just above a wide ribbon of light that winds ahead and twists half a turn every
493// stretch. The camera banks with the ribbon, so the sky rolls while the ribbon stays under
494// you and flips over in the distance. Edge lights pulse with their bands, highs twinkle the
495// sky and attacks fire chevrons racing away along the ribbon.
496float2 boreRibbonPath(float z) { return float2(2.6 * sin(z * .12) + .9 * sin(z * .29 + 1), 1.4 * sin(z * .15 + 2) + .5 * sin(z * .33)); }
497float boreRibbonTwist(float z) { return z * PI / 9 + .4 * sin(z * .2); }
498float boreRibbonField(float3 p, thread float2 &local) {
499 local = rot(p.xy - boreRibbonPath(p.z), -boreRibbonTwist(p.z));
500 return sdBox2(local, float2(.9, .02)) - .01;
501}
502
503float3 mobiusRide(Ctx c, constant NativeFrame &f) {
504 float camZ = c.travel * 1.6;
505 float tw = boreRibbonTwist(camZ);
506 float2 normal = float2(-sin(tw), cos(tw));
507 float3 eye = float3(boreRibbonPath(camZ) + normal * .95, camZ);
508 float3 target = float3(boreRibbonPath(camZ + 5), camZ + 5);
509 float3 rd = cameraRay(c.uv, target - eye, float3(normal, 0), 1.1);
510 float t = .05, d = 1, nearest = 9;
511 float2 local = 0;
512 for (int i = 0; i < 64; ++i) {
513 d = boreRibbonField(eye + rd * t, local);
514 nearest = min(nearest, d / (1 + t * .08));
515 if (d < .0015 + t * .0015 || t > 45) break;
516 t += d * .6;
517 }
518 // Sky: a soft nebula, stars that twinkle with the highs and the ribbon's own aura.
519 float az = atan2(rd.x, rd.z), el = asin(clamp(rd.y, -1.f, 1.f));
520 float n = fbm2(float2(az * 2.5, el * 2.5) + c.t * .01, 4);
521 float2 starCell = floor(float2(az, el) * 60);
522 float starSeed = hash21(starCell);
523 float star = step(.9f, starSeed) * exp(-boreSq(length(fract(float2(az, el) * 60) - .5) * 5)) * (.4 + c.b.w * 4 * boreTwinkle(starSeed, c.t));
524 float3 sky = paint(.15 + n * .5, f) * (.08 + .7 * n * n) + paint(.85, f) * star;
525 sky += paint(.7, f) * glow(nearest, .12) * (.35 + c.b.x * .8);
526 if (t > 45 || d > .05) return sky;
527 float3 p = eye + rd * t;
528 float zw = p.z, xs = local.x;
529 bool under = local.y < 0;
530 float foot = t * c.px / 1.1 + .005;
531 // Chevron rungs roll with the bass; three lanes carry the low-mids.
532 float rung = zw * .9 - abs(xs) * .45 - c.t * .5, rungId = floor(rung + .5);
533 float rungs = (1 - smoothstep(.05f, .08f + foot, abs(rung - rungId))) * (.25 + c.b.x * 2.2 * boreSq(.5 + .5 * sin(rungId * .7 - c.t * 2)));
534 float lanes = glow(min(abs(xs), abs(abs(xs) - .45)), .02 + foot) * (.25 + c.b.y * 2);
535 // Lamps along both edges, each lamp on its own band.
536 float lampRun = zw / .8 - c.t * .35, seg = floor(lampRun + .5), sideId = xs > 0 ? 2 : 0;
537 float rail = glow(.9 - abs(xs), .07 + foot);
538 float lamp = rail * pow(.5f + .5f * cos(fract(lampRun) * TAU), 4.f);
539 float band = boreBand(c, seg + sideId);
540 float h = boreAttack(c);
541 float chev = zw - abs(xs) * .7 - camZ - 3 - (1 - h) * 24;
542 float chevrons = c.hit * pow(.5f + .5f * cos(chev * TAU / 1.2), 8.f) * exp(-boreSq(chev / 2.4));
543 float hue = (under ? .35 : .62) + xs * .1 + zw * .012;
544 float sheen = .5 + .5 * cos(xs * 2.2 + zw * .3);
545 float3 surface = mix(deep(hue + .3, f), paint(hue, f), .3 + .4 * sheen) * (under ? .14 : .22);
546 surface += paint(hue + .2, f) * (lanes + rungs) + paint(.88, f) * (rail * .3 + lamp * (.8 + band * 4)) + paint(.95, f) * chevrons * 3;
547 return mix(sky, surface, exp(-t * (.03 + c.fog * .03)));
548}
549NATIVE_EFFECT(mobiusRide)
550
551// Coaster versions: the same designs riding the roller-coaster course.
552NATIVE_COASTER(ringworldDrift, 301, false)
553NATIVE_COASTER(twinBore, 302, false)
554NATIVE_COASTER(coilRun, 303, false)
555NATIVE_COASTER(bellowsPassage, 304, false)
556NATIVE_COASTER(branchingBore, 305, false)
557NATIVE_COASTER(skywardShaft, 306, false)
558NATIVE_COASTER(veilPassage, 307, false)