CurveEvaluateService.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. using UnityEngine;
  2. using UnityEngine.Assertions;
  3. namespace UnityUIPlayables
  4. {
  5. internal sealed class CurveEvaluateService
  6. {
  7. internal float EvaluateRepeat(Curve curve, float time, float loopDuration)
  8. {
  9. Assert.IsNotNull(curve);
  10. Assert.IsTrue(time >= 0);
  11. Assert.IsTrue(loopDuration > 0);
  12. var progress = time / loopDuration;
  13. var repeatedProgress = Math.RepeatWithLargerBoundaryValue(progress, 1.0f);
  14. var evaluatedProgress = curve.Evaluate(repeatedProgress);
  15. return evaluatedProgress;
  16. }
  17. internal float EvaluateReverse(Curve curve, float time, float loopDuration)
  18. {
  19. Assert.IsNotNull(curve);
  20. Assert.IsTrue(time >= 0);
  21. Assert.IsTrue(loopDuration > 0);
  22. var progress = time / loopDuration;
  23. var reverse = Mathf.Floor(progress) % 2 != 0;
  24. var repeatedProgress = Math.RepeatWithLargerBoundaryValue(progress, 1.0f);
  25. var evaluatedProgress = curve.Evaluate(repeatedProgress);
  26. if (reverse)
  27. evaluatedProgress = 1 - evaluatedProgress;
  28. return evaluatedProgress;
  29. }
  30. internal float EvaluatePingPong(Curve curve, float time, float loopDuration)
  31. {
  32. Assert.IsNotNull(curve);
  33. Assert.IsTrue(time >= 0);
  34. Assert.IsTrue(loopDuration > 0);
  35. var progress = time / loopDuration;
  36. var repeatedProgress = Mathf.PingPong(progress, 1.0f);
  37. var evaluatedProgress = curve.Evaluate(repeatedProgress);
  38. return evaluatedProgress;
  39. }
  40. }
  41. }