summaryrefslogtreecommitdiff
path: root/src/effects/ntsc.wgsl
blob: 31d4e0db2a6d7b2cef5d6354b7a809c948dab94b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// NTSC post-process effect with fisheye/barrel distortion
#include "sequence_uniforms"
#include "render/fullscreen_uv_vs"
#include "math/noise"

const f32 vignetteRounding = 160.0f;
const f32 vignetteSmoothness = 0.7f;


@group(0) @binding(0) var input_sampler: sampler;
@group(0) @binding(1) var input_texture: texture_2d<f32>;
@group(0) @binding(2) var<uniform> uniforms: UniformsSequenceParams;

// Barrel (fisheye) distortion: strength > 0 = barrel, < 0 = pincushion
fn fisheye(uv: vec2f, strength: f32) -> vec2f {
    let c = uv * 2.0 - 1.0;
    let r2 = c * c;
    return uv * 1.03 * (1.0 + vec2f(.1, .24) * strength * r2);
}

fn vignette(vec2f uv) -> f32 {
    uv *= 1.99;
    f32 amount = 1.0 - sqrt(pow(abs(uv.x), vignetteRounding) + pow(abs(uv.y), vignetteRounding));
    f32 vhard = smoothstep(0., vignetteSmoothness, amount);
    return vhard;
}

@fragment fn fs_main(in: VertexOutput) -> @location(0) vec4f {
    let t = uniforms.time;

    // Fisheye/barrel distortion
    let uv = fisheye(in.uv, 0.18);
    uv = vignette(uv);

    // Black outside screen edges
    if (uv.x < 0.0 || uv.x > 1.0 || uv.y < 0.0 || uv.y > 1.0) {
        discard; // return vec4f(0.0, 0.0, 0.0, 1.0);
    }

    // Chroma separation (horizontal RGB bleeding)
    let bleed = 2.5 / uniforms.resolution.x;
    let r = textureSample(input_texture, input_sampler, uv + vec2f( bleed, 0.0)).r;
    let g = textureSample(input_texture, input_sampler, uv                    ).g;
    let b = textureSample(input_texture, input_sampler, uv - vec2f( bleed, 0.0)).b;
    var col = vec3f(r, g, b);

    // Scanlines
    let scan = sin(uv.y * uniforms.resolution.y * 3.14159265) * 0.5 + 0.5;
    col *= 0.82 + 0.18 * scan;

    // Per-pixel temporal noise
    let pixel = floor(uv * uniforms.resolution);
    let n = hash_2f(pixel + vec2f(t * 47.3, t * 31.7)) * 0.08 - 0.04;
    col += n;

    // Horizontal jitter line (random scanline rolling artifact)
    let jitter_y = fract(t * 0.37);
    let jitter_band = abs(uv.y - jitter_y);
    if (jitter_band < 0.003) {
        col += hash_1f(uv.x * 100.0 + t) * 0.15;
    }

    // Vignette (stronger at corners due to fisheye)
    let vig = in.uv * 2.0 - 1.0;
    col *= 1.0 - dot(vig, vig) * 0.45;

    // Slight NTSC warm tint (boost red/green, attenuate blue)
    col.r *= 1.04;
    col.g *= 1.01;
    col.b *= 0.94;

    return vec4f(clamp(col, vec3f(0.0), vec3f(1.0)), 1.0);
}