PulseCanvas / app · pulsecanvas.ai / Sources / PulseCanvas / Resources / NativeNature.metal
| 1 | // Organic, landscape and volumetric designs. Original PulseCanvas procedural code. |
| 2 | // Every effect receives Ctx from NativeCommon.metal; audio terms are zero at zero Chaos. |
| 3 | |
| 4 | float3 natureNormal(float d0, float dx, float dy, float dz) { return normalize(float3(dx - d0, dy - d0, dz - d0) + 1e-6); } |
| 5 | |
| 6 | // Folding fractal tree in local coordinates; returns distance to the nearest branch and |
| 7 | // the depth of that branch. |
| 8 | |
| 9 | |
| 10 | // ------------------------------------------------------------------ 34 Electric Orchard |
| 11 | // Rows of luminous fractal trees recede across a dark orchard. Current climbs every tree |
| 12 | // in pulses; each tree pulses on its band and attacks flash the whole canopy. |
| 13 | |
| 14 | |
| 15 | // ------------------------------------------------------------------ 36 Graviton Petals |
| 16 | // A flight through a weightless garden of huge flowers that open and close. Each bloom |
| 17 | // opens on its band; attacks shiver the petals. |
| 18 | |
| 19 | |
| 20 | // ------------------------------------------------------------------ 42 Sonic Topography |
| 21 | // A living contour map seen from above. Glowing isolines wrap hills that rise and fall; |
| 22 | // each region's hills lift with their band, attacks ripple through the contours, and the |
| 23 | // map glides and slowly turns with Zoom speed. |
| 24 | float topographyHeight(float2 p, Ctx c) { |
| 25 | float h = fbm2(p * .35, 5) * 2.2; |
| 26 | float2 cell = floor(p * .25); |
| 27 | float band = c.b[int(hash21(cell) * 3.99)]; |
| 28 | h += band * .6 * smoothstep(.2f, 1.f, fbm2(p * .5 + 3, 2)); |
| 29 | h += c.hit * .08 * sin(length(p - float2(sin(c.m) * 3, c.travel * 1.5)) * 3 - c.m * 6); |
| 30 | return h + .15 * sin(p.x * .4 + c.t * .1) * sin(p.y * .3 - c.t * .08); |
| 31 | } |
| 32 | float3 sonicTopography(Ctx c, constant NativeFrame &f) { |
| 33 | float2 xz = rot(c.uv, c.t * .01) * 3.2 / c.size + float2(sin(c.t * .05) * 2, c.travel * 1.5); |
| 34 | float e = .02, h = topographyHeight(xz, c); |
| 35 | float2 slope = float2(topographyHeight(xz + float2(e, 0), c) - h, topographyHeight(xz + float2(0, e), c) - h) / e; |
| 36 | float3 n = normalize(float3(-slope, 1.2)); |
| 37 | float shade = .2 + .8 * max(dot(n, normalize(float3(-.5, .5, .7))), 0.f); |
| 38 | float levels = h * 10; |
| 39 | float w = fwidth(levels); |
| 40 | float toLevel = .5 - abs(fract(levels) - .5); |
| 41 | float contour = 1 - smoothstep(.015f, .015f + max(w * 1.5, .02f), toLevel); |
| 42 | float toMajor = .5 - abs(fract(h * 2) - .5); |
| 43 | float major = 1 - smoothstep(.01f, .01f + max(fwidth(h * 2) * 1.5, .015f), toMajor); |
| 44 | float2 gridLines = abs(fract(xz * .5) - .5); |
| 45 | float grid = (1 - smoothstep(.0f, .015f, min(gridLines.x, gridLines.y))) * .12; |
| 46 | float water = smoothstep(.75f, .7f, h); |
| 47 | float3 land = deep(.25 + h * .1, f) * .1 * shade + paint(.15 + h * .3, f) * .08 * shade; |
| 48 | float3 sea = deep(.6, f) * .08 + paint(.5, f) * .05 * (1 + sin(h * 40 - c.t)); |
| 49 | float3 color = mix(land, sea, water * .8); |
| 50 | color += paint(.25 + h * .25, f) * contour * .8 * (.5 + shade) + paint(.9, f) * major * 1.1 + paint(.6, f) * grid; |
| 51 | return color; |
| 52 | } |
| 53 | |
| 54 | // ------------------------------------------------------------------ 43 Prismatic Dunes |
| 55 | // A low flight over wind-rippled dunes at golden hour. Ridges split the light into |
| 56 | // prismatic sheen; bass lifts dune crests as gusts pass and highs blow sand off them. |
| 57 | float duneHeight(float2 p, Ctx c) { |
| 58 | float ridges = 0; |
| 59 | float2 q = p * .25; |
| 60 | ridges += (1 - abs(sin(q.x * 1.3 + sin(q.y * .7) * 1.5 + q.y * .3))) * 1.2; |
| 61 | ridges += (1 - abs(sin(q.x * 2.7 - q.y * .9 + 2))) * .4; |
| 62 | ridges += fbm2(q * 1.5, 3) * .5; |
| 63 | float gust = (c.b.x * 1.1 + c.hit * .8) * pow(.5 + .5 * sin(q.y * 1.4 - c.m * 2), 3.f); |
| 64 | float ripples = sin(p.x * 2.2 + p.y * .6 + fbm2(p * .4, 2) * 3) * .03; |
| 65 | return ridges * (1 + gust) + ripples; |
| 66 | } |
| 67 | float3 prismaticDunes(Ctx c, constant NativeFrame &f) { |
| 68 | c.travel += c.m * f.camera.w * .7; |
| 69 | float3 ro = float3(c.t * .2, 4.0, c.travel * 2.2); |
| 70 | float3 rd = cameraRay(c.uv, float3(.2, -.18, 1), float3(0, 1, 0), 1.5); |
| 71 | float3 sun = normalize(float3(-.6, .22, 1)); |
| 72 | float3 sky = mix(paint(.65, f) * 1.1, deep(.3, f) * .25, smoothstep(-.05f, .55f, rd.y)); |
| 73 | sky += paint(.82, f) * pow(max(dot(rd, sun), 0.f), 200.f) * 3 + paint(.85, f) * pow(max(dot(rd, sun), 0.f), 6.f) * .4; |
| 74 | float travel = 0; |
| 75 | bool hit = false; |
| 76 | for (int i = 0; i < 90; ++i) { |
| 77 | float3 p = ro + rd * travel; |
| 78 | float gap = p.y - duneHeight(p.xz / c.size, c); |
| 79 | if (gap < .006) { hit = true; break; } |
| 80 | travel += max(gap * .45, .015f); |
| 81 | if (travel > 60) break; |
| 82 | } |
| 83 | if (!hit) return sky; |
| 84 | float3 p = ro + rd * travel; |
| 85 | float2 xz = p.xz / c.size; |
| 86 | float e = .015, h = duneHeight(xz, c); |
| 87 | float3 n = normalize(float3(h - duneHeight(xz + float2(e, 0), c), e, h - duneHeight(xz + float2(0, e), c))); |
| 88 | float diffuse = max(dot(n, sun), 0.f); |
| 89 | float shadowSide = smoothstep(.0f, .25f, diffuse); |
| 90 | float prism = pow(1 - abs(dot(n, normalize(rd + sun))), 6.f); |
| 91 | float3 sand = mix(deep(.35, f) * .2, paint(.55 + h * .1, f), shadowSide) * (.25 + diffuse * .9); |
| 92 | sand += paint(.55 + .12 * sin(dot(n.xz, float2(1, .5)) * 2 + h), f) * prism * .45; |
| 93 | float drift = pow(fbm2(xz * 3 + float2(c.t * .8, 0), 3), 4.f) * smoothstep(.8f, 1.4f, h) * (.3 + c.b.w * 2 + c.hit * 3); |
| 94 | sand += paint(.95, f) * drift; |
| 95 | float ripples = .5 + .5 * sin(xz.x * 2.5 + xz.y * 1.5 - c.t - c.m * 3); |
| 96 | sand += paint(.48 + n.x * .16, f) * ripples * (c.b.y * .65 + c.hit * .7); |
| 97 | return mix(sky, sand, exp(-travel * (.012 + c.fog * .03))); |
| 98 | } |
| 99 | |
| 100 | // ------------------------------------------------------------------ 44 Honeycomb Tide |
| 101 | // A flight low over golden comb. Honey in every cell rises and falls like a tide; cells |
| 102 | // fill on their own band and attacks make the honey surfaces shimmer. |
| 103 | |
| 104 | |
| 105 | // ------------------------------------------------------------------ 45 Nebula Nursery |
| 106 | // A slow flight into an emission nebula where new stars ignite. Bands light their own |
| 107 | // layers of gas; attacks flare the young stars' diffraction spikes. |
| 108 | |
| 109 | |
| 110 | // ------------------------------------------------------------------ 46 Crystal Orchard |
| 111 | // A glide across a plain of quartz clusters. Hexagonal crystals catch the light and glow |
| 112 | // from within; each cluster grows on its band and attacks spark across the facets. |
| 113 | float crystalField(float3 p, Ctx c, thread float &glowOut) { |
| 114 | // Only the two nearest cells on each axis can hold the closest crystal. |
| 115 | float spacing = 2.4; |
| 116 | float2 cellPosition = p.xz / spacing; |
| 117 | float2 base = floor(cellPosition - .5); |
| 118 | float d = p.y; |
| 119 | glowOut = 0; |
| 120 | for (int y = 0; y <= 1; ++y) for (int x = 0; x <= 1; ++x) { |
| 121 | float2 g = base + float2(x, y); |
| 122 | float3 seed = hash33(float3(g, 3)); |
| 123 | float band = c.b[int(seed.x * 3.99)]; |
| 124 | for (int k = 0; k < 2; ++k) { |
| 125 | float3 s2 = hash33(float3(g, float(k) + 7)); |
| 126 | float2 center = float2((g.x + .5 + (s2.x - .5) * .5) * spacing, (g.y + .5 + (s2.y - .5) * .5) * spacing); |
| 127 | // A crystal (radius ≤ .32, ≤ 3.3 tall, leaning ≤ .35 rad) lies within 1.5 of its base axis, |
| 128 | // so one farther away than the current nearest cannot be the nearest surface. |
| 129 | if (length(p.xz - center) - 1.5 > d) continue; |
| 130 | float3 q = p - float3(center.x, 0, center.y); |
| 131 | q.xy = rot(q.xy, (s2.z - .5) * .7); |
| 132 | float height = min((.6 + s2.z * 1.1) * (1 + band * .45) * (.85 + .15 * sin(c.t * .3 + seed.y * 9)), 2.6f); |
| 133 | float radius = .2 + s2.y * .12; |
| 134 | float prism = max(sdHex(q.xz, radius), max(-q.y, q.y - height - radius * 1.8 + length(q.xz) * 1.8)); |
| 135 | if (prism < d) { d = prism; glowOut = seed.z; } |
| 136 | } |
| 137 | } |
| 138 | return d; |
| 139 | } |
| 140 | float3 crystalOrchard(Ctx c, constant NativeFrame &f) { |
| 141 | // The glide over the orchard speeds up with the music clock. |
| 142 | float3 ro = float3(sin(c.t * .06) * 1.5, 3.4, c.travel * 1.6 + c.m * .5); |
| 143 | float3 rd = cameraRay(c.uv, float3(0, -.45, 1), float3(0, 1, 0), 1.35); |
| 144 | float travel = 0, glow = 0; |
| 145 | bool hit = false; |
| 146 | for (int i = 0; i < 64; ++i) { |
| 147 | float3 p = ro + rd * travel; |
| 148 | float d = crystalField(p, c, glow); |
| 149 | if (d < .002 * (1 + travel * .3)) { hit = true; break; } |
| 150 | // Above the tallest crystal the field is exact, so take long steps. |
| 151 | travel += min(d * .75, p.y > 3.1 ? 1.5f : .6f); |
| 152 | if (travel > 35) break; |
| 153 | } |
| 154 | float3 sky = mix(paint(.75, f) * .3, deep(.4, f) * .08, smoothstep(-.1f, .5f, rd.y)); |
| 155 | if (!hit) return sky; |
| 156 | float3 p = ro + rd * travel; |
| 157 | float e = .002, g2; |
| 158 | float d0 = crystalField(p, c, g2); |
| 159 | float3 n = natureNormal(d0, crystalField(p + float3(e, 0, 0), c, g2), crystalField(p + float3(0, e, 0), c, g2), crystalField(p + float3(0, 0, e), c, g2)); |
| 160 | float3 light = normalize(float3(.4, .7, -.3)); |
| 161 | float diffuse = max(dot(n, light), 0.f); |
| 162 | float fresnel = pow(1 - max(dot(-rd, n), 0.f), 3.f); |
| 163 | float3 color; |
| 164 | if (p.y < .01) { |
| 165 | float3 reflected = mix(paint(.75, f) * .3, deep(.4, f) * .08, smoothstep(-.1f, .5f, reflect(rd, n).y)); |
| 166 | // The floor catches the light of nearby crystals as they glow on their bands. |
| 167 | float floorBand = c.b[int(hash21(floor(p.xz * .5)) * 3.99)]; |
| 168 | color = deep(.3, f) * .08 * (.6 + .4 * fbm2(p.xz * 3, 3)) + reflected * .3 + paint(.6, f) * floorBand * .12; |
| 169 | } else { |
| 170 | // Each crystal cluster glows from within on its own band. |
| 171 | float crystalBand = c.b[int(hash21(floor(p.xz * .5)) * 3.99)]; |
| 172 | float inner = (.5 + .5 * sin(p.y * 6 - c.t - c.m * 2 + glow * 20)) * (1 + c.hit * 1.4 * step(.4, glow)) * (1 + crystalBand * 2.2); |
| 173 | color = paint(.3 + glow * .5, f) * (.12 + diffuse * .4 + inner * .45) + paint(.95, f) * (fresnel * .8 + pow(max(dot(reflect(rd, n), light), 0.f), 40.f) * (1 + c.hit * 2)); |
| 174 | } |
| 175 | return mix(sky, color, exp(-travel * (.035 + c.fog * .05))); |
| 176 | } |
| 177 | |
| 178 | // ------------------------------------------------------------------ 48 Velvet Nebula |
| 179 | // Towering dust pillars stand against a glowing nebula, their rims lit from behind. |
| 180 | // Layers drift past; bands brighten the emission behind each pillar and attacks light |
| 181 | // the tips where stars are being born. |
| 182 | float3 velvetNebula(Ctx c, constant NativeFrame &f) { |
| 183 | float3 color = 0.f; |
| 184 | float2 glowCenter = float2(c.extent.x * .4, c.extent.y * .6); |
| 185 | float2 flow = float2(fbm2(c.uv * .8 + c.t * .03, 3), fbm2(c.uv * .8 - c.t * .025 + 4, 3)) - .5; |
| 186 | float3 emission = mix(paint(.3, f), paint(.85, f), exp(-length(c.uv - glowCenter) * .9)) * (.4 + .6 * fbm2(c.uv * 1.5 + flow * 1.2 + c.t * .04, 5)); |
| 187 | emission *= 1 + c.b.y * .8; |
| 188 | color = emission; |
| 189 | color += paint(.95, f) * pow(hash21(floor(c.uv * 120)), 60.f); |
| 190 | const int count = 4; |
| 191 | for (int k = 0; k < count; ++k) { |
| 192 | float2 slot = layerSlot(k, count, c.travel * 0.06); |
| 193 | float phase = slot.x, id = slot.y, fade = layerFade(phase); |
| 194 | float scale = layerScale(phase, 1.6) * .9 / c.size; |
| 195 | float2 p = c.uv * scale + float2(id * 2.7 + c.t * .03 * (1 + phase), 0); |
| 196 | float column = floor(p.x / 1.4 + .5); |
| 197 | float local = p.x - column * 1.4; |
| 198 | float seed = hash21(float2(column, id)); |
| 199 | float top = -.2 + seed * 1.1 + .15 * sin(c.t * .05 + seed * 9); |
| 200 | float wobble = fbm2(float2(p.y * 2 - c.t * .05, column * 3 + id), 4) - .5 + .06 * sin(c.t * .3 + column + p.y * 2); |
| 201 | float width = (.28 + seed * .2) * (1 - smoothstep(top - .6, top + .1, p.y) * .6); |
| 202 | float pillar = abs(local + wobble * .35) - width; |
| 203 | pillar = max(pillar, p.y - top - fbm2(float2(local * 4, id), 3) * .3); |
| 204 | float edge = c.px * scale * 2; |
| 205 | float body = (1 - smoothstep(-edge, edge, pillar)) * fade; |
| 206 | float rim = exp(-abs(pillar) * 25) * smoothstep(.1f, -.3f, p.y - top) * 0 + exp(-abs(pillar) * 30); |
| 207 | float band = c.b[int(seed * 3.99)]; |
| 208 | float tip = exp(-length(float2(local, p.y - top)) * 6) * (.4 + c.hit * 2 + band); |
| 209 | float3 dust = deep(.15 + seed * .1, f) * .05 * (.6 + fbm2(p * 6, 4)); |
| 210 | color = mix(color, dust, body * mix(.7, 1., phase)); |
| 211 | color += paint(.8, f) * rim * .5 * fade + paint(.95, f) * tip * .4 * fade; |
| 212 | } |
| 213 | return color; |
| 214 | } |
| 215 | |
| 216 | // ------------------------------------------------------------------ 49 Arc Foundry |
| 217 | // A dark steel foundry floor lit by welding arcs. Showers of sparks fly, fall and bounce; |
| 218 | // each arc flickers on its band and attacks throw bigger showers. |
| 219 | |
| 220 | |
| 221 | // ------------------------------------------------------------------ 51 Paper Cascades |
| 222 | // Layered paper-cut hills drift toward the viewer, each casting a soft shadow on the |
| 223 | // one behind. Ridges carry tiny cut-out trees; bands lift their own hills and attacks |
| 224 | // flutter the paper edges. |
| 225 | |
| 226 | |
| 227 | // ------------------------------------------------------------------ 53 Fractal Canopy |
| 228 | // Looking up through a forest canopy: fractal branches against a bright sky, clusters of |
| 229 | // leaves and sun rays through the gaps. Bass sends gusts through the branches; bands |
| 230 | // rustle their own trees and attacks flash the sun through the leaves. |
| 231 | |
| 232 | |
| 233 | // ------------------------------------------------------------------ 58 Kelp Cathedral |
| 234 | // A kelp forest: tall swaying stalks carrying long rippled blades in deep layers, god |
| 235 | // rays from the surface and drifting marine snow. Bands sway their own stalks; attacks |
| 236 | // shimmer the light shafts. |
| 237 | |
| 238 | |
| 239 | // ------------------------------------------------------------------ 59 Solar Filigree |
| 240 | // The curved limb of a star fills the lower canvas: boiling granulation, a glowing |
| 241 | // chromosphere and loops of plasma arching into black space, threaded with flowing |
| 242 | // filaments. Each loop brightens on its band, attacks flare them higher, and Zoom speed |
| 243 | // turns the star beneath the viewer. |
| 244 | |
| 245 | |
| 246 | // ------------------------------------------------------------------ 77 Zen Rake |
| 247 | // A raked gravel garden seen from above. Grooves flow around moss-ringed stones; the |
| 248 | // rake slowly redraws them. Bass sends ripples outward from each stone; highs glint on |
| 249 | // the gravel. |
| 250 | |
| 251 | |
| 252 | // ------------------------------------------------------------------ 93 Mitosis Garden |
| 253 | // A living culture under phase-contrast light: membranes, nuclei and organelles drift |
| 254 | // in two focal planes, and cells pinch apart as they divide. Each cell swells on its |
| 255 | // band; attacks quicken the cytoplasm. |
| 256 | |
| 257 | |
| 258 | // ------------------------------------------------------------------ 94 Neural Bloom |
| 259 | // Large neurons glow in shallow focus: bright somas with nuclei, tapering dendrites and |
| 260 | // a few long axons. Action potentials race along the axons; each neuron fires at a rate |
| 261 | // set by its band and attacks trigger cascades. The far layer is soft, like microscopy. |
| 262 | |
| 263 | |
| 264 | // ------------------------------------------------------------------ 95 Jellyfish Abyss |
| 265 | // Bioluminescent jellyfish drift through the deep in layers. Bells contract and trail |
| 266 | // rippling tentacles; each jelly pulses on its band and attacks make them contract. |
| 267 | |
| 268 | |
| 269 | // ------------------------------------------------------------------ 96 Glacier Rift |
| 270 | // A flight down a crevasse of translucent glacier ice: banded walls, glowing fractures |
| 271 | // and blowing snow. Bands light their own fracture families; attacks shimmer the ice. |
| 272 | |
| 273 | |
| 274 | // ------------------------------------------------------------------ 97 Firefly Meadow |
| 275 | // A moonlit meadow. Layers of tall grass sway toward the viewer while fireflies blink |
| 276 | // and drift; each firefly blinks on its band and attacks send a wave of light through |
| 277 | // the swarm. |
| 278 | |
| 279 | |
| 280 | #define NATURE_EFFECTS(X) X(sonicTopography) X(prismaticDunes) X(crystalOrchard) X(velvetNebula) |
| 281 | NATURE_EFFECTS(NATIVE_EFFECT) |