I'm making a voxel game in LWJGL and figured out how to implement an SSAO shader but it's not looking how I expected it too. Looking at blocks from far away is fine, I can see a gradient-shadow outline of the block. But up close, that gradient-shadow goes away just leaving me with the color of the block(plus another simple shader I made to give each block side a different shade).
Here's what the game looks like from different distances: https://github.com/ninthworld/GreedyMeshing/raw/master/screenshot1.png https://github.com/ninthworld/GreedyMeshing/raw/master/screenshot2.png
Here's what I expected it to look like: https://github.com/ninthworld/GreedyMeshing/raw/master/screenshot3.png
And this is what my SSAO.frag shader code looks like:
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);
}
The two samplers are each receiving a texture I created from the GL_RGBA and GL_DEPTH_COMPONENT and then gl_FragColor is drawing to a 2D plane that takes up the screen.
If looking at more of the code would help, here's the github page https://github.com/ninthworld/GreedyMeshing