Flaming Cellular Automata

Simple cellular automata demonstrating the survival rules know by Star Wars rules.

Created by marcogomez on Sun, 03 Oct 2021 16:54:41 GMT.


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

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

void main(void) {
  vec2 uv = gl_FragCoord.xy / resolution.xy;
  vec4 prgm4 = texture2D(prgm4Texture, uv);
  float fadeIn = min(1.0, time * 0.2);
  prgm4.xyz *= fadeIn;
  gl_FragColor = prgm4;
}

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

uniform sampler2D noiseTexture;
uniform sampler2D prgm1Texture;
uniform vec2 resolution;
uniform vec2 mouselerp;
uniform bool mousedown;
uniform float time;
uniform int frame;
uniform float fft;

#define survive (n == 3 || n == 4 || n == 5)
#define birth (n == 2)
#define history 4.0
#define randomize true

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

float getCell(vec2 uv) {
  return texture2D(prgm1Texture, fract(uv / resolution.xy)).x;
}

int getNeighbors(vec2 uv) {
  float o = 1.0;
  float n = (
    floor(getCell(uv + vec2(+o, +0))) +
    floor(getCell(uv + vec2(-o, +0))) +
    floor(getCell(uv + vec2(+0, +o))) +
    floor(getCell(uv + vec2(+0, -o))) +
    floor(getCell(uv + vec2(+o, +o))) +
    floor(getCell(uv + vec2(-o, +o))) +
    floor(getCell(uv + vec2(+o, -o))) +
    floor(getCell(uv + vec2(-o, -o)))
  );
  return int(n * 1.0);
}

void main(void) {
  float mouseSize = (16.0 + fft * 48.0) / min(resolution.x, resolution.y);
  vec2 uv = gl_FragCoord.xy;
  float t = fft + time * 0.5;
  bool stutter = (sin(t * 0.25) * cos(t * 0.25) > 0.0) ? false : true;
  float minRes = min(resolution.x, resolution.y) / (50.0 - fft * 50.0);
  float o = osc(-0.1, 0.2, time, 0.0625);
  vec2 v2o = vec2(0.75 + o, 0.85 + o);
  vec2 m = (distance(vec2(0.0), mouselerp.xy) < 0.1)
    ? (stutter) ? floor(((vec2(cos(t), sin(t) * cos(t)) * v2o) * minRes) + 0.5) / minRes : (vec2(cos(t), sin(t) * cos(t)) * v2o)
    : mouselerp;

  float cell = getCell(uv);
  int n = getNeighbors(uv);
  if(!(cell == 1.0 && survive)) {
    if(cell == 0.0 && birth) {
      cell = 1.0;
    } else {
      cell = max(0.0, cell - (1.0 / history));
    }
  }
  if (
    distance((gl_FragCoord.xy / resolution.xy) * 2.0 - 1.0, m) < mouseSize ||
    (frame == 0 && randomize) ||
    mousedown && randomize && mod(float(frame), 3.0) == 0.0
  ) {
    cell = floor(texture2D(noiseTexture, fract((uv + float(frame)) / resolution.xy)).r + 0.5);
  }
  gl_FragColor = vec4(cell, float(n) / 8.0, 0.0, 1.0);
}

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

uniform sampler2D prgm1Texture;
uniform sampler2D prgm2Texture;
uniform vec2 resolution;
uniform int frame;

const float tileSize = 2.0;

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

void main(void) {
  vec2 uv = gl_FragCoord.xy / resolution.xy;
  float r = rand(uv);
  vec3 mCol;
  if (r > 0.6667) {
    mCol = vec3(1.0, 0.7, 0.3);
  } else if (r > 0.3334) {
    mCol = vec3(0.7, 0.5, 0.0);
  } else {
    mCol = vec3(0.5, 0.3, 0.0);
  }
  vec4 prgm1 = texture2D(prgm1Texture, uv);
  vec4 prgm2 = texture2D(prgm2Texture, uv);
  if (mod(float(frame), 2.0) == 0.0) {
    prgm1.xyz *= mCol;
    gl_FragColor = prgm1;
  }
  prgm2.xyz = mix(prgm2.xyz, prgm2.xyz * mCol, 0.25);
  gl_FragColor += prgm2 * 0.97;
}

// ╔═════════════╦═════════════════╦════════════════╗
// ║ 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 vec2 hashv2 = vec2(12.9898, 78.233);
const float hashS = 43758.5453123;

float hash(vec2 p) {
  return fract(sin(dot(p, vec2(41.0, 289.0))) * hashS);
}

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

void main(void) {
  vec2 uv = gl_FragCoord.xy / resolution.xy;
  vec4 prgm2 = texture2D(prgm2Texture, uv);
  float frameScale = 29.97;
  float frameTime = floor(time * frameScale) / frameScale;
  vec4 col = prgm2;
  float w = 0.1;
  vec2 a = vec2(uv.x - 0.5, uv.y - 0.66);
  vec2 b = a * 0.15 / float(10.0);
  uv += b * (hash(uv.xy + fract(time)) * 2.0);
  for (float i = 1.0; i > 0.9; i-= 0.000625) {
    uv -= 0.5;
    uv *= i;
    uv += 0.5;
    col += texture2D(prgm2Texture, uv) * w * 1.5;
    w *= 0.95;
  }
  vec3 gA = gaussgrain(frameTime) * 0.42;
  vec3 gB = gaussgrain(frameTime + 0.1) * 0.42;
  col *= 0.9; col.xyz += gA * gB;
  gl_FragColor = mix(prgm2, col, 0.25);
}

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

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

const float PI = acos(-1.0);
const float TAU = PI * 2.0;
const float hardscan = -32.0; // -8.0 = soft | -16.0 = medium
const float hardPix = -8.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 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(prgm3Texture, 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(resolution.x / 4.0, resolution.y / 3.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 = mix(uv, warp(uv, warpAmount), 0.75);
  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.7);
  gl_FragColor = color;
}