-
Notifications
You must be signed in to change notification settings - Fork 11
/
Easing.cs
218 lines (204 loc) · 8.65 KB
/
Easing.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Sniper.Lighting.DMX
{
public static class Easing
{
// Adapted from source : http://www.robertpenner.com/easing/
public static float Ease(double linearStep, float acceleration, EasingType type)
{
float easedStep = acceleration > 0 ? EaseIn(linearStep, type) :
acceleration < 0 ? EaseOut(linearStep, type) :
(float)linearStep;
return MathHelper.Lerp(linearStep, easedStep, Math.Abs(acceleration));
}
public static float EaseIn(double linearStep, EasingType type)
{
switch (type)
{
case EasingType.Step: return linearStep < 0.5 ? 0 : 1;
case EasingType.Linear: return (float)linearStep;
case EasingType.Sine: return Sine.EaseIn(linearStep);
case EasingType.Quadratic: return Power.EaseIn(linearStep, 2);
case EasingType.Cubic: return Power.EaseIn(linearStep, 3);
case EasingType.Quartic: return Power.EaseIn(linearStep, 4);
case EasingType.Quintic: return Power.EaseIn(linearStep, 5);
//case EasingType.Bounce: return 0;
//case EasingType.Elastic: return 0;
}
throw new NotImplementedException();
}
public static float EaseOut(double linearStep, EasingType type)
{
switch (type)
{
case EasingType.Step: return linearStep < 0.5 ? 0 : 1;
case EasingType.Linear: return (float)linearStep;
case EasingType.Sine: return Sine.EaseOut(linearStep);
case EasingType.Quadratic: return Power.EaseOut(linearStep, 2);
case EasingType.Cubic: return Power.EaseOut(linearStep, 3);
case EasingType.Quartic: return Power.EaseOut(linearStep, 4);
case EasingType.Quintic: return Power.EaseOut(linearStep, 5);
//case EasingType.Bounce: return 0;
//case EasingType.Elastic: return 0;
}
throw new NotImplementedException();
}
public static float EaseInOut(double linearStep, EasingType easeInType, EasingType easeOutType)
{
return linearStep < 0.5 ? EaseInOut(linearStep, easeInType) : EaseInOut(linearStep, easeOutType);
}
public static float EaseInOut(double linearStep, EasingType type)
{
switch (type)
{
case EasingType.Step: return linearStep < 0.5 ? 0 : 1;
case EasingType.Linear: return (float)linearStep;
case EasingType.Sine: return Sine.EaseInOut(linearStep);
case EasingType.Quadratic: return Power.EaseInOut(linearStep, 2);
case EasingType.Cubic: return Power.EaseInOut(linearStep, 3);
case EasingType.Quartic: return Power.EaseInOut(linearStep, 4);
case EasingType.Quintic: return Power.EaseInOut(linearStep, 5);
//case EasingType.Bounce: return Bounce.EaseInOut(linearStep, begin, delta, duration);
//case EasingType.Elastic: return 0;
}
throw new NotImplementedException();
}
static class Sine
{
public static float EaseIn(double s)
{
return (float)Math.Sin(s * MathHelper.HalfPi - MathHelper.HalfPi) + 1;
}
public static float EaseOut(double s)
{
return (float)Math.Sin(s * MathHelper.HalfPi);
}
public static float EaseInOut(double s)
{
return (float)(Math.Sin(s * MathHelper.Pi - MathHelper.HalfPi) + 1) / 2;
}
}
static class Power
{
public static float EaseIn(double s, int power)
{
return (float)Math.Pow(s, power);
}
public static float EaseOut(double s, int power)
{
var sign = power % 2 == 0 ? -1 : 1;
return (float)(sign * (Math.Pow(s - 1, power) + sign));
}
public static float EaseInOut(double s, int power)
{
s *= 2;
if (s < 1) return EaseIn(s, power) / 2;
var sign = power % 2 == 0 ? -1 : 1;
return (float)(sign / 2.0 * (Math.Pow(s - 2, power) + sign * 2));
}
}
//static class Elastic
//{
// //time=current step, begin=start value, change=delta (amount of change e.g. start@50, end@95 = 45), duration=number of steps
// public static float EaseIn(double t, double b, double c, double d, double a, double p)
// {
// if (t == 0)
// return (float)b;
// if ((t /= d) == 1)
// return (float)(b + c);
// if (p == 0)
// p = d * .3;
// float s = 0;
// if (a == 0 || a < Math.Abs(c))
// {
// a = c;
// s = (float)(p / 4);
// }
// else
// {
// s = (float)(p / (2 * Math.PI) * Math.Asin(c / a));
// }
// return (float)(-(a * Math.Pow(2, 10 * (t -= 1)) * Math.Sin((t * d - s) * (2 * Math.PI) / p)) + b);
// }
// public static float EaseOut(double t, double b, double c, double d, double a, double p)
// {
// if (t == 0)
// return (float)b;
// if ((t /= d) == 1)
// return (float)(b + c);
// if (p == 0)
// p = d * .3;
// float s = 0;
// if (a == 0 || a < Math.Abs(c))
// {
// a = c;
// s = (float)(p / 4);
// }
// else
// {
// s = (float)(p / (2 * Math.PI) * Math.Asin(c / a));
// }
// return (float)(a * Math.Pow(2, -10 * t) * Math.Sin((t * d - s) * (2 * Math.PI) / p) + c + b);
// }
// public static float EaseInOut(double t, double b, double c, double d, double a, double p)
// {
// if (t == 0)
// return (float)(b);
// if ((t /= d / 2) == 2)
// return (float)(b + c);
// if (p == 0)
// p = d * (.3 * 1.5);
// float s = 0;
// if (a == 0 || a < Math.Abs(c))
// {
// a = c;
// s = (float)(p / 4);
// }
// else
// {
// s = (float)(p / (2 * Math.PI) * Math.Asin(c / a));
// }
// if (t < 1)
// {
// return (float)(-.5 * (a * Math.Pow(2, 10 * (t -= 1)) * Math.Sin((t * d - s) * (2 * Math.PI) / p)) + b);
// }
// return (float)(a * Math.Pow(2, -10 * (t -= 1)) * Math.Sin((t * d - s) * (2 * Math.PI) / p) * .5 + c + b);
// }
//}
//static class Bounce
//{
// //time=current step, begin=start value, change=delta (amount of change e.g. start@50, end@95 = 45), duration=number of steps
// public static float EaseIn(double t, double b, double c, double d)
// {
// return (float)(c - Bounce.EaseOut(d - t, 0, c, d) + b);
// }
// public static float EaseOut(double t, double b, double c, double d)
// {
// if ((t /= d) < (1 / 2.75))
// {
// return (float)(c * (7.5625 * t * t) + b);
// }
// else if (t < (2 / 2.75))
// {
// return (float)(c * (7.5625 * (t -= (1.5 / 2.75)) * t + .75) + b);
// }
// else if (t < (2.5 / 2.75))
// {
// return (float)(c * (7.5625 * (t -= (2.25 / 2.75)) * t + .9375) + b);
// }
// else
// {
// return (float)(c * (7.5625 * (t -= (2.625 / 2.75)) * t + .984375) + b);
// }
// }
// public static float EaseInOut(double t, double b, double c, double d)
// {
// if (t < d / 2) return (float)(Bounce.EaseIn(t * 2, 0, c, d) * .5 + b);
// else return (float)(Bounce.EaseOut(t * 2 - d, 0, c, d) * .5 + c * .5 + b);
// }
//}
}
}