X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=blobdiff_plain;f=shaderlib%2Fambientocclusion_combine.glsl;h=f33520f8aa05ae76bd25a0499cf86ae319b4fa26;hp=572ba2f14bcf73858c3b500d992ffebafa14d3db;hb=1b9640375606a305f19c76cf15406202322b5bdf;hpb=63317e423282c1d8a7965743b20a9059a8086e67 diff --git a/shaderlib/ambientocclusion_combine.glsl b/shaderlib/ambientocclusion_combine.glsl index 572ba2f1..f33520f8 100644 --- a/shaderlib/ambientocclusion_combine.glsl +++ b/shaderlib/ambientocclusion_combine.glsl @@ -4,20 +4,24 @@ import ambientocclusion; ////// fragment void main() { - float sample = depth_ratio.x/(texture(depth, texcoord).r-depth_ratio.y); - float sum = 1.0; - float count = 1.0; - for(int i=0; i<=3; ++i) - for(int j=0; j<=3; ++j) + vec3 center = unproject(vec3(vertex.xy, texture(depth, texcoord).r)); + float min_depth = project(vec3(center.xy, center.z+occlusion_radius*0.1)).z; + float max_depth = project(vec3(center.xy, center.z-occlusion_radius*0.1)).z; + vec2 tex_scale = 1.0/vec2(textureSize(occlusion, 0)); + float sum = 0.0; + float count = 0.0; + for(int i=0; i<4; ++i) + for(int j=0; j<4; ++j) { - vec2 offs = vec2(float(i)-1.5, float(j)-1.5)/screen_size; - float dxy = length(offs)*-sample; - float dz = depth_ratio.x/(texture(depth, texcoord+offs).r-depth_ratio.y)-sample; - if(abs(dz)<3.0*dxy) + vec2 offset = vec2(float(i)-1.5, float(j)-1.5)*tex_scale; + float occ = texture(occlusion, texcoord+offset).r; + float depth = texture(depth, texcoord+offset).r; + if(depth>=min_depth && depth<=max_depth) { - sum += texture(occlusion, texcoord+offs).r; - count += 1.0; + sum += occ; + count += 1; } } - frag_color = texture(source, texcoord)*sum/count; + vec4 src_color = texture(source, texcoord); + frag_color = vec4(src_color.rgb*mix(1.0, min(sum*2.0/count, 1.0), darkness), src_color.a); }