coc.glslf 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #version GLSL_VERSION
  2. /*==============================================================================
  3. VARS
  4. ==============================================================================*/
  5. #var PRECISION highp
  6. #var COC_TYPE COC_ALL
  7. /*============================================================================*/
  8. precision PRECISION sampler2D;
  9. #include <precision_statement.glslf>
  10. #include <std.glsl>
  11. #include <depth_fetch.glslf>
  12. uniform sampler2D u_color;
  13. uniform sampler2D u_depth;
  14. #if COC_TYPE == COC_COMBINE
  15. uniform sampler2D u_coc_fg;
  16. #endif
  17. uniform float u_view_max_depth;
  18. uniform float u_dof_dist;
  19. uniform float u_dof_front_start;
  20. uniform float u_dof_front_end;
  21. uniform float u_dof_rear_start;
  22. uniform float u_dof_rear_end;
  23. uniform vec2 u_camera_range;
  24. /*==============================================================================
  25. SHADER INTERFACE
  26. ==============================================================================*/
  27. GLSL_IN vec2 v_texcoord;
  28. //------------------------------------------------------------------------------
  29. GLSL_OUT vec4 GLSL_OUT_FRAG_COLOR;
  30. /*==============================================================================
  31. MAIN
  32. ==============================================================================*/
  33. // REFERENCES:
  34. // general coc (circle of confusion) and foreground blur:
  35. // http://http.developer.nvidia.com/GPUGems3/gpugems3_ch28.html
  36. //
  37. // bokeh imitation:
  38. // http://ivizlab.sfu.ca/papers/cgf2012.pdf
  39. // http://www.frostbite.com/2011/08/more-performance-five-rendering-ideas-from-battlefield-3-and-need-for-speed-the-run/
  40. void main(void) {
  41. vec4 color = GLSL_TEXTURE(u_color, v_texcoord);
  42. if (u_dof_dist > 0.0) {
  43. float depth = depth_fetch(u_depth, v_texcoord, u_camera_range);
  44. depth *= u_view_max_depth;
  45. float coc = 0.0;
  46. #if COC_TYPE == COC_COMBINE
  47. vec4 color_fg = GLSL_TEXTURE(u_coc_fg, v_texcoord);
  48. float coc_fg = color_fg.a;
  49. #endif
  50. if (depth < u_dof_dist)
  51. coc = (u_dof_dist - depth - u_dof_front_start) / (u_dof_front_end - u_dof_front_start);
  52. #if COC_TYPE != COC_FOREGROUND
  53. else
  54. coc = (depth - u_dof_dist - u_dof_rear_start) / (u_dof_rear_end - u_dof_rear_start);
  55. #endif
  56. coc = clamp(coc, 0.0, 1.0);
  57. #if COC_TYPE == COC_COMBINE
  58. coc = max(coc, coc_fg);
  59. #endif
  60. #if COC_TYPE == COC_FOREGROUND
  61. GLSL_OUT_FRAG_COLOR = vec4(coc);
  62. #else
  63. // multiply color by coc to prevent intensity leakage
  64. GLSL_OUT_FRAG_COLOR = vec4(color.xyz * coc, coc);
  65. #endif
  66. } else
  67. GLSL_OUT_FRAG_COLOR = vec4(color.xyz, 0.0);
  68. }