PulseCanvas / app · pulsecanvas.ai / Sources / PulseCanvas / Resources / NativeCommon.metal
| 1 | #include <metal_stdlib> |
| 2 | using namespace metal; |
| 3 | |
| 4 | // PulseCanvas native effect prelude. Original procedural code: no external images, |
| 5 | // preset equations or third-party shader sources. Classic mathematical forms are |
| 6 | // implemented from their definitions. This file is prepended to every Native*.metal |
| 7 | // effect file when the renderer compiles the native library. |
| 8 | // |
| 9 | // Layout matches NativeUniforms in VisualizerRenderer.swift. The first fields match |
| 10 | // SpatialFrame so every native pass shares one uniform block. |
| 11 | struct NativeFrame { |
| 12 | float2 resolution; float time; int scene; |
| 13 | float4 rhythm; // attack swell, drive, low and high levels (Chaos-scaled) |
| 14 | float4 style; // brightness, size, fog, camera zoom |
| 15 | float4 camera; // rotation, Chaos, detail, zoom speed |
| 16 | float4 colors[5]; |
| 17 | float4 bands; // smoothed low, low-mid, high-mid, high (Chaos-scaled) |
| 18 | float4 motion; // zoom travel, music clock, procedural time, smoothed energy |
| 19 | }; |
| 20 | |
| 21 | // Everything an effect needs, already normalized. Every audio term is zero at zero Chaos. |
| 22 | struct Ctx { |
| 23 | float2 uv; // centred, y up; the shorter canvas edge spans [-1, 1] |
| 24 | float2 screen; // 0...1 across the canvas |
| 25 | float2 extent; // half-size of the canvas in uv units (>= 1 on each axis) |
| 26 | float px; // one pixel in uv units |
| 27 | float t; // procedural time (speed-scaled, never audio-driven) |
| 28 | float m; // music clock: advances faster in energetic passages |
| 29 | float e; // smoothed musical energy, 0...~1 |
| 30 | float hit; // short attack envelope |
| 31 | float4 b; // smoothed bands: low, low-mid, high-mid, high |
| 32 | float travel; // integrated forward zoom distance |
| 33 | float size; // size control, 1 at default |
| 34 | float fog; // depth control, 0...1 |
| 35 | float detail; // detail control, 0...1 |
| 36 | }; |
| 37 | |
| 38 | constant float PI = 3.14159265; |
| 39 | constant float TAU = 6.2831853; |
| 40 | |
| 41 | // ---------------------------------------------------------------- hashing / noise |
| 42 | float hash11(float p) { p = fract(p * .1031); p *= p + 33.33; p *= p + p; return fract(p); } |
| 43 | float hash21(float2 p) { float3 q = fract(float3(p.xyx) * .1031); q += dot(q, q.yzx + 33.33); return fract((q.x + q.y) * q.z); } |
| 44 | float2 hash22(float2 p) { float3 q = fract(float3(p.xyx) * float3(.1031, .1030, .0973)); q += dot(q, q.yzx + 33.33); return fract((q.xx + q.yz) * q.zy); } |
| 45 | float3 hash33(float3 p) { p = fract(p * float3(.1031, .1030, .0973)); p += dot(p, p.yxz + 33.33); return fract((p.xxy + p.yxx) * p.zyx); } |
| 46 | float hash31(float3 p) { p = fract(p * .1031); p += dot(p, p.zyx + 31.32); return fract((p.x + p.y) * p.z); } |
| 47 | |
| 48 | float noise2(float2 p) { |
| 49 | float2 i = floor(p), f = fract(p), u = f * f * (3 - 2 * f); |
| 50 | return mix(mix(hash21(i), hash21(i + float2(1, 0)), u.x), mix(hash21(i + float2(0, 1)), hash21(i + 1), u.x), u.y); |
| 51 | } |
| 52 | float noise3(float3 p) { |
| 53 | float3 i = floor(p), f = fract(p), u = f * f * (3 - 2 * f); |
| 54 | float a = hash31(i), b = hash31(i + float3(1, 0, 0)), c = hash31(i + float3(0, 1, 0)), d = hash31(i + float3(1, 1, 0)); |
| 55 | float e = hash31(i + float3(0, 0, 1)), g = hash31(i + float3(1, 0, 1)), h = hash31(i + float3(0, 1, 1)), k = hash31(i + 1); |
| 56 | return mix(mix(mix(a, b, u.x), mix(c, d, u.x), u.y), mix(mix(e, g, u.x), mix(h, k, u.x), u.y), u.z); |
| 57 | } |
| 58 | float fbm2(float2 p, int octaves) { |
| 59 | float sum = 0, amplitude = .5; |
| 60 | for (int i = 0; i < octaves; ++i) { sum += amplitude * noise2(p); p = float2(p.x * 1.6 - p.y * 1.2, p.x * 1.2 + p.y * 1.6) + 3.1; amplitude *= .5; } |
| 61 | return sum; |
| 62 | } |
| 63 | float fbm3(float3 p, int octaves) { |
| 64 | float sum = 0, amplitude = .5; |
| 65 | for (int i = 0; i < octaves; ++i) { sum += amplitude * noise3(p); p = p * 2.03 + float3(1.7, -3.1, 2.3); amplitude *= .5; } |
| 66 | return sum; |
| 67 | } |
| 68 | // Smooth divergence-free-looking flow from a noise potential. |
| 69 | float2 curl2(float2 p) { |
| 70 | const float e = .05; |
| 71 | float a = noise2(p + float2(0, e)), b = noise2(p - float2(0, e)); |
| 72 | float c = noise2(p + float2(e, 0)), d = noise2(p - float2(e, 0)); |
| 73 | return float2(a - b, d - c) / (2 * e); |
| 74 | } |
| 75 | // Voronoi: x = nearest distance, y = second distance, zw = nearest cell id. |
| 76 | float4 voronoi(float2 p, float jitter, float phase) { |
| 77 | float2 cell = floor(p), f = fract(p); |
| 78 | float first = 8, second = 8; float2 winner = 0; |
| 79 | for (int y = -1; y <= 1; ++y) for (int x = -1; x <= 1; ++x) { |
| 80 | float2 g = float2(x, y), seed = hash22(cell + g); |
| 81 | float2 site = g + .5 + jitter * .5 * sin(phase + seed * TAU); |
| 82 | float d = length(site - f); |
| 83 | if (d < first) { second = first; first = d; winner = cell + g; } else second = min(second, d); |
| 84 | } |
| 85 | return float4(first, second, winner); |
| 86 | } |
| 87 | |
| 88 | // ---------------------------------------------------------------- geometry |
| 89 | float2 rot(float2 p, float a) { float c = cos(a), s = sin(a); return float2(c * p.x - s * p.y, s * p.x + c * p.y); } |
| 90 | float smin(float a, float b, float k) { float h = clamp(.5 + .5 * (b - a) / k, 0.f, 1.f); return mix(b, a, h) - k * h * (1 - h); } |
| 91 | float smax(float a, float b, float k) { return -smin(-a, -b, k); } |
| 92 | float sdBox(float3 p, float3 b) { float3 q = abs(p) - b; return length(max(q, 0.f)) + min(max(q.x, max(q.y, q.z)), 0.f); } |
| 93 | float sdBox2(float2 p, float2 b) { float2 q = abs(p) - b; return length(max(q, 0.f)) + min(max(q.x, q.y), 0.f); } |
| 94 | float sdTorus(float3 p, float2 t) { return length(float2(length(p.xz) - t.x, p.y)) - t.y; } |
| 95 | float sdCapsule(float3 p, float3 a, float3 b, float r) { float3 pa = p - a, ba = b - a; float h = clamp(dot(pa, ba) / dot(ba, ba), 0.f, 1.f); return length(pa - ba * h) - r; } |
| 96 | float sdSegment(float2 p, float2 a, float2 b) { float2 pa = p - a, ba = b - a; float h = clamp(dot(pa, ba) / max(dot(ba, ba), 1e-6f), 0.f, 1.f); return length(pa - ba * h); } |
| 97 | float sdHex(float2 p, float r) { const float3 k = float3(-.8660254, .5, .57735); p = abs(p); p -= 2 * min(dot(k.xy, p), 0.f) * k.xy; p -= float2(clamp(p.x, -k.z * r, k.z * r), r); return length(p) * sign(p.y); } |
| 98 | // Hexagonal tiling: xy = local offset, zw = cell id. |
| 99 | float4 hexCell(float2 p) { |
| 100 | const float2 s = float2(1, 1.7320508); |
| 101 | float2 a = p - s * floor(p / s + .5), b = p - s * (floor((p - s * .5) / s + .5) + .5); |
| 102 | return dot(a, a) < dot(b, b) ? float4(a, round(p / s)) : float4(b, round((p - s * .5) / s) + .5); |
| 103 | } |
| 104 | // Repeat with a stable cell index. |
| 105 | float repeatId(float x, float period) { return floor(x / period + .5); } |
| 106 | |
| 107 | // ---------------------------------------------------------------- shading |
| 108 | float3 paint(float phase, constant NativeFrame &f) { |
| 109 | float cycle = phase - floor(phase * .5) * 2; |
| 110 | float x = (cycle < 1 ? cycle : 2 - cycle) * 4; |
| 111 | int i = min(3, int(x)); |
| 112 | return mix(f.colors[i].rgb, f.colors[i + 1].rgb, x - float(i)); |
| 113 | } |
| 114 | float3 deep(float phase, constant NativeFrame &f) { return paint(phase, f) * paint(phase + .5, f); } |
| 115 | float glow(float d, float width) { return width * width / (width * width + d * d); } |
| 116 | float line(float d, float width, float px) { return 1 - smoothstep(width - px, width + px, abs(d)); } |
| 117 | float fill(float d, float px) { return 1 - smoothstep(-px, px, d); } |
| 118 | float3 hsvTint(float3 color, float amount) { float l = dot(color, float3(.299, .587, .114)); return mix(float3(l), color, amount); } |
| 119 | // Thin-film interference tint, used for soap, oil and opal colors; blended with the palette. |
| 120 | float3 filmColor(float thickness, constant NativeFrame &f) { |
| 121 | float3 rainbow = .5 + .5 * cos(TAU * (thickness + float3(0, .33, .67))); |
| 122 | return mix(paint(thickness, f), rainbow * paint(thickness * .5 + .2, f) * 2, .45); |
| 123 | } |
| 124 | |
| 125 | // ---------------------------------------------------------------- raymarch helpers |
| 126 | float3 cameraRay(float2 uv, float3 forward, float3 up, float lens) { |
| 127 | float3 w = normalize(forward), u = normalize(cross(up, w)), v = cross(w, u); |
| 128 | return normalize(uv.x * u + uv.y * v + lens * w); |
| 129 | } |
| 130 | // Slot k (back to front) of a forward-moving stack of n layers. x = phase (0 far, 1 at |
| 131 | // the viewer), y = a stable layer id that survives the wrap, so layers never pop. |
| 132 | float2 layerSlot(int k, int n, float travel) { |
| 133 | float shift = travel * float(n); |
| 134 | return float2((float(k) + fract(shift)) / float(n), float(k) - floor(shift)); |
| 135 | } |
| 136 | float layerFade(float phase) { return smoothstep(0.f, .22f, phase) * (1 - smoothstep(.8f, 1.f, phase)); } |
| 137 | // Magnification for a layer: far layers are small and dense, near layers large. |
| 138 | float layerScale(float phase, float range) { return exp2((1 - phase) * range); } |
| 139 | // Seamless scale-space zoom for self-similar patterns: two octaves crossfade at weight 0. |
| 140 | float2 octaveZoom(float travel, int octave) { |
| 141 | float z = fract(travel); |
| 142 | float weight = octave == 0 ? 1 - smoothstep(0.f, 1.f, z) : smoothstep(0.f, 1.f, z); |
| 143 | return float2(exp2(float(octave) - z), weight); |
| 144 | } |
| 145 | // Oblique camera over a ground plane; the horizon sits above the canvas so the plane |
| 146 | // fills every aspect ratio. Returns ground x, forward distance and view distance. |
| 147 | float3 groundView(float2 uv, float2 extent, float height, float margin) { |
| 148 | float horizon = extent.y + margin; |
| 149 | float depth = height / max(horizon - uv.y, 1e-3f); |
| 150 | return float3(uv.x * depth, depth, depth * length(float2(1, uv.x))); |
| 151 | } |
| 152 | |
| 153 | // ---------------------------------------------------------------- context / output |
| 154 | Ctx makeContext(float2 position, constant NativeFrame &f) { |
| 155 | Ctx c; |
| 156 | float2 res = max(f.resolution, float2(1)); |
| 157 | float shortSide = min(res.x, res.y); |
| 158 | c.screen = position / res; |
| 159 | c.extent = res / shortSide; |
| 160 | float2 uv = (position - res * .5) / (shortSide * .5); |
| 161 | uv.y = -uv.y; |
| 162 | float zoom = clamp(f.style.w, .3f, 2.f); |
| 163 | c.uv = rot(uv, f.camera.x) / zoom; |
| 164 | c.extent /= zoom; |
| 165 | c.px = 2 / (shortSide * zoom); |
| 166 | c.travel = f.motion.x; |
| 167 | c.m = f.motion.y; |
| 168 | c.t = f.motion.z; |
| 169 | c.e = clamp(f.motion.w, 0.f, 1.5f); |
| 170 | c.hit = clamp(f.rhythm.x, 0.f, 1.f); |
| 171 | c.b = clamp(f.bands, 0.f, 1.f); |
| 172 | c.size = sqrt(clamp(f.style.y, .3f, 6.f)); |
| 173 | c.fog = clamp(f.style.z, 0.f, 1.f); |
| 174 | c.detail = clamp(f.camera.z, 0.f, 1.f); |
| 175 | return c; |
| 176 | } |
| 177 | |
| 178 | // ---------------------------------------------------------------- roller-coaster course |
| 179 | // Coaster versions ride a real tube that winds through space instead of a straight bore. Each |
| 180 | // pixel's ray is traced against a tube whose centreline bends away ahead of the camera; the design |
| 181 | // is handed the wall's own coordinates (the angle around the tube and the depth along it) as |
| 182 | // the uv a straight tunnel has there. The walls stay rigid: they sweep past while the far end |
| 183 | // swings away around each bend and drops behind the wall on tight turns, the track climbs and |
| 184 | // dives, and the camera banks into each turn. Mirrored in NativeExpansionCommon.metal. |
| 185 | float2 coasterPath(float z, float seed) { |
| 186 | return float2(1.5 * sin(z * .3 + seed) + .6 * sin(z * .55 + seed * 2.3), |
| 187 | 1.1 * sin(z * .26 + seed * 1.7 + 1.1) + .5 * sin(z * .5 + seed * .6)); |
| 188 | } |
| 189 | float2 coasterSlope(float z, float seed) { |
| 190 | return float2(.45 * cos(z * .3 + seed) + .33 * cos(z * .55 + seed * 2.3), |
| 191 | .286 * cos(z * .26 + seed * 1.7 + 1.1) + .25 * cos(z * .5 + seed * .6)); |
| 192 | } |
| 193 | // Distance from the tube's axis across its section: round, or square for box corridors. |
| 194 | float coasterRadius(float2 q, bool square) { return square ? max(fabs(q.x), fabs(q.y)) : length(q); } |
| 195 | // Where the ray is across the tube's section at depth z, relative to the bending centreline. |
| 196 | float2 coasterLateral(float2 ray, float z, float along, float2 origin, float2 slope, float seed) { |
| 197 | return ray * z - (coasterPath(along + z, seed) - origin - slope * z); |
| 198 | } |
| 199 | Ctx coasterTube(Ctx c, float seed, float along, bool square) { |
| 200 | // The camera rides the centreline. Its view lags the track a little, so each turn swings |
| 201 | // the outer wall into view before the far end goes round it, and it banks into the turn. |
| 202 | float2 origin = coasterPath(along, seed), slope = coasterSlope(along - 1.2, seed); |
| 203 | float2 ray = rot(c.uv, -.3 * (coasterSlope(along + 4, seed).x - slope.x)); |
| 204 | float spread = length(ray); |
| 205 | float z = .05, inside = .05, gap = 1; |
| 206 | float2 lateral = ray * z; |
| 207 | for (int i = 0; i < 40; ++i) { |
| 208 | lateral = coasterLateral(ray, z, along, origin, slope, seed); |
| 209 | gap = 1 - coasterRadius(lateral, square); |
| 210 | if (gap < 0) break; |
| 211 | inside = z; |
| 212 | if (gap < .002 || z > 60) break; |
| 213 | z += gap * .85 / (spread + min(2.f, .4f * z + .6f)); |
| 214 | } |
| 215 | // A step can cross the wall: bisect between the last depth inside and the first outside. |
| 216 | if (gap < 0) { |
| 217 | float outside = z; |
| 218 | for (int i = 0; i < 6; ++i) { |
| 219 | z = .5 * (inside + outside); |
| 220 | lateral = coasterLateral(ray, z, along, origin, slope, seed); |
| 221 | if (coasterRadius(lateral, square) < 1) inside = z; else outside = z; |
| 222 | } |
| 223 | } |
| 224 | float2 uv = lateral / max(coasterRadius(lateral, square), 1e-4f) / z; |
| 225 | // Pixel footprint for anti-aliasing: walls bunch up on the inside of a turn. |
| 226 | c.px = clamp(max(length(dfdx(uv)), length(dfdy(uv))), c.px * .25f, c.px * 6); |
| 227 | c.uv = uv; |
| 228 | return c; |
| 229 | } |
| 230 | // The Coaster versions (390–455) ride the track of the design they come from. |
| 231 | Ctx coasterRide(Ctx c, int source, bool box) { return coasterTube(c, float(source) * 1.37, c.travel * 2.4 + c.m * .8, box); } |
| 232 | |
| 233 | float4 finish(float3 color, float2 position, constant NativeFrame &f) { |
| 234 | color = max(color, float3(0)) * max(0.f, f.style.x); |
| 235 | color = 1 - exp(-color * 1.55); |
| 236 | // Static ordered dither keeps smooth gradients from banding in 8-bit video. |
| 237 | float dither = (hash21(floor(position)) - .5) / 255; |
| 238 | return float4(saturate(color + dither), 1); |
| 239 | } |
| 240 | |
| 241 | // Each effect is `float3 name(Ctx c, constant NativeFrame &f)`; this emits its fragment. |
| 242 | #define NATIVE_EFFECT(name) \ |
| 243 | fragment float4 name##Fragment(float4 position [[position]], constant NativeFrame &f [[buffer(0)]]) { \ |
| 244 | Ctx c = makeContext(position.xy, f); \ |
| 245 | return finish(name(c, f), position.xy, f); \ |
| 246 | } |
| 247 | // A design's Coaster version: the same design riding the roller-coaster course (box: square tube). |
| 248 | #define NATIVE_COASTER(name, source, box) \ |
| 249 | fragment float4 name##CoasterFragment(float4 position [[position]], constant NativeFrame &f [[buffer(0)]]) { \ |
| 250 | Ctx c = coasterRide(makeContext(position.xy, f), source, box); \ |
| 251 | return finish(name(c, f), position.xy, f); \ |
| 252 | } |
| 253 | |
| 254 | vertex float4 nativeVertex(uint id [[vertex_id]]) { |
| 255 | const float2 p[3] = {float2(-1, -1), float2(3, -1), float2(-1, 3)}; |
| 256 | return float4(p[id], 0, 1); |
| 257 | } |