Branchwell

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

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