Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Shader that adds outlines
  2.  
  3. const float EDGE_THRESHOLD = 0.1;
  4. const float EDGE_THICKNESS = 1.0;
  5.  
  6. const float INFINITY = 1000.0;
  7.  
  8. uniform sampler2D sampler0;
  9. uniform sampler2D sampler1;
  10. uniform sampler2D sampler2;
  11.  
  12. uniform float near;
  13. uniform float far;
  14.  
  15. uniform float aspectRatio;
  16. uniform float displayHeight;
  17. uniform float displayWidth;
  18.  
  19. float getDepth(vec2 coord);
  20.  
  21. void main() {
  22.     gl_FragColor = texture2D(sampler0, gl_TexCoord[0].st);
  23.    
  24.     vec2 o11 = vec2(1.0, aspectRatio)*EDGE_THICKNESS/displayWidth;
  25.     vec2 base = gl_TexCoord[0].st - 2*o11;
  26.  
  27.     float depth = getDepth(gl_TexCoord[0].st);
  28.     float avg = depth;
  29.     float laplace = 24*depth;
  30.     float sample;
  31.     int n = 1;
  32.  
  33.     for (int i = 0; i < 5; ++i) {
  34.         for (int j = 0; j < 5; ++j) {
  35.             if (i != 2 || j != 2) {
  36.                 sample = getDepth(base + vec2(float(i) * o11.s, float(j) * o11.t));
  37.                 laplace -= sample;
  38.                 if (sample < 1.0) {
  39.                     ++n;
  40.                     avg += sample*sample;
  41.                 }
  42.             }
  43.         }
  44.     }
  45.    
  46.     avg /= n;
  47.  
  48.     if (laplace > EDGE_THRESHOLD) {
  49.         avg = clamp(avg, 0.0, 1.0);
  50.         gl_FragColor.rgb = mix(vec3(0.0), gl_Fog.color.rgb, 0.5*avg*clamp(depth, 0.0, 1.0));
  51.     }
  52. }
  53.  
  54. float getDepth(vec2 coord) {
  55.     // Return linearized depth in [0.0, 1.0]
  56.     float depth = texture2D(sampler1, coord).x;
  57.     float depth2 = texture2D(sampler2, coord).x;
  58.     if (depth2 < 1.0) {
  59.         depth = depth2;
  60.     }
  61.    
  62.     if (depth == 1.0) {
  63.         // Attempt to detect sky
  64.         return INFINITY;
  65.     }
  66.  
  67.     return (near - depth*far)/(near + depth*(far - near));
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement