VHS Dreams

This piece is based on a Voronoi pattern with a mimicked underwater effect based on texture displacement and several other post-processing effects.

Created by marcogomez on Thu, 27 May 2021 21:27:53 GMT.


// ╔═════════════╦═════════════════╦════════════════╗
// ║ Marco Gomez ║ @TheCodeTherapy ║ https://mgz.me ║
// ╚═════════════╩═════════════════╩════════════════╝
precision highp float;

// prgm0 -> main out + VHS post-processing effect (in: prgm2, prgm3, prgm7)
// prgm1 -> liquid wave displacement A (in: prgm1, prgm2, prgm4)
// prgm2 -> liquid wave displacement B (in: prgm1, prgm2, prgm4)
// prgm3 -> caustics
// prgm4 -> voronoi pattern (in: prgm3)
// prgm5 -> Apply liquid wave displacement (in: prgm2, prgm4)
// prgm6 -> post-processing (vig, brightness, contrast, sat, filmicReinhard) (in: prgm5)
// prgm7 -> post-processing (CRT)

uniform sampler2D prgm2Texture;
uniform sampler2D prgm3Texture;
uniform sampler2D prgm7Texture;
uniform vec2 resolution;
uniform float time;
uniform float fft;

const float h = 0.002;
const float v = 0.002;
const float g = 0.12;

float stepm(float a, float b, float c) {
  return step(c, sin(time + a * cos(time * b)));
}

vec3 badVHS(vec2 uv, sampler2D tex) {
  float tmod = mod(time * 0.25, 3.0);
  float lookyMod = uv.y - tmod;
  float window = 1.0 / (1.0 + 20.0 * lookyMod * lookyMod);
  float lookyStep = stepm(4.0, 4.0, 0.3) * 0.5;
  uv.x = uv.x + sin(uv.y * 10.0 + time) / 100.0 * lookyStep * (1.0 + cos(time * 80.0)) * window * 0.25;
  float vShift = v * stepm(2.0, 3.0, 0.9) * (sin(time) * sin(time * 20.0) + (0.5 + 0.1 * sin(time * 200.0) * cos(time)));
  //uv.y = mod(uv.y + vShift, 5.0);
  vec3 desatColor;
  float _r, _g, _b;
  float x = sin(0.3 * time + uv.y * 21.0) * sin(0.7 * time + uv.y * 29.0) * sin(0.3 + 0.33 * time + uv.y * 31.0) * h;
  _r = texture2D(tex, vec2(x + uv.x + 0.001, uv.y + 0.001)).x + 0.007;
  _g = texture2D(tex, vec2(x + uv.x + 0.000, uv.y - 0.002)).y + 0.007;
  _b = texture2D(tex, vec2(x + uv.x - 0.002, uv.y + 0.000)).z + 0.007;
  _r += 0.08 * texture2D(tex, 0.75 * vec2(x +  0.012, -0.013) + vec2(uv.x + 0.001, uv.y + 0.001)).x;
  _g += 0.05 * texture2D(tex, 0.75 * vec2(x + -0.011, -0.010) + vec2(uv.x + 0.000, uv.y - 0.002)).y;
  _b += 0.08 * texture2D(tex, 0.75 * vec2(x + -0.010, -0.009) + vec2(uv.x - 0.002, uv.y + 0.000)).z;
  float _luma = 0.3 * _r + 0.6 * _g + 0.1 * _b;
  float _desat = 0.3;
  desatColor = vec3(
    _r + _desat * (_luma - _r),
    _g + _desat * (_luma - _g),
    _b + _desat * (_luma - _b)
  );
  desatColor = clamp(desatColor, 0.0, 1.0);
  return desatColor;
}

float oscillate(float s, float e, float t) {
  return (e - s) * 0.5 + s + sin(t) * (e - s) * 0.5;
}

float gaussian(float z, float u, float o) {
  return (
    (1.0 / (o * sqrt(2.0 * 3.14159265359))) *
    (exp(-(((z - u) * (z - u)) / (2.0 * (o * o)))))
  );
}

vec3 gaussgrain(float t) {
  vec2 ps = vec2(1.0) / resolution.xy;
  vec2 uv = gl_FragCoord.xy * ps;
  float seed = dot(uv, vec2(12.9898, 78.233));
  float noise = fract(sin(seed) * 43758.5453123 + t);
  noise = gaussian(noise, 0.0, 0.5);
  return vec3(noise);
}

vec2 curve(vec2 uv) {
  uv = (uv - 0.5) * 2.0;
  uv *= 1.1;
  uv.x *= 1.0 + pow((abs(uv.y) / 5.0), 2.0);
  uv.y *= 1.0 + pow((abs(uv.x) / 4.0), 2.0);
  uv = (uv / 2.0) + 0.5;
  uv = uv * 0.92 + 0.04;
  return uv;
}

void main(void) {
  vec2 uv = gl_FragCoord.xy / resolution.xy;
  vec4 prgm2 = texture2D(prgm2Texture, uv);
  vec4 prgm3 = texture2D(prgm3Texture, uv);
  vec4 prgm6 = texture2D(prgm7Texture, uv);
  uv = curve(uv);
  float frameScale = 29.97;
  float frameTime = floor(time * frameScale) / frameScale;
  vec3 grain = gaussgrain(frameTime * 2.0);
  vec3 grainB = gaussgrain(frameTime * 0.5);
  vec3 vhsCol = badVHS(uv, prgm7Texture) + (grain * grain) * g;
  float vig = (0.0 + 1.0 * 21.0 * uv.x * uv.y * (1.0 - uv.x) * (1.0 - uv.y));
  vhsCol *= vec3(pow(abs(vig), 1.4));
  float scans = clamp(0.35 + 0.35 * sin(3.5 * time + uv.y * resolution.y * 1.5), 0.0, 1.0);
  float s = pow(scans, 1.33);
  vhsCol = vhsCol * vec3(1.4 + 1.7 * s);
  if (uv.x < 0.0 || uv.x > 1.0) { vhsCol *= 0.0; }
  if (uv.y < 0.0 || uv.y > 1.0) { vhsCol *= 0.0; }
  vhsCol *= 1.0 - 0.37 * vec3(clamp((mod(gl_FragCoord.x, 2.0) - 1.0) * 2.0, 0.0, 1.0));
  vec4 tvhs = vec4(vhsCol * 1.05, 1.0);
  float osc = clamp(oscillate(-0.5, 0.5, time * 0.03), 0.0, 0.3);
  float mixAmount = 0.091 + clamp(prgm3.x * 2.1, 0.0, 0.409) + osc;
  vec4 finalColor = mix(prgm6, tvhs, clamp(mixAmount, 0.0, 1.0));
  finalColor = mix(finalColor, finalColor * finalColor, 0.7 - fft * 0.4) * 1.2;
  finalColor.rgb -= grainB * 0.03;
  finalColor.rgb = mix(finalColor.rgb, vhsCol, 0.05);
  finalColor = clamp(finalColor, 0.0, 1.0);
  finalColor.a = 1.0;
  gl_FragColor = finalColor;
}

// ╔═════════════╦═════════════════╦════════════════╗
// ║ Marco Gomez ║ @TheCodeTherapy ║ https://mgz.me ║
// ╚═════════════╩═════════════════╩════════════════╝
precision highp float;

uniform sampler2D prgm1Texture;
uniform sampler2D prgm2Texture;
uniform sampler2D prgm4Texture;
uniform vec2 resolution;
uniform vec2 mouse;
uniform float time;
uniform int frame;

void main(void) {
  vec2 uv = gl_FragCoord.xy / resolution.xy;
  vec2 mp = ((mouse + 1.0) * 0.5) * resolution;
  vec3 e = vec3(vec2(1.0) / resolution.xy, 0.0);
  vec2 q = uv;
  vec4 c = texture2D(prgm1Texture, q);
  float p11 = c.x;
  float p10 = texture2D(prgm2Texture, q - e.zy).x + texture2D(prgm4Texture, q - e.zy).x * 0.002;
  float p01 = texture2D(prgm2Texture, q - e.xz).x + texture2D(prgm4Texture, q - e.xz).x * 0.002;
  float p21 = texture2D(prgm2Texture, q + e.xz).x + texture2D(prgm4Texture, q + e.xz).x * 0.002;
  float p12 = texture2D(prgm2Texture, q + e.zy).x + texture2D(prgm4Texture, q + e.zy).x * 0.002;
  float d = 0.0;
  d = smoothstep(0.5, 96.0, length(mp.xy - gl_FragCoord.xy) * 1.5);
  d += -(p11 - 0.5) * 2.0 + (p10 + p01 + p21 + p12 - 2.0);
  d *= 0.995;
  d *= max(min(1.0, float(frame)), 0.0) * clamp(time - 1.0, 0.0, 1.0);
  d = d * 0.5 + 0.5;
  gl_FragColor = vec4(d, 0.0, 0.0, 1.0);
}

// ╔═════════════╦═════════════════╦════════════════╗
// ║ Marco Gomez ║ @TheCodeTherapy ║ https://mgz.me ║
// ╚═════════════╩═════════════════╩════════════════╝
precision highp float;

uniform sampler2D prgm1Texture;
uniform sampler2D prgm2Texture;
uniform sampler2D prgm4Texture;
uniform vec2 resolution;
uniform vec2 mouselerp;
uniform float time;
uniform int frame;

void main(void) {
  vec2 uv = gl_FragCoord.xy / resolution.xy;
  vec2 mp = ((mouselerp + 1.0) * 0.5) * resolution;
  vec3 e = vec3(vec2(1.0) / resolution.xy, 0.0);
  vec2 q = uv;
  vec4 c = texture2D(prgm2Texture, q);
  float p11 = c.x;
  float p10 = texture2D(prgm1Texture, q - e.zy).x + texture2D(prgm4Texture, q - e.zy).x * 0.004;
  float p01 = texture2D(prgm1Texture, q - e.xz).x + texture2D(prgm4Texture, q - e.xz).x * 0.004;
  float p21 = texture2D(prgm1Texture, q + e.xz).x + texture2D(prgm4Texture, q + e.xz).x * 0.004;
  float p12 = texture2D(prgm1Texture, q + e.zy).x + texture2D(prgm4Texture, q + e.zy).x * 0.004;
  float d = 0.0;
  d = smoothstep(0.0, 256.0, length(mp.xy - gl_FragCoord.xy) * resolution.x);
  d += -(p11 - 0.5) * 2.0 + (p10 + p01 + p21 + p12 - 2.0);
  d *= 0.997;
  d *= max(min(1.0, float(frame)), 0.0) * clamp(time - 1.0, 0.0, 1.0);
  d = d * 0.5 + 0.5;
  gl_FragColor = vec4(d, 0.0, 0.0, 1.0);
}

// ╔═════════════╦═════════════════╦════════════════╗
// ║ Marco Gomez ║ @TheCodeTherapy ║ https://mgz.me ║
// ╚═════════════╩═════════════════╩════════════════╝
precision highp float;

uniform vec2 resolution;
uniform float time;
uniform float fft;

const float PI = acos(-1.0);
const float TAU = 2.0 * PI;
const vec3 wc = vec3(0.1, 0.1, 0.4);
const int maxIter = 5;

void main(void) {
  vec2 uv = gl_FragCoord.xy / resolution.xy;
  float t1 = time * 0.5 + 23.0;
  vec2 p = mod(uv * TAU, TAU) - 250.0;
  p.x += sin(1.5 * p.y + time) * 0.42;
  p.y += cos(1.5 * p.x + time) * 0.42;
  vec2 i = vec2(p);
  float c = 1.0;
  float inten = 0.0042 + fft * 0.005;
  for (int n = 0; n < maxIter; n++) {
    float t = t1 * (1.0 - (3.5 / float(n + 1)));
    i = p + vec2(
      cos(t - i.x) + sin(t + i.y),
      sin(t - i.y) + cos(t + i.x)
    );
    c += 1.0 / length(
      vec2(
        p.x / (sin( i.x + t) / inten),
        p.y / (cos( i.y + t) / inten)
      )
    );
  }
  c /= float(maxIter);
  c = 1.17 - pow(abs(c), 1.4);
  float fadein = min(max(0.42 * time - 0.5, 0.0) * 0.42, 1.0);
  vec3 col = vec3(pow(abs(c), 8.0));
  col = clamp(col + wc, 0.0, 1.0);
  col *= clamp(fadein, 0.0, 1.0);
  col = clamp(col * col + wc * 0.3, 0.0, 1.0) * 0.33;
  gl_FragColor = vec4(col, 1.0);
}

// ╔═════════════╦═════════════════╦════════════════╗
// ║ Marco Gomez ║ @TheCodeTherapy ║ https://mgz.me ║
// ╚═════════════╩═════════════════╩════════════════╝
precision highp float;

uniform sampler2D prgm3Texture;
uniform vec2 resolution;
uniform int frame;
uniform vec2 mouselerp;
uniform float time;
uniform float fft;

const float PI = acos(-1.0); //         π or acos(-1.0) or 180°
const float TAU = PI * 2.0; //          τ = π * 2 or 360°
const float HALFPI = PI / 2.0; //       π / 2 [ rad(deg(π/2)) ] or 90°
const float ttp = 1.0 / 3.0;
const float ssp = 1.0 / 6.0;
const float r = 43758.5453123;

float osc(float s, float e, float t) {
  return (e - s) * 0.5 + s + sin(t) * (e - s) * 0.5;
}

vec2 rotate(vec2 uv, float a) {
  return vec2(uv.x * cos(a) - uv.y * sin(a), uv.x * sin(a) + uv.y * cos(a));
}

vec2 curve(vec2 uv) {
  return normalize(
    vec3(
      uv.xy,
      osc(-4.20, -5.20, time * 0.125) - fft * 0.6667
    )
  ).xy * (osc(2.1, 3.5, time * 0.1) - fft * 0.3334);
}

float r11(float n) {
  return fract(sin(n) * r);
}

vec2 r22(vec2 uv) {
  return fract(
    vec2(
      sin(uv.x * 591.32 + uv.y * 154.077),
      cos(uv.x * 391.32 + uv.y * 49.077)
    )
  );
}

float n11(float uv) {
  float fl = floor(uv);
  float fc = fract(uv);
  return mix(r11(fl), r11(fl + 1.000004), fc);
}

float vn(vec2 uv) {
  vec2 p = floor(uv);
  vec2 f = fract(uv);
  vec2 rs = vec2(8.0);
  for (int j = -1; j <= 1; j++) {
    for (int i = -1; i <= 1; i++) {
      vec2 b = vec2(i, j);
      vec2 r = vec2(b) - f + r22(p + b);
      float d = max(abs(r.x), abs(r.y));
      if (d < rs.x) {
        rs.y = rs.x;
        rs.x = d;
      } else if (d < rs.y) {
        rs.y = d;
      }
    }
  }
  return rs.y-rs.x;
}

vec3 ContrastSaturationBrightness(vec3 color, float brt, float sat, float con) {
  const float AvgLumR = 0.5;
  const float AvgLumG = 0.5;
  const float AvgLumB = 0.5;
  const vec3 LumCoeff = vec3(0.2125, 0.7154, 0.0721);
  vec3 AvgLumin = vec3(AvgLumR, AvgLumG, AvgLumB);
  vec3 brtColor = color * brt;
  vec3 intensity = vec3(dot(brtColor, LumCoeff));
  vec3 satColor = mix(intensity, brtColor, sat);
  vec3 conColor = mix(AvgLumin, satColor, con);
  return conColor;
}

void main(void) {
  float t1 = time * 0.17;
  float t2 = time * 0.1428571428571429;
  float t3 = time * 0.2;
  float t4 = time * ttp;
  vec2 uv = gl_FragCoord.xy / resolution.xy;
  vec4 prgm3 = texture2D(prgm3Texture, uv);
  uv = (gl_FragCoord.xy * 2.0 - resolution.xy) / min(resolution.x, resolution.y);
  uv *= 1.5;
  uv = curve(rotate(uv, -0.785398)) * -1.0;
  vec2 uv_b = uv;
  float v = 0.20 - length(uv) * 0.30 + fft * 0.09;
  uv -= vec2(t1, t1);
  uv -= rotate(mouselerp, -0.785398);
  // uv.x += mouselerp.x;
  // uv.y += mouselerp.y;
  float a = 0.7;
  float f = 1.0;
  vec2 imouse = rotate(vec2(-mouselerp.x, -mouselerp.y), -0.785398) * 0.125;
  vec2 ifft = vec2(fft, -fft) * 0.12;
  float o = -1.5;
  float ost = osc(0.021, 0.055, time * 0.33);
  for (int i = 0; i < 3; i++) {
    float v1 = vn(uv * f + 5.0);
    float v2 = 0.0;
    vec2 im = imouse * (float(i + 3) * 0.25);
    uv += im;
    if (i > 0) {
      if (i == 3) {
        im *= 0.0125;
        uv *= 0.0125;
        a *= sqrt(a / f);
        v1 = mix(v1 * 1.5, v1 * 1.5 * v2, 0.6);
      }
      float va = 0.0;
      float vb = 0.0;
      if (i == 1) {
        va = 1.0 - smoothstep(0.0, ost, v1);
        v2 = vn(uv * f * 0.1 + 21.0 + t2 + fft * 0.17);
        vb = 1.0 - smoothstep(0.0, 0.1, v2);
      } else {
        va = 1.0 - smoothstep(0.0, 0.1, v1);
        v2 = vn((uv + ifft) * f * 0.5 + (imouse * 0.5) + 50.0 + t3 * 2.1 - fft ) * 0.6667;
        if (i == 3) { vb = 1.0 - smoothstep(-0.5, 0.5, v2 * 2.0); }
        else { vb = 1.0 - smoothstep(-0.1, 0.1, v2); }
      }
      v += a * pow(abs(va) * (0.5 + vb), 2.0);
    }
    v1 = 1.0 - smoothstep(0.0, 0.2, v1);
    v2 = mix(0.12, a * (n11(v1 * 7.5 + 0.3)), o);
    if (i == 0) { v += v2 * 0.91; }
    f *= 3.0;
    a *= 0.7 + abs(sin(t4)) * 0.33334;
  }
  float e = exp(-length(uv_b * 0.45)) * 0.91;
  v *= mix(e, e * (1.0 - abs(o * 0.25)), 0.25);
  vec3 cxp = vec3(2.0, 1.8, 1.4) * 1.21;
  vec3 col = vec3(
    pow(abs(v * 1.02), cxp.x), pow(abs(v), cxp.y), pow(abs(v), cxp.z)
  ) * 12.0;
  col = ContrastSaturationBrightness(col, 1.0, 0.2, 0.9);
  vec3 tone = vec3(0.08, 0.05, -0.05);
  col += tone * 0.5;
  vec4 color = vec4(col, 1.0);
  color = mix(color, color * color + 0.15 , fft);
  prgm3 = prgm3 * vec4(0.75, 0.7, 1.2, 1.0) * 6.0;
  vec4 finalColor = mix(mix(color, 0.2 * prgm3 + prgm3 * color, 0.5), color * color, 0.125);
  finalColor = clamp(finalColor, 0.0, 1.0);
  finalColor.a = 1.0;
  gl_FragColor = finalColor;
}

// ╔═════════════╦═════════════════╦════════════════╗
// ║ Marco Gomez ║ @TheCodeTherapy ║ https://mgz.me ║
// ╚═════════════╩═════════════════╩════════════════╝
precision highp float;

uniform sampler2D prgm2Texture;
uniform sampler2D prgm4Texture;
uniform vec2 resolution;
uniform vec2 mouse;
uniform float time;

void main(void) {
  vec2 uv = gl_FragCoord.xy / resolution.xy;
  vec2 mp = (( mouse + 1.0) * 0.5) * resolution;
  vec3 e = vec3(vec2(1.0) / resolution.xy, 0.0) * 0.21;
  float p10 = texture2D(prgm2Texture, uv - e.zy).x;
  float p01 = texture2D(prgm2Texture, uv - e.xz).x;
  float p21 = texture2D(prgm2Texture, uv + e.xz).x;
  float p12 = texture2D(prgm2Texture, uv + e.zy).x;
  vec3 grad = normalize(vec3(p21 - p01, p12 - p10, 1.0));
  vec4 c = texture2D(prgm4Texture, uv + grad.xy * 0.35);
  vec3 light = normalize(vec3(0.2, -0.5, 0.7));
  float diffuse = dot(grad, light);
  float spec = pow(abs(max(0.0, -reflect(light, grad).z)), 32.0);
  vec4 col = mix(c, vec4(0.7, 0.8, 1.0, 1.0), 0.25) * max(diffuse, 0.0) + spec;
  col.rgb *= vec3(1.1, 1.0, 0.8);
  col = clamp(col, 0.0, 1.0);
  gl_FragColor = vec4(col.rgb, 1.0);
}

// ╔═════════════╦═════════════════╦════════════════╗
// ║ Marco Gomez ║ @TheCodeTherapy ║ https://mgz.me ║
// ╚═════════════╩═════════════════╩════════════════╝
precision highp float;

uniform sampler2D prgm5Texture;
uniform vec2 resolution;

const float reinhardAmount = 1.1;
const float brightness = 2.6;
const float saturation = 0.91;
const float contrast = 1.1;
const float vignetteRoundness = 0.21;
const float vignetteSmoothness = 0.42;
const float vignetteMix = 0.7;
const float amount = 0.8;
const vec2 vignetteSize = vec2(0.25, 0.45);
const float W = 1.2;
const float T = 7.5;

float filmicReinhardCurve(float x) {
  float q = (T * T + 1.0) * x * x;
  return q / (q + x + T * T);
}

vec3 filmicReinhard(vec3 c) {
  float w = filmicReinhardCurve(W);
  return vec3(
    filmicReinhardCurve(c.r),
    filmicReinhardCurve(c.g),
    filmicReinhardCurve(c.b)
  ) / w;
}

vec3 ContrastSaturationBrightness(vec3 color, float brt, float sat, float con) {
  const float AvgLumR = 0.5;
  const float AvgLumG = 0.5;
  const float AvgLumB = 0.5;
  const vec3 LumCoeff = vec3(0.2125, 0.7154, 0.0721);
  vec3 AvgLumin = vec3(AvgLumR, AvgLumG, AvgLumB);
  vec3 brtColor = color * brt;
  vec3 intensity = vec3(dot(brtColor, LumCoeff));
  vec3 satColor = mix(intensity, brtColor, sat);
  vec3 conColor = mix(AvgLumin, satColor, con);
  return conColor;
}

float sdSquare(vec2 point, float width) {
  vec2 d = abs(point) - width;
  return min(max(d.x, d.y), 0.0) + length(max(d, 0.0));
}

float vignette(vec2 uv, vec2 size, float roundness, float smoothness) {
  uv -= 0.5;
  float minWidth = min(size.x, size.y);
  uv.x = sign(uv.x) * clamp(abs(uv.x) - abs(minWidth - size.x), 0.0, 1.0);
  uv.y = sign(uv.y) * clamp(abs(uv.y) - abs(minWidth - size.y), 0.0, 1.0);
  float boxSize = minWidth * (1.0 - roundness);
  float dist = sdSquare(uv, boxSize) - (minWidth * roundness);
  return 1.0 - smoothstep(0.0, smoothness, dist);
}

void main(void) {
  vec2 uv = gl_FragCoord.xy / resolution.xy;
  vec4 prgm5 = texture2D(prgm5Texture, uv);
  vec3 reinhard = filmicReinhard(prgm5.rgb);
  vec3 color = prgm5.rgb;
  color = mix(prgm5.rgb, reinhard, reinhardAmount);
  color = ContrastSaturationBrightness(color, brightness, saturation, contrast);
  float v = vignette(uv, vignetteSize, vignetteRoundness, vignetteSmoothness);
  vec3 vig = color * v;
  color = mix(color, vig, vignetteMix);
  color = mix(prgm5.rgb, color, amount);
  color = clamp(color, 0.0, 1.0);
  gl_FragColor = vec4(color, 1.0);
}

// ╔═════════════╦═════════════════╦════════════════╗
// ║ Marco Gomez ║ @TheCodeTherapy ║ https://mgz.me ║
// ╚═════════════╩═════════════════╩════════════════╝
precision highp float;

uniform sampler2D prgm6Texture;
uniform vec2 resolution;

const float hardscan = -16.0; // -8.0 = soft | -16.0 = medium
const float hardPix = -4.0; // -2.0 = soft | -4.0 = hard
const float maskDark = 0.5;
const float maskLight = 2.5;

float toLinear(float c) {
  return (c <= 0.04045) ? c / 12.92 : pow(abs((c + 0.055) / 1.055), 2.4);
}

vec3 toLinear(vec3 c) {
  return vec3(toLinear(c.r), toLinear(c.g), toLinear(c.b));
}

float toSRGB(float c) {
  return(c < 0.0031308 ? c * 12.92 : 1.055 * pow(abs(c), 0.41666) - 0.055);
}

vec3 toSRGB(vec3 c) {
  return vec3(toSRGB(c.r), toSRGB(c.g), toSRGB(c.b));
}

vec3 fetch(vec2 uv, vec2 off, vec2 res) {
  uv = floor(uv * res + off) / res;
  if (max(abs(uv.x - 0.5), abs(uv.y - 0.5)) > 0.5) {
    return vec3(0.0);
  }
  return toLinear(texture2D(prgm6Texture, uv.xy, -16.0).xyz);
}

vec2 dist(vec2 uv, vec2 res) {
  uv = uv * res;
  return -((uv - floor(uv)) - vec2(0.5));
}

float gauss(float uv, float scale) {
  return exp2(scale * uv * uv);
}

vec3 horz3(vec2 uv, float off, vec2 res) {
  vec3 b = fetch(uv, vec2(-1.0, off), res);
  vec3 c = fetch(uv, vec2(+0.0, off), res);
  vec3 d = fetch(uv, vec2(+1.0, off), res);
  float dst = dist(uv, res).x;
  float scale = hardPix;
  float wb = gauss(dst - 1.0, scale);
  float wc = gauss(dst + 0.0, scale);
  float wd = gauss(dst + 1.0, scale);
  return (b * wb + c * wc + d * wd) / (wb + wc + wd);
}

vec3 horz5(vec2 uv, float off, vec2 res) {
  vec3 a = fetch(uv, vec2(-2.0, off), res);
  vec3 b = fetch(uv, vec2(-1.0, off), res);
  vec3 c = fetch(uv, vec2(+0.0, off), res);
  vec3 d = fetch(uv, vec2(+1.0, off), res);
  vec3 e = fetch(uv, vec2(+2.0, off), res);
  float dst = dist(uv, res).x;
  float scale = hardPix;
  float wa = gauss(dst - 2.0, scale);
  float wb = gauss(dst - 1.0, scale);
  float wc = gauss(dst + 0.0, scale);
  float wd = gauss(dst + 1.0, scale);
  float we = gauss(dst + 2.0, scale);
  return (a * wa + b * wb + c * wc + d * wd + e * we) / (wa + wb + wc + wd + we);
}

float scan(vec2 uv, float off, vec2 res) {
  float dst = dist(uv, res).y;
  return gauss(dst + off, hardscan);
}

vec3 tri(vec2 uv, vec2 res) {
  vec3 a = horz3(uv, -1.0, res);
  vec3 b = horz5(uv, +0.0, res);
  vec3 c = horz3(uv, +1.0, res);
  float wa = scan(uv, -1.0, res);
  float wb = scan(uv, +0.0, res);
  float wc = scan(uv, +1.0, res);
  return a * wa + b * wb + c * wc;
}

vec2 warp(vec2 uv, vec2 warpAmount) {
  uv = uv * 2.0 - 1.0;
  vec2 offset = abs(uv.yx) / vec2(warpAmount.x, warpAmount.y);
  uv = uv + uv * offset * offset;
  uv = uv * 0.5 + 0.5;
  return uv;
}

void drawVig(inout vec3 color, vec2 uv) {
  float vignette = uv.x * uv.y * (1.0 - uv.x) * (1.0 - uv.y);
  vignette = clamp(pow(abs(16.0 * vignette), 0.2), 0.0, 1.0);
  color *= vignette;
}

vec3 mask(vec2 uv) {
  uv.x += uv.y * 3.0;
  vec3 m = vec3(maskDark, maskDark, maskDark);
  uv.x = fract(uv.x / 6.0);
  if (uv.x < 0.333) {
    m.r = maskLight;
  } else if (uv.x < 0.666) {
    m.g = maskLight;
  } else {
    m.b = maskLight;
  }
  return m;
}

float bar(float uv, float bar) {
  uv -= bar;
  return uv * uv < 4.0 ? 0.0 : 1.0;
}

void main(void) {
  vec2 uv = gl_FragCoord.xy / resolution.xy;
  vec4 prgm6 = texture2D(prgm6Texture, uv);
  float crtResDivisor = 1.5;
  vec2 warpAmount = vec2(7.0, 5.0);
  vec2 res = vec2(resolution.x / (crtResDivisor + 1.0), resolution.y / crtResDivisor);
  uv = warp(uv, warpAmount);
  vec4 color = vec4(tri(uv, res) * mask(gl_FragCoord.xy), 1.0);
  color.xyz = toSRGB(color.xyz);
  if (uv.x < 0.0 || uv.x > 1.0 || uv.y < 0.0 || uv.y > 1.0) { color *= 0.0; }
  drawVig(color.xyz, uv);
  gl_FragColor = mix(prgm6, color, 0.42);
}