CRT Phosphor Scanline Grille shader

crt-phosphor-scanline-grille.glsl · 813 lines · GLSL ES 3.00 · MIT

CRT Phosphor Scanline GrilleSci-Fi & Noise
CRT Phosphor Scanline Grille WebGL2 procedural shader preview
FPS 60BUFFER 960×540SOURCE 813 linesSPEC GLSL ES 3.00

Seamless PNG Texture Baker
1-Click Engine Shader Export
WebGL2 .glsl
.glsl
Godot 4 .gdshader
Three.js GLSL3

Engine shader code & wrappers

Sci-Fi & Micro-Optics
crt-phosphor-scanline-grille.glsl (813 lines)
#version 300 es
// CRT Phosphor Scanline Grille (crt-phosphor-scanline-grille.glsl)
// Upstream study: https://github.com/gokayfem/glsl-studies/blob/main/electronics/lib/02_crt_aperture_grille_phosphor_stripes.glsl
// License: MIT (Copyright 2026 Gokay Aydogan) + MIT/CC0 helpers — https://pixelfx.io/LICENSE-SHADERS.txt
// Uniforms: uResolution (vec2), uTime (float), uOpen (0..1), uLightMode (0..3), uCamMode (0..1)
precision highp float;
precision highp int;

uniform vec2 uResolution;
uniform float uTime;
uniform float uOpen;
uniform int uLightMode;
uniform int uCamMode;

out vec4 fragColor;

// ============================================================================
// Trinitron CRT Aperture Grille & Phosphor Stripes Study
// 3D thick curved leaded-glass CRT bezel module grounded on dark studio bench
// ============================================================================

const float PI = 3.14159265359;
const float SCREEN_YC = 0.99;
const float SCREEN_HW = 0.85;
const float SCREEN_HH = 0.62;
const float GLASS_THICK = 0.088;

// Material IDs
const float MAT_BEZEL      = 1.0;
const float MAT_BEZEL_TRIM = 2.0;
const float MAT_GLASS      = 3.0;
const float MAT_METAL      = 4.0;
const float MAT_RUBBER     = 5.0;
const float MAT_LED        = 6.0;
const float MAT_KNOB       = 7.0;

// ----------------------------------------------------------------------------
// Hash & Procedural Noise
// ----------------------------------------------------------------------------
float hash12(vec2 p) {
    vec3 p3 = fract(vec3(p.xyx) * 0.1031);
    p3 += dot(p3, p3.yzx + 33.33);
    return fract((p3.x + p3.y) * p3.z);
}

float valueNoise(vec2 p) {
    vec2 i = floor(p);
    vec2 f = fract(p);
    f = f * f * (3.0 - 2.0 * f);
    float a = hash12(i);
    float b = hash12(i + vec2(1.0, 0.0));
    float c = hash12(i + vec2(0.0, 1.0));
    float d = hash12(i + vec2(1.0, 1.0));
    return mix(mix(a, b, f.x), mix(c, d, f.x), f.y);
}

float fbm2(vec2 p) {
    float v = 0.0;
    float a = 0.5;
    mat2 m = mat2(0.80, 0.60, -0.60, 0.80);
    for (int i = 0; i < 3; i++) {
        v += a * valueNoise(p);
        p = m * p * 2.03 + 17.1;
        a *= 0.5;
    }
    return v;
}

// ----------------------------------------------------------------------------
// SDF Primitives & Operators
// ----------------------------------------------------------------------------
float sdBox(vec3 p, vec3 b) {
    vec3 q = abs(p) - b;
    return length(max(q, 0.0)) + min(max(q.x, max(q.y, q.z)), 0.0);
}

float sdRoundBox(vec3 p, vec3 b, float r) {
    vec3 q = abs(p) - b;
    return length(max(q, 0.0)) + min(max(q.x, max(q.y, q.z)), 0.0) - r;
}

float sdRoundRect2D(vec2 p, vec2 b, float r) {
    vec2 q = abs(p) - b;
    return length(max(q, 0.0)) + min(max(q.x, q.y), 0.0) - r;
}

float sdCylinderZ(vec3 p, float r, float h) {
    vec2 d = abs(vec2(length(p.xy), p.z)) - vec2(r, h);
    return min(max(d.x, d.y), 0.0) + length(max(d, 0.0));
}

float sdCylinderY(vec3 p, float r, float h) {
    vec2 d = abs(vec2(length(p.xz), p.y)) - vec2(r, h);
    return min(max(d.x, d.y), 0.0) + length(max(d, 0.0));
}

float smin(float a, float b, float k) {
    float h = clamp(0.5 + 0.5 * (b - a) / k, 0.0, 1.0);
    return mix(b, a, h) - k * h * (1.0 - h);
}

float smax(float a, float b, float k) {
    return -smin(-a, -b, k);
}

vec2 opU(vec2 a, vec2 b) {
    return (a.x < b.x) ? a : b;
}

// Cylindrical Trinitron faceplate front Z profile (strong horizontal curve, subtle vertical curve)
float crtFrontZ(vec2 xy) {
    float dy = xy.y - SCREEN_YC;
    return 0.448 - 0.148 * xy.x * xy.x - 0.018 * dy * dy;
}

// ----------------------------------------------------------------------------
// Hero SDF (CRT Bezel Module grounded on y = 0)
// ----------------------------------------------------------------------------
vec2 mapHero(vec3 p) {
    // 1. Four low-profile rubber feet resting on y = 0
    vec3 pf = p;
    pf.x = abs(pf.x) - 0.84;
    pf.z = abs(pf.z) - 0.30;
    pf.y -= 0.032;
    float dFeet = sdCylinderY(pf, 0.115, 0.032) - 0.006;
    vec2 res = vec2(dFeet, MAT_RUBBER);

    // 2. Main front broadcast bezel cabinet (y: 0.06..1.78, x: -1.10..1.10, z: -0.04..0.48)
    vec3 pb = p - vec3(0.0, 0.92, 0.22);
    float dFrontCab = sdRoundBox(pb, vec3(1.04, 0.80, 0.20), 0.06);

    // Rear stepped chassis housing (slightly narrower, extending back to z = -0.46)
    vec3 pr = p - vec3(0.0, 0.91, -0.18);
    float dRearCab = sdRoundBox(pr, vec3(0.95, 0.74, 0.24), 0.055);
    float dBody = smin(dFrontCab, dRearCab, 0.035);

    // Subtle recessed front perimeter seam groove
    float dSeam = sdBox(p - vec3(0.0, 0.92, 0.04), vec3(1.12, 0.88, 0.008));
    dBody = max(dBody, -dSeam);

    // Side ventilation louvers carved into rear chassis flanks
    vec3 pv = p;
    pv.x = abs(pv.x) - 0.985;
    pv.y -= 1.02;
    pv.z += 0.19;
    float slotIdx = clamp(round(pv.y / 0.055), -5.0, 5.0);
    vec3 pSlot = vec3(pv.x, pv.y - slotIdx * 0.055, pv.z);
    float dVents = sdRoundBox(pSlot, vec3(0.045, 0.011, 0.16), 0.004);
    dBody = max(dBody, -dVents);

    // Chamfered rectangular-rounded screen cavity cutout in front bezel
    vec2 qScr = p.xy - vec2(0.0, SCREEN_YC);
    // Taper the aperture opening inward as z goes back (45-deg chamfer lip)
    float bevelExpand = clamp((p.z - 0.36) * 0.42, 0.0, 0.075);
    float dOpening2D = sdRoundRect2D(qScr, vec2(SCREEN_HW - 0.055, SCREEN_HH - 0.055) + bevelExpand, 0.065);
    float dCavity = max(dOpening2D, 0.24 - p.z);
    dBody = smax(dBody, -dCavity, 0.014);

    // Bottom recessed control strip trough (y: 0.155..0.285)
    float dCtrlRecess = sdRoundBox(p - vec3(0.0, 0.22, 0.485), vec3(0.86, 0.052, 0.028), 0.012);
    dBody = smax(dBody, -dCtrlRecess, 0.008);

    res = opU(res, vec2(dBody, MAT_BEZEL));

    // 3. Dark inner bezel mask / trim collar hugging the curved glass perimeter
    float dMaskOuter = sdRoundRect2D(qScr, vec2(SCREEN_HW + 0.015, SCREEN_HH + 0.015), 0.072);
    float dMaskInner = sdRoundRect2D(qScr, vec2(SCREEN_HW - 0.032, SCREEN_HH - 0.032), 0.058);
    float zFront = crtFrontZ(p.xy);
    float dTrimRing = max(max(dMaskOuter, -dMaskInner), abs(p.z - (zFront - 0.015)) - 0.038);
    res = opU(res, vec2(dTrimRing, MAT_BEZEL_TRIM));

    // 4. Thick curved Trinitron leaded-glass faceplate
    // Front curved surface is p.z - crtFrontZ(p.xy) <= 0, bounded laterally inside bezel
    float dGlassBounds = sdRoundRect2D(qScr, vec2(SCREEN_HW + 0.005, SCREEN_HH + 0.005), 0.062);
    // Scale curved surface distance conservatively by inverse gradient length
    float gradLen = sqrt(1.0 + (0.296 * p.x) * (0.296 * p.x) + (0.036 * qScr.y) * (0.036 * qScr.y));
    float dGlassFace = (p.z - zFront) / gradLen;
    float dGlassBack = (zFront - 0.16 - p.z) / gradLen;
    float dGlass = max(max(dGlassFace, dGlassBack), dGlassBounds);
    res = opU(res, vec2(dGlass, MAT_GLASS));

    // 5. Bottom control strip hardware: power button, glowing status LED, 3 knurled micro-knobs
    // Power square button (left)
    float dPwrBtn = sdRoundBox(p - vec3(-0.75, 0.22, 0.468), vec3(0.028, 0.022, 0.016), 0.004);
    res = opU(res, vec2(dPwrBtn, MAT_BEZEL_TRIM));

    // Status LED jewel
    float dLed = sdCylinderZ(p - vec3(-0.64, 0.22, 0.465), 0.013, 0.008) - 0.003;
    res = opU(res, vec2(dLed, MAT_LED));

    // Center subtle metallic brand badge plate
    float dBadge = sdRoundBox(p - vec3(0.0, 0.22, 0.462), vec3(0.145, 0.016, 0.006), 0.003);
    res = opU(res, vec2(dBadge, MAT_METAL));

    // 3 rotary micro-knobs on the right side (BRIGHT / CONTR / PHASE)
    for (int i = 0; i < 3; i++) {
        float kx = 0.44 + float(i) * 0.155;
        vec3 pk = p - vec3(kx, 0.22, 0.474);
        float ang = atan(pk.y, pk.x);
        float knurl = 0.0014 * sin(ang * 18.0);
        float dKnob = sdCylinderZ(pk, 0.026 + knurl, 0.020) - 0.003;
        res = opU(res, vec2(dKnob, MAT_KNOB));
    }

    // 4 subtle recessed bezel corner hex bolts
    vec3 pBolt = p;
    pBolt.x = abs(pBolt.x) - 0.97;
    pBolt.y = abs(pBolt.y - 0.92) - 0.72;
    pBolt.z -= 0.474;
    float dBolt = sdCylinderZ(pBolt, 0.016, 0.006) - 0.002;
    res = opU(res, vec2(dBolt, MAT_METAL));

    return res;
}

vec2 mapScene(vec3 p) {
    vec2 hero = mapHero(p);
    float dBench = p.y;
    return (dBench < hero.x) ? vec2(dBench, 0.0) : hero;
}

// ----------------------------------------------------------------------------
// Unit-Weight Tetrahedral Normal (Zero-Bug Rule #1)
// ----------------------------------------------------------------------------
vec3 calcHeroNormal(vec3 p) {
    const vec2 k = vec2(1.0, -1.0);
    const float e = 0.0008;
    return normalize(
        k.xyy * mapHero(p + k.xyy * e).x +
        k.yyx * mapHero(p + k.yyx * e).x +
        k.yxy * mapHero(p + k.yxy * e).x +
        k.xxx * mapHero(p + k.xxx * e).x
    );
}

// ----------------------------------------------------------------------------
// Clean Hero-Only Soft Shadow (Zero-Bug Rule #3: min(res, k*h/t), no ph)
// ----------------------------------------------------------------------------
float calcSoftShadow(vec3 ro, vec3 rd, float tMax, float k) {
    float res = 1.0;
    float t = 0.025;
    for (int i = 0; i < 48; i++) {
        float h = mapHero(ro + rd * t).x;
        if (h < 0.0005) return 0.0;
        res = min(res, k * h / t);
        t += max(h, 0.015);
        if (t > tMax) break;
    }
    return clamp(res, 0.0, 1.0);
}

float calcAO(vec3 p, vec3 n) {
    float occ = 0.0;
    float sca = 1.0;
    for (int i = 1; i <= 5; i++) {
        float h = 0.02 + 0.055 * float(i);
        float d = mapScene(p + h * n).x;
        occ += (h - d) * sca;
        sca *= 0.72;
    }
    return clamp(1.0 - 2.6 * occ, 0.12, 1.0);
}

// ----------------------------------------------------------------------------
// CRT Signal Generators: Retro SMPTE Bars & Lissajous Oscilloscope
// ----------------------------------------------------------------------------
vec3 evalSMPTE(vec2 uv) {
    // uv in [-1, 1] x [-1, 1]
    float x01 = clamp(uv.x * 0.5 + 0.5, 0.0, 0.9999);
    float y01 = clamp(uv.y * 0.5 + 0.5, 0.0, 0.9999);

    vec3 col = vec3(0.06);

    if (y01 > 0.33) {
        // Top 67%: 7 standard 75% SMPTE color bars
        int idx = int(floor(x01 * 7.0));
        if      (idx == 0) col = vec3(0.76, 0.76, 0.76); // 75% White
        else if (idx == 1) col = vec3(0.76, 0.74, 0.07); // Yellow
        else if (idx == 2) col = vec3(0.07, 0.75, 0.76); // Cyan
        else if (idx == 3) col = vec3(0.07, 0.75, 0.09); // Green
        else if (idx == 4) col = vec3(0.75, 0.08, 0.74); // Magenta
        else if (idx == 5) col = vec3(0.76, 0.08, 0.07); // Red
        else               col = vec3(0.08, 0.10, 0.78); // Blue
    } else if (y01 > 0.25) {
        // Middle 8%: Reverse castellation strip
        int idx = int(floor(x01 * 7.0));
        if      (idx == 0) col = vec3(0.08, 0.10, 0.78);
        else if (idx == 1) col = vec3(0.05, 0.05, 0.06);
        else if (idx == 2) col = vec3(0.75, 0.08, 0.74);
        else if (idx == 3) col = vec3(0.05, 0.05, 0.06);
        else if (idx == 4) col = vec3(0.07, 0.75, 0.76);
        else if (idx == 5) col = vec3(0.05, 0.05, 0.06);
        else               col = vec3(0.76, 0.76, 0.76);
    } else {
        // Bottom 25%: -I, 100% White, +Q, Black, PLUGE tri-step, Black
        if      (x01 < 0.178) col = vec3(0.04, 0.16, 0.34); // -I
        else if (x01 < 0.357) col = vec3(0.95, 0.96, 0.95); // 100% White
        else if (x01 < 0.536) col = vec3(0.24, 0.05, 0.42); // +Q
        else if (x01 < 0.714) col = vec3(0.04, 0.04, 0.05); // Black
        else if (x01 < 0.762) col = vec3(0.02, 0.02, 0.03); // Superblack
        else if (x01 < 0.810) col = vec3(0.05, 0.05, 0.06); // Black
        else if (x01 < 0.857) col = vec3(0.11, 0.11, 0.12); // +4 IRE PLUGE
        else                  col = vec3(0.04, 0.04, 0.05);
    }

    // Subtle rolling vertical refresh band for life
    float refreshY = fract(-uTime * 0.16);
    float band = exp(-abs(y01 - refreshY) * 18.0) * 0.07;
    col *= (1.0 + band);

    return col;
}

float sdSegment2D(vec2 p, vec2 a, vec2 b) {
    vec2 pa = p - a;
    vec2 ba = b - a;
    float h = clamp(dot(pa, ba) / max(dot(ba, ba), 1e-6), 0.0, 1.0);
    return length(pa - ba * h);
}

vec3 evalOscilloscope(vec2 uv) {
    // Deep P31 phosphor bed
    vec3 col = vec3(0.012, 0.048, 0.042);

    // Subtle illuminated graticule grid (10x8 divisions)
    vec2 gridUV = uv * vec2(5.0, 4.0);
    vec2 gDist = abs(fract(gridUV + 0.5) - 0.5) / max(fwidth(gridUV), vec2(0.015));
    float gridLine = 1.0 - smoothstep(0.6, 1.8, min(gDist.x, gDist.y));
    // Center axis ticks
    vec2 axisDist = abs(uv) / max(fwidth(uv), vec2(0.004));
    float axisLine = 1.0 - smoothstep(0.7, 1.9, min(axisDist.x, axisDist.y));
    col += vec3(0.03, 0.16, 0.14) * (gridLine * 0.38 + axisLine * 0.45);

    // Lissajous parametric figure: x = 0.74 sin(3t + phi), y = 0.64 sin(2t + 0.6)
    float phi = 0.65 + uTime * 0.55;
    const int STEPS = 44;
    float minD = 1e5;
    float minD2 = 1e5;
    vec2 prev = vec2(0.74 * sin(phi), 0.64 * sin(0.6));
    vec2 prev2 = vec2(0.74 * sin(phi - 0.22), 0.64 * sin(0.6 - 0.15));

    for (int i = 1; i <= STEPS; i++) {
        float t = (float(i) / float(STEPS)) * 2.0 * PI;
        vec2 cur = vec2(0.74 * sin(3.0 * t + phi), 0.64 * sin(2.0 * t + 0.6));
        minD = min(minD, sdSegment2D(uv, prev, cur));
        prev = cur;

        // Subtle persistence ghost trace
        vec2 cur2 = vec2(0.74 * sin(3.0 * t + phi - 0.22), 0.64 * sin(2.0 * t + 0.45));
        minD2 = min(minD2, sdSegment2D(uv, prev2, cur2));
        prev2 = cur2;
    }

    // Core beam + phosphor green-cyan halo
    float core = exp(-minD * 58.0);
    float innerGlow = exp(-minD * 18.0);
    float outerHalo = exp(-minD * 6.5);
    float ghost = exp(-minD2 * 24.0) * 0.28;

    vec3 beamCol = vec3(0.10, 0.95, 0.72) * innerGlow * 0.95
                 + vec3(0.05, 0.55, 0.48) * outerHalo * 0.35
                 + vec3(0.75, 1.00, 0.95) * core * 1.15
                 + vec3(0.06, 0.68, 0.55) * ghost;

    return col + beamCol;
}

vec3 evalSignalRaw(vec2 uv) {
    float openT = smoothstep(0.0, 1.0, clamp(uOpen, 0.0, 1.0));
    vec3 smpte = evalSMPTE(uv);
    vec3 scope = evalOscilloscope(uv);
    return mix(smpte, scope, openT);
}

// Analytical antialiased periodic pulse for phosphor stripes & scanlines
float aaPulse(float phase, float duty, float fw) {
    // Returns smooth filtered pulse in [0, 1] with exact DC average = duty when fw >= 1
    float f = clamp(fw, 0.001, 2.0);
    float w = clamp(duty, 0.02, 0.98);
    float p = fract(phase);
    float edge = clamp(f * 0.55, 0.02, 0.45);
    float pulse = smoothstep(0.0, edge, p) - smoothstep(w, w + edge, p);
    // Blend smoothly toward DC average as pixel footprint approaches Nyquist
    float nyq = smoothstep(0.35, 1.15, fw);
    return mix(pulse, w, nyq);
}

// Full Trinitron Aperture-Grille Phosphor Plane evaluation
vec3 evalTrinitronPhosphor(vec2 uv) {
    // Soft mask at active phosphor edges (dark graphite border inside leaded glass)
    vec2 edgeD = smoothstep(vec2(1.02), vec2(0.955), abs(uv));
    float activeMask = edgeD.x * edgeD.y;
    if (activeMask <= 0.001) return vec3(0.015, 0.018, 0.020);

    vec3 sig = evalSignalRaw(uv);

    // 1. Vertical RGB phosphor stripe triads (aperture grille)
    // 120 triads across full width -> 60 cycles per unit uv.x
    float triadCoord = (uv.x * 0.5 + 0.5) * 120.0;
    float fwX = fwidth(triadCoord);

    // Slightly staggered R / G / B vertical stripes within each triad, separated by thin grille slits
    float rStripe = aaPulse(triadCoord - 0.04, 0.25, fwX);
    float gStripe = aaPulse(triadCoord - 0.37, 0.25, fwX);
    float bStripe = aaPulse(triadCoord - 0.70, 0.25, fwX);
    // Normalize energy so average triad gain stays balanced (~1.0)
    vec3 triadMask = vec3(rStripe, gStripe, bStripe) * 3.35;

    // 2. Crisp horizontal scanlines (fwidth-antialiased) with electron-beam spot bloom
    float scanCoord = (uv.y * 0.5 + 0.5) * 92.0;
    float fwY = fwidth(scanCoord);
    float sigLum = dot(sig, vec3(0.299, 0.587, 0.114));
    float scanDuty = mix(0.46, 0.78, clamp(sigLum, 0.0, 1.0));
    float scanMask = aaPulse(scanCoord, scanDuty, fwY) / max(scanDuty, 0.46) * 0.88 + 0.12;

    // 3. Signature Trinitron horizontal damper-wire shadow lines at uv.y = +/-0.34
    float fwDamper = max(fwidth(uv.y), 0.0025);
    float wire1 = smoothstep(0.0025, 0.0025 + fwDamper * 1.4, abs(uv.y - 0.34));
    float wire2 = smoothstep(0.0025, 0.0025 + fwDamper * 1.4, abs(uv.y + 0.34));
    float damperShadow = mix(0.32, 1.0, wire1 * wire2);

    // Combine phosphor emission
    vec3 phosphor = sig * triadMask * scanMask * damperShadow;

    // 4. Soft internal leaded-glass halation (wide-radius internal scatter)
    vec3 blurSig = (
        evalSignalRaw(uv + vec2( 0.055,  0.035)) +
        evalSignalRaw(uv + vec2(-0.055,  0.035)) +
        evalSignalRaw(uv + vec2( 0.055, -0.035)) +
        evalSignalRaw(uv + vec2(-0.055, -0.035)) +
        sig
    ) * 0.20;
    vec3 halation = blurSig * vec3(0.95, 1.0, 0.98) * 0.24;

    // Subtle CRT corner vignette
    float vig = 1.0 - 0.18 * dot(uv * vec2(0.78, 0.82), uv * vec2(0.78, 0.82));

    vec3 outCol = (phosphor + halation) * vig;
    // Unlit phosphor powder base reflectance inside glass
    vec3 phosphorBed = vec3(0.025, 0.030, 0.028);
    return mix(phosphorBed, phosphorBed + outCol, activeMask);
}

// ----------------------------------------------------------------------------
// Studio Environment: Softboxes + Negative-Fill Flags (Zero-Bug Rule #4)
// ----------------------------------------------------------------------------
vec3 evalStudioEnv(vec3 rd, int lightMode) {
    // Base neutral dark studio dome
    float up = clamp(rd.y * 0.5 + 0.5, 0.0, 1.0);
    vec3 bg = mix(vec3(0.045, 0.048, 0.052), vec3(0.11, 0.115, 0.125), smoothstep(0.2, 0.95, up));

    // Overhead main studio softbox
    vec3 sbTopDir = normalize(vec3(-0.22, 0.88, 0.42));
    float sbTop = smoothstep(0.84, 0.94, dot(rd, sbTopDir));

    // Left vertical strip softbox (catches cylindrical glass curve cleanly)
    vec3 sbLeftDir = normalize(vec3(-0.82, 0.26, 0.51));
    float sbLeft = smoothstep(0.88, 0.96, dot(rd, sbLeftDir));

    // Right vertical strip softbox
    vec3 sbRightDir = normalize(vec3(0.84, 0.22, 0.49));
    float sbRight = smoothstep(0.90, 0.97, dot(rd, sbRightDir));

    // Negative-fill studio flags (carve crisp dark reflection bands on curved glass)
    vec3 flag1Dir = normalize(vec3(-0.46, 0.18, 0.87));
    float flag1 = smoothstep(0.86, 0.95, dot(rd, flag1Dir));
    vec3 flag2Dir = normalize(vec3(0.52, 0.12, 0.84));
    float flag2 = smoothstep(0.87, 0.95, dot(rd, flag2Dir));

    if (lightMode == 1) {
        // Pure neutral grayscale studio
        bg = vec3(mix(0.06, 0.15, smoothstep(0.15, 0.95, up)));
        bg += vec3(1.10) * sbTop + vec3(0.65) * sbLeft + vec3(0.50) * sbRight;
    } else if (lightMode == 2) {
        // Grazing: dramatic side strip softboxes, dim overhead
        bg *= 0.72;
        bg += vec3(0.45) * sbTop + vec3(1.65, 1.60, 1.52) * sbLeft + vec3(0.85, 0.92, 1.05) * sbRight;
    } else if (lightMode == 3) {
        // Backlit: bright rear cyclorama glow
        float backGlow = smoothstep(0.15, 0.95, -rd.z) * smoothstep(-0.1, 0.55, rd.y);
        bg += vec3(0.95, 0.97, 1.02) * backGlow * 1.45;
        bg += vec3(0.55) * sbTop + vec3(0.45) * sbLeft + vec3(0.45) * sbRight;
    } else {
        // Hero studio lighting
        bg += vec3(1.18, 1.15, 1.10) * sbTop
            + vec3(0.85, 0.90, 0.98) * sbLeft
            + vec3(0.58, 0.56, 0.54) * sbRight;
    }

    // Apply negative-fill flags
    bg *= (1.0 - 0.72 * max(flag1, flag2));
    return max(bg, vec3(0.02));
}

// ----------------------------------------------------------------------------
// Studio PBR Direct Lighting Helper
// ----------------------------------------------------------------------------
float D_GGX(float NoH, float rough) {
    float a = rough * rough;
    float a2 = a * a;
    float d = (NoH * NoH) * (a2 - 1.0) + 1.0;
    return a2 / max(PI * d * d, 1e-5);
}

float G_Schlick(float NoV, float NoL, float rough) {
    float r = rough + 1.0;
    float k = (r * r) * 0.125;
    float gv = NoV / (NoV * (1.0 - k) + k);
    float gl = NoL / (NoL * (1.0 - k) + k);
    return gv * gl;
}

vec3 shadeDirectLight(vec3 p, vec3 N, vec3 V, vec3 L, vec3 lightCol,
                      vec3 albedo, float rough, float metal, float shadow) {
    float NoL = max(dot(N, L), 0.0);
    if (NoL <= 0.0001) return vec3(0.0);

    vec3 H = normalize(V + L);
    float NoV = max(dot(N, V), 0.001);
    float NoH = max(dot(N, H), 0.0);
    float VoH = max(dot(V, H), 0.0);

    vec3 F0 = mix(vec3(0.04), albedo, metal);
    vec3 F = F0 + (1.0 - F0) * pow(1.0 - VoH, 5.0);
    float D = D_GGX(NoH, rough);
    float G = G_Schlick(NoV, NoL, rough);

    vec3 spec = (D * G * F) / max(4.0 * NoV * NoL, 0.001);
    vec3 kD = (1.0 - F) * (1.0 - metal);
    return (kD * albedo / PI + spec) * lightCol * NoL * shadow;
}

// ----------------------------------------------------------------------------
// ACES Filmic Tone Mapping
// ----------------------------------------------------------------------------
vec3 acesToneMap(vec3 x) {
    const float a = 2.51;
    const float b = 0.03;
    const float c = 2.43;
    const float d = 0.59;
    const float e = 0.14;
    return clamp((x * (a * x + b)) / (x * (c * x + d) + e), 0.0, 1.0);
}

// ----------------------------------------------------------------------------
// Main Render
// ----------------------------------------------------------------------------
void main() {
    vec2 uv = (gl_FragCoord.xy - 0.5 * uResolution.xy) / uResolution.y;

    // Camera setup: uCamMode 0 = Hero 3/4, uCamMode 1 = ~2.5x Macro on curved phosphor glass & bezel corner
    vec3 ro, ta;
    float fl;
    if (uCamMode == 1) {
        ro = vec3(0.76, 1.18, 1.56);
        ta = vec3(0.30, 1.07, 0.36);
        fl = 2.55;
    } else {
        ro = vec3(2.15, 1.82, 4.65);
        ta = vec3(0.0, 0.80, 0.02);
        fl = 2.10;
    }

    vec3 fw = normalize(ta - ro);
    vec3 rt = normalize(cross(fw, vec3(0.0, 1.0, 0.0)));
    vec3 up = cross(rt, fw);
    vec3 rd = normalize(uv.x * rt + uv.y * up + fl * fw);

    // Raymarch scene
    float t = 0.0;
    float matId = -1.0;
    bool hit = false;
    const float TMAX = 18.0;

    for (int i = 0; i < 120; i++) {
        vec3 p = ro + rd * t;
        vec2 h = mapScene(p);
        if (h.x < 0.0007) {
            hit = true;
            matId = h.y;
            break;
        }
        t += h.x * 0.92;
        if (t > TMAX) break;
    }

    // Studio cyclorama background
    vec3 col = evalStudioEnv(rd, uLightMode);
    if (uLightMode != 1) {
        float bgVig = 1.0 - 0.25 * dot(uv, uv);
        col *= bgVig;
    }

    if (hit) {
        vec3 p = ro + rd * t;
        float dHero = mapHero(p).x;
        // Zero-Bug Rule #2: exact bench classification
        bool isBench = (p.y <= dHero + 0.001);

        vec3 N = isBench ? vec3(0.0, 1.0, 0.0) : calcHeroNormal(p);
        vec3 V = -rd;
        float NoV = max(dot(N, V), 0.001);
        float ao = calcAO(p, N);

        // Configure studio light rig per uLightMode
        vec3 keyDir, fillDir, rimDir;
        vec3 keyCol, fillCol, rimCol;
        float emitScale = 1.0;

        if (uLightMode == 1) {
            // Pure neutral grayscale, emission muted
            keyDir  = normalize(vec3(-0.55, 0.78, 0.62));
            fillDir = normalize(vec3( 0.72, 0.42, 0.55));
            rimDir  = normalize(vec3( 0.20, 0.65, -0.74));
            keyCol  = vec3(1.85);
            fillCol = vec3(0.62);
            rimCol  = vec3(0.75);
            emitScale = 0.0;
        } else if (uLightMode == 2) {
            // Grazing raking light
            keyDir  = normalize(vec3(-0.92, 0.28, 0.26));
            fillDir = normalize(vec3( 0.75, 0.22, 0.62));
            rimDir  = normalize(vec3( 0.45, 0.75, -0.48));
            keyCol  = vec3(2.35, 2.26, 2.12);
            fillCol = vec3(0.35, 0.39, 0.46);
            rimCol  = vec3(1.15, 1.22, 1.35);
            emitScale = 0.55;
        } else if (uLightMode == 3) {
            // Backlit silhouette & rim
            keyDir  = normalize(vec3(-0.28, 0.62, -0.73));
            fillDir = normalize(vec3( 0.55, 0.38,  0.74));
            rimDir  = normalize(vec3( 0.68, 0.52, -0.52));
            keyCol  = vec3(2.30, 2.34, 2.42);
            fillCol = vec3(0.45, 0.47, 0.52);
            rimCol  = vec3(1.75, 1.72, 1.68);
            emitScale = 0.72;
        } else {
            // Hero balanced studio lighting
            keyDir  = normalize(vec3(-0.52, 0.76, 0.64));
            fillDir = normalize(vec3( 0.74, 0.40, 0.54));
            rimDir  = normalize(vec3( 0.26, 0.62, -0.74));
            keyCol  = vec3(1.88, 1.84, 1.78);
            fillCol = vec3(0.56, 0.61, 0.70);
            rimCol  = vec3(0.95, 1.00, 1.12);
            emitScale = 1.0;
        }

        // Shadow on key light using Zero-Bug Rule #3 (bias p + N * 0.014)
        float shKey = calcSoftShadow(p + N * 0.014, keyDir, 6.0, 10.0);

        if (isBench) {
            // Dark studio bench with subtle micro-textured satin grain
            float grain = fbm2(p.xz * 6.5) * 0.025 + fbm2(p.xz * 26.0) * 0.012;
            vec3 benchAlbedo = (uLightMode == 1)
                ? vec3(0.13 + grain * 0.4)
                : vec3(0.085, 0.090, 0.098) + grain * 0.45;
            float benchRough = 0.36 + grain * 1.4;

            vec3 lit = shadeDirectLight(p, N, V, keyDir, keyCol, benchAlbedo, benchRough, 0.0, shKey);
            lit += shadeDirectLight(p, N, V, fillDir, fillCol, benchAlbedo, benchRough, 0.0, 0.85);
            lit += shadeDirectLight(p, N, V, rimDir, rimCol, benchAlbedo, benchRough, 0.0, 0.85);

            // Studio environment reflection on bench
            vec3 R = reflect(-V, N);
            float fres = 0.035 + 0.965 * pow(1.0 - NoV, 5.0);
            vec3 envR = evalStudioEnv(R, uLightMode);
            lit += benchAlbedo * evalStudioEnv(N, uLightMode) * 0.55 * ao;
            lit += envR * fres * (1.0 - benchRough) * 0.55 * ao;

            // Soft CRT screen bounce glow cast onto the bench in front of the monitor
            if (emitScale > 0.01 && p.z > 0.25) {
                float openT = smoothstep(0.0, 1.0, clamp(uOpen, 0.0, 1.0));
                vec3 bounceTint = mix(vec3(0.36, 0.44, 0.48), vec3(0.08, 0.65, 0.48), openT);
                float dFront = length(vec2(p.x * 0.72, (p.z - 0.55) * 0.85));
                float spill = exp(-dFront * 1.35) * smoothstep(0.22, 0.62, p.z);
                lit += bounceTint * spill * 0.14 * emitScale * ao;
            }

            // Distance fade into studio cyclorama
            float distFade = smoothstep(5.5, 14.0, length(p.xz));
            col = mix(lit, evalStudioEnv(rd, uLightMode), distFade);
        } else if (matId > MAT_GLASS - 0.5 && matId < MAT_GLASS + 0.5) {
            // ================================================================
            // Thick Curved Leaded CRT Glass with Parallax Depth & Phosphors
            // ================================================================
            vec3 R = reflect(-V, N);
            float F0 = 0.048;
            float fresnel = F0 + (1.0 - F0) * pow(1.0 - NoV, 5.0);

            // Refract view ray through thick leaded glass (IOR = 1.54) to recessed phosphor plane
            vec3 Rglass = refract(rd, N, 1.0 / 1.54);
            float tGlass = GLASS_THICK / max(-Rglass.z, 0.28);
            vec3 pPhos = p + Rglass * tGlass;
            vec2 phosUV = (pPhos.xy - vec2(0.0, SCREEN_YC)) / vec2(SCREEN_HW - 0.045, SCREEN_HH - 0.045);

            // Beer-Lambert leaded-glass tint absorption
            vec3 glassTrans = exp(-vec3(0.28, 0.19, 0.23) * tGlass * 2.8);

            // Phosphor emission (muted cleanly in uLightMode == 1)
            vec3 phosCol = vec3(0.085);
            if (emitScale > 0.001) {
                phosCol = evalTrinitronPhosphor(phosUV) * emitScale;
            } else {
                // In neutral mode 1, show subtle curved-glass depth & neutral inner border
                vec2 ed = smoothstep(vec2(1.02), vec2(0.95), abs(phosUV));
                phosCol = mix(vec3(0.055), vec3(0.11), ed.x * ed.y);
            }

            // Front-surface studio softbox specular reflection + direct studio specular highlights
            vec3 envSpec = evalStudioEnv(R, uLightMode);
            vec3 directSpec = shadeDirectLight(p, N, V, keyDir, keyCol, vec3(0.02), 0.06, 0.0, shKey)
                            + shadeDirectLight(p, N, V, fillDir, fillCol, vec3(0.02), 0.08, 0.0, 1.0)
                            + shadeDirectLight(p, N, V, rimDir, rimCol, vec3(0.02), 0.08, 0.0, 1.0);

            // Subtle secondary internal glass reflection offset by parallax
            vec3 secSpec = evalStudioEnv(normalize(R + vec3(0.03, -0.02, 0.0)), uLightMode) * 0.12 * glassTrans;

            // Combine transmitted phosphor glow + crisp glass reflections
            col = phosCol * glassTrans * (1.0 - fresnel * 0.65)
                + (envSpec * 0.72 + directSpec * 0.85) * fresnel
                + directSpec * 0.22
                + secSpec * fresnel;

            // Subtle ambient studio floor lift so darkest glass edges stay > lum 10
            col += vec3(0.022) * ao;
        } else {
            // ================================================================
            // Opaque Bezel, Trim, Metal Hardware, Knobs, LED & Rubber Feet
            // ================================================================
            vec3 albedo = vec3(0.18);
            float rough = 0.42;
            float metal = 0.0;
            vec3 emissive = vec3(0.0);

            // Subtle molded polymer micro-stipple texture on bezel
            float stipple = fbm2(p.xy * 42.0 + p.z * 18.0) - 0.5;

            if (matId < MAT_BEZEL + 0.5) {
                // Vintage Sony PVM charcoal-slate matte studio bezel
                albedo = (uLightMode == 1)
                    ? vec3(0.22 + stipple * 0.015)
                    : vec3(0.155, 0.162, 0.172) + stipple * 0.012;
                rough = 0.44 + stipple * 0.04;
            } else if (matId < MAT_BEZEL_TRIM + 0.5) {
                // Darker satin inner trim ring & power button
                albedo = (uLightMode == 1)
                    ? vec3(0.14)
                    : vec3(0.085, 0.088, 0.095);
                rough = 0.34;
            } else if (matId < MAT_METAL + 0.5) {
                // Brushed anodized aluminum badge & hex bolts
                albedo = (uLightMode == 1)
                    ? vec3(0.55)
                    : vec3(0.56, 0.58, 0.61);
                rough = 0.26;
                metal = 0.85;
            } else if (matId < MAT_RUBBER + 0.5) {
                // Matte rubber feet
                albedo = vec3(0.075);
                rough = 0.78;
            } else if (matId < MAT_LED + 0.5) {
                // Power status LED jewel
                albedo = vec3(0.08, 0.14, 0.10);
                rough = 0.16;
                if (emitScale > 0.01) {
                    float openT = smoothstep(0.0, 1.0, clamp(uOpen, 0.0, 1.0));
                    emissive = mix(vec3(0.18, 1.15, 0.42), vec3(0.10, 1.10, 0.85), openT) * emitScale;
                }
            } else {
                // Knurled dark metallic rotary micro-knobs
                albedo = (uLightMode == 1)
                    ? vec3(0.26)
                    : vec3(0.20, 0.21, 0.23);
                rough = 0.30;
                metal = 0.65;
            }

            vec3 lit = shadeDirectLight(p, N, V, keyDir, keyCol, albedo, rough, metal, shKey);
            lit += shadeDirectLight(p, N, V, fillDir, fillCol, albedo, rough, metal, 0.90);
            lit += shadeDirectLight(p, N, V, rimDir, rimCol, albedo, rough, metal, 0.92);

            // Studio environment ambient & specular IBL
            vec3 R = reflect(-V, N);
            vec3 F0 = mix(vec3(0.04), albedo, metal);
            vec3 fres = F0 + (1.0 - F0) * pow(1.0 - NoV, 5.0);
            vec3 envDiff = evalStudioEnv(N, uLightMode);
            vec3 envSpec = evalStudioEnv(R, uLightMode);
            lit += albedo * (1.0 - metal) * envDiff * 0.62 * ao;
            lit += envSpec * fres * (1.0 - rough * 0.78) * 0.65 * ao;

            // Subtle recessed inner-bezel chamfer bounce ONLY on inward-facing lip (never on outer front bezel)
            if (emitScale > 0.01 && p.z > 0.33 && p.z < 0.445) {
                vec2 qScr = p.xy - vec2(0.0, SCREEN_YC);
                float dEdge = sdRoundRect2D(qScr, vec2(SCREEN_HW - 0.015, SCREEN_HH - 0.015), 0.060);
                float inward = smoothstep(0.45, 0.90, -dot(N.xy, normalize(qScr + 1e-4)));
                if (inward > 0.01 && dEdge < 0.030 && dEdge > -0.045) {
                    float openT = smoothstep(0.0, 1.0, clamp(uOpen, 0.0, 1.0));
                    vec3 softTint = mix(vec3(0.42, 0.46, 0.50), vec3(0.10, 0.72, 0.54), openT);
                    float proximity = exp(-max(dEdge, 0.0) * 40.0);
                    lit += softTint * albedo * inward * proximity * emitScale * 0.38;
                }
            }

            col = lit + emissive;
        }
    }

    // Exposure calibration + ACES filmic tone mapping (keeps hero lum cleanly in 10..246)
    col *= 1.05;
    col = acesToneMap(col);
    col = pow(max(col, 0.0), vec3(1.0 / 2.2));
    // Gentle studio floor guard against 0-crushed blacks
    col = clamp(col, vec3(0.035), vec3(0.965));

    fragColor = vec4(col, 1.0);
}

Mathematical models & shader specifications

Identifiercrt-phosphor-scanline-grille
CategorySci-Fi, Plasma & Noise Shaders · Sci-Fi & Micro-Optics
Upstream studyelectronics/lib/02_crt_aperture_grille_phosphor_stripes.glsl (commit 14ad58d)
Animation cycleContinuous procedural uTime
Declared uniformsuResolution, uTime, uOpen, uLightMode, uCamMode
Source size813 lines · 32.5 KB standalone GLSL ES 3.00
PNG baker outputs512px · 1024px · 2048px RGBA8 (4-tap seamless + alpha key)
Companion pixel VFXFree MIT ui sprite sheets
LicenseMIT License + CC0 / MIT Helpers

Physical & shading models implemented

GGX microfacet specular (Trowbridge–Reitz 1975; Walter et al. 2007); Schlick Fresnel reflectance (1994); Beer–Lambert volumetric absorption/attenuation

Adapted upstream mathematical helpers

David Hoskins, hash · Krzysztof Narkowicz, filmic curve · Inigo Quilez, distance formulation · Inigo Quilez, quadratic distance blend · Inigo Quilez, tetrahedral SDF normal

All sci-fi & noise shaders