Advertisement
Guest User

Untitled

a guest
Jun 2nd, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*[Vertex]*/
  2. in vec4 attr_Position;
  3. in vec4 attr_TexCoord0;
  4.  
  5. out vec2 var_ScreenTex;
  6.  
  7. void main()
  8. {
  9.     gl_Position = attr_Position;
  10.     var_ScreenTex = attr_TexCoord0.xy;
  11. }
  12.  
  13. /*[Fragment]*/
  14. uniform sampler2D u_ScreenDepthMap; // colormap
  15. uniform sampler2D u_ScreenImageMap; // lightmap
  16. uniform vec4 u_ViewInfo; // znear, zfar, 0, 0
  17. uniform vec2 u_ScreenInfo; // width, height
  18.  
  19. uniform vec4 u_SSAOSettings; // aocap, 0, aoMultiplier, lightmap
  20. uniform vec4 u_SSAOSettings2; // 0, aorange, depthTolerance, 0
  21.  
  22. in vec2 var_ScreenTex;
  23.  
  24. out vec4 out_Color;
  25.  
  26. //
  27. // AO Shader by Monsterovich :D
  28. //
  29.  
  30. float readDepth( in vec2 coord, in float znear, in float zfar ) {
  31.     return (2.0 * znear) / (zfar + znear - texture( u_ScreenDepthMap, coord ).x * (zfar - znear)); 
  32. }
  33.  
  34. float compareDepths( in float depth1, in float depth2, in float znear, in float zfar  ) {
  35.     float diff = sqrt( clamp(1.0-(depth1-depth2) / ( u_SSAOSettings2.y /* aorange */ / (zfar - znear)),0.0,1.0) );
  36.     float ao = min(u_SSAOSettings.x /* aocap */,max(0.0,depth1-depth2-u_SSAOSettings2.z /* depthTolerance */) * u_SSAOSettings.z /* aoMultiplier */) * diff;
  37.     return ao;
  38. }
  39.  
  40. const float P1 = 0.70710678118; // sin,cos(pi/4)
  41. const float P2x = 0.92387953251; // cos(pi/8)
  42. const float P2y = 0.38268343236; // sin(pi/8)
  43. const float P3x = P2y; // cos(3*pi/8)
  44. const float P3y = P2x; // sin(3*pi/8)
  45.  
  46.  
  47. void main(void)
  48. {
  49.     float depth = readDepth( var_ScreenTex, u_ViewInfo.x, u_ViewInfo.y );
  50.  
  51.     float d;
  52.  
  53.     float pw = 1.0 / u_ScreenInfo.x;
  54.     float ph = 1.0 / u_ScreenInfo.y;
  55.  
  56.     float aoCap = u_SSAOSettings.x;
  57.     float ao = 0.0;
  58.     float aoScale = 1.0;
  59.  
  60.     int i;
  61.     for (i = 0; i < 4; i++)
  62.     {
  63.         // This creates a circle, using precalculated sin/cos for performance reasons
  64.         // pi / 8 (4 points)
  65.         d = readDepth( vec2(var_ScreenTex.x+pw*P2x,var_ScreenTex.y+ph*P2y), u_ViewInfo.x, u_ViewInfo.y);
  66.         ao += compareDepths(depth,d,u_ViewInfo.x,u_ViewInfo.y) / aoScale;
  67.  
  68.         d = readDepth( vec2(var_ScreenTex.x-pw*P2x,var_ScreenTex.y+ph*P2y), u_ViewInfo.x, u_ViewInfo.y);
  69.         ao += compareDepths(depth,d,u_ViewInfo.x,u_ViewInfo.y) / aoScale;
  70.  
  71.         d = readDepth( vec2(var_ScreenTex.x+pw*P2x,var_ScreenTex.y-ph*P2y), u_ViewInfo.x, u_ViewInfo.y);
  72.         ao += compareDepths(depth,d,u_ViewInfo.x,u_ViewInfo.y) / aoScale;
  73.  
  74.         d = readDepth( vec2(var_ScreenTex.x-pw*P2x,var_ScreenTex.y-ph*P2y), u_ViewInfo.x, u_ViewInfo.y);
  75.         ao += compareDepths(depth,d,u_ViewInfo.x,u_ViewInfo.y) / aoScale;
  76.  
  77.         // 3*pi / 8 (4 points)
  78.         d = readDepth( vec2(var_ScreenTex.x+pw*P3x,var_ScreenTex.y+ph*P3y), u_ViewInfo.x, u_ViewInfo.y);
  79.         ao += compareDepths(depth,d,u_ViewInfo.x,u_ViewInfo.y) / aoScale;
  80.  
  81.         d = readDepth( vec2(var_ScreenTex.x-pw*P3x,var_ScreenTex.y+ph*P3y), u_ViewInfo.x, u_ViewInfo.y);
  82.         ao += compareDepths(depth,d,u_ViewInfo.x,u_ViewInfo.y) / aoScale;
  83.  
  84.         d = readDepth( vec2(var_ScreenTex.x+pw*P3x,var_ScreenTex.y-ph*P3y), u_ViewInfo.x, u_ViewInfo.y);
  85.         ao += compareDepths(depth,d,u_ViewInfo.x,u_ViewInfo.y) / aoScale;
  86.  
  87.         d = readDepth( vec2(var_ScreenTex.x-pw*P3x,var_ScreenTex.y-ph*P3y), u_ViewInfo.x, u_ViewInfo.y);
  88.         ao += compareDepths(depth,d,u_ViewInfo.x,u_ViewInfo.y) / aoScale;
  89.  
  90.         // pi / 4 (4 points)
  91.         d = readDepth( vec2(var_ScreenTex.x+pw/P1,var_ScreenTex.y+ph/P1), u_ViewInfo.x, u_ViewInfo.y);
  92.         ao += compareDepths(depth,d,u_ViewInfo.x,u_ViewInfo.y) / aoScale;
  93.  
  94.         d = readDepth( vec2(var_ScreenTex.x-pw/P1,var_ScreenTex.y+ph/P1), u_ViewInfo.x, u_ViewInfo.y);
  95.         ao += compareDepths(depth,d,u_ViewInfo.x,u_ViewInfo.y) / aoScale;
  96.  
  97.         d = readDepth( vec2(var_ScreenTex.x+pw/P1,var_ScreenTex.y-ph/P1), u_ViewInfo.x, u_ViewInfo.y);
  98.         ao += compareDepths(depth,d,u_ViewInfo.x,u_ViewInfo.y) / aoScale;
  99.  
  100.         d = readDepth( vec2(var_ScreenTex.x-pw/P1,var_ScreenTex.y-ph/P1), u_ViewInfo.x, u_ViewInfo.y);
  101.         ao += compareDepths(depth,d,u_ViewInfo.x,u_ViewInfo.y) / aoScale;
  102.  
  103.         // up/down/left/right
  104.         d = readDepth( vec2(var_ScreenTex.x+pw,var_ScreenTex.y), u_ViewInfo.x, u_ViewInfo.y);
  105.         ao += compareDepths(depth,d,u_ViewInfo.x,u_ViewInfo.y) / aoScale;
  106.  
  107.         d = readDepth( vec2(var_ScreenTex.x-pw,var_ScreenTex.y), u_ViewInfo.x, u_ViewInfo.y);
  108.         ao += compareDepths(depth,d,u_ViewInfo.x,u_ViewInfo.y) / aoScale;
  109.  
  110.         d = readDepth( vec2(var_ScreenTex.x,var_ScreenTex.y-ph), u_ViewInfo.x, u_ViewInfo.y);
  111.         ao += compareDepths(depth,d,u_ViewInfo.x,u_ViewInfo.y) / aoScale;
  112.  
  113.         d = readDepth( vec2(var_ScreenTex.x,var_ScreenTex.y+ph), u_ViewInfo.x, u_ViewInfo.y);
  114.         ao += compareDepths(depth,d,u_ViewInfo.x,u_ViewInfo.y) / aoScale;
  115.  
  116.         pw *= 2.0;
  117.         ph *= 2.0;
  118.         aoScale *= 1.2;
  119.     }
  120.  
  121.     ao /= 32.0;
  122.  
  123.     float done = (1.0 - ao);
  124.     if (u_SSAOSettings.w > 1)
  125.     {
  126.         float orig = texture(u_ScreenImageMap,var_ScreenTex).x;
  127.         done *= u_SSAOSettings.w;
  128.     }
  129.  
  130.     out_Color = vec4(done, done, done, 0.0);
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement