Search Unity

Matrix Multiply?

Discussion in 'Entity Component System' started by MikeUpchat, Feb 27, 2021.

  1. MikeUpchat

    MikeUpchat

    Joined:
    Sep 24, 2010
    Posts:
    1,056
    In my jobs I have a fair few Matrix4x4.MatrixMultiply3x4(Vector3) floating around, what is the best way of doing those using simd methods or should I just keep them as they are?
     
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,780
    As far I am aware, burst with jobs are well optimized for matrix operations.
    There was some comparison floating about, or probably even it was on one of GDC talks.
    Sorry I don't remember. You could try look on SIMD matrix operation, or maybe search even youtube and GDC?

    My understanding is, it should be already optimized for SIMD.
    Hence if you need them just use them. If in doubt, run some benchmark and stress test.
     
  3. Mortuus17

    Mortuus17

    Joined:
    Jan 6, 2020
    Posts:
    105
    Don't use the old "Matrix4x4" etc. types, instead use Unity.Mathematics' float4x4 etc. types, since they are mapped to SIMD registers.
    Use "math.mul()".
     
    Nyanpas and Antypodish like this.
  4. MikeUpchat

    MikeUpchat

    Joined:
    Sep 24, 2010
    Posts:
    1,056
    Thanks for the reply. It seems I was confused by a float4x3 * float3 giving a float4. So the next question is, is there a method to cast/convert a Matrix4x4 to a float4x3 or do I need to copy the elements directly?
     
  5. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,780
    You want to loose column. But multiplying matrices don't work that.

    Depends what type of multiplication you are doing. What you actually want to multiply by?
    There are some solution. Mostly related to transformations, position, scaling, rotation. But if you got some custom equation, you may need to do each element multiplication by a hand.
     
  6. MikeUpchat

    MikeUpchat

    Joined:
    Sep 24, 2010
    Posts:
    1,056
    No its a standard replacement for Matrix4x4.MultiplyPoint4x3 of a vertex position, so bog standard pos, rot, scale matrix.
     
  7. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,780
  8. MikeUpchat

    MikeUpchat

    Joined:
    Sep 24, 2010
    Posts:
    1,056
    Many thanks for your help :)