PulseCanvas / app · pulsecanvas.ai / Sources / PulseCanvas / Resources / NativeGeometry.metal
| 1 | // Fractal, tessellation, kaleidoscope and optical designs. Original PulseCanvas code; |
| 2 | // classic forms (Mandelbulb power formula, Menger sponge, sphere inversion, Poincare disk) |
| 3 | // are implemented from their mathematical definitions. Audio terms are zero at zero Chaos. |
| 4 | |
| 5 | float3 geometryNormal(float d0, float dx, float dy, float dz) { return normalize(float3(dx - d0, dy - d0, dz - d0) + 1e-6); } |
| 6 | |
| 7 | // ------------------------------------------------------------------ 35 Glass Mosaic |
| 8 | // Three stained-glass screens at different depths drift toward the viewer, a moving light |
| 9 | // behind them. Light passes through every screen, so overlapping panes mix colors. Each |
| 10 | // pane brightens with its band; attacks flare the light. |
| 11 | |
| 12 | |
| 13 | // ------------------------------------------------------------------ 47 Opal Tessera |
| 14 | // An endless zoom into a triangular opal mosaic: each tile subdivides into four as the |
| 15 | // view deepens. Tiles tilt to catch the light, flashing play-of-color; bands tilt their |
| 16 | // own tiles further and attacks flash the facets. |
| 17 | float3 triangleTile(float2 p, thread float2 &id, thread float &edge) { |
| 18 | float2 s = float2(p.x - p.y * .57735, p.y * 1.1547); |
| 19 | float2 cell = floor(s), local = fract(s); |
| 20 | float upper = step(1.f, local.x + local.y); |
| 21 | id = cell * 2 + upper; |
| 22 | float a = local.x, b = local.y, cc = 1 - a - b; |
| 23 | edge = min(min(abs(a), abs(b)), abs(cc)); |
| 24 | edge = upper > .5 ? min(min(1 - a, 1 - b), abs(a + b - 1)) : edge; |
| 25 | return float3(local, upper); |
| 26 | } |
| 27 | float3 opalLevel(float2 p, float level, Ctx c, constant NativeFrame &f, thread float &edgeOut) { |
| 28 | float2 id; float edge; |
| 29 | triangleTile(p, id, edge); |
| 30 | edgeOut = edge; |
| 31 | float seed = hash21(id + level * 31); |
| 32 | float band = c.b[int(seed * 3.99)]; |
| 33 | float tilt = sin(c.t * (.3 + seed * .5) + seed * 20 + c.m * .8) * .6 + band * .8; |
| 34 | float play = fbm2(p * 3 + id * .37 + tilt, 3); |
| 35 | float3 film = filmColor(play * 1.5 + tilt * .4 + seed, f); |
| 36 | float flash = pow(max(0.f, sin(play * 12 + tilt * 3)), 12.f) * (.5 + c.hit * 1.5); |
| 37 | float milk = .35 + .65 * smoothstep(.0f, .15f, edge); |
| 38 | return film * (.35 + .5 * smoothstep(-.4f, .8f, tilt)) * milk + paint(.95, f) * flash * .6; |
| 39 | } |
| 40 | float3 opalTessera(Ctx c, constant NativeFrame &f) { |
| 41 | float3 color = 0.f; |
| 42 | float edges = 0; |
| 43 | for (int octave = 0; octave < 2; ++octave) { |
| 44 | float2 z = octaveZoom(c.travel * .1, octave); |
| 45 | float level = floor(c.travel * .1) + float(octave); |
| 46 | float edge; |
| 47 | float3 layer = opalLevel(c.uv * z.x * 1.8 / c.size, level, c, f, edge); |
| 48 | color += layer * z.y; |
| 49 | edges += (1 - smoothstep(.0f, .03f, edge)) * z.y; |
| 50 | } |
| 51 | return color * (1 - edges * .85) + paint(.7, f) * edges * .05; |
| 52 | } |
| 53 | |
| 54 | // ------------------------------------------------------------------ 56 Laser Origami |
| 55 | // A Miura-folded paper landscape recedes to the horizon while palette lasers sweep across |
| 56 | // it; every beam bends over the folds. Bass deepens the folds, the music clock sweeps the |
| 57 | // lasers and attacks flash them. |
| 58 | |
| 59 | |
| 60 | // ------------------------------------------------------------------ 60 Mandel Bloom |
| 61 | // A Mandelbulb unfurls and folds through a continuously bending liquid domain. Bass |
| 62 | // twists its cross-sections, mids push waves through its lobes and attacks ripple veins. |
| 63 | // Continuous domain bending changes the actual folds, rather than only orbiting them. |
| 64 | // Integer power preserves the azimuth seam; rotating the domain preserves the poles. |
| 65 | float mandelField(float3 p, Ctx c, thread float4 &trap) { |
| 66 | float phase = c.t * .72 + c.m * .60; |
| 67 | float2 twist = rot(p.xz, sin(p.y * 1.5 + phase * .45) * (.16 + c.b.x * .28)); |
| 68 | float3 q = float3(twist.x, p.y, twist.y); |
| 69 | float3 wave = sin(q.yzx * 1.9 + float3(phase, -phase * .83, phase * .71)); |
| 70 | float3 fine = sin(q.zxy * 3.6 + float3(-phase * 1.2, phase * .92, -phase * .76)); |
| 71 | float strength = .10 + c.b.y * .14 + c.hit * .09; |
| 72 | q += wave * strength + fine * (.025 + c.b.z * .045); |
| 73 | q /= float3(1 + .09 * sin(phase * .63), 1 + .11 * cos(phase * .51), 1 + .08 * sin(phase * .57 + 2)); |
| 74 | float3 z = q; |
| 75 | float dr = 1, r = 0; |
| 76 | const float power = 8; |
| 77 | trap = float4(1e3); |
| 78 | int iterations = 6 + int(c.detail * 3); |
| 79 | for (int i = 0; i < iterations; ++i) { |
| 80 | r = max(length(z), 1e-6f); |
| 81 | if (r > 2) break; |
| 82 | float theta = acos(clamp(z.z / r, -1.f, 1.f)) * power; |
| 83 | float phi = atan2(z.y, z.x) * power; |
| 84 | dr = pow(r, power - 1) * power * dr + 1; |
| 85 | float zr = pow(r, power); |
| 86 | z = zr * float3(sin(theta) * cos(phi), sin(phi) * sin(theta), cos(theta)) + q; |
| 87 | trap = min(trap, float4(abs(z), dot(z, z))); |
| 88 | } |
| 89 | // Domain warping stretches distances; conservative steps keep thin folds intact. |
| 90 | return .5 * log(r) * r / max(dr, 1e-6f) / 2.1; |
| 91 | } |
| 92 | float3 mandelBloom(Ctx c, constant NativeFrame &f) { |
| 93 | float orbit = c.t * .04 + c.travel * .06; |
| 94 | // The bulb stays inside radius ~1.3 for these powers; the camera orbits outside it. |
| 95 | float distance = 1.75 + .2 * (.5 + .5 * sin(c.travel * .05 + c.t * .02)); |
| 96 | // Normalized, so the orbit keeps its full distance at every phase. |
| 97 | float3 direction = normalize(float3(sin(orbit) * cos(orbit * .7), sin(orbit * .5) * .5, cos(orbit) * cos(orbit * .7)) + float3(0, 0, 1e-3)); |
| 98 | float3 ro = direction * distance; |
| 99 | // Aim at a point on the far flank, so the surface fills most of the frame off-centre. |
| 100 | float3 side = normalize(cross(direction, float3(0, 1, 0)) + float3(1e-3, 0, 0)); |
| 101 | float3 target = side * (.55 + .1 * sin(c.t * .05)) + float3(0, .25 * cos(c.t * .04), 0); |
| 102 | float3 rd = cameraRay(c.uv / c.size, target - ro, float3(0, 1, 0), 1.7); |
| 103 | float travel = 0; |
| 104 | float4 trap; |
| 105 | bool hit = false; |
| 106 | int steps = 0; |
| 107 | for (int i = 0; i < 180; ++i) { |
| 108 | float d = mandelField(ro + rd * travel, c, trap); |
| 109 | if (d < max(.0002f, .00045 * travel)) { hit = true; break; } |
| 110 | travel += max(d * .75, .0001f); |
| 111 | steps = i; |
| 112 | if (travel > 6) break; |
| 113 | } |
| 114 | float3 background = deep(.4 + c.uv.y * .1, f) * .1 + paint(.6 + c.uv.x * .1, f) * .13 * smoothstep(.3f, .8f, fbm2(c.uv * 1.4 + float2(c.t * .02, 0), 4)); |
| 115 | if (!hit) return background + paint(.8, f) * pow(float(steps) / 180, 1.5f) * .6; |
| 116 | float3 p = ro + rd * travel; |
| 117 | float e = .0006 * travel + .0002; |
| 118 | float4 t2; |
| 119 | float d0 = mandelField(p, c, t2); |
| 120 | float3 n = geometryNormal(d0, mandelField(p + float3(e, 0, 0), c, t2), mandelField(p + float3(0, e, 0), c, t2), mandelField(p + float3(0, 0, e), c, t2)); |
| 121 | mandelField(p, c, trap); |
| 122 | float occlusion = 1 - float(steps) / 180; |
| 123 | float3 light = normalize(float3(.6, .7, -.4)); |
| 124 | float diffuse = max(dot(n, light), 0.f); |
| 125 | float rim = pow(1 - max(dot(-rd, n), 0.f), 2.f); |
| 126 | float flow = .5 + .5 * sin(dot(p, float3(3.1, 2.4, 2.8)) - c.t * 1.1 - c.m * 2.0); |
| 127 | float veins = (exp(-trap.x * 6) + exp(-trap.y * 6) * .6) * (.35 + flow * .45); |
| 128 | float band = c.b[int(clamp(trap.w * 1.4, 0.f, 3.99f))]; |
| 129 | float3 albedo = paint(.15 + trap.w * .5 + trap.z * .3, f); |
| 130 | float3 color = albedo * (.05 + diffuse * .8) * occlusion + paint(.9, f) * veins * (.4 + band * 1.7 + c.hit * flow) + paint(.6, f) * rim * .35; |
| 131 | return mix(background, color, exp(-travel * c.fog * .2)); |
| 132 | } |
| 133 | |
| 134 | // ------------------------------------------------------------------ 61 Menger Nave |
| 135 | // A flight through an endless Menger sponge cathedral. Each level of holes breathes with |
| 136 | // its own band, so the architecture opens and closes at different scales with the music. |
| 137 | float mengerField(float3 p, Ctx c) { |
| 138 | // Infinite sponge in units where each solid cube spans 2; the camera rides an open axis. |
| 139 | float unit = 1.5; |
| 140 | float3 q = p / unit; |
| 141 | float d = -1; |
| 142 | float s = 1; |
| 143 | for (int i = 0; i < 4; ++i) { |
| 144 | float band = c.b[min(i, 3)]; |
| 145 | float3 a = fmod(q * s + 1000, 2.f) - 1; |
| 146 | s *= 3; |
| 147 | float3 r = abs(1 - 3 * abs(a)); |
| 148 | float da = max(r.x, r.y), db = max(r.y, r.z), dc = max(r.z, r.x); |
| 149 | float cross = (min(da, min(db, dc)) - 1 + band * .25) / s; |
| 150 | d = max(d, cross); |
| 151 | } |
| 152 | return d * unit; |
| 153 | } |
| 154 | float3 mengerNave(Ctx c, constant NativeFrame &f) { |
| 155 | float3 ro = float3(sin(c.t * .05) * .12, cos(c.t * .04) * .12, c.travel * 1.6 + 3); |
| 156 | float3 rd = cameraRay(c.uv, float3(sin(c.t * .09) * .25, sin(c.t * .07) * .2, 1), float3(sin(c.t * .05) * .2, 1, 0), 1.3 * c.size); |
| 157 | float travel = 0; |
| 158 | bool hit = false; |
| 159 | int steps = 0; |
| 160 | for (int i = 0; i < 110; ++i) { |
| 161 | float d = mengerField(ro + rd * travel, c); |
| 162 | if (d < .0008 * (1 + travel)) { hit = true; break; } |
| 163 | travel += d; |
| 164 | steps = i; |
| 165 | if (travel > 30) break; |
| 166 | } |
| 167 | float3 fogColor = paint(.75, f) * .2; |
| 168 | if (!hit) return fogColor; |
| 169 | float3 p = ro + rd * travel; |
| 170 | float e = .001, d0 = mengerField(p, c); |
| 171 | float3 n = geometryNormal(d0, mengerField(p + float3(e, 0, 0), c), mengerField(p + float3(0, e, 0), c), mengerField(p + float3(0, 0, e), c)); |
| 172 | float occlusion = pow(1 - float(steps) / 110, 2.f); |
| 173 | float3 light = normalize(float3(.3, .8, .5)); |
| 174 | float diffuse = max(dot(n, light), 0.f); |
| 175 | float pulse = pow(.5 + .5 * sin(p.z * .8 - c.t * 1.5 - c.m * 3), 12.f) * (.3 + c.hit * 1.5); |
| 176 | float3 albedo = paint(.25 + dot(abs(n), float3(.1, .2, .3)) + p.z * .01, f); |
| 177 | float3 color = albedo * (.1 + diffuse * .9) * occlusion + paint(.9, f) * pulse * occlusion; |
| 178 | return mix(fogColor, color, exp(-travel * (.06 + c.fog * .08))); |
| 179 | } |
| 180 | |
| 181 | // ------------------------------------------------------------------ 62 Apollonian Foam |
| 182 | // A flight through an Apollonian sphere-inversion foam. Bubble walls glow by their orbit |
| 183 | // depth; each depth listens to its own band, and attacks shimmer across the foam. |
| 184 | |
| 185 | |
| 186 | // ------------------------------------------------------------------ 63 Hyperbolic Rosette |
| 187 | // An {8,3} tiling of the Poincare disk, scaled so the disk overfills the canvas. A |
| 188 | // continuous hyperbolic translation carries the viewer through the tiling forever. Tile |
| 189 | // walls glow; bands light the tiles in their screen sector and attacks pulse the walls. |
| 190 | |
| 191 | |
| 192 | // ------------------------------------------------------------------ 64 Quasicrystal Bloom |
| 193 | // Seven plane waves at equal angles interfere into a quasicrystal that never repeats. As |
| 194 | // the phases advance the pattern seems to bloom outward forever. Each wave's strength |
| 195 | // follows a band and attacks kick its phase. |
| 196 | float3 quasicrystalBloom(Ctx c, constant NativeFrame &f) { |
| 197 | float2 p = c.uv * 9 / c.size; |
| 198 | float sum = 0, gradientX = 0, gradientY = 0; |
| 199 | const int waves = 7; |
| 200 | for (int i = 0; i < waves; ++i) { |
| 201 | float angle = PI * float(i) / float(waves); |
| 202 | float2 d = float2(cos(angle), sin(angle)); |
| 203 | float band = c.b[i % 4]; |
| 204 | float phase = c.travel * .9 + c.t * .25 + c.m * (.3 + .1 * float(i)); |
| 205 | float amplitude = 1 + band * .8 + c.hit * .5 * float(i % 2); |
| 206 | float w = dot(p, d) + phase; |
| 207 | sum += cos(w) * amplitude; |
| 208 | gradientX -= sin(w) * amplitude * d.x; |
| 209 | gradientY -= sin(w) * amplitude * d.y; |
| 210 | } |
| 211 | sum /= float(waves); |
| 212 | float contour = fract(sum * 3 + c.t * .05); |
| 213 | float lines = exp(-abs(contour - .5) * 18); |
| 214 | float3 n = normalize(float3(-gradientX * .08, -gradientY * .08, 1)); |
| 215 | float light = max(dot(n, normalize(float3(-.4, .6, .7))), 0.f); |
| 216 | float3 color = paint(.5 + sum * .9, f) * (.12 + .5 * light) * smoothstep(-.4f, .8f, sum + .3); |
| 217 | color += paint(.9 + sum * .2, f) * lines * .6 + paint(.95, f) * pow(max(sum, 0.f), 4.f) * 1.4; |
| 218 | return color; |
| 219 | } |
| 220 | |
| 221 | // ------------------------------------------------------------------ 65 Tesseract Garden |
| 222 | // Layers of rotating tesseracts drift toward the viewer, each turning through four |
| 223 | // dimensions so its cubes fold through one another. Bands spin individual tesseracts |
| 224 | // faster; attacks flare their edges. |
| 225 | |
| 226 | |
| 227 | // ------------------------------------------------------------------ 66 Harmonograph |
| 228 | // Three decaying pendulum traces sweep across the canvas like ink from a harmonograph. |
| 229 | // The visible window of each trace rolls continuously inward; bands tug individual |
| 230 | // pendulum phases, so the loops shear and re-form with the music. |
| 231 | |
| 232 | |
| 233 | // ------------------------------------------------------------------ 67 Moire Engine |
| 234 | // Three fine gratings (rings, rays and lines) overlap; their interference throws huge |
| 235 | // moving fringes across the canvas. Zoom speed scales one grating against the others; |
| 236 | // bands swing the gratings and attacks shift their frequency. |
| 237 | |
| 238 | |
| 239 | // ------------------------------------------------------------------ 68 Riley Undulation |
| 240 | // Op-art stripes whose widths and phases swell into an illusion of a rolling surface. |
| 241 | // Bass sends a travelling bulge through the stripes; highs add a fine shimmer. |
| 242 | |
| 243 | |
| 244 | // ------------------------------------------------------------------ 69 Kaleido Chamber |
| 245 | // A three-mirror kaleidoscope: a chamber of tumbling glass shards reflected across an |
| 246 | // endless triangular lattice. The music clock tumbles the shards, bands brighten their |
| 247 | // colors and attacks rotate the chamber. |
| 248 | |
| 249 | |
| 250 | // ------------------------------------------------------------------ 70 Girih Lattice |
| 251 | // An eight-fold star-and-cross pattern of interlaced gilded straps, mapped through a |
| 252 | // log-polar Droste projection centred off-canvas, so zoom flows endlessly. Star tiles |
| 253 | // light with their bands; attacks run light along the straps. |
| 254 | |
| 255 | |
| 256 | #define GEOMETRY_EFFECTS(X) X(opalTessera) X(mandelBloom) X(mengerNave) X(quasicrystalBloom) |
| 257 | GEOMETRY_EFFECTS(NATIVE_EFFECT) |