/
Easing.cs
299 lines (227 loc) · 8.02 KB
/
Easing.cs
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
using System.Collections;
using System.Collections.Generic;
using System;
using UnityEngine;
namespace hjijijing.Tweening
{
public static class Easing
{
//SINE
public static Func<float, float> easeInSine = (t) =>
{
return 1f - Mathf.Cos((Mathf.PI * t) / 2f);
};
public static Func<float, float> easeOutSine = (t) =>
{
return Mathf.Sin((t * Mathf.PI) / 2f);
};
public static Func<float, float> easeInOutSine = (t) =>
{
return -(Mathf.Cos(Mathf.PI * t) - 1f) / 2f;
};
//QUAD
public static Func<float, float> easeInQuad = (t) =>
{
return t * t;
};
public static Func<float, float> easeOutQuad = (t) =>
{
return 1f - ((1f - t) * (1f - t));
};
public static Func<float, float> easeInOutQuad = (t) =>
{
return t < 0.5f ? 2f * t * t : 1f - Mathf.Pow(-2f * t + 2f, 2f) / 2f;
};
//CUBIC
public static Func<float, float> easeInCubic = (t) =>
{
return t * t * t;
};
public static Func<float, float> easeOutCubic = (t) =>
{
return 1f - Mathf.Pow(1f - t, 3f);
};
public static Func<float, float> easeInOutCubic = (t) =>
{
return t < 0.5f ? 4f * t * t * t : 1f - Mathf.Pow(-2f * t + 2f, 3f) / 2f;
};
//QUART
public static Func<float, float> easeInQuart = (t) =>
{
return t * t * t * t;
};
public static Func<float, float> easeOutQuart = (t) =>
{
return 1f - Mathf.Pow(1f - t, 4f);
};
public static Func<float, float> easeInOutQuart = (t) =>
{
return t < 0.5f ? 8f * t * t * t * t : 1f - Mathf.Pow(-2f * t + 2f, 4f) / 2f;
};
//Quint
public static Func<float, float> easeInQuint = (t) =>
{
return t * t * t * t * t;
};
public static Func<float, float> easeOutQuint = (t) =>
{
return 1f - Mathf.Pow(1f - t, 5f);
};
public static Func<float, float> easeInOutQuint = (t) =>
{
return t < 0.5f ? 16f * t * t * t * t * t : 1f - Mathf.Pow(-2f * t + 2f, 5f) / 2f;
};
//Expo
public static Func<float, float> easeInExpo = (t) =>
{
return t == 0f ? 0f : Mathf.Pow(2f, 10f * t - 10f);
};
public static Func<float, float> easeOutExpo = (t) =>
{
return t == 1f ? 1f : 1f - Mathf.Pow(2f, -10f * t);
};
public static Func<float, float> easeInOutExpo = (t) =>
{
return t == 0f ? 0f : t == 1f ? 1f : t < 0.5f ? Mathf.Pow(2f, 20f * t - 10f) / 2f : (2f - Mathf.Pow(2f, -20f * t + 10f)) / 2f;
};
//Circ
public static Func<float, float> easeInCirc = (t) =>
{
return 1 - Mathf.Sqrt(1f - Mathf.Pow(t, 2f));
};
public static Func<float, float> easeOutCirc = (t) =>
{
return Mathf.Sqrt(1f - Mathf.Pow(t - 1f, 2f));
};
public static Func<float, float> easeInOutCirc = (t) =>
{
return t < 0.5f ? (1f - Mathf.Sqrt(1f - Mathf.Pow(2f * t, 2f))) / 2f : (Mathf.Sqrt(1f - Mathf.Pow(-2f * t + 2f, 2f)) + 1f) / 2f;
};
//Back
public static Func<float, float> easeInBack = (t) =>
{
float c1 = 1.70158f;
float c3 = c1 + 1f;
return c3 * t * t * t - c1 * t * t;
};
public static Func<float, float> easeOutBack = (t) =>
{
float c1 = 1.70158f;
float c3 = c1 + 1f;
return 1f + c3 * Mathf.Pow(t - 1f, 3f) + c1 * Mathf.Pow(t - 1f, 2f);
};
public static Func<float, float> easeInOutBack = (t) =>
{
float c1 = 1.70158f;
float c2 = c1 * 1.525f;
return t < 0.5f
? (Mathf.Pow(2f * t, 2f) * ((c2 + 1f) * 2f * t - c2)) / 2f
: (Mathf.Pow(2f * t - 2f, 2f) * ((c2 + 1f) * (t * 2f - 2f) + c2) + 2f) / 2f;
};
//Elastic
public static Func<float, float> easeInElastic = (t) =>
{
float c4 = (2f * Mathf.PI) / 3f;
return t == 0f
? 0f
: t == 1f
? 1f
: -Mathf.Pow(2f, 10f * t - 10f) * Mathf.Sin((t * 10f - 10.75f) * c4);
};
public static Func<float, float> easeOutElastic = (t) =>
{
float c4 = (2f * Mathf.PI) / 3f;
return t == 0f
? 0f
: t == 1f
? 1f
: Mathf.Pow(2f, -10f * t) * Mathf.Sin((t * 10f - 0.75f) * c4) + 1f;
};
public static Func<float, float> easeInOutElastic = (t) =>
{
float c5 = (2f * Mathf.PI) / 4.5f;
return t == 0f
? 0f
: t == 1f
? 1f
: t < 0.5f
? -(Mathf.Pow(2f, 20f * t - 10f) * Mathf.Sin((20f * t - 11.125f) * c5)) / 2f
: (Mathf.Pow(2f, -20f * t + 10f) * Mathf.Sin((20f * t - 11.125f) * c5)) / 2f + 1f;
};
//Bounce
public static Func<float, float> easeInBounce = (t) =>
{
return 1f - easeOutBounce(1f - t);
};
public static Func<float, float> easeOutBounce = (t) =>
{
float n1 = 7.5625f;
float d1 = 2.75f;
if (t < 1f / d1)
{
return n1 * t * t;
}
else if (t < 2f / d1)
{
return n1 * (t -= 1.5f / d1) * t + 0.75f;
}
else if (t < 2.5f / d1)
{
return n1 * (t -= 2.25f / d1) * t + 0.9375f;
}
else
{
return n1 * (t -= 2.625f / d1) * t + 0.984375f;
}
};
public static Func<float, float> easeInOutBounce = (t) =>
{
return t < 0.5f
? (1f - easeOutBounce(1f - 2f * t)) / 2f
: (1f + easeOutBounce(2f * t - 1f)) / 2f;
};
public static Func<float, float> linear = (t) => t;
public static Func<float, float> randomEasing(float min, float max)
{
return (t) => { return t == 0f ? 0f : t == 1f ? 1f : UnityEngine.Random.Range(min, max); };
}
public static Func<float, float> reverseEasing(Func<float, float> easing)
{
return (t) => { return 1-easing(t); };
}
public static Func<float, float> scaleEasing(Func<float, float> easing, float scale)
{
return (t) => { return scale * easing(t); };
}
public static Func<float, float> negativeEasing(Func<float, float> easing)
{
return scaleEasing(easing, -1);
}
public static Func<float, float> combineEasings(Func<float, float> easing1, Func<float, float> easing2, float transitionPoint = 0.5f)
{
return (t) =>
{
if (t <= transitionPoint)
return easing1(t / transitionPoint);
else
return easing2((t - transitionPoint)/(1 - transitionPoint));
};
}
public static Func<float, float> addEasings(Func<float, float> easing1, Func<float, float> easing2)
{
return (t) => { return easing1(t) + easing2(t); };
}
public static Func<float, float> subtractEasings(Func<float, float> easing1, Func<float, float> easing2)
{
return (t) => { return easing1(t) - easing2(t); };
}
public static Func<float, float> multiplyEasings(Func<float, float> easing1, Func<float, float> easing2)
{
return (t) => { return easing1(t) * easing2(t); };
}
public static Func<float, float> averageEasings(Func<float, float> easing1, Func<float, float> easing2)
{
return scaleEasing(addEasings(easing1, easing2), 0.5f);
}
}
}