fragmentShader = `
uniform sampler2D u_texture;
uniform float dx;
uniform float dy;
uniform float iTime;
varying vec2 v_uv;
int lod = 5;
float thresh = .5;
// #define S(v) smoothstep( -.8,.8,(v)/fwidth(v) ) // antialiasing draw
float map(vec2 uv) {
// ivec2 s = textureSize(u_texture, 0);
return texelFetch(u_texture, ivec2(uv), lod).r;
}
float map1(vec2 uv) {
return texture(u_texture, uv).r;
}
float S(float v) {
return smoothstep( -.8,.8,(v)/fwidth(v) );
}
void main() {
vec2 uv = v_uv;
vec2 v;
vec4 c = vec4( map1(uv) , map1(uv+vec2(dx,0)), // value at corners
map1(uv+vec2(0,dy)), map1(uv+vec2(dx,dy)) );
vec4 b = step(thresh,c); // is > threshold ?
int n = int(b.x+b.y+b.z+b.w); // number of yes
// U = fract(U); // tile local coordinates
if (n==0) gl_FragColor += .3; // empty ( for test )
else if (n==4) gl_FragColor += 1.; // full
else if (n==1 || n==3 || n==2 && b.x==b.w ) { // corner + checker case
if (n==3) b = 1.-b;
if(b.x>0.) gl_FragColor += S( (1.-uv.x-uv.y)*c.x + uv.x *c.y + uv.y *c.z - thresh );
if(b.y>0.) gl_FragColor += S( ( uv.x -uv.y)*c.y + (1.-uv.x)*c.x + uv.y *c.w - thresh );
if(b.z>0.) gl_FragColor += S( (-uv.x+ uv.y )*c.z + uv.x *c.w + (1.-uv.y)*c.x - thresh );
if(b.w>0.) gl_FragColor += S( (uv.x+uv.y-1.)*c.w + (1.-uv.x)*c.z + (1.-uv.y)*c.y - thresh );
} else { // n==2 // (checker treated above)
if (b.x==b.y) v = (thresh-c.xy)/(c.zw-c.xy), // horizontal
gl_FragColor += S( uv.y - mix(v.x,v.y,uv.x) );
else if (b.x==b.z) v = (thresh-c.xz)/(c.yw-c.xz), // vertical
gl_FragColor += S( uv.x - mix(v.x,v.y,uv.y) );
if (b.x>0.) gl_FragColor = 1.-gl_FragColor;
}
// gl_FragColor = c;
// gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
}
`