]> git.tdb.fi Git - libs/gl.git/blob - builtin_data/_ambientocclusion_combine.glsl
Simplify Program by removing transient data
[libs/gl.git] / builtin_data / _ambientocclusion_combine.glsl
1 import flat_effect;
2 import _ambientocclusion;
3
4 #pragma MSP stage(fragment)
5 layout(location=0) out vec4 frag_color;
6 void main()
7 {
8         vec3 center = unproject(vec3(vertex.xy, texture(depth, texcoord).r));
9         vec2 tex_scale = 1.0/vec2(textureSize(occlusion, 0));
10         float sum = 0.0;
11         float count = 0.0;
12         for(int i=0; i<4; ++i)
13                 for(int j=0; j<4; ++j)
14                 {
15                         vec2 offset = vec2(float(i), float(j))-1.5;
16                         vec2 sample_coord = texcoord+offset*tex_scale;
17                         float occ = texture(occlusion, sample_coord).r;
18                         float sample_depth = texture(depth, sample_coord).r;
19                         float z_range = occlusion_radius*length(offset)*edge_depth_threshold;
20                         float min_depth = project(vec3(center.xy, center.z+z_range)).z;
21                         float max_depth = project(vec3(center.xy, center.z-z_range)).z;
22                         if(sample_depth>=min_depth && sample_depth<=max_depth)
23                         {
24                                 sum += occ;
25                                 count += 1.0;
26                         }
27                 }
28         vec4 src_color = texture(source, texcoord);
29         frag_color = vec4(src_color.rgb*mix(1.0, min(sum/count, 1.0), darkness), src_color.a);
30 }