Mathd.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132
  1. // 'double' precision variants for some of Unity's Mathf functions.
  2. using System.Runtime.CompilerServices;
  3. namespace Mirror
  4. {
  5. public static class Mathd
  6. {
  7. // Unity 2020 doesn't have Math.Clamp yet.
  8. /// <summary>Clamps value between 0 and 1 and returns value.</summary>
  9. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  10. public static double Clamp(double value, double min, double max)
  11. {
  12. if (value < min) return min;
  13. if (value > max) return max;
  14. return value;
  15. }
  16. /// <summary>Clamps value between 0 and 1 and returns value.</summary>
  17. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  18. public static double Clamp01(double value) => Clamp(value, 0, 1);
  19. /// <summary>Calculates the linear parameter t that produces the interpolant value within the range [a, b].</summary>
  20. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  21. public static double InverseLerp(double a, double b, double value) =>
  22. a != b ? Clamp01((value - a) / (b - a)) : 0;
  23. /// <summary>Linearly interpolates between a and b by t with no limit to t.</summary>
  24. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  25. public static double LerpUnclamped(double a, double b, double t) =>
  26. a + (b - a) * t;
  27. }
  28. }