50
submitted 1 year ago* (last edited 1 year ago) by sixfold@lemmy.sdf.org to c/programming@programming.dev

I've been playing with shadertoy a bit, and here is a lil demo. It's probably not the best way to do it, code suggestions welcome.

https://www.shadertoy.com/view/dtlBRM

Here's most of the code for reference:

void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
    // fetch neighbor values from last frame, loop back onto screen if off screen
    mat3 n;
    n[0][0] = texelFetch(iChannel0, ivec2(mod(vec2(fragCoord) + vec2(-1., -1.), iResolution.xy)), 0).g;
    n[1][0] = texelFetch(iChannel0, ivec2(mod(vec2(fragCoord) + vec2(0., -1.), iResolution.xy)), 0).g;
    n[2][0] = texelFetch(iChannel0, ivec2(mod(vec2(fragCoord) + vec2(1., -1.), iResolution.xy)), 0).g;
    n[0][1] = texelFetch(iChannel0, ivec2(mod(vec2(fragCoord) + vec2(-1., 0.), iResolution.xy)), 0).g;
    n[1][1] = texelFetch(iChannel0, ivec2(mod(vec2(fragCoord) + vec2(0., 0.), iResolution.xy)), 0).g;
    n[2][1] = texelFetch(iChannel0, ivec2(mod(vec2(fragCoord) + vec2(1., 0.), iResolution.xy)), 0).g;
    n[0][2] = texelFetch(iChannel0, ivec2(mod(vec2(fragCoord) + vec2(-1., 1.), iResolution.xy)), 0).g;
    n[1][2] = texelFetch(iChannel0, ivec2(mod(vec2(fragCoord) + vec2(0., 1.), iResolution.xy)), 0).g;
    n[2][2] = texelFetch(iChannel0, ivec2(mod(vec2(fragCoord) + vec2(1., 1.), iResolution.xy)), 0).g;
    
    // sum of neighbors
    float sum = n[0][0] + n[1][0] + n[2][0] +
                n[0][1] +           n[2][1] +
                n[0][2] + n[1][2] + n[2][2];
                
    if(n[1][1] == 0.) {
        if(sum == 3.) {
            // if dead and has 3 neighbors, come alive
            fragColor = vec4(0., 1., 0., 1.);
        } else {
            // otherwise stay dead
            fragColor = vec4(0., 0., 0., 1.);
        }
    } else {
        if(sum == 2. || sum == 3.) {
            // if alive and has 2 or 3 neighbors, stay alive
            fragColor = vec4(0., 1., 0., 1.);
        } else {
            // otherwise, die
            fragColor = vec4(0., 0., 0., 1.);
        }
    }
}
all 9 comments
sorted by: hot top controversial new old
[-] BangersAndMash@lemmy.world 5 points 1 year ago

Oh that's pretty rad. I can imagine it being used in a movie as a face for some AI that's dying or suffering

[-] CookieOfFortune@lemmy.world 4 points 1 year ago

Interesting. I wonder if there are videos that increase the chance of creating infinite automata?

[-] lascapi@jlai.lu 4 points 1 year ago

Ooo!! 🤩 I love the concept!

Did you try with a more complex input video?

[-] sixfold@lemmy.sdf.org 1 points 1 year ago
[-] lascapi@jlai.lu 1 points 1 year ago

Yeah, with some objects, in a room, outside in the nature. Just to see how it's working with more than one subject in the middle. 🙂

[-] livingcoder@programming.dev 1 points 1 year ago

That's awesome! What are your thoughts on keeping the colors of your face as they naturally are and as the GoL algorithm acts on the pixels, you blend the colors together?

[-] sixfold@lemmy.sdf.org 2 points 1 year ago* (last edited 1 year ago)

I was thinking of doing three separate GOL simulations, one on each RGB channel, and letting the colors mix that way into like 6 colors. right now, I clamp the pixel brightness values to 0 or 1, so that's why it's black/white, or rather black/green.

[-] livingcoder@programming.dev 1 points 1 year ago

That would be cool, seeing RGB GOL cells moving outward. Maybe instead of 0 or 1 brightness you could just round it for the cell's purposes, maybe having a const float for flexibility: if it's greater than threshold then it's "on".

this post was submitted on 23 Aug 2023
50 points (98.1% liked)

Programming

17314 readers
71 users here now

Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!

Cross posting is strongly encouraged in the instance. If you feel your post or another person's post makes sense in another community cross post into it.

Hope you enjoy the instance!

Rules

Rules

  • Follow the programming.dev instance rules
  • Keep content related to programming in some way
  • If you're posting long videos try to add in some form of tldr for those who don't want to watch videos

Wormhole

Follow the wormhole through a path of communities !webdev@programming.dev



founded 1 year ago
MODERATORS