-
Notifications
You must be signed in to change notification settings - Fork 0
/
rjd_easing.h
277 lines (234 loc) · 8.28 KB
/
rjd_easing.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
#pragma once
#define RJD_EASING_H 1
enum rjd_ease_type
{
RJD_EASE_TYPE_LINE, // linear
RJD_EASE_TYPE_SINE, // sine
RJD_EASE_TYPE_CUBE, // cubic
RJD_EASE_TYPE_QUAD, // quadratic
RJD_EASE_TYPE_QUAR, // quartic
RJD_EASE_TYPE_QUIN, // quintic
RJD_EASE_TYPE_EXPO, // exponential
RJD_EASE_TYPE_CIRC, // circular
RJD_EASE_TYPE_BACK, // back
RJD_EASE_TYPE_ELAS, // elastic
RJD_EASE_TYPE_BOUN, // bounce
RJD_EASE_TYPE_MAX,
};
enum rjd_ease_dir
{
RJD_EASE_DIR_INOUT,
RJD_EASE_DIR_IN,
RJD_EASE_DIR_OUT,
RJD_EASE_DIR_MAX,
};
typedef float (rjd_ease_func)(float t);
static inline float rjd_ease(float t, enum rjd_ease_type type, enum rjd_ease_dir dir);
static inline float rjd_ease_between(float t, float min, float max, rjd_ease_func* f);
static inline float rjd_ease_line(float t);
static inline float rjd_ease_in_sine(float t);
static inline float rjd_ease_in_quad(float t);
static inline float rjd_ease_in_cube(float t);
static inline float rjd_ease_in_quar(float t);
static inline float rjd_ease_in_quin(float t);
static inline float rjd_ease_in_expo(float t);
static inline float rjd_ease_in_circ(float t);
static inline float rjd_ease_in_back(float t);
static inline float rjd_ease_in_elas(float t);
static inline float rjd_ease_in_boun(float t);
static inline float rjd_ease_out_sine(float t);
static inline float rjd_ease_out_quad(float t);
static inline float rjd_ease_out_cube(float t);
static inline float rjd_ease_out_quar(float t);
static inline float rjd_ease_out_quin(float t);
static inline float rjd_ease_out_expo(float t);
static inline float rjd_ease_out_circ(float t);
static inline float rjd_ease_out_back(float t);
static inline float rjd_ease_out_elas(float t);
static inline float rjd_ease_out_boun(float t);
static inline float rjd_ease_inout_sine(float t);
static inline float rjd_ease_inout_quad(float t);
static inline float rjd_ease_inout_cube(float t);
static inline float rjd_ease_inout_quar(float t);
static inline float rjd_ease_inout_quin(float t);
static inline float rjd_ease_inout_expo(float t);
static inline float rjd_ease_inout_circ(float t);
static inline float rjd_ease_inout_back(float t);
static inline float rjd_ease_inout_elas(float t);
static inline float rjd_ease_inout_boun(float t);
// impl
static inline float rjd_ease(float t, enum rjd_ease_type type, enum rjd_ease_dir dir)
{
switch (dir) {
case RJD_EASE_DIR_IN: {
switch(type) {
case RJD_EASE_TYPE_LINE: return rjd_ease_line(t);
case RJD_EASE_TYPE_SINE: return rjd_ease_in_sine(t);
case RJD_EASE_TYPE_CUBE: return rjd_ease_in_cube(t);
case RJD_EASE_TYPE_QUAD: return rjd_ease_in_quad(t);
case RJD_EASE_TYPE_QUAR: return rjd_ease_in_quar(t);
case RJD_EASE_TYPE_QUIN: return rjd_ease_in_quin(t);
case RJD_EASE_TYPE_EXPO: return rjd_ease_in_expo(t);
case RJD_EASE_TYPE_CIRC: return rjd_ease_in_circ(t);
case RJD_EASE_TYPE_BACK: return rjd_ease_in_back(t);
case RJD_EASE_TYPE_ELAS: return rjd_ease_in_elas(t);
case RJD_EASE_TYPE_BOUN: return rjd_ease_in_boun(t);
case RJD_EASE_TYPE_MAX: break;
}
}
case RJD_EASE_DIR_OUT: {
switch(type) {
case RJD_EASE_TYPE_LINE: return rjd_ease_line(t);
case RJD_EASE_TYPE_SINE: return rjd_ease_out_sine(t);
case RJD_EASE_TYPE_CUBE: return rjd_ease_out_cube(t);
case RJD_EASE_TYPE_QUAD: return rjd_ease_out_quad(t);
case RJD_EASE_TYPE_QUAR: return rjd_ease_out_quar(t);
case RJD_EASE_TYPE_QUIN: return rjd_ease_out_quin(t);
case RJD_EASE_TYPE_EXPO: return rjd_ease_out_expo(t);
case RJD_EASE_TYPE_CIRC: return rjd_ease_out_circ(t);
case RJD_EASE_TYPE_BACK: return rjd_ease_out_back(t);
case RJD_EASE_TYPE_ELAS: return rjd_ease_out_elas(t);
case RJD_EASE_TYPE_BOUN: return rjd_ease_out_boun(t);
case RJD_EASE_TYPE_MAX: break;
}
}
case RJD_EASE_DIR_INOUT: {
switch(type) {
case RJD_EASE_TYPE_LINE: return rjd_ease_line(t);
case RJD_EASE_TYPE_SINE: return rjd_ease_inout_sine(t);
case RJD_EASE_TYPE_CUBE: return rjd_ease_inout_cube(t);
case RJD_EASE_TYPE_QUAD: return rjd_ease_inout_quad(t);
case RJD_EASE_TYPE_QUAR: return rjd_ease_inout_quar(t);
case RJD_EASE_TYPE_QUIN: return rjd_ease_inout_quin(t);
case RJD_EASE_TYPE_EXPO: return rjd_ease_inout_expo(t);
case RJD_EASE_TYPE_CIRC: return rjd_ease_inout_circ(t);
case RJD_EASE_TYPE_BACK: return rjd_ease_inout_back(t);
case RJD_EASE_TYPE_ELAS: return rjd_ease_inout_elas(t);
case RJD_EASE_TYPE_BOUN: return rjd_ease_inout_boun(t);
case RJD_EASE_TYPE_MAX: break;
}
}
default: break;
}
RJD_ASSERTFAIL("type (%d) or dir (%d) was invalid.\n", type, dir);
return 0;
}
static inline float rjd_ease_between(float t, float min, float max, rjd_ease_func* f) {
return f(t) * (max - min) + min;
}
static inline float rjd_ease_line(float t) {
return t;
}
static inline float rjd_ease_in_sine(float t) {
return sinf((t - 1) * RJD_MATH_PI/2.0f) + 1;
}
static inline float rjd_ease_in_quad(float t) {
return t*t;
}
static inline float rjd_ease_in_cube(float t) {
return t*t*t;
}
static inline float rjd_ease_in_quar(float t) {
return t*t*t*t;
}
static inline float rjd_ease_in_quin(float t) {
return t*t*t*t*t;
}
static inline float rjd_ease_in_expo(float t) {
return t == 0 ? t : powf(2, 10*(t - 1));
}
static inline float rjd_ease_in_circ(float t) {
return -sqrtf(1-t*t) + 1;
}
static inline float rjd_ease_in_back(float t) {
return t * t * t - t * sinf(t * RJD_MATH_PI);
}
static inline float rjd_ease_in_elas(float t) {
return sinf(13.0f * RJD_MATH_PI / 2.0f * t) * powf(2, 10 * (t - 1));
}
static inline float rjd_ease_in_boun(float t) {
return 1 - rjd_ease_out_boun(1 - t);
}
static inline float rjd_ease_out_sine(float t) {
return sinf(t*RJD_MATH_PI/2.0f);
}
static inline float rjd_ease_out_quad(float t) {
float tt = t - 1;
return 1 - (tt * tt);
}
static inline float rjd_ease_out_cube(float t) {
float tt = t - 1;
return tt * tt * tt + 1;
}
static inline float rjd_ease_out_quar(float t) {
float tt = t - 1;
return 1 - (tt * tt * tt * tt);
}
static inline float rjd_ease_out_quin(float t) {
float tt = t - 1;
return tt * tt * tt * tt * tt + 1;
}
static inline float rjd_ease_out_expo(float t) {
return t == 1 ? t : -powf(2, -10*t) + 1;
}
static inline float rjd_ease_out_circ(float t) {
float tt = t - 1;
return sqrtf(1 - tt * tt);
}
static inline float rjd_ease_out_back(float t) {
float tt = 1 - t;
return 1 - (tt*tt*tt - tt*sinf(tt*RJD_MATH_PI));
}
static inline float rjd_ease_out_elas(float t) {
return sinf(-13.0f * RJD_MATH_PI / 2.0f * (t + 1)) * powf(2, -10 * t) + 1;
}
static inline float rjd_ease_out_boun(float t) {
if (t < 4.0f/11.0f) {
return 121 * t * t / 16.0f;
} else if (t < 8.0f/11.0f) {
return 363.0f/40.0f*t*t - 99.0f/10.0f*t + 17.0f/5.0f;
} else if (t < 9.0f/10.0f) {
return 4356.0f/361.0f*t*t - 35442.0f/1805.0f*t + 16061.0f/1805.0f;
} else {
return 54.0f/5.0f*t*t - 513.0f/25.0f*t + 268.0f/25.0f;
}
}
static inline float rjd_ease_inout_sine(float t) {
return sinf(t*RJD_MATH_PI - RJD_MATH_PI/2.0f) / 2.0f + 0.5f;
}
static inline float rjd_ease_inout_quad(float t) {
float tt = t * 2.0f;
return ((tt < 1) ? rjd_ease_in_quad(tt) : rjd_ease_out_quad(tt - 1) + 1) / 2;
}
static inline float rjd_ease_inout_cube(float t) {
float tt = t * 2.0f;
return ((tt < 1) ? rjd_ease_in_cube(tt) : rjd_ease_out_cube(tt - 1) + 1) / 2;
}
static inline float rjd_ease_inout_quar(float t) {
float tt = t * 2.0f;
return ((tt < 1) ? rjd_ease_in_quar(tt) : rjd_ease_out_quar(tt - 1) + 1) / 2;
}
static inline float rjd_ease_inout_quin(float t) {
float tt = t * 2.0f;
return ((tt < 1) ? rjd_ease_in_quin(tt) : rjd_ease_out_quin(tt - 1) + 1) / 2;
}
static inline float rjd_ease_inout_expo(float t) {
float tt = t * 2.0f;
return ((tt < 1) ? rjd_ease_in_expo(tt) : rjd_ease_out_expo(tt - 1) + 1) / 2;
}
static inline float rjd_ease_inout_circ(float t) {
float tt = t * 2.0f;
return ((tt < 1) ? rjd_ease_in_circ(tt) : rjd_ease_out_circ(tt - 1) + 1) / 2;
}
static inline float rjd_ease_inout_back(float t) {
float tt = t * 2.0f;
return ((tt < 1) ? rjd_ease_in_back(tt) : rjd_ease_out_back(tt - 1) + 1) / 2;
}
static inline float rjd_ease_inout_elas(float t) {
float tt = t * 2.0f;
return ((tt < 1) ? rjd_ease_in_elas(tt) : rjd_ease_out_elas(tt - 1) + 1) / 2;
}
static inline float rjd_ease_inout_boun(float t) {
float tt = t * 2.0f;
return ((tt < 1) ? rjd_ease_in_boun(tt) : rjd_ease_out_boun(tt - 1) + 1) / 2;
}