I have a texture intended as a mask. The texture type is GL_R32F
(32 bit float) and the data inside is a signed distance field (= value of each texel is distance to nearest mask edge). For test purposes, the mask is just a circle part and is directly drawn in greyscale. The fragment shader handling this texture has following code:
void main() {
float mask = texture( tex, fragTexCoord).r;
vec4 res = vec4(1.0,1.0,1.0, 1.0);
if(mask > 0.0) //test failed, outside of mask
res = vec4(0.0,0.0,0.0, 0.0);
//smoothing if there is an edge nearby
//0 or 1 otherwise
res.w = smoothstep(0.0, 1.0, -1.0 * mask);
finalColor = res;
}
however if the mask is drawn too big, its edges appear blurry, because smoothstep afflicts too many fragments. This could be solved by multiplying the mask by some value, however it would stop smoothing if is the mask was drawn too small.
For illustration, on left is the mask(100px), in the middle is its zoomed edge and on right there is zoomed edge of mask if drawn big(600px). How to smooth the edge to appear correct regardless of the size? (=zoomed edges should always look the same, like the middle image)