源代码

  1. vec4 czm_windowToEyeCoordinates(vec4 fragmentCoordinate)
  2. {
  3. // 窗口坐标重建NDC坐标
  4. float x = 2.0 * (fragmentCoordinate.x - czm_viewport.x) / czm_viewport.z - 1.0;
  5. float y = 2.0 * (fragmentCoordinate.y - czm_viewport.y) / czm_viewport.w - 1.0;
  6. float z = (fragmentCoordinate.z - czm_viewportTransformation[3][2]) / czm_viewportTransformation[2][2];
  7. vec4 q = vec4(x, y, z, 1.0);
  8. // NDC坐标转裁剪(投影)坐标
  9. q /= fragmentCoordinate.w;
  10. // 裁剪(投影)坐标转y(相机)坐标
  11. if (!(czm_inverseProjection == mat4(0.0))) // IE and Edge sometimes do something weird with != between mat4s
  12. {
  13. q = czm_inverseProjection * q;
  14. }
  15. else
  16. {
  17. float top = czm_frustumPlanes.x;
  18. float bottom = czm_frustumPlanes.y;
  19. float left = czm_frustumPlanes.z;
  20. float right = czm_frustumPlanes.w;
  21. float near = czm_currentFrustum.x;
  22. float far = czm_currentFrustum.y;
  23. q.x = (q.x * (right - left) + left + right) * 0.5;
  24. q.y = (q.y * (top - bottom) + bottom + top) * 0.5;
  25. q.z = (q.z * (near - far) - near - far) * 0.5;
  26. q.w = 1.0;
  27. }
  28. return q;
  29. }
  30. vec4 czm_windowToEyeCoordinates(vec2 fragmentCoordinateXY, float depthOrLogDepth)
  31. {
  32. // See reverseLogDepth.glsl. This is separate to re-use the pow.
  33. #ifdef LOG_DEPTH
  34. float near = czm_currentFrustum.x;
  35. float far = czm_currentFrustum.y;
  36. float log2Depth = depthOrLogDepth * czm_log2FarDepthFromNearPlusOne;
  37. float depthFromNear = pow(2.0, log2Depth) - 1.0;
  38. float depthFromCamera = depthFromNear + near;
  39. vec4 windowCoord = vec4(fragmentCoordinateXY, far * (1.0 - near / depthFromCamera) / (far - near), 1.0);
  40. vec4 eyeCoordinate = czm_windowToEyeCoordinates(windowCoord);
  41. eyeCoordinate.w = 1.0 / depthFromCamera; // Better precision
  42. return eyeCoordinate;
  43. #else
  44. vec4 windowCoord = vec4(fragmentCoordinateXY, depthOrLogDepth, 1.0);
  45. vec4 eyeCoordinate = czm_windowToEyeCoordinates(windowCoord);
  46. #endif
  47. return eyeCoordinate;
  48. }

文档

该函数能将窗口坐标计算至眼坐标(也即相机坐标、观察坐标)。有两个重载。

1. 参数

重载1 ① fragmentCoordinate

参数类型:vec4

片元的窗口坐标。

重载2 ① fragmentCoordinateXY

参数类型:vec2

片元的窗口坐标,二维。

重载2 ② depthOrLogDepth

参数类型:float

(对数)深度值。

2. 返回值

返回值类型:vec4

注意事项

depthTexture 可以是对数深度纹理,也可以是非对数深度纹理。