nearFarScalar.glsl 960 B

1234567891011121314151617181920212223242526
  1. /**
  2. * Computes a value that scales with distance. The scaling is clamped at the near and
  3. * far distances, and does not extrapolate. This function works with the
  4. * {@link NearFarScalar} JavaScript class.
  5. *
  6. * @name czm_nearFarScalar
  7. * @glslFunction
  8. *
  9. * @param {vec4} nearFarScalar A vector with 4 components: Near distance (x), Near value (y), Far distance (z), Far value (w).
  10. * @param {float} cameraDistSq The square of the current distance from the camera.
  11. *
  12. * @returns {float} The value at this distance.
  13. */
  14. float czm_nearFarScalar(vec4 nearFarScalar, float cameraDistSq)
  15. {
  16. float valueAtMin = nearFarScalar.y;
  17. float valueAtMax = nearFarScalar.w;
  18. float nearDistanceSq = nearFarScalar.x * nearFarScalar.x;
  19. float farDistanceSq = nearFarScalar.z * nearFarScalar.z;
  20. float t = (cameraDistSq - nearDistanceSq) / (farDistanceSq - nearDistanceSq);
  21. t = pow(clamp(t, 0.0, 1.0), 0.2);
  22. return mix(valueAtMin, valueAtMax, t);
  23. }