kernelBlur.fragment.fx 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // Parameters
  2. uniform sampler2D textureSampler;
  3. uniform vec2 delta;
  4. // Varying
  5. varying vec2 sampleCenter;
  6. #ifdef DOF
  7. uniform sampler2D circleOfConfusionSampler;
  8. uniform vec2 cameraMinMaxZ;
  9. float sampleDistance(const in vec2 offset) {
  10. float depth = texture2D(circleOfConfusionSampler, offset).g; // depth value from DepthRenderer: 0 to 1
  11. return cameraMinMaxZ.x + (cameraMinMaxZ.y - cameraMinMaxZ.x)*depth; // actual distance from the lens
  12. }
  13. float sampleCoC(const in vec2 offset) {
  14. float coc = texture2D(circleOfConfusionSampler, offset).r;
  15. return coc; // actual distance from the lens
  16. }
  17. #endif
  18. #include<kernelBlurVaryingDeclaration>[0..varyingCount]
  19. #ifdef PACKEDFLOAT
  20. vec4 pack(float depth)
  21. {
  22. const vec4 bit_shift = vec4(255.0 * 255.0 * 255.0, 255.0 * 255.0, 255.0, 1.0);
  23. const vec4 bit_mask = vec4(0.0, 1.0 / 255.0, 1.0 / 255.0, 1.0 / 255.0);
  24. vec4 res = fract(depth * bit_shift);
  25. res -= res.xxyz * bit_mask;
  26. return res;
  27. }
  28. float unpack(vec4 color)
  29. {
  30. const vec4 bit_shift = vec4(1.0 / (255.0 * 255.0 * 255.0), 1.0 / (255.0 * 255.0), 1.0 / 255.0, 1.0);
  31. return dot(color, bit_shift);
  32. }
  33. #endif
  34. void main(void)
  35. {
  36. float computedWeight = 0.0;
  37. #ifdef PACKEDFLOAT
  38. float blend = 0.;
  39. #else
  40. vec4 blend = vec4(0.);
  41. #endif
  42. #ifdef DOF
  43. float sumOfWeights = CENTER_WEIGHT; // Since not all values are blended, keep track of sum to devide result by at the end to get an average (start at center weight as center pixel is added by default)
  44. float factor = 0.0;
  45. // Add center pixel to the blur by default
  46. #ifdef PACKEDFLOAT
  47. blend += unpack(texture2D(textureSampler, sampleCenter)) * CENTER_WEIGHT;
  48. #else
  49. blend += texture2D(textureSampler, sampleCenter) * CENTER_WEIGHT;
  50. #endif
  51. #endif
  52. #include<kernelBlurFragment>[0..varyingCount]
  53. #include<kernelBlurFragment2>[0..depCount]
  54. #ifdef PACKEDFLOAT
  55. gl_FragColor = pack(blend);
  56. #else
  57. gl_FragColor = blend;
  58. #endif
  59. #ifdef DOF
  60. gl_FragColor /= sumOfWeights;
  61. #endif
  62. }