最近在看矩阵,顺路记录一下复习吧
[ m1 m2 m3 m4 ] [ m11 m12 m13 m14 ] [ m21 m22 m23 m24 ] [ m31 m32 m33 m34 ] v'x = m1 * vx + m11 * vy + m21 * vz + m31 * vw v'y = m2 * vx + m12 * vy + m22 * vz + m32 * vw v'z = m3 * vx + m13 * vy + m23 * vz + m33 * vw v'w = m4 * vx + m14 * vy + m24 * vz + m34 * vw
v'x = m1*vx + m2*vy + m3*vz + m4*vw v'y = m11*vx + m12*vy + m13*vz + m14*vw v'z = m21*vx + m22*vy + m23*vz + m24*vw v'w = m31*vx + m32*vy + m33*vz + m34*vw
[ 1 2 3 4 ] [ 5 6 7 8 ] [ 9 10 11 12 ] [ 13 14 15 16 ] 向量 v = (1, 2, 3, 1) v'x = 1*1 + 2*2 + 3*3 + 4*1 = 19 v'y = 5*1 + 6*2 + 7*3 + 8*1 = 47 v'z = 9*1 + 10*2 + 11*3 + 12*1 = 75 v'w = 13*1 + 14*2 + 15*3 + 16*1 = 103 向量乘法结果为 v' = (19, 47, 75, 103)
using System.Collections; using System.Collections.Generic; using UnityEngine; public class MatrixDemo : MonoBehaviour { public Matrix4x4 matrix1; public Vector4 MovePoint; // Start is called before the first frame update void Start() { //这里是赋值整个变换矩阵 M=T*R*S matrix1.SetTRS(transform.position,transform.rotation,transform.localScale); } // Update is called once per frame void Update() { if (Input.GetKeyDown(KeyCode.Q)) { OnMatrix4x4Move(); } } /// /// 矩阵平移,第四列是标记坐标信息的 /// public void OnMatrix4x4Move() { Vector4 v4 = new Vector4(0,0,0,1); matrix1[0, 3] = MovePoint.x; matrix1[1, 3] = MovePoint.y; matrix1[2, 3] = MovePoint.z; v4 = matrix1 * v4; transform.position = new Vector3(v4.x,v4.y,v4.z); } }
using System.Collections; using System.Collections.Generic; using UnityEngine; public class MatrixDemo : MonoBehaviour { public Matrix4x4 matrix1; // public Matrix4x4 matrixRotate; public Vector4 MovePoint; public Vector3 RotateData; // Start is called before the first frame update void Start() { //这里是赋值整个变换矩阵 M=T*R*S matrix1.SetTRS(transform.position, transform.rotation, transform.localScale); } // Update is called once per frame void Update() { if (Input.GetKeyDown(KeyCode.Q)) { OnMatrix4x4Move(); } if (Input.GetKeyDown(KeyCode.W)) { OnMatrix4x4Rotate(); } } /// /// 矩阵平移,第四列是标记坐标信息的 /// public void OnMatrix4x4Move() { Vector4 v4 = new Vector4(0, 0, 0, 1); matrix1[0, 3] = MovePoint.x; matrix1[1, 3] = MovePoint.y; matrix1[2, 3] = MovePoint.z; v4 = matrix1 * v4; transform.position = new Vector3(v4.x, v4.y, v4.z); } /// /// 矩阵旋转,这里是按本地旋转来的,例如先转x轴30再转Y,这里Y是基于X30度去转的 /// public void OnMatrix4x4Rotate() { Matrix4x4 matrixRotateX = Matrix4x4.identity; Matrix4x4 matrixRotateY = Matrix4x4.identity; Matrix4x4 matrixRotateZ = Matrix4x4.identity; Matrix4x4 matrixRotate = Matrix4x4.identity; // if (RotateData.x != 0) { matrixRotateX[1, 1] = Mathf.Cos(RotateData.x * Mathf.Deg2Rad); matrixRotateX[2, 1] = Mathf.Sin(RotateData.x * Mathf.Deg2Rad); matrixRotateX[1, 2] = -Mathf.Sin(RotateData.x * Mathf.Deg2Rad); matrixRotateX[2, 2] = Mathf.Cos(RotateData.x * Mathf.Deg2Rad); } // if (RotateData.y != 0) { matrixRotateY[0, 0] = Mathf.Cos(RotateData.y * Mathf.Deg2Rad); matrixRotateY[2, 0] = -Mathf.Sin(RotateData.y * Mathf.Deg2Rad); matrixRotateY[0, 2] = Mathf.Sin(RotateData.y * Mathf.Deg2Rad); matrixRotateY[2, 2] = Mathf.Cos(RotateData.y * Mathf.Deg2Rad); } // if (RotateData.z != 0) { matrixRotateZ[0, 0] = Mathf.Cos(RotateData.z * Mathf.Deg2Rad); matrixRotateZ[1, 0] = Mathf.Sin(RotateData.z * Mathf.Deg2Rad); matrixRotateZ[0, 1] = -Mathf.Sin(RotateData.z * Mathf.Deg2Rad); matrixRotateZ[1, 1] = Mathf.Cos(RotateData.z * Mathf.Deg2Rad); } matrixRotate = matrixRotateX * matrixRotateY * matrixRotateZ; float vW = Mathf.Sqrt(matrixRotate.m00 + matrixRotate.m11 + matrixRotate.m22 + 1) / 2; float w = vW * 4; float vX = (matrixRotate.m21 - matrixRotate.m12) / w; float vY = (matrixRotate.m02 - matrixRotate.m20) / w; float vZ = (matrixRotate.m10 - matrixRotate.m01) / w; transform.rotation= new Quaternion(vX, vY, vZ, vW); } }
using System.Collections; using System.Collections.Generic; using UnityEngine; public class MatrixDemo : MonoBehaviour { public Matrix4x4 matrix1; // public Matrix4x4 matrixRotate; public Vector4 MovePoint; public Vector3 RotateData; public Vector3 ScaleData=Vector3.one; // Start is called before the first frame update void Start() { //这里是赋值整个变换矩阵 M=T*R*S matrix1.SetTRS(transform.position, transform.rotation, transform.localScale); } // Update is called once per frame void Update() { if (Input.GetKeyDown(KeyCode.Q)) { OnMatrix4x4Move(); } if (Input.GetKeyDown(KeyCode.W)) { OnMatrix4x4Rotate(); } if (Input.GetKeyDown(KeyCode.E)) { OnMatrix4x4Scale(); } } /// /// 矩阵平移,第四列是标记坐标信息的 /// public void OnMatrix4x4Move() { Vector4 v4 = new Vector4(0, 0, 0, 1); matrix1[0, 3] = MovePoint.x; matrix1[1, 3] = MovePoint.y; matrix1[2, 3] = MovePoint.z; v4 = matrix1 * v4; transform.position = new Vector3(v4.x, v4.y, v4.z); } /// /// 矩阵旋转,这里是按本地旋转来的,例如先转x轴30再转Y,这里Y是基于X30度去转的 /// public void OnMatrix4x4Rotate() { Matrix4x4 matrixRotateX = Matrix4x4.identity; Matrix4x4 matrixRotateY = Matrix4x4.identity; Matrix4x4 matrixRotateZ = Matrix4x4.identity; Matrix4x4 matrixRotate = Matrix4x4.identity; // if (RotateData.x != 0) { matrixRotateX[1, 1] = Mathf.Cos(RotateData.x * Mathf.Deg2Rad); matrixRotateX[2, 1] = Mathf.Sin(RotateData.x * Mathf.Deg2Rad); matrixRotateX[1, 2] = -Mathf.Sin(RotateData.x * Mathf.Deg2Rad); matrixRotateX[2, 2] = Mathf.Cos(RotateData.x * Mathf.Deg2Rad); } // if (RotateData.y != 0) { matrixRotateY[0, 0] = Mathf.Cos(RotateData.y * Mathf.Deg2Rad); matrixRotateY[2, 0] = -Mathf.Sin(RotateData.y * Mathf.Deg2Rad); matrixRotateY[0, 2] = Mathf.Sin(RotateData.y * Mathf.Deg2Rad); matrixRotateY[2, 2] = Mathf.Cos(RotateData.y * Mathf.Deg2Rad); } // if (RotateData.z != 0) { matrixRotateZ[0, 0] = Mathf.Cos(RotateData.z * Mathf.Deg2Rad); matrixRotateZ[1, 0] = Mathf.Sin(RotateData.z * Mathf.Deg2Rad); matrixRotateZ[0, 1] = -Mathf.Sin(RotateData.z * Mathf.Deg2Rad); matrixRotateZ[1, 1] = Mathf.Cos(RotateData.z * Mathf.Deg2Rad); } matrixRotate = matrixRotateX * matrixRotateY * matrixRotateZ; float vW = Mathf.Sqrt(matrixRotate.m00 + matrixRotate.m11 + matrixRotate.m22 + 1) / 2; float w = vW * 4; float vX = (matrixRotate.m21 - matrixRotate.m12) / w; float vY = (matrixRotate.m02 - matrixRotate.m20) / w; float vZ = (matrixRotate.m10 - matrixRotate.m01) / w; transform.rotation= new Quaternion(vX, vY, vZ, vW); } /// /// 矩阵缩放 /// public void OnMatrix4x4Scale() { Matrix4x4 matrixScale = Matrix4x4.identity; Vector4 v4 = new Vector4(1, 1, 1, 1); matrixScale.m00 = ScaleData.x; matrixScale.m11 = ScaleData.y; matrixScale.m22 = ScaleData.z; v4 = matrixScale * v4; transform.localScale = new Vector3(v4.x, v4.y, v4.z); } }