How to get rid of shader artifact?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Mietzsche81

Hey gang! I’m using a greyscale mask on a shader to set the alpha value for a 2D canvas item. I have checked the mask PNG to make sure that all the colors are pixel perfect, and there are no anti-aliasing artifacts or anything there. However, when I go to render, godot creates artifacts that seem to result from interpolation from texture sampling. How do I fix this?

Here is the effect I’m seeing

EDIT: klaas has confirmed my suspicions about interpolation. For anyone ecnountering the same issue, here is a sample method to use texelFetch() from UV in lieu of texture:

float value = texture(mask, UV, 1).r;


int texelX = int(UV.x * float(width) + float(offset_x)  );
int texelY = int(UV.y * float(height) + float(offset_y) );
ivec2 texel = ivec2( texelX, texelY );
float value = texelFetch( mask, texel, 0).r;


:bust_in_silhouette: Reply From: klaas

those are sampling artifacts from the GPU itself.

Try to use texelFetch() instead of texture() this will give you destinct pixels of the texture. Texture() will allways interpolate due to subpixel sampling.

Read this for some more insight: How do OpenGL's texelFetch and texture differ? - Game Development Stack Exchange

This is exactly the answer I needed. Thank you.

Mietzsche81 | 2020-08-23 18:23