The Code Therapy

Biomes in a Sponge Painting

A parallax of displaced layers painted by Fractal Brownian Motion and Perlin Noise fetched colors

Created by marcogomez on Sun, 03 Oct 2021 09:49:12 GMT.


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

uniform sampler2D prgm4Texture;
uniform vec2 resolution;

out vec4 fragColor;

void main(void) {
  vec2 uv = gl_FragCoord.xy / resolution.xy;
  vec4 prgm4 = texture(prgm4Texture, uv);
  fragColor = prgm4;
}

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

uniform vec2 resolution;
uniform float time;

out vec4 fragColor;

const float hk = 1.0 / sqrt(3.0);

vec3 hueShift(vec3 col, float a) {
  const vec3 k = vec3(hk);
  float ca = cos(a);
  return vec3(col * ca + cross(k, col) * sin(a) + k * dot(k, col) * (1.0 - ca));
}

mat2 rot(float a) {
  return mat2(cos(a), -sin(a), sin(a), cos(a));
}

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

vec2 fade(vec2 t) {
  return t * t * t * (t * (t * 6.0 - 15.0) + 10.0);
}

vec4 permute(vec4 x) {
  return mod(((x * 34.0) + 1.0) * x, 289.0);
}

float perlin(vec2 uv) {
  vec4 pi = floor(uv.xyxy) + vec4(0.0, 0.0, 1.0, 1.0);
  pi = mod(pi, 289.0);
  vec4 pf = fract(uv.xyxy) - vec4(0.0, 0.0, 1.0, 1.0);
  vec4 ix = pi.xzxz, iy = pi.yyww, fx = pf.xzxz, fy = pf.yyww;
  vec4 i = permute(permute(ix) + iy);
  vec4 gx = 2.0 * fract(i * 0.0243902439) - 1.0;
  vec4 gy = abs(gx) - 0.5;
  vec4 tx = floor(gx + 0.5);
  gx = gx - tx;
  vec2 g00 = vec2(gx.x, gy.x);
  vec2 g01 = vec2(gx.z, gy.z);
  vec2 g10 = vec2(gx.y, gy.y);
  vec2 g11 = vec2(gx.w, gy.w);
  vec4 n = 1.79284291400159 - 0.85373472095314 * vec4(
    dot(g00, g00), dot(g01, g01),
    dot(g10, g10), dot(g11, g11)
  );
  g00 *= n.x; g01 *= n.y; g10 *= n.z; g11 *= n.w;
  float n00 = dot(g00, vec2(fx.x, fy.x));
  float n01 = dot(g01, vec2(fx.z, fy.z));
  float n10 = dot(g10, vec2(fx.y, fy.y));
  float n11 = dot(g11, vec2(fx.w, fy.w));
  vec2 fxy = fade(pf.xy);
  vec2 nx = mix(vec2(n00, n01), vec2(n10, n11), fxy.x);
  float nxy = mix(nx.x, nx.y, fxy.y);
  return 2.3 * nxy;
}

float clamps(float x) {
  return min(x, 0.98);
}

vec3 clamPos(vec3 c) {
  return clamp(c, 0.01, 1.0);
}

vec3 getBackground(vec2 suv, vec2 uv, float ar) {
  float t1 = time * 0.01;
  float t2 = time * 0.02;
  for (int i = 1; i < 4; i++) {
    float fi = float(i);
    vec2 u = uv + t1;
    u.x += 0.5 / fi * sin(fi * uv.y + t2 + 0.3 * fi) + 0.5;
    u.y += (0.5 * ar) / fi * sin(fi * uv.x + t2 + 0.3 * fi);
    uv = u;
  }
  vec3 colA = vec3(
    clamps(sin(3.0 * uv.x) + osc(0.2, 0.4, t2)),
    clamps(sin(2.0 * uv.y) + osc(0.1, 0.4, t1)),
    sin(uv.x + uv.y)
  );
  vec3 colB = vec3(
    clamps(sin(4.0 * uv.x) + osc(0.2, 0.4, t2)),
    clamps(sin(3.0 * uv.y) + osc(0.1, 0.4, t1)),
    sin(uv.x + uv.y)
  );
  vec3 col = mix(clamPos(colA), clamPos(colB), 0.5);
  float gs = smoothstep(-1.5, 1.5, dot(col, vec3(0.299, 0.587, 0.114)));
  vec3 gray = mix(sqrt(vec3(gs * gs + 0.1)), vec3(gs * gs + 0.1), 0.5);
  return clamp(mix(col, gray, 0.9), 0.0, 1.0);
}

void main(void) {
  vec2 suv = gl_FragCoord.xy / resolution.xy;
  vec2 uv = (suv - 0.5) * 1.5;
  uv *= rot(time * -0.03);
  float ar = resolution.x / resolution.y;
  vec3 col = getBackground(suv, uv * 2.0, ar);
  col = hueShift(col, time * -0.12);
  fragColor = vec4(col, 1.0);
}

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

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

out vec4 fragColor;

const float PI = acos(-1.0);
const float TAU = PI * 2.0;
const float W = 1.2;
const float T = 7.5;
const float amount = 1.0;
const float saturation = 1.4;
const float vignetteMix = 0.5;
const float vignetteSmoothness = 0.5;
const float reinhardAmount = 1.2;
const float contrast = 1.7;
const float brightness = 1.0;

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

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 vignetteSize = vec2(0.25, 0.25) * 1.0 - fft * 0.1;
  float vignetteRoundness = 0.12 + fft * 0.5;
  vec2 uv = gl_FragCoord.xy / resolution.xy;
  vec4 prgm1 = texture(prgm1Texture, uv);
  vec3 reinhard = filmicReinhard(prgm1.rgb);
  vec3 color = prgm1.rgb;
  color = mix(prgm1.rgb, reinhard, reinhardAmount);
  color = ContrastSaturationBrightness(color, brightness, saturation, contrast);
  float v = vignette(uv, vignetteSize, vignetteRoundness, vignetteSmoothness);
  vec3 vig = color / v;
  vec3 g = gaussgrain(time * 0.001) * 0.06;
  color = mix(color, vig, vignetteMix);
  color = mix(prgm1.rgb, color, amount) - g;
  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 fftTexture;
uniform sampler2D prgm2Texture;
uniform vec2 resolution;
uniform float time;
uniform int frame;

out vec4 fragColor;

const float PI = acos(-1.0);
const float TAU = PI * 2.0;
const float bgWaveSpeed = 0.25;
const float xDistMag = 0.021;
const float yDistMag = 0.012;
const float xSineCycles = TAU;
const float ySineCycles = TAU;
const uint k = 1103515245U;

vec3 hash(uvec3 x) {
  x = ((x >> 8U) ^ x.yzx) * k;
  x = ((x >> 8U) ^ x.yzx) * k;
  x = ((x >> 8U) ^ x.yzx) * k;
  return vec3(x) * (1.0 / float(0xffffffffU));
}

vec3 mixColor(vec3 col1, vec3 col2, float v) {
  v = clamp(v, 0.0, 1.0);
  return clamp(col1 + v * (col2 - col1 * 1.5), 0.0, 1.0);
}

float noise(vec2 x) {
  vec2 f = fract(x);
  vec2 u = f * f * f * (f * (f * 6.0 - 15.0) + 10.0);
  vec2 du = 30.0 * f * f * (f * (f - 2.0) + 1.0);
  vec2 p = floor(x);
  float div = 900.0;
  float a = texture(noiseTexture, (p + vec2(0.0, 0.0)) / div).x;
  float b = texture(noiseTexture, (p + vec2(1.0, 0.0)) / div).x;
  float c = texture(noiseTexture, (p + vec2(0.0, 1.0)) / div).x;
  float d = texture(noiseTexture, (p + vec2(1.0, 1.0)) / div).x;
  return (
    a + (b - a) *
    u.x + (c - a) *
    u.y + (a - b - c + d) *
    u.x * u.y
  );
}

float fbm(vec2 x, int detail) {
  float a = 0.0;
  float b = 1.0;
  float acc = 0.0;
  for (int i = 0; i < detail; i++) {
    float n = noise(x);
    a += b*n;
    acc += b;
    b *= 0.8;
    x *= 2.0;
  }
  return a / acc;
}
void main(void) {
  vec2 uv = gl_FragCoord.xy / resolution.y;
  float minRes = min(resolution.x, resolution.y);
  vec2 fc = gl_FragCoord.xy / minRes;
  float wt = time * 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) * 2.0;
  vec3 col = vec3(0.6, 0.7, 1.0) * 1.3;
  float l, h, disp, dist;
  float t = time * 2.0;
  vec2 uv2;
  float acc = 3.0;
  for (float i = 100.0; i >- 10.0; i -= 10.0) {
    l = i / 90.0;
    disp = i / 30.0;
    dist = i;
    uv2 = uv + vec2(t / dist + acc, 0.0); acc += 4.0;
    h = (fbm(uv2 + distortOffset, 12) - 0.5) * disp;
    ivec2 iva = ivec2(acc *  2.0, acc * 3.0);
    ivec2 ivb = ivec2(acc *  4.0, acc * 5.0);
    ivec2 ivc = ivec2(acc *  8.0, acc * 7.0);
    ivec2 ivd = ivec2(acc * 16.0, acc * 9.0);
    int offset = int(floor(min(resolution.x, resolution.y) / 12.0));
    if (uv.y < h + l - 0.04) { col = vec3(-1.0); }
    if (uv.y < h + l - 0.05) { col = mix(texelFetch(prgm2Texture, iva, 0).xyz, texelFetch(prgm2Texture, iva + offset, 0).xyz, 0.5); }
    if (uv.y < h + l - 0.04) { col = mix(col, vec3(0.0), 0.2); }
    if (uv.y < h + l - 0.10) { col = mix(texelFetch(prgm2Texture, ivb, 0).xyz, texelFetch(prgm2Texture, ivb + offset, 0).xyz, 0.5); }
    if (uv.y < h + l - 0.20) { col = mix(texelFetch(prgm2Texture, ivc, 0).xyz, texelFetch(prgm2Texture, ivc + offset, 0).xyz, 0.5); }
    if (uv.y < h + l - 0.40) { col = mix(texelFetch(prgm2Texture, ivd, 0).xyz, texelFetch(prgm2Texture, ivd + offset, 0).xyz, 0.5); }
  }

  uv = gl_FragCoord.xy / resolution.xy;
  col *= 0.5 + 0.5 * pow(16.0 * uv.x * uv.y * (1.0 - uv.x) * (1.0 - uv.y), 0.3);
  fragColor = vec4(col, 1.0);
}

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

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

out vec4 fragColor;

const float PI = acos(-1.0);
const float TAU = PI * 2.0;
const float W = 1.2;
const float T = 7.5;
const float amount = 1.0;
const float saturation = 0.9;
const float vignetteMix = 0.5;
const float vignetteSmoothness = 0.5;
const float reinhardAmount = 0.5;
const float contrast = 0.9;
const float brightness = 1.0;

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

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 vignetteSize = vec2(0.25, 0.25) * 1.0 - fft * 0.1;
  float vignetteRoundness = 0.12 + fft * 0.5;
  vec2 uv = gl_FragCoord.xy / resolution.xy;
  vec4 prgm3 = texture(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;
  vec3 g = gaussgrain(time) * 0.04;
  color = mix(color, vig, vignetteMix);
  color = mix(prgm3.rgb, color, amount) - g;
  color = clamp(mix(color, color * color, 0.6), 0.0, 1.0);
  fragColor = vec4(color, 1.0);
}