| 1 | // ============================================================================ |
| 2 | // 6. LumaKeyer |
| 3 | // p[0] = (low, high, invert, blend) |
| 4 | // p[1] = (has_mask, mask_invert, mbm, post_multiply) |
| 5 | // p[2] = (matte_contrast, matte_gamma, channel, pass_mode: 0=matte, 1=composite, 2=single) |
| 6 | // p[3] = (skip_r, skip_g, skip_b, skip_a) |
| 7 | // ============================================================================ |
| 8 | fn luma_key_channel(src: vec4<f32>, ch: i32) -> f32 { |
| 9 | if (ch == 0) { return src.b; } |
| 10 | if (ch == 1) { return src.g; } |
| 11 | if (ch == 2) { return src.r; } |
| 12 | if (ch == 3) { return src.a; } |
| 13 | if (ch == 5) { |
| 14 | let mx = max(src.r, max(src.g, src.b)); |
| 15 | let mn = min(src.r, min(src.g, src.b)); |
| 16 | return select((mx - mn) / mx, 0.0, mx <= 1e-5); |
| 17 | } |
| 18 | return dot(src.rgb, vec3<f32>(0.299, 0.587, 0.114)); |
| 19 | } |
| 20 | |
| 21 | @compute @workgroup_size(8, 8) |
| 22 | fn luma_key_op(@builtin(global_invocation_id) gid: vec3<u32>) { |
| 23 | let dims = textureDimensions(dst_tex); |
| 24 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } |
| 25 | let p = vec2<i32>(gid.xy); |
| 26 | let src = textureLoad(src_a, p, 0); |
| 27 | let pass_mode = i32(u.p[2].w + 0.5); |
| 28 | |
| 29 | var a: f32; |
| 30 | if (pass_mode == 1) { |
| 31 | a = clamp(textureLoad(src_b, p, 0).a, 0.0, 1.0); |
| 32 | } else { |
| 33 | let low = u.p[0].x; |
| 34 | let high = u.p[0].y; |
| 35 | let invert = u.p[0].z > 0.5; |
| 36 | let val = luma_key_channel(src, i32(u.p[2].z + 0.5)); |
| 37 | a = clamp((val - low) / max(high - low, 1e-5), 0.0, 1.0); |
| 38 | let contrast = u.p[2].x; |
| 39 | if (contrast >= 0.0) { |
| 40 | a = clamp(a * (1.0 + 4.0 * contrast), 0.0, 1.0); |
| 41 | } else { |
| 42 | a = clamp(1.0 - (1.0 - a) * (1.0 - 4.0 * contrast), 0.0, 1.0); |
| 43 | } |
| 44 | a = pow(clamp(a, 0.0, 1.0), 1.0 / max(u.p[2].y, 1e-4)); |
| 45 | if (invert) { |
| 46 | a = 1.0 - a; |
| 47 | } |
| 48 | if (pass_mode == 0) { |
| 49 | textureStore(dst_tex, p, vec4<f32>(a, a, a, a)); |
| 50 | return; |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | let post_mult = u.p[1].w > 0.5; |
| 55 | let keyed = vec4<f32>(select(src.rgb, src.rgb * a, post_mult), a); |
| 56 | let has_mask = u.p[1].x > 0.5; |
| 57 | let mbm = has_mask && u.p[1].z > 0.5; |
| 58 | let raw_mask = read_mask(gid.xy, dims, u.p[1].x, u.p[1].y); |
| 59 | let m = clamp(raw_mask * u.p[0].w, 0.0, 1.0); |
| 60 | var out_col = select(mix(src, keyed, m), keyed * m, mbm); |
| 61 | let unproc = select(src, vec4<f32>(0.0), mbm); |
| 62 | out_col = select(out_col, unproc, u.p[3] > vec4<f32>(0.5)); |
| 63 | store_dst(p, out_col); |
| 64 | } |
| 65 | // ============================================================================ |
| 66 | // 8. FastNoise (3D simplex FBM with Seethe, Angle, Discontinuous & gradient LUT) |
| 67 | // p[0] = (x_scale, y_scale, detail, contrast) |
| 68 | // p[1] = (brightness, seethe, discontinuous, center_x) |
| 69 | // p[2] = (center_y, angle_rad, inverted, type) with type 2 = Gradient, else Two Color |
| 70 | // p[3] = color1 RGBA, p[4] = color2 RGBA (used directly in Two Color mode without clamping `val`) |
| 71 | // src_b = 256x1 straight-alpha color LUT indexed by the noise value in Gradient mode |
| 72 | // ============================================================================ |
| 73 | const PERM: array<u32, 256> = array<u32, 256>( |
| 74 | 151u, 160u, 137u, 91u, 90u, 15u, 131u, 13u, 201u, 95u, 96u, 53u, 194u, 233u, 7u, 225u, |
| 75 | 140u, 36u, 103u, 30u, 69u, 142u, 8u, 99u, 37u, 240u, 21u, 10u, 23u, 190u, 6u, 148u, |
| 76 | 247u, 120u, 234u, 75u, 0u, 26u, 197u, 62u, 94u, 252u, 219u, 203u, 117u, 35u, 11u, 32u, |
| 77 | 57u, 177u, 33u, 88u, 237u, 149u, 56u, 87u, 174u, 20u, 125u, 136u, 171u, 168u, 68u, 175u, |
| 78 | 74u, 165u, 71u, 134u, 139u, 48u, 27u, 166u, 77u, 146u, 158u, 231u, 83u, 111u, 229u, 122u, |
| 79 | 60u, 211u, 133u, 230u, 220u, 105u, 92u, 41u, 55u, 46u, 245u, 40u, 244u, 102u, 143u, 54u, |
| 80 | 65u, 25u, 63u, 161u, 1u, 216u, 80u, 73u, 209u, 76u, 132u, 187u, 208u, 89u, 18u, 169u, |
| 81 | 200u, 196u, 135u, 130u, 116u, 188u, 159u, 86u, 164u, 100u, 109u, 198u, 173u, 186u, 3u, 64u, |
| 82 | 52u, 217u, 226u, 250u, 124u, 123u, 5u, 202u, 38u, 147u, 118u, 126u, 255u, 82u, 85u, 212u, |
| 83 | 207u, 206u, 59u, 227u, 47u, 16u, 58u, 17u, 182u, 189u, 28u, 42u, 223u, 183u, 170u, 213u, |
| 84 | 119u, 248u, 152u, 2u, 44u, 154u, 163u, 70u, 221u, 153u, 101u, 155u, 167u, 43u, 172u, 9u, |
| 85 | 129u, 22u, 39u, 253u, 19u, 98u, 108u, 110u, 79u, 113u, 224u, 232u, 178u, 185u, 112u, 104u, |
| 86 | 218u, 246u, 97u, 228u, 251u, 34u, 242u, 193u, 238u, 210u, 144u, 12u, 191u, 179u, 162u, 241u, |
| 87 | 81u, 51u, 145u, 235u, 249u, 14u, 239u, 107u, 49u, 192u, 214u, 31u, 181u, 199u, 106u, 157u, |
| 88 | 184u, 84u, 204u, 176u, 115u, 121u, 50u, 45u, 127u, 4u, 150u, 254u, 138u, 236u, 205u, 93u, |
| 89 | 222u, 114u, 67u, 29u, 24u, 72u, 243u, 141u, 128u, 195u, 78u, 66u, 215u, 61u, 156u, 180u |
| 90 | ); |
| 91 | |
| 92 | const GRAD16: array<vec3<f32>, 16> = array<vec3<f32>, 16>( |
| 93 | vec3<f32>(0.0, 1.0, 1.0), vec3<f32>(0.0, 1.0, -1.0), vec3<f32>(0.0, -1.0, 1.0), vec3<f32>(0.0, -1.0, -1.0), |
| 94 | vec3<f32>(1.0, 0.0, 1.0), vec3<f32>(1.0, 0.0, -1.0), vec3<f32>(-1.0, 0.0, 1.0), vec3<f32>(-1.0, 0.0, -1.0), |
| 95 | vec3<f32>(1.0, 1.0, 0.0), vec3<f32>(1.0, -1.0, 0.0), vec3<f32>(-1.0, 1.0, 0.0), vec3<f32>(-1.0, -1.0, 0.0), |
| 96 | vec3<f32>(1.0, 0.0, -1.0), vec3<f32>(-1.0, 0.0, -1.0), vec3<f32>(0.0, -1.0, 1.0), vec3<f32>(0.0, 1.0, 1.0), |
| 97 | ); |
| 98 | |
| 99 | fn simplex_corner(ijk: vec3<i32>, d: vec3<f32>) -> f32 { |
| 100 | let t = SIMPLEX_RADIUS2 - dot(d, d); |
| 101 | if (t <= 0.0) { return 0.0; } |
| 102 | let c = vec3<u32>(ijk & vec3<i32>(255)); |
| 103 | let h = PERM[(c.z + PERM[(c.y + PERM[c.x]) & 255u]) & 255u] & 15u; |
| 104 | let t2 = t * t; |
| 105 | return t2 * t2 * dot(GRAD16[h], d); |
| 106 | } |
| 107 | |
| 108 | // Fusion's lattice: Gustavson-style 3D simplex noise with kernel (0.5196 - r^2)^4 and a 16-entry |
| 109 | // gradient table, recovered from Float32 probes (residual 2e-7). |
| 110 | fn simplex3d(p: vec3<f32>) -> f32 { |
| 111 | let cell = floor(p + (p.x + p.y + p.z) * (1.0 / 3.0)); |
| 112 | let ijk = vec3<i32>(cell); |
| 113 | let d0 = p - (cell - (cell.x + cell.y + cell.z) * (1.0 / 6.0)); |
| 114 | var a = vec3<i32>(0); |
| 115 | var b = vec3<i32>(0); |
| 116 | if (d0.x >= d0.y) { |
| 117 | if (d0.y >= d0.z) { a = vec3<i32>(1, 0, 0); b = vec3<i32>(1, 1, 0); } |
| 118 | else if (d0.x >= d0.z) { a = vec3<i32>(1, 0, 0); b = vec3<i32>(1, 0, 1); } |
| 119 | else { a = vec3<i32>(0, 0, 1); b = vec3<i32>(1, 0, 1); } |
| 120 | } else { |
| 121 | if (d0.y < d0.z) { a = vec3<i32>(0, 0, 1); b = vec3<i32>(0, 1, 1); } |
| 122 | else if (d0.x < d0.z) { a = vec3<i32>(0, 1, 0); b = vec3<i32>(0, 1, 1); } |
| 123 | else { a = vec3<i32>(0, 1, 0); b = vec3<i32>(1, 1, 0); } |
| 124 | } |
| 125 | let g = 1.0 / 6.0; |
| 126 | return SIMPLEX_GAIN * ( |
| 127 | simplex_corner(ijk, d0) |
| 128 | + simplex_corner(ijk + a, d0 - vec3<f32>(a) + g) |
| 129 | + simplex_corner(ijk + b, d0 - vec3<f32>(b) + 2.0 * g) |
| 130 | + simplex_corner(ijk + vec3<i32>(1), d0 - 0.5) |
| 131 | ); |
| 132 | } |
| 133 | |
| 134 | // Octave i samples the lattice at 2^i * (x, y, seethe) + 50.3 * i with weight 0.5^i; a fractional |
| 135 | // Detail scales the last octave. Discontinuous sums 2|n| per octave, Contrast pivots on 0.5 (on 0 |
| 136 | // when Discontinuous), Inverted runs before Brightness, Brightness adds half its value, and |
| 137 | // Gradient + Discontinuous shifts the lookup by 0.25. |
| 138 | const SIMPLEX_RADIUS2: f32 = 0.5196152; |
| 139 | const SIMPLEX_GAIN: f32 = 16.642; |
| 140 | const OCTAVE_OFFSET: f32 = 50.3; |
| 141 | |
| 142 | @compute @workgroup_size(8, 8) |
| 143 | fn fast_noise_op(@builtin(global_invocation_id) gid: vec3<u32>) { |
| 144 | let dims = textureDimensions(dst_tex); |
| 145 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } |
| 146 | let center = vec2<f32>(u.p[1].w, u.p[2].x); |
| 147 | let scale = u.p[0].xy; |
| 148 | let detail = clamp(u.p[0].z, 0.0, 10.0); |
| 149 | let contrast = u.p[0].w; |
| 150 | let brightness = u.p[1].x; |
| 151 | let seethe = u.p[1].y; |
| 152 | let disc = u.p[1].z > 0.5; |
| 153 | let angle = u.p[2].y; |
| 154 | let gradient = u.p[2].w > 1.5; |
| 155 | |
| 156 | var sum = 0.0; |
| 157 | if (abs(contrast) > 1e-6 && detail > 0.0) { |
| 158 | // Frame-width units, y up, measured from the pixel corner. |
| 159 | let w = f32(dims.x); |
| 160 | let h = f32(dims.y); |
| 161 | let inv_w = 1.0 / w; |
| 162 | let rel = vec2<f32>(f32(gid.x) * inv_w, (h - 1.0 - f32(gid.y)) * inv_w) - center * vec2<f32>(1.0, h * inv_w); |
| 163 | var p: vec2<f32>; |
| 164 | if (abs(angle) < 1e-6) { |
| 165 | p = rel * scale; |
| 166 | } else { |
| 167 | let ca = cos(angle); |
| 168 | let sa = sin(angle); |
| 169 | p = vec2<f32>(rel.x * ca + rel.y * sa, rel.y * ca - rel.x * sa) * scale; |
| 170 | } |
| 171 | |
| 172 | let full_octaves = i32(floor(detail)); |
| 173 | let frac_w = detail - f32(full_octaves); |
| 174 | var amp = select(1.0, 2.0, disc); |
| 175 | var cur = vec3<f32>(p, seethe); |
| 176 | var off = 0.0; |
| 177 | for (var i = 0; i < full_octaves; i = i + 1) { |
| 178 | let n = simplex3d(cur + vec3<f32>(off)); |
| 179 | sum = sum + amp * select(n, abs(n), disc); |
| 180 | cur = cur * 2.0; |
| 181 | off = off + OCTAVE_OFFSET; |
| 182 | amp = amp * 0.5; |
| 183 | } |
| 184 | if (frac_w > 0.0) { |
| 185 | let n = simplex3d(cur + vec3<f32>(off)); |
| 186 | sum = sum + (amp * frac_w) * select(n, abs(n), disc); |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | var val = select(0.5 + sum, sum, disc); |
| 191 | val = select((val - 0.5) * contrast + 0.5, val * contrast, disc); |
| 192 | if (gradient && disc) { val = val + 0.25; } |
| 193 | if (u.p[2].z > 0.5) { val = 1.0 - val; } |
| 194 | val = val + 0.5 * brightness; |
| 195 | |
| 196 | // Fusion leaves the ramp color unpremultiplied; downstream Merge treats it as premultiplied. |
| 197 | // Two-Color mode (Type == 0) interpolates Color1 -> Color2 without clamping `val`, allowing |
| 198 | // Contrast > 1.0 to extrapolate HDR/bright highlights; Gradient mode clamps lookup to [0, 1]. |
| 199 | var out_col: vec4<f32>; |
| 200 | if (gradient) { |
| 201 | let lut_n = f32(textureDimensions(src_b).x); |
| 202 | let lut_u = (0.5 + clamp(val, 0.0, 1.0) * (lut_n - 1.0)) / lut_n; |
| 203 | out_col = textureSampleLevel(src_b, samp_lin, vec2<f32>(lut_u, 0.5), 0.0); |
| 204 | } else { |
| 205 | out_col = mix(u.p[3], u.p[4], val); |
| 206 | } |
| 207 | store_dst(vec2<i32>(gid.xy), out_col); |
| 208 | } |
| 209 | // ============================================================================ |
| 210 | // 12. Fuse.Outline (Clover's exact 2020 Outline.fuse passes!) |
| 211 | // outline_boost: boosts alpha * 120 (clamped in R/A via normalized scale, so we pass scale factor!) |
| 212 | // outline_finish: clamps dilated alpha, tints with outline color, then composites original on top! |
| 213 | // ============================================================================ |
| 214 | @compute @workgroup_size(8, 8) |
| 215 | fn outline_boost(@builtin(global_invocation_id) gid: vec3<u32>) { |
| 216 | let dims = textureDimensions(dst_tex); |
| 217 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } |
| 218 | let src = textureLoad(src_a, vec2<i32>(gid.xy), 0); |
| 219 | let a = clamp(src.a * 120.0, 0.0, 1.0); |
| 220 | textureStore(dst_tex, vec2<i32>(gid.xy), vec4<f32>(a, a, a, a)); |
| 221 | } |
| 222 | |
| 223 | @compute @workgroup_size(8, 8) |
| 224 | fn outline_tint(@builtin(global_invocation_id) gid: vec3<u32>) { |
| 225 | let dims = textureDimensions(dst_tex); |
| 226 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } |
| 227 | let blurred = textureLoad(src_a, vec2<i32>(gid.xy), 0); |
| 228 | let col = u.p[0]; // (r, g, b, a) |
| 229 | // Because we clamped boosted alpha to 1.0 in u8, emulate the 120x dilation threshold |
| 230 | let a = clamp(blurred.a * u.p[1].x, 0.0, 1.0) * col.a; |
| 231 | textureStore(dst_tex, vec2<i32>(gid.xy), vec4<f32>(col.rgb * a, a)); |
| 232 | } |
| 233 | |
| 234 | @compute @workgroup_size(8, 8) |
| 235 | fn outline_composite(@builtin(global_invocation_id) gid: vec3<u32>) { |
| 236 | let dims = textureDimensions(dst_tex); |
| 237 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } |
| 238 | let outline = textureLoad(src_a, vec2<i32>(gid.xy), 0); |
| 239 | let orig = textureLoad(src_b, vec2<i32>(gid.xy), 0); |
| 240 | // Original over outline |
| 241 | let out_rgb = orig.rgb + outline.rgb * (1.0 - orig.a); |
| 242 | let out_a = orig.a + outline.a * (1.0 - orig.a); |
| 243 | store_dst(vec2<i32>(gid.xy), vec4<f32>(out_rgb, out_a)); |
| 244 | } |
| 245 | |
| 246 | // ============================================================================ |
| 247 | // 13. Custom Wave Shader (matches `sin(x|y * w * n1 + n2 * n1 * w) * 0.5 + 0.5` per channel) |
| 248 | // p[0] = (n1, n2, w_ref, 0) |
| 249 | // p[1] = (r_mode, g_mode, b_mode, a_const) where mode: 0=const 0, 1=sin(x*...), 2=sin(y*...) |
| 250 | // ============================================================================ |
| 251 | fn eval_wave_ch(mode: i32, fu: vec2<f32>, n1: f32, n2: f32, w: f32) -> f32 { |
| 252 | let deg2rad = 0.017453292519943295; |
| 253 | if (mode == 1) { |
| 254 | return sin((fu.x * w * n1 + n2 * n1 * w) * deg2rad) * 0.5 + 0.5; |
| 255 | } else if (mode == 2) { |
| 256 | return sin((fu.y * w * n1 + n2 * n1 * w) * deg2rad) * 0.5 + 0.5; |
| 257 | } |
| 258 | return 0.0; |
| 259 | } |
| 260 | |
| 261 | @compute @workgroup_size(8, 8) |
| 262 | fn custom_wave_op(@builtin(global_invocation_id) gid: vec3<u32>) { |
| 263 | let dims = textureDimensions(dst_tex); |
| 264 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } |
| 265 | let fu = pix_to_fu(gid.xy, dims); |
| 266 | let n1 = u.p[0].x; |
| 267 | let n2 = u.p[0].y; |
| 268 | let w = u.p[0].z; |
| 269 | // Fusion CustomTool trig functions operate in DEGREES! |
| 270 | let r = eval_wave_ch(i32(u.p[1].x + 0.5), fu, n1, n2, w); |
| 271 | let g = eval_wave_ch(i32(u.p[1].y + 0.5), fu, n1, n2, w); |
| 272 | let b = eval_wave_ch(i32(u.p[1].z + 0.5), fu, n1, n2, w); |
| 273 | let a = u.p[1].w; |
| 274 | textureStore(dst_tex, vec2<i32>(gid.xy), vec4<f32>(r, g, b, a)); |
| 275 | } |
| 276 | // ============================================================================ |
| 277 | // 26. UltraKeyer (Fusion color-difference chroma keyer + background spill subtract) |
| 278 | // p[0] = (bg_r, bg_g, bg_b, matte_sep) |
| 279 | // p[1] = (low_thresh, high_thresh, post_mult, spill_sup) |
| 280 | // p[2] = (blend, has_mask, mask_invert, invert_matte) |
| 281 | // p[3] = (r_lo, r_hi, g_lo, g_hi) |
| 282 | // p[4] = (b_lo, b_hi, l_lo, l_hi) |
| 283 | // p[5] = (bg_untouched, gamma, mbm, range_active) |
| 284 | // p[6] = (skip_r, skip_g, skip_b, skip_a) |
| 285 | // ============================================================================ |
| 286 | fn uk_axis_in(v: f32, lo: f32, hi: f32) -> bool { |
| 287 | if (lo <= 1e-5 && hi >= 1.0 - 1e-5) { return true; } |
| 288 | return v >= lo && v <= hi; |
| 289 | } |
| 290 | |
| 291 | fn uk_in_range(src: vec4<f32>) -> bool { |
| 292 | if (u.p[5].w < 0.5) { return false; } |
| 293 | let lum = dot(src.rgb, vec3<f32>(0.299, 0.587, 0.114)); |
| 294 | let v_r = 0.5 + (src.r - lum) / 1.60; |
| 295 | let v_g = 0.5 + (src.g - lum) / 1.32; |
| 296 | let v_b = 0.5 + (src.b - lum) / 2.24; |
| 297 | return uk_axis_in(v_r, u.p[3].x, u.p[3].y) |
| 298 | && uk_axis_in(v_g, u.p[3].z, u.p[3].w) |
| 299 | && uk_axis_in(v_b, u.p[4].x, u.p[4].y) |
| 300 | && uk_axis_in(lum, u.p[4].z, u.p[4].w); |
| 301 | } |
| 302 | |
| 303 | @compute @workgroup_size(8, 8) |
| 304 | fn ultra_keyer_op(@builtin(global_invocation_id) gid: vec3<u32>) { |
| 305 | let dims = textureDimensions(dst_tex); |
| 306 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } |
| 307 | let src = textureLoad(src_a, vec2<i32>(gid.xy), 0); |
| 308 | let bg = u.p[0].xyz; |
| 309 | let lo_th = u.p[1].x; |
| 310 | let hi_th = max(u.p[1].y, lo_th + 1e-4); |
| 311 | let post_mult = u.p[1].z > 0.5; |
| 312 | let spill_sup = u.p[1].w; |
| 313 | let blend = u.p[2].x; |
| 314 | let inv_matte = u.p[2].w > 0.5; |
| 315 | let bg_untouched = u.p[5].x > 0.5; |
| 316 | let gamma = max(u.p[5].y, 1e-4); |
| 317 | |
| 318 | var rgb = src.rgb; |
| 319 | var alpha: f32; |
| 320 | if (bg_untouched) { |
| 321 | var raw_a = clamp(1.0 - max(src.b - src.r, 0.0), 0.0, 1.0); |
| 322 | if (u.p[0].w > 1e-4) { |
| 323 | raw_a = clamp(pow(raw_a, 1.0 + u.p[0].w * 4.0), 0.0, 1.0); |
| 324 | } |
| 325 | alpha = pow(clamp((raw_a - lo_th) / (hi_th - lo_th), 0.0, 1.0), 1.0 / gamma); |
| 326 | if (inv_matte) { alpha = 1.0 - alpha; } |
| 327 | if (uk_in_range(src)) { alpha = 0.0; } |
| 328 | rgb = src.rgb * alpha; |
| 329 | } else { |
| 330 | // Fusion 21.1 UltraKeyer exact color-difference matte + background subtraction |
| 331 | var k = 0.0; |
| 332 | if (bg.g >= bg.r && bg.g >= bg.b) { |
| 333 | let bg_ex = max(bg.g - 0.5 * (bg.r + bg.b), 1e-4); |
| 334 | k = max((src.g - 0.5 * (src.r + src.b)) / bg_ex, 0.0); |
| 335 | } else if (bg.b >= bg.r && bg.b >= bg.g) { |
| 336 | let bg_ex = max(bg.b - 0.5 * (bg.r + bg.g), 1e-4); |
| 337 | k = max((src.b - 0.5 * (src.r + src.g)) / bg_ex, 0.0); |
| 338 | } else { |
| 339 | let bg_ex = max(bg.r - 0.5 * (bg.g + bg.b), 1e-4); |
| 340 | k = max((src.r - 0.5 * (src.g + src.b)) / bg_ex, 0.0); |
| 341 | } |
| 342 | |
| 343 | rgb = max(src.rgb - bg * k, vec3<f32>(0.0)); |
| 344 | var raw_a = clamp(max(1.0 - k, max(rgb.r, max(rgb.g, rgb.b))), 0.0, 1.0); |
| 345 | if (u.p[0].w > 1e-4) { |
| 346 | raw_a = clamp(pow(raw_a, 1.0 + u.p[0].w * 4.0), 0.0, 1.0); |
| 347 | } |
| 348 | alpha = pow(clamp((raw_a - lo_th) / (hi_th - lo_th), 0.0, 1.0), 1.0 / gamma); |
| 349 | |
| 350 | if (inv_matte) { alpha = 1.0 - alpha; } |
| 351 | alpha = alpha * src.a; |
| 352 | if (uk_in_range(src)) { alpha = 0.0; } |
| 353 | if (alpha <= 1e-5) { |
| 354 | rgb = vec3<f32>(0.0); |
| 355 | } |
| 356 | |
| 357 | if (spill_sup > 1e-4) { |
| 358 | if (bg.g >= bg.r && bg.g >= bg.b) { |
| 359 | let lim = max(rgb.r, rgb.b); |
| 360 | if (rgb.g > lim) { rgb.g = mix(rgb.g, lim, spill_sup); } |
| 361 | } else if (bg.b >= bg.r && bg.b >= bg.g) { |
| 362 | let lim = max(rgb.r, rgb.g); |
| 363 | if (rgb.b > lim) { rgb.b = mix(rgb.b, lim, spill_sup); } |
| 364 | } else { |
| 365 | let lim = max(rgb.g, rgb.b); |
| 366 | if (rgb.r > lim) { rgb.r = mix(rgb.r, lim, spill_sup); } |
| 367 | } |
| 368 | } |
| 369 | if (post_mult) { |
| 370 | rgb = rgb * alpha; |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | let keyed = vec4<f32>(rgb, alpha); |
| 375 | let has_mask = u.p[2].y > 0.5; |
| 376 | let mbm = has_mask && u.p[5].z > 0.5; |
| 377 | let raw_mask = read_mask(gid.xy, dims, u.p[2].y, u.p[2].z); |
| 378 | let m = clamp(raw_mask * blend, 0.0, 1.0); |
| 379 | var out_col = select(mix(src, keyed, m), keyed * m, mbm); |
| 380 | let unproc = select(src, vec4<f32>(0.0), mbm); |
| 381 | out_col = select(out_col, unproc, u.p[6] > vec4<f32>(0.5)); |
| 382 | store_dst(vec2<i32>(gid.xy), out_col); |
| 383 | } |
| 384 | // ============================================================================ |
| 385 | // 30. Plasma (Fusion 4-circle concentric/sine plasma interference generator) |
| 386 | // p[0..3] = (cx, cy, c_scale, c_type) for Circles 1..4 (type 0=Off, 1=Linear, >=2=Sine) |
| 387 | // p[4] = (scale, phase, red_phase, green_phase) |
| 388 | // p[5] = (blue_phase, blend, has_mask, mask_invert) |
| 389 | // p[6] = (alpha_phase, operation: 0=Add 1=Subtract 2=Multiply 3=Divide 4=Maximum 5=Minimum 6=Difference, 0, 0) |
| 390 | // ============================================================================ |
| 391 | fn plasma_circle_term(u_pos: vec2<f32>, aspect_inv: f32, c: vec4<f32>, master_scale: f32, wrap: bool) -> f32 { |
| 392 | let ctype = i32(c.w + 0.5); |
| 393 | if (ctype <= 0) { return 0.0; } |
| 394 | let delta = vec2<f32>(u_pos.x - c.x, (u_pos.y - c.y) * aspect_inv); |
| 395 | let d = length(delta); |
| 396 | let s = max(master_scale * c.z, 1e-5); |
| 397 | if (ctype == 1) { |
| 398 | let lin = 39.26990817 * d / s; |
| 399 | // Smooth the Type-1 cone tip at d = 0 with a tiny radial offset matching Fusion, |
| 400 | // preventing 1/t spikes in Divide mode without visibly shifting linear fringes. |
| 401 | let lin_r = sqrt(lin * lin + 0.001916 * 0.001916) - 0.001916; |
| 402 | return select(lin_r, lin_r - 6.2831853 * floor(lin_r / 6.2831853), wrap); |
| 403 | } |
| 404 | let r0 = 0.0125 * s; |
| 405 | let dr = sqrt(d * d + r0 * r0) - r0; |
| 406 | return 2.2089323 * (1.0 + sin(33.6842105 * dr / s)); |
| 407 | } |
| 408 | |
| 409 | @compute @workgroup_size(8, 8) |
| 410 | fn plasma_op(@builtin(global_invocation_id) gid: vec3<u32>) { |
| 411 | let dims = textureDimensions(dst_tex); |
| 412 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } |
| 413 | let fdims = vec2<f32>(dims); |
| 414 | // Fusion Plasma maps center (0.5, 0.5) to pixel (W/2 - 1, H/2) => u=(x+1)/W, v=(H-y)/H |
| 415 | let u_pos = vec2<f32>((f32(gid.x) + 1.0) / fdims.x, (fdims.y - f32(gid.y)) / fdims.y); |
| 416 | let aspect_inv = fdims.y / max(fdims.x, 1.0); |
| 417 | let master_scale = max(u.p[4].x, 1e-5); |
| 418 | let tau = 6.2831853; |
| 419 | let ph = u.p[4].y; |
| 420 | |
| 421 | // Fusion folds the four circle terms with `Operation`. Maximum and Minimum see Type 1 as a sawtooth |
| 422 | // in [0, 2pi); Add, Subtract, Multiply, Divide and Difference see the raw linear ramp. |
| 423 | let op = i32(u.p[6].y + 0.5); |
| 424 | let wrap = op == 4 || op == 5; |
| 425 | var t = array<f32, 4>(0.0, 0.0, 0.0, 0.0); |
| 426 | var active_count = 0u; |
| 427 | for (var i = 0u; i < 4u; i = i + 1u) { |
| 428 | t[i] = plasma_circle_term(u_pos, aspect_inv, u.p[i], master_scale, wrap); |
| 429 | if (i32(u.p[i].w + 0.5) > 0) { |
| 430 | active_count = active_count + 1u; |
| 431 | } |
| 432 | } |
| 433 | var th = 0.0; |
| 434 | switch op { |
| 435 | case 1: { th = -(t[0] + t[1] + t[2] + t[3]); } |
| 436 | case 2: { |
| 437 | // Fusion multiplies all 4 circles normalized by (tau / 4) and scales back by tau. |
| 438 | let sc = tau * 0.25; |
| 439 | th = tau * (t[0] / sc) * (t[1] / sc) * (t[2] / sc) * (t[3] / sc); |
| 440 | } |
| 441 | case 3: { |
| 442 | // Divide starts at 1.0 and divides by each active circle normalized by (tau / N). |
| 443 | if (active_count > 0u) { |
| 444 | let sc = tau / f32(active_count); |
| 445 | var acc = 1.0; |
| 446 | var bad = false; |
| 447 | for (var i = 0u; i < 4u; i = i + 1u) { |
| 448 | if (i32(u.p[i].w + 0.5) <= 0) { continue; } |
| 449 | if (abs(t[i]) <= 1e-6) { |
| 450 | bad = true; |
| 451 | break; |
| 452 | } |
| 453 | acc = acc / (t[i] / sc); |
| 454 | } |
| 455 | let res = tau * acc; |
| 456 | th = select(res, -tau * ph, bad || !(abs(res) < 205887.4)); |
| 457 | } |
| 458 | } |
| 459 | case 4: { th = max(max(t[0], t[1]), max(t[2], t[3])); } |
| 460 | case 5: { th = min(min(t[0], t[1]), min(t[2], t[3])); } |
| 461 | case 6: { th = abs(abs(abs(t[0] - t[1]) - t[2]) - t[3]); } |
| 462 | default: { th = t[0] + t[1] + t[2] + t[3]; } |
| 463 | } |
| 464 | |
| 465 | if (!(abs(th) < 1e6)) { th = -tau * ph; } |
| 466 | let r = 0.5 + 0.5 * cos(th + tau * (ph + u.p[4].z)); |
| 467 | let g = 0.5 + 0.5 * cos(th + tau * (ph + u.p[4].w)); |
| 468 | let b = 0.5 + 0.5 * cos(th + tau * (ph + u.p[5].x)); |
| 469 | let a = 0.5 + 0.5 * cos(th + tau * (ph + u.p[6].x)); |
| 470 | |
| 471 | let m = read_mask(gid.xy, dims, u.p[5].z, u.p[5].w) * u.p[5].y; |
| 472 | let out_col = vec4<f32>(r, g, b, a) * clamp(m, 0.0, 1.0); |
| 473 | store_dst(vec2<i32>(gid.xy), out_col); |
| 474 | } |