2 import _ambientocclusion;
4 #pragma MSP stage(fragment)
5 layout(location=0) out float frag_out;
8 vec3 tex_scale = vec3(1.0/vec2(textureSize(depth, 0)), 0.0);
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);
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);
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;
23 float min_depth = project(vec3(center.xy, center.z+occlusion_radius)).z;
24 float occlusion_sum = 0.0;
26 for(int i=0; i<n_samples; ++i)
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)
32 if(sample_depth<psp.z)
37 frag_out = 1.0-occlusion_sum/count;