Search Unity

How to multiply point by transformation matrix with new mathematics library

Discussion in 'Scripting' started by keenanwoodall, Aug 13, 2018.

  1. keenanwoodall

    keenanwoodall

    Joined:
    May 30, 2014
    Posts:
    598
    I'm doing some mesh deformation and need to transform each vertice by some 4x4 matrix. I can get it to work easily with the old math system.

    vertice = someTransformation.MultiplyPoint3x4(vertice);


    I'm trying to do the same transformation, but with the new math library. So instead of being a Vector3, the vertice is a float3 and instead of being a Matrix4x4, the matrix is a float4x4.

    I'm using the 'mul' method to multiply the matrix and the point.

    vertice = mul (matrix, vertice);


    However, I can't do this because you can't multiply a float4x4 and a float3. You have to multiply a float4x4 and a float4. So I'm "appending" a float (not sure if it should be a zero or one, or if it even matters)

    vertice = mul (matrix, float4 (vertice, 1f));


    But this doesn't work either because the mul method returns a float4. This is where I'm starting to feel like I'm doing this wrong. I can't even cast to a float3, and even if I could - creating and casting excessively in some code thats running over every vertex feels icky.

    I'm not shader savvy and the new math library is mimicking shader vector operations, so I'm sure I'm missing something.

    [EDIT] Ok, I figured it out. Very simple solution. The reason I didn't try it was because intellisense didn't suggest it for some reason. Simply add ".xyz" to the end.
    vertice = mul (matrix, float4 (vertice, 1f)).xyz;
     
    Last edited: Aug 24, 2018
    xVergilx and Lynxed like this.
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,776
    You definitely need match rows to column, when multiplying matrices.
    You can check basics on multiplying matrices by hand. So you will get an idea, what can be 0 what 1.
    I don't want to make mistake, so I won't be posting example.

    Again, ECS & Job forum is your friend for such question.