Home > Blockchain >  Matrix4x4 TRS without built in functions
Matrix4x4 TRS without built in functions

Time:10-22

I'm new to stackoverflow and c# so please don't hesitate to give me more tips to ask better questions.

I'm trying to learn more about Matrix4x4.TRS function in Unity. I have seen that there's a big math part behind it, such as Quaternion to rotate and Vector3 to translate and scale. I would like to learn more about the math so that i could create my own matrix4x4 TRS without using the provided by Unity.

CodePudding user response:

Very simple task once you realize what data goes where. Think 4 columns of Vector4 here:

c0     c1     c2     c3

Xx  ,  Yx  ,  Zx  ,  Tx
Xy  ,  Yy  ,  Zy  ,  Ty
Xz  ,  Yz  ,  Zz  ,  Tz
 0  ,   0  ,   0  ,  1

c0/X - x direction (ends with 0)

c1/Y - y direction (ends with 0)

c2/Z - z direction (ends with 0)

c3/T - translation (ends with 1)

// inputs:
Vector3 position = new Vector3(1f, 2f, 3f);
Quaternion rotation = Quaternion.Euler(100f, 200f, 300f);
Vector3 scale = new Vector3(1f, 2f, 3f);

// calculations:
Vector4 c0 = new Vector4( rotation * new Vector3(scale.x,0,0) , 0 );
Vector4 c1 = new Vector4( rotation * new Vector3(0,scale.y,0) , 0 );
Vector4 c2 = new Vector4( rotation * new Vector3(0,0,scale.z) , 0 );
Vector4 c4 = new Vector4( position , 1 );

// output:
Matrix4x4 TRS = new Matrix4x4( c0, c1, c2, c3 );

^ code written from memory, so excuse me if there is a mistake somewhere.

  • Related