diff --git a/Runtime/Enums.meta b/Runtime/Enums.meta new file mode 100644 index 0000000..b9b98d3 --- /dev/null +++ b/Runtime/Enums.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: b1b99a56b83244e3870c2f7f401db756 +timeCreated: 1717834452 \ No newline at end of file diff --git a/Runtime/Enums/VectorAxes.meta b/Runtime/Enums/VectorAxes.meta new file mode 100644 index 0000000..c608a54 --- /dev/null +++ b/Runtime/Enums/VectorAxes.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 408e3d7fa3354540837bb98b16b02d61 +timeCreated: 1717834533 \ No newline at end of file diff --git a/Runtime/Enums/VectorAxes/Vector2Axis.cs b/Runtime/Enums/VectorAxes/Vector2Axis.cs new file mode 100644 index 0000000..2ee55fc --- /dev/null +++ b/Runtime/Enums/VectorAxes/Vector2Axis.cs @@ -0,0 +1,8 @@ +namespace Better.Commons.Runtime.Enums +{ + public enum Vector2Axis + { + X = 0, + Y = 1, + } +} \ No newline at end of file diff --git a/Runtime/Enums/VectorAxes/Vector2Axis.cs.meta b/Runtime/Enums/VectorAxes/Vector2Axis.cs.meta new file mode 100644 index 0000000..df233a4 --- /dev/null +++ b/Runtime/Enums/VectorAxes/Vector2Axis.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 4c00abbc3e994243a2c739b33035f80f +timeCreated: 1717834466 \ No newline at end of file diff --git a/Runtime/Enums/VectorAxes/Vector3Axis.cs b/Runtime/Enums/VectorAxes/Vector3Axis.cs new file mode 100644 index 0000000..dae08c0 --- /dev/null +++ b/Runtime/Enums/VectorAxes/Vector3Axis.cs @@ -0,0 +1,9 @@ +namespace Better.Commons.Runtime.Enums +{ + public enum Vector3Axis + { + X = 0, + Y = 1, + Z = 2, + } +} \ No newline at end of file diff --git a/Runtime/Enums/VectorAxes/Vector3Axis.cs.meta b/Runtime/Enums/VectorAxes/Vector3Axis.cs.meta new file mode 100644 index 0000000..48b708e --- /dev/null +++ b/Runtime/Enums/VectorAxes/Vector3Axis.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 53595e87bac847d3963339d8a3a0bcf7 +timeCreated: 1717834513 \ No newline at end of file diff --git a/Runtime/Enums/VectorAxes/Vector4Axis.cs b/Runtime/Enums/VectorAxes/Vector4Axis.cs new file mode 100644 index 0000000..1fc5384 --- /dev/null +++ b/Runtime/Enums/VectorAxes/Vector4Axis.cs @@ -0,0 +1,10 @@ +namespace Better.Commons.Runtime.Enums +{ + public enum Vector4Axis + { + X = 0, + Y = 1, + Z = 2, + W = 4, + } +} \ No newline at end of file diff --git a/Runtime/Enums/VectorAxes/Vector4Axis.cs.meta b/Runtime/Enums/VectorAxes/Vector4Axis.cs.meta new file mode 100644 index 0000000..f8f31ef --- /dev/null +++ b/Runtime/Enums/VectorAxes/Vector4Axis.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 76d4b57ca7f749648e2ea67f7d42f89c +timeCreated: 1717834502 \ No newline at end of file diff --git a/Runtime/Extensions/Vector2Extensions.cs b/Runtime/Extensions/Vector2Extensions.cs index fb57d4b..c0e41ae 100644 --- a/Runtime/Extensions/Vector2Extensions.cs +++ b/Runtime/Extensions/Vector2Extensions.cs @@ -1,4 +1,5 @@ -using Better.Commons.Runtime.Utility; +using Better.Commons.Runtime.Enums; +using Better.Commons.Runtime.Utility; using UnityEngine; namespace Better.Commons.Runtime.Extensions @@ -29,5 +30,10 @@ public static Vector2 Abs(this Vector2 self) { return Vector2Utility.Abs(self); } + + public static float GetAxis(this Vector2 self, Vector2Axis axis) + { + return self[(int)axis]; + } } } \ No newline at end of file diff --git a/Runtime/Extensions/Vector3Extensions.cs b/Runtime/Extensions/Vector3Extensions.cs index 9ac9588..b980df0 100644 --- a/Runtime/Extensions/Vector3Extensions.cs +++ b/Runtime/Extensions/Vector3Extensions.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using Better.Commons.Runtime.Enums; using Better.Commons.Runtime.Utility; using Unity.Collections; using UnityEngine; @@ -57,5 +58,10 @@ public static Vector3 Abs(this Vector3 self) { return Vector3Utility.Abs(self); } + + public static float GetAxis(this Vector3 self, Vector3Axis axis) + { + return self[(int)axis]; + } } } \ No newline at end of file diff --git a/Runtime/Extensions/Vector4Extensions.cs b/Runtime/Extensions/Vector4Extensions.cs new file mode 100644 index 0000000..27f63cc --- /dev/null +++ b/Runtime/Extensions/Vector4Extensions.cs @@ -0,0 +1,41 @@ +using System.Collections.Generic; +using Better.Commons.Runtime.Enums; +using Better.Commons.Runtime.Utility; +using Unity.Collections; +using UnityEngine; + +namespace Better.Commons.Runtime.Extensions +{ + public static class Vector4Extensions + { + public static bool Approximately(this Vector4 self, Vector4 other) + { + return Vector4Utility.Approximately(self, other); + } + + public static Vector4 DirectionTo(this Vector4 self, Vector4 to) + { + return Vector4Utility.Direction(self, to); + } + + public static float DistanceTo(this Vector4 self, Vector4 to) + { + return Vector4.Distance(self, to); + } + + public static float SqrDistanceTo(this Vector4 self, Vector4 to) + { + return Vector4Utility.SqrDistanceTo(self, to); + } + + public static Vector4 Abs(this Vector4 self) + { + return Vector4Utility.Abs(self); + } + + public static float GetAxis(this Vector4 self, Vector4Axis axis) + { + return self[(int)axis]; + } + } +} \ No newline at end of file diff --git a/Runtime/Extensions/Vector4Extensions.cs.meta b/Runtime/Extensions/Vector4Extensions.cs.meta new file mode 100644 index 0000000..1e49198 --- /dev/null +++ b/Runtime/Extensions/Vector4Extensions.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: b334c610eff946f4bc81fcd99105f70b +timeCreated: 1717835267 \ No newline at end of file diff --git a/Runtime/Utility/Vector2Utility.cs b/Runtime/Utility/Vector2Utility.cs index b3c2a88..66b0f94 100644 --- a/Runtime/Utility/Vector2Utility.cs +++ b/Runtime/Utility/Vector2Utility.cs @@ -1,4 +1,7 @@ -using UnityEngine; +using System.Collections.Generic; +using Better.Commons.Runtime.Enums; +using Better.Commons.Runtime.Extensions; +using UnityEngine; namespace Better.Commons.Runtime.Utility { @@ -22,6 +25,22 @@ public static Vector2 MiddlePoint(Vector2 start, Vector2 end, Vector2 offset) return middlePoint + offset; } + public static Vector2 SlerpUnclamped(Vector2 a, Vector2 b, float t) + { + var dot = Vector2.Dot(a.normalized, b.normalized); + dot = Mathf.Clamp(dot, -1.0f, 1.0f); + var theta = Mathf.Acos(dot) * t; + var relativeVector = (b - a * dot).normalized; + + return a * Mathf.Cos(theta) + relativeVector * Mathf.Sin(theta); + } + + public static Vector2 Slerp(Vector2 a, Vector2 b, float t) + { + t = Mathf.Clamp01(t); + return SlerpUnclamped(a, b, t); + } + public static Vector2 AxesInverseLerp(Vector2 a, Vector2 b, Vector2 value) { return new Vector2( @@ -62,5 +81,15 @@ public static Vector2 Abs(Vector2 source) source.y = Mathf.Abs(source.y); return source; } + + public static Vector2 ApplyAxes(Vector2 target, Vector2 source, IEnumerable axes) + { + foreach (var axis in axes) + { + target[(int)axis] = source.GetAxis(axis); + } + + return target; + } } } \ No newline at end of file diff --git a/Runtime/Utility/Vector3Utility.cs b/Runtime/Utility/Vector3Utility.cs index 3da90c4..9927014 100644 --- a/Runtime/Utility/Vector3Utility.cs +++ b/Runtime/Utility/Vector3Utility.cs @@ -1,5 +1,7 @@ using System.Collections.Generic; using System.Linq; +using Better.Commons.Runtime.Enums; +using Better.Commons.Runtime.Extensions; using Unity.Collections; using UnityEngine; @@ -140,7 +142,7 @@ public static Vector3 AxesInverseLerp(Vector3 a, Vector3 b, Vector3 value) Mathf.InverseLerp(a.z, b.z, value.z) ); } - + public static Vector3 Flat(Vector3 source) { source.y = default; @@ -166,5 +168,15 @@ public static Vector3 Abs(Vector3 self) self.z = Mathf.Abs(self.z); return self; } + + public static Vector3 ApplyAxes(Vector3 target, Vector3 source, IEnumerable axes) + { + foreach (var axis in axes) + { + target[(int)axis] = source.GetAxis(axis); + } + + return target; + } } } \ No newline at end of file diff --git a/Runtime/Utility/Vector4Utility.cs b/Runtime/Utility/Vector4Utility.cs new file mode 100644 index 0000000..2e72598 --- /dev/null +++ b/Runtime/Utility/Vector4Utility.cs @@ -0,0 +1,102 @@ +using System.Collections.Generic; +using Better.Commons.Runtime.Enums; +using Better.Commons.Runtime.Extensions; +using UnityEngine; + +namespace Better.Commons.Runtime.Utility +{ + public struct Vector4Utility + { + public static bool Approximately(Vector4 current, Vector4 other) + { + return Mathf.Approximately(current.x, other.x) + && Mathf.Approximately(current.y, other.y) + && Mathf.Approximately(current.z, other.z) + && Mathf.Approximately(current.w, other.w); + } + + public static Vector4 MiddlePoint(Vector4 start, Vector4 end) + { + var t = start + end; + return t / 2; + } + + public static Vector4 MiddlePoint(Vector4 start, Vector4 end, Vector4 offset) + { + var middlePoint = MiddlePoint(start, end); + return middlePoint + offset; + } + + public static Vector4 SlerpUnclamped(Vector4 a, Vector4 b, float t) + { + a.Normalize(); + b.Normalize(); + + var dot = Vector4.Dot(a, b); + dot = Mathf.Clamp(dot, -1.0f, 1.0f); + + var theta = Mathf.Acos(dot) * t; + var relativeVector = b - a * dot; + relativeVector.Normalize(); + + return a * Mathf.Cos(theta) + relativeVector * Mathf.Sin(theta); + } + + public static Vector4 Slerp(Vector4 a, Vector4 b, float t) + { + t = Mathf.Clamp01(t); + return SlerpUnclamped(a, b, t); + } + + public static Vector4 AxesInverseLerp(Vector4 a, Vector4 b, Vector4 value) + { + return new Vector4( + Mathf.InverseLerp(a.x, b.x, value.x), + Mathf.InverseLerp(a.y, b.y, value.y) + ); + } + + public static float InverseLerp(Vector4 a, Vector4 b, Vector4 value) + { + if (a == b) + { + return default; + } + + var ab = b - a; + var av = value - a; + + var result = Vector4.Dot(av, ab) / Vector4.Dot(ab, ab); + return Mathf.Clamp01(result); + } + + public static Vector4 Direction(Vector4 from, Vector4 to) + { + var difference = to - from; + return difference.normalized; + } + + public static float SqrDistanceTo(Vector4 from, Vector4 to) + { + var difference = to - from; + return difference.sqrMagnitude; + } + + public static Vector4 Abs(Vector4 source) + { + source.x = Mathf.Abs(source.x); + source.y = Mathf.Abs(source.y); + return source; + } + + public static Vector4 ApplyAxes(Vector4 target, Vector4 source, IEnumerable axes) + { + foreach (var axis in axes) + { + target[(int)axis] = source.GetAxis(axis); + } + + return target; + } + } +} \ No newline at end of file diff --git a/Runtime/Utility/Vector4Utility.cs.meta b/Runtime/Utility/Vector4Utility.cs.meta new file mode 100644 index 0000000..d592370 --- /dev/null +++ b/Runtime/Utility/Vector4Utility.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 71fc5d73db3246168b34816cf51c1536 +timeCreated: 1717834198 \ No newline at end of file diff --git a/package.json b/package.json index 63ae647..cc141d0 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "com.tdw.better.commons", "displayName": "Better Commons", - "version": "0.0.10", + "version": "0.0.11", "unity": "2021.3", "description": " ", "dependencies": {