windowToEyeCoordinates.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. //This file is automatically rebuilt by the Cesium build process.
  2. export default "/**\n\
  3. * Transforms a position from window to eye coordinates.\n\
  4. * The transform from window to normalized device coordinates is done using components\n\
  5. * of (@link czm_viewport} and {@link czm_viewportTransformation} instead of calculating\n\
  6. * the inverse of <code>czm_viewportTransformation</code>. The transformation from\n\
  7. * normalized device coordinates to clip coordinates is done using <code>fragmentCoordinate.w</code>,\n\
  8. * which is expected to be the scalar used in the perspective divide. The transformation\n\
  9. * from clip to eye coordinates is done using {@link czm_inverseProjection}.\n\
  10. *\n\
  11. * @name czm_windowToEyeCoordinates\n\
  12. * @glslFunction\n\
  13. *\n\
  14. * @param {vec4} fragmentCoordinate The position in window coordinates to transform.\n\
  15. *\n\
  16. * @returns {vec4} The transformed position in eye coordinates.\n\
  17. *\n\
  18. * @see czm_modelToWindowCoordinates\n\
  19. * @see czm_eyeToWindowCoordinates\n\
  20. * @see czm_inverseProjection\n\
  21. * @see czm_viewport\n\
  22. * @see czm_viewportTransformation\n\
  23. *\n\
  24. * @example\n\
  25. * vec4 positionEC = czm_windowToEyeCoordinates(gl_FragCoord);\n\
  26. */\n\
  27. vec4 czm_windowToEyeCoordinates(vec4 fragmentCoordinate)\n\
  28. {\n\
  29. // Reconstruct NDC coordinates\n\
  30. float x = 2.0 * (fragmentCoordinate.x - czm_viewport.x) / czm_viewport.z - 1.0;\n\
  31. float y = 2.0 * (fragmentCoordinate.y - czm_viewport.y) / czm_viewport.w - 1.0;\n\
  32. float z = (fragmentCoordinate.z - czm_viewportTransformation[3][2]) / czm_viewportTransformation[2][2];\n\
  33. vec4 q = vec4(x, y, z, 1.0);\n\
  34. \n\
  35. // Reverse the perspective division to obtain clip coordinates.\n\
  36. q /= fragmentCoordinate.w;\n\
  37. \n\
  38. // Reverse the projection transformation to obtain eye coordinates.\n\
  39. if (!(czm_inverseProjection == mat4(0.0))) // IE and Edge sometimes do something weird with != between mat4s\n\
  40. {\n\
  41. q = czm_inverseProjection * q;\n\
  42. }\n\
  43. else\n\
  44. {\n\
  45. float top = czm_frustumPlanes.x;\n\
  46. float bottom = czm_frustumPlanes.y;\n\
  47. float left = czm_frustumPlanes.z;\n\
  48. float right = czm_frustumPlanes.w;\n\
  49. \n\
  50. float near = czm_currentFrustum.x;\n\
  51. float far = czm_currentFrustum.y;\n\
  52. \n\
  53. q.x = (q.x * (right - left) + left + right) * 0.5;\n\
  54. q.y = (q.y * (top - bottom) + bottom + top) * 0.5;\n\
  55. q.z = (q.z * (near - far) - near - far) * 0.5;\n\
  56. q.w = 1.0;\n\
  57. }\n\
  58. \n\
  59. return q;\n\
  60. }\n\
  61. \n\
  62. /**\n\
  63. * Transforms a position given as window x/y and a depth or a log depth from window to eye coordinates.\n\
  64. * This function produces more accurate results for window positions with log depth than\n\
  65. * conventionally unpacking the log depth using czm_reverseLogDepth and using the standard version\n\
  66. * of czm_windowToEyeCoordinates.\n\
  67. *\n\
  68. * @name czm_windowToEyeCoordinates\n\
  69. * @glslFunction\n\
  70. *\n\
  71. * @param {vec2} fragmentCoordinateXY The XY position in window coordinates to transform.\n\
  72. * @param {float} depthOrLogDepth A depth or log depth for the fragment.\n\
  73. *\n\
  74. * @see czm_modelToWindowCoordinates\n\
  75. * @see czm_eyeToWindowCoordinates\n\
  76. * @see czm_inverseProjection\n\
  77. * @see czm_viewport\n\
  78. * @see czm_viewportTransformation\n\
  79. *\n\
  80. * @returns {vec4} The transformed position in eye coordinates.\n\
  81. */\n\
  82. vec4 czm_windowToEyeCoordinates(vec2 fragmentCoordinateXY, float depthOrLogDepth)\n\
  83. {\n\
  84. // See reverseLogDepth.glsl. This is separate to re-use the pow.\n\
  85. #ifdef LOG_DEPTH\n\
  86. float near = czm_currentFrustum.x;\n\
  87. float far = czm_currentFrustum.y;\n\
  88. float log2Depth = depthOrLogDepth * czm_log2FarDepthFromNearPlusOne;\n\
  89. float depthFromNear = pow(2.0, log2Depth) - 1.0;\n\
  90. float depthFromCamera = depthFromNear + near;\n\
  91. vec4 windowCoord = vec4(fragmentCoordinateXY, far * (1.0 - near / depthFromCamera) / (far - near), 1.0);\n\
  92. vec4 eyeCoordinate = czm_windowToEyeCoordinates(windowCoord);\n\
  93. eyeCoordinate.w = 1.0 / depthFromCamera; // Better precision\n\
  94. return eyeCoordinate;\n\
  95. #else\n\
  96. vec4 windowCoord = vec4(fragmentCoordinateXY, depthOrLogDepth, 1.0);\n\
  97. vec4 eyeCoordinate = czm_windowToEyeCoordinates(windowCoord);\n\
  98. #endif\n\
  99. return eyeCoordinate;\n\
  100. }\n\
  101. ";