-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEasing.cs
More file actions
456 lines (370 loc) · 13.2 KB
/
Easing.cs
File metadata and controls
456 lines (370 loc) · 13.2 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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Assertions;
// Class for quick implementation of easing functions inspired by https://easings.net/
// Also helpful https://www.desmos.com/calculator (Some behaviors are marked with desmos formula)
// The purpose of this class is for the flexible interpolation of any value to another state.
// First an Easing is created with start and end values. Then it can be sampled.
// If the easing is in a transition, it will return the interpolated value based on its chosen behavior.
// If it has not begun, it will return the start value, and if it finished the transition, it will continue to return the end value.
public class Easing
{
public enum EasingBehavior
{
NULL = -1,
LINEAR, // Returns timeFactor
START_VALUE, // Returns _startValue
END_VALUE, // Returns _endValue
CURVE, // Use supplied animation curve
QUAD_IN,
QUAD_OUT,
QUAD_IN_OUT, // x * x
CUBIC_IN,
CUBIC_OUT,
CUBIC_IN_OUT, // x * x * x
TRIG_IN, // f\left(x\right)\ =\ 1-\cos\left(\frac{x\pi}{2}\right)
TRIG_OUT, // f\left(x\right)\ =\sin\left(\frac{x\pi}{2}\right)
TRIG_IN_OUT, // f\left(x\right)\ =\frac{\left(\cos\left(x\pi\right)-1\right)}{-2}
EXPO_IN,
EXPO_OUT,
EXPO_IN_OUT, // Exponential
BOUNCE_IN,
BOUNCE_OUT,
BOUNCE_IN_OUT, // Like a ball
BACK_IN,
BACK_OUT,
BACK_IN_OUT, // Wind-up [Beyond 0-1]
ELASTIC_IN,
ELASTIC_OUT,
ELASTIC_IN_OUT, // Rubber Band [Beyond 0-1]
//---------
NUM_EASINGS
}
public enum LoopBehavior
{
NO_LOOP = 0,
RESET,
PING_PONG,
PING_PONG_ONCE,
}
public enum PlayState
{
UNPLAYED = 0, // Sample() == _startValue
IS_PLAYING, // Sample() == [calculated]
FINISHED, // Sample() == _endValue
}
public EasingBehavior _easingType = EasingBehavior.NULL;
public LoopBehavior _loopType = LoopBehavior.NO_LOOP;
public AnimationCurve _animationCurve = null;
public bool _timeScaleRelative = true;
public bool _paused = false;
public PlayState _PlayState { get { return _playState; } }
private PlayState _playState = PlayState.UNPLAYED;
private float _startTime = float.NegativeInfinity;
public float _duration = 1f;
public float _startValue = 0f;
public float _endValue = 1f;
public Easing(EasingBehavior type, LoopBehavior loopType, float duration = float.NegativeInfinity)
{
_easingType = type;
_loopType = loopType;
if (!float.IsNegativeInfinity(duration))
{
// Optionally set duration
_duration = duration;
}
}
public Easing(AnimationCurve curve, LoopBehavior loopType, float duration = float.NegativeInfinity)
{
_animationCurve = curve;
_easingType = EasingBehavior.CURVE;
_loopType = loopType;
if (!float.IsNegativeInfinity(duration))
{
// Optionally set duration
_duration = duration;
}
}
// Get the current value, must be called from Update()
public float Sample()
{
if (_paused)
{
if (_timeScaleRelative)
{
_startTime += Time.deltaTime;
}
else
{
_startTime += Time.unscaledDeltaTime;
}
}
// Return value based on state of easing
if (_playState == PlayState.UNPLAYED)
{
return _startValue;
}
else if (_playState == PlayState.IS_PLAYING)
{
float timeFactor = (GetCurrentTime() - _startTime) / _duration;
if (timeFactor > 1f)
{
if (_loopType == LoopBehavior.NO_LOOP)
{
// Easing finished
_playState = PlayState.FINISHED;
return _endValue;
}
else
{
// Calculate new transition
while (timeFactor > 1f)
{
_startTime += _duration;
timeFactor = (GetCurrentTime() - _startTime) / _duration;
switch (_loopType)
{
case LoopBehavior.RESET:
// No change, reset back to start value
break;
case LoopBehavior.PING_PONG:
// Alternate start and endpoints
float tmp = _endValue;
_endValue = _startValue;
_startValue = tmp;
break;
case LoopBehavior.PING_PONG_ONCE:
// Only loop this time
_loopType = LoopBehavior.NO_LOOP;
goto case LoopBehavior.PING_PONG;
}
}
}
}
// Calculate current value
return GetValueAtTime(timeFactor);
}
Assert.IsTrue(_playState == PlayState.FINISHED);
return _endValue;
}
#region State Management
// Easing marks the start time for calculations
public void Begin(float startValue, float endValue, float duration = float.NegativeInfinity)
{
_playState = PlayState.IS_PLAYING;
_startTime = GetCurrentTime();
_startValue = startValue;
_endValue = endValue;
if (!float.IsNegativeInfinity(duration))
{
// Optionally set duration
_duration = duration;
}
if (_easingType == EasingBehavior.NULL)
{
Debug.LogError("Easing type not yet assigned!");
}
}
// Begin Easing without values, so raw output can be applied to a Vector3.Lerp(), for instance
public void Begin(float duration = float.NegativeInfinity)
{
Begin(0f, 1f, duration);
}
public void Reset()
{
_playState = PlayState.UNPLAYED;
_startTime = float.NegativeInfinity;
}
#endregion
#region Public Queries
// Perform easing calculation
public float GetValueAtTime(float t)
{
float value = t;
switch (_easingType)
{
case EasingBehavior.LINEAR:
// Value is linear by default
break;
case EasingBehavior.START_VALUE:
value = 0f;
break;
case EasingBehavior.END_VALUE:
value = 1f;
break;
case EasingBehavior.CURVE:
if (_animationCurve != null)
{
value = _animationCurve.Evaluate(t);
}
else
{
Debug.LogError("Easing's animation curve is null!");
}
break;
case EasingBehavior.QUAD_IN:
value = t * t;
break;
case EasingBehavior.QUAD_OUT:
value = 1f - ((1f - t) * (1f - t));
break;
case EasingBehavior.QUAD_IN_OUT:
if (t < 0.5f)
{
value = 2f * t * t;
}
else
{
value = 1f - (Mathf.Pow(-2f * t + 2f, 2f) / 2f);
}
break;
case EasingBehavior.CUBIC_IN:
value = t * t * t;
break;
case EasingBehavior.CUBIC_OUT:
value = 1f - Mathf.Pow(1f - t, 3f);
break;
case EasingBehavior.CUBIC_IN_OUT:
if (t < 0.5f)
{
value = 4f * t * t * t;
}
else
{
value = 1f - (Mathf.Pow(-2f * t + 2f, 3f) / 2f);
}
break;
case EasingBehavior.TRIG_IN:
value = 1f - Mathf.Cos(t * Mathf.PI / 2f);
break;
case EasingBehavior.TRIG_OUT:
value = Mathf.Sin(t * Mathf.PI / 2f);
break;
case EasingBehavior.TRIG_IN_OUT:
value = (Mathf.Cos(Mathf.PI * t) - 1f) / -2f;
break;
case EasingBehavior.EXPO_IN:
if (t > 0f)
{
value = Mathf.Pow(2f, (10f * t) - 10f);
}
break;
case EasingBehavior.EXPO_OUT:
if (t < 1f)
{
value = 1f - Mathf.Pow(2f, -10f * t);
}
break;
case EasingBehavior.EXPO_IN_OUT:
if (t != 0f
&& t != 1f)
{
if (t < 0.5f)
{
value = Mathf.Pow(2f, (20f * t) - 10f) / 2f;
}
else
{
value = (2f - Mathf.Pow(2f, (-20f * t) + 10f)) / 2f;
}
}
break;
case EasingBehavior.BOUNCE_IN:
value = 1f - CalculateBounceOut(1f - t);
break;
case EasingBehavior.BOUNCE_OUT:
value = CalculateBounceOut(t);
break;
case EasingBehavior.BOUNCE_IN_OUT:
if (t < 0.5f)
{
value = (1f - CalculateBounceOut(1f - (2f * t))) / 2f;
}
else
{
value = (1f + CalculateBounceOut((2f * t) - 1f)) / 2f;
}
break;
case EasingBehavior.BACK_IN:
value = (2.70158f * t * t * t) - (1.70158f * t * t);
break;
case EasingBehavior.BACK_OUT:
value = 1f + (2.70158f * Mathf.Pow(t - 1f, 3f)) + (1.70158f * Mathf.Pow(t - 1f, 2f));
break;
case EasingBehavior.BACK_IN_OUT:
if (t < 0.5f)
{
value = (Mathf.Pow(2f * t, 2f) * ((2.595f + 1f) * 2f * t - 2.595f)) / 2f;
}
else
{
value = (Mathf.Pow(2f * t - 2f, 2f) * ((2.595f + 1f) * (t * 2f - 2f) + 2.595f) + 2f) / 2f;
}
break;
case EasingBehavior.ELASTIC_IN:
if (t != 0f
&& t != 1f)
{
value = -Mathf.Pow(2f, 10f * t - 10f) * Mathf.Sin((t * 10f - 10.75f) * ((2f * Mathf.PI) / 3f));
}
break;
case EasingBehavior.ELASTIC_OUT:
if (t != 0f
&& t != 1f)
{
value = Mathf.Pow(2f, -10f * t) * Mathf.Sin((t * 10f - 0.75f) * ((2f * Mathf.PI) / 3f)) + 1f;
}
break;
case EasingBehavior.ELASTIC_IN_OUT:
if (t != 0f
&& t != 1f)
{
if (t < 0.5f)
{
value = -(Mathf.Pow(2f, 20f * t - 10f) * Mathf.Sin((20f * t - 11.125f) * ((2f * Mathf.PI) / 4.5f))) / 2f;
}
else
{
value = (Mathf.Pow(2f, -20f * t + 10f) * Mathf.Sin((20f * t - 11.125f) * ((2f * Mathf.PI) / 4.5f))) / 2f + 1f;
}
}
break;
default:
Debug.LogWarning("Easing not defined: " + _easingType.ToString());
break;
}
return Mathf.LerpUnclamped(_startValue, _endValue, value);
}
#endregion
#region Internal Calculations
private float GetCurrentTime()
{
if (_timeScaleRelative)
return Time.time;
else
return Time.unscaledTime;
}
private float CalculateBounceOut(float timeFactor)
{
float n1 = 7.5625f;
float d1 = 2.75f;
if (timeFactor < 1f / d1)
{
return n1 * timeFactor * timeFactor;
}
else if (timeFactor < 2f / d1)
{
return n1 * (timeFactor -= 1.5f / d1) * timeFactor + 0.75f;
}
else if (timeFactor < 2.5f / d1)
{
return n1 * (timeFactor -= 2.25f / d1) * timeFactor + 0.9375f;
}
else
{
return n1 * (timeFactor -= 2.625f / d1) * timeFactor + 0.984375f;
}
}
#endregion
}