The Code Therapy

My 90s Room ( LightRays )

Another tribute to those that inspired me to explore my love for visual experiences since my childhood to these days.

Created by marcogomez on Fri, 11 Jun 2021 01:04:09 GMT.


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

uniform sampler2D prgm4Texture;
uniform vec2 resolution;

void main(void) {
  vec2 uv = gl_FragCoord.xy / resolution.xy;
  vec4 prgm4 = texture2D(prgm4Texture, uv);
  gl_FragColor = vec4(prgm4.xyz, 1.0);
}

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

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

const float contrast = 1.0;
const float brightness = 1.0;
const float saturation = 0.7;
const float tr = 1.0 / 1.5;
const int starLayers = 9;
const float starSpeed = 42.0;
const float barSize = 0.12;
const float barsAng = 1.4;

vec2 barPos;
vec3 barCol;
vec3 cp;

float hash (vec2 uv) {
  return fract(sin(dot(uv, vec2(12.4124, 48.4124))) * 48512.41241);
}

float starsNoise (vec2 uv) {
  vec2 b = floor(uv);
  return mix(
    mix(hash(b), hash(b + vec2(1.0, 0.0)), 0.5),
    mix(hash(b + vec2(0.0, 1.0)), hash(b + vec2(1.0, 1.0)), 0.5),
    0.5
  );
}

vec3 mixCol(float val, float r, float g, float b) {
  return vec3(val * r, val * g, val * b);
}

void bar(float pos, float r, float g, float b) {
  if (
    (barPos.y <= pos + barSize) &&
    (barPos.y >= pos - barSize)
  ) {
    barCol = mixCol(1.0 - abs(pos - barPos.y) / barSize, r, g, b);
  }
}

vec3 twist(vec3 p, float t) {
  float f = sin(t / 3.0) * 1.45;
  float c = cos(f * p.y);
  float s = sin(f / 2.0 * p.y);
  mat2 m = mat2(c, -s, s, c);
  return vec3(m * p.xz, p.y);
}

float cube(vec3 p, float t) {
  p = twist(p, t);
  cp.x = sin(-t);
  cp.y = cos(-t);
  mat2 m = mat2(cp.y, -cp.x, cp.x, cp.y);
  p.xy *= m; p.xy *= m; p.yz *= m;
  p.zx *= m; p.zx *= m; p.zx *= m;
  cp = p;
  return length(max(abs(p) - vec3(0.4), 0.0)) - 0.08;
}

float face(vec2 uv) {
  uv.y = mod(uv.y, 1.0);
  return (uv.y < uv.x != 1.0 - uv.y < uv.x) ? 1.0 : 0.0;
}

vec3 getNormal(vec3 p, float t) {
  vec2 e = vec2(0.005, -0.005);
  return normalize(
    e.xyy * cube(p + e.xyy, t) +
    e.yyx * cube(p + e.yyx, t) +
    e.yxy * cube(p + e.yxy, t) +
    e.xxx * cube(p + e.xxx, t)
  );
}

vec3 plasmaColor(float val) {
  float slice = 1.0;
  float r = (sin(val + slice * 1.0) + 1.0) * 0.5;
  float g = (sin(val + slice * 2.0) + 1.0) * 0.5;
  float b = (sin(val + slice * 4.0) + 1.0) * 0.5;
  return vec3(r, g, b);
}

vec4 renderAmiga(vec2 uv, float t) {
  vec2 suv = uv;
  uv = uv * 2.0 - 1.0;
  float ar = resolution.x / resolution.y;
  uv.x *= ar;
  uv *= 1.2;
  float st = t * 0.7;
  float x = gl_FragCoord.x;
  float stp = 1.0;
  float dist = 0.0;
  float near = -1.0;
  float far = -1.0;
  vec3 lightPos = vec3(1.5, 0.5, 1.0);
  float hitdist = -1.0;
  float ay = max(0.1, 0.5 - t / 6.0);
  vec3 ro = vec3(0.0, 0.0, 2.1);
  vec3 rd = normalize(vec3(uv, -2.0));
  for (int i = 0; i < 128; i++) {
    stp = cube(ro + rd * dist, st);
    dist += stp * 0.5;
    if (dist > 3.0) { break; }
    if (stp < 0.01) {
      far = (
        face(+cp.yx) +
        face(-cp.yx) +
        face(+cp.xz) +
        face(-cp.xz) +
        face(+cp.zy) +
        face(-cp.zy)
      );
      if (hitdist < 0.0) { hitdist = dist; }
      if (near < 0.0) { near = far; }
      dist += 0.05;
    }
  }
  float stars = 0.0;
  float fl, s;
  for (int layer = 0; layer < starLayers; layer++) {
    fl = float(layer);
    s = (500.0 - fl * 30.0);
    stars += step(
      0.1,
      pow(
        abs(starsNoise(mod(vec2(suv.x * s + t * starSpeed - fl * 100.0, suv.y * s), resolution.x))),
        21.0
      )
    ) * (fl / float(starLayers));
  }
  barPos = uv;
  barCol = vec3(stars);
  bar(sin(t                      ), 1.0, 0.0, 0.0);
  bar(sin(t + barsAng / 6.0      ), 1.0, 1.0, 0.0);
  bar(sin(t + barsAng / 6.0 * 2.0), 0.0, 1.0, 0.0);
  bar(sin(t + barsAng / 6.0 * 3.0), 0.0, 1.0, 1.0);
  bar(sin(t + barsAng / 6.0 * 4.0), 0.5, 0.0, 1.0);
  bar(sin(t + barsAng / 6.0 * 5.0), 1.0, 0.0, 1.0);
  vec3 col = barCol;
  if (near > 0.0) {
    vec3 sp = ro + rd * hitdist;
    vec3 ld = lightPos - sp;
    float lDist = max(length(ld), 0.001);
    ld /= lDist;
    float atten = 1.0 / (1.0 + lDist * 0.2 + lDist * 0.1);
    float ambience = 0.5;
    vec3 sn = getNormal(sp, st);
    float diff = min(0.3, max(dot(sn, ld), 0.0));
    float spec = pow(max(dot(reflect(-ld, sn), -rd), 0.0), 32.0);
    col = mix(
      vec3(0.12, 0.0, 0.3),
      vec3(0.5),
      vec3((near * 0.45 + far * far * 0.04))
    );
    col = col * (diff + ambience) + vec3(0.78, 0.5, 1.0) * spec / 1.5;
  }
  vec4 color = vec4(col, 1.0);
  return color;
}

vec4 renderPlasma(vec2 uv, float t) {
  float rt = t * 0.25;
  uv *= -mat2(cos(rt), -sin(rt), sin(rt), cos(rt));
  float mt = mod(t, 360.0) / 1.0 * 5.0;
  //uv += vec2(mt * 0.01, mt * 0.03);
  float x = uv.x;
  float y = uv.y;
  float zoom = 60.0 + sin(t * 0.1) * 21.0;
  float x2 = x / zoom;
  float y2 = y / zoom;
  float val = (
    128.0 + (32.0 * sin((x / 4.0 * zoom + 10.0 * sin(mt / 128.0) * 8.0) / 8.0)) +
    128.0 + (32.0 * cos((y / 5.0 * zoom + 10.0 * cos(mt / 142.0) * 8.0) / 8.0)) +
    (
      128.0 + (128.0 * sin(mt / 40.0 - sqrt(x * x + y * y) * sin(mt / 64.0) / 8.0)) / 3.0 +
      128.0 + (128.0 * sin(mt / 80.0 + sqrt(2.0 * x * x + y * y) * sin(mt / 256.0) / 8.0)) / 3.0
    )
  ) / 4.0;
  vec3 col = plasmaColor(val + mt);
  vec4 color = vec4(clamp(col * col, 0.0, 1.0), 1.0);
  return color;
}

vec4 renderNightGrid(vec2 uv, float t) {
  vec2 suv = uv;
  uv = uv - vec2(0.5);
  float horizon = 0.0;
  float fov = 0.3;
  float scaling = 0.1;
  vec3 p = vec3(uv.x, fov, uv.y - horizon);
  vec2 sc = vec2(p.x / p.z, p.y / p.z) * scaling;
  sc.y -= time * 0.2;
  if (sc.y > 0.0) { sc.y *= 0.0; }
  float checkerboard = sign((mod(sc.x, 0.1) - 0.05) * (mod(sc.y, 0.1) - 0.05));
  checkerboard *= p.z * p.z * 10.0;
  float stars = 0.0;
  float fl, s;
  for (int layer = 0; layer < starLayers; layer++) {
    fl = float(layer);
    s = (500.0 - fl * 30.0);
    stars += step(
      0.1,
      pow(
        abs(starsNoise(mod(vec2(suv.x * s + time * starSpeed - fl * 100.0, suv.y * s), resolution.x))),
        21.0
      )
    ) * (fl / float(starLayers));
  }
  vec4 color = (uv.y <= 0.0)
    ? vec4(vec3(checkerboard * 0.1, 0.0, checkerboard), 1.0)
    : vec4(vec3(sc.y, 0.0, uv.y - 0.1) + stars, 1.0);
  color = clamp(color * 3.0, 0.0, 1.0);
  return color;
}

vec4 ContrastSaturationBrightness(vec4 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.xyz * brt;
  vec3 intensity = vec3(dot(brtColor, LumCoeff));
  vec3 satColor = mix(intensity, brtColor, sat);
  vec3 conColor = mix(AvgLumin, satColor, con);
  return vec4(conColor, 1.0);
}

vec4 getScreenContent(int fx, vec2 uv) {
  float effects = 7.0;
  int idx = int(effects) - 1;
  fx = int(fract(float(fx) * 1.61456) * effects);
  int temp = int(float(fx) / float(idx));
  fx -= temp * idx;
  vec4 value = vec4(0.0);
  if (fx == 0) {
    value = renderPlasma(uv, time);
  } else if (fx == 1) {
    value = renderNightGrid(uv, time);
  } else if (fx == 2) {
    value = renderAmiga(uv, time);
  } else if (fx == 3) {
    value = mix(renderAmiga(uv, time), renderPlasma(uv, time), 0.4);
  } else if (fx == 4) {
    value = mix(renderAmiga(uv, time), renderNightGrid(uv, time), 0.4);
  } else {
    value = mix(renderNightGrid(uv, time), renderPlasma(uv, time), 0.2);
  }
  return value;
}

vec4 transition(vec2 uv, float effectLength) {
  int fx = int(time / effectLength);
  float frac = mod(time, effectLength) / effectLength;
  vec4 valueA = getScreenContent(fx, uv);
  vec4 valueB = getScreenContent(fx - 1, uv);
  return mix(valueB, valueA, smoothstep(tr, 1.0, frac));
}

void main(void) {
  vec2 uv = gl_FragCoord.xy / resolution.xy;
  vec4 color = transition(uv, 12.0);
  color = ContrastSaturationBrightness(color, brightness, saturation, contrast);
  gl_FragColor = clamp(mix(color, color * color, 0.3), 0.005, 1.0);
}

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

uniform sampler2D noiseTexture;
uniform sampler2D prgm1Texture;
uniform vec2 resolution;
uniform float time;

vec4 rgbShift(vec2 p , vec4 shift) {
  shift *= 2.0 * shift.w - 1.0;
  vec2 rs = vec2(shift.x, -shift.y);
  vec2 gs = vec2(shift.y, -shift.z);
  vec2 bs = vec2(shift.z, -shift.x);
  float r = texture2D(prgm1Texture, p + rs, 0.0).x;
  float g = texture2D(prgm1Texture, p + gs, 0.0).y;
  float b = texture2D(prgm1Texture, p + bs, 0.0).z;
  return vec4(r,g,b,1.0);
}

vec4 noise(vec2 uv) {
  return texture2D(noiseTexture, uv, 0.0);
}

vec4 vec4pow(vec4 v, float p) {
  return vec4(
    pow(v.x, p),
    pow(v.y, p),
    pow(v.z, p),
    v.w
  );
}

void main(void) {
  vec2 uv = gl_FragCoord.xy / resolution.xy;
  vec2 mo = uv * 2.0 - 1.0;
  mo *= 0.01;
  vec3 chromaticAberration;
  chromaticAberration.r = texture2D(prgm1Texture, uv - mo * 0.05, 0.0).r;
  chromaticAberration.g = texture2D(prgm1Texture, uv - mo * 0.15, 0.0).g;
  chromaticAberration.b = texture2D(prgm1Texture, uv - mo * 0.25, 0.0).b;
  vec4 color = vec4(vec3(0.0), 1.0);
  color.xyz = mix(color.xyz, chromaticAberration, 0.3);
  const float speed = 0.01;
  const float amplitude = 0.01;
  vec4 shift = vec4pow(
    noise(
      vec2(speed * time, speed * time / 25.0 )
    ), 8.0
  ) * vec4(vec3(amplitude), 1.0);
  color += rgbShift(uv, shift);
  gl_FragColor = color;
}

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

uniform sampler2D prgm2Texture;
uniform vec2 resolution;
uniform vec2 mouselerp;
uniform float time;

#define ss smoothstep
const float PI = acos(-1.0);
const float TAU = PI * 2.0;
const float SQRTAU = sqrt(TAU);
const vec2 hashv2 = vec2(12.9898, 78.233);
const float hashS = 43758.5453123;
const int blurIter = 8;
const float blurSize = 0.07;
const float width = 0.49;
const float height = 0.3;

float gaussian(float z, float u, float o) {
  return (
    (1.0 / (o * SQRTAU)) *
    (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, hashv2);
  float noise = fract(sin(seed) * hashS + t);
  noise = gaussian(noise, 0.0, 0.5);
  return vec3(noise);
}

float rand(vec2 co) {
  return fract(sin(dot(co.xy, hashv2)) * hashS);
}

vec2 CurvedSurface(vec2 uv, float r) {
  return r * uv / sqrt(r * r - dot(uv, uv));
}

vec2 crtCurve(vec2 uv, float r, bool content) {
  uv = CurvedSurface(uv, 1.5);
  if (content) { uv *= 0.5 / vec2(width, height); }
  uv.x -= mouselerp.x * 0.5;
  return uv;
}

float roundSquare(vec2 p, vec2 b, float r) {
  return length(max(abs(p) - b, 0.0)) - r;
}

float rs(vec2 uv, float r) {
  return roundSquare(uv, vec2(width, height) + r, 0.05);
}

vec2 borderReflect(vec2 p, float r) {
  float eps = 0.0001;
  vec2 epsx = vec2(eps, 0.0);
  vec2 epsy = vec2(0.0, eps);
  vec2 b = (1.0 + vec2(r, r)) * 0.5;
  r /= 3.0;
  p -= 0.5;
  vec2 normal = vec2(
    roundSquare(p - epsx, b, r) - roundSquare(p + epsx, b, r),
    roundSquare(p - epsy, b, r) - roundSquare(p + epsy, b, r)
  ) / eps;
  float d = roundSquare(p, b, r);
  p += 0.5;
  return p + d * normal;
}

vec2 normalizeAndFix() {
  vec2 uv = (gl_FragCoord.xy / resolution.xy) * 2.0 - 1.0;
  float targetAR = 16.0 / 9.0;
  float ar = resolution.x / resolution.y;
  uv.x *= ar;
  uv *= (
    ar < targetAR &&
    resolution.x < 800.0 &&
    resolution.x < resolution.y
  ) ? 1.1 : 0.55;
  return uv;
}

void main(void) {
  float s = 0.0021;
  vec4 color = vec4(0.02, 0.02, 0.03, 0.0);
  const vec4 multColor = vec4(0.21);
  const float ambient = 0.12;
  vec4 bezel = vec4(0.5);
  vec2 uv = normalizeAndFix();
  vec2 suv = gl_FragCoord.xy / resolution.xy;
  float gs = 2.0;
  float grid = (
    (mod(floor((suv.x) * resolution.x / gs), 2.0) == 0.0 ? 1.0 : 0.0) *
    (mod(floor((suv.y) * resolution.y / gs), 2.0) == 0.0 ? 1.0 : 0.0)
  );
  vec2 uvC = crtCurve(uv, 1., true) + 0.5;
  vec2 uvS = crtCurve(uv, 1., false);
  vec2 uvE = crtCurve(uv, 1.25, false);
  color += (max(0.0, ambient - 0.25 * distance(uvS, vec2(0.0))) * ss(s, -s, rs(uvS, 0.0)));
  color += (bezel * ambient * 0.7 * ss(-s, s, rs(uvS, 0.0)) * ss(s, -s, rs(uvE, 0.05)));
  color -= (bezel * ss(-s * 2.0, s * 10.0, rs(uvE, 0.05)) * ss(s * 2.0, -s * 2.0, rs(uvE, 0.05)));
  color += (bezel * ambient * ss(-s, s, rs(uvE, 0.05)) * ss(s, -s, rs(uvE, 0.15)));
  for (int i = 0; i < blurIter; i++) {
    vec2 uvR = borderReflect(uvC + (vec2(rand(uvC + float(i)), rand(uvC + float(i) + 0.1)) - 0.5) * blurSize, 0.05);
    color += (
      (multColor - bezel * ambient) *
      texture2D(prgm2Texture, uvR) / float(blurIter) *
      ss(-s, s, rs(uvS, 0.0)) *
      ss(s, -s, rs(uvE, 0.05))
    );
  }
  vec4 prgmMipMaps = texture2D(prgm2Texture, uvC, 3.0) + texture2D(prgm2Texture, uvC, 4.0) + texture2D(prgm2Texture, uvC, 5.0);
  color += (prgmMipMaps * ss(0.0, -s * 20.0, rs(uvS, -0.05)) * 0.5);
  color += (
    max(0.0, (1.0 - 2.0 * gl_FragCoord.y / resolution.y)) * vec4(0.1, 0.1, 0.1, 0.0) *
    ss(-0.3, 0.3, roundSquare(uvC - vec2(0.5, 0.0), vec2(width + 0.2, height), 0.1)) *
    ss(-s * 2.0, s * 2.0, roundSquare(uvE, vec2(width, height) + 0.15, 0.05))
  ) * 1.5;
  if (
    uvC.x > 0.0 &&
    uvC.x < 1.0 &&
    uvC.y > 0.0 &&
    uvC.y < 1.0
  ) {
    color += texture2D(prgm2Texture, uvC);
    color = mix(color, color * grid, 0.6);
  }
  vec3 g = gaussgrain(time) * 0.015;
  color.xyz += g;
  gl_FragColor = color;
}

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

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

const float PI = acos(-1.0);
const float TAU = PI * 2.0;
const float SQRTAU = sqrt(TAU);

const int samples = 8;
const float density = 0.5;
const float weight = 0.7;
const float Exposure = 0.55;

float rand(vec2 uv) {
    uv = fract(uv * vec2(5.3987, 5.4421));
    uv += dot(uv.yx, uv.xy + vec2(21.5351, 14.3137));
    float xy = uv.x * uv.y;
    return fract(xy * 95.4307) + fract(xy * 75.04961) - 1.0;
}

float gaussian(float z, float u, float o) {
  return (
    (1.0 / (o * SQRTAU)) *
    (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);
}

void main(void) {
  vec2 uv = gl_FragCoord.xy / resolution.xy;
  vec2 dist = (vec2(uv.x, uv.y) - (-vec2(mouselerp.x * 0.5, -0.05) + 0.5));
  float density = 0.75 + 0.2 * sin(2.0 * radians(360.0));
  dist *= 1.0 / float(samples) * density;
  vec3 g = gaussgrain(time) * 0.021;
  vec3 color = texture2D(prgm3Texture, uv).rgb;
  vec3 prgm5 = color;
  float illuminationDecay = 1.0;
  for (int i = 0; i < samples; i++)   {
    uv -= dist;
    vec3 sample_ = texture2D(prgm3Texture, uv + dist * rand(uv)).rgb + g;
    sample_ *= illuminationDecay * weight;
    color += sample_ * 0.7;
    float Decay = 0.65 + fft * 0.3;
    illuminationDecay *= Decay;
  }
  gl_FragColor = vec4(mix(prgm5, color * Exposure, 0.21), 1.0);
}