PulseCanvas / app · pulsecanvas.ai / Sources / PulseCanvas / Resources / NativeExpansionFractalTunnel.metal
| 1 | // Original PulseCanvas designs 365–369. Flights through fractal tunnels: fractal sets wrapped onto tube walls, intertwined fractal tubes, a raymarched quaternion Julia and a loxodromic double spiral. The owner asked for "fractals twisting into each other ... through a fractal tunnel". Vivid and glowing; avoid the look of the withdrawn 126 Fractal Throat (grey folded-box blades) and the kept 61 Menger Nave / 60 Mandel Bloom. |
| 2 | // Escape-time sets, distance estimates and the Koenigs linearisation are implemented from their definitions. |
| 3 | |
| 4 | // ---------------------------------------------------------------- complex helpers (ft_) |
| 5 | float2 ft_mul(float2 a, float2 b) { return float2(a.x * b.x - a.y * b.y, a.x * b.y + a.y * b.x); } |
| 6 | float2 ft_sq(float2 a) { return float2(a.x * a.x - a.y * a.y, 2 * a.x * a.y); } |
| 7 | float2 ft_div(float2 a, float2 b) { return float2(a.x * b.x + a.y * b.y, a.y * b.x - a.x * b.y) / max(dot(b, b), 1e-20f); } |
| 8 | float2 ft_exp(float2 a) { return exp(a.x) * float2(cos(a.y), sin(a.y)); } |
| 9 | float2 ft_log(float2 a) { return float2(.5f * log(max(dot(a, a), 1e-30f)), atan2(a.y, a.x)); } |
| 10 | float2 ft_sqrt(float2 a) { |
| 11 | float m = length(a); |
| 12 | return float2(sqrt(max(.5f * (m + a.x), 0.f)), (a.y < 0 ? -1.f : 1.f) * sqrt(max(.5f * (m - a.x), 0.f))); |
| 13 | } |
| 14 | // Triangle wave: 1 at integers, 0 at half-integers; a continuous mirror fold. |
| 15 | float ft_tri(float x) { return abs(fract(x) * 2 - 1); } |
| 16 | // Band level for a continuous family index with period 4; neighbouring families blend. |
| 17 | float ft_band(float x, float4 b) { |
| 18 | float ph = x - 4 * floor(x * .25f), sum = 0; |
| 19 | for (int i = 0; i < 4; ++i) { float d = abs(ph - float(i)); d = min(d, 4 - d); sum += max(0.f, 1 - d) * b[i]; } |
| 20 | return sum; |
| 21 | } |
| 22 | |
| 23 | // ---------------------------------------------------------------- 365 Mandelbrot Wormhole |
| 24 | // The tube wall is parameterised by the multiplier mu of the main cardioid (c = mu/2 - mu^2/4): |
| 25 | // the tunnel angle is the internal angle, so every crown carries the ring of bulbs that sits on |
| 26 | // the cardioid; |mu| swings in and out of the boundary along depth, so crowns of bulbs, their |
| 27 | // filaments and the cardioid interior stream past and twist into the vanishing point. |
| 28 | float3 mandelbrotWormhole(Ctx c, constant NativeFrame &f) { |
| 29 | float2 p = c.uv / c.size + float2(sin(c.t * .11f), cos(c.t * .09f)) * .05f; |
| 30 | float r = max(length(p), 1e-3f), ang = atan2(p.y, p.x); |
| 31 | float near = -log(r) + 1.2f; // log-polar distance from the viewer |
| 32 | float depth = near + c.travel * .55f; |
| 33 | float s = .15f + .07f * sin(depth * 1.3f + c.t * .1f) + .28f * sin(depth * 6.f); |
| 34 | float theta = 3 * ang + depth * .9f + c.t * .05f; // three crowns around, twisting with depth |
| 35 | float2 mu = exp(s) * float2(cos(theta), sin(theta)); |
| 36 | float2 cc = .5f * mu - .25f * ft_sq(mu); |
| 37 | float cpu = max(length(fwidth(cc)), 1e-7f) / c.px; |
| 38 | |
| 39 | // Analytic components: main cardioid (multiplier mu or 2 - mu) and the period-2 disc. |
| 40 | float2 mirror = float2(2, 0) - mu; |
| 41 | float mainR = min(length(mu), length(mirror)); |
| 42 | float twoR = 4 * length(cc + float2(1, 0)); |
| 43 | float2 z = 0, dz = 0; float m2 = 0, atom = 1e9; int per = 1; bool escaped = false; int n = 0; |
| 44 | if (mainR >= 1 && twoR >= 1) { |
| 45 | for (n = 0; n < 64; ++n) { |
| 46 | dz = 2 * ft_mul(z, dz) + float2(1, 0); |
| 47 | z = ft_sq(z) + cc; |
| 48 | m2 = dot(z, z); |
| 49 | if (m2 < atom) { atom = m2; per = n + 1; } |
| 50 | if (m2 > 1e4) { escaped = true; break; } |
| 51 | } |
| 52 | } else per = mainR < 1 ? 1 : 2; |
| 53 | |
| 54 | float front = 1.2f + (1 - c.hit) * 4.5f; |
| 55 | float wave = c.hit * exp(-(near - front) * (near - front) * 5); |
| 56 | float3 col; |
| 57 | if (escaped) { |
| 58 | float nu = float(n) + 1 - log2(.5f * log(m2)); |
| 59 | float de = .25f * sqrt(m2 / max(dot(dz, dz), 1e-20f)) * log(m2) / cpu; |
| 60 | // High-mid lights the halo and equipotential threads, treble the neon filaments. |
| 61 | float halo = exp(-de * 40); |
| 62 | float neon = glow(de, .0035f + .003f * c.detail + .004f * c.b.w); |
| 63 | float equip = pow(ft_tri(nu * .5f), 12.f) * exp(-de * 6); |
| 64 | col = deep(.9f + nu * .03f, f) * .06f; |
| 65 | col += paint(.75f + nu * .035f, f) * (.2f * halo + .16f * equip) * (1 + 3 * c.b.z); |
| 66 | col += paint(.05f + .08f * sin(nu * .4f), f) * neon * (1 + 2 * c.b.w + 2.5f * wave); |
| 67 | col += paint(.95f, f) * halo * wave * .7f; |
| 68 | } else { |
| 69 | // Components light by period: cardioid with the bass, period 2 low-mid, 3 high-mid, 4+ treble. |
| 70 | float fam = float(min(per, 4) - 1); |
| 71 | float lit = 1 + 3 * c.b[int(fam)]; |
| 72 | float edge = per == 1 ? mainR : twoR; |
| 73 | float rim = per <= 2 ? pow(edge, 24.f) : smoothstep(.0f, .5f, sqrt(atom)); |
| 74 | float body = per <= 2 ? pow(edge, 4.f) : rim; |
| 75 | float2 m = per == 1 ? (length(mu) < 1 ? mu : mirror) : cc + float2(1, 0); |
| 76 | float spokes = line(sin(atan2(m.y, m.x) * (per == 1 ? 7 : 4) + c.t * .2f), .08f, .03f) * body; |
| 77 | float level = line(sin(log(1.0001f - edge) * 5 - c.t * .6f), .12f, .06f) * (1 - rim) * smoothstep(.3f, .9f, edge); |
| 78 | col = per == 1 ? deep(.15f, f) * .1f + paint(.02f, f) * (.18f * spokes + .7f * rim) * lit + paint(.85f, f) * (body * .06f + level * .18f) * lit |
| 79 | : paint(.3f + .22f * fam, f) * (.08f + .3f * body + .5f * rim + .15f * spokes + .2f * level) * lit; |
| 80 | col += paint(.9f, f) * rim * wave * 1.5f; |
| 81 | } |
| 82 | float fade = smoothstep(.01f, .2f, r) * exp(-near * .12f * (.4f + c.fog)) * .72f; |
| 83 | return col * fade + paint(.88f, f) * glow(r, .05f) * .5f + deep(.4f, f) * .03f; |
| 84 | } |
| 85 | NATIVE_EFFECT(mandelbrotWormhole) |
| 86 | |
| 87 | // ---------------------------------------------------------------- 366 Burning Ship Abyss |
| 88 | // A twisting square shaft. Each wall shows the Burning Ship's armada (hulls, masts and |
| 89 | // rigging) mirrored at the waterline and at the mast tips, so spires hang from both sides of |
| 90 | // every hull as you descend; the walls fold at their centres and corners. |
| 91 | float3 burningShipAbyss(Ctx c, constant NativeFrame &f) { |
| 92 | float2 p = c.uv / c.size; |
| 93 | float r0 = max(length(p), 1e-3f); |
| 94 | p = rot(p, -.3f * log(r0) + c.t * .035f); |
| 95 | float2 a = abs(p); |
| 96 | float d = max(max(a.x, a.y), 1e-3f); |
| 97 | float u = min(a.x, a.y) / d; |
| 98 | float near = 1.2f - log(d); |
| 99 | float depth = near + c.travel * .5f; |
| 100 | float v = depth * 1.5f; |
| 101 | // Side walls and floor/ceiling show different stretches of the armada; they meet at the corners. |
| 102 | float x0 = a.x > a.y ? -1.97f : -1.84f; |
| 103 | float2 cc = float2(mix(x0, -1.55f, u), -.004f - .15f * (1 - ft_tri(v))); |
| 104 | float cpu = max(length(fwidth(cc)), 1e-8f) / c.px; |
| 105 | |
| 106 | float2 z = 0; float md = 0, m2 = 0, trap = 1e9; bool escaped = false; int n = 0; |
| 107 | for (n = 0; n < 56; ++n) { |
| 108 | md = 2 * sqrt(m2) * md + 1; |
| 109 | z = abs(z); |
| 110 | z = float2(z.x * z.x - z.y * z.y, 2 * z.x * z.y) + cc; |
| 111 | m2 = dot(z, z); |
| 112 | trap = min(trap, abs(z.y)); |
| 113 | if (m2 > 1e4) { escaped = true; break; } |
| 114 | } |
| 115 | float mast = (cc.x + 1.93f) / .08f; |
| 116 | float lit = 1 + 3.2f * ft_band(mast, c.b); |
| 117 | float front = .8f + (1 - c.hit) * 5; |
| 118 | float ring = c.hit * exp(-(near - front) * (near - front) * 7); |
| 119 | float3 col; |
| 120 | if (escaped) { |
| 121 | float nu = float(n) + 1 - log2(.5f * log(m2)); |
| 122 | float de = .25f * sqrt(m2) * log(m2) / md / cpu; |
| 123 | float heat = saturate(nu / 40.f); |
| 124 | float halo = exp(-de * 200), neon = glow(de, .0015f); |
| 125 | float flame = pow(ft_tri(nu * .5f), 8.f); |
| 126 | col = deep(.2f + heat * .3f, f) * (.05f + .25f * heat * heat); |
| 127 | col += paint(.55f + .1f * sin(nu * .7f), f) * flame * heat * .18f; |
| 128 | col += paint(.15f + heat * .6f, f) * (pow(heat, 2.2f) * 1.1f + .1f * heat) * (.7f + .3f * flame) * lit; |
| 129 | col += paint(.6f, f) * neon * lit * .3f + paint(1.f, f) * (neon + flame * halo + .4f * flame * heat) * ring * 1.6f; |
| 130 | } else { |
| 131 | col = (paint(.62f, f) * .3f + paint(.95f, f) * .35f * glow(trap, .02f)) * lit + paint(.9f, f) * ring * .5f; |
| 132 | } |
| 133 | col += paint(.3f, f) * glow(1 - u, .012f) * .3f; // the shaft's corner seams |
| 134 | float fade = smoothstep(.01f, .25f, d) * exp(-near * .05f * (.4f + c.fog)); |
| 135 | return col * fade + deep(.3f, f) * glow(d, .12f) * .5f; |
| 136 | } |
| 137 | NATIVE_EFFECT(burningShipAbyss) |
| 138 | |
| 139 | // ---------------------------------------------------------------- 367 Fractal Double Helix |
| 140 | // Two raymarched helical tubes braid around the flight axis, weaving in and out; each wall |
| 141 | // carries a tiled, living Julia set (its own parameter orbiting), shaded with rim light and a |
| 142 | // neon halo in the gaps. |
| 143 | float3 fractalDoubleHelix(Ctx c, constant NativeFrame &f) { |
| 144 | float2 uv = c.uv / c.size; |
| 145 | // Every z frequency below divides a 16-unit period, so the camera wraps without a seam. |
| 146 | float camZ = c.travel * 1.25f; |
| 147 | camZ -= 16 * floor(camZ / 16); |
| 148 | float2 sway = float2(.1f * sin(c.t * .13f), .08f * cos(c.t * .11f)); |
| 149 | float3 ro = float3(sway, camZ); |
| 150 | float3 rd = cameraRay(uv, float3(.1f * sin(c.t * .07f), .08f * cos(c.t * .09f), 1), float3(0, 1, 0), 1.2f); |
| 151 | const float K = PI / 2, R = .8f; |
| 152 | float spin = c.t * .12f; |
| 153 | float rhoA = .25f + .12f * c.b.x, rhoB = .25f + .12f * c.b.y; |
| 154 | float tt = .05f, gA = 0, gB = 0, dA = 1, dB = 1; bool hit = false; |
| 155 | float3 q = ro; |
| 156 | for (int i = 0; i < 48; ++i) { |
| 157 | q = ro + rd * tt; |
| 158 | float ph = q.z * K + spin, weave = .3f * sin(q.z * (PI / 8) + c.t * .2f); |
| 159 | float2 h = float2(cos(ph), sin(ph)); |
| 160 | dA = length(q.xy - h * R * (1 + weave)) - rhoA; dB = length(q.xy + h * R * (1 - weave)) - rhoB; |
| 161 | float dd = min(dA, dB); |
| 162 | gA += exp(-max(dA, 0.f) * 5); gB += exp(-max(dB, 0.f) * 5); |
| 163 | if (dd < .0015f * tt) { hit = true; break; } |
| 164 | tt += dd * .7f; |
| 165 | if (tt > 22) break; |
| 166 | } |
| 167 | bool strandA = dA < dB; |
| 168 | float ph = q.z * K + spin, weave = .3f * sin(q.z * (PI / 8) + c.t * .2f); |
| 169 | float sgn = strandA ? 1 : -1; |
| 170 | float2 h = float2(cos(ph), sin(ph)) * R * (1 + sgn * weave) * sgn; |
| 171 | float2 a2 = q.xy - h; |
| 172 | float2 local = rot(a2, -ph); |
| 173 | float psi = atan2(local.y, local.x); |
| 174 | // Rays that slip between the strands meet the enclosing fractal tunnel wall. |
| 175 | const float wallR = 2.6f; |
| 176 | float ra = dot(rd.xy, rd.xy), rb = dot(ro.xy, rd.xy), rc = dot(ro.xy, ro.xy) - wallR * wallR; |
| 177 | float tw = (-rb + sqrt(max(rb * rb - ra * rc, 0.f))) / max(ra, 1e-6f); |
| 178 | float3 qw = ro + rd * tw; |
| 179 | float thw = atan2(qw.y, qw.x) + qw.z * (PI / 24); |
| 180 | float2 zj = hit ? (2 * float2(ft_tri(q.z * 1.125f + (strandA ? 0 : .5f)), ft_tri(psi / PI)) - 1) * 1.45f |
| 181 | : (2 * float2(ft_tri(qw.z * .375f), ft_tri(thw * 3 / PI)) - 1) * 1.45f; |
| 182 | float zpx = max(length(fwidth(zj)), 1e-6f) / c.px; |
| 183 | // Living Julia sets: parameters glide just inside the main cardioid's rim. |
| 184 | float ca = c.t * .05f + (hit ? (strandA ? 0 : 2.2f) : 4.1f); |
| 185 | float2 em = .97f * float2(cos(ca), sin(ca)); |
| 186 | float2 cj = .5f * em - .25f * ft_sq(em); |
| 187 | float m2 = dot(zj, zj), md = 1, trap = 1e9; bool escaped = false; int n = 0; |
| 188 | for (n = 0; n < 32; ++n) { |
| 189 | md *= 2 * sqrt(m2); |
| 190 | zj = ft_sq(zj) + cj; |
| 191 | m2 = dot(zj, zj); |
| 192 | trap = min(trap, abs(sqrt(m2) - .4f)); |
| 193 | if (m2 > 64) { escaped = true; break; } |
| 194 | } |
| 195 | float hueBase = hit ? (strandA ? .05f : .6f) : .3f; |
| 196 | float bandFill = strandA ? c.b.x : c.b.y, bandLine = strandA ? c.b.z : c.b.w; |
| 197 | if (!hit) { bandFill = 0; bandLine = .5f * (c.b.z + c.b.w); } |
| 198 | float3 tex; |
| 199 | if (escaped) { |
| 200 | float nu = float(n) + 1 - log2(.5f * log(m2)); |
| 201 | float de = .25f * sqrt(m2) * log(m2) / max(md, 1e-6f) / zpx; |
| 202 | float bands = pow(ft_tri(nu * .5f), 4.f); |
| 203 | tex = deep(hueBase + .1f, f) * .07f + paint(hueBase + nu * .03f, f) * (.03f + .2f * bands) * exp(-de * 4) |
| 204 | + paint(hueBase + .45f, f) * glow(de, .005f + .004f * bandLine) * (1 + 2.5f * bandLine); |
| 205 | } else { |
| 206 | // Koenigs coordinate of the attracting fixed point em/2: a rose of spokes and rings. |
| 207 | float2 kz = ft_div(zj - .5f * em, ft_exp(float(n) * float2(log(.97f), ca))); |
| 208 | float rose = pow(.5f + .5f * cos(atan2(kz.y, kz.x) * 5 + log(max(length(kz), 1e-6f)) * 3 - c.t * .5f), 3.f); |
| 209 | tex = paint(hueBase + .3f + .2f * rose, f) * (.06f + .5f * rose) * (.7f + 1.8f * bandFill); |
| 210 | } |
| 211 | float pulseZ = camZ + 1.5f + (1 - c.hit) * 14; |
| 212 | float3 col; |
| 213 | if (hit) { |
| 214 | float2 nxy = normalize(a2); |
| 215 | float3 nrm = normalize(float3(nxy, dot(nxy, R * K * float2(sin(ph), -cos(ph)) * sgn))); |
| 216 | float facing = abs(dot(nrm, rd)); |
| 217 | float rim = pow(1 - facing, 3.f); |
| 218 | float pulse = c.hit * exp(-(q.z - pulseZ) * (q.z - pulseZ) * 2); |
| 219 | col = tex * (.5f + .7f * facing) + paint(hueBase + .1f, f) * rim * .8f + paint(.95f, f) * pulse * 1.5f; |
| 220 | col *= exp(-tt * .07f * (.4f + c.fog)); |
| 221 | } else { |
| 222 | float pulse = c.hit * exp(-(qw.z - pulseZ) * (qw.z - pulseZ) * .5f); |
| 223 | col = (tex * .4f + paint(.9f, f) * pulse * .5f) * exp(-(qw.z - camZ) * .09f * (.4f + c.fog)); |
| 224 | } |
| 225 | if (!hit) col += paint(.25f, f) * gA * .006f + paint(.75f, f) * gB * .006f; |
| 226 | col += paint(.9f, f) * glow(length(uv - sway * 1.2f), .08f) * .35f; |
| 227 | return col; |
| 228 | } |
| 229 | NATIVE_EFFECT(fractalDoubleHelix) |
| 230 | |
| 231 | // ---------------------------------------------------------------- 368 Quaternion Bloom |
| 232 | // Distance estimate of a quaternion Julia set, q <- q^2 + c, sliced into 3D by a rotating |
| 233 | // 4D plane. |dq| is tracked as a scalar because the quaternion norm is multiplicative. |
| 234 | float ft_qjulia(float3 p, float4 cq, float2 slice, thread float4 &trap) { |
| 235 | float4 q = float4(p.x, p.y * slice.x, p.z, p.y * slice.y); |
| 236 | float m2 = dot(q, q), md2 = 1; |
| 237 | trap = float4(1e3); |
| 238 | for (int i = 0; i < 8; ++i) { |
| 239 | md2 *= 4 * m2; |
| 240 | q = float4(q.x * q.x - dot(q.yzw, q.yzw), 2 * q.x * q.yzw) + cq; |
| 241 | m2 = dot(q, q); |
| 242 | if (i < 3) trap = min(trap, float4(abs(q.x), abs(q.y), abs(length(q.yzw) - .5f), abs(sqrt(m2) - .8f))); |
| 243 | if (m2 > 256) break; |
| 244 | } |
| 245 | return .25f * sqrt(m2 / md2) * log(m2); |
| 246 | } |
| 247 | float3 quaternionBloom(Ctx c, constant NativeFrame &f) { |
| 248 | float2 uv = c.uv / c.size; |
| 249 | float az = c.travel * .21f + c.t * .02f, el = .35f * sin(c.travel * .07f + 1); |
| 250 | float dist = 1.85f - .25f * sin(c.travel * .11f); |
| 251 | float3 ro = dist * float3(cos(el) * sin(az), sin(el), cos(el) * cos(az)); |
| 252 | float3 rd = cameraRay(uv, -ro, float3(0, 1, 0), 1.35f); |
| 253 | float sa = c.t * .06f; |
| 254 | float2 slice = float2(cos(sa), sin(sa)); |
| 255 | float ca = c.t * .045f; |
| 256 | float4 cq = float4(-.2f + .07f * sin(ca), .4f + .1f * sin(ca * .8f + 1), -.4f + .1f * cos(ca * .6f), -.4f + .08f * sin(ca * 1.1f + 2)); |
| 257 | float bR = 1.5f, bb = dot(ro, rd), cc0 = dot(ro, ro) - bR * bR, disc = bb * bb - cc0; |
| 258 | float haloAcc = 0; bool hit = false; float tt = 0; float4 trap = 1e3; float steps = 0; |
| 259 | if (disc > 0) { |
| 260 | float t0 = max(-bb - sqrt(disc), 0.f), t1 = -bb + sqrt(disc); |
| 261 | tt = t0; |
| 262 | float dd = 1; |
| 263 | for (int i = 0; i < 32; ++i) { |
| 264 | dd = ft_qjulia(ro + rd * tt, cq, slice, trap); |
| 265 | haloAcc += exp(-max(dd, 0.f) * 20); |
| 266 | if (dd < .003f * tt) break; |
| 267 | tt += max(dd, .002f); |
| 268 | steps += 1; |
| 269 | if (tt > t1) { dd = 1; break; } |
| 270 | } |
| 271 | hit = dd < .03f; |
| 272 | } |
| 273 | float r = length(uv), ang = atan2(uv.y, uv.x); |
| 274 | // A corona of soft petals unfolding behind the bloom. |
| 275 | float lr = log(max(r, 1e-3f)); |
| 276 | float petals = pow(.5f + .5f * cos(ang * 7 + sin(lr * 2.5f - c.t * .3f) * 1.2f - c.t * .08f), 4.f); |
| 277 | float veil = pow(.5f + .5f * cos(ang * 4 - lr * 3 + c.t * .15f), 6.f); |
| 278 | float3 col = deep(.55f, f) * .07f + paint(.45f + .25f * petals, f) * (petals * .2f + veil * .08f) * smoothstep(.25f, 1.2f, r) * exp(-r * .4f); |
| 279 | col += paint(.45f, f) * haloAcc * .025f; |
| 280 | if (hit) { |
| 281 | float3 pos = ro + rd * tt; |
| 282 | float4 tmp; |
| 283 | const float e = .003f; |
| 284 | float d0 = ft_qjulia(pos, cq, slice, trap); |
| 285 | float3 nrm = normalize(float3(ft_qjulia(pos + float3(e, 0, 0), cq, slice, tmp) - d0, |
| 286 | ft_qjulia(pos + float3(0, e, 0), cq, slice, tmp) - d0, |
| 287 | ft_qjulia(pos + float3(0, 0, e), cq, slice, tmp) - d0)); |
| 288 | float facing = max(0.f, -dot(nrm, rd)); |
| 289 | float rim = pow(1 - facing, 2.5f); |
| 290 | float diff = max(0.f, dot(nrm, normalize(float3(.5f, .8f, .3f)))); |
| 291 | float shell = .2f + (1 - c.hit) * 1.4f; |
| 292 | float flare = c.hit * exp(-(length(pos) - shell) * (length(pos) - shell) * 30); |
| 293 | float3 vc = 0; |
| 294 | for (int k = 0; k < 4; ++k) vc += paint(.6f + .1f * float(k), f) * glow(trap[k], .01f + .012f * c.b[k]) * (.35f + 3 * c.b[k] + 3 * flare); |
| 295 | float ao = 1 - steps / 48; |
| 296 | float3 base = paint(.3f + .6f * saturate(trap.w * 1.5f), f) * (.12f + .6f * diff) * ao; |
| 297 | col = base + vc * .5f + paint(.95f, f) * rim * 1.1f; |
| 298 | } |
| 299 | return col; |
| 300 | } |
| 301 | NATIVE_EFFECT(quaternionBloom) |
| 302 | |
| 303 | // ---------------------------------------------------------------- 369 Loxodrome Fractal |
| 304 | // Bipolar coordinates W = log((p - source) / (p - sink)) feed a loxodromic flow through the |
| 305 | // exponential picture of a Julia set at its repelling fixed point alpha. The Koenigs relation |
| 306 | // G(f(z)) = 2 G(z) makes that picture exactly periodic under w -> w + log(lambda), so the |
| 307 | // detail streams out of one spiral pole and into the other without ever resetting. |
| 308 | float3 loxodromeFractal(Ctx c, constant NativeFrame &f) { |
| 309 | float2 p = c.uv / c.size; |
| 310 | float2 axis = rot(c.extent.x >= c.extent.y ? float2(1, 0) : float2(0, 1), .3f * sin(c.t * .05f)); |
| 311 | float sep = .55f * max(c.extent.x, c.extent.y) / c.size; |
| 312 | float2 side = float2(-axis.y, axis.x) * .12f; |
| 313 | float2 p1 = -axis * sep + side, p2 = axis * sep - side; |
| 314 | float2 W = ft_log(ft_div(p - p1, p - p2)); |
| 315 | float dW = length(p2 - p1) / max(length(p - p1) * length(p - p2), 1e-6f); |
| 316 | |
| 317 | float ca = c.t * .03f; |
| 318 | float2 cj = float2(-.05f, .84f) + .025f * float2(cos(ca), sin(ca * 1.3f)); |
| 319 | float2 alpha = .5f * (float2(1, 0) - ft_sqrt(float2(1, 0) - 4 * cj)); |
| 320 | float2 lam = 2 * alpha; |
| 321 | float2 L = ft_log(lam); |
| 322 | // w = A W with A (2 pi i) = 2 pi i + L: one turn around a pole is one level deeper. |
| 323 | float2 A = float2(1, 0) + ft_div(L, float2(0, TAU)); |
| 324 | float2 w = ft_mul(A, W) - L.x * c.travel * .9f * float2(1, .6f) + float2(0, c.t * .06f); |
| 325 | float k = floor(w.x / L.x); |
| 326 | float2 wr = w - k * L; |
| 327 | const float sigma = .05f; |
| 328 | const int pre = 8; |
| 329 | float2 delta = sigma * ft_exp(wr - float(pre) * L); |
| 330 | float2 ddelta = delta; // d delta / d w' |
| 331 | for (int i = 0; i < pre; ++i) { |
| 332 | ddelta = ft_mul(ddelta, lam + 2 * delta); |
| 333 | delta = ft_mul(delta, lam + delta); |
| 334 | } |
| 335 | float2 z = alpha + delta, dz = ddelta; |
| 336 | float m2 = dot(z, z), trap = 1e9; bool escaped = false; int n = 0; |
| 337 | for (n = 0; n < 40; ++n) { |
| 338 | dz = 2 * ft_mul(z, dz); |
| 339 | z = ft_sq(z) + cj; |
| 340 | m2 = dot(z, z); |
| 341 | trap = min(trap, length(z - alpha)); |
| 342 | if (m2 > 1e4) { escaped = true; break; } |
| 343 | } |
| 344 | float wpx = length(A) * dW; |
| 345 | float front = mix(-3.5f, 3.5f, 1 - c.hit); |
| 346 | float wave = c.hit * exp(-(W.x - front) * (W.x - front) * 1.2f); |
| 347 | float3 col; |
| 348 | if (escaped) { |
| 349 | float nu = float(n) + 1 - log2(.5f * log(m2)) - k; |
| 350 | float de = .25f * sqrt(m2 / max(dot(dz, dz), 1e-30f)) * log(m2) / max(wpx, 1e-6f); |
| 351 | float lit = 1 + 3 * ft_band(nu * .5f, c.b); |
| 352 | float halo = exp(-de * 18), neon = glow(de, .004f); |
| 353 | float flow = pow(ft_tri(nu * .5f), 8.f) * exp(-de * 3); |
| 354 | col = deep(.3f + nu * .02f, f) * .12f + paint(.45f + .25f * sin(nu * .3f), f) * (.3f * halo + .16f * flow) * lit; |
| 355 | col += paint(.92f, f) * neon * (1 + 3 * wave) + paint(1.f, f) * halo * wave * .6f; |
| 356 | } else { |
| 357 | col = deep(.2f, f) * .25f + paint(.7f, f) * glow(trap, .05f) * .4f; |
| 358 | } |
| 359 | float pole = min(length(p - p1), length(p - p2)); |
| 360 | col *= smoothstep(.0f, .12f, pole) * (.3f + .55f * pow(abs(W.y) / PI, 1.5f)); |
| 361 | col += paint(.92f, f) * glow(length(p - p1), .07f) * .7f + paint(.55f, f) * glow(length(p - p2), .07f) * .5f; |
| 362 | return col; |
| 363 | } |
| 364 | NATIVE_EFFECT(loxodromeFractal) |
| 365 | |
| 366 | // Coaster versions: the same designs riding the roller-coaster course. |
| 367 | NATIVE_COASTER(mandelbrotWormhole, 365, false) |
| 368 | NATIVE_COASTER(burningShipAbyss, 366, false) |
| 369 | NATIVE_COASTER(fractalDoubleHelix, 367, false) |