]> git.tdb.fi Git - libs/gl.git/blob - builtin_data/_ambientocclusion_occlude.glsl
Add support for integer vertex attributes
[libs/gl.git] / builtin_data / _ambientocclusion_occlude.glsl
1 import flat_effect;
2 import _ambientocclusion;
3
4 #pragma MSP stage(fragment)
5 layout(location=0) out float frag_out;
6 void main()
7 {
8         vec3 tex_scale = vec3(1.0/vec2(textureSize(depth, 0)), 0.0);
9
10         vec3 center = get_fragment_position(texcoord);
11         vec3 left = get_fragment_position(texcoord-tex_scale.xz);
12         vec3 right = get_fragment_position(texcoord+tex_scale.xz);
13         vec3 bottom = get_fragment_position(texcoord-tex_scale.zy);
14         vec3 top = get_fragment_position(texcoord+tex_scale.zy);
15
16         vec3 normal = normalize(cross(get_slope(left, center, right), get_slope(bottom, center, top)));
17         vec3 tangent = (abs(normal.x)>abs(normal.y) ? vec3(-normal.z, 0.0, normal.x) : vec3(0.0, -normal.z, normal.y));
18         vec3 binormal = cross(normal, tangent);
19
20         vec4 rv = texture(rotate, gl_FragCoord.xy/4.0)*2.0-1.0;
21         mat3 transform = mat3(tangent, binormal, normal)*mat3(rv.xy, 0.0, rv.zx, 0.0, 0.0, 0.0, 1.0)*occlusion_radius;
22
23         float min_depth = project(vec3(center.xy, center.z+occlusion_radius)).z;
24         float occlusion_sum = 0.0;
25         float count = 0.0;
26         for(int i=0; i<n_samples; ++i)
27         {
28                 vec3 psp = project(center+transform*sample_points[i]);
29                 float sample_depth = texture(depth, psp.xy*0.5+0.5).r;
30                 if(sample_depth>=min_depth)
31                 {
32                         if(sample_depth<psp.z)
33                                 occlusion_sum += 1.0;
34                         count += 1.0;
35                 }
36         }
37         frag_out = 1.0-occlusion_sum/count;
38 }