Branchwell

PulseCanvas / app · pulsecanvas.ai / Sources / PulseCanvas / Resources / Strobe.metal

1#include <metal_stdlib>
2using namespace metal;
3// Original fullscreen overlay, composed after all scene effects.
4vertex float4 whiteFlashVertex(uint id [[vertex_id]]) {
5 const float2 p[3] = { float2(-1, -1), float2(3, -1), float2(-1, 3) };
6 return float4(p[id], 0, 1);
7}
8fragment float4 whiteFlashFragment(constant float &strength [[buffer(0)]]) {
9 return float4(1, 1, 1, clamp(strength, 0.0f, 1.0f));
10}
11
12// Uniform Gaussian softness: sample distance is independent of screen position.
13// Clamp at the canvas border so opposite edges never bleed into each other.
14fragment float4 contentBlurFragment(float4 position [[position]],
15 texture2d<float> source [[texture(0)]],
16 constant float2 &step [[buffer(0)]]) {
17 constexpr sampler sampling(coord::normalized, address::clamp_to_edge, filter::linear);
18 float2 uv = position.xy / float2(source.get_width(), source.get_height());
19 const float weights[5] = {70.f, 56.f, 28.f, 8.f, 1.f};
20 float3 color = source.sample(sampling, uv).rgb * weights[0];
21 for (int i = 1; i <= 4; ++i) {
22 color += (source.sample(sampling, uv + step * float(i)).rgb
23 + source.sample(sampling, uv - step * float(i)).rgb) * weights[i];
24 }
25 return float4(color / 256.f, 1);
26}
27
28// Two expanding views trade places only when their weight reaches zero.
29// This keeps forward zoom continuous without a visible reset at each cycle.
30fragment float4 forwardZoomFragment(float4 position [[position]],
31 texture2d<float> source [[texture(0)]],
32 constant float &travel [[buffer(0)]]) {
33 constexpr sampler sampling(coord::normalized, address::clamp_to_edge, filter::linear);
34 float2 uv=position.xy/float2(source.get_width(),source.get_height());
35 float phase=fract(travel), next=fract(phase+.5);
36 float weight=pow(sin(phase*3.14159265),2.f);
37 float3 first=source.sample(sampling,(uv-.5)/exp2(phase*2)+.5).rgb;
38 float3 second=source.sample(sampling,(uv-.5)/exp2(next*2)+.5).rgb;
39 // Retain a wider depth layer so narrow ribbons cannot leave the canvas empty.
40 float3 wide=source.sample(sampling,uv).rgb;
41 return float4(mix(wide,mix(second,first,weight),.65),1);
42}
43
44// Flip OpenGL's bottom-up pixels while preserving the preset's own colors.
45fragment float4 projectMFragment(float4 position [[position]],
46 texture2d<float> source [[texture(0)]],
47 constant float4 &style [[buffer(0)]],
48 constant float2 &outputSize [[buffer(1)]]) {
49 constexpr sampler sampling(coord::normalized, address::clamp_to_edge, filter::linear);
50 // Fragment positions belong to the destination drawable, which may be larger
51 // than the capped OpenGL surface. Map the full drawable to normalized UVs.
52 float2 uv = position.xy / outputSize;
53 float2 p = (uv - .5) / max(.3f, style.y);
54 float c = cos(style.z), s = sin(style.z);
55 uv = float2(c*p.x-s*p.y, s*p.x+c*p.y) + .5;
56 uv.y = 1 - uv.y;
57 return float4(source.sample(sampling, uv).rgb * style.x, 1);
58}