Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
stenzek
GitHub Repository: stenzek/duckstation
Path: blob/master/src/common/easing.h
4222 views
1
// SPDX-FileCopyrightText: 2019-2024 Connor McLaughlin <[email protected]>
2
// SPDX-License-Identifier: CC-BY-NC-ND-4.0
3
4
#pragma once
5
#include "types.h"
6
#include <cmath>
7
8
// From https://github.com/nicolausYes/easing-functions/blob/master/src/easing.cpp
9
10
namespace Easing {
11
inline constexpr float pi = 3.1415926545f;
12
13
template<typename T>
14
ALWAYS_INLINE_RELEASE T InSine(T t)
15
{
16
return std::sin(1.5707963f * t);
17
}
18
19
template<typename T>
20
ALWAYS_INLINE_RELEASE T OutSine(T t)
21
{
22
return 1 + std::sin(1.5707963f * (--t));
23
}
24
25
template<typename T>
26
ALWAYS_INLINE_RELEASE T InOutSine(T t)
27
{
28
return 0.5f * (1 + std::sin(3.1415926f * (t - 0.5f)));
29
}
30
31
template<typename T>
32
ALWAYS_INLINE_RELEASE T InQuad(T t)
33
{
34
return t * t;
35
}
36
37
template<typename T>
38
ALWAYS_INLINE_RELEASE T OutQuad(T t)
39
{
40
return t * (2 - t);
41
}
42
43
template<typename T>
44
ALWAYS_INLINE_RELEASE T InOutQuad(T t)
45
{
46
return t < 0.5f ? 2 * t * t : t * (4 - 2 * t) - 1;
47
}
48
49
template<typename T>
50
ALWAYS_INLINE_RELEASE T InCubic(T t)
51
{
52
return t * t * t;
53
}
54
55
template<typename T>
56
ALWAYS_INLINE_RELEASE T OutCubic(T t)
57
{
58
return 1 + (--t) * t * t;
59
}
60
61
template<typename T>
62
ALWAYS_INLINE_RELEASE T InOutCubic(T t)
63
{
64
return t < 0.5f ? 4 * t * t * t : 1 + (--t) * (2 * (--t)) * (2 * t);
65
}
66
67
template<typename T>
68
ALWAYS_INLINE_RELEASE T InQuart(T t)
69
{
70
t *= t;
71
return t * t;
72
}
73
74
template<typename T>
75
ALWAYS_INLINE_RELEASE T OutQuart(T t)
76
{
77
t = (--t) * t;
78
return 1 - t * t;
79
}
80
81
template<typename T>
82
ALWAYS_INLINE_RELEASE T InOutQuart(T t)
83
{
84
if (t < 0.5)
85
{
86
t *= t;
87
return 8 * t * t;
88
}
89
else
90
{
91
t = (--t) * t;
92
return 1 - 8 * t * t;
93
}
94
}
95
96
template<typename T>
97
ALWAYS_INLINE_RELEASE T InQuint(T t)
98
{
99
T t2 = t * t;
100
return t * t2 * t2;
101
}
102
103
template<typename T>
104
ALWAYS_INLINE_RELEASE T OutQuint(T t)
105
{
106
T t2 = (--t) * t;
107
return 1 + t * t2 * t2;
108
}
109
110
template<typename T>
111
ALWAYS_INLINE_RELEASE T InOutQuint(T t)
112
{
113
T t2;
114
if (t < 0.5)
115
{
116
t2 = t * t;
117
return 16 * t * t2 * t2;
118
}
119
else
120
{
121
t2 = (--t) * t;
122
return 1 + 16 * t * t2 * t2;
123
}
124
}
125
126
template<typename T>
127
ALWAYS_INLINE_RELEASE T InExpo(T t)
128
{
129
return (std::pow(static_cast<T>(2), static_cast<T>(8) * t) - static_cast<T>(1)) / static_cast<T>(255);
130
}
131
132
template<typename T>
133
ALWAYS_INLINE_RELEASE T OutExpo(T t)
134
{
135
return static_cast<T>(1) - std::pow(static_cast<T>(2), static_cast<T>(-8) * t);
136
}
137
138
template<typename T>
139
ALWAYS_INLINE_RELEASE T InOutExpo(T t)
140
{
141
if (t < 0.5f)
142
{
143
return (std::pow(2, 16 * t) - 1) / 510;
144
}
145
else
146
{
147
return 1 - 0.5f * std::pow(2, -16 * (t - 0.5f));
148
}
149
}
150
151
template<typename T>
152
ALWAYS_INLINE_RELEASE T InCirc(T t)
153
{
154
return 1 - std::sqrt(1 - t);
155
}
156
157
template<typename T>
158
ALWAYS_INLINE_RELEASE T OutCirc(T t)
159
{
160
return std::sqrt(t);
161
}
162
163
template<typename T>
164
ALWAYS_INLINE_RELEASE T InOutCirc(T t)
165
{
166
if (t < 0.5f)
167
{
168
return (1 - std::sqrt(1 - 2 * t)) * 0.5f;
169
}
170
else
171
{
172
return (1 + std::sqrt(2 * t - 1)) * 0.5f;
173
}
174
}
175
176
template<typename T>
177
ALWAYS_INLINE_RELEASE T InBack(T t)
178
{
179
return t * t * (2.70158f * t - 1.70158f);
180
}
181
182
template<typename T>
183
ALWAYS_INLINE_RELEASE T OutBack(T t)
184
{
185
t -= 1;
186
return 1 + t * t * (2.70158f * t + 1.70158f);
187
}
188
189
template<typename T>
190
ALWAYS_INLINE_RELEASE T InOutBack(T t)
191
{
192
if (t < 0.5f)
193
{
194
return t * t * (7 * t - 2.5f) * 2;
195
}
196
else
197
{
198
return 1 + (--t) * t * 2 * (7 * t + 2.5f);
199
}
200
}
201
202
template<typename T>
203
ALWAYS_INLINE_RELEASE T InElastic(T t)
204
{
205
T t2 = t * t;
206
return t2 * t2 * std::sin(t * pi * 4.5f);
207
}
208
209
template<typename T>
210
ALWAYS_INLINE_RELEASE T OutElastic(T t)
211
{
212
T t2 = (t - 1) * (t - 1);
213
return 1 - t2 * t2 * std::cos(t * pi * 4.5f);
214
}
215
216
template<typename T>
217
ALWAYS_INLINE_RELEASE T InOutElastic(T t)
218
{
219
T t2;
220
if (t < 0.45f)
221
{
222
t2 = t * t;
223
return 8 * t2 * t2 * std::sin(t * pi * 9);
224
}
225
else if (t < 0.55f)
226
{
227
return 0.5f + 0.75f * std::sin(t * pi * 4);
228
}
229
else
230
{
231
t2 = (t - 1) * (t - 1);
232
return 1 - 8 * t2 * t2 * std::sin(t * pi * 9);
233
}
234
}
235
236
template<typename T>
237
ALWAYS_INLINE_RELEASE T InBounce(T t)
238
{
239
return std::pow(2, 6 * (t - 1)) * std::abs(sin(t * pi * 3.5f));
240
}
241
242
template<typename T>
243
ALWAYS_INLINE_RELEASE T OutBounce(T t)
244
{
245
return 1 - std::pow(2, -6 * t) * std::abs(std::cos(t * pi * 3.5f));
246
}
247
248
template<typename T>
249
ALWAYS_INLINE_RELEASE T InOutBounce(T t)
250
{
251
if (t < 0.5f)
252
{
253
return 8 * std::pow(2, 8 * (t - 1)) * std::abs(std::sin(t * pi * 7));
254
}
255
else
256
{
257
return 1 - 8 * std::pow(2, -8 * t) * std::abs(std::sin(t * pi * 7));
258
}
259
}
260
261
} // namespace Easing
262
263