summaryrefslogtreecommitdiff
path: root/src/effects/ntsc.wgsl
blob: a701a252e33227ad56f07b056e52430d3655f4f9 (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include "sequence_uniforms"
#include "render/fullscreen_uv_vs"
#include "math/noise"
#include "math/color"
#include "math/color_c64"
#include "debug/debug_print"

const PI = 3.14159265;
const TAU = 6.28318530718;

const XSIZE = 54.0 * 8.;
const YSIZE = 33.0 * 8.;
const SCAN_FLICKER = 2.33;
const X_INTERFERENCE = 1.1;
const Y_INTERFERENCE = 0.101;
const LUMA_BRIGHTNESS = 1.1;
const CHROMA_SATURATION = 1.6;
const BLUR_SIZE = 0.2;
const LUMA_BLUR = 1.7;
const CHROMA_BLUR = 0.7;
const CHROMA_SIZE = 6.0;
const SUB_CARRIER = 2.1;
const CROSS_TALK = 0.1;
const CROSS_Y_INTERFERENCE = 30.;
const CHROMA_MOD_FREQ = (0.4 * PI);


@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, scale: f32) -> vec2f {
  const strength = vec2f(0.1, 0.24);
  return uv * (1.0 + scale * strength * (uv * uv).yx) * 0.60 + .50;
}

fn vignette(uv: vec2f) -> f32 {
  const vignetteRounding = 160.0;
  const vignetteSmoothness = 0.7;
  let uv2 = 2.0 * uv - 1.0;
  let amount = 1.0 - sqrt(pow(abs(uv2.x), vignetteRounding) + pow(abs(uv2.y), vignetteRounding));
  return smoothstep(0., vignetteSmoothness, amount);
}

// returns Luma, chroma subcarrier level, phase, transparency
fn get_luma_chroma_phase_a(uv: vec2f) -> vec4f {
  let rgba = textureSample(input_texture, input_sampler, uv);
  return rgba_to_luma_chroma_phase(rgba, uv.y, YSIZE);
}

fn get_value(uv: vec2f, off: f32, yscale: f32) -> vec4f {
  return get_luma_chroma_phase_a(uv + off * vec2f(1., yscale));
}

fn peak(y: f32, ypos: f32, scale: f32) -> f32 {
  return clamp((y - 1.) * scale * log(abs(y - ypos)), 0.0, 1.0);
}

// 12-taps horizontal filtering
const luma_filter = array<f32, 2 * 12 + 1>(
   0.0105, 0.0134, 0.0057,-0.0242,-0.0824,
  -0.1562,-0.2078,-0.1850,-0.0546, 0.1626,
   0.3852, 0.5095, 0.5163, 0.4678, 0.2844,
   0.0515,-0.1308,-0.2082,-0.1891,-0.1206,
  -0.0511,-0.0065, 0.0114, 0.0127, 0.008
);
const chroma_filter = array<f32, 2 * 12 + 1>(
  0.001,  0.0010, 0.0001, 0.0002, -0.0003,
  0.0062, 0.0120,-0.0079, 0.0978,  0.1059,
 -0.0394, 0.2732, 0.2941, 0.1529, -0.021,
  0.1347, 0.0415,-0.0032, 0.0115,  0.002,
 -0.0001, 0.0002, 0.001,  0.001,   0.001
);

fn randomized_f32(p: vec2f, t: f32) -> f32 {
  return hash_2f_alt(vec2f(p * 0.152 + t * 1500. + 50.0));
}



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

    // Fisheye/barrel distortion
    var uv = fisheye(in.st, 0.18);
    let mframe = sin(bt * 32.) * 1.2;
    uv.y += mframe * SCAN_FLICKER / uniforms.resolution.y;  // flicker at target resolution

    // interference
    let cur_line = round(uv.y * YSIZE);
    var r = randomized_f32(vec2f(0.0, cur_line), uniforms.time);
    if (r > 0.995) { r *= 3.0; }

    let x_interf = X_INTERFERENCE * r / XSIZE;
    let y_interf = Y_INTERFERENCE * r * peak(uv.y, 0.2, 0.03);
    uv.x += x_interf - y_interf;

    // luma fringing
    let d = (BLUR_SIZE + y_interf * 100.0) / XSIZE;
    var lc_signal = vec4f(0.0);
    for (var i = 0; i < 25; i += 1) {
      let offset = f32(i) - 12.0;
      let suml = luma_filter[i] * get_value(uv, offset * d, 0.67);
      let sumc = chroma_filter[i] * get_value(uv, offset * d * CHROMA_SIZE, 0.67);
      lc_signal += vec4f(suml.x, sumc.y, sumc.z, suml.a);
    }
    let base = get_luma_chroma_phase_a(uv);
    var signal = mix(base, lc_signal, vec4f(LUMA_BLUR, CHROMA_BLUR, CHROMA_BLUR, 1.));

    // luma / chroma saturation
    let lchroma = signal.y * CHROMA_SATURATION;
    let phase = signal.z * TAU;

    signal.x *= LUMA_BRIGHTNESS;
    signal.y = lchroma * sin(phase);
    signal.z = lchroma * cos(phase);
    // Slight NTSC warm tint (boost red/green, attenuate blue)
    signal *= vec4f(1.04, 1.01, .94, 1.);

    // color subcarrier signal, crosstalk
    let chroma_phase = t * 60.0 * PI * 0.6667;
    let mod_phase = chroma_phase + dot(uv, vec2f(1.0, 0.1)) * CHROMA_MOD_FREQ * XSIZE * 2.0;
    let scarrier = SUB_CARRIER * lchroma;
    let i_mod = cos(mod_phase);
    let q_mod = sin(mod_phase);

    signal.x *= 1.0 + CROSS_TALK * scarrier * q_mod - y_interf * CROSS_Y_INTERFERENCE;
    signal.y *= 1.0 + scarrier * i_mod;
    signal.z *= 1.0 + scarrier * q_mod;

    var col = yiqa_to_rgba(signal);
    col = Dither(col, uv, XSIZE, YSIZE);

    let border_col = get_border_c64(uv, uniforms.beat_time, YSIZE);

    let v_strength = vignette(uv);
    let scanl = 0.82 + 0.5 * sin(PI * uv.y * uniforms.resolution.y / 2.);
    col = scanl * mix(border_col, col, v_strength);
    col = clamp(col, vec4f(0., 0., 0., 1.), vec4f(1.0));

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

    col = debug_f32(col, in.position.xy / 2., vec2f(100., 75.), uniforms.beat_time);
    col = debug_str(col, in.position.xy / 2., vec2f(100., 150.), vec4u(0x48656C6Cu, 0x6F000000u, 0u, 0u), 5u);
    return col;
}