writeLogDepth.glsl 2.0 KB

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