48 lines
1.7 KiB
C#
48 lines
1.7 KiB
C#
|
|
using System;
|
||
|
|
using UnityEngine;
|
||
|
|
using UnityEngine.TestTools;
|
||
|
|
|
||
|
|
namespace BracerLib.Utility
|
||
|
|
{
|
||
|
|
[ExcludeFromCoverage]
|
||
|
|
public static class Vector3Extensions
|
||
|
|
{
|
||
|
|
public static Vector3 With(this Vector3 vector, float? x = null, float? y = null, float? z = null) => new(x ?? vector.x, y ?? vector.y, z ?? vector.z);
|
||
|
|
|
||
|
|
public static Vector3 Add(this Vector3 vector, float? x = null, float? y = null, float? z = null)
|
||
|
|
=> new(
|
||
|
|
x != null ? vector.x + x.Value : vector.x,
|
||
|
|
y != null ? vector.y + y.Value : vector.y,
|
||
|
|
z != null ? vector.z + z.Value : vector.z
|
||
|
|
);
|
||
|
|
/// <summary>
|
||
|
|
/// Take an existing Vector3 and truncate all its values to a certain number of decimal places.
|
||
|
|
/// </summary>
|
||
|
|
public static Vector3 Truncate(this Vector3 value, int decimalPlaces = 3)
|
||
|
|
{
|
||
|
|
double adjust = 0.5f / Mathf.Pow(10, decimalPlaces);
|
||
|
|
|
||
|
|
return new Vector3(
|
||
|
|
(float)Math.Round(value.x - adjust, decimalPlaces),
|
||
|
|
(float)Math.Round(value.y - adjust, decimalPlaces),
|
||
|
|
(float)Math.Round(value.z - adjust, decimalPlaces)
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
public static Vector3 Floor(this Vector3 value)
|
||
|
|
{
|
||
|
|
return new Vector3(Mathf.Floor(value.x), Mathf.Floor(value.y), Mathf.Floor(value.z));
|
||
|
|
}
|
||
|
|
|
||
|
|
public static Vector3 Ceiling(this Vector3 value)
|
||
|
|
{
|
||
|
|
return new Vector3(Mathf.Ceil(value.x), Mathf.Ceil(value.y), Mathf.Ceil(value.z));
|
||
|
|
}
|
||
|
|
|
||
|
|
public static Vector3 ProjectToLine(this Vector3 point, Vector3 lineStart, Vector3 lineEnd)
|
||
|
|
{
|
||
|
|
return Vector3.Project(point - lineStart, lineEnd - lineStart) + lineStart;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|