circleOfConfusion.fragment.fx 817 B

12345678910111213141516171819202122232425
  1. // samplers
  2. uniform sampler2D depthSampler;
  3. // varyings
  4. varying vec2 vUV;
  5. // preconputed uniforms (not effect parameters)
  6. uniform vec2 cameraMinMaxZ;
  7. // uniforms
  8. uniform float focusDistance;
  9. uniform float cocPrecalculation;
  10. float sampleDistance(const in vec2 offset) {
  11. float depth = texture2D(depthSampler, offset).r; // depth value from DepthRenderer: 0 to 1
  12. return (cameraMinMaxZ.x + (cameraMinMaxZ.y - cameraMinMaxZ.x)*depth)*1000.0; // actual distance from the lens in scene units/1000 (eg. millimeter)
  13. }
  14. void main(void)
  15. {
  16. float pixelDistance = sampleDistance(vUV);
  17. float coc = abs(cocPrecalculation* ((focusDistance - pixelDistance)/pixelDistance));
  18. coc = clamp(coc, 0.0, 1.0);
  19. gl_FragColor = vec4(coc, texture2D(depthSampler, vUV).r, coc, 1.0);
  20. }