dof.glslf 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #version GLSL_VERSION
  2. /*==============================================================================
  3. VARS
  4. ==============================================================================*/
  5. #var PRECISION highp
  6. #var DOF_TYPE DOF_SIMPLE
  7. /*============================================================================*/
  8. precision PRECISION sampler2D;
  9. #include <precision_statement.glslf>
  10. #include <std.glsl>
  11. #if DOF_TYPE == DOF_BOKEH
  12. uniform highp sampler2D u_sharp;
  13. uniform highp sampler2D u_blurred1;
  14. uniform highp sampler2D u_blurred2;
  15. uniform float u_dof_dist;
  16. #else
  17. #include <depth_fetch.glslf>
  18. uniform sampler2D u_sharp;
  19. uniform sampler2D u_blurred;
  20. uniform sampler2D u_depth;
  21. uniform float u_view_max_depth;
  22. uniform float u_dof_dist;
  23. uniform float u_dof_front_end;
  24. uniform float u_dof_rear_end;
  25. uniform vec2 u_camera_range;
  26. #endif
  27. /*==============================================================================
  28. SHADER INTERFACE
  29. ==============================================================================*/
  30. GLSL_IN vec2 v_texcoord;
  31. //------------------------------------------------------------------------------
  32. GLSL_OUT vec4 GLSL_OUT_FRAG_COLOR;
  33. /*==============================================================================
  34. MAIN
  35. ==============================================================================*/
  36. void main(void) {
  37. vec4 tex_sharp = GLSL_TEXTURE(u_sharp, v_texcoord);
  38. if (u_dof_dist > 0.0) {
  39. #if DOF_TYPE == DOF_BOKEH
  40. // NOTE: see postprocessing/coc.glslf
  41. float coc;
  42. vec4 tex_blurred1 = GLSL_TEXTURE(u_blurred1, v_texcoord);
  43. vec4 tex_blurred2 = GLSL_TEXTURE(u_blurred2, v_texcoord);
  44. // combining blurs for hexagonal bokeh
  45. vec4 blurred = min(tex_blurred1.rgba, tex_blurred2.rgba);
  46. coc = blurred.a;
  47. if (coc > 0.0) {
  48. // divide by coc to recover color
  49. blurred = vec4(blurred.rgb / coc, tex_sharp.a);
  50. // mix for smooth blurred areas borders
  51. GLSL_OUT_FRAG_COLOR = mix(tex_sharp, blurred, min(coc * 5.0, 1.0));
  52. }
  53. else
  54. GLSL_OUT_FRAG_COLOR = tex_sharp;
  55. #elif DOF_TYPE == DOF_SIMPLE
  56. float depth = depth_fetch(u_depth, v_texcoord, u_camera_range);
  57. depth *= u_view_max_depth;
  58. float strength;
  59. if (depth < u_dof_dist)
  60. strength = (u_dof_dist - depth) / u_dof_front_end;
  61. else
  62. strength = (depth - u_dof_dist) / u_dof_rear_end;
  63. strength = clamp(strength, 0.0, 1.0);
  64. vec4 tex_blurred = GLSL_TEXTURE(u_blurred, v_texcoord);
  65. GLSL_OUT_FRAG_COLOR = mix(tex_sharp, tex_blurred, strength);
  66. #else
  67. GLSL_OUT_FRAG_COLOR = tex_sharp;
  68. #endif
  69. } else
  70. GLSL_OUT_FRAG_COLOR = tex_sharp;
  71. }