xxxxxxxxxx
49
precision mediump float;
// Pixel position
varying vec2 pos;
// Uniforms set by filterShader
uniform sampler2D filter_background; // contains the image being filtered
uniform vec2 filter_res; // contains the image resolution in pixels
float is_empty(vec3 col) {
// If cell is black, the sum of R, G, B is 0
// Therefore 1-sum = 1, and step returns 1
// If any channel has some colour, 1-sum < 1 and step returns 0
return step(1., 1. - (col.r + col.g + col.b));
}
float edge_of_screen(vec2 pos) {
float top = step(0., 1. - pos.y);
float bottom = step(1., pos.y);
float left = step(0., 1. - pos.x);
float right = step(1., pos.x);
return step(1., top + bottom + left + right);
}
void main() {
// Figure out how far a pixel is
// Zero stored in z for swizzle convenience
vec3 normalRes = vec3(1./filter_res, 0.);
// Get colour of current pixel
vec3 curr = texture2D(filter_background, pos).rgb;
vec3 up = texture2D(filter_background, pos - normalRes.zy).rgb;
float curr_empty = is_empty(curr);
float up_empty = is_empty(up);
vec3 col = (up * curr_empty * (1. - up_empty)) +
curr * up_empty;
// Output the cell (white = alive, black = dead)
gl_FragColor = vec4(col, 1.);
}