采样纹理时在GLSL中使用floor()函数会留下毛刺 [英] Using floor() function in GLSL when sampling a texture leaves glitch

查看:639
本文介绍了采样纹理时在GLSL中使用floor()函数会留下毛刺的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我看到的问题的着色示例:

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;
}

我看到的错误是在网格正方形之间有时绘制了1-2个像素线有时.

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

请注意,我们不仅在网络上使用GLSL ES,而且在Unity中使用HLSL都已经看到了这个问题.

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

与我不理解的floor()和浮点运算有什么关系吗?我不仅想知道如何解决它,还想知道为什么为什么会发生?

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?

推荐答案

原来是从纹理中为这些网格线使用低细节mipmap.将纹理上的滤镜设置更改为最近"或线性"滤镜,而不是"mipmap"进行了修复.

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.

还可以使用第三个"bias"参数来调用texture2D,该参数将修改将使用的mipmap级别.

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

感谢sharttoy的评论者回答了这个问题!

Thanks to the commenter on shadertoy for answering the question!

(不确定如何为自定义着色器实际选择mipmap级别而不会产生偏差,如果有人知道的话,会很感兴趣!)

这篇关于采样纹理时在GLSL中使用floor()函数会留下毛刺的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆