Advertisement
Caiwan

ssao.frag

Oct 21st, 2012
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.33 KB | None | 0 0
  1. // ssao3.frag
  2.  
  3. // sokadik ssao kiserlet
  4. //
  5. // Caiwan / IR
  6. // 2012
  7.  
  8. uniform sampler2D tex0;
  9. uniform sampler2D normalTex;
  10. uniform sampler2D depthTex;
  11. uniform sampler2D noiseTex;
  12.  
  13. uniform vec2 resolution;
  14.  
  15. uniform float near;
  16. uniform float far;
  17. uniform float fov;
  18.  
  19. uniform float noiseSize;
  20.  
  21. uniform mat4 projectionMatrix;
  22.  
  23. float aspectRatio;  
  24. vec2 noiseScale = vec2(3.,3.);
  25.  
  26. uniform vec3 kernel[32];
  27.  
  28. const int kernelSize = 8;  
  29. const float radius = .1;
  30.  
  31. //http://www.john-chapman.net/content.php?id=8
  32.  
  33. vec2 getTexcoord(vec2 uv){
  34.     vec2 uv_ = uv;
  35.     uv_.s = clamp(uv.s * (resolution.x/2048.), 0., 1.);
  36.     uv_.t = clamp(uv.t * (resolution.y/2048.), 0., 1.); //anyad
  37.     return uv_;
  38. }
  39.  
  40. vec3 getNormal(vec2 uv){
  41.     return (2.*texture2D(normalTex, getTexcoord(uv)).xyz)-1.; // anyad geci
  42. }
  43.  
  44. vec3 getNormal2(){
  45.     return (2.*texture2D(normalTex, gl_TexCoord[0].st).xyz)-1.;
  46. }
  47.  
  48. float getLinearDepth(float zz, float _near, float _far){
  49.     float z = 2.*_far*_near/((_far-_near)*(zz-(_far+_near)/(_far-_near)));
  50.     return -z;
  51. }
  52.  
  53. float getDepth(vec2 uv){
  54.     //float zz = float(texture2D(depthTex, getTexcoord(uv)));
  55.     //float z = 2.*far*near/((far-near)*(zz-(far+near)/(far-near)));
  56.     //return z;
  57.     //return getLinearDepth(zz, near, far);
  58.     return getLinearDepth(float(texture2D(depthTex, getTexcoord(uv))), near, far);
  59. }
  60.                
  61.  
  62. vec3 getViewRay(vec2 tc) {
  63.     float hfar = 2.0 * tan(fov/2.0) * far;
  64.     float wfar = hfar * aspectRatio;    
  65.     vec3 ray = vec3(wfar * (tc.x - 0.5), hfar * (tc.y - 0.5), -far);    
  66.     return ray;                      
  67. }  
  68.  
  69. void main (){
  70.     vec2 screenPos = gl_FragCoord.xy/resolution;
  71.     //screenPos.y = 1.0 - screenPos.y;
  72.     noiseScale = noiseSize / resolution;
  73.     aspectRatio = resolution.x / resolution.y;
  74.    
  75.     float linearDepth = getDepth(screenPos);          
  76.     vec3 origin = getViewRay(screenPos) * linearDepth;  
  77.            
  78.     //vec3 normal = normalize(vNormal);  
  79.     vec3 normal = getNormal2();  
  80.    
  81.     //gl_FragColor.rgb = texture2D(normalTex, gl_TexCoord[0].st).xyz;
  82.     //gl_FragColor.a = 1.;
  83.     //return;
  84.    
  85.     vec3 rvec = texture2D(noiseTex, screenPos.xy * noiseScale).xyz * 2.0 - 1.0;
  86.     //vec3 rvec = texture2D(noiseTex, screenPos.xy).xyz * 2.0 - 1.0;
  87.     vec3 tangent = normalize(rvec - normal * dot(rvec, normal));
  88.     vec3 bitangent = cross(normal, tangent);
  89.     mat3 tbn = mat3(tangent, bitangent, normal);        
  90.    
  91.     gl_FragColor.rgb = .5*rvec+.5;
  92.     //gl_FragColor.rgb = vec3(linearDepth/far);
  93.    
  94.     //float z = float(texture2D(depthTex, getTexcoord(screenPos)));
  95.     //float z = texture2D(depthTex, getTexcoord(screenPos)).r;
  96.     //float lz = getLinearDepth(z, 1., 100.)/100.;
  97.    
  98.     //gl_FragColor.rgb = vec3(lz);
  99.     gl_FragColor.a = 1.;
  100.     return;
  101.    
  102.     float occlusion = 0.0;
  103.     if (linearDepth-far < 0.)
  104.         for(int i = 0; i < kernelSize; ++i) {        
  105.             vec3 sample = origin + (tbn * kernel[i]) * radius;
  106.             vec4 offset = projectionMatrix * vec4(sample, 1.0);    
  107.             offset.xy /= offset.w;
  108.             offset.xy = offset.xy * 0.5 + 0.5;        
  109.             float sampleDepth = -sample.z/far;
  110.             float depthBufferValue = getDepth(offset.xy);                            
  111.             float range_check = abs(linearDepth - depthBufferValue);
  112.             if (range_check < radius && depthBufferValue <= sampleDepth) {
  113.                 occlusion +=  1.0;
  114.             }
  115.         }        
  116.        
  117.     occlusion = 1.0 - occlusion / float(kernelSize);
  118.  
  119.     gl_FragColor.rgb = vec3(occlusion);
  120.     gl_FragColor.a = 1.0;  
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement