MotionAdapters.cs
Runtime/Adapters/BuiltIn/MotionAdapters.cs
1 using System;
2 using UnityEngine;
3
4 namespace MeatMotion.Adapters
5 {
6 internal interface IProgressOnlyAdapter { }
7 // Value consists exclusively of independent float components with linear interpolation.
8 internal interface IFloatComponents : IProgressOnlyAdapter { }
9
10 internal static class IntegerInterpolation
11 {
12 internal static double Round(double value, RoundingMode mode)
13 {
14 switch (mode)
15 {
16 case RoundingMode.ToZero: return Math.Truncate(value);
17 case RoundingMode.AwayFromZero: return value < 0 ? Math.Floor(value) : Math.Ceiling(value);
18 case RoundingMode.ToNegativeInfinity: return Math.Floor(value);
19 case RoundingMode.ToPositiveInfinity: return Math.Ceiling(value);
20 default: return Math.Round(value); // Rounds midpoints to even.
21 }
22 }
23 }
24
25 public readonly struct FloatMotionAdapter : IMotionAdapter<float, NoOptions>, IFloatComponents
26 {
27 public float Evaluate(ref float startValue, ref float endValue, ref NoOptions options, in MotionEvaluationContext context)
28 => startValue + (endValue - startValue) * context.Progress;
29 }
30
31 public readonly struct DoubleMotionAdapter : IMotionAdapter<double, NoOptions>, IProgressOnlyAdapter
32 {
33 public double Evaluate(ref double startValue, ref double endValue, ref NoOptions options, in MotionEvaluationContext context)
34 => startValue + (endValue - startValue) * context.Progress;
35 }
36
37 public readonly struct IntMotionAdapter : IMotionAdapter<int, IntegerOptions>, IProgressOnlyAdapter
38 {
39 public int Evaluate(ref int startValue, ref int endValue, ref IntegerOptions options, in MotionEvaluationContext context)
40 {
41 float a = startValue, b = endValue;
42 return (int)IntegerInterpolation.Round(a + (b - a) * context.Progress, options.RoundingMode);
43 }
44 }
45
46 public readonly struct LongMotionAdapter : IMotionAdapter<long, IntegerOptions>, IProgressOnlyAdapter
47 {
48 public long Evaluate(ref long startValue, ref long endValue, ref IntegerOptions options, in MotionEvaluationContext context)
49 {
50 double a = startValue, b = endValue;
51 return (long)IntegerInterpolation.Round(a + (b - a) * context.Progress, options.RoundingMode);
52 }
53 }
54
55 public readonly struct Vector2MotionAdapter : IMotionAdapter<Vector2, NoOptions>, IFloatComponents
56 {
57 public Vector2 Evaluate(ref Vector2 startValue, ref Vector2 endValue, ref NoOptions options, in MotionEvaluationContext context)
58 => Vector2.LerpUnclamped(startValue, endValue, context.Progress);
59 }
60
61 public readonly struct Vector3MotionAdapter : IMotionAdapter<Vector3, NoOptions>, IFloatComponents
62 {
63 public Vector3 Evaluate(ref Vector3 startValue, ref Vector3 endValue, ref NoOptions options, in MotionEvaluationContext context)
64 => Vector3.LerpUnclamped(startValue, endValue, context.Progress);
65 }
66
67 public readonly struct Vector4MotionAdapter : IMotionAdapter<Vector4, NoOptions>, IFloatComponents
68 {
69 public Vector4 Evaluate(ref Vector4 startValue, ref Vector4 endValue, ref NoOptions options, in MotionEvaluationContext context)
70 => Vector4.LerpUnclamped(startValue, endValue, context.Progress);
71 }
72
73 public readonly struct QuaternionMotionAdapter : IMotionAdapter<Quaternion, NoOptions>, IProgressOnlyAdapter
74 {
75 public Quaternion Evaluate(ref Quaternion startValue, ref Quaternion endValue, ref NoOptions options, in MotionEvaluationContext context)
76 => Quaternion.LerpUnclamped(startValue, endValue, context.Progress);
77 }
78
79 public readonly struct ColorMotionAdapter : IMotionAdapter<Color, NoOptions>, IFloatComponents
80 {
81 public Color Evaluate(ref Color startValue, ref Color endValue, ref NoOptions options, in MotionEvaluationContext context)
82 => Color.LerpUnclamped(startValue, endValue, context.Progress);
83 }
84
85 public readonly struct RectMotionAdapter : IMotionAdapter<Rect, NoOptions>, IFloatComponents
86 {
87 public Rect Evaluate(ref Rect startValue, ref Rect endValue, ref NoOptions options, in MotionEvaluationContext context)
88 {
89 Vector2 position = Vector2.LerpUnclamped(startValue.position, endValue.position, context.Progress);
90 Vector2 size = Vector2.LerpUnclamped(startValue.size, endValue.size, context.Progress);
91 return new Rect(position, size);
92 }
93 }
94 }