【问题标题】:Losing texture definition in fragment shader在片段着色器中丢失纹理定义
【发布时间】:2019-03-01 05:09:16
【问题描述】:

我正在尝试使用片段着色器绘制参考网格,我发现纹理在缩小时会丢失其定义,正如您在此处看到的那样。有人知道为什么吗?放大后看起来不错。

四边形上的网格纹理

没有缩放的网格纹理

这里是代码:

#version 330 core
uniform int multiplicationFactor;
uniform lowp float threshold;
uniform vec4 gridColor;

in vec2 vUV;

void main() {
    // multiplicationFactor scales the number of stripes
    vec2 t = vUV * multiplicationFactor;

    // the threshold constant defines the with of the lines
    if (abs(t.x - round(t.x)) <= threshold  || abs(t.y - round(t.y)) < threshold )
        gl_FragColor = gridColor;    
    else
        discard;
}

【问题讨论】:

  • 看起来是精度问题。 't = uUV;'并与“(阈值/乘法因子)”进行比较有什么不同吗?
  • 我不太明白你的意思,但如果你担心阈值精度低,我相信这不是问题。我已经从高到低的精度进行了测试,发现纹理有难以察觉的变化!
  • 不完全是,但我认为 Rabbid76 有答案。

标签: c++ opengl graphics glsl fragment-shader


【解决方案1】:

您必须根据视口的大小计算到线的距离。

添加vec2 resolution 类型的统一变量,其中包含视口的宽度和高度,变量thresholdhast 包含以像素为单位的线条粗细(例如1.0):

#version 330 core

uniform int        multiplicationFactor;
uniform lowp float threshold;   // line width in pixel
uniform vec4       gridColor;
uniform vec2       resolution;  // width and height of the viewport

in vec2 vUV;

void main()
{
    float m   = float(multiplicationFactor);
    vec2 t    = vUV * m;
    vec2 dist = abs(t - round(t)) / m; 
    vec2 th   = threshold / resolution;

    if (dist.x > th.x  && dist.y > th.y)
        discard;

    gl_FragColor = gridColor;    
}

【讨论】:

  • 这与我对原始代码的预期不太一样(实线变为虚线)。当我换行时: vec2 dist = abs(t - round(t)) / m;到 vec2 dist = abs(t - round(t)) / 分辨率;它再次变得坚固!质地的质量肯定会提高。我将把它记下来作为 OpenGL 图形管道的缩放距离限制。我不能投票给你的答案,因为这是我在这里的第一天,但​​无论如何感谢 +1
猜你喜欢
  • 2013-09-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多