vertexLogDepth.js 2.2 KB

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