Advertisement
Guest User

Untitled

a guest
Mar 20th, 2015
701
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.61 KB | None | 0 0
  1. #version 150
  2.  
  3. #define SAMPLES 16
  4.  
  5. const float PI2 = 2 * 3.1415926535897932384626433832795;
  6.  
  7. uniform sampler2D normalBuffer;
  8. uniform sampler2D depthBuffer;
  9.  
  10. uniform mat4 inverseProj;
  11.  
  12. uniform float inverseAspectRatio;
  13. uniform vec2 minMaxSsaoWidth;
  14.  
  15. in vec2 texCoords;
  16.  
  17. out float fragColor;
  18.  
  19.  
  20. vec2 offsets[16] = vec2[16](
  21.     vec2(0.044821292, 0.110167414) * 2 - 1,
  22.     vec2(0.1380541, 0.4925164) * 2 - 1,
  23.     vec2(0.1711376, 0.60476863) * 2 - 1,
  24.     vec2(0.0026801229, 0.8000565) * 2 - 1,
  25.     vec2(0.4459834, 0.14888838) * 2 - 1,
  26.     vec2(0.29496264, 0.38575724) * 2 - 1,
  27.     vec2(0.4706212, 0.6823994) * 2 - 1,
  28.     vec2(0.33042485, 0.8249533) * 2 - 1,
  29.     vec2(0.57578623, 0.1047747) * 2 - 1,
  30.     vec2(0.67860055, 0.25636286) * 2 - 1,
  31.     vec2(0.6422993, 0.5212651) * 2 - 1,
  32.     vec2(0.69669557, 0.9629153) * 2 - 1,
  33.     vec2(0.9217044, 0.1944445) * 2 - 1,
  34.     vec2(0.8729959, 0.4054296) * 2 - 1,
  35.     vec2(0.76195055, 0.6761958) * 2 - 1,
  36.     vec2(0.97839516, 0.90347415 * 2 - 1)
  37. );
  38.  
  39.  
  40.  
  41.  
  42. float rand(vec2 co){
  43.     return fract(sin(dot(co.xy, vec2(12.9898, 78.233))) * 43758.5453);
  44. }
  45.  
  46. #pragma optionNV(unroll all)
  47. void main(){
  48.  
  49.     float depthValue = texture(depthBuffer, texCoords).r;
  50.    
  51.     float ssao = 1.0;
  52.    
  53.     vec4 center = inverseProj * vec4(texCoords * 2.0 - 1.0, depthValue * 2 - 1, 1.0);
  54.     center.xyz /= center.w;
  55.    
  56.     vec3 normal = texture(normalBuffer, texCoords).xyz;
  57.  
  58.     float invDistance = inversesqrt(dot(center.xyz, center.xyz));
  59.     float rawOffsetScale = 0.25 * invDistance;
  60.  
  61.     //Fade out SSAO when close to the camera to reduce the effect of clamping the sample radius.
  62.     float clampedOffsetScale = clamp(rawOffsetScale, minMaxSsaoWidth.x, minMaxSsaoWidth.y);
  63.     float powerScale = clamp(clampedOffsetScale / rawOffsetScale, 0.0, 1);
  64.     powerScale *= powerScale;
  65.  
  66.     //Compensates for aspect ratio when sampling area around center pixel.
  67.     vec2 offsetScale = clampedOffsetScale * vec2(inverseAspectRatio, 1.0);
  68.    
  69.     //Calculate a rotation matrix to randomly offset the samples per pixel.
  70.     float angle = rand(texCoords) * PI2;
  71.     float sin = sin(angle);
  72.     float cos = cos(angle);
  73.     mat2 rotation = mat2(
  74.         cos, -sin,
  75.         sin,  cos
  76.     );
  77.    
  78.     for(int i = 0; i < SAMPLES; i++){
  79.         vec2 offset = (offsets[i] * rotation) * offsetScale;
  80.         vec2 sampleCoords = texCoords + offset;
  81.        
  82.         vec4 sample = inverseProj * vec4(vec3(sampleCoords, texture(depthBuffer, sampleCoords).r) * 2.0 - 1.0, 1.0);
  83.         sample.xyz /= sample.w;
  84.        
  85.         vec3 dPos = sample.xyz - center.xyz;
  86.  
  87.         float d = dot(dPos, dPos);
  88.         vec3 direction = dPos * inversesqrt(d);
  89.         float falloff = 1 + d*50;
  90.  
  91.         ssao *= 1 - clamp(dot(direction, normal) * powerScale / falloff, 0, 1);
  92.     }
  93.    
  94.     fragColor = ssao;
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement