Branchwell

PulseCanvas / app · pulsecanvas.ai / experiments / metal-studies / Creative.metal

1#include <metal_stdlib>
2using namespace metal;
3// PulseCanvas visual experiments. No preset equations, engine code, or external images.
4struct Frame { float time; uint frame; uint style; float pulse; };
5float ribbon(float distance, float width) {
6 return exp(-abs(distance) / width);
7}
8float3 violetUndertow(float2 p, constant Frame &f) {
9 float t = f.time;
10 float2 center = .24f * float2(sin(t * .37f), cos(t * .29f));
11 float2 q = p - center;
12 float radius = length(q), angle = atan2(q.y, q.x);
13 // Local travelling ripples deform each spiral arm separately on the beat.
14 float ripple = f.pulse * .12f * sin(radius * 15.f - t * 8.f + angle * 3.f);
15 float spiral = radius * 13.f + angle * 7.f - t * 1.8f + ripple * 7.f;
16 float strand = abs(sin(spiral));
17 float secondary = abs(sin(radius * 19.f - angle * 5.f + t * .9f));
18 float light = ribbon(strand, .08f) * (.24f + .2f * sin(radius * 4.f - t) * sin(radius * 4.f - t));
19 light += ribbon(secondary, .035f) * .18f;
20 float mixAmount = .5f + .5f * sin(radius * 2.5f + angle + t * .3f);
21 float3 color = mix(float3(.18f,.04f,.65f),float3(.95f,.27f,.61f),mixAmount);
22 float sparks = pow(max(0.f, cos(angle * 11.f + radius * 8.f - t * 3.f)), 24.f);
23 return color * light + float3(.55f,.7f,1.f) * sparks * ribbon(strand,.035f) * (.08f + f.pulse * .32f);
24}
25float3 auroraWeave(float2 p, constant Frame &f) {
26 float t = f.time;
27 float3 color = float3(0);
28 // Distinct depths and lateral phases create overlapping sheets across the frame.
29 for (int layer = 0; layer < 9; ++layer) {
30 float depth = float(layer) / 8.f;
31 float x = p.x * (1.1f + depth * .65f) + sin(t * .22f + depth * 5.f) * .35f;
32 float travel = x * 3.f - t * (.55f + depth * .2f);
33 float beatBend = f.pulse * .17f * sin(x * 8.f - t * 7.f + depth * 9.f);
34 float y = sin(travel + depth * 4.f) * .45f + sin(x * 5.f + t * .7f) * .1f;
35 y += (depth - .5f) * 1.4f + beatBend;
36 float edge = ribbon(p.y - y, .008f + depth * .007f);
37 float glow = ribbon(p.y - y, .04f) * .12f;
38 float3 tint = mix(float3(.04f,.5f,.75f), float3(.6f,1.f,.35f), depth);
39 color += tint * (edge * .48f + glow) * (.7f + .3f * sin(travel * .5f + depth * 6.f));
40 }
41 return color;
42}
43kernel void creativeFrame(texture2d<float,access::sample> previous [[texture(0)]],
44 texture2d<float,access::write> output [[texture(1)]],
45 constant Frame &f [[buffer(0)]], uint2 id [[thread_position_in_grid]]) {
46 if(id.x >= output.get_width() || id.y >= output.get_height()) return;
47 float2 size = float2(output.get_width(),output.get_height());
48 float2 uv = (float2(id) + .5f) / size;
49 float2 p = (uv - .5f) * float2(size.x / size.y,1.f) * 2.f;
50 float3 fresh = f.style == 0 ? violetUndertow(p,f) : auroraWeave(p,f);
51 constexpr sampler history(coord::normalized,address::clamp_to_edge,filter::linear);
52 // Short, controlled trails. No global camera pulse or full-screen scale jump.
53 float3 trail = previous.sample(history,uv + float2(.0008f,-.0004f)).rgb * .32f;
54 float3 color = 1.f - exp(-(fresh + trail) * 1.7f);
55 output.write(float4(color,1),id);
56}