-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEasing.cpp
More file actions
341 lines (302 loc) · 7.79 KB
/
Copy pathEasing.cpp
File metadata and controls
341 lines (302 loc) · 7.79 KB
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#include "Easing.h"
#include "DekiMath.h"
#include <cmath>
namespace deki {
namespace Ease {
// Easing math runs in plain float. The engine convention is radians for all
// angular arguments, so trig functions get their arguments in radians directly
// (e.g. "t * pi" stays "t * DekiMath::kPi", "t * pi/2" becomes "t * kHalfPi").
float Linear(float t)
{
return t;
}
// Sine
float SineIn(float t)
{
// 1 - cos((pi/2) * t)
return 1.0f - std::cos(t * DekiMath::kHalfPi);
}
float SineOut(float t)
{
return std::sin(t * DekiMath::kHalfPi);
}
float SineInOut(float t)
{
// (1 - cos(pi*t)) / 2
return (1.0f - std::cos(t * DekiMath::kPi)) * 0.5f;
}
// Quad
float QuadIn(float t)
{
return t * t;
}
float QuadOut(float t)
{
float inv = 1.0f - t;
return 1.0f - inv * inv;
}
float QuadInOut(float t)
{
if (t < 0.5f)
{
return 2.0f * t * t;
}
float base = 2.0f - 2.0f * t;
return 1.0f - (base * base) * 0.5f;
}
// Cubic
float CubicIn(float t)
{
return t * t * t;
}
float CubicOut(float t)
{
float inv = 1.0f - t;
return 1.0f - inv * inv * inv;
}
float CubicInOut(float t)
{
if (t < 0.5f)
{
return 4.0f * t * t * t;
}
float base = 2.0f - 2.0f * t;
return 1.0f - (base * base * base) * 0.5f;
}
// Quart
float QuartIn(float t)
{
float t2 = t * t;
return t2 * t2;
}
float QuartOut(float t)
{
float inv = 1.0f - t;
float inv2 = inv * inv;
return 1.0f - inv2 * inv2;
}
float QuartInOut(float t)
{
if (t < 0.5f)
{
float t2 = t * t;
return 8.0f * t2 * t2;
}
float base = 2.0f - 2.0f * t;
float base2 = base * base;
return 1.0f - (base2 * base2) * 0.5f;
}
// Quint
float QuintIn(float t)
{
float t2 = t * t;
return t2 * t2 * t;
}
float QuintOut(float t)
{
float inv = 1.0f - t;
float inv2 = inv * inv;
return 1.0f - inv2 * inv2 * inv;
}
float QuintInOut(float t)
{
if (t < 0.5f)
{
float t2 = t * t;
return 16.0f * t2 * t2 * t;
}
float base = 2.0f - 2.0f * t;
float base2 = base * base;
return 1.0f - (base2 * base2 * base) * 0.5f;
}
// Expo
float ExpoIn(float t)
{
if (t == 0.0f) return 0.0f;
return std::pow(2.0f, 10.0f * t - 10.0f);
}
float ExpoOut(float t)
{
if (t == 1.0f) return 1.0f;
return 1.0f - std::pow(2.0f, -10.0f * t);
}
float ExpoInOut(float t)
{
if (t == 0.0f) return 0.0f;
if (t == 1.0f) return 1.0f;
if (t < 0.5f)
{
return std::pow(2.0f, 20.0f * t - 10.0f) * 0.5f;
}
return (2.0f - std::pow(2.0f, 10.0f - 20.0f * t)) * 0.5f;
}
// Circ
float CircIn(float t)
{
return 1.0f - std::sqrt(1.0f - t * t);
}
float CircOut(float t)
{
float tm1 = t - 1.0f;
return std::sqrt(1.0f - tm1 * tm1);
}
float CircInOut(float t)
{
if (t < 0.5f)
{
float twoT = 2.0f * t;
return (1.0f - std::sqrt(1.0f - twoT * twoT)) * 0.5f;
}
float base = 2.0f - 2.0f * t;
return (std::sqrt(1.0f - base * base) + 1.0f) * 0.5f;
}
// Back constants (Penner: c1 = 1.70158, c2 = c1 * 1.525, c3 = c1 + 1).
static constexpr float kBack_c1 = 1.70158f;
static constexpr float kBack_c2 = 1.70158f * 1.525f;
static constexpr float kBack_c3 = 1.70158f + 1.0f;
float BackIn(float t)
{
// c3*t^3 - c1*t^2
float t2 = t * t;
return kBack_c3 * t2 * t - kBack_c1 * t2;
}
float BackOut(float t)
{
// 1 + c3*(t-1)^3 + c1*(t-1)^2
float tm1 = t - 1.0f;
float tm1_2 = tm1 * tm1;
return 1.0f + kBack_c3 * tm1_2 * tm1 + kBack_c1 * tm1_2;
}
float BackInOut(float t)
{
if (t < 0.5f)
{
float twoT = 2.0f * t;
return (twoT * twoT * ((kBack_c2 + 1.0f) * twoT - kBack_c2)) * 0.5f;
}
float base = 2.0f * t - 2.0f;
return (base * base * ((kBack_c2 + 1.0f) * (t * 2.0f - 2.0f) + kBack_c2) + 2.0f) * 0.5f;
}
// Elastic. Penner uses sin arguments in radians: (2pi)/3 and (2pi)/4.5.
static constexpr float kElastic_c4Rad = 2.0f * DekiMath::kPi / 3.0f;
static constexpr float kElastic_c5Rad = 2.0f * DekiMath::kPi / 4.5f;
float ElasticIn(float t)
{
if (t == 0.0f) return 0.0f;
if (t == 1.0f) return 1.0f;
float amp = std::pow(2.0f, 10.0f * t - 10.0f);
float phaseRad = (t * 10.0f - 10.75f) * kElastic_c4Rad;
return -amp * std::sin(phaseRad);
}
float ElasticOut(float t)
{
if (t == 0.0f) return 0.0f;
if (t == 1.0f) return 1.0f;
float amp = std::pow(2.0f, -10.0f * t);
float phaseRad = (t * 10.0f - 0.75f) * kElastic_c4Rad;
return amp * std::sin(phaseRad) + 1.0f;
}
float ElasticInOut(float t)
{
if (t == 0.0f) return 0.0f;
if (t == 1.0f) return 1.0f;
float phaseRad = (20.0f * t - 11.125f) * kElastic_c5Rad;
float s = std::sin(phaseRad);
if (t < 0.5f)
{
float amp = std::pow(2.0f, 20.0f * t - 10.0f);
return -(amp * s) * 0.5f;
}
float amp = std::pow(2.0f, 10.0f - 20.0f * t);
return (amp * s) * 0.5f + 1.0f;
}
// Bounce. n1 = 7.5625, d1 = 2.75. Splits 0..1 into four bounces.
static constexpr float kBounce_n1 = 7.5625f;
static constexpr float kBounce_t1 = 1.0f / 2.75f;
static constexpr float kBounce_t2 = 2.0f / 2.75f;
static constexpr float kBounce_t3 = 2.5f / 2.75f;
static constexpr float kBounce_o1 = 1.5f / 2.75f;
static constexpr float kBounce_o2 = 2.25f / 2.75f;
static constexpr float kBounce_o3 = 2.625f / 2.75f;
static constexpr float kBounce_b2 = 0.75f;
static constexpr float kBounce_b3 = 0.9375f;
static constexpr float kBounce_b4 = 0.984375f;
static float BounceOutImpl(float t)
{
if (t < kBounce_t1)
{
return kBounce_n1 * t * t;
}
else if (t < kBounce_t2)
{
float tt = t - kBounce_o1;
return kBounce_n1 * tt * tt + kBounce_b2;
}
else if (t < kBounce_t3)
{
float tt = t - kBounce_o2;
return kBounce_n1 * tt * tt + kBounce_b3;
}
else
{
float tt = t - kBounce_o3;
return kBounce_n1 * tt * tt + kBounce_b4;
}
}
float BounceIn(float t)
{
return 1.0f - BounceOutImpl(1.0f - t);
}
float BounceOut(float t)
{
return BounceOutImpl(t);
}
float BounceInOut(float t)
{
if (t < 0.5f)
{
return (1.0f - BounceOutImpl(1.0f - 2.0f * t)) * 0.5f;
}
return (1.0f + BounceOutImpl(2.0f * t - 1.0f)) * 0.5f;
}
EasingFunc GetFunction(EaseType type)
{
switch (type)
{
case EaseType::Linear: return Linear;
case EaseType::SineIn: return SineIn;
case EaseType::SineOut: return SineOut;
case EaseType::SineInOut: return SineInOut;
case EaseType::QuadIn: return QuadIn;
case EaseType::QuadOut: return QuadOut;
case EaseType::QuadInOut: return QuadInOut;
case EaseType::CubicIn: return CubicIn;
case EaseType::CubicOut: return CubicOut;
case EaseType::CubicInOut: return CubicInOut;
case EaseType::QuartIn: return QuartIn;
case EaseType::QuartOut: return QuartOut;
case EaseType::QuartInOut: return QuartInOut;
case EaseType::QuintIn: return QuintIn;
case EaseType::QuintOut: return QuintOut;
case EaseType::QuintInOut: return QuintInOut;
case EaseType::ExpoIn: return ExpoIn;
case EaseType::ExpoOut: return ExpoOut;
case EaseType::ExpoInOut: return ExpoInOut;
case EaseType::CircIn: return CircIn;
case EaseType::CircOut: return CircOut;
case EaseType::CircInOut: return CircInOut;
case EaseType::BackIn: return BackIn;
case EaseType::BackOut: return BackOut;
case EaseType::BackInOut: return BackInOut;
case EaseType::ElasticIn: return ElasticIn;
case EaseType::ElasticOut: return ElasticOut;
case EaseType::ElasticInOut: return ElasticInOut;
case EaseType::BounceIn: return BounceIn;
case EaseType::BounceOut: return BounceOut;
case EaseType::BounceInOut: return BounceInOut;
default:
return Linear;
}
}
} // namespace Ease
} // namespace deki