-
Notifications
You must be signed in to change notification settings - Fork 0
/
IntegralEaseManager.cs
302 lines (262 loc) · 11.2 KB
/
IntegralEaseManager.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
298
299
300
301
302
using System;
using DG.Tweening;
using UnityEngine;
namespace GameUtil
{
public class IntegralEaseManager
{
public const float PI = 3.141593f;
public const float PIOver2 = PI * 0.5f;
public const float OneOverPI = 1 / PI;
//10 * Math.Log(2) = 6.9314718
private const float Tenln2 = 6.9314718f;
private const float OneOver3 = 0.3333333333f;
/// <param name="from">between 0 and 1</param>
/// <param name="to">between 0 and 1</param>
public static float GetDefiniteIntegral(Ease ease, float from, float to)
{
//跨越了0.5的临界点
if ((from < 0.5f || to < 0.5f) && (from >= 0.5f || to >= 0.5f))
{
bool negative;
float min, max;
if (from < to)
{
negative = false;
min = from;
max = to;
}
else
{
negative = true;
min = to;
max = from;
}
float? value = null;
switch (ease)
{
case Ease.InOutQuad:
value = InOutQuadIntegralFunctionLeft(0.5f) - InOutQuadIntegralFunctionLeft(min) +
InOutQuadIntegralFunctionRight(max) - InOutQuadIntegralFunctionRight(0.5f);
break;
case Ease.InOutCubic:
value = InOutCubicIntegralFunctionLeft(0.5f) - InOutCubicIntegralFunctionLeft(min) +
InOutCubicIntegralFunctionRight(max) - InOutCubicIntegralFunctionRight(0.5f);
break;
case Ease.InOutQuart:
value = InOutQuartIntegralFunctionLeft(0.5f) - InOutQuartIntegralFunctionLeft(min) +
InOutQuartIntegralFunctionRight(max) - InOutQuartIntegralFunctionRight(0.5f);
break;
case Ease.InOutQuint:
value = InOutQuintIntegralFunctionLeft(0.5f) - InOutQuintIntegralFunctionLeft(min) +
InOutQuintIntegralFunctionRight(max) - InOutQuintIntegralFunctionRight(0.5f);
break;
case Ease.InOutExpo:
value = InOutExpoIntegralFunctionLeft(0.5f) - InOutExpoIntegralFunctionLeft(min) +
InOutExpoIntegralFunctionRight(max) - InOutExpoIntegralFunctionRight(0.5f);
break;
case Ease.InOutCirc:
value = InOutCircIntegralFunctionLeft(0.5f) - InOutCircIntegralFunctionLeft(min) +
InOutCircIntegralFunctionRight(max) - InOutCircIntegralFunctionRight(0.5f);
break;
}
if (value.HasValue)
return negative ? -value.Value: value.Value;
}
var primitiveFunction = GetIntegralFunction(ease);
return primitiveFunction(to) - primitiveFunction(from);
}
public static Func<float, float> GetIntegralFunction(Ease ease)
{
switch (ease)
{
case Ease.Unset:
case Ease.Linear:
return LinearIntegralFunction;
case Ease.InSine:
return InSineIntegralFunction;
case Ease.OutSine:
return OutSineIntegralFunction;
case Ease.InOutSine:
return InOutSineIntegralFunction;
case Ease.InQuad:
return InQuadIntegralFunction;
case Ease.OutQuad:
return OutQuadIntegralFunction;
case Ease.InOutQuad:
return InOutQuadIntegralFunction;
case Ease.InCubic:
return InCubicIntegralFunction;
case Ease.OutCubic:
return OutCubicIntegralFunction;
case Ease.InOutCubic:
return InOutCubicIntegralFunction;
case Ease.InQuart:
return InQuartIntegralFunction;
case Ease.OutQuart:
return OutQuartIntegralFunction;
case Ease.InOutQuart:
return InOutQuartIntegralFunction;
case Ease.InQuint:
return InQuintIntegralFunction;
case Ease.OutQuint:
return OutQuintIntegralFunction;
case Ease.InOutQuint:
return InOutQuintIntegralFunction;
case Ease.InExpo:
return InExpoIntegralFunction;
case Ease.OutExpo:
return OutExpoIntegralFunction;
case Ease.InOutExpo:
return InOutExpoIntegralFunction;
case Ease.InCirc:
return InCircIntegralFunction;
case Ease.OutCirc:
return OutCircIntegralFunction;
case Ease.InOutCirc:
return InOutCircIntegralFunction;
default:
Debug.LogError("Not support " + ease);
// OutQuad
return OutQuadIntegralFunction;
}
}
/// <param name="value">between 0 and 1</param>
public static float LinearIntegralFunction(float value)
{
return value * value * 0.5f;
}
public static float InSineIntegralFunction(float value)
{
return -2 * OneOverPI * (float) Math.Sin(value * PIOver2) + value;
}
public static float OutSineIntegralFunction(float value)
{
return -2 * OneOverPI * (float) Math.Cos(value * PIOver2);
}
public static float InOutSineIntegralFunction(float value)
{
return -0.5f * (OneOverPI * (float) Math.Sin(value * PI) - value);
}
public static float InQuadIntegralFunction(float value)
{
return OneOver3 * value * value * value;
}
public static float OutQuadIntegralFunction(float value)
{
return value * value - OneOver3 * value * value * value;
}
public static float InOutQuadIntegralFunction(float value)
{
return value < 0.5f ? InOutQuadIntegralFunctionLeft(value) : InOutQuadIntegralFunctionRight(value);
}
public static float InOutQuadIntegralFunctionLeft(float value)
{
return 2 * OneOver3 * value * value * value;
}
public static float InOutQuadIntegralFunctionRight(float value)
{
return -2 * OneOver3 * value * value * value + 2 * value * value - value;
}
public static float InCubicIntegralFunction(float value)
{
return 0.25f * (float) Math.Pow(value, 4);
}
public static float OutCubicIntegralFunction(float value)
{
return 0.25f * (float) Math.Pow(value - 1, 4) + value;
}
public static float InOutCubicIntegralFunction(float value)
{
return value < 0.5f ? InOutCubicIntegralFunctionLeft(value) : InOutCubicIntegralFunctionRight(value);
}
public static float InOutCubicIntegralFunctionLeft(float value)
{
return (float) Math.Pow(value, 4);
}
public static float InOutCubicIntegralFunctionRight(float value)
{
return (float) Math.Pow(value - 1, 4) + value;
}
public static float InQuartIntegralFunction(float value)
{
return 0.2f * (float) Math.Pow(value, 5);
}
public static float OutQuartIntegralFunction(float value)
{
return value - 0.2f * (float) Math.Pow(value - 1, 5);
}
public static float InOutQuartIntegralFunction(float value)
{
return value < 0.5f ? InOutQuartIntegralFunctionLeft(value) : InOutQuartIntegralFunctionRight(value);
}
public static float InOutQuartIntegralFunctionLeft(float value)
{
return 1.6f * (float) Math.Pow(value, 5);
}
public static float InOutQuartIntegralFunctionRight(float value)
{
return 1.6f * (float) Math.Pow(value - 1, 5) + value;
}
public static float InQuintIntegralFunction(float value)
{
return OneOver3 * 0.5f * (float) Math.Pow(value, 6);
}
public static float OutQuintIntegralFunction(float value)
{
return OneOver3 * 0.5f * (float) Math.Pow(value - 1, 6) + value;
}
public static float InOutQuintIntegralFunction(float value)
{
return value < 0.5f ? InOutQuintIntegralFunctionLeft(value) : InOutQuintIntegralFunctionRight(value);
}
public static float InOutQuintIntegralFunctionLeft(float value)
{
return 8 * OneOver3 * (float) Math.Pow(value, 6);
}
public static float InOutQuintIntegralFunctionRight(float value)
{
return 8 * OneOver3 * (float) Math.Pow(value - 1, 6) + value;
}
public static float InExpoIntegralFunction(float value)
{
return (float) Math.Pow(2, 10 * (value - 1)) / Tenln2;
}
public static float OutExpoIntegralFunction(float value)
{
return (float) Math.Pow(2, -10 * value) / Tenln2 + value;
}
public static float InOutExpoIntegralFunction(float value)
{
return value < 0.5 ? InOutExpoIntegralFunctionLeft(value) : InOutExpoIntegralFunctionRight(value);
}
public static float InOutExpoIntegralFunctionLeft(float value)
{
return (float) Math.Pow(2, 10 * (2 * value - 1)) / (4 * Tenln2);
}
public static float InOutExpoIntegralFunctionRight(float value)
{
return value - (float) Math.Pow(2, -10 * (2 * value - 1)) / (4 * Tenln2);
}
public static float InCircIntegralFunction(float value)
{
return -0.5f * ((float) Math.Asin(value) + (float) Math.Sqrt(1 - value) * value * (float) Math.Sqrt(value + 1) - 2 * value);
}
public static float OutCircIntegralFunction(float value)
{
return 0.5f * ((float) Math.Sqrt(2 - value) * (value - 1) * (float) Math.Sqrt(value) + (float) Math.Asin(value - 1));
}
public static float InOutCircIntegralFunction(float value)
{
return value < 0.5 ? InOutCircIntegralFunctionLeft(value) : InOutCircIntegralFunctionRight(value);
}
public static float InOutCircIntegralFunctionLeft(float value)
{
return -0.125f * ((float) Math.Asin(2 * value) + 2 * (float) Math.Sqrt(1 - 4 * value * value) * value - 4 * value);
}
public static float InOutCircIntegralFunctionRight(float value)
{
return 0.125f * (2 * (float) Math.Sqrt(-4 * value * value + 8 * value - 3) * (value - 1) + value - (float) Math.Asin(2 - 2 * value));
}
}
}