xxxxxxxxxx
99
precision mediump float;
// I'M MISSING the modulo operator, but otherwise that's basically it.
// lets grab texcoords just for fun
varying vec2 vTexCoord;
uniform float iResolutionX;
uniform float iResolutionY;
const float eps = 0.01;
const int N = 10;
// our texture coming from p5
uniform sampler2D tex0;
void main() {
vec2 uv = vTexCoord;
uv = 1.0 - uv;
vec2 pw = vec2(1. / iResolutionX, 1. / iResolutionY);
vec4 tex = texture2D(tex0, uv);
vec2 v3 = vec2(3, 0) * pw.y;
vec2 v2 = vec2(2, 0) * pw.y;
vec2 v1 = vec2(1, 0) * pw.y;
vec2 h3 = vec2(0, 3) * pw.x;
vec2 h2 = vec2(0, 3) * pw.x;
vec2 h1 = vec2(0, 3) * pw.x;
vec4 C[16];
// UP
C[0] = texture2D(tex0, uv - h1 + v3);
C[1] = texture2D(tex0, uv + v3);
C[2] = texture2D(tex0, uv + h1 + v3);
// Top Right Corner
C[3] = texture2D(tex0, uv + v2 + h2);
// Right
C[4] = texture2D(tex0, uv + v1 + h3);
C[5] = texture2D(tex0, uv + h3);
C[6] = texture2D(tex0, uv - v1 + h3);
// Bottom Right Corner
C[7] = texture2D(tex0, uv - v2 + h2);
// Bottom
C[8] = texture2D(tex0, uv - h1 - v3);
C[9] = texture2D(tex0, uv - v3);
C[10] = texture2D(tex0, uv + h1 - v3);
// Bottom Left Corner
C[11] = texture2D(tex0, uv - v2 - h2);
// Left
C[12] = texture2D(tex0, uv + v1 - h3);
C[13] = texture2D(tex0, uv - h3);
C[14] = texture2D(tex0, uv - v1 - h3);
// Top Left Corner
C[15] = texture2D(tex0, uv + v2 - h2);
float C_grey[16];
for(int i = 0; i < 16; ++i) {
C_grey[i] = (C[i].r + C[i].g + C[i].b) / 3.;
}
float center_grey = (tex.r + tex.g + tex.b) / 3.;
float diff;
for(int i = 0; i < 16; ++i) {
int curr = 0;
if(C_grey[i] > center_grey + eps) {
diff = 0.;
for(int j = 1; j < 16; ++j) {
int k = i + j;
if(k > 15) break;
if(C_grey[i + j] < center_grey + eps) break;
diff += C_grey[i + j] - center_grey;
curr += 1;
}
} else if(C_grey[i] < center_grey - eps) {
diff = 0.;
for(int j = 1; j < 16; ++j) {
int k = i + j;
if(k > 15) break;
if(C_grey[i + j] > center_grey - eps) break;
diff += center_grey - C_grey[i + j];
curr += 1;
}
}
if(curr > N) {
gl_FragColor = vec4(0.0, diff / float(N), 0.0, 1.0);
return;
}
}
gl_FragColor = vec4(0., 0., 0., 1.0);;
}