Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@markusfisch
Last active January 14, 2020 10:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markusfisch/93f149bc3e804a6b8a211891c6f3f60a to your computer and use it in GitHub Desktop.
Save markusfisch/93f149bc3e804a6b8a211891c6f3f60a to your computer and use it in GitHub Desktop.
#ifdef GL_FRAGMENT_PRECISION_HIGH
precision highp float;
#else
precision mediump float;
#endif
uniform vec2 resolution;
uniform int pointerCount;
uniform vec3 pointers[10];
uniform sampler2D backbuffer;
float oneIfZero(float value) {
return step(abs(value), 0.1);
}
vec4 get4(float x, float y) {
return texture2D(backbuffer,
(gl_FragCoord.xy + vec2(x, y)) / resolution);
}
vec4 evaluate(float sum) {
vec4 cell = get4(0.0, 0.0);
float wasAlive = cell[3];
float has3 = oneIfZero(sum - 3.0);
float has2 = oneIfZero(sum - 2.0);
// a cell is (or becomes) alive if it has 3 neighbors
// or if it has 2 neighbors *and* was alive already
float isAlive = min(1.0, has3 + has2 * wasAlive);
float justDied = (1.0 - isAlive) * wasAlive;
float afterglow = cell[2];
return vec4(
afterglow * isAlive,
afterglow * (1.0 - isAlive),
max(afterglow * 0.99, justDied),
isAlive
);
}
float get(float x, float y) {
return get4(x, y).a;
}
void main() {
float sum =
get(-1.0, -1.0) +
get(-1.0, 0.0) +
get(-1.0, 1.0) +
get(0.0, -1.0) +
get(0.0, 1.0) +
get(1.0, -1.0) +
get(1.0, 0.0) +
get(1.0, 1.0);
float tapSize = min(resolution.x, resolution.y) * 0.05;
for (int n = 0; n < pointerCount; ++n) {
if (distance(pointers[n].xy, gl_FragCoord.xy) < tapSize) {
sum = 3.0;
break;
}
}
gl_FragColor = evaluate(sum);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment