diff --git a/John-Conway's-Game-of-Life:2D/BufferA.glsl b/John-Conway's-Game-of-Life:2D/BufferA.glsl new file mode 100644 index 0000000..7f8914f --- /dev/null +++ b/John-Conway's-Game-of-Life:2D/BufferA.glsl @@ -0,0 +1,49 @@ +void mainImage(out vec4 fragColor, in vec2 fragCoord) +{ + vec2 uv = fragCoord/iResolution.xx; + vec2 st = pixelize(uv); + ivec2 texel = ivec2(st * iResolution.xx); + + // initialize the buffer with some noise on start + if (iFrame == 0) + { + fragColor = vec4( step(0.9, fract(sin(dot(st + iDate.z, vec2(12.345, 97.34857))*33.2984)*456.2349) ) ); + return; + } + + // paint at the mouse cursor + if (pixelize(iMouse.xy/iResolution.xx) == st) + { + fragColor = vec4(1); + return; + } + + // only continue in iteration at every nth rendered frame + if (iFrame % FRAME_INTERLACE != 0) + { + fragColor = texture(iChannel0, fragCoord/iResolution.xy); + return; + } + + + // sum neighbooring alive cells + int c = 0; + for (int x = -1; x < 2; x++) + for (int y = -1; y < 2; y++) + { + if (x == 0 && y == 0) + continue; + + c += int(texelFetch(iChannel0, texel + ivec2(x,y), 0).r); + } + + // center cell + int x = int(texelFetch(iChannel0, texel, 0).r); + + vec3 live = vec3(0); + + if (x == 1 && c == 2 || c == 3) + live = vec3(1); + + fragColor = vec4(live, 1.0); +} diff --git a/John-Conway's-Game-of-Life:2D/Common.glsl b/John-Conway's-Game-of-Life:2D/Common.glsl new file mode 100644 index 0000000..5ce8872 --- /dev/null +++ b/John-Conway's-Game-of-Life:2D/Common.glsl @@ -0,0 +1,15 @@ +const float GRID_PIXEL = 75.0; +// how many frames to ignore when computing next iteration of game of life. +// Frame-Rate = fps/FRAME_INTERLACE +const int FRAME_INTERLACE = 10; + +// pixelize +float pixelize(in float x) +{ + return floor(x * GRID_PIXEL)/GRID_PIXEL; +} + +vec2 pixelize(in vec2 x) +{ + return floor(x * GRID_PIXEL)/GRID_PIXEL; +} diff --git a/John-Conway's-Game-of-Life:2D/Image.glsl b/John-Conway's-Game-of-Life:2D/Image.glsl new file mode 100644 index 0000000..4fe09a8 --- /dev/null +++ b/John-Conway's-Game-of-Life:2D/Image.glsl @@ -0,0 +1,31 @@ + +const float GRID_THICKNESS = 0.1; + +vec3 blurBufA(in vec2 uv, in float r, in float t) +{ + vec3 s = vec3(0); + for (float x = -r; x < r+1.; x+=t) + for (float y = -r; y < r+1.; y+=t) + { + vec2 st = uv + vec2(x,y)/iResolution.xy; + vec3 p = texture(iChannel0, st).rgb; + float f = (1.0 - abs(x/r)) * (1.0 - abs(y/r)); + + s += p * f; + } + return s / (r*r) * t * t; +} + + +void mainImage(out vec4 fragColor, in vec2 fragCoord) +{ + vec2 uv = fragCoord/iResolution.xy; + + vec3 col = texture(iChannel0, uv).rgb; + vec3 gol = texture(iChannel0, uv).rgb; + + vec2 kk = step(vec2(GRID_THICKNESS), fract(fragCoord/iResolution.xx * GRID_PIXEL)); + float g = 1.0 - kk.x * kk.y; + + fragColor = vec4(mix(blurBufA(uv, 16., 2.) + gol, vec3(1.0/3.0), g * 0.5), 1.0); +} diff --git a/John-Conway's-Game-of-Life:2D/README.md b/John-Conway's-Game-of-Life:2D/README.md new file mode 100644 index 0000000..ce95a84 --- /dev/null +++ b/John-Conway's-Game-of-Life:2D/README.md @@ -0,0 +1,4 @@ +# John Conway's Game of Life: 2D +Multipass GPU implementation of the famous Game of Life. + +![overview.png](https://git.teridax.de/teridax/Shadertoy-Shaders/raw/branch/main/John-Conway's-Game-of-Life:2D/overview.png) diff --git a/John-Conway's-Game-of-Life:2D/overview.png b/John-Conway's-Game-of-Life:2D/overview.png new file mode 100644 index 0000000..ef04581 Binary files /dev/null and b/John-Conway's-Game-of-Life:2D/overview.png differ