I just want to make sure I’m understanding what row major and column major mean. The Unity documentation for Matrix4x4s here says that they are column major. But when I create a Matrix4x4 variable in a MonoBehaviour and saw it in the inspector, it seems that the rows come first. E00, E01, E02, and E03 is the first row.
I tested this out with Graphics.DrawMeshInstanced(…), using the matrix in my inspector, and E00 is definitely the object’s x-axis scale, which makes sense. E03 is definitely the object’s x-axis translation, which also makes sense (cause the 4th dimension is needed with matrix multiplication in order to also get in translation). So E00, E01, E02, & E03 must be the first row, meaning the matrix should be row major right?
(Or perhaps did they just switch it up in the inspector – just the way it appears?)
I know the naming of the fields is a bit confusing. Have a look at my Matrix crash course. It should cover most aspects of Unity’s Matrix4x4 struct.
Note that the indexer that the Matrix4x4 struct has uses flattended columns (so 0-3 is first column, 4-7 is second column, …) This reflects the actual memory layout of the fields:
public struct Matrix4x4
{
public float m00;
public float m10;
public float m20;
public float m30;
public float m01;
public float m11;
public float m21;
public float m31;
public float m02;
public float m12;
public float m22;
public float m32;
public float m03;
public float m13;
public float m23;
public float m33;
// [ ... ]
}
So the first number in the field name is the row index while the second number is the column index. So as you said (m00, m01, m02, m03) represents the first row.
Keep in mind that the struct has a GetColumn and a GetRow method that returns a Vector4 for the given column / row index. Likewise there’s also a Setcolumn and SetRow method.