PulseCanvas / app · pulsecanvas.ai / Sources / PulseCanvas / Resources / NativeStructure.metal
| 1 | // Architecture, machinery and lattice designs. Original PulseCanvas procedural code. |
| 2 | // Every effect receives Ctx from NativeCommon.metal; audio terms are zero at zero Chaos. |
| 3 | |
| 4 | float3 normalFrom(float3 p, float e, float d0, float dx, float dy, float dz) { return normalize(float3(dx - d0, dy - d0, dz - d0)); } |
| 5 | |
| 6 | // ------------------------------------------------------------------ 32 Chrome Reef |
| 7 | // A flight through a labyrinth of chrome coral. A channel stays open around the camera; |
| 8 | // each coral branch swells with its band and attacks send ripples along the surfaces. |
| 9 | float2 reefPath(float z) { return float2(sin(z * .21) * 1.3, cos(z * .17) * .9); } |
| 10 | float reefField(float3 p, Ctx c) { |
| 11 | float s = 1.6 / c.size; |
| 12 | float3 q = p * s; |
| 13 | float band = c.b[int(hash31(floor(q * .5)) * 3.99)]; |
| 14 | q += .25 * sin(q.yzx * .7 + c.t * .2 + c.m * .35); |
| 15 | float gyroid = dot(sin(q), cos(q.yzx)); |
| 16 | float detail = dot(sin(q * 3.1 + c.t * .1), cos(q.zxy * 3.1)) * .12; |
| 17 | float shell = abs(gyroid + detail) - (.28 + band * .22 + c.hit * .05 * sin(q.z * 4 - c.m * 6)); |
| 18 | float d = shell / (s * 2.2); |
| 19 | float2 path = reefPath(p.z); |
| 20 | return max(d, 1.1 - length(p.xy - path)); |
| 21 | } |
| 22 | float3 chromeReef(Ctx c, constant NativeFrame &f) { |
| 23 | float z = c.travel * 1.4; |
| 24 | float3 ro = float3(reefPath(z), z); |
| 25 | float3 ahead = float3(reefPath(z + 1.5), z + 1.5) - ro; |
| 26 | float3 rd = cameraRay(c.uv, ahead, float3(sin(c.t * .07) * .3, 1, 0), 1.3); |
| 27 | float travel = 0, steps = 0; |
| 28 | bool hit = false; |
| 29 | for (int i = 0; i < 96; ++i) { |
| 30 | float d = reefField(ro + rd * travel, c); |
| 31 | if (d < .0015 * (1 + travel)) { hit = true; break; } |
| 32 | travel += d * .8; |
| 33 | steps += 1; |
| 34 | if (travel > 18) break; |
| 35 | } |
| 36 | float3 haze = deep(.25 + rd.y * .2, f) * .25 + paint(.6, f) * .05; |
| 37 | if (!hit) return haze; |
| 38 | float3 p = ro + rd * travel; |
| 39 | float e = .003, d0 = reefField(p, c); |
| 40 | float3 n = normalFrom(p, e, d0, reefField(p + float3(e, 0, 0), c), reefField(p + float3(0, e, 0), c), reefField(p + float3(0, 0, e), c)); |
| 41 | float3 r = reflect(rd, n); |
| 42 | float occlusion = 1 - steps / 96; |
| 43 | float3 environment = mix(deep(.15, f) * .3, paint(.35 + r.y * .35, f) * 1.3, smoothstep(-.3f, .5f, r.y)); |
| 44 | environment += paint(.95, f) * pow(max(dot(r, normalize(float3(.3, .8, .4))), 0.f), 18.f) * 2; |
| 45 | environment += paint(.7, f) * pow(.5 + .5 * sin(r.x * 6 + r.z * 4 + c.t * .2), 12.f) * .5; |
| 46 | float fresnel = .35 + .65 * pow(1 - max(dot(-rd, n), 0.f), 3.f); |
| 47 | float polyps = pow(.5 + .5 * sin(p.x * 18) * sin(p.y * 18) * sin(p.z * 18), 10.f) * .25 * (1 + c.b.w); |
| 48 | float3 color = environment * fresnel * (.35 + .65 * occlusion) + paint(.8, f) * polyps * occlusion; |
| 49 | return mix(haze, color, exp(-travel * (.05 + c.fog * .12))); |
| 50 | } |
| 51 | |
| 52 | // ------------------------------------------------------------------ 37 Ion Cathedral |
| 53 | // A flight down an endless gothic nave: pointed arches, ribbed vaults and columns, lit by |
| 54 | // ionised light that runs along the ribs. The music clock drives the light pulses; bass |
| 55 | // thickens the glow and attacks send a flash down the nave. |
| 56 | |
| 57 | // Each bay of the nave listens to its own band. |
| 58 | |
| 59 | |
| 60 | // ------------------------------------------------------------------ 38 Velvet Circuit |
| 61 | // A low flight over a circuit board. Truchet routing keeps every copper trace connected, |
| 62 | // with 45-degree chamfers, vias and chips. Signal packets race along the traces on the |
| 63 | // music clock; each trace family brightens with its own band. |
| 64 | |
| 65 | |
| 66 | // ------------------------------------------------------------------ 39 Lunar Loom |
| 67 | // A moonlit jacquard tapestry billows gently. Real over-under threads carry a large |
| 68 | // woven motif of drifting crescents; individual threads shimmer on their bands, bass |
| 69 | // swells the cloth and attacks send a bright shuttle thread racing across. |
| 70 | |
| 71 | |
| 72 | // ------------------------------------------------------------------ 71 Gear Works |
| 73 | // Layers of meshing brass gear trains drift toward the viewer. Neighbouring gears counter- |
| 74 | // rotate with matched teeth; the music clock drives each train and gears glow with their |
| 75 | // bands. Alternate layers use larger gears for depth. |
| 76 | |
| 77 | |
| 78 | // ------------------------------------------------------------------ 72 Piston Hall |
| 79 | // Down the length of an engine hall: rows of cylinders whose pistons pump on a shared |
| 80 | // crankshaft. Bass lengthens each row's stroke, attacks fire the combustion glow, and |
| 81 | // the music clock raises engine speed. |
| 82 | float pistonField(float3 p, Ctx c, thread float &fire) { |
| 83 | float3 q = p; |
| 84 | float row = repeatId(q.z, 1.6); |
| 85 | q.z -= row * 1.6; |
| 86 | float side = sign(q.x); |
| 87 | q.x = abs(q.x) - 1.5; |
| 88 | float phase = row * 1.3 + (side > 0 ? PI : 0) + c.t * 2.2 + c.m * 2.5; |
| 89 | float stroke = .45 + c.b.x * .8 + c.hit * .25; |
| 90 | float lift = sin(phase) * stroke; |
| 91 | float cylinder = max(length(q.xz) - .52, abs(q.y - .6) - .7); |
| 92 | cylinder = max(cylinder, -(length(q.xz) - .44)); |
| 93 | float piston = max(length(q.xz) - .42, abs(q.y - (1.45 + lift)) - .28); |
| 94 | piston = min(piston, max(length(q.xz) - .3, abs(q.y - (1.78 + lift)) - .05)); |
| 95 | float rod = sdCapsule(q, float3(0, 1.2 + lift, 0), float3(sin(phase) * .25, -.3, 0), .08); |
| 96 | float block = sdBox(float3(q.x, q.y + 1, q.z), float3(.8, .35, .78)); |
| 97 | float floorPlane = p.y + 1.4; |
| 98 | fire = (.5 + .5 * cos(phase)) * smoothstep(.35f, .0f, abs(q.y - 1.3)) * smoothstep(.6f, .2f, length(q.xz)); |
| 99 | return min(min(min(cylinder, piston), min(rod, block)), floorPlane); |
| 100 | } |
| 101 | float3 pistonHall(Ctx c, constant NativeFrame &f) { |
| 102 | c.travel += c.m * f.camera.w * .5; |
| 103 | float3 ro = float3(sin(c.t * .07) * .4, 3.1 + sin(c.t * .2) * .2, c.travel * 2); |
| 104 | float3 rd = cameraRay(c.uv, float3(sin(c.t * .11) * .2, -.55, 1), float3(0, 1, 0), 1.3); |
| 105 | float travel = 0, fire = 0, glow = 0; |
| 106 | bool hit = false; |
| 107 | for (int i = 0; i < 90; ++i) { |
| 108 | float3 p = ro + rd * travel; |
| 109 | float d = pistonField(p, c, fire); |
| 110 | glow += fire * .02 * exp(-max(d, 0.f) * 6); |
| 111 | if (d < .002) { hit = true; break; } |
| 112 | travel += d * .8; |
| 113 | if (travel > 35) break; |
| 114 | } |
| 115 | float3 color = deep(.3, f) * .08; |
| 116 | if (hit) { |
| 117 | float3 p = ro + rd * travel; |
| 118 | float e = .003, dummy; |
| 119 | float d0 = pistonField(p, c, fire); |
| 120 | float3 n = normalFrom(p, e, d0, pistonField(p + float3(e, 0, 0), c, dummy), pistonField(p + float3(0, e, 0), c, dummy), pistonField(p + float3(0, 0, e), c, dummy)); |
| 121 | float3 r = reflect(rd, n); |
| 122 | float3 light = normalize(float3(.4, .9, -.3)); |
| 123 | float diffuse = max(dot(n, light), 0.f); |
| 124 | float specular = pow(max(dot(r, light), 0.f), 30.f); |
| 125 | float stripes = pow(.5 + .5 * sin(atan2(r.x, r.z) * 6), 20.f) * smoothstep(0.f, .4f, r.y); |
| 126 | float3 metal = paint(.35 + n.y * .2, f) * (.12 + diffuse * .7) + paint(.95, f) * (specular * 1.5 + stripes * .8); |
| 127 | metal += paint(.7, f) * pow(max(r.y, 0.f), 3.f) * .4; |
| 128 | metal += paint(.8, f) * fire * 1.5 * (1 + c.hit * 2); |
| 129 | float wave = .5 + .5 * sin(p.z * 5 - c.t * 3 - c.m * 4); |
| 130 | metal += paint(.35 + .18 * sin(p.x * 2), f) * (c.b[int(abs(p.z) * 2) % 4] * wave * .85 + c.hit * pow(wave, 5.f)); |
| 131 | color = metal; |
| 132 | } |
| 133 | color += paint(.8, f) * glow * (1.5 + c.hit * 1.5 + c.b.x * 1.5); |
| 134 | return mix(color, deep(.3, f) * .15 + paint(.6, f) * .1, 1 - exp(-travel * (.03 + c.fog * .05))); |
| 135 | } |
| 136 | |
| 137 | // ------------------------------------------------------------------ 73 Orrery Engine |
| 138 | // A colossal brass orrery seen from above, its hub off-canvas, so engraved orbits and |
| 139 | // planets sweep across the frame. Orbits repeat in scale for an endless zoom; each planet |
| 140 | // orbits on its own band and attacks light the engraved tracks. |
| 141 | |
| 142 | |
| 143 | // ------------------------------------------------------------------ 74 Space Frame |
| 144 | // A flight through a colossal octet space-frame of box beams, lit by moving spotlights. |
| 145 | // Nodes flare on their own bands and light pulses run along the beams with the music. |
| 146 | float beam(float2 q, float w) { float2 d = abs(q) - w; return length(max(d, 0.f)) + min(max(d.x, d.y), 0.f); } |
| 147 | float frameField(float3 p, Ctx c, thread float &node) { |
| 148 | float cell = 3; |
| 149 | float3 id = floor(p / cell + .5); |
| 150 | float3 q = p - id * cell; |
| 151 | float w = .07 * c.size; |
| 152 | float d = min(min(beam(q.yz, w), beam(q.xz, w)), beam(q.xy, w)); |
| 153 | // Face diagonals in the xz and xy planes. |
| 154 | float2 diagonal = float2((q.x + q.z) * .7071, (q.x - q.z) * .7071); |
| 155 | d = min(d, beam(float2(min(abs(diagonal.x), abs(diagonal.y)), q.y), w * .7)); |
| 156 | float2 diagonal2 = float2((q.x + q.y) * .7071, (q.x - q.y) * .7071); |
| 157 | d = min(d, beam(float2(min(abs(diagonal2.x), abs(diagonal2.y)), q.z), w * .7)); |
| 158 | float band = c.b[int(hash31(id) * 3.99)]; |
| 159 | node = length(q) - (.22 + band * .12) * c.size; |
| 160 | return min(d, node); |
| 161 | } |
| 162 | float3 spaceFrame(Ctx c, constant NativeFrame &f) { |
| 163 | float3 ro = float3(1.5 + sin(c.t * .1) * .3, .75 + cos(c.t * .08) * .2, c.travel * 2.4); |
| 164 | float3 rd = cameraRay(c.uv, float3(sin(c.t * .07) * .3, sin(c.t * .05) * .2, 1), float3(sin(c.t * .04) * .3, 1, 0), 1.2); |
| 165 | float travel = 0, node = 9, nodeGlow = 0; |
| 166 | bool hit = false; |
| 167 | for (int i = 0; i < 100; ++i) { |
| 168 | float3 p = ro + rd * travel; |
| 169 | float d = frameField(p, c, node); |
| 170 | nodeGlow += exp(-max(node, 0.f) * 5) * .015; |
| 171 | if (d < .002) { hit = true; break; } |
| 172 | travel += d * .9; |
| 173 | if (travel > 45) break; |
| 174 | } |
| 175 | float3 sky = deep(.3 + rd.y * .2, f) * .12 + paint(.75, f) * pow(max(dot(rd, normalize(float3(.5, .4, 1))), 0.f), 12.f) * .5; |
| 176 | float3 color = sky; |
| 177 | if (hit) { |
| 178 | float3 p = ro + rd * travel; |
| 179 | float e = .003, dummy, d0 = frameField(p, c, dummy); |
| 180 | float3 n = normalFrom(p, e, d0, frameField(p + float3(e, 0, 0), c, dummy), frameField(p + float3(0, e, 0), c, dummy), frameField(p + float3(0, 0, e), c, dummy)); |
| 181 | float3 light = normalize(float3(.5, .4, 1)); |
| 182 | float diffuse = max(dot(n, light), 0.f); |
| 183 | float specular = pow(max(dot(reflect(rd, n), light), 0.f), 24.f); |
| 184 | float pulse = pow(.5 + .5 * sin(dot(p, float3(.3, .2, 1)) * 1.5 - c.t * 2 - c.m * 3), 10.f); |
| 185 | color = paint(.3 + dot(p, float3(.01, .02, .015)), f) * (.05 + diffuse * .5) + paint(.95, f) * specular * .8; |
| 186 | color += paint(.85, f) * pulse * .6 * (.3 + c.b.y * 1.5); |
| 187 | color = mix(sky, color, exp(-travel * (.035 + c.fog * .05))); |
| 188 | } |
| 189 | return color + paint(.9, f) * nodeGlow * (1 + c.hit * 2); |
| 190 | } |
| 191 | |
| 192 | // ------------------------------------------------------------------ 75 Brutalist Shift |
| 193 | // An isometric concrete megastructure under hard sunlight. Tower blocks slide up and |
| 194 | // down on slow schedules, each also rising with its own band; attacks pop window lights. |
| 195 | |
| 196 | |
| 197 | // ------------------------------------------------------------------ 76 Rose Window |
| 198 | // Colored light streams through a vast rose window into a dusty nave. The tracery turns |
| 199 | // slowly; panes brighten with their bands and attacks flare the light shafts. |
| 200 | |
| 201 | |
| 202 | // ------------------------------------------------------------------ 92 Atom Lattice |
| 203 | // A flight through a ball-and-stick crystal with two kinds of atoms. Each atom vibrates |
| 204 | // on its own band, bonds stretch with them, and attacks send a flash along the bonds. |
| 205 | float3 atomOffset(float3 id, Ctx c) { |
| 206 | float3 seed = hash33(id); |
| 207 | float band = c.b[int(seed.x * 3.99)]; |
| 208 | return sin(c.t * (1.3 + seed * 1.7) + c.m * 3 * seed + seed * TAU) * (.03 + band * .14); |
| 209 | } |
| 210 | float atomField(float3 p, Ctx c, thread float &kind, thread float &bond) { |
| 211 | // Body-centred lattice: test the nearest corner atom, the nearest centre atom and |
| 212 | // the single bond between them; farther primitives cannot be closer. |
| 213 | float cell = 2; |
| 214 | float3 cornerId = floor(p / cell + .5); |
| 215 | float3 corner = cornerId * cell + atomOffset(cornerId, c); |
| 216 | float3 centerId = floor(p / cell); |
| 217 | float3 center = (centerId + .5) * cell + atomOffset(centerId + .5, c); |
| 218 | float cornerRadius = (.36 + .08 * hash31(cornerId)) * c.size * .8; |
| 219 | float cornerAtom = length(p - corner) - cornerRadius; |
| 220 | float centerAtom = length(p - center) - .26 * c.size * .8; |
| 221 | float3 octant = centerId + step(float3(.5), fract(p / cell)); |
| 222 | float3 bondCorner = octant * cell + atomOffset(octant, c); |
| 223 | bond = sdCapsule(p, bondCorner, center, .06); |
| 224 | kind = centerAtom < cornerAtom ? 1. : 0.; |
| 225 | return min(min(cornerAtom, centerAtom), bond); |
| 226 | } |
| 227 | float3 atomLattice(Ctx c, constant NativeFrame &f) { |
| 228 | float3 ro = float3(1 + sin(c.t * .11) * .2, cos(c.t * .09) * .2, c.travel * 2.2); |
| 229 | float3 rd = cameraRay(c.uv, float3(sin(c.t * .06) * .4, cos(c.t * .05) * .3, 1), float3(0, 1, 0), 1.25); |
| 230 | rd.xy = rot(rd.xy, c.t * .03); |
| 231 | float travel = 0, kind = 0, bond = 9; |
| 232 | bool hit = false; |
| 233 | for (int i = 0; i < 90; ++i) { |
| 234 | float d = atomField(ro + rd * travel, c, kind, bond); |
| 235 | if (d < .002) { hit = true; break; } |
| 236 | travel += d * .9; |
| 237 | if (travel > 30) break; |
| 238 | } |
| 239 | float3 background = deep(.5, f) * .06; |
| 240 | if (!hit) return background; |
| 241 | float3 p = ro + rd * travel; |
| 242 | float e = .003, k2, b2; |
| 243 | float d0 = atomField(p, c, k2, b2); |
| 244 | float3 n = normalFrom(p, e, d0, atomField(p + float3(e, 0, 0), c, k2, b2), atomField(p + float3(0, e, 0), c, k2, b2), atomField(p + float3(0, 0, e), c, k2, b2)); |
| 245 | float isBond = bond < .003 ? 1. : 0.; |
| 246 | float3 light = normalize(float3(-.4, .7, -.5)); |
| 247 | float diffuse = max(dot(n, light), 0.f); |
| 248 | float specular = pow(max(dot(reflect(rd, n), light), 0.f), 40.f); |
| 249 | float rim = pow(1 - max(dot(-rd, n), 0.f), 3.f); |
| 250 | float3 albedo = isBond > .5 ? paint(.5, f) * .5 : paint(kind > .5 ? .85 : .2 + hash31(floor(p / 2)) * .3, f); |
| 251 | float flash = isBond * c.hit * pow(.5 + .5 * sin(p.z * 2 - c.m * 8), 8.f) * 2; |
| 252 | float3 color = albedo * (.1 + diffuse * .8) + paint(.95, f) * (specular * .9 + flash) + paint(.7, f) * rim * .5; |
| 253 | return mix(background, color, exp(-travel * (.04 + c.fog * .07))); |
| 254 | } |
| 255 | |
| 256 | // ------------------------------------------------------------------ 98 Synth Horizon |
| 257 | // A neon grid racing toward a striped sun behind wireframe mountains. Bass rolls waves |
| 258 | // through the floor toward the viewer, highs light the sun's bands, attacks flash the grid. |
| 259 | float3 synthHorizon(Ctx c, constant NativeFrame &f) { |
| 260 | float horizon = .08 * c.extent.y; |
| 261 | float3 color = 0.f; |
| 262 | if (c.uv.y > horizon) { |
| 263 | float h = c.uv.y - horizon; |
| 264 | color = mix(paint(.75, f) * .55, deep(.2, f) * .15, smoothstep(0.f, 1.2f, h)); |
| 265 | float2 sunCenter = float2(0, horizon + .42); |
| 266 | float sunDistance = length(c.uv - sunCenter); |
| 267 | float stripes = step(.5, fract((c.uv.y - sunCenter.y) * 14 - c.t * .3 + c.m * .2)) + step(0.f, c.uv.y - sunCenter.y + .05); |
| 268 | float sun = (1 - smoothstep(.48f, .5f, sunDistance)) * min(stripes, 1.f); |
| 269 | color = mix(color, mix(paint(.95, f), paint(.6, f), smoothstep(.4f, -.4f, c.uv.y - sunCenter.y)) * (1.3 + c.b.w * .8), sun); |
| 270 | color += paint(.85, f) * exp(-sunDistance * 3) * .25; |
| 271 | // Mountains. |
| 272 | float ridge = horizon + .18 * fbm2(float2(c.uv.x * 1.6 + c.travel * .02, 1), 4) + .1 * abs(sin(c.uv.x * 2.3)); |
| 273 | float mountain = step(c.uv.y, ridge); |
| 274 | float wire = exp(-abs(fract(c.uv.x * 12 + fbm2(float2(c.uv.x * 4, c.uv.y * 6), 2)) - .5) * 30) * .4 + exp(-abs(c.uv.y - ridge) * 120); |
| 275 | color = mix(color, deep(.2, f) * .1 + paint(.8, f) * wire * .6, mountain); |
| 276 | float stars = pow(hash21(floor(c.uv * 90)), 50.f) * smoothstep(.3f, .8f, h); |
| 277 | color += paint(.9, f) * stars; |
| 278 | } else { |
| 279 | float depth = .5 / max(horizon - c.uv.y, 1e-3f); |
| 280 | float2 floorUV = float2(c.uv.x * depth, depth + c.travel * 1.6 + c.t * .4); |
| 281 | float wave = (c.b.x * .5 + c.hit * .3) * sin(floorUV.y * .8 - c.m * 4) * exp(-depth * .08); |
| 282 | floorUV.x += wave * .4; |
| 283 | float2 grid = abs(fract(floorUV * float2(1.2, 1) / c.size) - .5); |
| 284 | float2 width = fwidth(floorUV * float2(1.2, 1) / c.size) * 1.2; |
| 285 | float lines = max(1 - smoothstep(.0f, 1.f, grid.x / max(width.x, .02f)), 1 - smoothstep(.0f, 1.f, grid.y / max(width.y, .02f))); |
| 286 | float glow = exp(-min(grid.x, grid.y) * 8) * .3; |
| 287 | float fade = exp(-depth * .05); |
| 288 | float ripple = c.hit * exp(-abs(fract(floorUV.y * .08 - c.m * .5) - .5) * 14); |
| 289 | color = deep(.25, f) * .08 + paint(.65 + wave * .2, f) * (lines * (1.1 + ripple * 1.5) + glow) * fade; |
| 290 | color += paint(.85, f) * exp(-(horizon - c.uv.y) * 12) * .5; |
| 291 | } |
| 292 | return color; |
| 293 | } |
| 294 | |
| 295 | #define STRUCTURE_EFFECTS(X) X(chromeReef) X(pistonHall) X(spaceFrame) X(atomLattice) X(synthHorizon) |
| 296 | STRUCTURE_EFFECTS(NATIVE_EFFECT) |