Easing.cs
Runtime/Curves/Easing.cs
1 using System;
2 using System.Runtime.CompilerServices;
3 using UnityEngine;
4
5 namespace MeatMotion
6 {
7 public enum Easing
8 {
9 // Keep the numeric values of the original two curves for serialized users.
10 Linear, OutQuad, InQuad, InOutQuad, InCubic, OutCubic, InOutCubic,
11 InQuart, OutQuart, InOutQuart, InQuint, OutQuint, InOutQuint,
12 InSine, OutSine, InOutSine, InExpo, OutExpo, InOutExpo,
13 InCirc, OutCirc, InOutCirc, InBack, OutBack, InOutBack,
14 InElastic, OutElastic, InOutElastic, InBounce, OutBounce, InOutBounce,
15 CustomAnimationCurve
16 }
17
18 public static partial class EaseUtility
19 {
20 internal static void CheckBuiltin(Easing ease)
21 {
22 if ((uint)ease >= (uint)Easing.CustomAnimationCurve)
23 throw new ArgumentOutOfRangeException(nameof(ease));
24 }
25
26 [MethodImpl(MethodImplOptions.AggressiveInlining)]
27 public static float Evaluate(float t, Easing ease)
28 {
29 if (ease == Easing.Linear) return t;
30 if (ease == Easing.OutQuad) return t * (2f - t);
31 return EvaluateOther(t, ease);
32 }
33
34 static float EvaluateOther(float t, Easing ease)
35 {
36 float u = 1f - t;
37 bool first = t < 0.5f;
38 float x = 2f * (first ? t : u);
39 switch (ease)
40 {
41 case Easing.InQuad: return t * t;
42 case Easing.InOutQuad: return Mirror(x * x, first);
43 case Easing.InCubic: return Cube(t);
44 case Easing.OutCubic: return 1f - Cube(u);
45 case Easing.InOutCubic: return Mirror(Cube(x), first);
46 case Easing.InQuart: return Fourth(t);
47 case Easing.OutQuart: return 1f - Fourth(u);
48 case Easing.InOutQuart: return Mirror(Fourth(x), first);
49 case Easing.InQuint: return Fourth(t) * t;
50 case Easing.OutQuint: return 1f - Fourth(u) * u;
51 case Easing.InOutQuint: return Mirror(Fourth(x) * x, first);
52 case Easing.InSine: return 1f - Mathf.Cos(t * (Mathf.PI * 0.5f));
53 case Easing.OutSine: return Mathf.Sin(t * (Mathf.PI * 0.5f));
54 case Easing.InOutSine: return (1f - Mathf.Cos(t * Mathf.PI)) * 0.5f;
55 case Easing.InCirc: return 1f - Mathf.Sqrt(1f - t * t);
56 case Easing.OutCirc: return Mathf.Sqrt(1f - u * u);
57 case Easing.InOutCirc: return Mirror(1f - Mathf.Sqrt(1f - x * x), first);
58 case Easing.InExpo: return Expo(t);
59 case Easing.OutExpo: return 1f - Expo(u);
60 case Easing.InOutExpo: return Mirror(Expo(x), first);
61 case Easing.InBack: return Back(t, 1.70158f);
62 case Easing.OutBack: return 1f - Back(u, 1.70158f);
63 case Easing.InOutBack: return Mirror(Back(x, 1.70158f * 1.525f), first);
64 case Easing.InElastic: return Elastic(t, 0.3f);
65 case Easing.OutElastic: return 1f - Elastic(u, 0.3f);
66 case Easing.InOutElastic: return Mirror(Elastic(x, 0.45f), first);
67 case Easing.InBounce: return 1f - Bounce(u);
68 case Easing.OutBounce: return Bounce(t);
69 case Easing.InOutBounce: return Mirror(1f - Bounce(1f - x), first);
70 default: return t;
71 }
72 }
73
74 static float Mirror(float value, bool first) => first ? value * 0.5f : 1f - value * 0.5f;
75 static float Cube(float x) => x * x * x;
76 static float Fourth(float x) => (x * x) * (x * x);
77 static float Expo(float x) => x == 0f ? 0f : Mathf.Pow(2f, (x - 1f) * 10f);
78 static float Back(float x, float overshoot) => x * x * ((overshoot + 1f) * x - overshoot);
79 static float Elastic(float x, float period)
80 {
81 if (x == 0f || x == 1f) return x;
82 return Expo(x) * Mathf.Cos((x - 1f) * (2f * Mathf.PI / period));
83 }
84
85 static float Bounce(float t)
86 {
87 float x = 2.75f * t;
88 if (x < 1f) return x * x;
89 if (x < 2f) { x -= 1.5f; return x * x + 0.75f; }
90 if (x < 2.5f) { x -= 2.25f; return x * x + 0.9375f; }
91 x -= 2.625f;
92 return x * x + 0.984375f;
93 }
94 }
95 }