1

我正在用 LWJGL 制作体素游戏,并想出了如何实现 SSAO 着色器,但它看起来也不是我所期望的。从远处看块很好,我可以看到块的渐变阴影轮廓。但近距离看,渐变阴影消失了,只剩下块的颜色(加上另一个简单的着色器,我为每个块的侧面赋予不同的阴影)。

这是从不同距离看游戏的样子: https://github.com/ninthworld/GreedyMeshing/raw/master/screenshot1.png https://github.com/ninthworld/GreedyMeshing/raw/master/screenshot2.png

这是我期望的样子: https ://github.com/ninthworld/GreedyMeshing/raw/master/screenshot3.png

这就是我的 SSAO.frag 着色器代码的样子:

uniform sampler2D texture0;
uniform sampler2D texture1;

uniform vec2 camerarange;
uniform vec2 screensize;

float readDepth( in vec2 coord ) {
    return (2.0 * camerarange.x) / (camerarange.y + camerarange.x - texture2D( texture0, coord ).x * (camerarange.y - camerarange.x));  
}


void main(void)
{   
    vec2 texCoord = gl_TexCoord[0].st;
    //vec2 texCoord = texture2D(texture0, gl_TexCoord[0].st).xy;
    //vec3 texColor = texture2D(texture1, gl_TexCoord[0].st).rgb;

    float depth = readDepth( texCoord );
    float d;

    float pw = 1.0 / screensize.x;
    float ph = 1.0 / screensize.y;

    float aoCap = 0.45;

    float ao = 0.0;

    float aoMultiplier=1000.0;

    float depthTolerance = 0.008;

    d=readDepth( vec2(texCoord.x+pw,texCoord.y+ph));
    ao+=min(aoCap,max(0.0,depth-d-depthTolerance) * aoMultiplier);

    d=readDepth( vec2(texCoord.x-pw,texCoord.y+ph));
    ao+=min(aoCap,max(0.0,depth-d-depthTolerance) * aoMultiplier);

    d=readDepth( vec2(texCoord.x+pw,texCoord.y-ph));
    ao+=min(aoCap,max(0.0,depth-d-depthTolerance) * aoMultiplier);

    d=readDepth( vec2(texCoord.x-pw,texCoord.y-ph));
    ao+=min(aoCap,max(0.0,depth-d-depthTolerance) * aoMultiplier);

    pw*=2.0;
    ph*=2.0;
    aoMultiplier/=2.0;

    d=readDepth( vec2(texCoord.x+pw,texCoord.y+ph));
    ao+=min(aoCap,max(0.0,depth-d-depthTolerance) * aoMultiplier);

    d=readDepth( vec2(texCoord.x-pw,texCoord.y+ph));
    ao+=min(aoCap,max(0.0,depth-d-depthTolerance) * aoMultiplier);

    d=readDepth( vec2(texCoord.x+pw,texCoord.y-ph));
    ao+=min(aoCap,max(0.0,depth-d-depthTolerance) * aoMultiplier);

    d=readDepth( vec2(texCoord.x-pw,texCoord.y-ph));
    ao+=min(aoCap,max(0.0,depth-d-depthTolerance) * aoMultiplier);

    pw*=2.0;
    ph*=2.0;
    aoMultiplier/=2.0;

    d=readDepth( vec2(texCoord.x+pw,texCoord.y+ph));
    ao+=min(aoCap,max(0.0,depth-d-depthTolerance) * aoMultiplier);

    d=readDepth( vec2(texCoord.x-pw,texCoord.y+ph));
    ao+=min(aoCap,max(0.0,depth-d-depthTolerance) * aoMultiplier);

    d=readDepth( vec2(texCoord.x+pw,texCoord.y-ph));
    ao+=min(aoCap,max(0.0,depth-d-depthTolerance) * aoMultiplier);

    d=readDepth( vec2(texCoord.x-pw,texCoord.y-ph));
    ao+=min(aoCap,max(0.0,depth-d-depthTolerance) * aoMultiplier);

    pw*=2.0;
    ph*=2.0;
    aoMultiplier/=2.0;

    d=readDepth( vec2(texCoord.x+pw,texCoord.y+ph));
    ao+=min(aoCap,max(0.0,depth-d-depthTolerance) * aoMultiplier);

    d=readDepth( vec2(texCoord.x-pw,texCoord.y+ph));
    ao+=min(aoCap,max(0.0,depth-d-depthTolerance) * aoMultiplier);

    d=readDepth( vec2(texCoord.x+pw,texCoord.y-ph));
    ao+=min(aoCap,max(0.0,depth-d-depthTolerance) * aoMultiplier);

    d=readDepth( vec2(texCoord.x-pw,texCoord.y-ph));
    ao+=min(aoCap,max(0.0,depth-d-depthTolerance) * aoMultiplier);

    ao/=16.0;

    gl_FragColor = vec4(1.0-ao) * texture2D(texture1, texCoord);
}

两个采样器分别接收我从 GL_RGBA 和 GL_DEPTH_COMPONENT 创建的纹理,然后 gl_FragColor 绘制到占据屏幕的 2D 平面。

如果查看更多代码会有所帮助,这里是 github 页面https://github.com/ninthworld/GreedyMeshing

4

0 回答 0