CRT television

glint · Examples · Tutorials · Gallery

A test card on an old television: curved glass, scanlines, an RGB shadow mask, colour fringes, static and a rolling bar. Each effect is a line or two.

CRT television: a GLSL shader

An example in glint, a free live shader editor in the browser: the GLSL code floats over the running picture and recompiles as you type.

The code

One pass, in GLSL.

fragment shader

#version 300 es
precision highp float;

uniform float time;
uniform vec2 resolution;

in vec2 v_texcoord;
out vec4 fragColor;

float hash(float n) { return fract(sin(n * 127.1) * 43758.5453); }
float hash(vec2 p) { return fract(sin(dot(p, vec2(127.1, 311.7))) * 43758.5453); }
vec2 hash2(float n) { return fract(sin(vec2(n * 127.1, n * 269.5)) * 43758.5453); }
mat2 rot(float a) { float c = cos(a), s = sin(a); return mat2(c, s, -s, c); }
vec3 pal(float t) { return 0.5 + 0.5 * cos(6.28318 * (t + vec3(0.0, 0.33, 0.67))); }

vec3 testCard(vec2 uv) {
    // Colour bars at 75%, a strip of their complements, and a grey ramp.
    if (uv.y > 0.33) {
        int bar = int(uv.x * 7.0);
        vec3 bars[7] = vec3[7](vec3(0.75), vec3(0.75, 0.75, 0.0), vec3(0.0, 0.75, 0.75), vec3(0.0, 0.75, 0.0), vec3(0.75, 0.0, 0.75), vec3(0.75, 0.0, 0.0), vec3(0.0, 0.0, 0.75));
        vec3 c = bars[clamp(bar, 0, 6)];
        vec2 d = (uv - vec2(0.5, 0.66)) * vec2(4.0 / 3.0, 1.0);
        c = mix(c, vec3(0.9), smoothstep(0.004, 0.0, abs(length(d) - 0.25)));      // a circle
        return c;
    }
    if (uv.y > 0.25) return vec3(0.75) - vec3(uv.x > 0.5 ? 0.75 : 0.0, 0.0, 0.0) * step(0.5, fract(uv.x * 7.0));
    return vec3(floor(uv.x * 8.0) / 7.0);
}

vec2 curve(vec2 uv) {
    uv = uv * 2.0 - 1.0;
    vec2 o = abs(uv.yx) / vec2(5.0, 4.0);
    uv += uv * o * o;
    return uv * 0.5 + 0.5;
}

void main() {
    vec2 st = curve(gl_FragCoord.xy / resolution);
    if (st.x < 0.0 || st.y < 0.0 || st.x > 1.0 || st.y > 1.0) { fragColor = vec4(0.02, 0.02, 0.02, 1.0); return; }
    st.x += sin(st.y * 90.0 + time * 12.0) * 0.0008;                   // unsteady horizontal sync
    vec3 col = vec3(testCard(st + vec2(0.0015, 0.0)).r, testCard(st).g, testCard(st - vec2(0.0015, 0.0)).b);
    col += 0.08 * smoothstep(0.1, 0.0, abs(fract(st.y - time * 0.12) - 0.5) - 0.3);  // rolling bar
    col += (hash(gl_FragCoord.xy + fract(time) * 100.0) - 0.5) * 0.12;         // static
    col *= 0.7 + 0.3 * pow(sin(st.y * 240.0 * 3.14159), 2.0);                 // 240 scanlines
    vec3 mask = vec3(0.75);
    mask[int(mod(gl_FragCoord.x, 3.0))] = 1.25;                                 // the RGB phosphor stripes
    col *= mask;
    vec2 v = st * (1.0 - st);
    col *= pow(v.x * v.y * 16.0, 0.3);                                          // darker corners
    fragColor = vec4(col, 1.0);
}

More More GLSL examples

Bokeh · ASCII · Soap bubbles · Sierpinski tetrahedron · Lightning · Candle · Planet · Isometric city