The Code Therapy

The Rhombic Triacontahedron AA

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, this version with TXAA (temporal anti-aliasing).

Created by marcogomez on Wed, 20 Oct 2021 01:56:47 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 vec2 mouselerp;
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;
}

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

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 grainA = gaussgrain(frameTime * 1.0);
  vec3 grainB = gaussgrain(frameTime * 1.3);
  float m = clamp(1.0 - length(uv - 0.5), 0.0, 1.0);
  fragColor = mix(prgm2, prgm6, m * 0.7);
  fragColor.xyz += grainA * grainB * 0.1;
  fragColor.a = 1.0;
}

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

// This is a demo of my initial implementation of samplerCube uniforms for XYZ

// 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 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 = 128;
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.05, 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.07);
  }
  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 sampler2D prgm2Texture;
uniform vec2 resolution;
uniform float time;

#define inputTexture prgm1Texture
#define inoutTexture prgm2Texture

out vec4 fragColor;

vec3 encodePalYuv(vec3 rgb) {
  rgb = pow(rgb, vec3(2.0));
  return vec3(
    dot(rgb, vec3(0.299, 0.587, 0.114)),
    dot(rgb, vec3(-0.14713, -0.28886, 0.436)),
    dot(rgb, vec3(0.615, -0.51499, -0.10001))
  );
}

vec3 decodePalYuv(vec3 yuv) {
  vec3 rgb = vec3(
    dot(yuv, vec3(1., 0., 1.13983)),
    dot(yuv, vec3(1., -0.39465, -0.58060)),
    dot(yuv, vec3(1., 2.03211, 0.))
  );
  return pow(rgb, vec3(1.0 / 2.0));
}

void main(void) {
  vec2 uv = gl_FragCoord.xy / resolution.xy;
  vec4 lastColor = texture(inoutTexture, uv);
  vec3 antialiased = lastColor.xyz;
  float mixRate = clamp(lastColor.w, 0.01, 0.5);
  vec2 off = 1.0 / resolution.xy;
  vec3 in0 = texture(inputTexture, uv).xyz;
  antialiased = mix(antialiased * antialiased, in0 * in0, mixRate);
  antialiased = sqrt(antialiased);
  vec3 in1 = texture(inputTexture, uv + vec2(+off.x,    0.0)).xyz;
  vec3 in2 = texture(inputTexture, uv + vec2(-off.x,    0.0)).xyz;
  vec3 in3 = texture(inputTexture, uv + vec2(0.0,    +off.y)).xyz;
  vec3 in4 = texture(inputTexture, uv + vec2(0.0,    -off.y)).xyz;
  vec3 in5 = texture(inputTexture, uv + vec2(+off.x, +off.y)).xyz;
  vec3 in6 = texture(inputTexture, uv + vec2(-off.x, +off.y)).xyz;
  vec3 in7 = texture(inputTexture, uv + vec2(+off.x, -off.y)).xyz;
  vec3 in8 = texture(inputTexture, uv + vec2(-off.x, -off.y)).xyz;
  in0 = encodePalYuv(in0);
  in1 = encodePalYuv(in1);
  in2 = encodePalYuv(in2);
  in3 = encodePalYuv(in3);
  in4 = encodePalYuv(in4);
  in5 = encodePalYuv(in5);
  in6 = encodePalYuv(in6);
  in7 = encodePalYuv(in7);
  in8 = encodePalYuv(in8);
  vec3 minColor = min(min(min(in0, in1), min(in2, in3)), in4);
  vec3 maxColor = max(max(max(in0, in1), max(in2, in3)), in4);
  minColor = mix(
    minColor,
    min(min(min(in5, in6), min(in7, in8)), minColor),
    0.5
  );
  maxColor = mix(
    maxColor,
    max(max(max(in5, in6), max(in7, in8)), maxColor),
    0.5
  );
  vec3 preclamping = antialiased;
  antialiased = clamp(antialiased, minColor, maxColor);
  mixRate = 1.0 / (1.0 / mixRate + 1.0);
  vec3 diff = antialiased - preclamping;
  float clampAmount = dot(diff, diff);
  mixRate += clampAmount * 4.0;
  mixRate = clamp(mixRate, 0.05, 0.5);
  antialiased = decodePalYuv(antialiased);
  fragColor = vec4(antialiased, mixRate);
}

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

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

#define inputTexture prgm2Texture

out vec4 fragColor;

vec3 texSample(const int x, const int y, vec2 fragCoord) {
  vec2 uv = gl_FragCoord.xy / resolution.xy * resolution.xy;
  uv = (uv + vec2(x, y)) / resolution.xy;
  return texture(inputTexture, uv).xyz;
}

vec3 sharpenFilter(vec2 fragCoord, float strength) {
  vec3 f = (
    texSample(-1, -1, fragCoord) * -1.0 +
    texSample(+1, -1, fragCoord) * -1.0 +
    texSample(+0, +0, fragCoord) * +5.0 +
    texSample(-1, +1, fragCoord) * -1.0 +
    texSample(+1, +1, fragCoord) * -1.0
  );
  return mix(texSample( 0, 0, fragCoord), f , strength);
}

void main(void) {
  vec2 uv = gl_FragCoord.xy / resolution.xy;
  fragColor = vec4(sharpenFilter(gl_FragCoord.xy, 0.35), 1.0);
}

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

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

#define inputTexture prgm3Texture

out vec4 fragColor;

#define FXAA 1

#ifndef FXAA_PRESET
  #define FXAA_PRESET 5
  #define FXAA_DEBUG_SKIPPED 0
  #define FXAA_DEBUG_PASSTHROUGH 0
  #define FXAA_DEBUG_HORZVERT 0
  #define FXAA_DEBUG_PAIR 0
  #define FXAA_DEBUG_NEGPOS 0
  #define FXAA_DEBUG_OFFSET 0
  #define FXAA_DEBUG_HIGHLIGHT 0
  #define FXAA_LUMINANCE 1
#endif

#if (FXAA_PRESET == 0)
  #define FXAA_EDGE_THRESHOLD      (1.0 / 4.0)
  #define FXAA_EDGE_THRESHOLD_MIN  (1.0 / 12.0)
  #define FXAA_SEARCH_STEPS        2
  #define FXAA_SEARCH_ACCELERATION 4
  #define FXAA_SEARCH_THRESHOLD    (1.0 / 4.0)
  #define FXAA_SUBPIX              1
  #define FXAA_SUBPIX_FASTER       1
  #define FXAA_SUBPIX_CAP          (2.0 / 3.0)
  #define FXAA_SUBPIX_TRIM         (1.0 / 4.0)
#endif

#if (FXAA_PRESET == 1)
  #define FXAA_EDGE_THRESHOLD      (1.0 / 8.0)
  #define FXAA_EDGE_THRESHOLD_MIN  (1.0 / 16.0)
  #define FXAA_SEARCH_STEPS        4
  #define FXAA_SEARCH_ACCELERATION 3
  #define FXAA_SEARCH_THRESHOLD    (1.0 / 4.0)
  #define FXAA_SUBPIX              1
  #define FXAA_SUBPIX_FASTER       0
  #define FXAA_SUBPIX_CAP          (3.0 / 4.0)
  #define FXAA_SUBPIX_TRIM         (1.0 / 4.0)
#endif

#if (FXAA_PRESET == 2)
  #define FXAA_EDGE_THRESHOLD      (1.0 / 8.0)
  #define FXAA_EDGE_THRESHOLD_MIN  (1.0 / 24.0)
  #define FXAA_SEARCH_STEPS        8
  #define FXAA_SEARCH_ACCELERATION 2
  #define FXAA_SEARCH_THRESHOLD    (1.0 / 4.0)
  #define FXAA_SUBPIX              1
  #define FXAA_SUBPIX_FASTER       0
  #define FXAA_SUBPIX_CAP          (3.0 / 4.0)
  #define FXAA_SUBPIX_TRIM         (1.0 / 4.0)
#endif

#if (FXAA_PRESET == 3)
  #define FXAA_EDGE_THRESHOLD      (1.0 / 8.0)
  #define FXAA_EDGE_THRESHOLD_MIN  (1.0 / 24.0)
  #define FXAA_SEARCH_STEPS        16
  #define FXAA_SEARCH_ACCELERATION 1
  #define FXAA_SEARCH_THRESHOLD    (1.0 / 4.0)
  #define FXAA_SUBPIX              1
  #define FXAA_SUBPIX_FASTER       0
  #define FXAA_SUBPIX_CAP          (3.0 / 4.0)
  #define FXAA_SUBPIX_TRIM         (1.0 / 4.0)
#endif

#if (FXAA_PRESET == 4)
  #define FXAA_EDGE_THRESHOLD      (1.0 / 8.0)
  #define FXAA_EDGE_THRESHOLD_MIN  (1.0 / 24.0)
  #define FXAA_SEARCH_STEPS        24
  #define FXAA_SEARCH_ACCELERATION 1
  #define FXAA_SEARCH_THRESHOLD    (1.0 / 4.0)
  #define FXAA_SUBPIX              1
  #define FXAA_SUBPIX_FASTER       0
  #define FXAA_SUBPIX_CAP          (3.0 / 4.0)
  #define FXAA_SUBPIX_TRIM         (1.0 / 4.0)
#endif

#if (FXAA_PRESET == 5)
  #define FXAA_EDGE_THRESHOLD      (1.0 / 8.0)
  #define FXAA_EDGE_THRESHOLD_MIN  (1.0 / 24.0)
  #define FXAA_SEARCH_STEPS        32
  #define FXAA_SEARCH_ACCELERATION 1
  #define FXAA_SEARCH_THRESHOLD    (1.0 / 4.0)
  #define FXAA_SUBPIX              1
  #define FXAA_SUBPIX_FASTER       0
  #define FXAA_SUBPIX_CAP          (3.0 / 4.0)
  #define FXAA_SUBPIX_TRIM         (1.0 / 4.0)
#endif

#if (FXAA_PRESET == 6)
  #define FXAA_EDGE_THRESHOLD      (1.0 / 8.0)
  #define FXAA_EDGE_THRESHOLD_MIN  (1.0 / 24.0)
  #define FXAA_SEARCH_STEPS        128
  #define FXAA_SEARCH_ACCELERATION 1
  #define FXAA_SEARCH_THRESHOLD    (1.0 / 4.0)
  #define FXAA_SUBPIX              1
  #define FXAA_SUBPIX_FASTER       0
  #define FXAA_SUBPIX_CAP          (3.0 / 4.0)
  #define FXAA_SUBPIX_TRIM         (1.0 / 4.0)
#endif

#define FXAA_SUBPIX_TRIM_SCALE (1.0 / (1.0 - FXAA_SUBPIX_TRIM))

vec2 ToVec2(float value) { return vec2(value, value); }
vec3 ToVec3(float value) { return vec3(value, value, value); }
vec3 ToVec3(vec2 vector, float z) { return vec3(vector.x, vector.y, z); }
vec3 ToVec3(vec2 vector) { return ToVec3(vector, 0.0); }
vec4 ToVec4(vec2 vector, float z, float w) { return vec4(vector.x, vector.y, z, w); }
vec4 ToVec4(vec2 vector, float z) { return ToVec4(vector, z, 0.0); }
vec4 ToVec4(vec2 vector) { return ToVec4(vector, 0.0); }
vec4 ToVec4(vec3 vector, float w) { return vec4(vector.x, vector.y, vector.z, w); }
vec4 ToVec4(vec3 vector) { return ToVec4(vector, 0.0); }
vec4 ToVec4(float value, float w) { return vec4(value, value, value, w); }
vec4 ToVec4(float value) { return ToVec4(value, 0.0); }

vec4 TextureOffset(sampler2D tex, vec2 uv, vec2 offset) {
  return texture(tex, uv + offset);
}

vec3 Grayscale(vec3 color, int index) {
  int selectedChannel = clamp(index, 0, 2);
  return ToVec3(color[selectedChannel]);
}

vec4 Grayscale(vec4 color, int index) {
  int selectedChannel = clamp(index, 0, 3);
  return ToVec4(color[selectedChannel]);
}

vec3 Grayscale(vec3 color) { return Grayscale(color, 1); }
vec4 Grayscale(vec4 color) { return Grayscale(color, 1); }

float LinearRGBLuminance(vec3 color) {
  vec3 weight = vec3(0.2126729, 0.7151522, 0.0721750);
  return dot(color, weight);
}

float FXAALuminance(vec3 color) {
  #if FXAA_LUMINANCE == 0
  return LinearRGBLuminance(color);
  #else
  return color.g * (0.587 / 0.299) + color.r;
  #endif
}

float FXAAVerticalEdge(float lumaO, float lumaN, float lumaE, float lumaS, float lumaW, float lumaNW, float lumaNE, float lumaSW, float lumaSE) {
  float top = (0.25 * lumaNW) + (-0.5 * lumaN) + (0.25 * lumaNE);
  float middle = (0.50 * lumaW ) + (-1.0 * lumaO) + (0.50 * lumaE );
  float bottom = (0.25 * lumaSW) + (-0.5 * lumaS) + (0.25 * lumaSE);
  return abs(top) + abs(middle) + abs(bottom);
}

float FXAAHorizontalEdge(float lumaO, float lumaN, float lumaE, float lumaS, float lumaW, float lumaNW, float lumaNE, float lumaSW, float lumaSE) {
  float top = (0.25 * lumaNW) + (-0.5 * lumaW) + (0.25 * lumaSW);
  float middle = (0.50 * lumaN ) + (-1.0 * lumaO) + (0.50 * lumaS);
  float bottom = (0.25 * lumaNE) + (-0.5 * lumaE) + (0.25 * lumaSE);
  return abs(top) + abs(middle) + abs(bottom);
}

vec3 applyFXAA(sampler2D textureSource, vec2 textureDimensions, vec2 pixelPosition, vec2 screenResolution) {
  vec2 uv = pixelPosition / screenResolution;
  vec2 texel = vec2(1.0, 1.0) / textureDimensions;
  vec3 rgbN = TextureOffset(textureSource, uv, vec2(0, -texel.y)).rgb;
  vec3 rgbW = TextureOffset(textureSource, uv, vec2(-texel.x, 0)).rgb;
  vec3 rgbO = TextureOffset(textureSource, uv, vec2(0, 0)).rgb;
  vec3 rgbE = TextureOffset(textureSource, uv, vec2(texel.x, 0)).rgb;
  vec3 rgbS = TextureOffset(textureSource, uv, vec2(0, texel.y)).rgb;
  #if FXAA == 0
  return rgbO;
  #endif
  float lumaN = FXAALuminance(rgbN);
  float lumaW = FXAALuminance(rgbW);
  float lumaO = FXAALuminance(rgbO);
  float lumaE = FXAALuminance(rgbE);
  float lumaS = FXAALuminance(rgbS);
  float minLuma = min(lumaO, min(min(lumaN, lumaW), min(lumaS, lumaE)));
  float maxLuma = max(lumaO, max(max(lumaN, lumaW), max(lumaS, lumaE)));
  float localContrast = maxLuma - minLuma;
  if (localContrast < max(FXAA_EDGE_THRESHOLD_MIN, maxLuma * FXAA_EDGE_THRESHOLD)) {
    #if FXAA_DEBUG_SKIPPED
      return vec3(0);
    #else
      return rgbO;
    #endif
  }
  #if FXAA_SUBPIX > 0
    vec3 rgbL = (rgbN + rgbW + rgbO + rgbE + rgbS);
    #if FXAA_SUBPIX_FASTER
      rgbL *= (1.0 / 5.0);
    #endif
    float lumaL = (lumaN + lumaW + lumaS + lumaE) * 0.25;
    float pixelContrast = abs(lumaL - lumaO);
    float contrastRatio = pixelContrast / localContrast;
    float lowpassBlend = 0.0;
    #if FXAA_SUBPIX == 1
      lowpassBlend = max(0.0, contrastRatio - FXAA_SUBPIX_TRIM) * FXAA_SUBPIX_TRIM_SCALE;
      lowpassBlend = min(FXAA_SUBPIX_CAP, lowpassBlend);
    #elif FXAA_SUBPIX == 2
      lowpassBlend = contrastRatio;
    #endif
  #endif
  #if FXAA_DEBUG_PASSTHROUGH
    #if FXAA_SUBPIX > 0
      return vec3(localContrast, lowpassBlend, 0.0);
    #else
      return vec3(localContrast, 0.0, 0.0);
    #endif
  #endif
  vec3 rgbNW = TextureOffset(textureSource, uv, vec2(-texel.x, -texel.y)).rgb;
  vec3 rgbNE = TextureOffset(textureSource, uv, vec2(texel.x, -texel.y)).rgb;
  vec3 rgbSW = TextureOffset(textureSource, uv, vec2(-texel.x, texel.y)).rgb;
  vec3 rgbSE = TextureOffset(textureSource, uv, vec2(texel.x, texel.y)).rgb;

  #if FXAA_SUBPIX > 0
    #if FXAA_SUBPIX_FASTER == 0
      rgbL += (rgbNW + rgbNE + rgbSW + rgbSE);
      rgbL *= (1.0 / 9.0);
    #endif
  #endif

  float lumaNW = FXAALuminance(rgbNW);
  float lumaNE = FXAALuminance(rgbNE);
  float lumaSW = FXAALuminance(rgbSW);
  float lumaSE = FXAALuminance(rgbSE);
  float edgeVert = FXAAVerticalEdge(lumaO, lumaN, lumaE, lumaS, lumaW, lumaNW, lumaNE, lumaSW, lumaSE);
  float edgeHori = FXAAHorizontalEdge(lumaO, lumaN, lumaE, lumaS, lumaW, lumaNW, lumaNE, lumaSW, lumaSE);
  bool isHorizontal = edgeHori >= edgeVert;

  #if FXAA_DEBUG_HORZVERT
    if (isHorizontal) {
      return vec3(1.0, 0.75, 0.0);
    } else {
      return vec3(0.10, 0.10, 1.0);
    }
  #endif

  float edgeSign = isHorizontal ? -texel.y : -texel.x;
  float gradientNeg = isHorizontal ? abs(lumaN - lumaO) : abs(lumaW - lumaO);
  float gradientPos = isHorizontal ? abs(lumaS - lumaO) : abs(lumaE - lumaO);
  float lumaNeg = isHorizontal ? ((lumaN + lumaO) * 0.5) : ((lumaW + lumaO) * 0.5);
  float lumaPos = isHorizontal ? ((lumaS + lumaO) * 0.5) : ((lumaE + lumaO) * 0.5);
  bool isNegative = (gradientNeg >= gradientPos);
  float gradientHighest = isNegative ? gradientNeg : gradientPos;
  float lumaHighest = isNegative ? lumaNeg : lumaPos;
  if (isNegative) { edgeSign *= -1.0; }

  #if FXAA_DEBUG_PAIR
    return isHorizontal ? vec3(0.0, gradientHighest, lumaHighest) : vec3(0.0, lumaHighest, gradientHighest);
  #endif

  vec2 pointN = vec2(0.0, 0.0);
  pointN.x = uv.x + (isHorizontal ? 0.0 : edgeSign * 0.5);
  pointN.y = uv.y + (isHorizontal ? edgeSign * 0.5 : 0.0);
  gradientHighest *= FXAA_SEARCH_THRESHOLD;
  vec2 pointP = pointN;
  vec2 pointOffset = isHorizontal ? vec2(texel.x, 0.0) : vec2(0.0, texel.y);
  float lumaNegEnd = lumaNeg;
  float lumaPosEnd = lumaPos;
  bool searchNeg = false;
  bool searchPos = false;
  if (FXAA_SEARCH_ACCELERATION == 1) {
    pointN += pointOffset * vec2(-1.0);
    pointP += pointOffset * vec2(1.0);
  } else if (FXAA_SEARCH_ACCELERATION == 2) {
    pointN += pointOffset * vec2(-1.5);
    pointP += pointOffset * vec2(1.5);
    pointOffset *= vec2(2.0);
  } else if (FXAA_SEARCH_ACCELERATION == 3) {
    pointN += pointOffset * vec2(-2.0);
    pointP += pointOffset * vec2(2.0);
    pointOffset *= vec2(3.0);
  } else if(FXAA_SEARCH_ACCELERATION == 4) {
    pointN += pointOffset * vec2(-2.5);
    pointP += pointOffset * vec2(2.5);
    pointOffset *= vec2(4.0);
  }
  for (int i = 0; i < FXAA_SEARCH_STEPS; i++) {
    if (FXAA_SEARCH_ACCELERATION == 1) {
      if (!searchNeg) { lumaNegEnd = FXAALuminance(texture(textureSource, pointN).rgb); }
      if (!searchPos) { lumaPosEnd = FXAALuminance(texture(textureSource, pointP).rgb); }
    } else {
      if (!searchNeg) { lumaNegEnd = FXAALuminance(textureGrad(textureSource, pointN, pointOffset, pointOffset).rgb); }
      if (!searchPos) { lumaPosEnd = FXAALuminance(textureGrad(textureSource, pointP, pointOffset, pointOffset).rgb); }
    }
    searchNeg = searchNeg || (abs(lumaNegEnd - lumaHighest) >= gradientHighest);
    searchPos = searchPos || (abs(lumaPosEnd - lumaHighest) >= gradientHighest);
    #if FXAA_DEBUG_NEGPOS
      if (searchNeg) {
        return vec3(abs(lumaNegEnd - gradientNeg), 0.0, 0.0);
      } else if (searchPos) {
        return vec3(0.0, 0.0, abs(lumaPosEnd - gradientPos));
      }
    #endif

    if (searchNeg && searchPos) { break; }
    if (!searchNeg) { pointN -= pointOffset; }
    if (!searchPos) { pointP += pointOffset; }
  }

  float distanceNeg = isHorizontal ? uv.x - pointN.x : uv.y - pointN.y;
  float distancePos = isHorizontal ? pointP.x - uv.x : pointP.y - uv.y;
  bool isCloserToNegative = distanceNeg < distancePos;
  float lumaEnd = isCloserToNegative ? lumaNegEnd : lumaPosEnd;
  if (((lumaO - lumaNeg) < 0.0) == ((lumaEnd - lumaNeg) < 0.0)) {
    edgeSign = 0.0;
  }
  float filterSpanLength = (distancePos + distanceNeg);
  float filterDistance = isCloserToNegative ? distanceNeg : distancePos;
  float subpixelOffset = (0.5 + (filterDistance * (-1.0 / filterSpanLength))) * edgeSign;

  #if FXAA_DEBUG_OFFSET
    if (subpixelOffset < 0.0) {
      return isHorizontal ? vec3(1.0, 0.0, 0.0) : vec3(1.0, 0.7, 0.1);
    }
    if (subpixelOffset > 0.0) {
      return isHorizontal ? vec3(0.0, 0.0, 1.0) : vec3(0.1, 0.3, 1.0);
    }
  #endif

  vec3 rgbOffset = textureLod(textureSource, vec2(uv.x + (isHorizontal ? 0.0 : subpixelOffset), uv.y + (isHorizontal ? subpixelOffset : 0.0)), 0.0).rgb;

  #if FXAA_DEBUG_HIGHLIGHT
    return isHorizontal ? vec3(1.0, 0.0, 0.0) : vec3(0.0, 1.0, 0.0);
  #endif

  #if FXAA_SUBPIX == 0
    return vec3(rgbOffset);
  #else
    return mix(rgbOffset, rgbL, lowpassBlend);
  #endif
}

void main(void) {
  vec3 resultFXAA = applyFXAA(inputTexture, resolution.xy, gl_FragCoord.xy, resolution.xy);
  fragColor = ToVec4(resultFXAA, 1.0);
}

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

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

out vec4 fragColor;

#define inputTexture prgm4Texture
const float amount = 1.0;
const float contrast = 1.3;
const float saturation = 0.7;
const float brightness = 2.5;
const float reinhardAmount = 1.0;
const float matrixAmount = 0.1;
const float gaussGrainAmount = 0.1;
const float frameTimeScale = 29.97;
const vec2 vignetteSize = vec2(0.25, 0.25);
const float vignetteRoundness = 0.12;
const float vignetteMix = 0.2;
const float vignetteSmoothness = 0.42;

const float W = 1.2;
const float T = 7.5;
const float PI = acos(-1.0);
const float TAU = PI * 2.0;

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 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 matrixTonemapping(vec3 color) {
  return vec3(
    pow(abs(color.r), (3.0 / 2.0)),
    pow(abs(color.g), (4.0 / 5.0)),
    pow(abs(color.b), (3.0 / 2.0))
  );
}

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

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;
  float frameTime = floor(time * frameTimeScale) / frameTimeScale;
  vec3 grainA = gaussgrain(frameTime * 1.1);
  vec3 grainB = gaussgrain(frameTime * 1.3);
  vec3 grain = grainA * grainB * gaussGrainAmount;
  vec4 tex = texture(inputTexture, uv);
  vec3 color = tex.rgb;
  vec3 reinhard = filmicReinhard(tex.rgb);
  vec3 matrix = matrixTonemapping(tex.rgb);
  color = mix(color, reinhard, reinhardAmount);
  color = contrastSaturationBrightness(color, brightness, saturation, contrast);
  color = mix(color, matrix, matrixAmount);
  float v = vignette(uv, vignetteSize, vignetteRoundness, vignetteSmoothness);
  vec3 vig = color * v;
  color = mix(color, vig, vignetteMix);
  color = mix(tex.xyz, color, amount);
  color -= grain;
  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 prgm5Texture;
uniform vec2 resolution;
uniform float time;

#define inputTexture prgm5Texture

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(inputTexture, 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(1024.0, 768.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 grainA = gaussgrain(frameTime * 1.0);
  vec3 grainB = gaussgrain(frameTime * 1.3);
  vec3 g = grainA * grainB * 0.15;
  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;
}