-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathUtils.cs
More file actions
410 lines (328 loc) · 13 KB
/
Utils.cs
File metadata and controls
410 lines (328 loc) · 13 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
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using UnityEngine;
public static class Utils {
#region UTILITIES
#region Number Utils
public static float pixelsPerUnit = 16;
public static float PixelsToUnits(float value) => PixelsToUnits(value, pixelsPerUnit);
public static float PixelsToUnits(float value, float ppu) => value / ppu;
public static float UnitsToPixels(float value) => UnitsToPixels(value, pixelsPerUnit);
public static float UnitsToPixels(float value, float ppu) => value * ppu;
public static float ClampWithMax(float value, float max) => value > max ? max : value;
public static float ClampWithMin(float value, float min) => value < min ? min : value;
public static float FindTime(float speed, float distance) => distance / speed;
public static float FindDist(float speed, float time) => speed * time;
public static float FindSpeed(float distance, float time) => distance / time;
public static int IndexOfSmallest(float[] values) {
if (values == null || values.Length == 0)
throw new Exception("hey what the fuck, give me some values");
float smallest = values[0];
int index = 0;
for (int i = 1; i < values.Length; i++) {
if (values[i] < smallest) {
smallest = values[i];
index = i;
}
}
return index;
}
public static int IndexOfLargest(float[] values) {
if (values == null || values.Length == 0)
throw new Exception("hey what the fuck, give me some values");
float largest = values[0];
int index = 0;
for (int i = 1; i < values.Length; i++) {
if (values[i] > largest) {
largest = values[i];
index = i;
}
}
return index;
}
public static float ExpLerp01(float t, float exp = 10f) => ExpLerp(0, 1, Mathf.Clamp01(t), exp);
public static float ExpLerp(float a, float b, float t, float exp = 10f) {
return a + Mathf.Pow(Mathf.InverseLerp(a, b, t), exp) * (b - a);
}
public static float AbsMax(float a, float b) {
if (Mathf.Abs(a) >= Mathf.Abs(b))
return a;
return b;
}
public static float AbsMin(float a, float b) {
if (Mathf.Abs(a) <= Mathf.Abs(b))
return a;
return b;
}
public static float RoundToNearest(float value, float[] snaps) {
if (snaps == null)
throw new Exception("um wtf the snaps array u gave me is null :/");
if (snaps.Length == 0)
throw new Exception("lmao fucker u gotta put things in the snaps array to snap to");
float smallestDist = Mathf.Abs(value - snaps[0]);
int index = 0;
for (int i = 1; i < snaps.Length; i++) {
float dist = Mathf.Abs(value - snaps[i]);
if (dist < smallestDist) {
smallestDist = dist;
index = i;
}
}
return snaps[index];
}
#endregion
#region Vector Utils
public static Vector2 InverseLerp(Vector2 a, Vector2 b, Vector2 t) {
return new Vector2(Mathf.InverseLerp(a.x, b.x, t.x), Mathf.InverseLerp(a.y, b.y, t.y));
}
public static Vector2 Lerp(Vector2 a, Vector2 b, Vector2 t) {
return new Vector2(Mathf.Lerp(a.x, b.x, t.x), Mathf.Lerp(a.y, b.y, t.y));
}
#endregion
#region Colour Utils
// based on how unity did it:
// https://stackoverflow.com/questions/61372498/how-does-mathf-smoothdamp-work-what-is-it-algorithm
public static Color ColourSmoothDamp(Color current, Color target, ref Vector4 currentVelocity, float smoothTime) =>
ColourSmoothDamp(current, target, ref currentVelocity, smoothTime, Mathf.Infinity, Time.deltaTime);
public static Color ColourSmoothDamp(Color current, Color target, ref Vector4 currentVelocity, float smoothTime, float maxSpeed) =>
ColourSmoothDamp(current, target, ref currentVelocity, smoothTime, maxSpeed, Time.deltaTime);
public static Color ColourSmoothDamp(Color current, Color target, ref Vector4 currentVelocity, float smoothTime, float maxSpeed, float deltaTime) {
// Based on Game Programming Gems 4 Chapter 1.10
smoothTime = Mathf.Max(0.0001f, smoothTime);
float omega = 2f / smoothTime;
Vector4 currentColour = new Vector4(current.r, current.g, current.b, current.a);
Vector4 targetColour = new Vector4(target.r, target.g, target.b, target.a);
float x = omega * deltaTime;
float exp = 1f / (1f + x + 0.48f * x * x + 0.235f * x * x * x);
Vector4 change = currentColour - targetColour;
Vector4 originalTo = targetColour;
// Clamp maximum speed
Vector4 maxChange = maxSpeed * smoothTime * Vector4.one;
change.x = Mathf.Clamp(change.x, -maxChange.x, maxChange.x);
change.y = Mathf.Clamp(change.y, -maxChange.y, maxChange.y);
change.z = Mathf.Clamp(change.z, -maxChange.z, maxChange.z);
change.w = Mathf.Clamp(change.w, -maxChange.w, maxChange.w);
targetColour = currentColour - change;
Vector4 temp = (currentVelocity + (omega * change)) * deltaTime;
currentVelocity = (currentVelocity - omega * temp) * exp;
Vector4 output = targetColour + (change + temp) * exp;
// Prevent overshooting
if (originalTo.x - currentColour.x > 0.0f == output.x > originalTo.x) {
output.x = originalTo.x;
currentVelocity.x = (output.x - originalTo.x) / deltaTime;
}
if (originalTo.y - currentColour.y > 0.0f == output.y > originalTo.y) {
output.y = originalTo.y;
currentVelocity.y = (output.y - originalTo.y) / deltaTime;
}
if (originalTo.z - currentColour.z > 0.0f == output.z > originalTo.z) {
output.z = originalTo.z;
currentVelocity.z = (output.z - originalTo.z) / deltaTime;
}
if (originalTo.w - currentColour.w > 0.0f == output.w > originalTo.w) {
output.w = originalTo.w;
currentVelocity.w = (output.w - originalTo.w) / deltaTime;
}
return new Color(output.x, output.y, output.z, output.w);
}
#endregion
#region Coroutine Utils
/// <param name="duration">Duration in seconds.</param>
/// <param name="realtime">Should use real time or scaled time?</param>
/// <returns></returns>
public static IEnumerator PauseableWait(float duration, Func<bool> predicate, bool realtime = false) {
for (float elapsed = 0; elapsed < duration; elapsed += (realtime?Time.unscaledDeltaTime : Time.deltaTime)) {
while (predicate.Invoke())
yield return null;
yield return null;
}
}
/// <summary>
/// Call a function every frame for a duration.
/// </summary>
/// <param name="duration">How long call the function for.</param>
/// <param name="action">Function to be invoked and passed the elapsed time.</param>
/// <returns></returns>
public static IEnumerator DoOverTime(float duration, Action<float> action, bool callAgainOnComplete = true, Func<bool> pausePredicate = null) {
for (float elapsed = 0; elapsed < duration; elapsed += Time.deltaTime) {
if (pausePredicate != null && pausePredicate.Invoke())
yield return null;
action.Invoke(elapsed);
yield return null;
}
if (callAgainOnComplete)
action.Invoke(duration);
}
#endregion
#region Generic Utils
public static T DeepCopy<T>(T other) {
using(MemoryStream ms = new MemoryStream()) {
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(ms, other);
ms.Position = 0;
return (T)formatter.Deserialize(ms);
}
}
#endregion
#endregion
#region EXTENSION METHODS
#region Value Extensions
/// <summary>
/// The fractional component of the value.
/// </summary>
public static float Frac(this float f) => f - (int)f;
/// <summary>
/// The absolute fractional component of the value.
/// </summary>
public static float AbsFrac(this float f) => Math.Abs(f - (int)f);
/// <summary>
/// Is the character an uppercase letter?
/// </summary>
public static bool IsUpper(this char c) => c >= 'A' && c <= 'Z';
/// <summary>
/// Is the character a lowercase letter?
/// </summary>
public static bool IsLower(this char c) => c >= 'a' && c <= 'z';
/// <summary>
/// Is the character a letter?
/// </summary>
public static bool IsLetter(this char c) => c.IsUpper() || c.IsLower();
/// <summary>
/// Trim every string in an array.
/// </summary>
public static string[] Trim(this string[] s) {
for (int i = 0; i < s.Length; i++)
s[i] = s[i].Trim();
return s;
}
/// <summary>
/// Puts richtext colour tags around the string.
/// </summary>
public static string Colour(this string s, Color colour) {
string cHex = colour.ToHexRGBA();
return "<color=#" + cHex + ">" + s + "</color>";
}
public static void ClampWithMax(this ref float value, float max) {
if (value > max)
value = max;
}
public static void ClampWithMin(this ref float value, float min) {
if (value < min)
value = min;
}
/// <summary>
/// Is the int even?
/// </summary>
public static bool IsEven(this int i) => i % 2 == 0;
/// <summary>
/// Is the int odd?
/// </summary>
public static bool IsOdd(this int i) => i % 2 != 0;
/// <summary>
/// gets u either -1, 0, 1
/// </summary>
public static float Direction(this float value) {
if (value == 0)
return 0;
return value / Math.Abs(value);
}
/// <summary>
/// Converts HashSet into an array.
/// </summary>
public static T[] ToArray<T>(this HashSet<T> set) {
T[] array = new T[set.Count];
int count = 0;
foreach (T t in set) {
array[count] = t;
count++;
}
return array;
}
#endregion
#region Enum Extensions
/// <summary>
/// Returns how many entries there are in the enum.
/// </summary>
public static int LengthOfEnum<TEnum>(this TEnum t)where TEnum : struct => System.Enum.GetNames(typeof(TEnum)).Length;
/// <summary>
/// Inserts spaces in between words of an enum.
/// </summary>
public static string MakeEnumReadable<TEnum>(this TEnum t)where TEnum : struct, IConvertible {
string entry = t.ToString();
for (int i = 1; i < entry.Length; i++) {
if (entry[i].IsUpper()) {
entry = entry.Insert(i, " ");
i++;
}
}
return entry;
}
#endregion
#region Vector Extensions
public static Vector2 Add(this ref Vector2 v, float x, float y) => v + new Vector2(x, y);
public static Vector3 ToV3(this Vector2 v, float z) => new Vector3(v.x, v.y, z);
public static Vector2 Abs(this Vector2 v) => new Vector2(Mathf.Abs(v.x), Mathf.Abs(v.y));
public static Vector2Int Abs(this Vector2Int v) => new Vector2Int(Mathf.Abs(v.x), Mathf.Abs(v.y));
public static Vector3 Abs(this Vector3 v) => new Vector3(Mathf.Abs(v.x), Mathf.Abs(v.y), Mathf.Abs(v.z));
public static Vector3Int Abs(this Vector3Int v) => new Vector3Int(Mathf.Abs(v.x), Mathf.Abs(v.y), Mathf.Abs(v.z));
public static Vector2 Round(this ref Vector2 v) => v = v.Rounded();
public static Vector3 Round(this ref Vector3 v) => v = v.Rounded();
public static Vector2 Rounded(this Vector2 v) => new Vector2(Mathf.RoundToInt(v.x), Mathf.RoundToInt(v.y));
public static Vector3 Rounded(this Vector3 v) => new Vector3(Mathf.RoundToInt(v.x), Mathf.RoundToInt(v.y), Mathf.RoundToInt(v.z));
public static Vector2Int RoundedToInt(this Vector2 v) => new Vector2Int(Mathf.RoundToInt(v.x), Mathf.RoundToInt(v.y));
public static Vector3Int RoundedToInt(this Vector3 v) => new Vector3Int(Mathf.RoundToInt(v.x), Mathf.RoundToInt(v.y), Mathf.RoundToInt(v.z));
public static Vector2 Floor(this ref Vector2 v) => v = v.Floored();
public static Vector3 Floor(this ref Vector3 v) => v = v.Floored();
public static Vector2 Floored(this Vector2 v) => new Vector2(Mathf.Floor(v.x), Mathf.Floor(v.y));
public static Vector3 Floored(this Vector3 v) => new Vector3(Mathf.Floor(v.x), Mathf.Floor(v.y), Mathf.Floor(v.z));
public static Vector2 Ceil(this ref Vector2 v) => v = v.Ceiled();
public static Vector3 Ceil(this ref Vector3 v) => v = v.Ceiled();
public static Vector2 Ceiled(this Vector2 v) => new Vector2(Mathf.Ceil(v.x), Mathf.Ceil(v.y));
public static Vector3 Ceiled(this Vector3 v) => new Vector3(Mathf.Ceil(v.x), Mathf.Ceil(v.y), Mathf.Ceil(v.z));
public static Vector2 Clamp(this ref Vector2 v, Vector2 a, Vector2 b) {
v.x = Mathf.Clamp(v.x, Mathf.Min(a.x, b.x), Mathf.Max(a.x, b.x));
v.y = Mathf.Clamp(v.y, Mathf.Min(a.y, b.y), Mathf.Max(a.y, b.y));
return v;
}
#endregion
#region Colour Extensions
public static Color WithAlpha(this Color c, float alpha) {
c.a = alpha;
return c;
}
public static string ToHexRGBA(this Color c) {
string r = ((int)Mathf.Lerp(0, 255, c.r)).ToString("X");
string g = ((int)Mathf.Lerp(0, 255, c.g)).ToString("X");
string b = ((int)Mathf.Lerp(0, 255, c.b)).ToString("X");
string a = ((int)Mathf.Lerp(0, 255, c.a)).ToString("X");
return (r.Length == 2 ? r : "0" + r) + (g.Length == 2 ? g : "0" + g) + (b.Length == 2 ? b : "0" + b) + (a.Length == 2 ? a : "0" + a);
}
public static Gradient AsGradient(this Color c) {
Gradient grad = new Gradient();
grad.colorKeys = new GradientColorKey[] {
new GradientColorKey(c, 0),
new GradientColorKey(c, 1),
};
grad.alphaKeys = new GradientAlphaKey[] {
new GradientAlphaKey(c.a, 0),
new GradientAlphaKey(c.a, 1),
};
return grad;
}
#endregion
#region Bounds Extensions
public static bool IsPointInside(this Bounds bounds, Vector2 point) =>
point.x <= bounds.max.x && point.x >= bounds.min.x && point.y <= bounds.max.y && point.y >= bounds.min.y;
#endregion
#region MonoBehaviour Extensions
public static Transform GetGrandChild(this Transform trans) {
if (trans.childCount == 0)
return trans;
return trans.GetChild(0).GetGrandChild();
}
#endregion
#endregion
}