1
0
mirror of https://github.com/oliverbooth/X10D synced 2024-11-22 19:18:46 +00:00

Add System.Numerics/UnityEngine vector/quaternion conversions

This commit is contained in:
Oliver Booth 2022-05-09 20:40:55 +01:00
parent d17e1670de
commit 1428618ca9
No known key found for this signature in database
GPG Key ID: 32A00B35503AF634
10 changed files with 295 additions and 4 deletions

View File

@ -44,6 +44,8 @@
- X10D.Unity: Added `GameObject.SetLayerRecursively(int)`
- X10D.Unity: Added `GameObject.SetParent(GameObject[, bool])`
- X10D.Unity: Added `GameObject.SetParent(Transform[, bool])`
- X10D.Unity: Added `System.Numerics.Quaternion.ToUnityQuaternion()`
- X10D.Unity: Added `Quaternion.ToSystemQuaternion()`
- X10D.Unity: Added `Random.NextColorArgb()`
- X10D.Unity: Added `Random.NextColor32Argb()`
- X10D.Unity: Added `Random.NextColorRgb()`
@ -54,6 +56,12 @@
- X10D.Unity: Added `Random.NextUnitVector3()`
- X10D.Unity: Added `Transform.LookAt(GameObject[, Vector3])`
- X10D.Unity: Added `Transform.SetParent(GameObject[, bool])`
- X10D.Unity: Added `System.Numerics.Vector2.ToUnityVector()`
- X10D.Unity: Added `System.Numerics.Vector3.ToUnityVector()`
- X10D.Unity: Added `System.Numerics.Vector4.ToUnityVector()`
- X10D.Unity: Added `Vector2.ToSystemVector()`
- X10D.Unity: Added `Vector3.ToSystemVector()`
- X10D.Unity: Added `Vector4.ToSystemVector()`
- X10D.Unity: Added `Vector2.WithX()`
- X10D.Unity: Added `Vector2.WithY()`
- X10D.Unity: Added `Vector3.WithX()`

View File

@ -0,0 +1,53 @@
using System.Collections;
using NUnit.Framework;
using UnityEngine;
using UnityEngine.TestTools;
using X10D.Core;
using X10D.Unity.Numerics;
using Random = System.Random;
namespace X10D.Unity.Tests.Numerics
{
public class QuaternionTests
{
[UnityTest]
public IEnumerator ToSystemQuaternion_ShouldReturnQuaternion_WithEqualComponents()
{
var random = new Random();
float x = random.NextSingle();
float y = random.NextSingle();
float z = random.NextSingle();
float w = random.NextSingle();
var quaternion = new Quaternion(x, y, z, w);
var systemQuaternion = quaternion.ToSystemQuaternion();
Assert.AreEqual(quaternion.x, systemQuaternion.X, 1e-6f);
Assert.AreEqual(quaternion.y, systemQuaternion.Y, 1e-6f);
Assert.AreEqual(quaternion.z, systemQuaternion.Z, 1e-6f);
Assert.AreEqual(quaternion.w, systemQuaternion.W, 1e-6f);
yield break;
}
[UnityTest]
public IEnumerator ToUnityQuaternion_ShouldReturnQuaternion_WithEqualComponents()
{
var random = new Random();
float x = random.NextSingle();
float y = random.NextSingle();
float z = random.NextSingle();
float w = random.NextSingle();
var quaternion = new System.Numerics.Quaternion(x, y, z, w);
var unityQuaternion = quaternion.ToUnityQuaternion();
Assert.AreEqual(quaternion.X, unityQuaternion.x, 1e-6f);
Assert.AreEqual(quaternion.Y, unityQuaternion.y, 1e-6f);
Assert.AreEqual(quaternion.Z, unityQuaternion.z, 1e-6f);
Assert.AreEqual(quaternion.W, unityQuaternion.w, 1e-6f);
yield break;
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: a395fec41c5a4e9d9ffb05324e8159b0
timeCreated: 1652124913

View File

@ -2,12 +2,48 @@
using NUnit.Framework;
using UnityEngine;
using UnityEngine.TestTools;
using X10D.Core;
using X10D.Unity.Numerics;
using Random = System.Random;
namespace X10D.Unity.Tests.Numerics
{
public class Vector2Tests
{
[UnityTest]
public IEnumerator ToSystemVector_ShouldReturnVector_WithEqualComponents()
{
var random = new Random();
float x = random.NextSingle();
float y = random.NextSingle();
var vector = new Vector2(x, y);
var systemVector = vector.ToSystemVector();
Assert.AreEqual(vector.magnitude, systemVector.Length(), 1e-6f);
Assert.AreEqual(vector.x, systemVector.X, 1e-6f);
Assert.AreEqual(vector.y, systemVector.Y, 1e-6f);
yield break;
}
[UnityTest]
public IEnumerator ToUnityVector_ShouldReturnVector_WithEqualComponents()
{
var random = new Random();
float x = random.NextSingle();
float y = random.NextSingle();
var vector = new System.Numerics.Vector2(x, y);
var unityVector = vector.ToUnityVector();
Assert.AreEqual(vector.Length(), unityVector.magnitude, 1e-6f);
Assert.AreEqual(vector.X, unityVector.x, 1e-6f);
Assert.AreEqual(vector.Y, unityVector.y, 1e-6f);
yield break;
}
[UnityTest]
public IEnumerator WithX_ShouldReturnVectorWithNewX_GivenVector()
{

View File

@ -2,12 +2,52 @@
using NUnit.Framework;
using UnityEngine;
using UnityEngine.TestTools;
using X10D.Core;
using X10D.Unity.Numerics;
using Random = System.Random;
namespace X10D.Unity.Tests.Numerics
{
public class Vector3Tests
{
[UnityTest]
public IEnumerator ToSystemVector_ShouldReturnVector_WithEqualComponents()
{
var random = new Random();
float x = random.NextSingle();
float y = random.NextSingle();
float z = random.NextSingle();
var vector = new Vector3(x, y, z);
var systemVector = vector.ToSystemVector();
Assert.AreEqual(vector.magnitude, systemVector.Length(), 1e-6f);
Assert.AreEqual(vector.x, systemVector.X, 1e-6f);
Assert.AreEqual(vector.y, systemVector.Y, 1e-6f);
Assert.AreEqual(vector.z, systemVector.Z, 1e-6f);
yield break;
}
[UnityTest]
public IEnumerator ToUnityVector_ShouldReturnVector_WithEqualComponents()
{
var random = new Random();
float x = random.NextSingle();
float y = random.NextSingle();
float z = random.NextSingle();
var vector = new System.Numerics.Vector3(x, y, z);
var unityVector = vector.ToUnityVector();
Assert.AreEqual(vector.Length(), unityVector.magnitude, 1e-6f);
Assert.AreEqual(vector.X, unityVector.x, 1e-6f);
Assert.AreEqual(vector.Y, unityVector.y, 1e-6f);
Assert.AreEqual(vector.Z, unityVector.z, 1e-6f);
yield break;
}
[UnityTest]
public IEnumerator WithX_ShouldReturnVectorWithNewX_GivenVector()
{

View File

@ -2,12 +2,56 @@
using NUnit.Framework;
using UnityEngine;
using UnityEngine.TestTools;
using X10D.Core;
using X10D.Unity.Numerics;
using Random = System.Random;
namespace X10D.Unity.Tests.Numerics
{
public class Vector4Tests
{
[UnityTest]
public IEnumerator ToSystemVector_ShouldReturnVector_WithEqualComponents()
{
var random = new Random();
float x = random.NextSingle();
float y = random.NextSingle();
float z = random.NextSingle();
float w = random.NextSingle();
var vector = new Vector4(x, y, z, w);
var systemVector = vector.ToSystemVector();
Assert.AreEqual(vector.magnitude, systemVector.Length(), 1e-6f);
Assert.AreEqual(vector.x, systemVector.X, 1e-6f);
Assert.AreEqual(vector.y, systemVector.Y, 1e-6f);
Assert.AreEqual(vector.z, systemVector.Z, 1e-6f);
Assert.AreEqual(vector.w, systemVector.W, 1e-6f);
yield break;
}
[UnityTest]
public IEnumerator ToUnityVector_ShouldReturnVector_WithEqualComponents()
{
var random = new Random();
float x = random.NextSingle();
float y = random.NextSingle();
float z = random.NextSingle();
float w = random.NextSingle();
var vector = new System.Numerics.Vector4(x, y, z, w);
var unityVector = vector.ToUnityVector();
Assert.AreEqual(vector.Length(), unityVector.magnitude, 1e-6f);
Assert.AreEqual(vector.X, unityVector.x, 1e-6f);
Assert.AreEqual(vector.Y, unityVector.y, 1e-6f);
Assert.AreEqual(vector.Z, unityVector.z, 1e-6f);
Assert.AreEqual(vector.W, unityVector.w, 1e-6f);
yield break;
}
[UnityTest]
public IEnumerator WithW_ShouldReturnVectorWithNewW_GivenVector()
{
@ -24,7 +68,7 @@ namespace X10D.Unity.Tests.Numerics
Assert.AreEqual(new Vector4(1, 0, 0, 1), new Vector4(1, 0, 0, 0).WithW(1));
Assert.AreEqual(new Vector4(0, 1, 0, 1), new Vector4(0, 1, 0, 0).WithW(1));
Assert.AreEqual(new Vector4(0, 0, 1, 1), new Vector4(0, 0, 1, 0).WithW(1));
yield break;
}
@ -44,7 +88,7 @@ namespace X10D.Unity.Tests.Numerics
Assert.AreEqual(new Vector4(1, 0, 0, 0), new Vector4(1, 0, 0, 0).WithX(1));
Assert.AreEqual(new Vector4(1, 1, 0, 0), new Vector4(0, 1, 0, 0).WithX(1));
Assert.AreEqual(new Vector4(1, 0, 1, 0), new Vector4(0, 0, 1, 0).WithX(1));
yield break;
}
@ -64,7 +108,7 @@ namespace X10D.Unity.Tests.Numerics
Assert.AreEqual(new Vector4(1, 1, 0, 0), new Vector4(1, 0, 0, 0).WithY(1));
Assert.AreEqual(new Vector4(0, 1, 0, 0), new Vector4(0, 1, 0, 0).WithY(1));
Assert.AreEqual(new Vector4(0, 1, 1, 0), new Vector4(0, 0, 1, 0).WithY(1));
yield break;
}
@ -84,7 +128,7 @@ namespace X10D.Unity.Tests.Numerics
Assert.AreEqual(new Vector4(1, 0, 1, 0), new Vector4(1, 0, 0, 0).WithZ(1));
Assert.AreEqual(new Vector4(0, 1, 1, 0), new Vector4(0, 1, 0, 0).WithZ(1));
Assert.AreEqual(new Vector4(0, 0, 1, 0), new Vector4(0, 0, 1, 0).WithZ(1));
yield break;
}
}

View File

@ -0,0 +1,35 @@
using System.Diagnostics.Contracts;
using System.Runtime.CompilerServices;
using UnityEngine;
namespace X10D.Unity.Numerics;
/// <summary>
/// Numeric-extensions for <see cref="Quaternion" />.
/// </summary>
public static class QuaternionExtensions
{
/// <summary>
/// Converts the current quaternion to a <see cref="System.Numerics.Quaternion" />.
/// </summary>
/// <param name="quaternion">The quaternion to convert.</param>
/// <returns>The converted quaternion.</returns>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static System.Numerics.Quaternion ToSystemQuaternion(this Quaternion quaternion)
{
return new System.Numerics.Quaternion(quaternion.x, quaternion.y, quaternion.z, quaternion.w);
}
/// <summary>
/// Converts the current quaternion to a <see cref="Quaternion" />.
/// </summary>
/// <param name="quaternion">The quaternion to convert.</param>
/// <returns>The converted quaternion.</returns>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Quaternion ToUnityQuaternion(this System.Numerics.Quaternion quaternion)
{
return new Quaternion(quaternion.X, quaternion.Y, quaternion.Z, quaternion.W);
}
}

View File

@ -9,6 +9,30 @@ namespace X10D.Unity.Numerics;
/// </summary>
public static class Vector2Extensions
{
/// <summary>
/// Converts the current vector to a <see cref="System.Numerics.Vector2" />.
/// </summary>
/// <param name="vector">The vector to convert.</param>
/// <returns>The converted vector.</returns>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static System.Numerics.Vector2 ToSystemVector(this Vector2 vector)
{
return new System.Numerics.Vector2(vector.x, vector.y);
}
/// <summary>
/// Converts the current vector to a <see cref="Vector2" />.
/// </summary>
/// <param name="vector">The vector to convert.</param>
/// <returns>The converted vector.</returns>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector2 ToUnityVector(this System.Numerics.Vector2 vector)
{
return new Vector2(vector.X, vector.Y);
}
/// <summary>
/// Returns a vector whose Y component is the same as the specified vector, and whose X component is a new value.
/// </summary>

View File

@ -9,6 +9,30 @@ namespace X10D.Unity.Numerics;
/// </summary>
public static class Vector3Extensions
{
/// <summary>
/// Converts the current vector to a <see cref="System.Numerics.Vector3" />.
/// </summary>
/// <param name="vector">The vector to convert.</param>
/// <returns>The converted vector.</returns>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static System.Numerics.Vector3 ToSystemVector(this Vector3 vector)
{
return new System.Numerics.Vector3(vector.x, vector.y, vector.z);
}
/// <summary>
/// Converts the current vector to a <see cref="Vector3" />.
/// </summary>
/// <param name="vector">The vector to convert.</param>
/// <returns>The converted vector.</returns>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector3 ToUnityVector(this System.Numerics.Vector3 vector)
{
return new Vector3(vector.X, vector.Y, vector.Z);
}
/// <summary>
/// Returns a vector whose Y and Z components are the same as the specified vector, and whose X component is a new value.
/// </summary>

View File

@ -9,6 +9,30 @@ namespace X10D.Unity.Numerics;
/// </summary>
public static class Vector4Extensions
{
/// <summary>
/// Converts the current vector to a <see cref="System.Numerics.Vector4" />.
/// </summary>
/// <param name="vector">The vector to convert.</param>
/// <returns>The converted vector.</returns>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static System.Numerics.Vector4 ToSystemVector(this Vector4 vector)
{
return new System.Numerics.Vector4(vector.x, vector.y, vector.z, vector.w);
}
/// <summary>
/// Converts the current vector to a <see cref="Vector4" />.
/// </summary>
/// <param name="vector">The vector to convert.</param>
/// <returns>The converted vector.</returns>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector4 ToUnityVector(this System.Numerics.Vector4 vector)
{
return new Vector4(vector.X, vector.Y, vector.Z, vector.W);
}
/// <summary>
/// Returns a vector whose Y, Z, and W components are the same as the specified vector, and whose X component is a new
/// value.