Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Discussion Unity.Mathematics Tips and Tricks

Discussion in 'Scripting' started by joshrs926, Oct 14, 2022.

  1. joshrs926

    joshrs926

    Joined:
    Jan 31, 2021
    Posts:
    80
    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-Technologi...b/master/DOTS_Guide/cheatsheet/mathematics.md
     
    Last edited: Oct 16, 2022
  2. joshrs926

    joshrs926

    Joined:
    Jan 31, 2021
    Posts:
    80
    Component-wise operations:

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

    Code (CSharp):
    1. public void ComponentWiseMultiply(float3 a, float3 b)
    2. {
    3.     float3 c;
    4.     c = a * b;
    5.     // is the same as
    6.     c = new float3(a.x * b.x, a.y * b.y, a.z * b.z);
    7. }
     
    Last edited: Oct 15, 2022
  3. joshrs926

    joshrs926

    Joined:
    Jan 31, 2021
    Posts:
    80
    Last edited: Oct 15, 2022
  4. joshrs926

    joshrs926

    Joined:
    Jan 31, 2021
    Posts:
    80
    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:
    Code (CSharp):
    1. public bool IsInBounds(float3 position, float3 min, float3 max)
    2. {
    3.     return math.all(position >= min & position < max);
    4. }
     
    Last edited: Oct 15, 2022
    AnimalMan likes this.
  5. joshrs926

    joshrs926

    Joined:
    Jan 31, 2021
    Posts:
    80
    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:
    Code (CSharp):
    1. public void Demo()
    2. {
    3.     float3 a = new float3(1, 2, 3);
    4.     float2 axy = a.xy;
    5.     float2 azx = a.zx;
    6.  
    7.     float4 b = new float4(1, 2, 3, 4);
    8.     float3 bzxy = b.zxy;
    9.     float2 bzw = b.zw;
    10. }
     
    Last edited: Oct 15, 2022
    AnimalMan likes this.
  6. joshrs926

    joshrs926

    Joined:
    Jan 31, 2021
    Posts:
    80
    csum(float3)

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

    For example:
    Code (CSharp):
    1. // don’t actually use this method. Use math.dot() instead if you actually need the dot product :)
    2. public float DotProduct(float3 a, float3 b)
    3. {
    4.     // dot product = a.x * b.x + a.y * b.y + a.z * b.z
    5.     return csum(a * b);
    6. }
     
    Last edited: Oct 15, 2022
    AnimalMan likes this.
  7. AnimalMan

    AnimalMan

    Joined:
    Apr 1, 2018
    Posts:
    1,164
    You’re a good man

    i like this thread.
     
    joshrs926 likes this.
  8. joshrs926

    joshrs926

    Joined:
    Jan 31, 2021
    Posts:
    80
    math.select()

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

    Code (CSharp):
    1. float3 CleanDivision(float3 a, float3 b, float replacement = 0)
    2. {
    3.     // b might have 0s
    4.     float3 dirty = a / b;
    5.     float3 r = new float3(replacement);
    6.     // replaces NaNs and infinities with the replacement value
    7.     return math.select(r, dirty, math.isfinite(dirty));
    8. }
     
    Last edited: Oct 15, 2022
  9. joshrs926

    joshrs926

    Joined:
    Jan 31, 2021
    Posts:
    80
    Haha thanks man!
     
  10. joshrs926

    joshrs926

    Joined:
    Jan 31, 2021
    Posts:
    80
    Vector3.magnitude => math.length
     
    unity_nick_ likes this.
  11. joshrs926

    joshrs926

    Joined:
    Jan 31, 2021
    Posts:
    80
    math.cmax and math.cmin

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

    Code (CSharp):
    1. public void Demo()
    2. {
    3.     float4 a = new float3(1, 3, 2, -5);
    4.     float b = math.cmax(a);
    5.     // b is 3
    6. }
     
    Last edited: Oct 15, 2022
    AnimalMan likes this.
  12. joshrs926

    joshrs926

    Joined:
    Jan 31, 2021
    Posts:
    80
    math.max and math.min

    Returns the componentwise maximum/minimum of two values.

    Code (CSharp):
    1. public void Demo()
    2. {
    3.     float3 a = new float3(-1, 4, 9);
    4.     float3 b = new float3(-3, 5, 8);
    5.     float3 c = math.max(a, b);
    6.     // c is (-1, 5, 9)
    7. }
     
    AnimalMan likes this.
  13. joshrs926

    joshrs926

    Joined:
    Jan 31, 2021
    Posts:
    80
  14. joshrs926

    joshrs926

    Joined:
    Jan 31, 2021
    Posts:
    80
    Vector3.up => math.up() etc.
     
  15. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,821
    The technical term is "swizzle".
     
    joshrs926 likes this.