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”);

Unity Transformation

transform.position = new Vector3 (10, 0, 0); // 현재 transform 컴포넌트의 position 값을 위치 (10, 0, 0)로 이동한다

transform.localPosition = new Vector3 (10, 0, 0); // 부모 position의 상대적인 위치로 (10, 0, 0) 이동한다

transform.Translate (10, 0, 0); // 현재 position에서 (10, 0, 0) 만큼 이동한다.

transform.rotation = Quaternion.Euler (new Vector3 (10, 0, 0)); // 현재 transform 컴포넌트의 rotation 값을 오일러각 (10, 0, 0) 로 회전한다.

transform.Rotate (10, 0, 0); // 현재 rotation에서 (10, 0, 0)만큼 회전한다.

transform.localScale = new Vector3 (10, 10, 10); // transform 컴포넌트의 scale 값을 (10, 10, 10) 으로 크기변환한다.

Unity Matrix (Column-Major Order)

Vector3 Position = new Vector3(0.0f, 0.0f, 0.0f);
Matrix4x4 Model = Matrix4x4.Translate(new Vector3(1.0f, 2.0f, 3.0f));
// Debug.Log(Model);
// (1.0, 0.0, 0.0, 1.0)
// (0.0, 1.0, 0.0, 2.0)
// (0.0, 0.0, 1.0, 3.0)
// (0.0, 0.0, 0.0, 1.0)

Vector3 Transformed Model.MultiplyPoint3x4(Position)// P’ (1, 2, 3) (Unity Matrix uses Column-Major Order) 

KeyboardMouse

KeyboardMouse
– Draw Circle by GLPrimitiveTypes (Space-Key to change types)
– Draw Quad With Mouse Clicking
– Free-Draw Line With Mouse Dragging
– Rotate Cube With Mouse Horizontal/Vertical Movement
– Select Object With Mouse Clicking (mouse clicking on the empty space to unselect)

KeyboardMouse