-
Notifications
You must be signed in to change notification settings - Fork 0
/
Extensions.cs
585 lines (485 loc) · 17 KB
/
Extensions.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
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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
using System.Runtime.CompilerServices;
namespace Godot;
public static class StringExtensions
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string Interned(this string s)
{
return s switch {
null => null,
_ => string.Intern(s)
};
}
}
public static partial class FloatExtensions
{
/// <summary>
/// To make float interpolation consistent with other 'lerp'able types.
/// <returns></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float Lerp(this float f, float to, float w) {
return f + (w * (to - f));
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float InverseLerp(this float f, float from, float to) {
return(f - from) / (to - from);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsZeroApprox(this float f) {
return MathF.Abs(f) < 1E-06f;
}
/// https://github.com/godotengine/godot/blob/4a0160241fd0c1e874e297f6b08676cf0761e5e8/core/math/math_funcs.h#L599
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsEqualApprox(this float f, float b, float tolerance = 8.854187817f)
{
if (f == b)
return true;
return MathF.Abs(f - b) < tolerance;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float Min(this float f, float other) {
return MathF.Min(f, other);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float Max(this float f, float other) {
return MathF.Max(f, other);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float Clamped(this float f, float min, float max) {
return MathF.Max(min, MathF.Min(max, f));
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float Abs(this float f) {
return MathF.Abs(f);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float Squared(this float f) {
return f * f;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float Cubed(this float f) {
return f * f * f;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float Pow(this float f, float power) {
return MathF.Pow(f, power);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float Sqrt(this float f) {
return MathF.Sqrt(f);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector2 AsVector2(this float v) {
return new(v, v);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector3 AsVector3(this float v) {
return new(v, v, v);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Color AsColour(this float v, float a = 1.0f) {
return new(v, v, v, a);
}
}
public static class DoubleExtensions
{
/// <summary>
/// To make float interpolation consistent with other 'lerp'able types.
/// <returns></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double Lerp(this double f, double to, double w) {
return f + (w * (to - f));
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsZeroApprox(this double f) {
return Math.Abs(f) < 1E-06f;
}
}
public static class GodotObjectExtensions
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T GetMeta<[MustBeVariant] T>(this GodotObject @object, StringName id) {
return @object.GetMeta(id).As<T>();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T GetMetaOr<[MustBeVariant] T>(this GodotObject @object, StringName id, T defaultValue = default)
{
if ([email protected](id))
return defaultValue;
return @object.GetMeta(id).As<T>();
}
}
public static class Vector2Extensions
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector2 ReplaceX(this Vector2 v, float x)
=> new(x, v.Y);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector2 ReplaceY(this Vector2 v, float y)
=> new(v.X, y);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector2 Offset(this Vector2 v, float x, float y) {
return new(v.X + x, v.Y + y);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector2 Rotated90(this Vector2 v) {
return v.Rotated(0.25f * MathF.Tau);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector2 Rotated180(this Vector2 v) {
return v.Rotated(0.5f * MathF.Tau);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector2 RotatedNeg90(this Vector2 v) {
return v.Rotated(-0.25f * MathF.Tau);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector2 RotatedNeg180(this Vector2 v) {
return v.Rotated(-0.5f * MathF.Tau);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector2 Scaled(this Vector2 v, float s) {
return new(v.X * s, v.Y * s);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector2 Scaled(this Vector2 v, float x, float y) {
return new(v.X * x, v.Y * y);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector2 ScaledX(this Vector2 v, float x) {
return new(v.X * x, v.Y);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector2 ScaledY(this Vector2 v, float y) {
return new(v.X, v.Y * y);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsXDominant(this Vector2 v) {
Vector2 absV = v.Abs();
return (absV.X > absV.Y);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector2 XFlattened(this Vector2 v) {
return new(0.0f, v.Y);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector2 YFlattened(this Vector2 v) {
return new(v.X, 0.0f);
}
}
public static class Vector3Extensions
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector3 ReplaceX(this Vector3 v, float x)
=> new(x, v.Y, v.Z);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector3 ReplaceY(this Vector3 v, float y)
=> new(v.X, y, v.Z);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector3 ReplaceZ(this Vector3 v, float z)
=> new(v.X, v.Y, z);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector3 Offset(this Vector3 v, float x, float y, float z) {
return new(v.X + x, v.Y + y, v.Z + z);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector2 XZAsVec2(this Vector3 v) {
return new(v.X, v.Z);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float XZSquareMagnitude(this Vector3 v) {
return (v.X * v.X) + (v.Z * v.Z);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float XZMagnitude(this Vector3 v) {
return MathF.Sqrt((v.X * v.X) + (v.Z * v.Z));
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector3 XZNormalised(this Vector3 v, bool zeroY = true)
{
float len = v.XZMagnitude();
return new(v.X / len, zeroY ? 0.0f : v.Y, v.Z / len);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector3 XZFlattened(this Vector3 v)
{
return new(v.X, 0.0f, v.Z);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector3 XZFlattenedN(this Vector3 v)
{
return new Vector3(v.X, 0.0f, v.Z).Normalized();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector3 XZScale(this Vector3 v, float amount)
{
Vector3 scaled = v;
scaled.X *= amount;
scaled.Z *= amount;
return scaled;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector3 XZLerp(this Vector3 v, Vector3 o, float w)
{
Vector3 result = v;
result.X = result.X.Lerp(o.X, w);
result.Z = result.Z.Lerp(o.Z, w);
return result;
}
}
public static class ColourExtensions
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Color Clamped(this Color colour)
{
return new(
colour.R.Clamped(0.0f, 1.0f),
colour.G.Clamped(0.0f, 1.0f),
colour.B.Clamped(0.0f, 1.0f),
colour.A.Clamped(0.0f, 1.0f)
);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Color ShiftH(this Color colour, float hue) {
return Color.FromHsv(colour.H + hue, colour.S, colour.V);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Color ShiftS(this Color colour, float saturation) {
return Color.FromHsv(colour.H, colour.S + saturation, colour.V);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Color ShiftV(this Color colour, float value) {
return Color.FromHsv(colour.H, colour.S, colour.V + value);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Color ShiftSV(this Color colour, float saturation, float value) {
return Color.FromHsv(colour.H, colour.S + saturation, colour.V + value);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Color ShiftHSV(this Color colour, float hue, float saturation, float value) {
return Color.FromHsv(colour.H + hue, colour.S + saturation, colour.V + value);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Color ReplaceR(this Color colour, float r) {
return new(r, colour.G, colour.B, colour.A);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Color ReplaceG(this Color colour, float g) {
return new(colour.R, g, colour.B, colour.A);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Color ReplaceB(this Color colour, float b) {
return new(colour.R, colour.G, b, colour.A);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Color ReplaceA(this Color colour, float a) {
return new(colour.R, colour.G, colour.B, a);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Color ReplaceH(this Color colour, float h) {
return Color.FromHsv(h, colour.S, colour.V);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Color ReplaceS(this Color colour, float s) {
return Color.FromHsv(colour.H, s, colour.V);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Color ReplaceV(this Color colour, float v) {
return Color.FromHsv(colour.H, colour.S, v);
}
}
public static class Trans3DExtensions
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Transform3D SetForward(this Transform3D t, float w, Vector3 forward, Vector3? up = null)
{
Transform3D nt = t;
nt.Basis.Z = forward;
nt.Basis.Y = up ?? Vector3.Up;
nt.Basis.X = nt.Basis.Y.Cross(forward);
nt.Basis = nt.Basis.Orthonormalized();
return t.InterpolateWith(nt, w);
}
}
#region Nodes
public static class NodeExtensions
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void RemoveFromParent(this Node node)
{
Node parent = node.GetParentOrNull<Node>();
if (parent is null)
return;
parent.RemoveChild(node);
}
/// <summary>
/// A shorthand for getting nodes with the exact same name as their type.
/// </summary>
/// <returns></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T GetNodeComponent<T>(this Node node) where T: Node {
return node.GetNode<T>(typeof(T).Name);
}
}
public static class Node2DExtensions
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T SpawnPackage<T>(
this Node2D node,
PackedScene scene,
Vector2 position,
PackedScene.GenEditState editState = PackedScene.GenEditState.Disabled)
where T: Node2D
{
if (!GodotObject.IsInstanceValid(scene))
return null;
T instance = scene.Instantiate<T>(editState);
node.AddChild(instance);
instance.GlobalPosition = position;
return instance;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void SpawnPackage(
this Node2D node,
PackedScene scene,
Vector2 position,
PackedScene.GenEditState editState = PackedScene.GenEditState.Disabled)
{
if (!GodotObject.IsInstanceValid(scene))
return;
Node2D instance = scene.Instantiate<Node2D>(editState);
node.AddChild(instance);
instance.GlobalPosition = position;
}
}
public static class Node3DExtensions
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T SpawnPackage<T>(
this Node3D node,
PackedScene scene,
Vector3 position,
PackedScene.GenEditState editState = PackedScene.GenEditState.Disabled)
where T: Node3D
{
if (!GodotObject.IsInstanceValid(scene))
return null;
T instance = scene.Instantiate<T>(editState);
node.AddChild(instance);
instance.GlobalPosition = position;
return instance;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void SpawnPackage(
this Node3D node,
PackedScene scene,
Vector3 position,
PackedScene.GenEditState editState = PackedScene.GenEditState.Disabled)
{
if (!GodotObject.IsInstanceValid(scene))
return;
Node3D instance = scene.Instantiate<Node3D>(editState);
node.AddChild(instance);
instance.GlobalPosition = position;
}
}
public static class Rigidbody3DExtensions
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void ApplyTorqueAndImpulse(
this RigidBody3D rb,
Vector3 force,
Vector3 torque)
{
rb.ApplyImpulse(force);
rb.ApplyTorque(torque);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void ApplyVisualTorqueAndImpulse(
this RigidBody3D rb,
Vector3 force,
Vector3 torque)
{
rb.ApplyImpulse(force);
rb.ApplyTorqueImpulse(torque);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void ApplyTorqueAndImpulse(
this RigidBody3D rb,
Vector3 force,
Vector3 torque,
Vector3 fromPosition)
{
rb.ApplyImpulse(force, fromPosition);
rb.ApplyTorque(torque);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void ApplyVisualTorqueAndImpulse(
this RigidBody3D rb,
Vector3 force,
Vector3 torque,
Vector3 fromPosition)
{
rb.ApplyImpulse(force, fromPosition);
rb.ApplyTorqueImpulse(torque);
}
}
#endregion
# region Godot Collections
public static class GArrayExtensions
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsEmpty(this Collections.Array a) {
return a.Count < 1;
}
}
public static class GDictExtensions
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsEmpty(this Collections.Dictionary d) {
return d.Count < 1;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T Get<[MustBeVariant] T>(this Collections.Dictionary d, StringName key) {
return d[key].As<T>();
}
}
public static class PackedSceneExtensions
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T Spawn<T>(
this PackedScene scene,
Node parent,
Vector3 position,
PackedScene.GenEditState editState = PackedScene.GenEditState.Disabled)
where T: Node3D
{
T instance = scene.Instantiate<T>(editState);
parent.AddChild(instance);
instance.GlobalPosition = position;
return instance;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Spawn2D(
this PackedScene scene,
Node parent,
Vector2 position,
PackedScene.GenEditState editState = PackedScene.GenEditState.Disabled)
{
Node2D instance = scene.Instantiate<Node2D>(editState);
parent.AddChild(instance);
instance.GlobalPosition = position;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Spawn3D(
this PackedScene scene,
Node parent,
Vector3 position,
PackedScene.GenEditState editState = PackedScene.GenEditState.Disabled)
{
Node3D instance = scene.Instantiate<Node3D>(editState);
parent.AddChild(instance);
instance.GlobalPosition = position;
}
}
#endregion