vertexLogDepth.glsl 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #ifdef LOG_DEPTH
  2. // 1.0 at the near plane, increasing linearly from there.
  3. out float v_depthFromNearPlusOne;
  4. #ifdef SHADOW_MAP
  5. out vec3 v_logPositionEC;
  6. #endif
  7. #endif
  8. vec4 czm_updatePositionDepth(vec4 coords) {
  9. #if defined(LOG_DEPTH)
  10. #ifdef SHADOW_MAP
  11. vec3 logPositionEC = (czm_inverseProjection * coords).xyz;
  12. v_logPositionEC = logPositionEC;
  13. #endif
  14. // With the very high far/near ratios used with the logarithmic depth
  15. // buffer, floating point rounding errors can cause linear depth values
  16. // to end up on the wrong side of the far plane, even for vertices that
  17. // are really nowhere near it. Since we always write a correct logarithmic
  18. // depth value in the fragment shader anyway, we just need to make sure
  19. // such errors don't cause the primitive to be clipped entirely before
  20. // we even get to the fragment shader.
  21. coords.z = clamp(coords.z / coords.w, -1.0, 1.0) * coords.w;
  22. #endif
  23. return coords;
  24. }
  25. /**
  26. * Writes the logarithmic depth to gl_Position using the already computed gl_Position.
  27. *
  28. * @name czm_vertexLogDepth
  29. * @glslFunction
  30. */
  31. void czm_vertexLogDepth()
  32. {
  33. #ifdef LOG_DEPTH
  34. v_depthFromNearPlusOne = (gl_Position.w - czm_currentFrustum.x) + 1.0;
  35. gl_Position = czm_updatePositionDepth(gl_Position);
  36. #endif
  37. }
  38. /**
  39. * Writes the logarithmic depth to gl_Position using the provided clip coordinates.
  40. * <p>
  41. * An example use case for this function would be moving the vertex in window coordinates
  42. * before converting back to clip coordinates. Use the original vertex clip coordinates.
  43. * </p>
  44. * @name czm_vertexLogDepth
  45. * @glslFunction
  46. *
  47. * @param {vec4} clipCoords The vertex in clip coordinates.
  48. *
  49. * @example
  50. * czm_vertexLogDepth(czm_projection * vec4(positionEyeCoordinates, 1.0));
  51. */
  52. void czm_vertexLogDepth(vec4 clipCoords)
  53. {
  54. #ifdef LOG_DEPTH
  55. v_depthFromNearPlusOne = (clipCoords.w - czm_currentFrustum.x) + 1.0;
  56. czm_updatePositionDepth(clipCoords);
  57. #endif
  58. }