windowToEyeCoordinates.js 4.7 KB

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