CubismMath.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /**
  2. * Copyright(c) Live2D Inc. All rights reserved.
  3. *
  4. * Use of this source code is governed by the Live2D Open Software license
  5. * that can be found at https://www.live2d.com/eula/live2d-open-software-license-agreement_en.html.
  6. */
  7. using UnityEngine;
  8. namespace Live2D.Cubism.Framework.Utils
  9. {
  10. public class CubismMath
  11. {
  12. /// <summary>
  13. /// Returns the remainder of the division of two numbers.
  14. /// If invalid dividend or divisor is passed, it returns <see cref="float.NaN"/>.
  15. /// </summary>
  16. /// <param name="dividend"><see cref="Mathf.Infinity"/>, <see cref="Mathf.NegativeInfinity"/> and <see cref="float.NaN"/> are not allowed.</param>
  17. /// <param name="divisor"><see cref="float.NaN"/> and Zero is not allowed.</param>
  18. /// <returns></returns>
  19. public static float ModF(float dividend, float divisor)
  20. {
  21. if (!float.IsFinite(dividend) || Mathf.Approximately(divisor, 0)
  22. || float.IsNaN(dividend) || float.IsNaN(divisor))
  23. {
  24. Debug.LogWarning($"Invalid dividend or divisor. divided: {dividend}, divisor: {divisor} Mod() returns 'NaN'.");
  25. return float.NaN;
  26. }
  27. // Calculate the remainder of the division.
  28. return dividend % divisor;
  29. }
  30. /// <summary>
  31. /// Returns the value clamped within the specified range.
  32. /// </summary>
  33. /// <param name="val">Value to be checked within the range.</param>
  34. /// <param name="min">Minimum value.</param>
  35. /// <param name="max">Maximum value.</param>
  36. /// <returns>Clamped value within the range.</returns>
  37. public static float ClampF(float val, float min, float max)
  38. {
  39. if (val < min)
  40. {
  41. return min;
  42. }
  43. else if (max < val)
  44. {
  45. return max;
  46. }
  47. return val;
  48. }
  49. }
  50. }