Using floor() function in GLSL when sampling a texture leaves glitch

删除回忆录丶 提交于 2020-01-04 04:04:07

问题


Here's a shadertoy example of the issue I'm seeing:

https://www.shadertoy.com/view/4dVGzW

I'm sampling a texture by sampling from floor-ed texture coordinates:

#define GRID_SIZE 20.0

void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
    vec2 uv = fragCoord.xy / iResolution.xy;

    // Sample texture at integral positions
    vec2 texUV = floor(uv * GRID_SIZE) / GRID_SIZE;

    fragColor = texture2D(iChannel0, texUV).rgba;
}

The bug I'm seeing is that there are 1-2 pixel lines sometimes drawn between the grid squares.

Note that we've seen this issue not only using GLSL ES on the web, but also HLSL in Unity.

Is there something to do with floor() and floating point arithmetic that I don't understand?! I'd love to know not just how to fix it, but why exactly it's happening?


回答1:


Turned out to be using a low detail mipmap from the texture for those grid lines. Changing the filter settings on the texture to "nearest" or "linear" filtering rather than "mipmap" fixed it.

It is also possible to call texture2D with a 3rd "bias" parameter that modifies which mipmap level it will use.

Thanks to the commenter on shadertoy for answering the question!

(Not sure how the mipmap level actually gets chosen for custom shaders without the bias, would be interested if someone knows!)



来源:https://stackoverflow.com/questions/34951491/using-floor-function-in-glsl-when-sampling-a-texture-leaves-glitch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!