PulseCanvas / app · pulsecanvas.ai / Sources / PulseCanvas / Resources / NativeExpansionAtmospheric.metal
| 1 | #include <metal_stdlib> |
| 2 | using namespace metal; |
| 3 | |
| 4 | struct StudyFrame { |
| 5 | float phase; |
| 6 | float beat; |
| 7 | float travel; |
| 8 | float scale; |
| 9 | float fog; |
| 10 | }; |
| 11 | |
| 12 | constant float studyTurn = 6.28318530718f; |
| 13 | |
| 14 | constant float flightLength = 16.0f; |
| 15 | |
| 16 | static float repeatDepth(float depth, float period) { |
| 17 | return depth - floor(depth / period) * period; |
| 18 | } |
| 19 | |
| 20 | static float lattice(float3 cell) { |
| 21 | cell = fract(cell * 0.1031f); |
| 22 | cell += dot(cell, cell.yzx + 33.33f); |
| 23 | return fract((cell.x + cell.y) * cell.z); |
| 24 | } |
| 25 | |
| 26 | static float noise3D(float3 position, float depthCells) { |
| 27 | float3 cell = floor(position); |
| 28 | float3 blend = fract(position); |
| 29 | blend = blend * blend * (3.0f - 2.0f * blend); |
| 30 | |
| 31 | cell.z = repeatDepth(cell.z, depthCells); |
| 32 | float3 farCell = float3(cell.xy, repeatDepth(cell.z + 1.0f, depthCells)); |
| 33 | float nearLayer = mix( |
| 34 | mix(lattice(cell), lattice(cell + float3(1, 0, 0)), blend.x), |
| 35 | mix(lattice(cell + float3(0, 1, 0)), lattice(cell + float3(1, 1, 0)), blend.x), |
| 36 | blend.y |
| 37 | ); |
| 38 | float farLayer = mix( |
| 39 | mix(lattice(farCell), lattice(farCell + float3(1, 0, 0)), blend.x), |
| 40 | mix(lattice(farCell + float3(0, 1, 0)), lattice(farCell + float3(1, 1, 0)), blend.x), |
| 41 | blend.y |
| 42 | ); |
| 43 | return mix(nearLayer, farLayer, blend.z); |
| 44 | } |
| 45 | |
| 46 | // Overlapping randomized spheres form round lobes before finer noise erodes |
| 47 | // them. Radii plus center jitter stay below one cell, so eight nodes suffice. |
| 48 | static float billowField(float3 position, float depthCells) { |
| 49 | float3 cell = floor(position); |
| 50 | float3 local = fract(position); |
| 51 | float surface = -0.13f; |
| 52 | for (uint corner = 0; corner < 8; ++corner) { |
| 53 | float3 offset = float3(corner & 1, (corner >> 1) & 1, corner >> 2); |
| 54 | float3 node = cell + offset; |
| 55 | node.z = repeatDepth(node.z, depthCells); |
| 56 | float3 random = float3(lattice(node), lattice(node + 13.1f), lattice(node - 17.4f)); |
| 57 | float3 displacement = local - offset - (random - 0.5f) * 0.26f; |
| 58 | float radius = 0.58f + 0.19f * random.z; |
| 59 | float lobe = radius * radius - dot(displacement, displacement); |
| 60 | float join = max(0.09f - abs(lobe - surface), 0.0f); |
| 61 | float blend = smoothstep(-0.10f, -0.04f, min(lobe, surface)); |
| 62 | surface = max(surface, lobe) + join * join * 2.78f * blend; |
| 63 | } |
| 64 | return surface; |
| 65 | } |
| 66 | |
| 67 | static float seam(float depth) { |
| 68 | float turn = depth * (studyTurn / flightLength); |
| 69 | return 0.24f * sin(turn) + 0.13f * sin(turn * 2.0f - 0.7f); |
| 70 | } |
| 71 | |
| 72 | // The camera passes the same world-space filaments as it passes the billows. |
| 73 | static float emberSeam(float3 point, float arcDepth, float phase, float beat) { |
| 74 | float height = point.y; |
| 75 | float depth = 0.30f * sin(height * 1.9f); |
| 76 | float center = seam(point.z) + height * 0.54f - 0.43f |
| 77 | + (noise3D(float3(height * 4.0f, 2.0f, 1.0f), 16.0f) - 0.5f) * 0.44f |
| 78 | + (noise3D(float3(height * 13.0f, 7.0f, 4.0f), 16.0f) - 0.5f) * 0.14f; |
| 79 | float2 offset = float2((point.x - center) * 31.0f, (arcDepth - depth) * 6.0f); |
| 80 | float mask = smoothstep(-1.65f, -1.18f, height) * (1.0f - smoothstep(0.52f, 1.26f, height)); |
| 81 | float sweep = 0.23f + 0.77f * pow(0.5f + 0.5f * cos(height * 3.8f - phase * 5.0f), 3.0f); |
| 82 | return exp(-dot(offset, offset)) * mask * sweep * (0.25f + 1.4f * beat); |
| 83 | } |
| 84 | |
| 85 | // Each octave spans an integer number of cells along the flight. Sampling |
| 86 | // one flight length later therefore returns the same world-space density. |
| 87 | static float smokeDensity(float3 point, bool fine) { |
| 88 | float turn = point.z * (studyTurn / flightLength); |
| 89 | float across = dot(point.xy, float2(0.86f, -0.51f)) - seam(point.z); |
| 90 | float along = dot(point.xy, float2(0.51f, 0.86f)); |
| 91 | float width = 1.07f + 0.16f * sin(turn * 3.0f + 0.3f); |
| 92 | float shoulder = length(float2(across, along * 0.46f)) - width; |
| 93 | float billows = billowField(point * 1.3125f + float3(2.0f, 4.1f, 7.3f), 21.0f); |
| 94 | float body = noise3D(point * 0.9375f, 15.0f) - 0.48f; |
| 95 | float volume = clamp(shoulder, -1.8f, 0.75f) * 0.76f |
| 96 | + billows * 1.42f + body * 0.64f - 0.39f; |
| 97 | if (volume > -0.30f) { |
| 98 | float curls = noise3D(point * 4.125f + 2.7f, 66.0f); |
| 99 | float folds = noise3D(point * 9.1875f - 4.3f, 147.0f); |
| 100 | volume += (curls - 0.51f) * 0.40f + (folds - 0.53f) * 0.22f; |
| 101 | if (fine) { |
| 102 | volume += (noise3D(point * 18.0f + 8.2f, 288.0f) - 0.53f) * 0.12f; |
| 103 | } |
| 104 | } |
| 105 | return smoothstep(-0.06f, 0.29f, volume) * 6.2f; |
| 106 | } |
| 107 | |
| 108 | static float4 backlitRiftColor(float2 viewport, uint2 pixel, StudyFrame frame) { |
| 109 | viewport /= frame.scale; |
| 110 | float phase = frame.phase; |
| 111 | float3 eye = float3(0.0f, 0.0f, 1.75f + frame.travel * (flightLength / studyTurn)); |
| 112 | float3 ray = normalize(float3(viewport * 0.46f + float2(0.0f, -0.05f), 1.0f)); |
| 113 | float3 lamp = float3(-0.55f, 0.96f, 7.1f); |
| 114 | float3 shadow = float3(0.005f, 0.020f, 0.030f); |
| 115 | float3 mint = float3(0.13f, 0.81f, 0.53f); |
| 116 | float3 copper = float3(1.0f, 0.54f, 0.15f); |
| 117 | float transmission = 1.0f; |
| 118 | float3 color = float3(0.0f); |
| 119 | const int steps = 144; |
| 120 | float depthStep = 11.8f / float(steps); |
| 121 | float jitter = mix(0.14f, 0.86f, lattice(float3(float2(pixel), 21.0f))); |
| 122 | float distanceStep = depthStep / ray.z; |
| 123 | |
| 124 | for (int step = 0; step < steps && transmission > 0.012f; ++step) { |
| 125 | float depth = (float(step) + jitter) * depthStep; |
| 126 | float3 fromEye = ray * (depth / ray.z); |
| 127 | float3 point = eye + fromEye; |
| 128 | // Feather a billow as it passes the lens and as it enters visibility far ahead. |
| 129 | float fade = smoothstep(0.32f, 1.45f, depth) * (1.0f - smoothstep(10.0f, 11.8f, depth)); |
| 130 | float density = smokeDensity(point, true) * fade * (0.65f + 0.70f * frame.fog); |
| 131 | float3 toLamp = lamp - fromEye; |
| 132 | float distanceSquared = dot(toLamp, toLamp); |
| 133 | |
| 134 | float channel = point.x - 0.64f * point.y - seam(point.z); |
| 135 | float haze = exp(-abs(channel) * 1.26f) * exp(-distanceSquared * 0.40f); |
| 136 | float core = exp(-distanceSquared * 1.25f); |
| 137 | color += transmission * haze * distanceStep |
| 138 | * (float3(0.09f, 0.43f, 0.39f) + float3(1.18f, 1.29f, 1.02f) * core); |
| 139 | float arcDepth = repeatDepth(point.z - 1.5f, flightLength * 0.5f) - flightLength * 0.25f; |
| 140 | if (abs(arcDepth) < 0.85f && point.y > -1.65f && point.y < 1.26f) { |
| 141 | float arcFade = smoothstep(1.0f, 2.75f, depth) * fade; |
| 142 | color += transmission * distanceStep * copper * arcFade * emberSeam(point, arcDepth, phase, frame.beat); |
| 143 | } |
| 144 | |
| 145 | if (density < 0.008f) { |
| 146 | continue; |
| 147 | } |
| 148 | float3 lightDirection = toLamp * rsqrt(max(distanceSquared, 0.002f)); |
| 149 | float nearDensity = smokeDensity(point + lightDirection * 0.13f, false); |
| 150 | float middleDensity = smokeDensity(point + lightDirection * 0.39f, false); |
| 151 | float farDensity = smokeDensity(point + lightDirection * 0.85f, false); |
| 152 | float opticalDepth = density * 0.09f + nearDensity * 0.18f |
| 153 | + middleDensity * 0.34f + farDensity * 0.55f; |
| 154 | float light = exp(-opticalDepth); |
| 155 | float scatter = 0.28f + 0.72f * exp(-distanceSquared * 0.052f); |
| 156 | float forward = pow(max(dot(ray, lightDirection), 0.0f), 5.0f); |
| 157 | |
| 158 | float rim = saturate((density - nearDensity) * 0.52f); |
| 159 | float3 fill = float3(0.015f, 0.14f, 0.17f) * exp(-density * 0.37f); |
| 160 | float3 radiance = shadow + fill + mint * light * scatter * (0.61f + 0.80f * forward + 1.1f * rim); |
| 161 | float glint = exp(-distanceSquared * 0.12f) * light * (0.28f + 1.53f * rim); |
| 162 | float3 beatOffset = -toLamp + float3(0.40f, 0.10f, 1.0f); |
| 163 | float localBeat = frame.beat * exp(-dot(beatOffset, beatOffset) * 1.5f); |
| 164 | radiance += copper * glint * (1.24f + 1.0f * localBeat); |
| 165 | |
| 166 | float opacity = 1.0f - exp(-density * distanceStep); |
| 167 | color += transmission * opacity * radiance; |
| 168 | transmission *= 1.0f - opacity; |
| 169 | } |
| 170 | |
| 171 | float2 skyOffset = viewport - float2(-0.07f, 0.26f); |
| 172 | float sky = exp(-dot(skyOffset, skyOffset) * 2.1f); |
| 173 | color += transmission * mix(shadow, float3(0.034f, 0.14f, 0.14f), sky); |
| 174 | color *= mix(1.0f, 0.70f, smoothstep(0.55f, 1.9f, length(viewport))); |
| 175 | color = 1.0f - exp(-color * 1.66f); |
| 176 | color = pow(saturate(color), float3(0.73f)); |
| 177 | return float4(color, 1.0f); |
| 178 | } |
| 179 | |
| 180 | static float hash3(float3 p) { |
| 181 | p = fract(p * float3(0.1031f, 0.1030f, 0.0973f)); |
| 182 | p += dot(p, p.yxz + 33.33f); |
| 183 | return fract((p.x + p.y) * p.z); |
| 184 | } |
| 185 | |
| 186 | static float studyNoise3(float3 p, float repeatZ = 0.0f) { |
| 187 | float3 cell = floor(p); |
| 188 | float3 f = fract(p); |
| 189 | f = f * f * (3.0f - 2.0f * f); |
| 190 | float3 farCell = cell + float3(0, 0, 1); |
| 191 | if (repeatZ > 0.0f) { |
| 192 | cell.z -= floor(cell.z / repeatZ) * repeatZ; |
| 193 | farCell.z -= floor(farCell.z / repeatZ) * repeatZ; |
| 194 | } |
| 195 | float a = mix( |
| 196 | mix(hash3(cell), hash3(cell + float3(1, 0, 0)), f.x), |
| 197 | mix(hash3(cell + float3(0, 1, 0)), hash3(cell + float3(1, 1, 0)), f.x), f.y |
| 198 | ); |
| 199 | float b = mix( |
| 200 | mix(hash3(farCell), hash3(farCell + float3(1, 0, 0)), f.x), |
| 201 | mix(hash3(farCell + float3(0, 1, 0)), hash3(farCell + float3(1, 1, 0)), f.x), f.y |
| 202 | ); |
| 203 | return mix(a, b, f.z); |
| 204 | } |
| 205 | |
| 206 | |
| 207 | static float4 displayColor(float3 color, uint2 pixel) { |
| 208 | float grain = (hash3(float3(float2(pixel), 19.0f)) - 0.5f) / 255.0f; |
| 209 | float3 mapped = pow(saturate(1.0f - exp(-max(color, 0.0f))), float3(0.62f)); |
| 210 | return float4(mapped + grain, 1.0f); |
| 211 | } |
| 212 | |
| 213 | // Undersurface. A camera below a moving height field sees transmitted light |
| 214 | // within Snell's window, with internal reflection at more grazing angles. |
| 215 | static float waterLevel(float2 p, float t) { |
| 216 | return 0.13f * sin(dot(p, float2(1.7f, 2.2f)) - t) |
| 217 | + 0.085f * sin(dot(p, float2(-2.5f, 1.3f)) + t) |
| 218 | + 0.048f * sin(dot(p, float2(4.9f, -2.8f)) + 2.0f * t) |
| 219 | + 0.023f * sin(dot(p, float2(8.0f, 5.2f)) - 3.0f * t) |
| 220 | + 0.009f * sin(dot(p, float2(-13.0f, 8.1f)) + 4.0f * t); |
| 221 | } |
| 222 | |
| 223 | static float3 waterNormal(float2 p, float t) { |
| 224 | const float e = 0.016f; |
| 225 | float height = waterLevel(p, t); |
| 226 | float2 slope = float2(waterLevel(p + float2(e, 0), t), waterLevel(p + float2(0, e), t)) - height; |
| 227 | return normalize(float3(-slope.x, e, -slope.y)); |
| 228 | } |
| 229 | |
| 230 | static float surfaceDistance(float3 eye, float3 ray, float phase) { |
| 231 | float lo = 0.0f; |
| 232 | float hi = min((-eye.y + 0.31f) / max(ray.y, 0.001f), 30.0f); |
| 233 | if (eye.y + ray.y * hi < -0.30f) { |
| 234 | return 30.0f; |
| 235 | } |
| 236 | // Find the first interface even where a shallow ray crosses several waves. |
| 237 | const uint steps = 40; |
| 238 | float stepSize = hi / float(steps); |
| 239 | for (uint i = 1; i <= steps; ++i) { |
| 240 | float distance = float(i) * stepSize; |
| 241 | float3 point = eye + ray * distance; |
| 242 | if (point.y >= waterLevel(point.xz, phase)) { |
| 243 | hi = distance; |
| 244 | for (uint refine = 0; refine < 5; ++refine) { |
| 245 | float middle = 0.5f * (lo + hi); |
| 246 | float3 samplePoint = eye + ray * middle; |
| 247 | if (samplePoint.y > waterLevel(samplePoint.xz, phase)) { |
| 248 | hi = middle; |
| 249 | } else { |
| 250 | lo = middle; |
| 251 | } |
| 252 | } |
| 253 | return hi; |
| 254 | } |
| 255 | lo = distance; |
| 256 | } |
| 257 | return 30.0f; |
| 258 | } |
| 259 | |
| 260 | static float underwaterSilt(float3 eye, float3 ray, float distance, float phase) { |
| 261 | float particles = 0.0f; |
| 262 | for (int layer = 1; layer <= 11; ++layer) { |
| 263 | float depth = 0.65f + float(layer) * 0.68f; |
| 264 | if (depth > distance) { |
| 265 | break; |
| 266 | } |
| 267 | float3 point = eye + depth * ray; |
| 268 | float2 grid = point.xy * 4.0f + float2(0.22f * sin(phase), -0.21f * cos(phase + float(layer))); |
| 269 | float2 cell = floor(grid); |
| 270 | float3 seed = float3(cell, float(layer)); |
| 271 | float2 center = 0.18f + 0.64f * float2(hash3(seed), hash3(seed + 11.3f)); |
| 272 | float2 delta = fract(grid) - center; |
| 273 | float size = 0.027f + 0.036f * hash3(seed + 17.0f); |
| 274 | float brightness = pow(hash3(seed + 25.4f), 2.0f); |
| 275 | particles += exp(-dot(delta, delta) / (size * size)) * exp(-0.16f * depth) * brightness; |
| 276 | } |
| 277 | return particles; |
| 278 | } |
| 279 | |
| 280 | static float4 undersurfaceColor(float2 uv, uint2 pixel, StudyFrame frame) { |
| 281 | float phase = frame.phase; |
| 282 | float3 eye = float3(0.29f * sin(phase), (-1.36f + 0.10f * cos(phase)) * frame.scale, 0.23f * cos(phase)); |
| 283 | float3 ray = normalize(float3(uv.x * 0.68f, 0.72f + uv.y * 0.91f, 1.0f)); |
| 284 | float3 sun = normalize(float3(-0.55f, 0.45f, 0.78f)); |
| 285 | float distance = surfaceDistance(eye, ray, phase); |
| 286 | float3 color = float3(0.005f, 0.015f, 0.060f) + float3(0.01f, 0.065f, 0.19f) * smoothstep(-0.2f, 0.9f, ray.y); |
| 287 | if (distance < 29.9f) { |
| 288 | float3 hit = eye + ray * distance; |
| 289 | float3 normal = waterNormal(hit.xz, phase); |
| 290 | float3 airRay = refract(ray, -normal, 1.333f); |
| 291 | float transmission = smoothstep(0.0f, 0.45f, airRay.y); |
| 292 | float sunlight = pow(max(dot(airRay, sun), 0.0f), 9.0f); |
| 293 | float glint = pow(max(dot(airRay, sun), 0.0f), 90.0f); |
| 294 | float3 waterTint = exp(-distance * float3(0.39f, 0.23f, 0.12f)); |
| 295 | float3 sky = float3(0.055f, 0.18f, 0.43f) + float3(3.3f, 2.4f, 1.30f) * sunlight |
| 296 | + float3(4.6f, 3.9f, 2.7f) * glint; |
| 297 | float2 bent = hit.xz * float2(3.3f, 4.2f) + normal.xz * 5.5f; |
| 298 | float laceA = abs(sin(bent.x + 1.1f * sin(bent.y + phase))); |
| 299 | float laceB = abs(sin(bent.y + 1.4f * sin(bent.x - phase))); |
| 300 | float shimmer = exp(-min(laceA, laceB) * 7.0f) * exp(-distance * 0.47f); |
| 301 | float2 pool = hit.xz - float2(-0.57f, 1.18f); |
| 302 | float poolLight = exp(-dot(pool, pool) * 0.88f); |
| 303 | float reflectedSun = pow(max(dot(reflect(ray, -normal), float3(-0.55f, -0.42f, 0.72f)), 0.0f), 28.0f); |
| 304 | float3 reflection = float3(0.014f, 0.075f, 0.25f) + reflectedSun * float3(0.32f, 0.42f, 0.63f); |
| 305 | color += waterTint * mix(reflection, sky, transmission) |
| 306 | + shimmer * float3(0.61f, 0.78f, 1.12f) * (0.055f + 0.75f * poolLight); |
| 307 | } |
| 308 | float fan = 0.0f; |
| 309 | float reach = min(distance, 8.0f); |
| 310 | for (uint step = 0; step < 18; ++step) { |
| 311 | float3 point = eye + ray * (float(step) + 0.5f) * (reach / 18.0f); |
| 312 | float2 cast = point.xz - sun.xz * (point.y / sun.y); |
| 313 | float angle = 5.3f * cast.x + 1.4f * cast.y + 2.1f * sin(cast.y * 1.6f + phase); |
| 314 | float light = pow(0.5f + 0.5f * cos(angle), 7.0f + 3.0f * frame.beat); |
| 315 | float2 pool = cast - float2(-0.57f, 1.18f); |
| 316 | float spread = exp(-dot(pool, pool) * 0.26f); |
| 317 | fan += light * spread * exp(point.y * 0.44f); |
| 318 | } |
| 319 | color += fan * (reach / 18.0f) * float3(0.10f, 0.17f, 0.28f) * (0.65f + 0.70f * frame.fog); |
| 320 | color += underwaterSilt(eye, ray, distance, phase) * float3(0.038f, 0.081f, 0.13f); |
| 321 | return displayColor(color, pixel); |
| 322 | } |
| 323 | |
| 324 | // Mercury Fold. A narrow twisted band collects into larger bulbs, with |
| 325 | // separate beads shaped by the same lighting as the main mass. |
| 326 | static float softUnion(float a, float b, float width) { |
| 327 | float h = max(width - abs(a - b), 0.0f) / width; |
| 328 | return min(a, b) - h * h * width * 0.25f; |
| 329 | } |
| 330 | |
| 331 | static float mercuryShape(float3 p, float phase) { |
| 332 | float y = clamp(p.y, -1.0f, 1.0f); |
| 333 | float center = 0.37f * sin(y * 2.25f - 0.4f) |
| 334 | + 0.12f * sin(phase + y * 3.1f); |
| 335 | float depth = 0.22f * sin(y * 3.0f + phase); |
| 336 | float2 q = float2(p.x - center, p.z - depth); |
| 337 | float angle = 1.52f * y + 0.44f * sin(phase - y); |
| 338 | float2 band = float2(dot(q, float2(cos(angle), sin(angle))), dot(q, float2(-sin(angle), cos(angle)))); |
| 339 | float width = 0.35f + 0.16f * cos(3.5f * y - phase); |
| 340 | width *= 1.0f - 0.53f * pow(abs(y), 4.0f); |
| 341 | float2 edge = abs(band) - float2(width, 0.039f + 0.024f * cos(4.0f * y + phase)); |
| 342 | float ribbon = length(max(edge, 0.0f)) + min(max(edge.x, edge.y), 0.0f) - 0.084f; |
| 343 | float2 ends = float2(ribbon + 0.055f, abs(p.y) - 0.95f); |
| 344 | ribbon = length(max(ends, 0.0f)) + min(max(ends.x, ends.y), 0.0f) - 0.055f; |
| 345 | float3 bulb = float3(0.27f + 0.15f * cos(phase), 0.34f + 0.16f * sin(phase), 0.13f); |
| 346 | float shape = softUnion(ribbon, length((p - bulb) * float3(0.90f, 1.2f, 1.0f)) - 0.29f, 0.26f); |
| 347 | float3 lower = float3(-0.37f, -0.72f, -0.13f + 0.14f * sin(phase)); |
| 348 | shape = softUnion(shape, length(p - lower) - 0.21f, 0.19f); |
| 349 | for (uint i = 0; i < 6; ++i) { |
| 350 | float turn = phase + 1.83f * float(i); |
| 351 | float side = i % 2 == 0 ? -1.0f : 1.0f; |
| 352 | float3 drop = float3(side * (0.67f + float(i) * 0.061f) + 0.08f * cos(turn), |
| 353 | -0.82f + float(i) * 0.31f + 0.11f * sin(turn), |
| 354 | 0.27f * cos(turn)); |
| 355 | shape = min(shape, length(p - drop) - (0.045f + 0.027f * (0.5f + 0.5f * sin(turn)))); |
| 356 | } |
| 357 | float ripples = sin(p.y * 23.0f - phase * 2.0f + 3.6f * p.x) * sin(p.x * 13.0f + p.z * 8.0f); |
| 358 | return shape + ripples * 0.012f; |
| 359 | } |
| 360 | |
| 361 | static float3 studioReflection(float3 ray) { |
| 362 | float whiteStrip = exp(-pow((ray.x + 0.53f) * 7.5f, 2.0f)) * smoothstep(-0.55f, 0.22f, ray.y); |
| 363 | float softBox = pow(max(dot(ray, normalize(float3(0.29f, 0.85f, -0.43f))), 0.0f), 13.0f); |
| 364 | float amber = exp(-pow((ray.y + 0.38f + 0.18f * ray.x) * 7.5f, 2.0f)) * smoothstep(-0.2f, 0.77f, ray.x); |
| 365 | float blue = pow(max(dot(ray, normalize(float3(0.84f, 0.25f, 0.23f))), 0.0f), 14.0f); |
| 366 | return float3(0.008f, 0.012f, 0.025f) + whiteStrip * float3(1.6f, 1.85f, 2.0f) |
| 367 | + softBox * float3(1.35f, 1.2f, 1.04f) + amber * float3(2.25f, 0.76f, 0.25f) |
| 368 | + blue * float3(0.12f, 0.23f, 0.46f); |
| 369 | } |
| 370 | |
| 371 | static float4 mercuryFoldColor(float2 uv, uint2 pixel, StudyFrame frame) { |
| 372 | float phase = frame.phase; |
| 373 | float azimuth = 0.21f * sin(phase); |
| 374 | float3 eye = float3(3.6f * sin(azimuth), 0.15f + 0.19f * cos(phase), -3.6f * cos(azimuth)); |
| 375 | float3 forward = normalize(-eye); |
| 376 | float3 right = normalize(cross(float3(0, 1, 0), forward)); |
| 377 | float3 up = cross(forward, right); |
| 378 | float3 ray = normalize(forward + (0.34f / frame.scale) * (right * (uv.x + 0.13f) + up * uv.y)); |
| 379 | float tilt = -0.32f; |
| 380 | float2 axisX = float2(cos(tilt), sin(tilt)); |
| 381 | float2 axisY = float2(-sin(tilt), cos(tilt)); |
| 382 | float3 eyeLocal = float3(dot(eye.xy, axisX), dot(eye.xy, axisY), eye.z); |
| 383 | float3 rayLocal = float3(dot(ray.xy, axisX), dot(ray.xy, axisY), ray.z); |
| 384 | float at = 2.05f; |
| 385 | float3 point; |
| 386 | float distance = 2.0f; |
| 387 | for (uint step = 0; step < 100 && at < 5.7f; ++step) { |
| 388 | point = eyeLocal + rayLocal * at; |
| 389 | distance = mercuryShape(point, phase); |
| 390 | if (distance < 0.0018f) { |
| 391 | break; |
| 392 | } |
| 393 | at += max(distance * 0.46f, 0.003f); |
| 394 | } |
| 395 | float halo = exp(-dot(uv * float2(0.8f, 1.2f), uv * float2(0.8f, 1.2f)) * 1.5f); |
| 396 | float3 color = float3(0.003f, 0.003f, 0.007f) + halo * float3(0.019f, 0.012f, 0.011f); |
| 397 | if (distance < 0.0018f) { |
| 398 | const float e = 0.003f; |
| 399 | float3 normal = normalize(float3( |
| 400 | mercuryShape(point + float3(e, 0, 0), phase) - mercuryShape(point - float3(e, 0, 0), phase), |
| 401 | mercuryShape(point + float3(0, e, 0), phase) - mercuryShape(point - float3(0, e, 0), phase), |
| 402 | mercuryShape(point + float3(0, 0, e), phase) - mercuryShape(point - float3(0, 0, e), phase) |
| 403 | )); |
| 404 | float3 reflected = reflect(rayLocal, normal); |
| 405 | float rim = pow(1.0f - saturate(dot(-rayLocal, normal)), 3.0f); |
| 406 | float sheen = 0.72f + 0.24f * sin(point.y * 5.4f - phase + normal.x * 2.6f); |
| 407 | color = studioReflection(reflected) * sheen + float3(0.20f, 0.08f, 0.035f) * rim |
| 408 | * (0.54f + 0.24f * frame.beat); |
| 409 | } |
| 410 | return displayColor(color, pixel); |
| 411 | } |
| 412 | |
| 413 | // Ember Front. Particles live on periodic depth layers. The camera advances |
| 414 | // through the layers and the nearest grains stretch with their screen velocity. |
| 415 | static float4 emberFrontColor(float2 uv, uint2 pixel, StudyFrame frame) { |
| 416 | float phase = frame.phase; |
| 417 | float travel = 24.0f * frame.travel / studyTurn; |
| 418 | float3 ray = normalize(float3(uv * 0.57f, 1.0f)); |
| 419 | float ridge = 0.40f + uv.y * 0.29f + 0.11f * sin(uv.y * 3.1f + phase); |
| 420 | float cloud = exp(-abs(uv.x - ridge) * 2.1f); |
| 421 | float smoke = studyNoise3(float3(uv * 1.75f + float2(cos(phase), sin(phase)) * 0.46f, 7.6f)); |
| 422 | float3 color = float3(0.006f, 0.004f, 0.009f) + |
| 423 | cloud * (0.26f + 0.74f * smoke) * float3(0.15f, 0.031f, 0.012f); |
| 424 | const float layerSpacing = 0.67f; |
| 425 | |
| 426 | for (int i = 0; i < 28; ++i) { |
| 427 | float layer = floor(travel) + float(i); |
| 428 | float z = (layer - travel) * layerSpacing; |
| 429 | if (z < 0.45f) { |
| 430 | continue; |
| 431 | } |
| 432 | float seed = layer - floor(layer / 24.0f) * 24.0f; |
| 433 | float2 position = ray.xy * (z / ray.z); |
| 434 | float curl = sin(seed * (studyTurn / 24.0f) + position.y * 0.66f); |
| 435 | float shift = 1.07f * curl + 0.25f * sin(2.0f * phase + position.y * 1.1f); |
| 436 | float2 currentPoint = float2(position.x - shift - 0.30f * position.y, position.y); |
| 437 | // Rotate each layer's lattice in its own plane so rows of particles in |
| 438 | // different depth layers cannot line up into horizontal seams. |
| 439 | float rotation = studyTurn * hash3(float3(seed, 14.9f, 8.7f)); |
| 440 | float2 axisX = float2(cos(rotation), sin(rotation)); |
| 441 | float2 axisY = float2(-axisX.y, axisX.x); |
| 442 | float2 grid = float2(dot(currentPoint, axisX), dot(currentPoint, axisY)) * 4.8f; |
| 443 | float2 base = floor(grid); |
| 444 | float3 sampleSeed = float3(base, seed); |
| 445 | float2 center = 0.29f + 0.42f * float2(hash3(sampleSeed), hash3(sampleSeed + 19.27f)); |
| 446 | float2 delta = fract(grid) - center; |
| 447 | float random = hash3(sampleSeed + 7.36f); |
| 448 | float2 cellPoint = (axisX * (base.x + center.x) + axisY * (base.y + center.y)) / 4.8f; |
| 449 | float front = 0.26f + 0.42f * sin(seed * (studyTurn / 12.0f)); |
| 450 | float current = cellPoint.x - front; |
| 451 | float veil = smoothstep(-0.60f, 0.63f, current) * (1.0f - 0.55f * smoothstep(1.5f, 3.5f, current)); |
| 452 | float pockets = studyNoise3(float3(cellPoint, seed * 0.25f), 6.0f); |
| 453 | float occupancy = 0.032f + 0.64f * veil * smoothstep(0.20f, 0.75f, pockets); |
| 454 | if (random > occupancy) { |
| 455 | continue; |
| 456 | } |
| 457 | float brightness = hash3(sampleSeed + 37.0f); |
| 458 | float2 radial = position + float2(0.002f, -0.002f); |
| 459 | float2 outward = normalize(float2(dot(radial, axisX), dot(radial, axisY))); |
| 460 | float2 stroke = float2(dot(delta, outward), dot(delta, float2(-outward.y, outward.x))); |
| 461 | float width = frame.scale * (0.028f + 0.044f * brightness) * (1.0f + 0.020f * z); |
| 462 | float length = width * (1.0f + 1.82f * saturate(3.5f / z)); |
| 463 | float footprint = dot(stroke / float2(length, width), stroke / float2(length, width)); |
| 464 | float blast = smoothstep(0.5f, 1.55f, z) * (1.0f - smoothstep(9.0f, 17.5f, z)); |
| 465 | float core = exp(-footprint * 1.25f); |
| 466 | float glow = exp(-footprint * 0.25f) * 0.21f; |
| 467 | float3 copper = mix(float3(0.72f, 0.11f, 0.04f), float3(2.4f, 0.86f, 0.19f), brightness * brightness); |
| 468 | float3 grain = mix(copper, float3(0.18f, 0.27f, 0.63f), smoothstep(0.72f, 0.92f, hash3(sampleSeed + 43.9f))); |
| 469 | float pulse = 1.0f + 0.6f * frame.beat * smoothstep(0.6f, 1.0f, brightness); |
| 470 | float falloff = 1.0f - smoothstep(0.17f, 0.29f, max(abs(delta.x), abs(delta.y))); |
| 471 | color += grain * (core + glow) * blast * pulse * falloff * (0.11f + 0.74f * brightness); |
| 472 | } |
| 473 | return displayColor(color, pixel); |
| 474 | } |
| 475 | |
| 476 | // Anvil Flyover. A repeating terrain-like cloud deck, seen from above. |
| 477 | // Sampling X in the periodic dimension makes a continuous lateral flight. |
| 478 | static float anvilDensity(float3 p, bool detail) { |
| 479 | float3 q = p.zyx; |
| 480 | float large = studyNoise3(q * 0.25f + float3(21.0f, 4.0f, 1.0f), 4.0f); |
| 481 | float banks = studyNoise3(q * 0.625f + float3(0.0f, 3.0f, 11.1f), 10.0f); |
| 482 | float tower = smoothstep(0.51f, 0.82f, large); |
| 483 | float crowns = studyNoise3(q * 0.875f + float3(3.4f, 9.3f, 5.2f), 14.0f); |
| 484 | float ceiling = -0.48f + banks * 1.33f + tower * (1.69f + 1.30f * crowns); |
| 485 | const float base = -2.0f; |
| 486 | float volume = min(ceiling - p.y, p.y - base); |
| 487 | if (volume > -0.85f && detail) { |
| 488 | float fleece = studyNoise3(q * 1.5f + 6.0f, 24.0f); |
| 489 | float grain = studyNoise3(q * 3.75f - 4.4f, 60.0f); |
| 490 | float billows = studyNoise3(q * 8.0f + 11.3f, 128.0f); |
| 491 | volume += (fleece - 0.50f) * 1.40f + (grain - 0.53f) * 0.47f + (billows - 0.52f) * 0.19f; |
| 492 | } |
| 493 | return smoothstep(-0.23f, 0.28f, volume) * 2.55f; |
| 494 | } |
| 495 | |
| 496 | static float4 anvilFlyoverColor(float2 uv, uint2 pixel, StudyFrame frame) { |
| 497 | float phase = frame.phase; |
| 498 | float3 eye = float3(2.9f + 16.0f * (frame.travel / studyTurn), 2.62f + 0.31f * sin(phase) + (frame.scale - 1.0f) * 0.58f, 1.8f); |
| 499 | float3 ray = normalize(float3(0.43f * uv.x + 0.11f, 0.46f * uv.y - 0.21f, 1.0f)); |
| 500 | float3 sun = normalize(float3(0.56f, 0.28f, 0.91f)); |
| 501 | float towardSun = max(dot(ray, sun), 0.0f); |
| 502 | float horizon = exp(-abs(ray.y + 0.055f) * 5.5f); |
| 503 | float3 sky = mix(float3(0.041f, 0.039f, 0.15f), float3(0.45f, 0.22f, 0.31f), horizon); |
| 504 | sky += float3(0.50f, 0.27f, 0.31f) * pow(towardSun, 26.0f); |
| 505 | sky += float3(0.76f, 0.49f, 0.33f) * pow(towardSun, 260.0f); |
| 506 | float3 color = float3(0.0f); |
| 507 | float transmittance = 1.0f; |
| 508 | const uint steps = 116; |
| 509 | const float stride = 0.19f; |
| 510 | float offset = mix(0.12f, 0.88f, hash3(float3(float2(pixel), 3.1f))); |
| 511 | for (uint step = 0; step < steps && transmittance > 0.015f; ++step) { |
| 512 | float distance = 0.43f + (float(step) + offset) * stride; |
| 513 | float3 point = eye + ray * distance; |
| 514 | if (point.y < -2.45f) { |
| 515 | break; |
| 516 | } |
| 517 | float visibility = smoothstep(0.43f, 1.85f, distance) * (1.0f - smoothstep(17.0f, 22.0f, distance)); |
| 518 | float density = anvilDensity(point, true) * visibility * (0.65f + 0.70f * frame.fog); |
| 519 | if (density < 0.005f) { |
| 520 | continue; |
| 521 | } |
| 522 | |
| 523 | float nearDensity = anvilDensity(point + sun * 0.24f, true); |
| 524 | float occlusion = density * 0.18f |
| 525 | + nearDensity * 0.26f |
| 526 | + anvilDensity(point + sun * 0.65f, true) * 0.53f |
| 527 | + anvilDensity(point + sun * 1.6f, false) * 0.71f; |
| 528 | float light = exp(-occlusion); |
| 529 | float lavender = 0.60f + 0.40f * smoothstep(-0.9f, 1.4f, point.y); |
| 530 | float fillDensity = anvilDensity(point + float3(-0.29f, 0.23f, -0.37f), true); |
| 531 | float fill = exp(-fillDensity * 0.94f - density * 0.27f); |
| 532 | float3 shade = (float3(0.028f, 0.024f, 0.063f) + fill * float3(0.11f, 0.13f, 0.22f)) * lavender; |
| 533 | float rim = saturate((density - nearDensity) * 1.1f); |
| 534 | float3 radiance = shade + light * float3(0.98f, 0.85f, 1.06f) * (0.78f + 0.39f * towardSun) |
| 535 | + rim * light * pow(towardSun, 3.0f) * float3(0.69f, 0.26f, 0.14f); |
| 536 | float pocket = studyNoise3(point.zyx * 0.5f + 5.1f, 8.0f); |
| 537 | float discharge = smoothstep(0.68f, 0.83f, pocket) * frame.beat * frame.beat; |
| 538 | radiance += float3(0.11f, 0.061f, 0.15f) * discharge * (1.0f - light); |
| 539 | radiance = mix(radiance, sky * 0.87f, smoothstep(12.0f, 26.0f, distance) * 0.66f); |
| 540 | float opacity = 1.0f - exp(-density * stride); |
| 541 | color += transmittance * opacity * radiance; |
| 542 | transmittance *= 1.0f - opacity; |
| 543 | } |
| 544 | color += transmittance * sky; |
| 545 | return displayColor(color, pixel); |
| 546 | } |
| 547 | |
| 548 | // Pigment Strata. Inverse advection of horizontal pigment interfaces through |
| 549 | // rotating cells. Heterogeneous angular speeds stretch the edges into filaments. |
| 550 | static float2 pigmentVortex(float2 p, float2 center, float radius, float turn) { |
| 551 | float2 offset = p - center; |
| 552 | float weight = (1.0f - smoothstep(0.1f, 1.0f, length(offset) / radius)); |
| 553 | float angle = turn * weight * weight; |
| 554 | float2 rotated = float2( |
| 555 | dot(offset, float2(cos(angle), -sin(angle))), |
| 556 | dot(offset, float2(sin(angle), cos(angle))) |
| 557 | ); |
| 558 | return center + rotated; |
| 559 | } |
| 560 | |
| 561 | static float2 pigmentRow(float2 p, float height, float spacing, float phase, float offset) { |
| 562 | float tile = floor(p.x / spacing + offset) - offset; |
| 563 | float2 center = float2((tile + 0.5f) * spacing, height); |
| 564 | float spin = 2.9f + 0.73f * sin(phase + offset * 4.1f) |
| 565 | + 0.75f * sin(tile * 1.83f + offset); |
| 566 | return pigmentVortex(p, center, spacing * 0.49f, spin); |
| 567 | } |
| 568 | |
| 569 | static float4 pigmentStrataColor(float2 uv, uint2 pixel, StudyFrame frame) { |
| 570 | float phase = frame.phase; |
| 571 | float2 p = uv * float2(1.04f, 1.0f) / frame.scale; |
| 572 | p.x += 0.25f * sin(phase + p.y * 2.6f); |
| 573 | p.y += 0.12f * sin(1.7f * p.x + phase) + 0.065f * sin(4.4f * p.x - phase); |
| 574 | p = pigmentRow(p, 0.47f, 1.10f, phase, 0.23f); |
| 575 | p = pigmentRow(p, 0.10f, 1.65f, -phase, 0.66f); |
| 576 | p = pigmentRow(p, -0.51f, 0.84f, phase, 0.81f); |
| 577 | p = pigmentVortex(p, float2(0.17f, -0.23f), 0.83f, 7.8f + 2.3f * cos(phase)); |
| 578 | float wash = studyNoise3(float3(p * float2(3.3f, 22.0f) + float2(cos(phase), sin(phase)) * 0.37f, 4.3f)); |
| 579 | float grain = studyNoise3(float3(p * float2(18.0f, 78.0f), 12.1f)); |
| 580 | float boundary = p.y + (wash - 0.50f) * 0.075f + (grain - 0.50f) * 0.021f; |
| 581 | float3 color = float3(0.06f, 0.077f, 0.18f); |
| 582 | color = mix(color, float3(0.31f, 0.05f, 0.087f), smoothstep(-0.95f, -0.80f, boundary)); |
| 583 | color = mix(color, float3(0.78f, 0.28f, 0.10f), smoothstep(-0.62f, -0.49f, boundary)); |
| 584 | color = mix(color, float3(0.14f, 0.033f, 0.085f), smoothstep(-0.25f, -0.13f, boundary)); |
| 585 | color = mix(color, float3(1.20f, 0.72f, 0.35f), smoothstep(0.06f, 0.16f, boundary)); |
| 586 | color = mix(color, float3(0.25f, 0.141f, 0.235f), smoothstep(0.40f, 0.52f, boundary)); |
| 587 | color = mix(color, float3(0.024f, 0.045f, 0.11f), smoothstep(0.73f, 0.85f, boundary)); |
| 588 | float fibers = pow(0.5f + 0.5f * sin(boundary * 112.0f + wash * 5.0f), 10.0f); |
| 589 | float granulation = studyNoise3(float3(p * float2(57.0f, 131.0f), 9.2f)); |
| 590 | float mineral = 0.69f + 0.34f * wash + 0.24f * grain + 0.16f * granulation; |
| 591 | float luster = exp(-abs(boundary + 0.15f) * 20.0f) + exp(-abs(boundary - 0.15f) * 20.0f); |
| 592 | float2 glintOffset = (uv - float2(-0.30f, 0.38f)) * float2(0.5f, 1.65f); |
| 593 | float glint = exp(-dot(glintOffset, glintOffset) * 1.8f) * (0.2f + 0.8f * wash); |
| 594 | color = color * mineral + (0.065f + 0.05f * frame.beat + glint * 0.12f) |
| 595 | * luster * fibers * float3(1.0f, 0.72f, 0.32f); |
| 596 | float falloff = 1.0f - 0.23f * smoothstep(0.25f, 1.85f, length(uv)); |
| 597 | return displayColor(color * falloff, pixel); |
| 598 | } |
| 599 | |
| 600 | #if defined(PULSECANVAS_STUDY) |
| 601 | struct PreviewFrame { float phase; float beat; }; |
| 602 | static float2 screenPoint(uint2 pixel, uint2 size) { |
| 603 | float2 p = (float2(pixel) + 0.5f - 0.5f * float2(size)) * (2.0f / float(min(size.x, size.y))); |
| 604 | return float2(p.x, -p.y); |
| 605 | } |
| 606 | |
| 607 | #define STUDY_ENTRY(entry, color) \ |
| 608 | kernel void entry(texture2d<float, access::write> output [[texture(0)]], \ |
| 609 | constant PreviewFrame& preview [[buffer(0)]], uint2 pixel [[thread_position_in_grid]]) { \ |
| 610 | uint2 size = uint2(output.get_width(), output.get_height()); \ |
| 611 | if (any(pixel >= size)) { return; } \ |
| 612 | StudyFrame frame = {preview.phase, preview.beat, preview.phase, 1.0f, 0.5f}; \ |
| 613 | output.write(color(screenPoint(pixel, size), pixel, frame), pixel); \ |
| 614 | } |
| 615 | #else |
| 616 | static float4 styleAtmosphere(float4 color, constant NativeFrame& f) { |
| 617 | float3 rgb = color.rgb; |
| 618 | // The catalog default uses the authored grade. Alternate palettes tint |
| 619 | // the same lights and shadows using the standard five app color stops. |
| 620 | if (f.colors[0].w > 0.5f) { |
| 621 | float value = dot(rgb, float3(0.2126f, 0.7152f, 0.0722f)); |
| 622 | float3 tint = paint(value, f); |
| 623 | rgb = mix(rgb, tint * value / max(max(tint.r, tint.g), max(tint.b, 0.01f)), 0.68f); |
| 624 | } |
| 625 | return float4(saturate(rgb * f.style.x), color.a); |
| 626 | } |
| 627 | |
| 628 | #define STUDY_ENTRY(entry, color) \ |
| 629 | fragment float4 entry##Fragment(float4 position [[position]], constant NativeFrame& f [[buffer(0)]]) { \ |
| 630 | Ctx c = makeContext(position.xy, f); \ |
| 631 | float phase = fract(c.t / 8.0f) * studyTurn; \ |
| 632 | float travel = fract(c.travel / 8.0f) * studyTurn; \ |
| 633 | float beat = saturate(c.hit * 0.4f + f.rhythm.z * 0.6f); \ |
| 634 | StudyFrame frame = {phase, beat, travel, c.size, c.fog}; \ |
| 635 | return styleAtmosphere(color(c.uv, uint2(position.xy), frame), f); \ |
| 636 | } |
| 637 | #endif |
| 638 | |
| 639 | STUDY_ENTRY(smokeFrame, backlitRiftColor) |
| 640 | STUDY_ENTRY(undersurface, undersurfaceColor) |
| 641 | STUDY_ENTRY(mercuryFold, mercuryFoldColor) |
| 642 | STUDY_ENTRY(emberFront, emberFrontColor) |
| 643 | STUDY_ENTRY(anvilFlyover, anvilFlyoverColor) |
| 644 | STUDY_ENTRY(pigmentStrata, pigmentStrataColor) |
| 645 | #undef STUDY_ENTRY |