I’ve finally updated the code in the beginning of the thread. Seems to produce the correct results for all the position, rotation and scale.
New Method:
public static Vector3 PositionFromMatrix(Matrix4x4 m)
{
return m.GetColumn(3);
}
I found this theread based on the initial post.
I already have a matrix that I’ve exported from another piece of software.
Is there an elegant way to take my 16 numbers and just directly stick them into the 4x4?
Think this works. Hardcoded matrix for testing but will do a parser etc.
Matrix4x4 matrix = new Matrix4x4();
Vector4 column0 = new Vector4(0.9439778387f, -0.2836667956f, 0.1686386348f, 0f);
Vector4 column1 = new Vector4(-0.1615042061f, 4.85E-02f, -0.9856779448f, 0f);
Vector4 column2 = new Vector4(0.2877885186f, 0.9576939848f, 0f, 0f);
Vector4 column3 = new Vector4(-46.82842202f, 4.95691376f, -4.889713775f, 1f);
matrix.SetColumn(0, column0);
matrix.SetColumn(1, column1);
matrix.SetColumn(2, column2);
matrix.SetColumn(3, column3);
Test.transform.rotation = Quaternion.LookRotation(matrix.GetColumn(2), matrix.GetColumn(1));
Test.transform.position = matrix.GetColumn(3);
Just be aware Matrix4x4 is column based. So if your looking at examples of matrix multiplies online, any non graphics stuff will be showing you examples row based. So sometimes you maybe to to transpose a matrix you see online after making it. Though the fact you are settting up your matrix with SetColumn makes me think you already have a handle on this.
Thanks @takatok
to be honest it’s a bit of a black mystery box for me. I wrote a script in Rhino that pulls the transforms from my block instances in Rhinoceros. I then did semi-guess transforms in Rhino via trial and error until it looked right in Unity. Then compared the original object matrix values with the one that resulted in the correct transform in Unity, and manually transposed the values with the script… Every cell needed either a swap with another or a sign inversion… But it works! Can select all the bits in my Rhino model, hit a button and then Unity spawns a prefab based on each matrix in the correct spot and orientation.
Hey @wynott , I’m trying to tackle the same problem as you - rhino to unity via transform matrix. Would you be willing to shed some light for me on what cells needed swapping? I’m using grasshopper to grab the matrix from rhino.
I need to work with a 4x4 matrix (16 numbers) and apply this transformation matrix to obtain a new position, rotation and scale, as I can work it?