metersPerPixel.glsl 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /**
  2. * Computes the size of a pixel in meters at a distance from the eye.
  3. * <p>
  4. * Use this version when passing in a custom pixel ratio. For example, passing in 1.0 will return meters per native device pixel.
  5. * </p>
  6. * @name czm_metersPerPixel
  7. * @glslFunction
  8. *
  9. * @param {vec3} positionEC The position to get the meters per pixel in eye coordinates.
  10. * @param {float} pixelRatio The scaling factor from pixel space to coordinate space
  11. *
  12. * @returns {float} The meters per pixel at positionEC.
  13. */
  14. float czm_metersPerPixel(vec4 positionEC, float pixelRatio)
  15. {
  16. float width = czm_viewport.z;
  17. float height = czm_viewport.w;
  18. float pixelWidth;
  19. float pixelHeight;
  20. float top = czm_frustumPlanes.x;
  21. float bottom = czm_frustumPlanes.y;
  22. float left = czm_frustumPlanes.z;
  23. float right = czm_frustumPlanes.w;
  24. if (czm_sceneMode == czm_sceneMode2D || czm_orthographicIn3D == 1.0)
  25. {
  26. float frustumWidth = right - left;
  27. float frustumHeight = top - bottom;
  28. pixelWidth = frustumWidth / width;
  29. pixelHeight = frustumHeight / height;
  30. }
  31. else
  32. {
  33. float distanceToPixel = -positionEC.z;
  34. float inverseNear = 1.0 / czm_currentFrustum.x;
  35. float tanTheta = top * inverseNear;
  36. pixelHeight = 2.0 * distanceToPixel * tanTheta / height;
  37. tanTheta = right * inverseNear;
  38. pixelWidth = 2.0 * distanceToPixel * tanTheta / width;
  39. }
  40. return max(pixelWidth, pixelHeight) * pixelRatio;
  41. }
  42. /**
  43. * Computes the size of a pixel in meters at a distance from the eye.
  44. * <p>
  45. * Use this version when scaling by pixel ratio.
  46. * </p>
  47. * @name czm_metersPerPixel
  48. * @glslFunction
  49. *
  50. * @param {vec3} positionEC The position to get the meters per pixel in eye coordinates.
  51. *
  52. * @returns {float} The meters per pixel at positionEC.
  53. */
  54. float czm_metersPerPixel(vec4 positionEC)
  55. {
  56. return czm_metersPerPixel(positionEC, czm_pixelRatio);
  57. }