Hello
I am having some weird issues with my SSAO-shader, as you can see on the following pictures, showing an asteroid in a space-environment:
[ATTACH=CONFIG]1473[/ATTACH][ATTACH=CONFIG]1474[/ATTACH][ATTACH=CONFIG]1475[/ATTACH]
2nd picture is SSAO only. 3rd picture shows the depthbuffer. It seems like it highlights every edge of the asteroid, which looks totally wrong. On meshes with sharper edges, like the cockpit, it doesn’t show this behavior. I have no idea where this comes from, since I can’t see any problems with the depthbuffer.
This is my SSAO-code:
float readDepth(vec2 uv)
{
float n = 1.0; // camera z near
float f = 100.0; // camera z far
float z = texture2D(uDepthTexture, uv).x;
return (2.0 * n) / (f + n - z * (f - n));
}
float compareDepths( in float depth1, in float depth2 ) {
float aoCap = 1.0;
float aoMultiplier=10000.0;
float depthTolerance=0.000;
float aorange = 10.0;// units in space the AO effect extends to (this gets divided by the camera far range
float diff = sqrt( clamp(1.0-(depth1-depth2) / (aorange/(camerarange.y-camerarange.x)),0.0,1.0) );
float ao = min(aoCap,max(0.0,depth1-depth2-depthTolerance) * aoMultiplier) * diff;
return ao;
}
vec3 addssao()
{
vec2 texCoord = vUv.xy;
vec4 fragcolor=vec4(0.0);
float depth = readDepth( texCoord );
float d;
float pw = 1.0 / (float)uResolution.x;
float ph = 1.0 / (float)uResolution.y;
float aoCap = 1.0;
float ao = 0.0;
float aoMultiplier=10000.0;
float depthTolerance = 0.001;
float aoscale=1.0;
d=readDepth( vec2(texCoord.x+pw,texCoord.y+ph));
ao+=compareDepths(depth,d)/aoscale;
d=readDepth( vec2(texCoord.x-pw,texCoord.y+ph));
ao+=compareDepths(depth,d)/aoscale;
d=readDepth( vec2(texCoord.x+pw,texCoord.y-ph));
ao+=compareDepths(depth,d)/aoscale;
d=readDepth( vec2(texCoord.x-pw,texCoord.y-ph));
ao+=compareDepths(depth,d)/aoscale;
pw*=2.0;
ph*=2.0;
aoMultiplier/=2.0;
aoscale*=1.2;
d=readDepth( vec2(texCoord.x+pw,texCoord.y+ph));
ao+=compareDepths(depth,d)/aoscale;
d=readDepth( vec2(texCoord.x-pw,texCoord.y+ph));
ao+=compareDepths(depth,d)/aoscale;
d=readDepth( vec2(texCoord.x+pw,texCoord.y-ph));
ao+=compareDepths(depth,d)/aoscale;
d=readDepth( vec2(texCoord.x-pw,texCoord.y-ph));
ao+=compareDepths(depth,d)/aoscale;
pw*=2.0;
ph*=2.0;
aoMultiplier/=2.0;
aoscale*=1.2;
d=readDepth( vec2(texCoord.x+pw,texCoord.y+ph));
ao+=compareDepths(depth,d)/aoscale;
d=readDepth( vec2(texCoord.x-pw,texCoord.y+ph));
ao+=compareDepths(depth,d)/aoscale;
d=readDepth( vec2(texCoord.x+pw,texCoord.y-ph));
ao+=compareDepths(depth,d)/aoscale;
d=readDepth( vec2(texCoord.x-pw,texCoord.y-ph));
ao+=compareDepths(depth,d)/aoscale;
pw*=2.0;
ph*=2.0;
aoMultiplier/=2.0;
aoscale*=1.2;
d=readDepth( vec2(texCoord.x+pw,texCoord.y+ph));
ao+=compareDepths(depth,d)/aoscale;
d=readDepth( vec2(texCoord.x-pw,texCoord.y+ph));
ao+=compareDepths(depth,d)/aoscale;
d=readDepth( vec2(texCoord.x+pw,texCoord.y-ph));
ao+=compareDepths(depth,d)/aoscale;
d=readDepth( vec2(texCoord.x-pw,texCoord.y-ph));
ao+=compareDepths(depth,d)/aoscale;
ao/=16.0;
fragcolor = vec4(1.0-ao);
fragcolor.a=1.0;
return fragcolor;
}