A Tribute to Quake

The sinusoidal texture displacement underwater effect used on Quake

Created by marcogomez on Mon, 30 Aug 2021 17:07:28 GMT.


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

uniform sampler2D prgm3Texture;
uniform vec2 resolution;

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

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

uniform sampler2D noiseTexture;
uniform sampler2D eTexture1; // https://i.imgur.com/ShYcEmr.jpg
uniform sampler2D prgm3Texture;
uniform vec2 resolution;
uniform float time;

const float PI = acos(-1.0);
const float TAU = PI * 2.0;
const float bgWaveSpeed = 2.0;
const float xDistMag = 0.05;
const float yDistMag = 0.05;
const float xSineCycles = TAU;
const float ySineCycles = TAU;

float visibility(float roughness, float ndotv, float ndotl) {
  float m2 = roughness * roughness;
  float visV = ndotl * sqrt(ndotv * (ndotv - ndotv * m2) + m2);
  float visL = ndotv * sqrt(ndotl * (ndotl - ndotl * m2) + m2);
  return 0.5 / max(visV + visL, 0.00001);
}

float distribution(float roughness, float ndoth) {
  float m2 = roughness * roughness;
  float d = (ndoth * m2 - ndoth) * ndoth + 1.0;
  return m2 / (d * d * PI);
}

vec3 fresnel(vec3 specularColor, float vdoth) {
  vec3 fresnel = (
    clamp(50.0 * specularColor.y, 0.0, 1.0) *
    specularColor + (1.0 - specularColor) * pow((1.0 - vdoth), 5.0)
 );
  return fresnel;
}

vec3 lightSpecular(vec3 normal, vec3 viewDir, vec3 lightDir, vec3 lightColor, float roughness, vec3 specularColor) {
  vec3 halfVec = normalize(viewDir + lightDir);
  float vdoth = clamp(dot(viewDir, halfVec), 0.0, 1.0);
  float ndoth = clamp(dot(normal, halfVec), 0.0, 1.0);
  float ndotv = clamp(dot(normal, viewDir), 0.0, 1.0);
  float ndotl = clamp(dot(normal, lightDir), 0.0, 1.0);
  vec3 f = fresnel(specularColor, vdoth);
  float d = distribution(roughness, ndoth);
  float v = visibility(roughness, ndotv, ndotl);
  vec3 specular;
  specular = lightColor * f * (d * v * PI * ndotl);
  return specular;
}

float cylinder(vec3 p, float r, float height) {
  float d = length(p.xz) - r;
  d = max(d, abs(p.y) - height);
  return d;
}

float sphere(vec3 p, float s) {
  return length(p) - s;
}

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

float substract(float a, float b) {
  return max(+a, -b);
}

float unionMin(float a, float b) {
  return min(a, b);
}

float unionRound(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 triPrism(vec3 p, vec3 h) {
  vec3 q = abs(p);
  return max(q.y - h.y, max(q.z * 0.866025 + p.x * h.z, -p.x) - h.x * 0.5);
}

float scene(vec3 p, mat3 localToWorld) {
  p = p * localToWorld;
  float a = cylinder(p, 1.0, 0.1);
  float b = cylinder(p + vec3(0.12, 0.0, 0.0), 0.9, 0.2);
  float ring = substract(a, b);
  float c = box(p + vec3(-0.8, 0.0, 0.0), vec3(0.40, 0.1, 0.10));
  float d = box(p + vec3(-0.4, 0.0, 0.0), vec3(0.02, 0.1, 0.25));
  float e = triPrism(p + vec3(-1.275, 0.0, 0.0), vec3(0.15, 0.1, 0.16));
  float nail = unionMin(unionRound(c, d, 0.1), e);
  float ret = unionMin(ring, nail);
  return ret;
}

vec2 castRay(in vec3 ro, in vec3 rd, mat3 localToWorld) {
  const float maxd = 5.0;
  float h = 0.5;
  float t = 0.0;
  float steps = 0.0;
  for (int i = 0; i < 180; ++i) {
    if (h < 0.001 || t > maxd) { break; }
    h = scene(ro + rd * t, localToWorld) / 4.0;
    t += h;
    steps += 1.0;
  }
  if (t > maxd) { t = -1.0; }
  return vec2(t, steps);
}

vec3 sceneNormal(in vec3 pos, mat3 localToWorld) {
  vec3 eps = vec3(0.001, 0.0, 0.0);
  vec3 nor = vec3(
    scene(pos + eps.xyy, localToWorld) - scene(pos - eps.xyy, localToWorld),
    scene(pos + eps.yxy, localToWorld) - scene(pos - eps.yxy, localToWorld),
    scene(pos + eps.yyx, localToWorld) - scene(pos - eps.yyx, localToWorld)
  );
  return normalize(-nor);
}

void main(void) {
  vec2 q = gl_FragCoord.xy / resolution.xy;
  vec2 p = -1.0 + 2.0 * q;
  p.x *= resolution.x / resolution.y;
  float frameScale = 29.97;
  float frameTime = floor(time * frameScale) / frameScale;
  vec3 rayOrigin = vec3(0.0, -0.28, -3.5);
  vec3 rayDir = normalize(vec3(p.x, p.y, 2.0));
  float theta = -0.5 * PI;
  mat3 rotX = mat3(
    vec3(+cos(theta), +sin(theta), 0.0),
    vec3(-sin(theta), +cos(theta), 0.0),
    vec3(0.0, 0.0, 1.0)
  );
  float phi = 0.25 * PI + 2.0 * frameTime;
  mat3 rotY = mat3(
    vec3(1.0, 0.0, 0.0),
    vec3(0.0, +cos(phi), +sin(phi)),
    vec3(0.0, -sin(phi), +cos(phi))
  );
  mat3 localToWorld = rotX * rotY;

  float minRes = min (resolution.x, resolution.y);
  vec2 fc = gl_FragCoord.xy / minRes;
  float wt = frameTime * bgWaveSpeed;
  float xAngle = wt + fc.y * ySineCycles;
  float yAngle = wt + fc.x * xSineCycles;
  bool bxHalf, byHalf;
  vec2 distortOffset = vec2(sin(xAngle), sin(yAngle)) * vec2(xDistMag, yDistMag);
  vec3 color = vec3(texture2D(noiseTexture, (fc) + distortOffset).z) * 0.55;
  color = clamp(color * color * color * color * 5.0, 0.0, 1.0);
  vec2 t = castRay(rayOrigin, rayDir, localToWorld);
  if (t.x > 0.0) {
    vec3 pos = rayOrigin + t.x * rayDir;
    vec3 normal = sceneNormal(pos, localToWorld);
    vec3 lightDir = normalize(vec3(0.5, 0.3, 1.0));
    vec3 lightColor = vec3(1.6);
    vec3 posLS = pos * localToWorld;
    vec3 nrmLS = normal * localToWorld;
    vec2 uvX = posLS.yz;
    vec2 uvY = posLS.xz;
    vec2 uvZ = posLS.xy;
    vec3 textureX = texture2D(eTexture1, uvX).xyz;
    vec3 textureY = texture2D(eTexture1, uvY).xyz;
    vec3 textureZ = texture2D(eTexture1, uvZ).xyz;
    vec3 weights = max(abs(nrmLS), 0.00001);
    weights /= weights.x + weights.y + weights.z;
    vec3 texture = textureX * weights.x + textureY * weights.y + textureZ * weights.z;
    float rustMask = clamp(texture.x * 3.0 - 0.5, 0.0, 1.0);
    vec3 diffuseColor = mix(vec3(0.0), texture, rustMask);
    diffuseColor *= diffuseColor * vec3(0.94, 0.72, 0.47) * 1.5;
    vec3 specularColor = mix(texture, vec3(0.04), rustMask);
    float roughness = mix(0.2, 0.6, rustMask);
    vec3 diffuse = lightColor * clamp(dot(normal, lightDir), 0.0, 1.0);
    color = diffuseColor * (diffuse + 0.2);
    color += lightSpecular(normal, rayDir, lightDir, lightColor, roughness, specularColor);
  }
  if (sin(frameTime - 0.25) >= 0.92) {
    color += vec3(t.y / 250.0, sin(frameTime) * t.y / 400.0, 0.0) * (sin(frameTime - 0.25) - 0.92) * 30.0;
  }
  gl_FragColor = vec4(color, 1.0);
}

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

uniform sampler2D prgm1Texture;
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 int NUM_SAMPLES = 16;
const float Density = 0.25;
const float Weight = 0.5;
const float Exposure = 0.5;

float rand(vec2 co) {
  return fract(sin(dot(co.xy, vec2(12.9898, 78.233))) * 43758.5453);
}

void main(void) {
  vec2 uv = gl_FragCoord.xy / resolution.xy;
  vec2 suv = uv;
  float frameScale = 29.97;
  float frameTime = floor(time * frameScale) / frameScale;
  vec2 dist = (uv - 0.5);
  float Density = 0.75 + 0.2 * sin(2.0 * radians(360.0));
  dist *= 1.0 / float(NUM_SAMPLES) * Density;
  vec3 color = texture2D(prgm1Texture, uv).rgb;
  float illuminationDecay = 1.0;
  for (int i = 0; i < NUM_SAMPLES; i++)   {
    uv -= dist;
    vec3 sample_ = texture2D(prgm1Texture, uv + dist * rand(uv + time * 0.001)).rgb;
    sample_ *= illuminationDecay * Weight;
    color += sample_;
    float Decay = 0.6 + fft * 0.4;
    illuminationDecay *= Decay;
  }
  vec4 finalColor = texture2D(prgm1Texture, suv);
  float m = clamp(sin(frameTime - 0.25) * 0.75, 0.0, 1.0);
  finalColor = mix(finalColor, vec4(color, 1.0), m);
  gl_FragColor = finalColor;
}

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

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

const float PI = acos(-1.0);
const float TAU = PI * 2.0;
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(prgm2Texture, 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;
}

float rand(vec2 uv, float t) {
  float seed = dot(uv, vec2(12.9898, 78.233));
  return fract(sin(seed) * 43758.5453123 + t);
}

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 noise = rand(uv, t);
  noise = gaussian(noise, 0.0, 0.5);
  return vec3(noise);
}

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;
  vec3 g = gaussgrain(frameTime) * 0.07;
  vec2 pos = warp(uv, warpAmount);
  vec4 color = vec4(tri(pos, res) * mask(gl_FragCoord.xy), 1.0);
  color.xyz = toSRGB(color.xyz * 2.0) - g;
  color = mix(color, color * v, 0.5);
  drawVig(color.xyz, pos);
  gl_FragColor = color;
}