Pure Depth SSAO闪烁

时间:2017-09-28 10:55:38

标签: opengl-es pixel-shader glsles ssao

我尝试使用this tutorial将Pure Depth SSAO实现到OpenGL ES 2.0引擎中。 现在我经历了闪烁,看起来我从某个地方读取,我没有数据。 你能看出我犯了什么错误,或者你知道如何解决闪烁的问题吗?我需要这个在移动和html5上运行前向渲染,这就是为什么我使用SSAO的深度版本。 非常感谢

视频:Youtube

GLSL代码:

uniform sampler2D texture0;
uniform sampler2D texture1;

varying vec2 uvVarying;

vec3 GetNormalFromDepth(float depth, vec2 uv);

uniform mediump vec2 agk_resolution;
uniform float ssaoStrength;
uniform float ssaoBase;
uniform float ssaoArea;
uniform float ssaoFalloff;
uniform float ssaoRadius;

const int samples = 16;
vec3 sampleSphere[samples];

void main()
{
    highp float depth = texture2D(texture0, uvVarying).r;
    vec3 random = normalize( texture2D(texture1, uvVarying * agk_resolution / 64.0).rgb );

    vec3 position = vec3(uvVarying, depth);
    vec3 normal = GetNormalFromDepth(depth, uvVarying);

    sampleSphere[0] = vec3( 0.5381, 0.1856,-0.4319);
    sampleSphere[1] = vec3( 0.1379, 0.2486, 0.4430);
    sampleSphere[2] = vec3( 0.3371, 0.5679,-0.0057);
    sampleSphere[3] = vec3(-0.6999,-0.0451,-0.0019);
    sampleSphere[3] = vec3( 0.0689,-0.1598,-0.8547);
    sampleSphere[5] = vec3( 0.0560, 0.0069,-0.1843);
    sampleSphere[6] = vec3(-0.0146, 0.1402, 0.0762);
    sampleSphere[7] = vec3( 0.0100,-0.1924,-0.0344);
    sampleSphere[8] = vec3(-0.3577,-0.5301,-0.4358);
    sampleSphere[9] = vec3(-0.3169, 0.1063, 0.0158);
    sampleSphere[10] = vec3( 0.0103,-0.5869, 0.0046);
    sampleSphere[11] = vec3(-0.0897,-0.4940, 0.3287);
    sampleSphere[12] = vec3( 0.7119,-0.0154,-0.0918);
    sampleSphere[13] = vec3(-0.0533, 0.0596,-0.5411);
    sampleSphere[14] = vec3( 0.0352,-0.0631, 0.5460);
    sampleSphere[15] = vec3(-0.4776, 0.2847,-0.0271);

    float radiusDepth = ssaoRadius/depth;
    float occlusion = 0.0;
    for(int i=0; i < samples; i++)
    {
        vec3 ray = radiusDepth * reflect(sampleSphere[i], random);
        vec3 hemiRay = position + sign(dot(ray, normal)) * ray;

        float occDepth = texture2D(texture0, clamp(hemiRay.xy, 0.0, 1.0)).r;
        float difference = depth - occDepth;

        occlusion += step(ssaoFalloff, difference) * (1.0 - smoothstep(ssaoFalloff, ssaoArea, difference));

        // float rangeCheck = abs(difference) < radiusDepth ? 1.0 : 0.0;
        // occlusion += (occDepth <= position.z ? 1.0 : 0.0) * rangeCheck;
    }

    float ao = 1.0 - ssaoStrength * occlusion * (1.0 / float(samples));

    gl_FragColor = vec4(clamp(ao + ssaoBase, 0.0, 1.0));
}

vec3 GetNormalFromDepth(float depth, vec2 uv)
{
    vec2 offset1 = vec2(0.0,1.0/agk_resolution.y);
    vec2 offset2 = vec2(1.0/agk_resolution.x,0.0);

    float depth1 = texture2D(texture0, uv + offset1).r;
    float depth2 = texture2D(texture0, uv + offset2).r;

    vec3 p1 = vec3(offset1, depth1 - depth);
    vec3 p2 = vec3(offset2, depth2 - depth);

    vec3 normal = cross(p1, p2);
    normal.z = -normal.z;

    return normalize(normal);
}

1 个答案:

答案 0 :(得分:0)

我仔细检查了我的代码和你(Rabbid76)为JSFiddle创建的代码,并遇到了解决问题的if(深度&gt; 0.0)语句......所以你以某种方式回答了我的问题,我想感谢你并标记你