Skip to content

Commit

Permalink
Merge pull request #14 from techno-dwarf-works/feature/refactoring
Browse files Browse the repository at this point in the history
Version 0.0.11
  • Loading branch information
OpOpYaDev committed Jun 8, 2024
1 parent c58eb07 commit b3adc72
Show file tree
Hide file tree
Showing 17 changed files with 248 additions and 4 deletions.
3 changes: 3 additions & 0 deletions Runtime/Enums.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Runtime/Enums/VectorAxes.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Runtime/Enums/VectorAxes/Vector2Axis.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Better.Commons.Runtime.Enums
{
public enum Vector2Axis
{
X = 0,
Y = 1,
}
}
3 changes: 3 additions & 0 deletions Runtime/Enums/VectorAxes/Vector2Axis.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions Runtime/Enums/VectorAxes/Vector3Axis.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Better.Commons.Runtime.Enums
{
public enum Vector3Axis
{
X = 0,
Y = 1,
Z = 2,
}
}
3 changes: 3 additions & 0 deletions Runtime/Enums/VectorAxes/Vector3Axis.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions Runtime/Enums/VectorAxes/Vector4Axis.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Better.Commons.Runtime.Enums
{
public enum Vector4Axis
{
X = 0,
Y = 1,
Z = 2,
W = 4,
}
}
3 changes: 3 additions & 0 deletions Runtime/Enums/VectorAxes/Vector4Axis.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion Runtime/Extensions/Vector2Extensions.cs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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];
}
}
}
6 changes: 6 additions & 0 deletions Runtime/Extensions/Vector3Extensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using Better.Commons.Runtime.Enums;
using Better.Commons.Runtime.Utility;
using Unity.Collections;
using UnityEngine;
Expand Down Expand Up @@ -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];
}
}
}
41 changes: 41 additions & 0 deletions Runtime/Extensions/Vector4Extensions.cs
Original file line number Diff line number Diff line change
@@ -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];
}
}
}
3 changes: 3 additions & 0 deletions Runtime/Extensions/Vector4Extensions.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 30 additions & 1 deletion Runtime/Utility/Vector2Utility.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand All @@ -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(
Expand Down Expand Up @@ -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<Vector2Axis> axes)
{
foreach (var axis in axes)
{
target[(int)axis] = source.GetAxis(axis);
}

return target;
}
}
}
14 changes: 13 additions & 1 deletion Runtime/Utility/Vector3Utility.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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;
Expand All @@ -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<Vector3Axis> axes)
{
foreach (var axis in axes)
{
target[(int)axis] = source.GetAxis(axis);
}

return target;
}
}
}
102 changes: 102 additions & 0 deletions Runtime/Utility/Vector4Utility.cs
Original file line number Diff line number Diff line change
@@ -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<Vector4Axis> axes)
{
foreach (var axis in axes)
{
target[(int)axis] = source.GetAxis(axis);
}

return target;
}
}
}
3 changes: 3 additions & 0 deletions Runtime/Utility/Vector4Utility.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down

0 comments on commit b3adc72

Please sign in to comment.