Ambient Occlusion
user_6332734
glsl
2 years ago
2.3 kB
10
Indexable
uniform sampler2D Depthmap; uniform mat4 cam_view; uniform mat4 cam_proj; const float Radius = 4.; const float Samples = 8.; varying vec3 v_vPosition; varying vec2 v_vTexcoord; varying vec3 v_vNormal; /////////////////////Hash///////////////////////////////// vec3 Hash3(vec3 pp) { return normalize(fract(cos(pp*mat3(94.55,-69.38,-89.27,78.69,-34.54,23.67,-68.45,90.13,-57.85))*825.79)-.5); } //////////////////Color To Depth////////////////////////// float ColorToDepth(vec4 map) { return (map.r + (map.g / 256.) + (map.b / 65536.)) * 1000.; } //////////////////Camera Position////////////////////////// vec3 CameraPosition(mat4 view) { float camX = - (view[3][0] * view[0][0] + view[3][1] * view[0][1] + view[3][2] * view[0][2]); float camY = - (view[3][0] * view[1][0] + view[3][1] * view[1][1] + view[3][2] * view[1][2]); float camZ = - (view[3][0] * view[2][0] + view[3][1] * view[2][1] + view[3][2] * view[2][2]); return vec3( camX, camY, camZ ); } //////////////////World To Screen///////////////////////// vec2 WorldToScreen(vec3 pos, mat4 proj, mat4 view) { float bound = view[0][2] * pos.x + view[1][2] * pos.y + view[2][2] * pos.z + view[3][2]; if (bound < 0.) { return vec2(-1., -1.); } float cx = proj[2][0]+proj[0][0]*(view[0][0] * pos.x + view[1][0] * pos.y + view[2][0] * pos.z + view[3][0])/bound; float cy = proj[2][1]+proj[1][1]*(view[0][1] * pos.x + view[1][1] * pos.y + view[2][1] * pos.z + view[3][1])/bound; return vec2( .5 + .5 * cx, .5 + .5 * cy ); } /////////////////////////////////////////////////////////////// void main() { float ao = 0.; float bias = 0.001; vec3 camPosition = CameraPosition( cam_view ); for(float i = 1.; i < 360.; i+=(360./Samples)) { for( float j = -90.; j < 90.; j += (180./Samples)) { float R = cos(radians(i)) * -sin(radians(j)); vec3 Noise = Hash3( v_vPosition - (R * Radius)); float D = distance( v_vPosition - (Noise * Radius), camPosition ); vec2 coords = WorldToScreen( v_vPosition - (Noise * Radius), cam_proj, cam_view ); float Sample_Depth = ColorToDepth( texture2D( Depthmap, coords) ) + bias; ao += (sign(floor(Sample_Depth / D))) / (Samples*10.); } } ao = clamp(ao/0.5, 0.,1.); gl_FragColor = vec4(ao,ao,ao,1.); }
Editor is loading...