The Code Therapy

The Rhombic Triacontahedron

This is a piece made to experiment with my first implementation of samplerCube uniforms on XYZ Shader Editor so I can compose scenes with HDRI skyboxes.

Created by marcogomez on Sun, 17 Oct 2021 22:40:28 GMT.


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

// Cubemap declared on PRGM1
// you can find some nice HDRIs at https://polyhaven.com/hdris
// you can convert HDRI to Cubemap with https://matheowis.github.io/HDRI-to-CubeMap/

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

out vec4 fragColor;

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

float osc(float s, float e, float t, float ts) {
  return (e - s) / 2.0 + s + sin(t * ts) * (e - s) * 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, 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;
  vec4 prgm2 = texture(prgm2Texture, uv);
  vec4 prgm6 = texture(prgm6Texture, uv);
  float frameScale = 29.97;
  float frameTime = floor(time * frameScale) / frameScale;
  vec3 grain = gaussgrain(frameTime * 1.0);
  vec3 grainB = gaussgrain(frameTime * 1.3);
  fragColor = clamp(mix(prgm2, prgm6, osc(0.45, 0.65, time, 0.25)), 0.0, 1.0);
  fragColor.a = 1.0;
  fragColor.xyz -= grain * grainB * 0.07;
}

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

uniform sampler2D cubemapTexture0PX; // https://i.imgur.com/95JX8bl.png
uniform sampler2D cubemapTexture0NX; // https://i.imgur.com/Dr7yhmD.png
uniform sampler2D cubemapTexture0PY; // https://i.imgur.com/OvMX9n3.png
uniform sampler2D cubemapTexture0NY; // https://i.imgur.com/BDlj5mI.png
uniform sampler2D cubemapTexture0PZ; // https://i.imgur.com/d7fMPcO.png
uniform sampler2D cubemapTexture0NZ; // https://i.imgur.com/iDyUMaf.png
uniform samplerCube cubemap0;
uniform vec2 resolution;
uniform vec2 mouselerp;
uniform vec2 mouse;
uniform float time;
uniform float fft;

out vec4 fragColor;

const int maxSteps = 100;
const float maxDistance = 10.0;
const float surfaceDistance = 0.001;
const float PI = acos(-1.0);
const float TAU = PI * 2.0;

float acc;

mat2 rotate(float a) {
  float s = sin(a);
  float c = cos(a);
  return mat2(c, -s, s, c);
}

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

vec3 disturb(vec3 s, vec3 a) {
  float minRes = min (resolution.x, resolution.y);
  vec2 fc = gl_FragCoord.xy / minRes;
  float wt = time * 2.0;
  vec2 distortOffset = vec2(sin(wt + fc.y * TAU), sin(wt + fc.x * TAU)) * vec2(0.5);
  a.xy += distortOffset;
  vec3 disturbed = vec3(
    s.x * 1.0 +
    0.03 * sin(3.0 * time + fft + a.x * 5.0 - fft * 5.0) +
    0.02 * sin(2.0 * time + fft + a.y * 4.0 + fft * 4.0) +
    0.01 * sin(1.0 * time + fft + a.z * 3.0 + fft * 3.0),
    s.y * 1.0 +
    0.04 * sin(4.0 * time + fft + a.x * 4.0 + fft * 4.0) +
    0.03 * sin(3.0 * time + fft + a.y * 3.0 - fft * 3.0) +
    0.02 * sin(2.0 * time + fft + a.z * 2.0 + fft * 2.0),
    s.z * 1.0 +
    0.05 * sin(2.0 * time + a.x * 3.0 + fft * 3.0) +
    0.04 * sin(1.0 * time + a.y * 2.0 + fft * 2.0) +
    0.03 * sin(3.0 * time + a.z * 1.0 + fft - 1.0)
  );
  return disturbed;
}

float getDist(vec3 p) {
  float yOff = abs(sin(2.0 * PI * osc(0.0, 0.3, time, 1.0))) * 0.5;
  p.y += -0.3 + yOff;
  p.xz *= rotate(-time * 0.25 - mouselerp.x * PI * 0.35);
  float c = cos(PI / 5.0);
  float s = sqrt(0.75 - c * c);
  vec3 n = vec3(-0.5, -c, s);
  p = mix(disturb(p, p), p, 0.25);
  p = abs(p);
  p -= 2.0 * min(0.0, dot(p, n)) * n; p.xy = abs(p.xy);
  p -= 2.0 * min(0.0, dot(p, n)) * n; p.xy = abs(p.xy);
  p -= 2.0 * min(0.0, dot(p, n)) * n;
  float dist = p.z - 1.0 - fft * 0.3;
  return dist;
}

float rayMarch(vec3 ro, vec3 rd, float side) {
  float dist = 0.0;
  for (int i = 0; i < maxSteps; i++) {
    vec3 p = ro + rd * dist;
    float distSide = getDist(p) * side;
    dist += distSide;
    if (dist > maxDistance || abs(distSide) < surfaceDistance) { break; }
  }
  return dist;
}

vec3 getNormal(vec3 p) {
  float d = getDist(p);
  vec2 closeSample = vec2(0.03, 0.0);
  vec3 closeSampleV3 = vec3(
    getDist(p - closeSample.xyy),
    getDist(p - closeSample.yxy),
    getDist(p - closeSample.yyx)
  );
  vec3 normal = d - closeSampleV3;
  return normalize(normal);
}

vec3 getRayDir(vec2 uv, vec3 p, vec3 l, float z) {
  vec3 f = normalize(l - p);
  vec3 r = normalize(cross(vec3(0.0, 1.0, osc(-0.02, 0.02, time, 0.25)), f));
  vec3 u = cross(f, r);
  vec3 c = f * z;
  vec3 i = c + uv.x * r + uv.y * u;
  vec3 dir = normalize(i);
  return dir;
}

void main(void) {
  vec2 uv = (gl_FragCoord.xy - 0.5 * resolution.xy) / resolution.y;
  vec2 mousePos = vec2(mouselerp.x, (mouselerp.y + 0.25) * 0.25);
  vec3 ro = vec3(0.0, 1.0, -5.0) * 0.7;
  bool isMouseOut = length(mouse) > 2.0;
  if (isMouseOut) { acc += time * 0.125; }
  ro.yz *= rotate((-mousePos.y * PI));
  ro.xz *= rotate((-mousePos.x * TAU) - acc);
  vec3 rd = getRayDir(uv, ro, vec3(0.0), 0.6);
  vec3 col = texture(cubemap0, rd).rgb;
  float d = rayMarch(ro, rd, 1.0);
  float refractionIdx = 1.45;
  if (d < maxDistance) {
    vec3 p = ro + rd * d;
    vec3 n = getNormal(p);
    vec3 r = reflect(rd, n);
    vec3 refOutside = texture(cubemap0, r).rgb;
    vec3 rdIn = refract(rd, n, 1.0 / refractionIdx);
    vec3 pEnter = p - n * surfaceDistance * 3.0;
    float dIn = rayMarch(pEnter, rdIn, -1.0);
    vec3 pExit = pEnter + rdIn * dIn;
    vec3 nExit = -getNormal(pExit);
    vec3 reflTex = vec3(0.0);
    vec3 rdOut = vec3(0.0);
    float abb = 0.01;
    rdOut = refract(rdIn, nExit, refractionIdx - abb);
    if (dot(rdOut, rdOut) == 0.0) { rdOut = reflect(rdIn, nExit); }
    reflTex.r = texture(cubemap0, rdOut).r;
    rdOut = refract(rdIn, nExit, refractionIdx);
    if (dot(rdOut, rdOut) == 0.0) { rdOut = reflect(rdIn, nExit); }
    reflTex.g = texture(cubemap0, rdOut).g;
    rdOut = refract(rdIn, nExit, refractionIdx + abb);
    if (dot(rdOut, rdOut) == 0.0) { rdOut = reflect(rdIn, nExit); }
    reflTex.b = texture(cubemap0, rdOut).b;
    float dens = 0.2;
    float optDist = exp(-dIn * dens);
    reflTex = reflTex * optDist;
    float fresnel = pow(1.0 + dot(rd, n), 5.0);
    col = mix(reflTex, refOutside, fresnel);
    col = mix(col, (col * n * 2.0 * refOutside / (reflTex + 1e-4)) * 0.5 + 0.5, 0.12);
  }
  fragColor = clamp(vec4(col, 1.0), 0.0, 1.0);
}

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

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

out vec4 fragColor;

#define FXAA_REDUCE_MIN (1.0 / 128.0)
#define FXAA_REDUCE_MUL (1.0 / 8.0)
#define FXAA_SPAN_MAX 8.0

vec3 hueShift(vec3 color, float hueAdjust) {
  const vec3 kRGBToYPrime = vec3(0.299, 0.587, 0.114);
  const vec3 kRGBToI = vec3(0.596, -0.275, -0.321);
  const vec3 kRGBToQ = vec3(0.212, -0.523, 0.311);
  const vec3 kYIQToR = vec3(1.0, 0.956, 0.621);
  const vec3 kYIQToG = vec3(1.0, -0.272, -0.647);
  const vec3 kYIQToB = vec3(1.0, -1.107, 1.704);
  float YPrime = dot(color, kRGBToYPrime);
  float I = dot(color, kRGBToI);
  float Q = dot(color, kRGBToQ);
  float hue = atan(Q, I);
  float chroma = sqrt(I * I + Q * Q);
  hue += hueAdjust;
  Q = chroma * sin(hue);
  I = chroma * cos(hue);
  vec3 yIQ = vec3(YPrime, I, Q);
  return vec3(dot(yIQ, kYIQToR), dot(yIQ, kYIQToG), dot(yIQ, kYIQToB));
}

void main() {
  vec3 rgbNW = texture(prgm1Texture, (gl_FragCoord.xy + vec2(-1.0, -1.0)) / resolution.xy).xyz;
  vec3 rgbNE = texture(prgm1Texture, (gl_FragCoord.xy + vec2(+1.0, -1.0)) / resolution.xy).xyz;
  vec3 rgbSW = texture(prgm1Texture, (gl_FragCoord.xy + vec2(-1.0, +1.0)) / resolution.xy).xyz;
  vec3 rgbSE = texture(prgm1Texture, (gl_FragCoord.xy + vec2(+1.0, +1.0)) / resolution.xy).xyz;
  vec4 rgbaM = texture(prgm1Texture, gl_FragCoord.xy / resolution.xy);
  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)) / resolution.xy;
  vec3 rgbA = 0.5 * (
    texture(prgm1Texture, gl_FragCoord.xy / resolution.xy + dir * (1.0 / 3.0 - 0.5)).xyz +
    texture(prgm1Texture, gl_FragCoord.xy / resolution.xy + dir * (2.0 / 3.0 - 0.5)).xyz
  );
  vec3 rgbB = rgbA * 0.5 + 0.25 * (
    texture(prgm1Texture, gl_FragCoord.xy / resolution.xy + dir * -0.5).xyz +
    texture(prgm1Texture, gl_FragCoord.xy / resolution.xy + dir * 0.5).xyz
  );
  float lumaB = dot(rgbB, luma);
  if ((lumaB < lumaMin) || (lumaB > lumaMax)) {
    fragColor = clamp(vec4(rgbA, opacity), 0.0, 1.0);
  } else {
    fragColor = clamp(vec4(rgbB, opacity), 0.0, 1.0);
  }
}

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

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

out vec4 fragColor;

const float amount = 0.8;
float reinhardAmount = 1.0;
float contrast = 1.0;
float brightness = 1.9;
const float saturation = 0.7;
const vec2 vignetteSize = vec2(0.25, 0.25);
const float vignetteRoundness = 0.12;
const float vignetteMix = 1.0;
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 = texture(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);
  fragColor = vec4(color, 1.0);
}

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

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

out vec4 fragColor;

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 = texture(prgm3Texture, p + rs, 0.0).x;
  float g = texture(prgm3Texture, p + gs, 0.0).y;
  float b = texture(prgm3Texture, p + bs, 0.0).z;
  return vec4(r,g,b,1.0);
}

vec4 noise(vec2 uv) {
  return texture(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 = texture(prgm3Texture, uv - mo * 0.05, 0.0).r;
  chromaticAberration.g = texture(prgm3Texture, uv - mo * 0.15, 0.0).g;
  chromaticAberration.b = texture(prgm3Texture, uv - mo * 0.25, 0.0).b;
  vec4 color = vec4(vec3(0.0), 1.0);
  color.xyz = mix(color.xyz, chromaticAberration, 0.7);
  const float speed = 0.008;
  const float amplitude = 0.015;
  vec4 shift = vec4pow(
    noise(
      vec2(speed * time, speed * time / 25.0 )
    ), 8.0
  ) * vec4(vec3(amplitude), 1.0);
  fragColor = mix(color, color + rgbShift(uv, shift), 0.7);
}

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

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

out vec4 fragColor;

const float h = 0.002;
const float v = 0.00000021;
const float g = 0.17;

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

vec3 badVHS(vec2 uv, sampler2D tex) {
  vec3 oColor = texture(tex, uv).xyz;
  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 = texture(tex, vec2(x + uv.x + 0.001, uv.y + 0.001)).x + 0.007;
  _g = texture(tex, vec2(x + uv.x + 0.000, uv.y - 0.002)).y + 0.007;
  _b = texture(tex, vec2(x + uv.x - 0.002, uv.y + 0.000)).z + 0.007;
  _r += 0.08 * texture(tex, 0.75 * vec2(x +  0.012, -0.013) + vec2(uv.x + 0.001, uv.y + 0.001)).x;
  _g += 0.05 * texture(tex, 0.75 * vec2(x + -0.011, -0.010) + vec2(uv.x + 0.000, uv.y - 0.002)).y;
  _b += 0.08 * texture(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.2;
  desatColor = vec3(
    _r + _desat * (_luma - _r),
    _g + _desat * (_luma - _g),
    _b + _desat * (_luma - _b)
  );
  desatColor = clamp(desatColor, 0.0, 1.0);
  return mix(oColor, desatColor, 0.75);
}

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);
}

void main(void) {
  vec2 uv = gl_FragCoord.xy / resolution.xy;
  float frameScale = 29.97;
  float frameTime = floor(time * frameScale) / frameScale;
  vec3 grain = gaussgrain(frameTime * 1.0);
  vec3 grainB = gaussgrain(frameTime * 1.3);
  vec3 vhsCol = badVHS(uv, prgm4Texture) + (grain * grainB) * g;
  vec4 finalColor = vec4(vhsCol, 1.0);
  fragColor = finalColor;
}

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

uniform sampler2D prgm5Texture;
uniform vec2 resolution;
uniform float time;

out vec4 fragColor;

const float PI = acos(-1.0);
const float TAU = PI * 2.0;
const float hardscan = -8.0;
const float hardPix = -2.0;
const float maskDark = 0.5;
const float maskLight = 1.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(texture(prgm5Texture, 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);
}

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 = mix(color, color * vignette, 0.7);
}

void main(void) {
  vec2 res = vec2(800.0, 600.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;
  float s = clamp(0.35 + 0.35 * sin(3.0 * time + uv.y * res.y * 3.0), 0.0, 1.0);
  float scanLines = pow(s, 1.33);
  vec4 color = vec4(tri(uv, res) * mask(gl_FragCoord.xy), 1.0);
  color.xyz = toSRGB(color.xyz * 2.0) - g;
  color = mix(color, color * v, 0.7);
  drawVig(color.xyz, uv);
  color = mix(color, color - s, 0.125);
  fragColor = color;
}





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

// mandatory declaration for the audio buffer generation
// it will determine the audio size in seconds.
#define duration 187 // duration of the song in seconds
// you can also use it in your main function, as I did in
// this example, to have a nice time-based fadeOut effect =)

// Sound methods addapted from the beautiful work of
// SUGIMOTO Yoshiaki - https://twitter.com/catzpaw

uniform vec2 resolution;
uniform float sampleRate;
uniform float blockOffset;

out vec2 fragColor;

const float PI = acos(-1.0);
const float TAU = PI * 2.0;
const float base = 440.0;
const float bpm = 100.0;
const float steps = 480.0;
const float start = 2.0;
const vec4 envelope1 = vec4(0.10, 0.4, 0.50, 0.50);

#define masterDrive    0.5
#define masterPressure 1.0
#define delayRepeat    9
#define delayWet       0.7
#define delayFeedback  0.85
#define delayTime      0.05

float gtime = 0.0;
float gdyn = 1.0;

vec2 amp(vec2 i, vec3 p) {
  vec2 v = pow(abs(i * p.x), vec2(1.0 / p.y));
  return clamp(sign(i) * v, -1.0, 1.0) * p.z;
}

float freq(float n) {
  return pow(2.0, (n - 69.0) / 12.0) * base * TAU;
}

float oscSine(float x, float v) {
  return clamp(sin(x) * v, -1.0, 1.0);
}
float oscNoise(float x, float v) {
  x = floor(x * 1e3 / v) * 1e-3;
  return fract(sin(x * 1717.17) * 1212.12) * 2.0 - 1.0;
}

float envAD(float x, float a, float d) {
  return min(x / max(a, 1e-4), max(0.0, 1.0 - (x - a) / max(d, 1e-4)));
}

float envelopeADSR(float x, vec4 e, float g) {
  return max(
    0.0,
    min(1.0, x / max(e.x, 1e-4)) -
    min(1.0 - e.z, max(x - e.x, 0.0) * (1.0 - e.z) / max(e.y, 1e-4)) -
    max(x - g, 0.0) * e.z / max(e.w, 1e-4)
  );
}

float hash(float n) {
  return fract(sin(n) * 43758.5453123);
}

vec2 hash2(vec2 p) {
  return vec2(hash(p.x), hash(p.y));
}

vec2 noise(vec2 x) {
  vec2 p = floor(x);
  vec2 f = fract(x);
  f = f * f * (3.0 - 2.0 * f);
  vec2 res = mix(
    mix(hash2(p + 0.0), hash2(p + vec2(1.0, 0.0)), f.x),
    mix(hash2(p + vec2(0.0, 1.0)), hash2(p + vec2(1.0, 1.0)), f.x),
    f.y
  );
  return res - 0.5;
}

vec2 fbm(vec2 p) {
  vec2 f;
  f  = 0.50000 * noise(p); p = p * 2.32;
  f += 0.25000 * noise(p); p = p * 2.23;
  f += 0.12500 * noise(p); p = p * 2.31;
  f += 0.06250 * noise(p); p = p * 2.28;
  f += 0.03125 * noise(p);
  return f;
}

vec2 wind(float n) {
  vec2 pos = vec2(n * (162.017331), n * (132.066927));
  vec2 vol = noise(vec2(n * 23.131, -n * 42.13254)) * 1.0 + 1.0;
  vec2 noise = vec2(fbm(pos * 33.313)) * vol.x * 0.5 + vec2(fbm(pos * 4.519)) * vol.y;
  return noise;
}

float instrument(float f, float x) {
  return (
    oscSine(
      f * 0.5 * smoothstep(0.0, 0.02, x) +
      oscSine(f, 0.5) +
      oscSine(f * 2.0, 0.5) +
      oscNoise(x, 0.2) * envAD(x, 0.04, 0.45) * 0.4,
      1.0
    )
  );
}

float pral(float x) { float y = 20.0 / bpm; return ((x > y / 2.0) && (x < y)) ? +2.0 : 0.0; }
float mord(float x) { float y = 20.0 / bpm; return ((x > y / 2.0) && (x < y)) ? -2.0 : 0.0; }

#define P(l,s) float x = 1e3, y = 15.0 * float(l) / bpm, z = 0.0, v = mod(t, y * float(s));
#define N(s,n) if (v > float(s) * y) { x = v - float(s) * y; z = float(n); }
#define NN(s,n) if (v > float(s) * y) { z = float(n); }
#define NP(s,n) if (v > float(s) * y) { x = v - float(s) * y; z = float(n) + pral(x); }
#define NM(s,n) if (v > float(s) * y) { x = v - float(s) * y; z = float(n) + mord(x); }

#define R -1e3
#define D  62.0
#define E  64.0
#define G  67.0
#define A  69.0
#define B  71.0
#define LO -12.0 +

#define dynMF gdyn = 0.7;

vec2 sh(float x, float n) {
  n += sin(x * 24.0) * min(max(0.0, x - 0.5), 0.01) + 12.0;
  float fl = freq(n) * x;
  float fr = fl * 1.0043;
  fl *= 0.9957;
  return vec2(
    instrument(fl, x) * envelopeADSR(x, envelope1, 2.5),
    instrument(fr, x) * envelopeADSR(x, envelope1, 2.3)) * gdyn;
}

#define posret (z < 0.0) ? vec2(0.0) : sh(x, z + o) * 0.5

vec2 sh1(float t, float o) {
  P(2, 16); N(15, D);
  return posret;
}

vec2 sh2(float t, float o) {
  P(2, 16); N(0, E); N(8, G); N(10.9, E); NN(11.5, D); NN(12, LO B); N(14, LO A);
  return posret;
}

vec2 sh3(float t, float o) {
  P(2, 16); NM(0, LO B); N(14, R);
  return posret;
}

vec2 sh4(float t, float o) {
  P(2, 16); N(0, LO E); N(10.25, R); N(12, D); NN(13, E); NN(14, G); NN(15, A);
  return posret;
}

vec2 sh5(float t, float o) {
  P(2, 16); N(0, B); N(7.25, A); NN(8, B); N(15.75, R);
  return posret;
}
vec2 sh6(float t, float o) {
  P(2,16); N(4,E); NP(7,D); NN(8,LO B); N(15,LO A);
  return posret;
}
vec2 sh7(float t, float o) {
  P(2, 16); N(0, LO B); N(10, R); N(12, LO E); N(14, E);
  return posret;
}
vec2 sh8(float t, float o) {
  P(2, 16); N(0, E); N(12, G); NN(14, E); NN(15, D);
  return posret;
}
vec2 sh9(float t, float o) {
  P(2, 16); NM(0, E); N(12, R);
  return posret;
}

#define TRACK                 t = time; v = vec2(0.0);
#define SEGNO(block, blocks)  if (t > float(block) * l) { t = mod(t - float(block) * l, float(blocks) * l);
#define SEQ(block, patterns)  if (t > float(block) * l) { v = patterns; v *= d * smoothstep(0.0, 0.2, v); }
#define DS                    }
#define END                   o += v;

void sequence(float time, float l, float d, inout vec2 o) {
  vec2 v = vec2(0.0);
  float t = time;
  dynMF;
  TRACK;
  SEGNO(0, 10);
  SEQ(1, sh1(t, 12.0));
  SEQ(2, sh2(t, 12.0));
  SEQ(3, sh3(t, 12.0));
  SEQ(4, sh4(t, 12.0));
  SEQ(5, sh5(t, 12.0));
  SEQ(6, sh6(t, 12.0));
  SEQ(7, sh7(t, 12.0));
  SEQ(8, sh8(t, 12.0));
  SEQ(9, sh9(t, 12.0));
  DS END;
}

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

vec2 mainSound(float time) {
  float t = time;
  float stp = steps / bpm;
  float vol = 1.0;
  float dw = delayWet;
  float dt = delayTime;
  vec2 o = vec2(0.0);
  time -= start * 60.0 / bpm;
  if (time < 0.0) { return o; }
  gtime = time;
  sequence(time, stp, 1.0, o);
  for (int i = 0; i < delayRepeat; i++) {
    time -= dt;
    gtime -= dt;
    sequence(time, stp, dw, o);
    dw *= delayFeedback;
    dt += delayTime;
  }
  o += wind(time * 0.04) * osc(0.6, 0.9, t, 0.25);
  return amp(o, vec3(masterDrive, masterPressure, vol));
}

void main(void) {
  vec2 coord = floor(gl_FragCoord.xy);
  float time = blockOffset + (coord.x + coord.y * resolution.x) / sampleRate;
  float d = float(duration);
  float fadeIn = smoothstep(0.0, 4.0, time);
  float fadeOut = clamp(abs(max(d - (2.0 * 2.125), min(d, time)) - d), 0.0, 1.0);
  fragColor = mainSound(time) * fadeIn * fadeOut;
}