-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanimation_curve.cpp
278 lines (233 loc) · 6.62 KB
/
animation_curve.cpp
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
#include "animation_curve.hpp"
#include <algorithm>
#include <fmt/core.h>
float AnimationCurve::TangentLimit = 25;
AnimationCurve::AnimationCurve() : linear(true)
{
points.reserve(2);
ApplyPreset(Preset::Linear);
}
void AnimationCurve::ApplyPreset(Preset preset)
{
Point p0, p1;
p0.Time = 0;
p0.Value = 0;
p0.Locked = true;
p0.OutTangent = 0;
p0.InTangent = 0;
p1.Time = 1;
p1.Value = 1;
p1.Locked = true;
p1.OutTangent = 1;
p1.InTangent = 1;
switch (preset)
{
case Preset::Zero:
p0.OutTangent = 0;
p0.Value = 0;
p1.InTangent = 0;
p1.Value = 0;
break;
case Preset::One:
p0.OutTangent = 0;
p0.Value = 1;
p1.InTangent = 0;
p1.Value = 1;
break;
case Preset::Linear:
p0.OutTangent = 1;
p1.InTangent = 1;
break;
case Preset::EaseInOut:
p0.OutTangent = 0;
p1.InTangent = 0;
break;
case Preset::EaseIn:
p0.OutTangent = 0;
p1.InTangent = 1;
break;
case Preset::EaseOut:
p0.OutTangent = 1;
p1.InTangent = 0;
break;
default:
throw std::runtime_error("AnimationCurve :: Unknown preset");
}
points.clear();
points.emplace_back(p0);
points.emplace_back(p1);
}
void AnimationCurve::EnableLinearInterpolation(bool value)
{
linear = value;
}
// NOTE: add better continuity without distorting the curve,
// it does work ok, but it could be better
void AnimationCurve::AddKey(float time, float value)
{
if (time < 0)
time = 0;
if (time > 1)
time = 1;
for (int i = 0; i < points.size() - 1; ++i)
{
if (time > points[i].Time && time < points[i + 1].Time)
{
Point p;
p.Time = time;
p.Value = value;
p.OutTangent = 0.f;
p.InTangent = 0.f;
p.Locked = true;
points.emplace(points.begin() + i + 1, p);
return;
}
}
}
void AnimationCurve::RemoveKeyframe(int i)
{
// never delete first or last anchor
if (i == 0 || i == points.size() - 1)
return;
points.erase(points.begin() + i);
}
void AnimationCurve::SetPoint(int i, float t, float v)
{
float lowerLimit = 0;
float upperLimit = 1;
float gapLimit = 0.001;
if (i == 0 || i == points.size() - 1)
gapLimit = 0;
if (i != 0)
lowerLimit = points[i - 1].Time;
if (i != points.size() - 1)
upperLimit = points[i + 1].Time;
float x = Mathf::Clamp(t, lowerLimit + gapLimit, upperLimit - gapLimit);
float y = v;
points[i].Time = x;
points[i].Value = y;
}
void AnimationCurve::SetOutTangent(int i, float t, float v)
{
if (i >= points.size() - 1)
return;
float dt = t - points[i].Time;
float df = v - points[i].Value;
if (dt == 0)
return;
points[i].OutTangent = clampTangent(df / dt);
if (points[i].Locked)
points[i].InTangent = points[i].OutTangent;
// fmt::println("OutTangent: {}", points[i].OutTangent);
}
void AnimationCurve::SetInTangent(int i, float t, float v)
{
if (i <= 0)
return;
float dt = t - points[i].Time;
float df = v - points[i].Value;
if (dt == 0)
return;
points[i].InTangent = clampTangent(df / dt);
if (points[i].Locked)
points[i].OutTangent = points[i].InTangent;
// fmt::println("InTangent: ({} , {}) -> {}", t, v, points[i].InTangent);
}
void AnimationCurve::SetOutTangentValue(int i, float v)
{
points[i].OutTangent = v;
if (points[i].Locked)
points[i].InTangent = v;
}
void AnimationCurve::SetInTangentValue(int i, float v)
{
points[i].InTangent = v;
if (points[i].Locked)
points[i].OutTangent = v;
}
void AnimationCurve::ToggleTangentSplitJoin(int i, Tangent dominantTangnent)
{
if (i == 0 || i == points.size() - 1)
return;
points[i].Locked = !points[i].Locked;
if (dominantTangnent == Tangent::Out)
points[i].InTangent = points[i].OutTangent;
else
points[i].OutTangent = points[i].InTangent;
}
Vec2 AnimationCurve::Anchor(int i) const
{
return Vec2(points[i].Time, points[i].Value);
}
Vec2 AnimationCurve::OutTangent(int i) const
{
auto tangent = Mathf::NormalizedVec2(1.0f, points[i].OutTangent);
auto outTangentEnd = Anchor(i) + tangent * 0.12f;
return outTangentEnd;
}
Vec2 AnimationCurve::InTangent(int i) const
{
auto tangent = Mathf::NormalizedVec2(1.0f, points[i].InTangent);
auto inTangentEnd = Anchor(i) - tangent * 0.12f;
return inTangentEnd;
}
float AnimationCurve::function(float t, float p0, float out, float in, float p1) const
{
float t2 = t * t;
float t3 = t2 * t;
float h00 = 2.0f * t3 - 3.0f * t2 + 1.0f;
float h10 = t3 - 2.0f * t2 + t;
float h01 = -2.0f * t3 + 3.0f * t2;
float h11 = t3 - t2;
return p0 * h00 + out * h10 + p1 * h01 + in * h11;
}
float AnimationCurve::clampTangent(float tan) const
{
if (linear)
{
float linearThreshold = TangentLimit;
return Mathf::Clamp(tan, -TangentLimit - linearThreshold, TangentLimit + linearThreshold);
}
return tan;
}
float AnimationCurve::Evaluate(float t) const
{
if (t <= StartTime())
return points.front().Value;
if (t >= EndTime())
return points.back().Value;
int segments = Segments();
for (int i = 0; i < segments; ++i)
{
if (t <= points[i + 1].Time)
{
float tMax = points[i + 1].Time - points[i].Time;
float tCurrent = (t - points[i].Time);
float u = tCurrent / tMax;
if (linear)
{
// clamp to avoid extreme values, this assumes
// that allowed tangent values are between (-TangetLimit, TangentLimit),
// anything outside will cause to switch to fixed value
if (points[i].OutTangent >= TangentLimit)
return points[i + 1].Value;
if (points[i].OutTangent <= -TangentLimit)
return points[i].Value;
if (points[i + 1].InTangent >= TangentLimit)
return points[i + 1].Value;
if (points[i + 1].InTangent <= -TangentLimit)
return points[i].Value;
}
return function(u,
points[i].Value,
points[i].OutTangent * tMax,
points[i + 1].InTangent * tMax,
points[i + 1].Value);
}
}
return 0.0f;
}
float AnimationCurve::Evaluate(const Timer &timer) const
{
return Evaluate(timer.Evaluate());
}