DavidNorgren

Untitled

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