首页 > 解决方案 > 使用 OpenGL 实现更平滑的渐变过渡?

问题描述

我正在使用以下着色器来渲染天穹以模拟夜空。我的问题是颜色之间清晰可见的过渡。

是什么导致了这些苛刻的梯度过渡?

在此处输入图像描述

片段着色器:

#version 330
in vec3 worldPosition;
layout(location = 0) out vec4 outputColor;

void main()
{
    float height = 0.007*(abs(worldPosition.y)-200);    
    vec4 apexColor = vec4(0,0,0,1);
    vec4 centerColor = vec4(0.159, 0.132, 0.1, 1);

    outputColor = mix(centerColor, apexColor, height);
}

Fbo像素格式:

GL.TexImage2D(
    TextureTarget.Texture2D,
    0,
    PixelInternalFormat.Rgb32f,
    WindowWidth,
    WindowHeight,
    0,
    PixelFormat.Rgb,
    PixelType.Float,
    IntPtr.Zero )

标签: openglglslopentk

解决方案


正如 Ripi2 解释的那样,24 位颜色无法完美地表示渐变,可表示颜色之间的不连续性在单一颜色的渐变中变得刺眼可见。

为了隐藏色带,我实现了一种简单形式的有序抖动,并使用这种拜耳矩阵算法生成了 8x8 纹理。

在此处输入图像描述

vec4 dither = vec4(texture2D(MyTexture0, gl_FragCoord.xy / 8.0).r / 32.0 - (1.0 / 128.0));
colourOut += dither;

在此处输入图像描述


推荐阅读