writeLogDepth.glsl 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #ifdef LOG_DEPTH
  2. in float v_depthFromNearPlusOne;
  3. #ifdef POLYGON_OFFSET
  4. uniform vec2 u_polygonOffset;
  5. #ifdef GL_OES_standard_derivatives
  6. #extension GL_OES_standard_derivatives : enable
  7. #endif
  8. #endif
  9. #endif
  10. /**
  11. * Writes the fragment depth to the logarithmic depth buffer.
  12. * <p>
  13. * Use this when the vertex shader does not call {@link czm_vertexlogDepth}, for example, when
  14. * ray-casting geometry using a full screen quad.
  15. * </p>
  16. * @name czm_writeLogDepth
  17. * @glslFunction
  18. *
  19. * @param {float} depth The depth coordinate, where 1.0 is on the near plane and
  20. * depth increases in eye-space units from there
  21. *
  22. * @example
  23. * czm_writeLogDepth((czm_projection * v_positionEyeCoordinates).w + 1.0);
  24. */
  25. void czm_writeLogDepth(float depth)
  26. {
  27. #if (defined(LOG_DEPTH) && (__VERSION__ == 300 || defined(GL_EXT_frag_depth)))
  28. // Discard the vertex if it's not between the near and far planes.
  29. // We allow a bit of epsilon on the near plane comparison because a 1.0
  30. // from the vertex shader (indicating the vertex should be _on_ the near
  31. // plane) will not necessarily come here as exactly 1.0.
  32. if (depth <= 0.9999999 || depth > czm_farDepthFromNearPlusOne) {
  33. discard;
  34. }
  35. #ifdef POLYGON_OFFSET
  36. // Polygon offset: m * factor + r * units
  37. float factor = u_polygonOffset[0];
  38. float units = u_polygonOffset[1];
  39. #if (__VERSION__ == 300 || defined(GL_OES_standard_derivatives))
  40. // This factor doesn't work in IE 10
  41. if (factor != 0.0) {
  42. // m = sqrt(dZdX^2 + dZdY^2);
  43. float x = dFdx(depth);
  44. float y = dFdy(depth);
  45. float m = sqrt(x * x + y * y);
  46. // Apply the factor before computing the log depth.
  47. depth += m * factor;
  48. }
  49. #endif
  50. #endif
  51. gl_FragDepth = log2(depth) * czm_oneOverLog2FarDepthFromNearPlusOne;
  52. #ifdef POLYGON_OFFSET
  53. // Apply the units after the log depth.
  54. gl_FragDepth += czm_epsilon7 * units;
  55. #endif
  56. #endif
  57. }
  58. /**
  59. * Writes the fragment depth to the logarithmic depth buffer.
  60. * <p>
  61. * Use this when the vertex shader calls {@link czm_vertexlogDepth}.
  62. * </p>
  63. *
  64. * @name czm_writeLogDepth
  65. * @glslFunction
  66. */
  67. void czm_writeLogDepth() {
  68. #ifdef LOG_DEPTH
  69. czm_writeLogDepth(v_depthFromNearPlusOne);
  70. #endif
  71. }