PrimitiveMotionAdapters.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using Unity.Jobs;
  2. using Unity.Mathematics;
  3. using LitMotion;
  4. using LitMotion.Adapters;
  5. [assembly: RegisterGenericJobType(typeof(MotionUpdateJob<float, NoOptions, FloatMotionAdapter>))]
  6. [assembly: RegisterGenericJobType(typeof(MotionUpdateJob<double, NoOptions, DoubleMotionAdapter>))]
  7. [assembly: RegisterGenericJobType(typeof(MotionUpdateJob<int, IntegerOptions, IntMotionAdapter>))]
  8. [assembly: RegisterGenericJobType(typeof(MotionUpdateJob<long, IntegerOptions, LongMotionAdapter>))]
  9. namespace LitMotion.Adapters
  10. {
  11. public readonly struct FloatMotionAdapter : IMotionAdapter<float, NoOptions>
  12. {
  13. public float Evaluate(ref float startValue, ref float endValue, ref NoOptions options, in MotionEvaluationContext context)
  14. {
  15. return math.lerp(startValue, endValue, context.Progress);
  16. }
  17. }
  18. public readonly struct DoubleMotionAdapter : IMotionAdapter<double, NoOptions>
  19. {
  20. public double Evaluate(ref double startValue, ref double endValue, ref NoOptions options, in MotionEvaluationContext context)
  21. {
  22. return math.lerp(startValue, endValue, context.Progress);
  23. }
  24. }
  25. public readonly struct IntMotionAdapter : IMotionAdapter<int, IntegerOptions>
  26. {
  27. public int Evaluate(ref int startValue, ref int endValue, ref IntegerOptions options, in MotionEvaluationContext context)
  28. {
  29. var value = math.lerp(startValue, endValue, context.Progress);
  30. return options.RoundingMode switch
  31. {
  32. RoundingMode.AwayFromZero => value >= 0f ? (int)math.ceil(value) : (int)math.floor(value),
  33. RoundingMode.ToZero => (int)math.trunc(value),
  34. RoundingMode.ToPositiveInfinity => (int)math.ceil(value),
  35. RoundingMode.ToNegativeInfinity => (int)math.floor(value),
  36. _ => (int)math.round(value),
  37. };
  38. }
  39. }
  40. public readonly struct LongMotionAdapter : IMotionAdapter<long, IntegerOptions>
  41. {
  42. public long Evaluate(ref long startValue, ref long endValue, ref IntegerOptions options, in MotionEvaluationContext context)
  43. {
  44. var value = math.lerp((double)startValue, endValue, context.Progress);
  45. return options.RoundingMode switch
  46. {
  47. RoundingMode.AwayFromZero => value >= 0f ? (long)math.ceil(value) : (long)math.floor(value),
  48. RoundingMode.ToZero => (long)math.trunc(value),
  49. RoundingMode.ToPositiveInfinity => (long)math.ceil(value),
  50. RoundingMode.ToNegativeInfinity => (long)math.floor(value),
  51. _ => (long)math.round(value),
  52. };
  53. }
  54. }
  55. }