Unity.Mathematics Tips and Tricks

The Unity.Mathematics package has lots of goodies throughout it but there doesn’t seem to be much info online on the topic. So I thought it’d be great if there was a thread where we can share useful discoveries. I’m looking for 2 things mainly:

  • Useful tricks
  • Ways of doing things we’d normally do with Mathf, Vector3, Random, Matrix4x4, etc.

Edit: Actually Unity has a cheat sheet for the Mathematics package: https://github.com/Unity-Technologies/EntityComponentSystemSamples/blob/master/DOTS_Guide/cheatsheet/mathematics.md

Component-wise operations:

For all multi component values (eg float3, int3, double4, etc) the arithmetic operators (+ - / *) will be applied for each component. For example,

public void ComponentWiseMultiply(float3 a, float3 b)
{
    float3 c;
    c = a * b;
    // is the same as
    c = new float3(a.x * b.x, a.y * b.y, a.z * b.z);
}

Matrix4x4.MultiplyPoint(Vector3) => math.mul(float4x4, float4)

public float3 MultiplyPoint(float4x4 matrix, float3 point)
{
    return math.mul(matrix, new float4(point, 1)).xyz;
}

Credit: How to multiply point by transformation matrix with new mathematics library

Bool vectors eg bool2, bool3

You can implicitly get these by comparing float2s, float3s, int2s, int3s, etc with <, >=, ==, etc
To change them into a single bool you can use math.all() or math.any().
You can mask them or combine them using & and | (not && and not ||).
Also you can cast these into int2s, int3s, etc. False becomes 0 and true becomes 1.

For example:

public bool IsInBounds(float3 position, float3 min, float3 max)
{
    return math.all(position >= min & position < max);
}
1 Like

Swizzle vector values
(thanks @Owen-Reynolds for that term)

You can grab any components of a float2, float3, float4, etc in any order like so:

public void Demo()
{
    float3 a = new float3(1, 2, 3);
    float2 axy = a.xy;
    float2 azx = a.zx;

    float4 b = new float4(1, 2, 3, 4);
    float3 bzxy = b.zxy;
    float2 bzw = b.zw;
}
1 Like

csum(float3)

Returns the horizontal sum of components of a float3 vector.
Same for float2, int4, double3, etc

For example:

// don’t actually use this method. Use math.dot() instead if you actually need the dot product :)
public float DotProduct(float3 a, float3 b)
{
    // dot product = a.x * b.x + a.y * b.y + a.z * b.z
    return csum(a * b);
}
1 Like

You’re a good man

i like this thread.

1 Like

math.select()

You can use math.select as a quick way to do component-wise ternary operations. For example:

float3 CleanDivision(float3 a, float3 b, float replacement = 0)
{
    // b might have 0s
    float3 dirty = a / b;
    float3 r = new float3(replacement);
    // replaces NaNs and infinities with the replacement value
    return math.select(r, dirty, math.isfinite(dirty));
}

Haha thanks man!

Vector3.magnitude => math.length

1 Like

math.cmax and math.cmin

Gets you the max/min component in a multi component value eg float4

public void Demo()
{
    float4 a = new float3(1, 3, 2, -5);
    float b = math.cmax(a);
    // b is 3
}
1 Like

math.max and math.min

Returns the componentwise maximum/minimum of two values.

public void Demo()
{
    float3 a = new float3(-1, 4, 9);
    float3 b = new float3(-3, 5, 8);
    float3 c = math.max(a, b);
    // c is (-1, 5, 9)
}
1 Like

smoothstep(float3, float3, float3)
Returns a componentwise smooth Hermite interpolation between 0.0f and 1.0f when x is in [a, b].
https://docs.unity3d.com/Packages/com.unity.mathematics@1.2/api/Unity.Mathematics.math.smoothstep.html#Unity_Mathematics_math_smoothstep_Unity_Mathematics_float3_Unity_Mathematics_float3_Unity_Mathematics_float3_
Haven’t used this one yet but I have implemented my own smoothstep function in the past only to later find this in the Mathematics package! They have all sorts of random nuggets in there.

Vector3.up => math.up() etc.

The technical term is “swizzle”.

1 Like