Advertisement
Guest User

Untitled

a guest
Oct 29th, 2015
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.12 KB | None | 0 0
  1. #version 150 core
  2.  
  3. #define CONST_PI 3.141592653589793238
  4. #define CONST_POWER 4.0
  5. #define CONST_RADIUS 2.0
  6. #define CONST_SAMPLING_DIRECTIONS 4
  7. #define CONST_NUM_SAMPLING_STEPS 3
  8. #define CONST_TANGENT_BIAS 0.33
  9. #define CONST_RANGE 3
  10.  
  11. uniform sampler2D texture_depth;
  12. uniform sampler2D texture_normal;
  13. uniform sampler2D texture_noise;
  14.  
  15. uniform vec2 uScreenDimension;
  16. uniform float uInverseAspectRatio;
  17. uniform mat4 uInverseProjection;
  18.  
  19. uniform vec2 frustumCorner;
  20. uniform vec2 nearFar;
  21.  
  22. in vec2 pass_TextureCoord;
  23. out vec4 out_Color;
  24.  
  25. vec3 unproject( vec2 tc, float depth ) {
  26.     return vec3( (tc * 2.0 - 1.0) * frustumCorner, -1) * depth;
  27. }
  28.  
  29. float rand(vec2 co){
  30.     return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453);
  31. }
  32.  
  33. void main() {
  34.     float depth = texture( texture_depth, pass_TextureCoord ).r;
  35.     vec3 viewPos = unproject( pass_TextureCoord, depth );
  36.     vec3 viewNorm = texture(texture_normal, pass_TextureCoord).xyz * 2.0 - 1.0;
  37.    
  38.     float total = 0.0;
  39.     float sample_direction_increment = (CONST_PI * 2) / float(CONST_SAMPLING_DIRECTIONS);
  40.    
  41.     // Calculate noise
  42.     vec2 noiseScale = uScreenDimension/4.0;
  43.     vec2 randVector = normalize( texture( texture_noise, pass_TextureCoord * noiseScale).xy * 2.0 - 1.0);
  44.    
  45.     float offsetScale = 1 / (depth * nearFar.y);
  46.    
  47.     for (int i = 0; i < CONST_SAMPLING_DIRECTIONS; i++) {
  48.         float randomValue = rand(pass_TextureCoord); // Unused. Noise texture has better results
  49.         float sampling_angle = float(i) * sample_direction_increment;// + randomValue; // azimuth angle theta in the paper
  50.         vec2 sampleDir = vec2(cos(sampling_angle), sin(sampling_angle));
  51.        
  52.         // Apply noise
  53.         sampleDir = reflect(sampleDir, randVector); // If random rotation is used, comment out this line
  54.        
  55.         // March along sampleDir (vector)
  56.         float tangentAngle = acos(dot(vec3(sampleDir, 0.0), viewNorm)) - (0.5 * CONST_PI) + CONST_TANGENT_BIAS;
  57.         float horizonAngle = tangentAngle;
  58.         float SAMPLING_STEP = (CONST_RADIUS + ((randVector.x - randVector.y) * 0.5)) / float(CONST_NUM_SAMPLING_STEPS);
  59.         SAMPLING_STEP *= 0.1;
  60.        
  61.         // Scale by distance from camera
  62.         SAMPLING_STEP *= offsetScale;
  63.        
  64.         // Force min-max
  65.         SAMPLING_STEP = min(0.05, max( 0.001, SAMPLING_STEP ) );
  66.        
  67.         // Start occlusion check
  68.         float occlusion = 0.0;
  69.         for (int j = 0; j < CONST_NUM_SAMPLING_STEPS; j++) {
  70.             // march along the sampling direction and see what the horizon is
  71.             vec2 sampleOffset = float(j+1) * SAMPLING_STEP * sampleDir;
  72.             sampleOffset.x *= uInverseAspectRatio;
  73.             vec2 offTex = pass_TextureCoord + sampleOffset;
  74.            
  75.             // reconstruct view-space position for this sample
  76.             vec3 off_viewPos = unproject( offTex.st, texture( texture_depth, offTex.st ).r );//reconstructPosition( texture( texture_depth, offTex.st ).r, offTex );
  77.            
  78.             // get difference vector
  79.             vec3 diff = off_viewPos.xyz - viewPos.xyz;
  80.            
  81.             // find length
  82.             float diffLength = length(diff);
  83.            
  84.             // If there is an occlusion
  85.             float normalCheck   = 1.0 - clamp( dot( tangentAngle, horizonAngle ), 0.0, 1.0 );
  86.             float rangeCheck = smoothstep(0.0, 1.0, ( float(CONST_RADIUS * CONST_RANGE) * (1/uScreenDimension.x) ) / abs(diff.z));
  87.            
  88.             // find horizon angle
  89.             float x = diff.z / length(diff.xy);
  90.             float elevationAngle = x * inversesqrt(x*x + 1); // ORIGINAL, SLOWER --> atan(diff.z / length(diff.xy));
  91.             horizonAngle = max(horizonAngle, elevationAngle);
  92.  
  93.             // Handle attenuation
  94.             float normDiff = diffLength / float(CONST_RADIUS);
  95.             float attenuation = 1 - normDiff*normDiff;
  96.            
  97.             // Fade out
  98.             float fade = 1.0 - clamp( depth * 1.5, 0.0, 1.0);
  99.            
  100.            
  101.             // Apply occlusion
  102.             occlusion += clamp(attenuation * (sin(horizonAngle) - sin(tangentAngle)), 0.0, 1.0) * normalCheck * rangeCheck * fade;
  103.         }
  104.         occlusion /= float(CONST_NUM_SAMPLING_STEPS);
  105.         total += 1.0 - occlusion;
  106.     }
  107.    
  108.     // Divide ao by amount of directions
  109.     total /= CONST_SAMPLING_DIRECTIONS;
  110.    
  111.     // Power
  112.     total = pow( clamp( total, 0.0, 1.0), CONST_POWER );
  113.  
  114.     // Output ssao
  115.     out_Color = vec4(total, total, total, 1.0);
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement