summaryrefslogtreecommitdiff
path: root/shaders/post/crt.glsl
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-10-30 17:35:06 -0300
committerGabriel Schneider <[email protected]>2026-10-30 17:35:06 -0300
commit6d8da8721141284cf33bb901168b26135893eb86 (patch)
tree791d63401d4e85fbca1d597a8ce0326e70efd545 /shaders/post/crt.glsl
parentefd43a3a2cf03bb741366331158b368abf9ac583 (diff)
parentbf6810a8f9d6cc82050e7d9c0cfb06358301b574 (diff)
downloadpardes-6d8da8721141284cf33bb901168b26135893eb86.tar.gz
pardes-6d8da8721141284cf33bb901168b26135893eb86.zip
Merge latest upstream into the macOS branch for Zig 0.17
Retain the macOS branch history and local Acme selection color while adopting upstream's current core and platform changes. Pin Zig and ZLS to 0.17.0, and fetch the existing zstbi compatibility commit through upstream after its original fork was removed.
Diffstat (limited to 'shaders/post/crt.glsl')
-rw-r--r--shaders/post/crt.glsl85
1 files changed, 85 insertions, 0 deletions
diff --git a/shaders/post/crt.glsl b/shaders/post/crt.glsl
new file mode 100644
index 00000000..970d61ee
--- /dev/null
+++ b/shaders/post/crt.glsl
@@ -0,0 +1,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);
+}