DavidNorgren

DOF

Jan 15th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /// shader_high_dof
  2.  
  3. varying vec2 vTexCoord;
  4.  
  5. void main() {
  6.     vTexCoord = in_TextureCoord;
  7.     gl_Position = gm_Matrices[MATRIX_WORLD_VIEW_PROJECTION] * vec4(in_Position, 1.0);
  8. }
  9. //######################_==_YOYO_SHADER_MARKER_==_######################@~uniform sampler2D uDepthBuffer;
  10. uniform vec2 uScreenSize;
  11. uniform float uBlurSize;
  12. uniform float uDepth;
  13. uniform float uRange;
  14. uniform float uFadeSize;
  15. uniform float uNear;
  16. uniform float uFar;
  17.  
  18. varying vec2 vTexCoord;
  19.  
  20. const float pi = 3.14159265;
  21. const float tau = pi * 2.0;
  22. const int samples = 15;
  23. const int ringDetail = 4;
  24. const int totalSamples = 60; // samples * ringDetail
  25.  
  26. float screenSampleSize = uScreenSize.y * uBlurSize;
  27. vec2 texelSize = 1.0 / uScreenSize;
  28.  
  29. float unpackDepth(vec4 c) {
  30.     return c.r + c.g / 255.0 + c.b / (255.0 * 255.0);
  31. }
  32.  
  33. float getDepth(vec2 coord) {
  34.     return uNear+ unpackDepth(texture2D(uDepthBuffer, coord)) * (uFar - uNear);
  35. }
  36.  
  37. float getBlur(float d) {
  38.     return clamp((abs(d - uDepth) - uRange) / uFadeSize, 0.0, 1.0);
  39. }
  40.  
  41. float getFrontBlur(float d) {
  42.     return clamp(((uDepth - uRange) - d) / uFadeSize, 0.0, 1.0);
  43. }
  44.  
  45. float getBackBlur(float d) {
  46.     return clamp((d - (uDepth + uRange)) / uFadeSize, 0.0, 1.0);
  47. }
  48.  
  49. void main() {
  50.     float myDepth = getDepth(vTexCoord);
  51.     float myFrontBlur = getFrontBlur(myDepth);
  52.     float myBackBlur = getBackBlur(myDepth);
  53.    
  54.     float blur = 0.0;
  55.     float maxdis = screenSampleSize * screenSampleSize + screenSampleSize * screenSampleSize;
  56.     float colorDiv = 0.0;
  57.     vec4 colorAdd = vec4(0.0);
  58.    
  59.     gl_FragColor = texture2D(gm_BaseTexture, vTexCoord);
  60.    
  61.     for (int i = 0; i < totalSamples; i++) {
  62.         float angle = (float(i / ringDetail) / float(samples)) * tau;
  63.         float dis = 1.0 - mod(float(i),float(ringDetail)) / float(ringDetail);
  64.         vec2 offset = vec2(cos(angle), sin(angle)) * dis * screenSampleSize;
  65.         vec2 tex = vTexCoord + texelSize * offset;
  66.         float sampleDepth = getDepth(tex);
  67.         float sampleBlur = getBlur(sampleDepth);
  68.         float mul = dis * (1.0 - (1.0 - sampleBlur) * myBackBlur);
  69.        
  70.         if (mul > 0.0) {
  71.             colorAdd += texture2D(gm_BaseTexture, tex) * mul;
  72.             colorDiv += mul;
  73.         }
  74.        
  75.         blur += getFrontBlur(sampleDepth);
  76.     }
  77.    
  78.     blur /= float(totalSamples);
  79.     blur += myFrontBlur + myBackBlur;
  80.    
  81.     colorAdd *= blur;
  82.     colorDiv *= blur;
  83.    
  84.     gl_FragColor += colorAdd;
  85.     gl_FragColor /= colorDiv + 1.0;
  86. }
Advertisement
Add Comment
Please, Sign In to add comment