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
|
// Crt: a slot-mask tube's light, with nothing that moves a pixel. There is
// no barrel: every click lands where it is drawn, rails and grips at the
// window's edges included. What is left is light: a beam profile for the
// scanlines, an aperture mask, a soft bloom of the brightest ink, a faint
// vignette and the hum of a live tube. Everything is worked out in linear
// light and dithered back into 8 bits, so the vignette never bands. It is a
// Shadertoy file: pardes puts its prefix before it (shaders/post/prefix.glsl).
const float TAU = 6.28318530718;
// Level 3 glows wider and from dimmer ink.
float bloomRadiusPx() { return pardesByLevel(1.75, 1.75, 3.0); }
float bloomThreshold() { return pardesByLevel(0.6, 0.6, 0.3); } // linear-light channel peak
const float BLOOM_SOFTNESS = 0.25;
const float SCAN_PERIOD_PX = 3.0;
const float MASK_PERIOD_PX = 3.0;
const float HUM_HZ = 0.11;
// Each strength at levels 1, 2 and 3 (`Crt 1`..`Crt 3`): 2 is the default,
// 3 as strong as the old barrel CRT looked.
float chromaPx() { return pardesByLevel(0.2, 0.35, 2.2); } // red and blue apart, at the corners
float bloomStrength() { return pardesByLevel(0.03, 0.05, 0.3); }
float scanStrength() { return pardesByLevel(0.035, 0.07, 0.42); }
float maskStrength() { return pardesByLevel(0.01, 0.018, 0.12); }
float vignetteStrength() { return pardesByLevel(0.06, 0.12, 0.42); } // at the far corners only
float humStrength() { return pardesByLevel(0.002, 0.004, 0.012); }
float exposure() { return pardesByLevel(1.008, 1.015, 1.1); }
vec3 toLinear(vec3 c) {
return mix(pow((c + 0.055) / 1.055, vec3(2.4)), c / 12.92, lessThanEqual(c, vec3(0.04045)));
}
vec3 toSrgb(vec3 c) {
c = max(c, vec3(0.0));
return mix(1.055 * pow(c, vec3(1.0 / 2.4)) - 0.055, c * 12.92, lessThanEqual(c, vec3(0.0031308)));
}
vec3 scene(vec2 uv) {
return toLinear(texture(iChannel0, uv).rgb);
}
vec3 brightPass(vec2 uv) {
vec3 c = scene(uv);
float peak = max(c.r, max(c.g, c.b));
return c * smoothstep(bloomThreshold(), bloomThreshold() + BLOOM_SOFTNESS, peak);
}
// Interleaved gradient noise: a dither with no visible pattern.
float ign(vec2 p) {
return fract(52.9829189 * fract(dot(p, vec2(0.06711056, 0.00583715))));
}
void mainImage(out vec4 fragColor, in vec2 fragCoord) {
vec2 size = iResolution.xy;
vec2 texel = 1.0 / size;
vec2 uv = fragCoord / size;
vec2 cc = uv * 2.0 - 1.0;
float r2 = dot(cc, cc);
// The beam converges least at the corners: red and blue drift apart
// there, by a third of a pixel.
vec2 chroma = cc * (chromaPx() * r2 * 0.5) * texel;
vec3 col = vec3(scene(uv + chroma).r, scene(uv).g, scene(uv - chroma).b);
vec2 b = texel * bloomRadiusPx();
vec3 bloom = brightPass(uv + vec2(b.x, 0.0)) + brightPass(uv - vec2(b.x, 0.0)) +
brightPass(uv + vec2(0.0, b.y)) + brightPass(uv - vec2(0.0, b.y));
col += bloom * (0.25 * bloomStrength());
// A beam is brightest on its line and falls off between lines; the
// mask is three phosphor stripes a triad. Both average to one, so the
// theme keeps its brightness.
// Periods in logical pixels: the same look at any density.
float density = max(1.0, pardesScale);
float line = 0.5 + 0.5 * cos(fragCoord.y * TAU / (SCAN_PERIOD_PX * density));
col *= 1.0 - scanStrength() * (line * line - 0.375) / 0.625;
col *= 1.0 + maskStrength() * cos(fragCoord.x * TAU / (MASK_PERIOD_PX * density) + vec3(0.0, TAU / 3.0, 2.0 * TAU / 3.0));
float vignette = 1.0 - vignetteStrength() * pow(clamp(r2 * 0.5, 0.0, 1.0), 1.6);
float hum = 1.0 + humStrength() * sin((uv.y * 1.35 - iTime * HUM_HZ) * TAU);
col *= vignette * hum * exposure();
vec3 srgb = toSrgb(col);
srgb += (ign(fragCoord + float(iFrame % 64) * vec2(5.588238, 5.588238)) - 0.5) / 255.0;
fragColor = vec4(clamp(srgb, 0.0, 1.0), texture(iChannel0, uv).a);
}
|