Branchwell

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

1// Original PulseCanvas designs 310–319: portal tunnels. Each is a perspective tube or portal
2// stack whose walls carry one strong surface material (chainmail, feathers, tartan,
3// herringbone, quilting, lace, zippers, cable knit, basketry, louvres). Cross-sections and
4// cameras vary: a billowing bent bore, a pointed vault, a twisting rounded square, a
5// rectangular hall, an off-axis pipe, a lace stack, a helical seam, a padded bent tube,
6// an elliptic burrow seen from low down and an octagon. Original procedural code; every
7// audio term is zero at zero Chaos.
8
9// ---------------------------------------------------------------- shared portal helpers
10// Smooth wandering offset for a vanishing point.
11float2 ptlDrift(float t, float2 amount) {
12 return float2(sin(t * .21 + .6) + .5 * sin(t * .34 + 2.3), cos(t * .17 + 1.1) + .5 * sin(t * .29 + .2)) / 1.5 * amount;
13}
14// Bent view: far depths converge on the drifting vanishing point while the near walls stay
15// put. One-to-one for |vanish| < .9.
16float2 ptlBend(float2 uv, float2 vanish) { return uv - vanish * exp(-dot(uv, uv) * 1.6); }
17float ptlFog(float depth, Ctx c, float base) { return exp(-depth * (base + c.fog * .12)); }
18float ptlAttack(Ctx c) { return smoothstep(.03f, .65f, c.hit); }
19// Attack front: a band of depth that starts near the camera and runs away as the attack
20// decays, so every hit sends a local event down the tunnel.
21float ptlFront(Ctx c, float depth, float near, float far, float width) {
22 float x = (depth - near - (1 - ptlAttack(c)) * far) / width;
23 return c.hit * exp(-x * x);
24}
25float ptlWrap(float x, float period) { return x - period * floor(x / period); }
26float ptlBand(Ctx c, float id) { return c.b[min(3, int(ptlWrap(id, 4.f)))]; }
27
28// ---------------------------------------------------------------- 310 Chainmail Bore
29// A bent bore lined with interlocking 4-in-1 chainmail. Two staggered ring families tilt
30// opposite ways, so every crossing alternates over and under. Bass billows the mesh in
31// rolling folds and lights their crests, low-mids roll light behind the mesh, high-mids
32// tint the staggered ring family, highs spin bright glints around the rings and attacks run
33// a bright ripple through whole linked rings.
34struct PtlRing { float cover; float height; float across; float2 dir; float2 centre; float family; };
35
36PtlRing ptlNoRing() {
37 PtlRing ring;
38 ring.cover = 0; ring.height = -9; ring.across = 0; ring.dir = float2(0, 1); ring.centre = 0; ring.family = 0;
39 return ring;
40}
41PtlRing ptlMailRing(float2 p, float2 centre, float family, float aa) {
42 const float radius = .4, wire = .088;
43 PtlRing ring;
44 float2 l = p - centre;
45 float dist = length(l);
46 ring.dir = l / max(dist, 1e-4f);
47 ring.across = clamp((dist - radius) / wire, -1.f, 1.f);
48 ring.cover = 1 - smoothstep(wire - aa, wire + aa, abs(dist - radius));
49 // Opposite tilts for the two families make every crossing alternate over and under.
50 ring.height = (family > .5 ? -l.y : l.y) / radius;
51 ring.centre = centre;
52 ring.family = family;
53 return ring;
54}
55float3 ptlMailShade(PtlRing ring, Ctx c, constant NativeFrame &f, float unit, float travel, float crest) {
56 float seed = hash21(float2(ptlWrap(ring.centre.x, 20.f), ring.centre.y) + ring.family * 41);
57 float s = ring.across;
58 float3 n = normalize(float3(ring.dir * s, sqrt(max(0.f, 1 - s * s)) + .2));
59 // Each ring has its own light that circles it: the glint spins around the wire.
60 float spin = seed * TAU + c.t * (.5 + seed) * (seed > .5 ? 1 : -1);
61 float3 light = normalize(float3(cos(spin), sin(spin), 1.1));
62 float diffuse = max(0.f, dot(n, light));
63 float spec = pow(max(0.f, dot(n, normalize(light + float3(0, 0, 1)))), 28.f);
64 float ripple = ptlFront(c, ring.centre.y * unit - travel, .5, 6, .45);
65 float twinkle = step(.5f, seed) * pow(.5 + .5 * sin(c.t * 4.3 + seed * 60), 6.f);
66 float3 base = paint(.04 + seed * .24 + ring.family * (.3 + c.b.z * .2), f);
67 float3 color = base * (.04 + .5 * diffuse) * (1 + ring.family * c.b.z * 2.6 + crest * 2.6);
68 color += paint(.95, f) * spec * (.5 + c.b.w * 6 * (.25 + twinkle));
69 color += paint(.85 + seed * .1, f) * ripple * (1 + 2.5 * spec + diffuse);
70 return color;
71}
72
73float3 chainmailBore(Ctx c, constant NativeFrame &f) {
74 const float N = 20, unit = TAU / N;
75 float2 vanish = ptlDrift(c.t * .6, float2(.3, .2));
76 float2 q = ptlBend(c.uv / c.size, vanish);
77 float r = max(length(q), 1e-3f), a = atan2(q.y, q.x);
78 float travel = c.travel * 2.2, d0 = 1 / r;
79 // The mesh hangs in soft folds that roll toward the camera; bass deepens them.
80 float fold = .6 * sin(a * 3 + (d0 + travel) * .8 - c.t * .7) + .4 * sin(a * 2 - (d0 + travel) * .5 + c.t * .45);
81 float depth = d0 * (1 + (.05 + .22 * c.b.x) * fold);
82 float zw = depth + travel;
83 float2 p = float2(a, zw) / unit;
84 float fw = c.px / c.size * max(1.f, depth) / r / unit;
85 float aa = clamp(fw * .75f, .004f, .3f);
86 PtlRing best = ptlNoRing(), under = ptlNoRing();
87 float2 baseA = floor(p), baseB = floor(p - .5) + .5;
88 for (int k = 0; k < 8; ++k) {
89 float2 o = float2(float(k & 1), float((k >> 1) & 1));
90 PtlRing ring = ptlMailRing(p, (k < 4 ? baseA : baseB) + o, k < 4 ? 0.f : 1.f, aa);
91 if (ring.cover > .001) {
92 if (ring.height > best.height) { under = best; best = ring; }
93 else if (ring.height > under.height) under = ring;
94 }
95 }
96 float crest = max(0.f, fold) * c.b.x;
97 float behind = .5 + .5 * sin(zw * .7 - c.t * .8 + sin(a * 2 + c.t * .1) * 1.2);
98 float3 back = deep(.5, f) * .03 + paint(.45 + .15 * sin(a + zw * .05), f) * behind * behind * (.02 + .6 * c.b.y);
99 back += paint(.85, f) * ptlFront(c, depth, .5, 6, .6) * .4;
100 float3 color = back;
101 if (under.cover > 0) color = mix(color, ptlMailShade(under, c, f, unit, travel, crest) * .45, under.cover);
102 if (best.cover > 0) color = mix(color, ptlMailShade(best, c, f, unit, travel, crest), best.cover);
103 float3 average = back * .6 + paint(.2, f) * (.09 + crest * .2);
104 color = mix(average, color, smoothstep(.24f, .07f, fw));
105 float3 core = paint(.55, f) * .12 + paint(.9, f) * exp(-r * 7) * .8;
106 return mix(core, color, ptlFog(depth, c, .045));
107}
108NATIVE_EFFECT(chainmailBore)
109
110// ---------------------------------------------------------------- 311 Feather Vault
111// A pointed vault lined with long overlapping iridescent feathers whose tips flow toward the
112// viewer. Farther rows lie on top, like scales, and the feathers lean so the rows spiral.
113// Bass lifts waves of feathers (they reach out, catch light and throw shadows), low-mids
114// light the shafts, high-mids the vane rims, highs shimmer the barbs and attacks ruffle a
115// wave of feathers down the vault.
116float ptlVaultRadius(float a) {
117 float s = sin(a), co = cos(a);
118 // Pointed arch above (two circular arcs meeting at the apex), a flatter oval below.
119 if (s > 0) return sqrt(1.55f * 1.55f - .3025f * s * s) - .55f * abs(co);
120 return rsqrt(co * co + s * s / .8f);
121}
122struct PtlFeather { float alpha; float shadow; float3 color; };
123
124PtlFeather ptlFeather(float2 p, float row, float column, Ctx c, constant NativeFrame &f, float aa, float resolve, float rowDepth, float N) {
125 PtlFeather fe;
126 float seed = hash21(float2(ptlWrap(column, N), row));
127 float sway = .5 + .5 * sin(row * .7 + column * 1.3 - c.t * 1.2);
128 float ruffle = ptlFront(c, rowDepth, .45, 6, .6);
129 float lift = c.b.x * (.2 + .8 * sway * sway) * 1.5 + ruffle * 1.4;
130 float y = p.y - row + lift * .2;
131 float t = (y + 1.6) / 2.1; // 0 at the tip, 1 at the base
132 // Feathers lean and their tips curl, so the rows read as a flowing helix.
133 float x = p.x - column + t * .95 - .12 * (1 - t) * (1 - t) - .04 * sin(c.t * .7 + seed * 6);
134 float halfWidth = .5 * pow(saturate(t * 2.2), .6f) * (1 + lift * .08);
135 float edge = abs(x) - halfWidth;
136 float body = smoothstep(-.01f, .03f, t) * (1 - smoothstep(.9f, 1.f, t));
137 // Barbs sweep from the shaft back toward the base; some vanes split apart.
138 float barbCoord = y + abs(x) * 1.6;
139 float split = smoothstep(.04f, .015f, abs(fract(barbCoord * .8 + seed * 3) - .5)) * step(.08f, abs(x)) * step(.35f, seed);
140 fe.alpha = (1 - smoothstep(-aa, aa, edge)) * body * (1 - split * resolve * .9);
141 float st = (y + 1.8 + .2 * lift) / 2.1;
142 fe.shadow = (1 - smoothstep(-.1f, .12f, abs(x) - .55f * pow(saturate(st * 2.2), .6f))) * smoothstep(0.f, .1f, st) * (.45 + .55 * saturate(lift));
143 float across = saturate(abs(x) / max(halfWidth, 1e-3f));
144 float barbs = pow(.5 + .5 * cos(barbCoord * 14 * TAU), 2.f);
145 float shimmer = c.b.w * sin(barbCoord * 7 + c.t * 3 + seed * 9);
146 float3 film = filmColor(seed * .5 + across * .45 + t * .4 + shimmer * .3 + c.t * .02, f);
147 float tuck = mix(1.f, .12f, smoothstep(.22f, .55f, t));
148 float barbLight = mix(.55f, .3f + .8f * barbs * (1 + 2.5 * c.b.w), resolve);
149 float3 color = film * (.07 + .3 * (1 - across * .6)) * barbLight * tuck;
150 color += film * lift * .45 * pow(1 - across * .7, 2.f) * tuck;
151 float shaft = line(x, .028 * (1.3 - t * .6), aa) * smoothstep(.02f, .08f, t);
152 color += paint(.9, f) * shaft * (.12 + 2.6 * c.b.y) * tuck;
153 float rim = smoothstep(-.1f, -.01f, edge);
154 color += paint(.72 + seed * .12, f) * rim * (.06 + 2.2 * c.b.z) * tuck;
155 color += paint(.92, f) * ruffle * (.4 + barbs) * tuck;
156 fe.color = color;
157 return fe;
158}
159
160float3 featherVault(Ctx c, constant NativeFrame &f) {
161 const float N = 22, rowLength = .34;
162 float2 vanish = ptlDrift(c.t * .5, float2(.16, .1)) + float2(0, .14);
163 float2 q = ptlBend(c.uv / c.size, vanish);
164 float r = max(length(q), 1e-3f), a = atan2(q.y, q.x);
165 float depth = ptlVaultRadius(a) / r;
166 float travel = c.travel * 2.1, zw = depth + travel;
167 float2 p = float2(a * N / TAU, zw / rowLength);
168 float fw = c.px / c.size / r * max(N / TAU, depth / rowLength);
169 float aa = clamp(fw, .004f, .3f);
170 float resolve = smoothstep(.1f, .035f, fw);
171 float3 color = 0;
172 float transmit = 1, shade = 1;
173 float baseRow = floor(p.y);
174 for (int k = 2; k >= 0; --k) {
175 float row = baseRow + float(k);
176 float offset = ptlWrap(row, 2.f) * .5;
177 float lean = saturate((p.y - row + 1.6) / 2.1) * .95;
178 float column = floor(p.x + lean - offset + .5) + offset;
179 float other = column + (p.x + lean > column ? 1 : -1);
180 float rowDepth = row * rowLength - travel;
181 for (int j = 0; j < 2; ++j) {
182 PtlFeather fe = ptlFeather(p, row, j == 0 ? column : other, c, f, aa, resolve, rowDepth, N);
183 color += transmit * shade * fe.alpha * fe.color;
184 transmit *= 1 - fe.alpha;
185 shade *= 1 - .65 * fe.shadow;
186 }
187 }
188 color += transmit * shade * deep(.35, f) * .05;
189 // The apex rib of the pointed vault.
190 float rib = exp(-abs(a - PI * .5) * r * 60) * step(0.f, q.y);
191 color += paint(.8, f) * rib * (.12 + .5 * c.b.z);
192 float3 core = paint(.65, f) * .25 + paint(.92, f) * exp(-r * 6) * 1.1;
193 return mix(core, color, ptlFog(depth, c, .05));
194}
195NATIVE_EFFECT(featherVault)
196
197// ---------------------------------------------------------------- 312 Tartan Tube
198// A twisting rounded-square tube wrapped in tartan: a symmetric sett of crossing thread
199// stripes woven as a 2/2 twill, so the diagonal weave shows in every check. Each of the four
200// thread colours belongs to one band and brightens with it; attacks send a bright stripe
201// along the weft, lighting only the weft floats so the twill flashes.
202float ptlSett(float s) {
203 float x = abs(fract(s) - .5) * 2;
204 return x < .07 ? 3 : x < .36 ? 0 : x < .44 ? 2 : x < .64 ? 1 : x < .7 ? 3 : x < .84 ? 2 : 0;
205}
206float3 ptlThread(float slot, Ctx c, constant NativeFrame &f) {
207 float3 hue = slot < .5 ? paint(.97, f) : slot < 1.5 ? paint(.04, f) : slot < 2.5 ? paint(.74, f) : paint(.5, f);
208 float gain = slot < .5 ? .14 : slot < 2.5 ? .22 : .4;
209 return hue * (gain + 1.4 * c.b[min(3, int(slot))]);
210}
211
212float3 tartanTube(Ctx c, constant NativeFrame &f) {
213 const float T = 36, setts = 10, settLength = .75;
214 float2 vanish = ptlDrift(c.t * .5, float2(.22, .15));
215 float2 q = ptlBend(c.uv / c.size, vanish);
216 float r = max(length(q), 1e-3f);
217 float travel = c.travel * 2.1;
218 // The section twists along the tube, so flying forward slowly rolls the view.
219 float twist = (1 / r + travel) * .07 + c.t * .04;
220 float a = atan2(q.y, q.x) - twist;
221 float ca = cos(a) * cos(a), sa = sin(a) * sin(a);
222 float depth = rsqrt(sqrt(ca * ca + sa * sa)) / r;
223 float zw = depth + travel;
224 float2 yarnPos = float2(a / TAU * setts, zw / settLength) * T;
225 float2 cell = floor(yarnPos), local = fract(yarnPos);
226 float warpSlot = ptlSett((cell.x + .5) / T), weftSlot = ptlSett((cell.y + .5) / T);
227 float twill = ptlWrap(cell.x + cell.y, 4.f);
228 bool warpUp = twill < 2;
229 float3 warpColor = ptlThread(warpSlot, c, f), weftColor = ptlThread(weftSlot, c, f);
230 // Each float is a rounded thread that dips at the ends where it passes under.
231 float along = warpUp ? (twill + local.y) * .5 : (twill - 2 + local.x) * .5;
232 float dip = .65 + .35 * sin(PI * along);
233 float3 woven = (warpUp ? warpColor * (.35 + .65 * sin(PI * local.x)) : weftColor * (.35 + .65 * sin(PI * local.y))) * dip;
234 float fw = c.px / c.size / r * max(T * setts / TAU, depth * T / settLength);
235 float3 cloth = mix((warpColor + weftColor) * .33, woven, smoothstep(.55f, .2f, fw));
236 // Soft drapes in the cloth.
237 float drape = .7 + .3 * sin(a * 8 + zw * .6 + sin(zw * .3 + c.t * .2) * 2);
238 cloth *= drape;
239 float stripe = ptlFront(c, depth, .4, 5, .35);
240 float head = pow(.5 + .5 * cos(a - (1 - ptlAttack(c)) * 9), 6.f);
241 cloth += paint(.9, f) * stripe * (warpUp ? .25 : 1.3) * (1 + 2 * head);
242 float3 core = deep(.25, f) * .12 + paint(.9, f) * exp(-r * 9) * .6;
243 return mix(core, cloth, ptlFog(depth, c, .05));
244}
245NATIVE_EFFECT(tartanTube)
246
247// ---------------------------------------------------------------- 313 Herringbone Hall
248// A rectangular hall paved on every surface with 2:1 herringbone tiles and glowing grout;
249// the pattern folds continuously around the corners. Bass tilts and lights tiles in chevron
250// waves that roll down the hall, low-mids glow the vertical tiles, high-mids sheen the
251// horizontal ones, highs light the grout and attacks flash a whole herringbone row.
252struct PtlBrick { float2 local; float2 id; float2 centre; float vertical; float edge; };
253
254// Herringbone of 2x1 bricks in brick units: a staircase of horizontal/vertical pairs
255// repeating along (1,1) and (0,4).
256PtlBrick ptlHerringbone(float2 p) {
257 PtlBrick b;
258 float m = floor(p.x), lx = p.x - m;
259 float yp = p.y - m + 3, n = floor(yp / 4), yl = yp - 4 * n;
260 if (yl < 2) {
261 b.local = float2(yl, lx); b.id = float2(m - 2, n); b.vertical = 1;
262 b.centre = float2(m + .5, m - 2 + 4 * n);
263 } else if (yl < 3) {
264 b.local = float2(lx + 1, yl - 2); b.id = float2(m - 1, n); b.vertical = 0;
265 b.centre = float2(m, m - .5 + 4 * n);
266 } else {
267 b.local = float2(lx, yl - 3); b.id = float2(m, n); b.vertical = 0;
268 b.centre = float2(m + 1, m + .5 + 4 * n);
269 }
270 b.edge = min(min(b.local.x, 2 - b.local.x), min(b.local.y, 1 - b.local.y));
271 return b;
272}
273
274float3 herringboneHall(Ctx c, constant NativeFrame &f) {
275 const float W = 1.3, H = .82, unit = .15;
276 float2 vanish = ptlDrift(c.t * .5, float2(.16, .1));
277 float2 q = ptlBend(c.uv / c.size, vanish);
278 float ax = abs(q.x) / W, ay = abs(q.y) / H;
279 bool wall = ax > ay;
280 float depth = 1 / max(max(ax, ay), 1e-3f);
281 float travel = c.travel * 2.4, zw = depth + travel;
282 float2 hit = q * depth;
283 // Perimeter coordinate, continuous around the corners; mid = centre of this surface.
284 float across, mid;
285 if (wall) {
286 if (q.x > 0) { across = 2 * W + H + hit.y; mid = 2 * W + H; }
287 else { across = 4 * W + 3 * H - hit.y; mid = 4 * W + 3 * H; }
288 } else {
289 if (q.y < 0) { across = W + hit.x; mid = W; }
290 else { across = 3 * W + 2 * H - hit.x; mid = 3 * W + 2 * H; }
291 }
292 float2 p = rot(float2(across, zw), PI * .25) / unit;
293 PtlBrick b = ptlHerringbone(p);
294 float2 cs = rot(b.centre * unit, -PI * .25);
295 float rel = cs.y - travel;
296 float seed = hash21(b.id + b.vertical * 19);
297 float fw = c.px / c.size * depth * max(1.f, depth / (wall ? W : H)) / unit;
298 float aa = clamp(fw, .01f, .5f);
299 // Chevron waves roll down the hall; bass tilts and lights the tiles they cross.
300 float wave = pow(.5 + .5 * sin(rel * 1.6 - abs(cs.x - mid) * 1.4 - c.t * 1.2), 4.f);
301 float lit = wave * (.12 + c.b.x); // a small baseline keeps the corridor moving at zero zoom
302 float tilt = (b.local.x - 1) * (.22 + 1.2 * lit) * (seed > .5 ? 1 : -1);
303 float3 tile = paint(.02 + seed * .2 + b.vertical * .3, f) * (.05 + .08 * seed);
304 float twinkle = .5 + .5 * sin(seed * 23 + c.t * .8);
305 tile *= (1 + (b.vertical > .5 ? c.b.y : c.b.z) * 2.6 * twinkle) * (.85 + tilt);
306 float bevel = smoothstep(.03f, .16f, b.edge);
307 tile *= .55 + .45 * bevel;
308 tile += paint(.7 + seed * .12, f) * lit * 1.3 * bevel;
309 tile += paint(.92, f) * ptlFront(c, rel, .5, 6, .55) * 1.6;
310 float grout = line(b.edge, .045, aa);
311 float3 groutColor = paint(.72, f) * (.22 + 2.4 * c.b.w);
312 float3 color = mix(tile, groutColor, grout) + groutColor * glow(b.edge, .1) * .1;
313 float3 average = paint(.2, f) * .1 + groutColor * .1;
314 color = mix(average, color, smoothstep(.35f, .1f, fw));
315 // Dark creases where the surfaces meet.
316 color *= 1 - .7 * exp(-abs(ax - ay) * depth * 26);
317 float3 core = paint(.8, f) * .3 + paint(.95, f) * exp(-max(ax, ay) * 12) * 1.1;
318 return mix(core, color, ptlFog(depth, c, .045));
319}
320NATIVE_EFFECT(herringboneHall)
321
322
323// ---------------------------------------------------------------- 315 Lace Funnel
324// A funnel of layered lace rings drifting toward the viewer against a hot core of light.
325// The lace is laid out on conformal log-polar tiles, so every motif keeps its shape at
326// every radius: rows of rimmed eyelets in cloth stitch, five-petal flowers over net and
327// spotted net, with a picot inner edge and a scalloped border. Alternate layers turn against
328// each other. Each layer's eyelets glow with its own band, highs light the cords and attacks
329// bloom light through the openwork of one layer as it passes.
330float4 ptlLace(float2 g, float yIn, float yOut, float aa) {
331 float y = g.y - yIn, row = floor(y);
332 float shift = ptlWrap(row, 2.f) * .5;
333 float2 l = float2(fract(g.x + shift) - .5, y - row - .5);
334 float type = ptlWrap(row, 3.f);
335 float e = length(l);
336 float cord = 0, cloth = 0, net = 0, hole = 0;
337 if (type < .5) {
338 // Eyelet band: cloth stitch pierced by a rimmed eyelet, small holes at the corners.
339 float corner = length(abs(l) - .5);
340 hole = max(fill(e - .13, aa), fill(corner - .08, aa));
341 cord = max(line(e - .17, .045, aa), line(corner - .11, .03, aa));
342 cloth = 1;
343 } else if (type < 1.5) {
344 // Five-petal flower in cloth stitch over net, with a pierced centre.
345 float ang = atan2(l.y, l.x);
346 float petal = e - (.14 + .28 * pow(abs(cos(ang * 2.5)), .7f));
347 cloth = fill(petal, aa);
348 cord = max(line(petal, .03, aa), line(e - .075, .025, aa));
349 hole = fill(e - .055, aa);
350 net = 1;
351 } else {
352 // Spotted net with a ring motif.
353 cloth = fill(e - .08, aa);
354 cord = line(e - .24, .03, aa);
355 net = 1;
356 }
357 float4 h = hexCell(g * 5);
358 float hexEdge = .5 - max(abs(h.x), max(abs(.5 * h.x + .866 * h.y), abs(.5 * h.x - .866 * h.y)));
359 float netThread = mix(.3f, line(hexEdge, .09, aa * 5), smoothstep(.12f, .05f, aa));
360 float clothTex = mix(.6f, .5f + .5f * sin((l.x + l.y) * 50) * sin((l.x - l.y) * 50), smoothstep(.05f, .02f, aa));
361 // Picot inner edge and scalloped outer border.
362 float sx = fract(g.x) - .5;
363 float border = yOut - .35 + .35 * sqrt(max(0.f, 1 - 4 * sx * sx));
364 float inside = fill(g.y - border, aa) * fill(yIn - g.y, aa);
365 float edges = line(g.y - border, .05, aa) + line(g.y - yIn, .05, aa);
366 edges += line(length(float2(sx, g.y - yIn + .13)) - .1, .028, aa) * step(g.y, yIn);
367 float lace = max(cloth * .85, net * netThread * .7);
368 float opacity = max(inside * max(lace, cord), edges) * (1 - hole * inside);
369 float glowLight = inside * ((1 - hole) * (cloth * .32 * clothTex + net * (1 - cloth) * netThread * .55));
370 return float4(saturate(opacity), glowLight, inside * hole, saturate(inside * cord + edges));
371}
372
373float3 laceFunnel(Ctx c, constant NativeFrame &f) {
374 const float M = 16;
375 float2 uv = c.uv / c.size;
376 float ru = length(uv), au = atan2(uv.y, uv.x);
377 float rays = .6 + .4 * sin(au * 7 + c.t * .2) * sin(au * 4 - c.t * .13);
378 float3 color = deep(.3, f) * .1 + paint(.4, f) * glow(ru, .9) * .25 * rays + paint(.9, f) * glow(ru, .2) * 1.4;
379 const float yIn = -2.4, yOut = 2.6;
380 const int layers = 6;
381 for (int k = 0; k < layers; ++k) {
382 float2 s = layerSlot(k, layers, c.travel * .14);
383 float id = s.y;
384 float sc = layerScale(s.x, 3.6);
385 float turn = ptlWrap(id, 2.f) < 1 ? 1 : -1;
386 float2 off = float2(sin(id * .9 + c.t * .21), cos(id * .7 + c.t * .17)) * .1 * (1 - s.x);
387 float2 p = rot(uv * sc - off, turn * c.t * .1 + id * .6);
388 float rr = max(length(p), 1e-3f);
389 float2 g = float2(atan2(p.y, p.x), log(rr)) * M / TAU;
390 float aa = c.px / c.size * sc / rr * M / TAU;
391 float4 lace = ptlLace(g, yIn, yOut, aa);
392 float fade = layerFade(s.x);
393 float dim = .4 + .6 * s.x;
394 float band = ptlBand(c, id);
395 float bloom = ptlFront(c, 1 - s.x, .15, .65, .13);
396 float3 threadColor = paint(.55 + .15 * sin(id * 1.3), f);
397 float3 emit = threadColor * lace.y * .4 * dim + paint(.9, f) * lace.w * (.22 + 1.4 * c.b.w) * dim;
398 float3 eyelets = paint(.95, f) * lace.z * (.03 + 2.8 * band + 3 * bloom);
399 color = color * (1 - lace.x * fade * .88) + (emit + eyelets + paint(.95, f) * bloom * lace.y * 2) * fade;
400 }
401 return color;
402}
403NATIVE_EFFECT(laceFunnel)
404
405// ---------------------------------------------------------------- 316 Zipper Run
406// A tube sewn from one continuous helical zipper: the seam spirals down the walls and its
407// sliders run along it, opening lens-shaped sections that reveal hot light behind the teeth
408// and closing them again. Bass widens the openings, low-mids light the tape stitching,
409// high-mids the piping between turns, highs glint the metal teeth and attacks unzip a
410// section that runs away down the tube.
411float3 zipperRun(Ctx c, constant NativeFrame &f) {
412 const float pitch = .85, P = .13, tw = .05;
413 float2 vanish = ptlDrift(c.t * .55, float2(.24, .16));
414 float2 q = ptlBend(c.uv / c.size, vanish);
415 float r = max(length(q), 1e-3f);
416 float a = atan2(q.y, q.x) + c.t * .04;
417 float depth = 1 / r;
418 float travel = c.travel * 2.3, zw = depth + travel;
419 // One continuous helical seam: `across` runs along the tube, `along` follows the helix.
420 float turns = a / TAU;
421 float w = zw - turns * pitch;
422 float k = floor(w / pitch + .5);
423 float across = w - k * pitch;
424 float along = (turns + k) * TAU;
425 float aa = clamp(c.px / c.size / r * max(1.f, depth), .002f, .2f);
426 // Sliders run along the helix, opening lens-shaped sections and closing them again.
427 float lens = .6 * sin(along * .32 - c.t * .9) + .4 * sin(along * .11 + c.t * .35);
428 float open = max(0.f, lens - .2) * (.2 + .6 * c.b.x);
429 float unzip = ptlFront(c, depth, .4, 5, .6);
430 open += unzip * .3 * (.6 + .4 * sin(along * .5));
431 float sl = across + open * .5, sr = across - open * .5;
432 float nl = floor(along / P + .5), nr = floor(along / P) + .5;
433 float2 tl = float2(sl + .8 * tw, along - nl * P), tr = float2(sr - .8 * tw, along - nr * P);
434 float2 toothSize = float2(tw * 1.1, P * .24);
435 float toothL = sdBox2(tl, toothSize) - P * .05, toothR = sdBox2(tr, toothSize) - P * .05;
436 float teeth = fill(min(toothL, toothR), aa);
437 bool leftTooth = toothL < toothR;
438 float2 tooth = leftTooth ? tl : tr;
439 float toothId = leftTooth ? nl : nr + 5000;
440 float tape = max(fill(sl + 1.45 * tw, aa), fill(1.45 * tw - sr, aa));
441 // Hot light behind the chain.
442 float3 lightColor = paint(.04 + .08 * sin(along * .07 + c.t * .2), f) * (1.3 + 3 * unzip);
443 float3 color = lightColor;
444 // Fabric tapes: fine ribbing, colour drifting along the helix, two stitch lines each.
445 float edgeDist = abs(across) - open * .5 - 1.45 * tw;
446 float rib = .8 + .2 * sin(abs(across) * 150);
447 float fold = smoothstep(pitch * .5f, 0.f, abs(across));
448 float3 fabric = paint(.62 + .14 * sin(along * .05 + k), f) * (.05 + .17 * fold) * rib;
449 float seams = (line(edgeDist - .03, .006, aa) + line(edgeDist - .06, .006, aa)) * smoothstep(.3f, .55f, fract(along * 16));
450 fabric += paint(.35, f) * seams * (.12 + 2.6 * c.b.y);
451 color = mix(color, fabric, tape);
452 // Metal teeth: rounded crowns with glints on highs.
453 float seed = hash21(float2(toothId, k));
454 float crown = saturate(1 - abs(tooth.y) / (P * .29));
455 float3 metal = paint(.45, f) * (.1 + .7 * crown * crown) + paint(.9, f) * pow(crown, 8.f) * .4;
456 metal += paint(.95, f) * pow(seed, 5.f) * c.b.w * 5 * crown;
457 color = mix(color, metal, teeth);
458 // Light spills over the open edges.
459 color += lightColor * glow(abs(across) - open * .5, .02 + open * .3) * .35 * saturate(open * 6);
460 // Piping cord where neighbouring turns of the helix are sewn together.
461 float pipe = pitch * .5 - abs(across);
462 float cord = fill(pipe - .035, aa);
463 float3 piping = paint(.8, f) * (.06 + .25 * smoothstep(0.f, .035f, pipe)) * (1 + 3.5 * c.b.z);
464 color = mix(color, piping, cord) + paint(.78, f) * glow(pipe, .03) * c.b.z * .5;
465 float3 core = paint(.1, f) * .3 + paint(.95, f) * exp(-r * 8) * 1.1;
466 return mix(core, color, ptlFog(depth, c, .05));
467}
468NATIVE_EFFECT(zipperRun)
469
470// ---------------------------------------------------------------- 317 Cable Knit Tube
471// A strongly bending tube of chunky cable knit. Seven padded cables run down the walls, each
472// two braided strands of plied V stitches crossing over and under, with purl ridges between
473// and a soft fibre fuzz, lit by two coloured lights. Bass twists the cables further, each
474// cable's stitches brighten with its own band and attacks pull a ripple through the knit.
475float3 cableKnitTube(Ctx c, constant NativeFrame &f) {
476 const float N = 7, rowsPerColumn = 9, sw = .14;
477 float2 vanish = ptlDrift(c.t * .75, float2(.42, .3));
478 float2 q = ptlBend(c.uv / c.size, vanish);
479 float r = max(length(q), 1e-3f), a = atan2(q.y, q.x) + c.t * .025;
480 float u = a * N / TAU;
481 float column = floor(u + .5), x = u - column;
482 float depth = (1 - .07 * cos(x * TAU)) / r;
483 float travel = c.travel * 1.9, zw = depth + travel;
484 float colId = ptlWrap(column, N);
485 float band = ptlBand(c, colId);
486 float pull = ptlFront(c, depth, .4, 5, .55);
487 float v = zw * N / TAU + pull * .3;
488 float fw = c.px / c.size / r * max(1.f, depth) * N / TAU;
489 float resolve = smoothstep(.06f, .02f, fw);
490 // Two strands braid around each other; bass twists them further.
491 float twist = c.b.x * 2.8 * (.55 + .45 * sin(colId * 1.9 + c.t * .5));
492 float phase = v * 2.2 + twist + colId * 1.3;
493 float A = sw * (1 + .25 * c.b.x);
494 float x1 = A * cos(phase);
495 bool firstOver = ptlWrap(floor(phase / PI), 2.f) < 1;
496 float d1 = abs(x - x1), d2 = abs(x + x1);
497 float aaX = clamp(fw, .002f, .1f);
498 float c1 = fill(d1 - sw, aaX), c2 = fill(d2 - sw, aaX);
499 bool useFirst = (c1 > .5 && c2 > .5) ? firstOver : d1 < d2;
500 float strand = useFirst ? c1 : c2;
501 float centre = useFirst ? x1 : -x1;
502 // Plied V stitches: two stitch columns per strand, each stitch two leaning yarn legs.
503 float vc = (x - centre) / sw + 1;
504 float2 sp = float2(fract(vc) - .5, v * rowsPerColumn);
505 float rowBase = floor(sp.y);
506 float best = 9, along = 0, legSide = 1;
507 float2 away = float2(0, 1);
508 for (int i = 0; i < 2; ++i) {
509 float ry = rowBase + (i == 0 ? 0.f : (fract(sp.y) < .5 ? -1.f : 1.f));
510 for (int j = 0; j < 2; ++j) {
511 float side = j == 0 ? -1 : 1;
512 float2 a0 = float2(side * .4, ry + .95), a1 = float2(side * .06, ry);
513 float2 pa = sp - a0, ba = a1 - a0;
514 float h = saturate(dot(pa, ba) / dot(ba, ba));
515 float2 dv = pa - ba * h;
516 float d = length(dv);
517 if (d < best) { best = d; along = h; away = dv / max(d, 1e-4f); legSide = side; }
518 }
519 }
520 const float yarn = .21;
521 float across = saturate(best / yarn);
522 float bump = sqrt(saturate(1 - across * across));
523 float legMask = 1 - smoothstep(.85f, 1.05f, best / yarn);
524 float ply = .75 + .25 * sin(along * 18 + legSide * across * 3);
525 float3 n = normalize(float3(away * across, bump + .05));
526 float3 lightA = normalize(float3(-.7, .5, .55)), lightB = normalize(float3(.75, -.35, .55));
527 float hue = colId * .09 + (useFirst ? 0 : .12);
528 float3 knit = paint(.05 + hue, f) * (.02 + .1 * bump);
529 knit += (paint(.12 + hue * .5, f) * max(0.f, dot(n, lightA)) + paint(.7, f) * max(0.f, dot(n, lightB))) * .28 * ply;
530 knit *= legMask;
531 knit = mix(paint(.3 + hue, f) * .1, knit, resolve);
532 // Strands are rounded: their edges fall into shadow, and the strand on top shades the other.
533 float strandEdge = sw - abs(x - centre);
534 float otherEdge = abs(x - (useFirst ? -x1 : x1)) - sw;
535 knit *= smoothstep(-.01f, .05f, strandEdge) * .6 + .4;
536 knit *= 1 - .7 * ((useFirst != firstOver) ? smoothstep(.07f, 0.f, otherEdge) : 0.f);
537 knit *= 1 + band * 2.8;
538 // Purl ridges between the cables.
539 float2 pp = float2(x * 18 + .5 * floor(v * rowsPerColumn), v * rowsPerColumn);
540 float2 pl = fract(pp) - .5;
541 float purlBump = saturate(1 - length(pl * float2(1.1, 1.9)) * 1.5);
542 float3 purl = paint(.55 + colId * .05, f) * (.015 + .1 * mix(.4f, purlBump, resolve)) * (1 + band);
543 float3 color = mix(purl, knit, strand);
544 // Fibre fuzz: a soft halo of stray fibres along the strand edges.
545 float fuzz = noise2(float2(u * 50, v * 50)) * glow(min(d1, d2) - sw, .03) * resolve;
546 color += paint(.75, f) * fuzz * (.08 + band * .5);
547 color += paint(.92, f) * pull * (.3 + bump) * strand * 1.3;
548 float3 core = paint(.6, f) * .15 + paint(.9, f) * exp(-r * 8) * .8;
549 return mix(core, color, ptlFog(depth, c, .05));
550}
551NATIVE_EFFECT(cableKnitTube)
552
553// ---------------------------------------------------------------- 318 Basket Weave Burrow
554// An elliptic burrow seen from low down, woven from two families of wide helical strips that
555// pass over and under each other, with light leaking through the gaps. Bass lifts and shifts
556// the strips (opening the gaps), low-mids and high-mids each light one strip family, highs
557// brighten the gap light and attacks tighten a weave ring that squeezes down the burrow.
558float3 basketWeaveBurrow(Ctx c, constant NativeFrame &f) {
559 const float ea = 1.3, eb = .85, h = .3, N = 14;
560 float2 vanish = ptlDrift(c.t * .5, float2(.15, .08));
561 float2 q = ptlBend(c.uv / c.size, vanish);
562 float r = max(length(q), 1e-3f);
563 float2 dir = q / r;
564 float A = dir.x * dir.x / (ea * ea) + dir.y * dir.y / (eb * eb);
565 float B = dir.y * h / (eb * eb), C = h * h / (eb * eb) - 1;
566 float R = (B + sqrt(max(0.f, B * B - A * C))) / A;
567 float2 P = dir * R;
568 float travel = c.travel * 2.0;
569 float squeeze = ptlFront(c, R / r, .4, 5, .5);
570 float depth = R / r * (1 + .12 * squeeze);
571 float zw = depth + travel;
572 float phi = atan2((P.y - h) / eb, P.x / ea);
573 float2 g = float2(phi * N / TAU, zw * N / 6.8);
574 float lift = c.b.x * (.5 + .5 * sin(g.y * .5 - c.t * .9 + phi * 2));
575 float2 s = float2(g.x + g.y, g.x - g.y) + lift * .25 * float2(1, -1);
576 float2 cell = floor(s), l = fract(s) - .5;
577 float fw = c.px / c.size / r * max(N / TAU, depth * N / 6.8) * 1.5;
578 float aa = clamp(fw, .01f, .5f);
579 float halfWidth = .41 - .12 * lift + .06 * squeeze;
580 float coverA = fill(abs(l.x) - halfWidth, aa), coverB = fill(abs(l.y) - halfWidth, aa);
581 float hA = cos(PI * (s.y - .5 + cell.x)), hB = -cos(PI * (s.x - .5 + cell.y));
582 float topA = smoothstep(-.15f, .15f, hA - hB);
583 float3 stripA = paint(.06 + .08 * sin(cell.x * 1.7), f) * (.03 + .22 * (.5 + .5 * hA)) * (1 + 3 * c.b.y);
584 float3 stripB = paint(.42 + .08 * sin(cell.y * 1.3), f) * (.03 + .2 * (.5 + .5 * hB)) * (1 + 3 * c.b.z);
585 stripA *= 1 - .45 * (l.x / halfWidth) * (l.x / halfWidth);
586 stripB *= 1 - .45 * (l.y / halfWidth) * (l.y / halfWidth);
587 // A soft sheen on each strip where it crests over its neighbour.
588 stripA += paint(.75, f) * pow(saturate(hA), 6.f) * .14 * saturate(1 - abs(l.x) / halfWidth);
589 stripB += paint(.75, f) * pow(saturate(hB), 6.f) * .14 * saturate(1 - abs(l.y) / halfWidth);
590 // Grain lines along each strip.
591 float grain = smoothstep(.4f, .15f, fw);
592 stripA *= 1 - grain * .2 * (.5 + .5 * sin(s.y * 40 + cell.x * 3));
593 stripB *= 1 - grain * .2 * (.5 + .5 * sin(s.x * 40 + cell.y * 3));
594 float3 gap = paint(.8 + .08 * sin(zw * .3), f) * (.5 + 3.5 * c.b.w);
595 float3 color = gap;
596 color = mix(color, stripB, coverB * (1 - coverA * topA));
597 color = mix(color, stripA, coverA * (coverB > 0 ? topA : 1));
598 color += gap * glow(min(abs(l.x), abs(l.y)) - halfWidth, .04) * (1 - coverA * coverB) * .2;
599 color += paint(.92, f) * squeeze * (.6 + .5 * max(hA, hB));
600 float3 average = paint(.3, f) * .12 + gap * .05;
601 color = mix(average, color, smoothstep(.45f, .15f, fw));
602 float3 core = paint(.8, f) * .3 + paint(.95, f) * exp(-r * 8) * 1.1;
603 return mix(core, color, ptlFog(depth, c, .05));
604}
605NATIVE_EFFECT(basketWeaveBurrow)
606
607// ---------------------------------------------------------------- 319 Louvre Tunnel
608// A slowly rolling octagonal tunnel whose faces are banks of angled louvre slats with
609// coloured light behind them. Slats open and close in waves; bass opens groups of slat rows,
610// low-mids and high-mids brighten the light behind alternate faces, highs glint the
611// mullions and attacks snap a ring of slats open so light floods through locally.
612float3 louvreTunnel(Ctx c, constant NativeFrame &f) {
613 const float rin = .9, P = .14;
614 float2 vanish = ptlDrift(c.t * .5, float2(.2, .14));
615 float2 q = ptlBend(c.uv / c.size, vanish);
616 float r = max(length(q), 1e-3f);
617 float a = atan2(q.y, q.x) - c.t * .03;
618 float sector = floor(a / (TAU / 8) + .5);
619 float local = a - sector * TAU / 8;
620 float face = ptlWrap(sector, 8.f);
621 float depth = rin / (r * cos(local));
622 float across = rin * tan(local);
623 float travel = c.travel * 2.2, zw = depth + travel;
624 float row = floor(zw / P), zl = zw / P - row;
625 float rowDepth = (row + .5) * P - travel;
626 float fz = clamp(fwidth(zw) / P, .002f, .5f);
627 float wave = .5 + .5 * sin(row * .3 - c.t * 1.3 + face * .7);
628 float bassRows = pow(.5 + .5 * sin(row * .8 + face * 1.1 - c.t * .2), 3.f);
629 float snap = ptlFront(c, rowDepth, .4, 5, .45);
630 float open = clamp(.06 + .36 * wave * wave + 1.4 * c.b.x * bassRows + 1.6 * snap, 0.f, .85f);
631 float cover = 1 - open;
632 float slat = fill(zl - cover, fz);
633 float t01 = saturate(zl / max(cover, .05f));
634 bool even = ptlWrap(face, 2.f) < 1;
635 float lamp = .45 + .55 * cos(across / (rin * .41421356) * 1.3);
636 float3 light = paint(even ? .05 + .05 * sin(row * .13) : .78 + .05 * sin(row * .11), f) * (.35 + .6 * open) * lamp * (1 + 2.4 * (even ? c.b.y : c.b.z));
637 light += paint(.95, f) * snap * 1.5;
638 // Slat face: tilted, so it shades from its hinge to the lip that catches the light.
639 float sideShade = 1 - .5 * pow(abs(across) / (rin * .41421356), 4.f);
640 float3 slatColor = paint(.98, f) * (.025 + .14 * t01 * t01) * sideShade;
641 slatColor += light * pow(t01, 10.f) * (.15 + .5 * open);
642 float3 color = mix(light, slatColor, slat);
643 // Light floods over the slats around open gaps.
644 color += light * glow(zl - cover, .04 + open * .3) * open * .25 * slat;
645 // Mullions between faces.
646 float edge = rin * .41421356 - abs(across);
647 float mullion = fill(edge - .04, max(fz * P, .003f));
648 float glint = pow(.5 + .5 * sin(zw * 9 - c.t * 2 + face * 2), 12.f);
649 float3 frame = deep(.2, f) * .03 + paint(.5, f) * glint * smoothstep(.01f, .04f, edge) * c.b.w * 3;
650 color = mix(color, frame, mullion);
651 float3 average = mix(light, slatColor, .6);
652 color = mix(average, color, smoothstep(.45f, .15f, fz));
653 float3 core = paint(.8, f) * .35 + paint(.95, f) * exp(-r * 8) * 1.0;
654 return mix(core, color, ptlFog(depth, c, .05));
655}
656NATIVE_EFFECT(louvreTunnel)
657
658// Coaster versions: the same designs riding the roller-coaster course.
659NATIVE_COASTER(chainmailBore, 310, false)
660NATIVE_COASTER(featherVault, 311, false)
661NATIVE_COASTER(tartanTube, 312, false)
662NATIVE_COASTER(herringboneHall, 313, false)
663NATIVE_COASTER(laceFunnel, 315, false)
664NATIVE_COASTER(zipperRun, 316, false)
665NATIVE_COASTER(cableKnitTube, 317, false)
666NATIVE_COASTER(basketWeaveBurrow, 318, false)
667NATIVE_COASTER(louvreTunnel, 319, false)