depthClamp.glsl 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. // emulated noperspective
  2. #if (__VERSION__ == 300 || defined(GL_EXT_frag_depth)) && !defined(LOG_DEPTH)
  3. out float v_WindowZ;
  4. #endif
  5. /**
  6. * Emulates GL_DEPTH_CLAMP, which is not available in WebGL 1 or 2.
  7. * GL_DEPTH_CLAMP clamps geometry that is outside the near and far planes,
  8. * capping the shadow volume. More information here:
  9. * https://www.khronos.org/registry/OpenGL/extensions/ARB/ARB_depth_clamp.txt.
  10. *
  11. * When GL_EXT_frag_depth is available we emulate GL_DEPTH_CLAMP by ensuring
  12. * no geometry gets clipped by setting the clip space z value to 0.0 and then
  13. * sending the unaltered screen space z value (using emulated noperspective
  14. * interpolation) to the frag shader where it is clamped to [0,1] and then
  15. * written with gl_FragDepth (see czm_writeDepthClamp). This technique is based on:
  16. * https://stackoverflow.com/questions/5960757/how-to-emulate-gl-depth-clamp-nv.
  17. *
  18. * When GL_EXT_frag_depth is not available, which is the case on some mobile
  19. * devices, we must attempt to fix this only in the vertex shader.
  20. * The approach is to clamp the z value to the far plane, which closes the
  21. * shadow volume but also distorts the geometry, so there can still be artifacts
  22. * on frustum seams.
  23. *
  24. * @name czm_depthClamp
  25. * @glslFunction
  26. *
  27. * @param {vec4} coords The vertex in clip coordinates.
  28. * @returns {vec4} The modified vertex.
  29. *
  30. * @example
  31. * gl_Position = czm_depthClamp(czm_modelViewProjection * vec4(position, 1.0));
  32. *
  33. * @see czm_writeDepthClamp
  34. */
  35. vec4 czm_depthClamp(vec4 coords)
  36. {
  37. #ifndef LOG_DEPTH
  38. #if __VERSION__ == 300 || defined(GL_EXT_frag_depth)
  39. v_WindowZ = (0.5 * (coords.z / coords.w) + 0.5) * coords.w;
  40. coords.z = 0.0;
  41. #else
  42. coords.z = min(coords.z, coords.w);
  43. #endif
  44. #endif
  45. return coords;
  46. }