PulseCanvas / app · pulsecanvas.ai / Sources / PulseCanvas / Resources / NativeDrive.metal
| 1 | |
| 2 | // Independently travelling reflections on existing scene surfaces. Their four |
| 3 | // spatial phases follow different frequency bands; attacks launch narrow wavefronts. |
| 4 | // Only material samples change: the camera and horizon remain in place. |
| 5 | |
| 6 | // PulseCanvas native designs: driving scenes. Original procedural code; see NativeCommon.metal. |
| 7 | // Roads are pseudo-3D. A camera at eye height looks along a bending road: ground pixels get a |
| 8 | // depth from their distance below the horizon, vertical planes (rails, facades, cable planes) |
| 9 | // are solved per pixel, and roadside objects are projected from world-space instances whose |
| 10 | // ids stay stable as the travel distance grows, so nothing pops when a period wraps. |
| 11 | |
| 12 | // ---------------------------------------------------------------- shared helpers |
| 13 | // Lateral offset of a road centre along world distance s, its slope and its curvature. |
| 14 | |
| 15 | |
| 16 | // Road centre at depth z, seen from a camera at s0 that looks along the road's tangent. |
| 17 | |
| 18 | // The same with the camera's own bend and slope computed once per pixel. |
| 19 | struct DrvRoad { float4 k; float s0; float base; float slope; }; |
| 20 | |
| 21 | |
| 22 | // Depth at which the ray through screen x meets the vertical plane at road-relative lateral x. |
| 23 | // Damped fixed-point iteration; returns -1 where it does not settle (far, near the vanishing |
| 24 | // point), so callers simply skip the plane there. |
| 25 | |
| 26 | // Anti-aliased stripe of half width w around d = 0, given the pixel footprint aa. |
| 27 | float drvStripe(float d, float w, float aa) { |
| 28 | aa = max(aa, 1e-5f); |
| 29 | return (1 - smoothstep(w - aa, w + aa, abs(d))) * min(1.f, w / aa); |
| 30 | } |
| 31 | // Anti-aliased dashes centred on multiples of period; fades to the duty cycle when tiny. |
| 32 | |
| 33 | |
| 34 | // The band an element listens to, chosen by its integer id. |
| 35 | |
| 36 | // Point stars: one candidate per cell, a soft dot about a pixel wide. |
| 37 | |
| 38 | // Rear view of a car in metres, origin at the rear centre on the road. |
| 39 | // x = body coverage, y = tail lamps, z = rear glass. |
| 40 | |
| 41 | // Skyline of towers along a horizon: x = silhouette coverage, y = lit window amount. |
| 42 | |
| 43 | |
| 44 | // ---------------------------------------------------------------- 134 Night Highway |
| 45 | // A divided highway at night: median lamp masts throw sodium pools, tail lights pull away |
| 46 | // and oncoming headlights rush past on the far carriageway, the skyline glows ahead. |
| 47 | |
| 48 | |
| 49 | // ---------------------------------------------------------------- 135 Neon Boulevard |
| 50 | // Neon facade at (along, height) on one side of the street. blur widens the tubes for |
| 51 | // reflections in the wet asphalt. |
| 52 | |
| 53 | |
| 54 | // ---------------------------------------------------------------- 136 Desert Interstate |
| 55 | |
| 56 | |
| 57 | // ---------------------------------------------------------------- 137 Mountain Switchbacks |
| 58 | // Serpentine road: straight rows across the slope joined by hairpins. Returns distance to |
| 59 | // the centreline, arc length along the road and a signed offset (positive = outer/downhill). |
| 60 | |
| 61 | |
| 62 | // Pine crown height above p on a jittered grid (2x2 neighbours, tallest wins). |
| 63 | |
| 64 | // A chase view from above over hairpin bends cut into a forested slope at dusk. The camera |
| 65 | // swings with the car through each hairpin; mist pools in the valley below. Bass runs the |
| 66 | // rail reflectors in sequence, attacks flash the reflectors next to the car. |
| 67 | |
| 68 | |
| 69 | // ---------------------------------------------------------------- 138 Sodium Tunnel |
| 70 | // First depth where a camera ray (lateral/vertical slope per metre) leaves a tube whose |
| 71 | // centre follows the road bend: geometric march, then bisection. |
| 72 | |
| 73 | |
| 74 | // ---------------------------------------------------------------- 139 Coastal Cruise |
| 75 | |
| 76 | |
| 77 | // ---------------------------------------------------------------- 140 Long Exposure |
| 78 | // A hillside view of an S-shaped freeway at night as a long exposure: every lane is a |
| 79 | // continuous ribbon, warm one way and cool the other, with brighter car heads riding the |
| 80 | // front of each streak over the speckled city grid. Bass thickens the ribbons, highs add |
| 81 | // sparkles, attacks send a bright pulse down the fast lanes. |
| 82 | |
| 83 | |
| 84 | // ---------------------------------------------------------------- 141 Light Cycles |
| 85 | constant uint drvCyclePatterns[8] = {0x0Fu, 0x33u, 0x55u, 0x3Cu, 0x5Au, 0x66u, 0x69u, 0x96u}; |
| 86 | // Lateral lane (in steps) of a bike at the start of cycle n: balanced 8-step blocks of |
| 87 | // right-angle jogs, so every position has a closed form. |
| 88 | float drvCycleX(float n, float seed) { |
| 89 | float block = floor(n / 8); |
| 90 | uint i = uint(n - block * 8); |
| 91 | uint mask = drvCyclePatterns[uint(hash11(block * 7.3 + seed) * 7.99)]; |
| 92 | if (hash11(block * 3.7 + seed * 1.3) > .5) mask ^= 0xFFu; |
| 93 | uint ones = popcount(mask & ((1u << i) - 1u)); |
| 94 | return float(2 * int(ones) - int(i)); |
| 95 | } |
| 96 | float3 neonTrails(Ctx c, constant NativeFrame &f) { |
| 97 | const float Lf = 9, M = 3.2, Lc = Lf + M, wallH = 1.1, lens = 1.5; |
| 98 | float progress = c.travel * 9 + c.t * 3 + c.m * 8; |
| 99 | float pl = progress - 7; |
| 100 | float nl = floor(pl / Lc); |
| 101 | float camX = M * mix(drvCycleX(nl, 0), drvCycleX(nl + 1, 0), smoothstep(.2f, 1.f, (pl - nl * Lc) / Lc)); |
| 102 | float3 ro = float3(camX, 5.4, progress * Lf / Lc - 12); |
| 103 | float3 forward = normalize(float3(0, -.42, 1)); |
| 104 | float3 rd = cameraRay(c.uv, forward, float3(0, 1, 0), lens); |
| 105 | float3 camU = normalize(cross(float3(0, 1, 0), forward)), camV = cross(forward, camU); |
| 106 | float tFloor = rd.y < 0 ? ro.y / -rd.y : 1e5; |
| 107 | float tTop = rd.y < 0 ? (ro.y - wallH) / -rd.y : 1e5; |
| 108 | float3 G = ro + rd * tFloor; |
| 109 | float3 T = ro + rd * tTop; |
| 110 | float tBest = tFloor; |
| 111 | float3 hitColor = 0; |
| 112 | float3 floorGlow = 0, halo = 0; |
| 113 | float width = .14 * c.size; |
| 114 | // Ground footprint of the part of this ray that can see a wall, its glow or its halo. |
| 115 | float tHalo = rd.y < 0 ? max(ro.y - wallH - 2.5, 0.f) / -rd.y : 0; |
| 116 | float2 traceLo = min((ro + rd * tHalo).xz, G.xz) - 5, traceHi = max((ro + rd * tHalo).xz, G.xz) + 5; |
| 117 | for (int b = 0; b < 5; ++b) { |
| 118 | float fb = float(b); |
| 119 | float home = (b == 0 ? 0 : (b % 2 == 1 ? -1 : 1) * (floor((fb + 1) * .5) * 3)) * M; |
| 120 | float pb = progress * (1 + (b == 0 ? 0 : .03 * (fb - 2.5))) + (b == 0 ? 0 : fb * 7 - 4); |
| 121 | float nb = floor(pb / Lc); |
| 122 | float rb = pb - nb * Lc; |
| 123 | float seed = fb * 13.1; |
| 124 | float3 tone = paint(fb * .25, f); |
| 125 | float boost = 1 + c.b[b % 4] * 1.6; |
| 126 | for (int k = 0; k < 6 && rd.y < 0; ++k) { |
| 127 | float n = nb - float(k); |
| 128 | float xn = home + M * drvCycleX(n, seed), xn1 = home + M * drvCycleX(n + 1, seed); |
| 129 | float z0 = n * Lf; |
| 130 | float progStart = n * Lc; |
| 131 | if (max(xn, xn1) < traceLo.x || min(xn, xn1) > traceHi.x || z0 + Lf < traceLo.y || z0 > traceHi.y) continue; |
| 132 | for (int part = 0; part < 2; ++part) { |
| 133 | float2 a, bb; |
| 134 | float len; |
| 135 | if (part == 0) { |
| 136 | float reach = k == 0 ? min(rb, Lf) : Lf; |
| 137 | a = float2(xn, z0); bb = float2(xn, z0 + reach); len = reach; |
| 138 | } else { |
| 139 | if (k == 0 && rb < Lf) continue; |
| 140 | float reach = k == 0 ? (rb - Lf) / M : 1; |
| 141 | a = float2(xn, z0 + Lf); bb = float2(mix(xn, xn1, reach), z0 + Lf); len = M * reach; |
| 142 | } |
| 143 | float startProg = progStart + (part == 0 ? 0 : Lf); |
| 144 | // Floor glow from this wall. |
| 145 | float2 ba = bb - a; |
| 146 | float hG = clamp(dot(G.xz - a, ba) / max(dot(ba, ba), 1e-4f), 0.f, 1.f); |
| 147 | float age = pb - (startProg + hG * len); |
| 148 | float bright = exp(-max(age, 0.f) / 36) * (1 - smoothstep(3.6 * Lc, 5 * Lc, age)) * boost; |
| 149 | float dG = length(G.xz - a - ba * hG); |
| 150 | floorGlow += tone * bright * exp(-dG * 1.4) * .5; |
| 151 | // Top face. |
| 152 | float hT = clamp(dot(T.xz - a, ba) / max(dot(ba, ba), 1e-4f), 0.f, 1.f); |
| 153 | float dT = length(T.xz - a - ba * hT); |
| 154 | float ageT = pb - (startProg + hT * len); |
| 155 | float brightT = exp(-max(ageT, 0.f) / 36) * (1 - smoothstep(3.6 * Lc, 5 * Lc, ageT)) * boost; |
| 156 | if (dT < width && tTop < tBest) { tBest = tTop; hitColor = tone * brightT * 2.2 + paint(.95, f) * brightT * .6; } |
| 157 | // Side face (vertical plane). |
| 158 | float denom = part == 0 ? rd.x : rd.z; |
| 159 | float tPlane = abs(denom) > 1e-5 ? (part == 0 ? (a.x - ro.x) / denom : (a.y - ro.z) / denom) : -1.f; |
| 160 | if (tPlane > 0) { |
| 161 | float3 P = ro + rd * tPlane; |
| 162 | float along = part == 0 ? P.z : P.x; |
| 163 | float lo = part == 0 ? min(a.y, bb.y) : min(a.x, bb.x), hi = part == 0 ? max(a.y, bb.y) : max(a.x, bb.x); |
| 164 | float h = P.y / wallH; |
| 165 | float ageP = pb - (startProg + (part == 0 ? P.z - a.y : abs(P.x - a.x))); |
| 166 | float brightP = exp(-max(ageP, 0.f) / 36) * (1 - smoothstep(3.6 * Lc, 5 * Lc, ageP)) * boost; |
| 167 | if (along > lo && along < hi && h > 0 && h < 1 && tPlane < tBest) { |
| 168 | tBest = tPlane; |
| 169 | hitColor = tone * brightP * (.35 + 1.3 * pow(h, 3.f)) + paint(.95, f) * brightP * smoothstep(.9f, 1.f, h) * .8; |
| 170 | } |
| 171 | float out = length(float2(max(max(lo - along, along - hi), 0.f), max(max(-h, h - 1), 0.f) * wallH)); |
| 172 | halo += tone * brightP * exp(-out * 2.2) * .12 * step(hi - lo, 1e3); |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | // Bike head glow. |
| 177 | float xh = home + M * mix(drvCycleX(nb, seed), drvCycleX(nb + 1, seed), saturate((rb - Lf) / M)); |
| 178 | float3 headP = float3(xh, .6, nb * Lf + min(rb, Lf)); |
| 179 | float3 v = headP - ro; |
| 180 | float zc = dot(v, forward); |
| 181 | if (zc > .5) { |
| 182 | float2 sp = lens * float2(dot(v, camU), dot(v, camV)) / zc; |
| 183 | float r = length(c.uv - sp); |
| 184 | halo += tone * (glow(r, .5 / zc) * 2 + exp(-r * zc * 1.2) * .5) * boost; |
| 185 | if (b == 0) halo += paint(.95, f) * c.hit * (exp(-abs(length(G.xz - headP.xz) - c.hit * 6) * 2) * .8 + glow(r, 1.5 / zc) * 1.5); |
| 186 | } |
| 187 | } |
| 188 | float3 color; |
| 189 | if (tBest < tFloor - 1e-3) color = hitColor; |
| 190 | else if (rd.y < 0) { |
| 191 | float fp = c.px * tFloor / lens / max(-rd.y, .15f); |
| 192 | float2 cell = abs(fract(G.xz / 4 + .5) - .5) * 4; |
| 193 | float grid = max(drvStripe(cell.x, .03, fp), drvStripe(cell.y, .03, fp)); |
| 194 | color = deep(.92, f) * .05 + paint(.05, f) * grid * .22 + floorGlow; |
| 195 | } else color = 0; |
| 196 | float fogK = exp(-min(tBest, tFloor) * (.008 + c.fog * .02)); |
| 197 | float3 sky = deep(.95, f) * .12 + paint(.55, f) * .25 * exp(-abs(rd.y) * 14) + paint(.85, f) * .03 * step(.5, fract(rd.y * 60)); |
| 198 | color = mix(sky, color, rd.y < 0 ? fogK : 0) + halo; |
| 199 | return color; |
| 200 | } |
| 201 | |
| 202 | // ---------------------------------------------------------------- 142 Rally Forest |
| 203 | // Chasing a rally car down a forest track: trunks whip past in parallax rows, sun shafts |
| 204 | // fall through the canopy and dust rolls back from the car. Bass kicks up the dust plume, |
| 205 | // highs glitter in the shafts, attacks throw mud from the rear wheels. |
| 206 | |
| 207 | |
| 208 | // ---------------------------------------------------------------- 143 Bridge Run |
| 209 | // Crossing a long suspension bridge at night: floodlit towers approach and pass, the main |
| 210 | // cables sweep down and up strung with lights, suspenders flick past and the skyline glows |
| 211 | // across the river. Bass runs a wave along the cable lights, highs twinkle the skyline, |
| 212 | // attacks brighten the tower beacons. |
| 213 | |
| 214 | |
| 215 | // ---------------------------------------------------------------- 144 Coaster Loop |
| 216 | // Track centreline at u from the sines/cosines of its four base phases (u * freq + phase), |
| 217 | // with a vertical loop and a corkscrew in every 330 m of layout. twist is the rails' extra |
| 218 | // roll through the corkscrew; bank is the gentle lean into turns. |
| 219 | constant float4 drvCoasterFreq = float4(.037, .089, .05, .12); |
| 220 | constant float4 drvCoasterPhase = float4(0, 1.3, .4, 2.1); |
| 221 | |
| 222 | // The second track weaves around the first's base path, crossing above and below it. |
| 223 | |
| 224 | |
| 225 | // A projected track sample: screen centre, screen offset to the right rail, depth, position. |
| 226 | struct DrvRail { float2 c; float2 o; float z; float u; }; |
| 227 | |
| 228 | // One span of twin rails, a quadratic from m0 to m1 bending toward the sample k between them |
| 229 | // (corner cutting keeps the curve smooth), drawn in three pieces with perspective width, a |
| 230 | // glossy top highlight and a glow halo. |
| 231 | |
| 232 | // A night ride on a light-show coaster: glossy twin rails outlined in palette light sweep |
| 233 | // far ahead as the track climbs, loops and corkscrews, while a second track weaves around |
| 234 | // it with its own lit train. Slim dark towers and crossties streak past and a park |
| 235 | // glitters far below. The camera banks gently over a level horizon. Bass chases the rail |
| 236 | // lights, highs sparkle them, attacks send surges of light racing along the rails. |
| 237 | |
| 238 | |
| 239 | // ---------------------------------------------------------------- 145 Hover Lanes |
| 240 | |
| 241 | |
| 242 | // ---------------------------------------------------------------- 146 Bullet Train |
| 243 | // A golden-hour ride on a high-speed line, seen through the side window: a graded sky with |
| 244 | // a low sun, hills in aerial perspective, saturated field patches, a highway whose car |
| 245 | // lights streak by, lit farm windows and dark hedges and catenary poles whipping past in |
| 246 | // the foreground. Bass shakes the fields' colour bands, highs flash the distant lights, |
| 247 | // attacks sweep a tunnel portal's shadow across the near field. |
| 248 | |
| 249 | |
| 250 | // ---------------------------------------------------------------- 147 Apex Chase |
| 251 | |
| 252 | |
| 253 | // ---------------------------------------------------------------- 148 Snow Drive |
| 254 | |
| 255 | |
| 256 | // ---------------------------------------------------------------- 149 Dashboard Glow |
| 257 | constant int drvDigitMasks[10] = {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F}; |
| 258 | |
| 259 | // One round gauge: arcs, ticks, needle. value in 0..1; redline lights the last 15% of the sweep. |
| 260 | |
| 261 | // The night road through the windshield, out of focus. |
| 262 | |
| 263 | // Car interior at night: two glowing dials with a digital readout, the steering wheel in |
| 264 | // silhouette and the road ahead out of focus. Needles drift on their own and follow the |
| 265 | // bands with Chaos; attacks light the tachometer's rev-limit arc. |
| 266 | |
| 267 | |
| 268 | // ---------------------------------------------------------------- 150 Midnight Skyline |
| 269 | |
| 270 | |
| 271 | #define DRIVE_EFFECTS(X) X(neonTrails) |
| 272 | DRIVE_EFFECTS(NATIVE_EFFECT) |