The Code Therapy

OutRun Forever

Just something so I can use as my dynamic wallpaper bringing back the nostalgic feeling of OutRun.

Created by marcogomez on Sun, 19 Sep 2021 19:40:04 GMT.


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

uniform sampler2D prgm5Texture;
uniform vec2 resolution;

void main(void) {
  vec2 uv = gl_FragCoord.xy / resolution.xy;
  vec4 prgm5 = texture2D(prgm5Texture, uv);
  gl_FragColor = prgm5;
}

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

uniform sampler2D noiseTexture;
uniform vec2 resolution;
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 mat2 m2 = mat2(0.80, -0.60, 0.60, 0.80);

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, vec2(12.9898, 78.233));
  float noise = fract(sin(seed) * 43758.5453 + t);
  noise = gaussian(noise, 0.0, 0.5);
  return vec3(noise);
}

float noise(vec2 x) {
  vec2 p = floor(x);
  vec2 f = fract(x);
  f = f * f * (3.0 - 2.0 * f);
  float a = texture2D(noiseTexture, (p + vec2(0.5, 0.5 + fft * 0.25)) / 256.0, 0.0).x;
  float b = texture2D(noiseTexture, (p + vec2(1.5, 0.5 + fft * 0.25)) / 256.0, 0.0).x;
  float c = texture2D(noiseTexture, (p + vec2(0.5, 1.5 + fft * 0.25)) / 256.0, 0.0).x;
  float d = texture2D(noiseTexture, (p + vec2(1.5, 1.5 + fft * 0.25)) / 256.0, 0.0).x;
  return mix(mix(a, b, f.x), mix(c, d, f.x), f.y);
}

float fbm (vec2 uv) {
  float f = 0.0;
  f += 0.5000 * noise(uv); uv = m2 * uv * 2.02;
  f += 0.2500 * noise(uv); uv = m2 * uv * 2.03;
  f += 0.1250 * noise(uv); uv = m2 * uv * 2.01;
  f += 0.0625 * noise(uv);
  return f / 0.9375;
}

vec3 clouds(vec2 uv) {
  vec3 color;
  float t = time * 0.75;
  vec2 q = vec2( 0.0 ), r = vec2( 0.0 );
  q.x = fbm(uv + t * 0.25);
  q.y = fbm(uv + vec2(1.0));
  r.x = fbm(uv + q + vec2(1.7, 9.2) + 0.31 * t * 0.25);
  r.y = fbm(uv + q + vec2(8.3, 2.8) + 0.21 * t * 0.25);
  float f = fbm(uv + r);
  color = mix(vec3(1.0, 1.0, 1.0), vec3(0.3, 0.6, 1.6), clamp((f * f) * 2.0, 0.0, 1.0) - fft * 0.5);
  color = mix(color, vec3(0.4, 0.5, 3.00), clamp(length(r.x) - fft * 0.33, 0.0, 1.0));
  return color * color * color;
}

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

void main(void) {
  vec2 uv = gl_FragCoord.xy / resolution.y;
  uv.y *= (resolution.x / resolution.y) * -.0625 - 1.0;
  vec2 p = -1.0 + 2.0 * uv;
  p.x -= time * 0.0125;
  vec3 g = gaussGrain(time) * 0.04;
  vec3 col = clouds(p * 3.0) - g;
  gl_FragColor = vec4(col, 1.0);
}

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

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

const float PI = acos(-1.0); // π or acos(-1.0) or 180°
const float TAU = PI * 2.0; //  τ = π * 2 or 360°

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, vec2(12.9898, 78.233));
  float noise = fract(sin(seed) * 43758.5453 + t);
  noise = gaussian(noise, 0.0, 0.5);
  return vec3(noise);
}

vec3 mapWorldToRoad(vec3 p) {
  float x = (p.x + 7.0 * cos(p.z * 0.01) * sin(p.z * 0.02));
  float y = (7.0 * cos(p.z * 0.011) * sin(p.z * 0.012));
  return vec3(x, y, p.z);
}

vec3 mapRoadNormal(vec3 p) {
  const float eps = 0.02;
  vec3 dirZ = mapWorldToRoad(vec3(p.xy, p.z - eps)) - mapWorldToRoad(vec3(p.xy, p.z + eps));
  vec3 dirX = mapWorldToRoad(vec3(p.x - eps, p.yz)) - mapWorldToRoad(vec3(p.x + eps, p.yz));
  vec3 normal = cross(dirZ, dirX);
  return normalize(normal);
}

vec3 mapRoadColor(vec3 p) {
  vec3 normal = mapRoadNormal(p);
  vec3 light = vec3(4.5, 1.0, 4.5);
  vec3 lightDir = (normalize(light));
  float lightMix = clamp(dot(normal, lightDir), 0.0, 1.0);
  vec3 lightColor = vec3(0.9, 0.75, 0.3);
  vec3 stripeColor = vec3(0.5);
  float stripeMix = step(4.0, mod(p.z, 8.0)) * 0.1;
  vec3 groundColor = (
    vec3(0.135, 0.2, 0.2) +
    stripeColor * stripeMix +
    lightColor * lightMix
  );
  float midLine = abs(mapWorldToRoad(p).x * 0.45);
  float roadMix = 1.0 - smoothstep(0.0, 0.1, pow(abs(midLine), 64.0));
  float om = (1.0 - smoothstep(0.0, 0.1, pow(abs(midLine), 128.0)) + stripeMix) * 3.0;
  vec3 roadColor = vec3(0.25) + -stripeMix * 0.25;
  roadColor = clamp(mix(roadColor, 3.0 * vec3(om) + stripeMix, 1.0 - roadMix), 0.0, 1.0);
  groundColor = mix(groundColor, roadColor, roadMix);
  float lineMix = (
    (1.0 - smoothstep(0.0, 0.0005, pow(abs(midLine), 2.5))) *
    smoothstep(0.0, 0.5, abs(mod(p.z, 1.5)) - 0.75)
  );
  vec3 lineColor = vec3(1.0);
  groundColor = mix(groundColor, lineColor, lineMix * 0.7);
  return groundColor;
}

vec3 skyColor(vec2 uv) {
  float u = (1.5 - uv.y);
  return clamp(vec3(0.3 * u, 0.7 * u, 0.9 * u - 0.5) * 2.1, 0.0, 1.0);
}

bool intersectRoad(vec3 originPos, vec3 dir, out vec3 intersectPos) {
  const float t_min = 0.001;
  const float t_max = 100.0;
  const float dt = 1.0;
  float roadY = 0.0;
  float rayY = 0.0;
  float t = t_min;
  for (int i = 0; i < 100; ++i) {
    if (t >= t_max) { break; }
    vec3 rayPosition = originPos + dir * t;
    vec3 roadPosition = mapWorldToRoad(rayPosition);
    if (rayPosition.y < roadPosition.y) {
      t = (t - dt) + dt * (roadY - rayY) / ((rayPosition.y - rayY) - (roadPosition.y - roadY));
      intersectPos = originPos + dir * t;
      return true;
    }
    roadY = roadPosition.y;
    rayY = rayPosition.y;
    t += dt;
  }
  return false;
}

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

void main(void) {
  const float fov = radians(45.0);
  const float tanHFOV = tan(fov * 0.5);
  const float near = 0.1;
  const float far = 1.0;
  float speed = 12.0;
  vec2 uv = gl_FragCoord.xy / resolution.xy;
  vec2 suv = uv;
  uv = (uv * 2.0 - 1.0 ) * vec2(resolution.x / resolution.y, 1.0);
  vec3 roadCamPos = mapWorldToRoad(vec3(0.0, 0.0, speed * time));
  float carLength = 1.25;
  float carHeight = 0.75;
  float nextX = -mapWorldToRoad(vec3(0.0, 0.0, speed * time + carLength * 4.0)).x;
  float offset = clamp(osc(-1.7, -0.7, time * 0.12), -1.5, -0.5);
  //offset = -1.5;
  vec3 camPos = vec3(
    offset - roadCamPos.x,
    roadCamPos.y + carHeight,
    roadCamPos.z - carLength
  );
  float swivel = (nextX - camPos.x) * 0.07;
  vec3 rayDir = normalize(
    vec3(
      tanHFOV * uv.x + swivel,
      tanHFOV * uv.y - 0.05,
      1.0
    ) * (far - near)
  );
  vec3 intersectPos;
  vec4 prgm1 = texture2D(prgm1Texture, suv);
  vec4 g = vec4(vec3(gaussGrain(time) * 0.03), 1.0);
  vec4 finalColor = vec4(0.0);
  if (intersectRoad(camPos, rayDir, intersectPos)) {
    finalColor += vec4(mapRoadColor(intersectPos), 1.0) - g;
  } else {
    vec3 sky = skyColor(suv);
    finalColor += (
      mix(prgm1, vec4(sky, 1.0), 0.3) +
      smoothstep(0.5, -0.5, uv.y)
    );
  }
  finalColor = floor(finalColor * 64.0) / 64.0;
  gl_FragColor = finalColor;
}

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

uniform sampler2D prgm2Texture;
uniform vec2 resolution;

#define FXAA_REDUCE_MIN (1.0 / 128.0)
#define FXAA_REDUCE_MUL (1.0 / 8.0)
#define FXAA_SPAN_MAX 8.0
void main() {
  vec2 res = vec2(1.0 / resolution.x, 1.0 / resolution.y);
  vec3 rgbNW = texture2D(prgm2Texture, (gl_FragCoord.xy + vec2(-1.0, -1.0)) * res).xyz;
  vec3 rgbNE = texture2D(prgm2Texture, (gl_FragCoord.xy + vec2(1.0, -1.0)) * res).xyz;
  vec3 rgbSW = texture2D(prgm2Texture, (gl_FragCoord.xy + vec2(-1.0, 1.0)) * res).xyz;
  vec3 rgbSE = texture2D(prgm2Texture, (gl_FragCoord.xy + vec2(1.0, 1.0)) * res).xyz;
  vec4 rgbaM  = texture2D(prgm2Texture,  gl_FragCoord.xy * res);
  vec3 rgbM  = rgbaM.xyz;
  float opacity  = rgbaM.w;
  vec3 luma = vec3(0.299, 0.587, 0.114);
  float lumaNW = dot(rgbNW, luma);
  float lumaNE = dot(rgbNE, luma);
  float lumaSW = dot(rgbSW, luma);
  float lumaSE = dot(rgbSE, luma);
  float lumaM  = dot(rgbM,  luma);
  float lumaMin = min(lumaM, min(min(lumaNW, lumaNE), min(lumaSW, lumaSE)));
  float lumaMax = max(lumaM, max(max(lumaNW, lumaNE), max(lumaSW, lumaSE)));
  vec2 dir;
  dir.x = -((lumaNW + lumaNE) - (lumaSW + lumaSE));
  dir.y =  ((lumaNW + lumaSW) - (lumaNE + lumaSE));
  float dirReduce = max((lumaNW + lumaNE + lumaSW + lumaSE) * (0.25 * FXAA_REDUCE_MUL), FXAA_REDUCE_MIN);
  float rcpDirMin = 1.0 / (min(abs(dir.x), abs(dir.y)) + dirReduce);
  dir = min(vec2(FXAA_SPAN_MAX, FXAA_SPAN_MAX), max(vec2(-FXAA_SPAN_MAX, -FXAA_SPAN_MAX), dir * rcpDirMin)) * res;
  vec3 rgbA = 0.5 * (
    texture2D(prgm2Texture, gl_FragCoord.xy * res + dir * (1.0 / 3.0 - 0.5)).xyz +
    texture2D(prgm2Texture, gl_FragCoord.xy * res + dir * (2.0 / 3.0 - 0.5)).xyz
  );
  vec3 rgbB = rgbA * 0.5 + 0.25 * (
    texture2D(prgm2Texture, gl_FragCoord.xy * res + dir * -0.5).xyz +
    texture2D(prgm2Texture, gl_FragCoord.xy * res + dir * 0.5).xyz
  );
  float lumaB = dot(rgbB, luma);
  if ((lumaB < lumaMin) || (lumaB > lumaMax)) {
    gl_FragColor = vec4(rgbA, opacity);
  } else {
    gl_FragColor = vec4(rgbB, opacity);
  }
}

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

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

const float reinhardAmount = 1.1;
const float contrast = 1.1;
const float brightness = 3.0;
const float amount = 0.7;
const float saturation = 1.2;
const vec2 vignetteSize = vec2(0.35, 0.35);
const float vignetteRoundness = 0.21;
const float vignetteMix = 0.5;
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 prgm3 = texture2D(prgm3Texture, uv);
  vec3 reinhard = filmicReinhard(prgm3.rgb);
  vec3 color = prgm3.rgb;
  color = mix(prgm3.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(prgm3.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 prgm4Texture;
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(prgm4Texture, 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.3), 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;
  float crtResDivisor = 1.5;
  vec2 warpAmount = vec2(7.0, 5.0);
  uv = warp(uv, warpAmount);
  vec2 res = vec2(resolution.x / 3.0, resolution.y / 3.0);
  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 = color;
}