DavidNorgren

Untitled

Aug 3rd, 2015
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Vertex
  2. varying vec2 vertTexCoord;
  3.  
  4. attribute vec3 in_Position;
  5. attribute vec2 in_TextureCoord;
  6.  
  7. void main() {
  8.     vertTexCoord = in_TextureCoord;
  9.     gl_Position = gm_Matrices[MATRIX_WORLD_VIEW_PROJECTION] * vec4(in_Position, 1.0);
  10. }
  11.  
  12. // Fragment
  13. uniform sampler2D depthMap;
  14. uniform vec2 screenSize;
  15. uniform float blurSize;
  16. uniform float depth;
  17. uniform float range;
  18. uniform float fadeSize;
  19. uniform float zfar;
  20.  
  21. varying vec2 vertTexCoord;
  22.  
  23. const float pi = 3.14159265;
  24. const float tau = pi * 2.0;
  25. const int samples = 15;
  26. const int ringDetail = 4;
  27. const int totalSamples = samples * ringDetail;
  28.  
  29. float screenSampleSize = screenSize.y * blurSize;
  30. vec2 texelSize = 1.0 / screenSize;
  31.  
  32. float colorToFloat(vec3 c) { return c.r + c.g / 255.0 + c.b / (255.0 * 255.0); }
  33. float getDepth(vec2 coord) { return colorToFloat(texture2D(depthMap, coord).rgb) * zfar; }
  34. float getBlur(float d) { return clamp((abs(d - depth) - range) / fadeSize, 0.0, 1.0); }
  35. float getFrontBlur(float d) { return clamp(((depth - range) - d) / fadeSize, 0.0, 1.0); }
  36. float getBackBlur(float d) { return clamp((d - (depth + range)) / fadeSize, 0.0, 1.0); }
  37.  
  38. void main() {
  39.     float myDepth = getDepth(vertTexCoord), myFrontBlur = getFrontBlur(myDepth), myBackBlur = getBackBlur(myDepth);
  40.     float blur = 0.0, maxdis = screenSampleSize * screenSampleSize + screenSampleSize * screenSampleSize, colorDiv = 0.0;
  41.     vec4 colorAdd = vec4(0.0);
  42.     gl_FragColor = texture2D(gm_BaseTexture, vertTexCoord);
  43.    
  44.     for (int i = 0; i < totalSamples; i++) {
  45.         float angle = (float(i / ringDetail) / float(samples)) * tau;
  46.         float dis = 1.0 - mod(float(i),float(ringDetail)) / float(ringDetail);
  47.         vec2 offset = vec2(cos(angle), sin(angle)) * dis * screenSampleSize;
  48.         vec2 tex = vertTexCoord + texelSize * offset;
  49.         float sampleDepth = getDepth(tex);
  50.         float sampleBlur = getBlur(sampleDepth);
  51.         float mul = dis * (1.0 - (1.0 - sampleBlur) * myBackBlur);
  52.         if (mul > 0.0) {
  53.             colorAdd += texture2D(gm_BaseTexture, tex) * mul;
  54.             colorDiv += mul;
  55.         }
  56.         blur += getFrontBlur(sampleDepth);
  57.     }
  58.     blur /= float(totalSamples);
  59.     blur += myFrontBlur + myBackBlur;
  60.     colorAdd *= blur;
  61.     colorDiv *= blur;
  62.     gl_FragColor += colorAdd;
  63.     gl_FragColor /= colorDiv + 1.0;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment