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.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Porting issue to Unity.Mathematics

Discussion in 'Scripting' started by amitukind, Jan 17, 2020.

  1. amitukind

    amitukind

    Joined:
    Aug 4, 2017
    Posts:
    4
    I want to perform a simple math operation which is done as follows in classic unity maths

    Vector3 = Matrix4X4 * Vector3 
    ( which is working fine )

    When I am porting the same thing to Unity.Mathematics

    float3 = float4x4 * float3 
    ( error : Operator '*' cannot be applied to operands of type 'float4x4' and 'float3' ) neither math.mul works here

    So can anyone help me multiply a float4x4 and float3 and get a float3 as result which is already happening in classic unity code.
     
  2. Luxgile

    Luxgile

    Joined:
    Nov 27, 2016
    Posts:
    16
    To multiply a matrix with a vector, the vector must have the same elements as the columns/rows of the matrix. In this case you need a float4.
    Also iirc doing * and mul will give you different results depending on your case.
     
  3. amitukind

    amitukind

    Joined:
    Aug 4, 2017
    Posts:
    4
    I agree to your rule of Matrix multiplication, but the plugin I am using is already doing the Vector3 = Matrix4X4 * Vector3 without any issues and I have to replicate it to Unity.Mathematics, So any idea how this thing is working and how can I replicate it to Mathematics
     
  4. Luxgile

    Luxgile

    Joined:
    Nov 27, 2016
    Posts:
    16
    I think that's because unity is casting the Vector3 as a Vector4 and doing the multiplication but this doesn't happen with mathematics. I'm not 100% sure but converting your float3 to a float4 and setting the last element to 1 should work.
     
  5. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,385
    So Matrix4x4 doesn't have a * Vector3 operator, as you can see here:
    https://github.com/Unity-Technologies/UnityCsReference/blob/master/Runtime/Export/Math/Matrix4x4.cs

    BUT, Vector4 has an implicit conversion to Vector3 and back:
    https://github.com/Unity-Technologi...ob/master/Runtime/Export/Math/Vector4.cs#L260

    So what's going on is that your Vector3 is implicity converted to a Vector4 (with w as 0), multiplied, then converted back to Vector3 implicitly.

    Because this 0 for w, you're effectively performing the same operation as Matrix4x4.MultiplyPoint3x4 since the final w * 3rd column will always be +0:
    https://github.com/Unity-Technologi.../master/Runtime/Export/Math/Matrix4x4.cs#L301

    So if you want to convert to Unity.Mathematics, you need to make sure you're doing the same operation. So you want that w to be a 0.

    And of course run a test against it with multiple values and make sure you're getting the same results in the old and new.
     
    amitukind likes this.
  6. amitukind

    amitukind

    Joined:
    Aug 4, 2017
    Posts:
    4
    Yes you are right, Classic Unity Maths do all the implicit conversions while Mathematics accept only strict cases.