DepthOfField.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. //This file is automatically rebuilt by the Cesium build process.
  2. export default "uniform sampler2D colorTexture;\n\
  3. uniform sampler2D blurTexture;\n\
  4. uniform sampler2D depthTexture;\n\
  5. uniform float focalDistance;\n\
  6. varying vec2 v_textureCoordinates;\n\
  7. vec4 toEye(vec2 uv, float depth)\n\
  8. {\n\
  9. vec2 xy = vec2((uv.x * 2.0 - 1.0), ((1.0 - uv.y) * 2.0 - 1.0));\n\
  10. vec4 posInCamera = czm_inverseProjection * vec4(xy, depth, 1.0);\n\
  11. posInCamera = posInCamera / posInCamera.w;\n\
  12. return posInCamera;\n\
  13. }\n\
  14. float computeDepthBlur(float depth)\n\
  15. {\n\
  16. float f;\n\
  17. if (depth < focalDistance)\n\
  18. {\n\
  19. f = (focalDistance - depth) / (focalDistance - czm_currentFrustum.x);\n\
  20. }\n\
  21. else\n\
  22. {\n\
  23. f = (depth - focalDistance) / (czm_currentFrustum.y - focalDistance);\n\
  24. f = pow(f, 0.1);\n\
  25. }\n\
  26. f *= f;\n\
  27. f = clamp(f, 0.0, 1.0);\n\
  28. return pow(f, 0.5);\n\
  29. }\n\
  30. void main(void)\n\
  31. {\n\
  32. float depth = czm_readDepth(depthTexture, v_textureCoordinates);\n\
  33. vec4 posInCamera = toEye(v_textureCoordinates, depth);\n\
  34. float d = computeDepthBlur(-posInCamera.z);\n\
  35. gl_FragColor = mix(texture2D(colorTexture, v_textureCoordinates), texture2D(blurTexture, v_textureCoordinates), d);\n\
  36. }\n\
  37. ";