Skip to content

Instantly share code, notes, and snippets.

@wildbook
Last active June 28, 2017 08:36
Show Gist options
  • Save wildbook/e8b76cc79d2face00f2f01b18523dc11 to your computer and use it in GitHub Desktop.
Save wildbook/e8b76cc79d2face00f2f01b18523dc11 to your computer and use it in GitHub Desktop.
Vector utilities for Unity3D
using System;
using UnityEngine;
namespace Wildbook.Utilities
{
public static class VectorUtilities
{
#region Modify existing variable
/// <summary>
/// Modifies the specified axis of a Vector4 variable.
/// </summary>
/// <example>
/// <code>
/// Vector4 myVector = new Vector4(0, 0, 0, 0);
/// Vector.Modify(ref myVector, z: 10, w: 2);
/// </code>
/// </example>
/// <param name="def">The Vector4 to manipulate</param>
/// <param name="x">The new x.</param>
/// <param name="y">The new y.</param>
/// <param name="z">The new z.</param>
/// <param name="w">The new w.</param>
/// <returns></returns>
public static void Modify(ref Vector4 def, float? x = null, float? y = null, float? z = null, float? w = null)
{
def.Set(x ?? def.x, y ?? def.y, z ?? def.z, w ?? def.w);
}
/// <summary>
/// Modifies the specified axis of a Vector3 variable.
/// </summary>
/// <example>
/// <code>
/// Vector3 myVector = new Vector3(0, 0, 0);
/// Vector.Modify(ref myVector, x: 3.1f, z: 0);
/// </code>
/// </example>
/// <param name="def">The Vector3 to manipulate</param>
/// <param name="x">The new x.</param>
/// <param name="y">The new y.</param>
/// <param name="z">The new z.</param>
/// <returns></returns>
public static void Modify(ref Vector3 def, float? x = null, float? y = null, float? z = null)
{
def.Set(x ?? def.x, y ?? def.y, z ?? def.z);
}
/// <summary>
/// Modifies the specified axis of a Vector2 variable.
/// </summary>
/// <example>
/// <code>
/// Vector2 myVector = new Vector2(0, 0);
/// Vector.Modify(ref myVector.With(y: 1.6f);
/// </code>
/// </example>
/// <param name="def">The Vector3 to manipulate</param>
/// <param name="x">The new x.</param>
/// <param name="y">The new y.</param>
/// <returns></returns>
public static void Modify(ref Vector2 def, float? x = null, float? y = null)
{
def.Set(x ?? def.x, y ?? def.y);
}
public static void ApplyDeadzone(ref Vector2 input, float deadzone)
{
var modifiedInput = input.WithDeadzone(deadzone);
input.Set(modifiedInput.x, modifiedInput.y);
}
#endregion Modify
#region Return modified copy
public static Vector2 WithDeadzone(this Vector2 input, float deadzone)
{
if (deadzone <= 0 && deadzone >= 1) throw new ArgumentOutOfRangeException("deadzone");
if (input.magnitude < deadzone)
input = Vector2.zero;
else
input = input.normalized * ((input.magnitude - deadzone) / (1 - deadzone));
return input;
}
/// <summary>
/// Returns a modified copy of a Vector4.
/// </summary>
/// <example>
/// <code>
/// mesh.tangents[0] = defaultMesh.tangents[0].With(z: 10, w: 2);
/// </code>
/// </example>
/// <param name="def">The Vector4 to manipulate</param>
/// <param name="x">The new x.</param>
/// <param name="y">The new y.</param>
/// <param name="z">The new z.</param>
/// <param name="w">The new w.</param>
/// <returns></returns>
public static Vector4 With(this Vector4 def, float? x = null, float? y = null, float? z = null, float? w = null)
{
def.Set(x ?? def.x, y ?? def.y, z ?? def.z, w ?? def.w);
return def;
}
/// <summary>
/// Returns a modified copy of a Vector3.
/// </summary>
/// <example>
/// <code>
/// transform.position = defaultPosition.With(x: 3.1f, z: 0);
/// </code>
/// </example>
/// <param name="def">The Vector3 to manipulate</param>
/// <param name="x">The new x.</param>
/// <param name="y">The new y.</param>
/// <param name="z">The new z.</param>
/// <returns></returns>
public static Vector3 With(this Vector3 def, float? x = null, float? y = null, float? z = null)
{
def.Set(x ?? def.x, y ?? def.y, z ?? def.z);
return def;
}
/// <summary>
/// Returns a modified copy of a Vector2.
/// </summary>
/// <example>
/// <code>
/// Material.mainTextureOffset = defaultOffset.With(y: 1.6f);
/// </code>
/// </example>
/// <param name="def">The Vector3 to manipulate</param>
/// <param name="x">The new x.</param>
/// <param name="y">The new y.</param>
/// <returns></returns>
public static Vector2 With(this Vector2 def, float? x = null, float? y = null)
{
def.Set(x ?? def.x, y ?? def.y);
return def;
}
public static Vector3 SwapYZ(this Vector3 def)
{
def.Set(def.x, def.z, def.y);
return def;
}
/// <summary>
/// Returns a Vector3 with values X: X, Y: 0, Z: Y from the Vector2.
/// </summary>
/// <param name="def">The Vector3 to use as base.</param>
/// <returns></returns>
public static Vector3 ToX0Y(this Vector2 def)
{
return new Vector3(def.x, 0, def.y);
}
#endregion Modify
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment