Composing Transformation

// white cube at origin
cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
cube.GetComponent().material.color = new Color(1.0f, 1.0f, 1.0f, 1.0f);
cube.transform.parent = this.gameObject.transform;

// translation, rotation, scale
Vector3 translation = new Vector3(1.5, 0, 0);
Quaternion rotation = Quaternion.Euler(0, 0, 45);
Vector3 scale = new Vector3(0.2, 0.2, 0.2);

// white cube with S->R->T
cube.transform.localScale = scale;
cube.transform.rotation = rotation;
cube.transform.position = translation;
Matrix4x4 matrix = cube.transform.localToWorldMatrix;
Debug.Log(“cube.transform.localToWorldMatrix=\n” + matrix);

// Create a composing transformation Scale -> Rotation -> Translation
Matrix4x4 m1 = Matrix4x4.TRS(translation, rotation, scale);
// Create a composing transformation Scale -> Rotation -> Translation
Matrix4x4 m2 = Matrix4x4.Translate(translation) *
Matrix4x4.Rotate(rotation) * Matrix4x4.Scale(scale); // Scale -> Rotation -> Translation
if (m1 == m2) Debug.Log(“m1 == m2”);
if (m1 == matrix) Debug.Log(“m1 == matrix”);

// Create a composing transformation Translation -> Rotation ->Scale
Matrix4x4 m3 = Matrix4x4.Scale(scale) *
Matrix4x4.Rotate(rotation) * Matrix4x4.Translate(translation); 
if (m1 != m3) Debug.Log(“m1 != m3”);