0

试图在我的 OpenGL 应用程序中实现 SSAO。但想出了一些奇怪的神器。我怎么能解决这个黑色的东西?这是 SSAO 的片段着色器

"int KERNEL_MAX = 128;"
    "uniform vec3 kernel[128];"

    "uniform mat4 P;"
    "uniform sampler2D tex;"
    "uniform sampler2D texPos;"
    "uniform sampler2D texNormal;"
    "uniform sampler2D texDepth;"
    "uniform vec3 camPos;"

    "void main () {"

    "   vec4 pixel_color = texture2D(tex, v_uv);"
    "   vec3 pixel_pos = texture2D(texPos, v_uv).rgb;"
    "   vec3 pixel_normal = texture2D(texNormal, v_uv).rgb;"
    "   float pixel_depth = texture2D(texDepth, v_uv).r;"

    "   float occlusion = 0.0;"
    "   for (int i = 0; i < KERNEL_MAX; ++i) {"
    "       vec3 sample_ray = pixel_pos + kernel[i] * sign(dot(pixel_normal, kernel[i])) * 0.1;"
    "       vec4 sample_pos = (P * vec4(sample_ray, 1.0));"
    "       sample_pos.xy /= sample_pos.w;"
    "       sample_pos.xy = sample_pos.xy * 0.5 - 0.5;"

    "       float sample_depth = texture2D(texDepth, sample_pos.xy).r;"
    "       if (sample_depth < pixel_depth) { occlusion += 1.0; }"
    "   }"

在此处输入图像描述

4

1 回答 1

0

In general a bias is used, in Screen Space Ambient Occlusion algorithms:

const float bias = 0.0001;
if (sample_depth < pixel_depth - bias) { occlusion += 1.0; }
于 2020-05-26T16:17:57.133 回答