Im trying to develop a SSAO shader. it must operate in world space(because the rest of the pipeline is in worldspace aswell)
is seems like the range check is broken and there are on the left side of the image the wall grey but should be white.
following is the code for the ssao:
#version 130
in vec2 tex_coord;
uniform sampler2D gAlbedo; // not neeed
uniform sampler2D gPos; // in worldspace
uniform sampler2D gNormal; // in worldspace
uniform vec3 samples[16]; // rotation samples in halfsphere
uniform vec3 noise[16]; // noise
uniform vec3 cameraPos; // camera position in worldspace
uniform mat4 cameraMat; // camera matrix
out vec4 ssao;
void main(){
vec3 FragPos = texture(gPos, tex_coord).rgb;
vec3 Normal = normalize(texture(gNormal, tex_coord).rgb);
vec2 dim = vec2(960,540); // current display size FIXME
vec2 tpos = tex_coord * dim;
vec2 mods = vec2(mod(tpos.x,4),mod(tpos.y,4));
int noisepos = int(mods.x + 4 * mods.y);
vec3 viewDir = FragPos - cameraPos;
vec3 randomVec = normalize(cross(viewDir,noise[noisepos]));
float radius = 1.0f;
vec3 tangent = normalize(randomVec - Normal * dot(randomVec, Normal));
vec3 bitangent = cross(Normal, tangent);
mat3 TBN = mat3(tangent, bitangent, Normal);
float fragDepthlen = dot(FragPos - cameraPos,FragPos - cameraPos);
float occlusion = 0.0;
for (int i = 0; i < 16; ++i) {
vec3 sample = TBN * samples[i];
sample = sample * 0.2 + FragPos;
float fragDepth = dot(sample - cameraPos,sample - cameraPos);
vec4 offset = vec4(sample, 1.0);
offset = cameraMat * offset;
offset.xy /= offset.w;
offset.xy = offset.xy * 0.5 + 0.5;
vec3 TargetFragPos = texture(gPos, offset.xy).rgb;
float targetfragDepth = dot(TargetFragPos - cameraPos,TargetFragPos - cameraPos);
float rangeCheck = smoothstep(0.0, 1.0, 0.2 / abs(sqrt(fragDepthlen) - sqrt(targetfragDepth)));
occlusion += (targetfragDepth <= fragDepth - 0.025? 1.0 : 0.0);
}
occlusion = 1 - (occlusion / 16.0);
ssao = vec4(vec3(occlusion),1.0f);
}
Thanks for the help in advance