-
Notifications
You must be signed in to change notification settings - Fork 6
/
Tween.h
294 lines (246 loc) · 7.2 KB
/
Tween.h
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
// Adapted from http://robertpenner.com/easing/
// BSD License http://robertpenner.com/easing_terms_of_use.html
#ifndef TWEEN_H
#define TWEEN_H
#include <cmath>
// t - time
// b - position at t0
// c - offset from t0 at t1
// d - t1
// return position at time t
typedef float (*ease_fun_t)(float t, float b, float c, float d);
inline float easeInBack(float t, float b=0.f, float c=1.f, float d=1.f)
{
float s = 1.70158f;
t /= d;
return c*t*t*((s+1)*t - s) + b;
}
inline float easeOutBack(float t, float b=0.f, float c=1.f, float d=1.f)
{
float s = 1.70158f;
t = t/d-1;
return c*(t*t*((s+1)*t + s) + 1) + b;
}
inline float easeInOutBack(float t, float b=0.f, float c=1.f, float d=1.f)
{
d /= 2;
if (t > d)
return easeOutBack(t-d, b+(c-b)/2, c/2, d);
return easeInBack(t, b, c/2, d);
}
inline float easeOutBounce(float t, float b=0.f, float c=1.f, float d=1.f)
{
if ((t/=d) < (1/2.75f)) {
return c*(7.5625f*t*t) + b;
} else if (t < (2/2.75f)) {
float postFix = t-=(1.5f/2.75f);
return c*(7.5625f*(postFix)*t + .75f) + b;
} else if (t < (2.5/2.75)) {
float postFix = t-=(2.25f/2.75f);
return c*(7.5625f*(postFix)*t + .9375f) + b;
} else {
float postFix = t-=(2.625f/2.75f);
return c*(7.5625f*(postFix)*t + .984375f) + b;
}
}
inline float easeInBounce(float t, float b=0.f, float c=1.f, float d=1.f)
{
return c - easeOutBounce(d-t, 0, c, d) + b;
}
inline float easeInOutBounce(float t, float b=0.f, float c=1.f, float d=1.f)
{
if (t < d/2) return easeInBounce(t*2, 0, c, d) * .5f + b;
else return easeOutBounce(t*2-d, 0, c, d) * .5f + c*.5f + b;
}
inline float easeInCirc(float t, float b=0.f, float c=1.f, float d=1.f)
{
t /= d;
return -c * (std::sqrt(1 - t*t) - 1) + b;
}
inline float easeOutCirc(float t, float b=0.f, float c=1.f, float d=1.f)
{
t = t/d-1;
return c * std::sqrt(1 - t*t) + b;
}
inline float easeInOutCirc(float t, float b=0.f, float c=1.f, float d=1.f)
{
if ((t/=d/2) < 1) return -c/2 * (std::sqrt(1 - t*t) - 1) + b;
t -= 2;
return c/2 * (std::sqrt(1 - t*t) + 1) + b;
}
inline float easeInCubic(float t, float b=0.f, float c=1.f, float d=1.f)
{
t /= d;
return c*t*t*t + b;
}
inline float easeOutCubic(float t, float b=0.f, float c=1.f, float d=1.f)
{
t = t/d-1;
return c*(t*t*t + 1) + b;
}
inline float easeInOutCubic(float t, float b=0.f, float c=1.f, float d=1.f)
{
if ((t/=d/2) < 1) return c/2*t*t*t + b;
t -= 2;
return c/2*(t*t*t + 2) + b;
}
inline float easeInElastic(float t, float b=0.f, float c=1.f, float d=1.f)
{
if (t==0) return b;
if ((t/=d)==1) return b+c;
float p=d*.3f;
float a=c;
float s=p/4;
float postFix =a*std::pow(2,10*(t-=1)); // this is a fix, again, with post-increment operators
return -(postFix * std::sin((t*d-s)*(2*M_PI)/p )) + b;
}
inline float easeOutElastic(float t, float b=0.f, float c=1.f, float d=1.f)
{
if (t==0) return b;
if ((t/=d)==1) return b+c;
float p=d*.3f;
float a=c;
float s=p/4;
return (a*std::pow(2,-10*t) * std::sin( (t*d-s)*(2*M_PI)/p ) + c + b);
}
inline float easeInOutElastic(float t, float b=0.f, float c=1.f, float d=1.f)
{
if (t==0) return b;
if ((t/=d/2)==2) return b+c;
float p=d*(.3f*1.5f);
float a=c;
float s=p/4;
if (t < 1) {
float postFix = a*std::pow(2,10*(t-=1)); // postIncrement is evil
return -.5f*(postFix* std::sin( (t*d-s)*(2*M_PI)/p )) + b;
}
float postFix = a*std::pow(2,-10*(t-=1)); // postIncrement is evil
return postFix * std::sin( (t*d-s)*(2*M_PI)/p )*.5f + c + b;
}
inline float easeInExpo(float t, float b=0.f, float c=1.f, float d=1.f)
{
return (t==0) ? b : c * std::pow(2, 10 * (t/d - 1)) + b;
}
inline float easeOutExpo(float t, float b=0.f, float c=1.f, float d=1.f)
{
return (t==d) ? b+c : c * (-std::pow(2, -10 * t/d) + 1) + b;
}
inline float easeInOutExpo(float t, float b=0.f, float c=1.f, float d=1.f)
{
if (t==0) return b;
if (t==d) return b+c;
if ((t/=d/2) < 1) return c/2 * std::pow(2, 10 * (t - 1)) + b;
return c/2 * (-std::pow(2, -10 * --t) + 2) + b;
}
inline float easeNoneLinear(float t, float b=0.f, float c=1.f, float d=1.f)
{
return c*t/d + b;
}
inline float easeInLinear(float t, float b=0.f, float c=1.f, float d=1.f)
{
return c*t/d + b;
}
inline float easeOutLinear(float t, float b=0.f, float c=1.f, float d=1.f)
{
return c*t/d + b;
}
inline float easeInOutLinear(float t, float b=0.f, float c=1.f, float d=1.f)
{
return c*t/d + b;
}
inline float easeInQuad(float t, float b=0.f, float c=1.f, float d=1.f)
{
t /= d;
return c*t*t + b;
}
inline float easeOutQuad(float t, float b=0.f, float c=1.f, float d=1.f)
{
t /= d;
return -c *t*(t-2) + b;
}
inline float easeInOutQuad(float t, float b=0.f, float c=1.f, float d=1.f)
{
if ((t/=d/2) < 1) return ((c/2)*(t*t)) + b;
return -c/2 * (((t-2)*(t-1)) - 1) + b;
}
inline float easeInQuart(float t, float b=0.f, float c=1.f, float d=1.f)
{
t /= d;
return c*t*t*t*t + b;
}
inline float easeOutQuart(float t, float b=0.f, float c=1.f, float d=1.f)
{
t = t/d-1;
return -c * (t*t*t*t - 1) + b;
}
inline float easeInOutQuart(float t, float b=0.f, float c=1.f, float d=1.f)
{
if ((t/=d/2) < 1) return c/2*t*t*t*t + b;
t -= 2;
return -c/2 * (t*t*t*t - 2) + b;
}
inline float easeInQuint(float t, float b=0.f, float c=1.f, float d=1.f)
{
t /= d;
return c*t*t*t*t*t + b;
}
inline float easeOutQuint(float t, float b=0.f, float c=1.f, float d=1.f)
{
t = t/d-1;
return c*(t*t*t*t*t + 1) + b;
}
inline float easeInOutQuint(float t, float b=0.f, float c=1.f, float d=1.f)
{
if ((t/=d/2) < 1) return c/2*t*t*t*t*t + b;
t -= 2;
return c/2*(t*t*t*t*t + 2) + b;
}
inline float easeInSine(float t, float b=0.f, float c=1.f, float d=1.f)
{
return -c * std::cos(t/d * (M_PI/2)) + c + b;
}
inline float easeOutSine(float t, float b=0.f, float c=1.f, float d=1.f)
{
return c * std::sin(t/d * (M_PI/2)) + b;
}
inline float easeInOutSine(float t, float b=0.f, float c=1.f, float d=1.f)
{
return -c/2 * (std::cos(M_PI*t/d) - 1) + b;
}
#define EASE_FLOAT2(NAME) inline float2 NAME(float t, float2 b, float2 c, float d=1.f) { \
return float2(NAME(t, b.x, c.x, d), NAME(t, b.y, c.y, d)); }
#define EASE_FLOAT3(NAME) inline float3 NAME(float t, float3 b, float3 c, float d=1.f) { \
return float3(NAME(t, b.x, c.x, d), NAME(t, b.y, c.y, d), NAME(t, b.z, c.z, d)); }
#define EASE_VEC(NAME) EASE_FLOAT2(NAME) EASE_FLOAT3(NAME)
EASE_VEC(easeInOutLinear)
EASE_VEC(easeInSine)
EASE_VEC(easeOutSine)
EASE_VEC(easeInOutSine)
EASE_VEC(easeInQuad)
EASE_VEC(easeOutQuad)
EASE_VEC(easeInOutQuad)
EASE_VEC(easeInCubic)
EASE_VEC(easeOutCubic)
EASE_VEC(easeInOutCubic)
EASE_VEC(easeInQuart)
EASE_VEC(easeOutQuart)
EASE_VEC(easeInOutQuart)
EASE_VEC(easeInQuint)
EASE_VEC(easeOutQuint)
EASE_VEC(easeInOutQuint)
EASE_VEC(easeInExpo)
EASE_VEC(easeOutExpo)
EASE_VEC(easeInOutExpo)
EASE_VEC(easeInCirc)
EASE_VEC(easeOutCirc)
EASE_VEC(easeInOutCirc)
EASE_VEC(easeInBack)
EASE_VEC(easeOutBack)
EASE_VEC(easeInOutBack)
EASE_VEC(easeInElastic)
EASE_VEC(easeOutElastic)
EASE_VEC(easeInOutElastic)
EASE_VEC(easeInBounce)
EASE_VEC(easeOutBounce)
EASE_VEC(easeInOutBounce)
#endif