~npisanti/scrapbook

7b40fef146a907217603501cc7cadde3ebdcadfd — Nicola Pisanti 2 months ago eb7d977
cellular automata uses alpha channel
1 files changed, 10 insertions(+), 18 deletions(-)

M feedback/cellular.frag
M feedback/cellular.frag => feedback/cellular.frag +10 -18
@@ 44,36 44,28 @@ const mat4 custom = mat4(

#define RULES game_of_life

// red channel 
#define CH 0

void main (void) {
	
    vec2 st = gl_FragCoord.xy/u_resolution;
	float sx = st.x / u_resolution.x;
	float sy = st.y / u_resolution.y;

	float n0 = texture2D(u_tex0, vec2( st.x+sx, st.y-sy))[CH];
	float n1 = texture2D(u_tex0, vec2( st.x+sx, st.y))[CH];
	float n2 = texture2D(u_tex0, vec2( st.x+sx, st.y+sy))[CH];
	float n3 = texture2D(u_tex0, vec2( st.x-sx, st.y-sy))[CH];
	float n4 = texture2D(u_tex0, vec2( st.x-sx, st.y))[CH];
	float n5 = texture2D(u_tex0, vec2( st.x-sx, st.y+sy))[CH];
	float n6 = texture2D(u_tex0, vec2( st.x, st.y-sy))[CH];
	float n7 = texture2D(u_tex0, vec2( st.x, st.y+sy))[CH];
	float n0 = texture2D(u_tex0, vec2( st.x+sx, st.y-sy)).a;
	float n1 = texture2D(u_tex0, vec2( st.x+sx, st.y)).a;
	float n2 = texture2D(u_tex0, vec2( st.x+sx, st.y+sy)).a;
	float n3 = texture2D(u_tex0, vec2( st.x-sx, st.y-sy)).a;
	float n4 = texture2D(u_tex0, vec2( st.x-sx, st.y)).a;
	float n5 = texture2D(u_tex0, vec2( st.x-sx, st.y+sy)).a;
	float n6 = texture2D(u_tex0, vec2( st.x, st.y-sy)).a;
	float n7 = texture2D(u_tex0, vec2( st.x, st.y+sy)).a;

	float sum = min(n0+n1+n2+n3+n4+n5+n6+n7, 7.0); 

	float a = texture2D(u_tex0, st)[CH];
	float a = texture2D(u_tex0, st).a;

	sum += 8.0 * step( 0.5, a );

	int ix = int(mod(sum,4.0));
	int iy = int(sum/4.0);

	float alive = RULES [iy][ix];

    vec3 color = vec3(alive);

    gl_FragColor = vec4( color, 1.0 );
    gl_FragColor = vec4( vec3(1.0 ), RULES [iy][ix] );
}