The Code Therapy

Retrowave Sunset

In case you might need something to look at while listening to SynthWave songs.

Created by marcogomez on Tue, 12 Apr 2022 21:31:07 GMT.


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

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

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

void main(void) {
  vec2 uv = gl_FragCoord.xy / resolution.xy;
  vec4 texA = texture2D(prgm4Texture, uv);
  vec4 texB = texture2D(prgm2Texture, uv);
  float t = time * 0.25;
  float o = osc(t, t + 2.0, time * 0.35);
  vec3 color = mix(texA.xyz, texB.xyz, osc(-0.2, 0.2, o));
  gl_FragColor = vec4(color, 1.0);
}

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

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

const float PI = acos(-1.0);
const float TAU = PI * 2.0;
const vec2 hashv2 = vec2(12.9898, 78.233);
const float hashS = 43758.5453123;
const float starsSpeed = 1.2;
const int starsLayers = 12;

float rand(float n) {
  return fract(sin(n) * hashS);
}

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

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

float gaussian(float z, float u, float o) {
  return (
    (1.0 / (o * sqrt(TAU))) *
    (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 circle(vec2 uv, float r) {
  vec2 dist = uv - vec2(0.5);
  return 1.0 - smoothstep(r - (r * 0.03), r + (r * 0.03), dot(dist, dist) * 4.0);
}

float mapRange(float val, float min1, float max1, float min2, float max2) {
  return min2 + (val - min1) * (max2 - min2) / (max1 - min1);
}

float renderStars(vec2 uv) {
  float stars = 0.0;
  float fl, s;
  for (int layer = 0; layer < starsLayers; layer++) {
    fl = float(layer);
    s = (500.0 - fl * 30.0);
    stars += step(
      0.1,
      pow(
        abs(noise(mod(vec2(uv.x * s + time * starsSpeed - fl * 100.0, uv.y * s), resolution.x))),
        21.0
      )
    ) * (fl / float(starsLayers));
  }
  return stars;
}

void main(void) {
  const float skyLimit = 0.3;
  const float seaSpread = 0.1;
  const float sunLightSpread = 0.15;
  const float srDuration = 1.6;
  float audio = fft * 0.12;
  float stripsSize = 13.0;
  float seaRadius = 0.3 + audio;
  float sunRadius = 0.35 + audio;
  float sunLightRadius = sunRadius * 0.91;

  vec2 uv = gl_FragCoord.xy / resolution.xy;
  float rtime = clamp(log(time * 0.5) * 0.9, 0.0, srDuration);

  seaRadius += sin(rtime) * 0.1;
  sunRadius += sin(rtime) * 0.2;
  float sunStart = 0.9 - sin(rtime) * 0.9;

  vec3 bgColor = vec3(0.17, 0.12, 0.25);
  if (uv.y > skyLimit) {
    bgColor += mix(bgColor, (1.0 - uv.y) * vec3(0.7, 0.2, 0.5), 0.7);
  }
  vec3 sunLightColor = vec3(1.00, 0.14, 0.73);
  vec3 color = bgColor;

  float stars = renderStars(uv);
  float starsInSky = mix(0.0, stars, 1.0 - step(uv.y, skyLimit));
  color = mix(color, vec3(0.63, 0.60, 0.74), starsInSky * clamp(time * 0.5 - 0.5, 0.0, 1.0) * 0.6);

  float ar = resolution.x / resolution.y;
  vec2 cuv = vec2(
    uv.x * ar - ((resolution.x - resolution.y) / resolution.y * 0.5) / resolution.y,
    uv.y + sunStart
  );
  cuv -= vec2(0.12, 0.0);
  float sunLight = 1.0 - smoothstep(sunLightRadius - sunLightSpread, sunLightRadius + sunLightSpread, length(cuv - vec2(0.5, 0.55 - sunStart)));
  float sunLightMix = sunLight * 0.9 * (0.6 + noise(vec2(uv.x * 10.0 + time, uv.y * 60.0 + time)) * 0.4);
  color = mix(color, sunLightColor, sunLightMix);

  vec3 sunColor = mix(vec3(0.99, 0.98, 0.22), vec3(1.0, 0.0, 0.73), 1.0 - uv.y);
  float sunCut = step(1.0 - uv.y - sunStart, 0.75);
  float sun = circle(cuv, sunRadius) * sunCut;
  float sy = mapRange(pow(1.0 - uv.y + 0.0, 7.0), 0.0,  skyLimit, 0.0, 1.0);
  float sunStrips = 1.0 - step(fract(sy * stripsSize), 0.5);
  sunStrips = mix(sunStrips, 0.0, step(1.0 - uv.y , skyLimit));
  float sunStripsFade = 1.0 - smoothstep(0.32 - seaSpread * 2., 0.32 + seaSpread * 2., length(cuv - vec2(0.5, 0.45)));
  sunStrips *= sunStripsFade * (1.0 - (srDuration - rtime));
  sunColor = mix(sunColor, bgColor, sunStrips);
  color = mix(color, sunColor, sun);

  float y = mapRange(fract(uv.y), 0.0, skyLimit, 0.0, 1.0) - 0.04 + time * 0.05;
  float seaStrips = step(fract(y * stripsSize), 0.5);
  seaStrips = mix(seaStrips, 1.0, step(1.0 - uv.y, 1.0 - skyLimit));
  float seaGradient = mapRange(uv.y, 0.0, skyLimit, 0.0, 1.0);
  float seaFade = smoothstep(seaRadius - seaSpread, seaRadius + seaSpread, length(uv - vec2(0.5, 0.35)));
  vec3 seaColor = mix(vec3(0.212, 0.263, 0.467), vec3(0.208, 0.376, 0.761), seaGradient);
  seaColor = mix(seaColor, bgColor, seaFade);
  float seaStripsFade = 1.0 - smoothstep(0.3 - seaSpread * 2.0, 0.3 + seaSpread * 2.0, length(cuv - vec2(0.5, 0.45)));
  seaStrips *= seaStripsFade;
  vec3 seaReflectColor = mix(
    vec3(1.0, 0.58, 0.35),
    vec3(1.0, 0.10, 0.73),
    1.0 - mapRange(uv.y, skyLimit * 0.5, skyLimit, 0.0, 1.0)
  );

  seaColor = mix(seaColor, seaReflectColor, seaStrips);
  color = mix(color, seaColor, step(uv.y, skyLimit)) * clamp(time * 0.6667, 0.0, 1.0);

  color += gaussgrain(time * 0.35) * vec3(step(uv.y, skyLimit)) * 0.21;

  float frameScale = 2.00;
  float frameTime = floor(time * frameScale) / frameScale;
  float noise = rand(uv * 3.0);

  color = mix(color, vec3(noise), 0.06);
  color = mix(color, color * color, 0.5);
  color = color - gaussgrain(time) * 0.02;
  color *= clamp(time * 0.12, 0.0, 1.0);

  gl_FragColor = vec4(color, 1.0);
}

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

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

const float g = 0.12;

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

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

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

float texNoise(vec2 uv) {
  float s = texture2D(noiseTexture, vec2(1.0, 2.0 * cos(time)) * time * 8.0 + uv * 1.0).x;
  s *= s;
  return s;
}

float ramp(float y, float start, float end) {
  float ins = step(start, y) - step(end, y);
  float fact = (y - start) / (end - start) * ins;
  return (1.0 - fact) * ins;
}

float stripes(vec2 uv) {
  float n = texNoise(uv * vec2(0.5, 1.0) + vec2(1.0, 3.0));
  return ramp(mod(uv.y * 3.0 + time / 2.0 + sin(time * 0.5 + sin(time * 0.6)), 1.0), 0.5, 0.6) * n;
}

vec3 badVHS(vec2 uv, sampler2D tex) {
  float h = osc(0.0, 0.001, time * 0.2);
  float v = osc(0.0, 0.03, time * 0.3);
  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)
  );
  float s = stripes(uv) * osc(-0.3, 0.4, osc(time, time + 2.0, time * 0.35));
  float sb = stripes(uv - osc(0.001, 0.02, time * 0.1)) * osc(-0.3, 0.4, osc(time, time + 2.0, time * 0.45));
  desatColor += s * s + sb * sb;
  desatColor = clamp(desatColor, 0.0, 1.0);
  return desatColor;
}

void main(void) {
  vec2 uv = gl_FragCoord.xy / resolution.xy;
  vec4 prgm1 = texture2D(prgm1Texture, uv);
  float frameScale = 29.97;
  float frameTime = floor(time * frameScale) / frameScale;
  vec3 grain = gaussgrain(frameTime * 2.0);
  vec3 color = badVHS(uv, prgm1Texture) + grain * g;
  gl_FragColor = vec4(color, 1.0);
}

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

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

const float amount = 1.0;
const float reinhardAmount = 1.05;
const float contrast = 1.1;
const float brightness = 1.0;
const float saturation = 0.75;
const vec2 vignetteSize = vec2(0.3, 0.3);
const float vignetteRoundness = 0.9;
const float vignetteMix = 0.21;
const float vignetteSmoothness = 0.42;
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 prgm2 = texture2D(prgm2Texture, uv);
  vec3 reinhard = filmicReinhard(prgm2.rgb);
  vec3 color = prgm2.rgb;
  color = mix(prgm2.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(prgm2.xyz, color, amount);
  color = clamp(color, 0.0, 1.0);
  gl_FragColor = vec4(color, 1.0);
}

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

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

const float hardscan = -16.0;
const float hardPix = -4.0;
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 pos, vec2 off, vec2 res) {
  pos = floor(pos * res + off) / res;
  if (max(abs(pos.x - 0.5), abs(pos.y - 0.5)) > 0.5) {
    return vec3(0.0);
  }
  return toLinear(texture2D(prgm3Texture, pos.xy, -16.0).xyz);
}

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

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

vec3 horz3(vec2 pos, float off, vec2 res) {
  vec3 b = fetch(pos, vec2(-1.0, off), res);
  vec3 c = fetch(pos, vec2(+0.0, off), res);
  vec3 d = fetch(pos, vec2(+1.0, off), res);
  float dst = dist(pos, 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 pos, float off, vec2 res) {
  vec3 a = fetch(pos, vec2(-2.0, off), res);
  vec3 b = fetch(pos, vec2(-1.0, off), res);
  vec3 c = fetch(pos, vec2(+0.0, off), res);
  vec3 d = fetch(pos, vec2(+1.0, off), res);
  vec3 e = fetch(pos, vec2(+2.0, off), res);
  float dst = dist(pos, 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 pos, float off, vec2 res) {
  float dst = dist(pos, res).y;
  return gauss(dst + off, hardscan);
}

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

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

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

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.1), 0.0, 1.0);
  color *= vignette;
}

void main(void) {
  vec2 warpAmount = vec2(7.0, 5.0);
  vec2 res = vec2(640.0, 320.0);
  vec2 uv = gl_FragCoord.xy / resolution.xy;
  float vig = (0.0 + 1.0 * 21.0 * uv.x * uv.y * (1.0 - uv.x) * (1.0 - uv.y));
  float v = exp(-0.01 * length(uv)) * vig;
  float frameScale = 29.97;
  float frameTime = floor(time * frameScale) / frameScale;
  vec2 pos = warp(uv, warpAmount);
  vec4 color = vec4(tri(pos, res) * mask(gl_FragCoord.xy), 1.0);
  color.xyz = toSRGB(color.xyz * 2.0);
  color = mix(color, color * v, 0.5);
  drawVig(color.xyz, pos);
  gl_FragColor = color;
}