Unity Shader学习-数学基础-矩阵

目录

一.方阵-对角矩阵-单位矩阵

二.矩阵与向量

三.矩阵乘法

1.标量与矩阵相乘

2.矩阵与矩阵相乘

3.矩阵乘法的特点

四.向量矩阵转换

1.向量矩阵相乘

2.矩阵转换向量

五.二维旋转矩阵

六.三维绕轴旋转

七.绕任意轴旋转

九.沿轴缩放矩阵

十.沿任意轴缩放矩阵

 十一.正交投影

 十二.沿任意轴的正交投影

 十三.镜像投影

 十四.切变


一.方阵-对角矩阵-单位矩阵

方阵:n x n阶矩阵

例如:3 x 3矩阵  \begin{bmatrix} m11 & m12 &m13 \\ m21& m22&m23 \\ m31& m32& m33 \end{bmatrix}

对角矩阵:除主对角线的以外的值全为0

例如:\begin{bmatrix} 4 & 0& 0\\ 0& 9 &0 \\ 0 &0 & 10 \end{bmatrix}

单位矩阵:主对角线上的值全为1,其余值全为0

例如:\begin{bmatrix} 1 & 0 &0 \\ 0& 1&0 \\ 0& 0& 1 \end{bmatrix}

三者的关系:单位矩阵\subseteq对角矩阵\subseteq方阵

二.矩阵与向量

行向量:1 x n的矩阵

例如:\begin{bmatrix} 1 & 2& 3 \end{bmatrix}

列向量:n x 1的矩阵

例如:\begin{bmatrix} 1\\ 2\\ 3 \end{bmatrix}

三.矩阵的转置

定义:M矩阵的转置为M矩阵沿着主对角线反转,记作M^{T}

公式:M_{ij} = M_{ji}^{T}

定理:(M^T)^T = M

        若M为对角矩阵则:M^T=M

三.矩阵乘法

1.标量与矩阵相乘

设M为矩阵,k为常数则:kM = k\begin{bmatrix} m_{11}&m_{12} &... & m_{1n} \\ m_{21} &m_{22} &.. &m_{2n} \\ ...&...&&...\\ m_{n1} &m_{n2} &... &m_{nn} \end{bmatrix}=\begin{bmatrix} km_{11}&km_{12} &... & km_{1n} \\ km_{21} &km_{22} &.. &km_{2n} \\ ...&...&&...\\ km_{n1} &km_{n2} &... &km_{nn} \end{bmatrix}

2.矩阵与矩阵相乘

设M1:a行b列矩阵,M2:b行c列矩阵

条件:第一个矩阵的列等于第二个矩阵的行

结果:M1 * M2为a行c列矩阵

公式:C_{ij} = \sum_{k=1}^{n}a_{ik}b_{kj}

例如:M_{1} = \begin{bmatrix} a_{11}&a_{12} &a_{13} \\ a_{2,1}&a_{22} &a_{23} \end{bmatrix},M_{2}=\begin{bmatrix} b_{11} &b_{12} \\ b_{21}&b_{22} \\ b_{31}&b_{32} \end{bmatrix}

M_{1}* M_{2} = \begin{bmatrix} a_{11}*b_{11} + a_{12} * b_{21} + a_{13} * b_{31}& a_{11}*b_{12} + a_{12} * b_{22} + a_{13} * b_{32} \\ a_{21}*b_{11} + a_{22} * b_{21} + a_{23} * b_{31}& a_{21}*b_{12} + a_{22} * b_{22} + a_{23} * b_{32} \end{bmatrix}

3.矩阵乘法的特点

1.矩阵乘以对应的单位矩阵等于他本身

\begin{bmatrix} 1& 4 &9\\ 3& 5& 6\\ 4&4&4 \end{bmatrix} * \begin{bmatrix} 1& 0 &0\\ 0& 1& 0\\ 0&0&1 \end{bmatrix} = \begin{bmatrix} 1& 4 &9\\ 3& 5& 6\\ 4&4&4 \end{bmatrix}

2.不满足交换律

AB \neq BA

3.满足结合律

(AB)C = A(BC)

4.(AB)^T = B^TA^T

四.向量矩阵转换

1.向量矩阵相乘

用途:可用于向量的转换,在DX中使用的是行向量,openGL中使用的是列向量

\vec{a} = \begin{bmatrix} x &y &z \end{bmatrix},M=\begin{bmatrix} m_{11} &m_{12} &m_{13} \\ m_{21}&m_{22} &m_{23} \\ m_{31}&m_{32} & m_{33} \end{bmatrix}

\vec{a} * M = \begin{bmatrix} xm_{11} + ym_{21} + zm_{31} & xm_{12} + ym_{22} + zm_{32} &xm_{13} + ym_{23} + zm_{33} \end{bmatrix}

\vec{b} = \begin{bmatrix} x \\y \\z \end{bmatrix},M=\begin{bmatrix} m_{11} &m_{12} &m_{13} \\ m_{21}&m_{22} &m_{23} \\ m_{31}&m_{32} & m_{33} \end{bmatrix}

M * \vec{b} = \begin{bmatrix} xm_{11}+ym_{12} + zm_{13}\\ xm_{21} + ym_{22} + zm_{23}\\ xm_{31} + xm_{32} + xm_{33} \end{bmatrix}

总结:行向量左乘乘矩阵,列向量右乘矩阵

2.矩阵转换向量

\vec{a} = \begin{pmatrix} x,& y,&z \end{pmatrix}

将向量转换成矩阵并分解:\vec{a} = \begin{bmatrix} x\\ y\\ z \end{bmatrix} = \begin{bmatrix} x\\ 0\\ 0 \end{bmatrix} + \begin{bmatrix} 0\\ y\\ 0 \end{bmatrix} + \begin{bmatrix} 0\\ 0\\ z \end{bmatrix}

化简:\vec{a} = \begin{bmatrix} x\\ y\\ z \end{bmatrix} = x\begin{bmatrix} 1\\ 0\\ 0 \end{bmatrix} + y\begin{bmatrix} 0\\ 1\\ 0 \end{bmatrix} +z \begin{bmatrix} 0\\ 0\\ 1 \end{bmatrix}

我们令\vec{p} = \begin{pmatrix} 1, &0, &0 \end{pmatrix},\vec{q} = \begin{pmatrix} 0, &1, &0 \end{pmatrix},\vec{r} = \begin{pmatrix} 0, &0, &1 \end{pmatrix}

于是得到:\vec{a} = x\vec{p} + y\vec{q} + z\vec{r}

其中p,q,r又被称之为基向量

五.二维旋转矩阵

R(\theta )=\begin{bmatrix} \cos\theta & \sin \theta\\ -\sin \theta & \cos \theta \end{bmatrix},则R(\theta )被称为二维旋转矩阵

用途:计算\vec{M} = \begin{pmatrix} x ,&y \end{pmatrix}绕原点旋转\theta后得到的向量\vec{M_{0}} = (x_{0},y_{0})

公式:\vec{M_{0}} = \vec{M} * R(\theta )

证明:

六.三维绕轴旋转

左手坐标系:

右手坐标系:

 \theta角旋转正方向的判断,左手定则:大拇指指向需要旋转的轴,从轴的正端点,指向负端点旋转(左手坐标系为顺时针,右手坐标系为逆时针)

绕z轴旋转R_{z}(\theta ) = \begin{bmatrix} \cos\theta & \sin \theta &0 \\ -\sin \theta& \cos\theta&0 \\ 0& 0 & 1 \end{bmatrix}

绕x轴旋转R_{x}(\theta ) = \begin{bmatrix}1 & 1 &0 \\ 0& \cos\theta&\sin\theta \\ 0& -\sin \theta & \cos \theta \end{bmatrix}

绕y轴旋转R_{y}(\theta ) = \begin{bmatrix}\cos\theta & 0 &-\sin \theta \\ 0& 1&\0 \\ \sin \theta & 0& \cos \theta \end{bmatrix}

七.绕任意轴旋转

 如图为\vec{v}\vec{n}旋转\theta得到\vec{v'},其中OH垂直面AMH

我们将面AMH单独拿出来看,做HW垂直于HA,令HW等于HA

我们可以得到:\vec{HM} = \vec{HA}\cos\theta + \vec{HW}\sin\theta        ..........................1

\vec{HA} = \vec{v}-\vec{OH}=\vec{v}-(\vec{v}\cdot \vec{n})\vec{n}                                 ..........................2 

解释:(\vec{v}\cdot \vec{n})\vec{n}怎么得到的

\vec{v}\vec{n}方向上的投影为\frac{\vec{v}\cdot \vec{n}}{\left | \vec{n} \right |},\vec{n}的单位向量为\frac{\vec{n}}{\left | \vec{n} \right |}

\therefore \vec{OH} = (\vec{v}\cdot \vec{n})\vec{n},我们假设n为单位向量长度为1

\vec{HW} = \vec{n} \times \vec{HA}=\vec{n} \times(\vec{v}-\vec{OH}))=\vec{n}\times\vec{v}-\vec{n}\times\vec{OH} ..........................3

 由1,2,3得:\vec{HM} = (\vec{v}-(\vec{v}\cdot\vec{n})\vec{n}))cos\theta + (\vec{n}\times\vec{v}-\vec{n}\times\vec{OH})sin\theta\\

化简得:\vec{HM}=(\vec{v}-(\vec{v}\cdot\vec{n})\vec{n}))cos\theta + (\vec{n}\times\vec{v})sin\theta

\therefore \vec{v'} =\vec{OH} + \vec{HM} =(\vec{v}\cdot \vec{n})\vec{n} + (\vec{v}-(\vec{v}\cdot\vec{n})\vec{n}))cos\theta + (\vec{n}\times\vec{v})sin\theta

我们设\vec{p}=(1,0,0),\vec{q} = (0,1,0),\vec{r} = (0,0,1)

我们求各个方向上的向量

\therefore \vec{p'} =(\vec{p}\cdot \vec{n})\vec{n} + (\vec{p}-(\vec{p}\cdot\vec{n})\vec{n}))cos\theta + (\vec{n}\times\vec{p})sin\theta

\vec{q'} =(\vec{q}\cdot \vec{n})\vec{n} + (\vec{q}-(\vec{q}\cdot\vec{n})\vec{n}))cos\theta + (\vec{n}\times\vec{q})sin\theta

\vec{r} =(\vec{r}\cdot \vec{n})\vec{n} + (\vec{r}-(\vec{r}\cdot\vec{n})\vec{n}))cos\theta + (\vec{n}\times\vec{r})sin\theta

\vec{p'}=\begin{bmatrix} n^2_{x}(1-cos\theta)+cos\theta)\\n_{x}n_{y}(1-cos\theta )-n_{z}sin\theta) \\ n_{x}n{z}(1-cos\theta) + n_{y}sin\theta)) \end{bmatrix}
\vec{q'}=\begin{bmatrix} n_{x}n_{y}(1-cos\theta )-n_{z}sin\theta)\\ n^2_{y}(1-cos\theta)+cos\theta)\\ n_{y}n{z}(1-cos\theta) + n_{x}sin\theta)) \end{bmatrix}

\vec{r'}=\begin{bmatrix} n_{x}n_{z}(1-cos\theta )-n_{y}sin\theta)\\ n_{y}n{z}(1-cos\theta) + n_{x}sin\theta))\\ n^2_{z}(1-cos\theta)+cos\theta) \end{bmatrix}

\therefore R(n,\theta) = \begin{bmatrix} p'\\ q'\\ r' \end{bmatrix}=\begin{bmatrix} n^2_{x}(1-cos\theta)+cos\theta)&n_{x}n_{y}(1-cos\theta )-n_{z}sin\theta)& n_{x}n{z}(1-cos\theta) + n_{y}sin\theta)\\ n_{x}n_{y}(1-cos\theta )-n_{z}sin\theta)& n^2_{y}(1-cos\theta)+cos\theta)&n_{y}n{z}(1-cos\theta) + n_{x}sin\theta)\\ n_{x}n_{z}(1-cos\theta )-n_{y}sin\theta)& n_{y}n{z}(1-cos\theta) + n_{x}sin\theta))& n^2_{z}(1-cos\theta)+cos\theta) \end{bmatrix}

九.沿轴缩放矩阵

 设基向量\vec{p} = (1,0),\vec{q} = (0,1)

\vec{p'} = k_{x}\vec{p} = (k_{x},0),\vec{q'} = k_{y}\vec{q} = (0,k_{y})

二维缩放矩阵s(k_{1},k_{2}) = \begin{bmatrix} \vec{p'}\\\vec{q'} \end{bmatrix} = \begin{bmatrix} k_{x} & 0\\0 & k_{y} \end{bmatrix}

同理,三维缩放矩阵s(k_{1},k_{2},k_{3}) = \begin{bmatrix} \vec{p'}\\\vec{q'} \\\vec{r}\end{bmatrix} = \begin{bmatrix} k_{x} & 0 & 0\\0 & k_{y} & 0 \\ 0 & 0 & k_{z}\end{bmatrix}

十.沿任意轴缩放矩阵

1.二维缩放矩阵

\vec{v}沿\vec{n}方向缩放k倍得到\vec{v'}

\vec{v'} = \vec{v'_{\top }} +\vec{v'_{//}}=\vec{v_{\top}} + k\vec{v_{//}}=\vec{v}-(\vec{v}\cdot\vec{n})\vec{n} + k(\vec{v}\cdot \vec{n})\vec{n} = \vec{v} + (k-1)(\vec{v}\cdot\vec{n})\vec{n}

设基向量\vec{p} = (1,0),\vec{q} = (0,1)计算变换后的向量\vec{p'},\vec{q'}

\vec{p},\vec{q}带入得:

\vec{p'} = (1 + (k-1)n_{x}^2,(k-1)n_{x}n_{y})

\vec{q'} = ((k-1)n_{x}n_{y},1 + (k-1)n_{y}^2)

所以二维缩放矩阵为:s(n,k) = \begin{bmatrix} (1 + (k-1)n_{x}^2& (k-1)n_{x}n_{y})\\ ((k-1)n_{x}n_{y}&1 + (k-1)n_{y}^2) \end{bmatrix}

2.三维缩放矩阵

证明过程同二维,在这里直接给出结果

三维缩放矩阵为s(n,k) = \begin{bmatrix} 1 + (k-1)n_x^2& (k-1)n_xn_y &(k-1)n_xn_z \\ (k-1)n_xn_y&1 + (k-1)n_y^2 & (k-1)n_yn_z \\ (k-1)n_{x}n_z &(k-1)n_zn_y &1 + (k-1)n_z^2 \end{bmatrix}

 十一.正交投影

\vec{n}于投影垂直,s(\vec{n},0)为正交投影,其中0称为缩放因子,缩放因子为0时代表投影,-1代表镜像

1.二维

向x轴投影s(\begin{bmatrix} 0 & 1 \end{bmatrix},0) = p_x = \begin{bmatrix} 1 &0 \\ 0& 0 \end{bmatrix},向y轴投影s(\begin{bmatrix} 1 & 0 \end{bmatrix},0) =p_y = \begin{bmatrix} 0 &0 \\ 0& 1 \end{bmatrix}

例如向量(1,1)向x轴投影:\begin{bmatrix} 1 & 1 \end{bmatrix}\begin{bmatrix} 1 & 0\\ 0& 0 \end{bmatrix} = \begin{bmatrix} 1 & 0 \end{bmatrix}

2.三维

向xoy面投影 s(\begin{bmatrix} 0 & 0&1 \end{bmatrix},0) =p_{xy} = \begin{bmatrix} 1 & 0 & 0\\ 0 &1 & 0\\ 0& 0& 0 \end{bmatrix}

向xoz面投影s(\begin{bmatrix} 0 & 1 & 0\end{bmatrix},0) =p_{xz} = \begin{bmatrix} 1 & 0 & 0\\ 0 &0& 0\\ 0& 0& 1 \end{bmatrix}

向yoz面投影s(\begin{bmatrix} 1 & 0&0 \end{bmatrix},0) =p_{yz} = \begin{bmatrix} 0 & 0 & 0\\ 0 &1 & 0\\ 0& 0& 1 \end{bmatrix}

 十二.沿任意轴的正交投影

1.二维

P(n) = S(\vec{n},0) = \begin{bmatrix} 1-n_x^2 & -n_xn_y \\ -n_xn_y&1-n_y^2 \end{bmatrix}

2.三维

P(n) = S(\vec{n},0) = \begin{bmatrix} 1-n_x^2 &-n_xn_y &-n_yn_z \\ -n_xn_y&1-n_y^2 &-n_yn_z \\ -n_xn_z&-n_zn_y &1-n_z^2 \end{bmatrix}

 十三.镜像投影

1.二维

P(\vec{n}) = S(\vec{n},-1) = \begin{bmatrix} 1-2n_x^2 &-2n_xn_y \\ -2n_xn_y& 1-2n^y \end{bmatrix}

2.三维

P(\vec{n}) = S(\vec{n},-1) = \begin{bmatrix} 1-2n_x^2 &-2n_xn_y &-2n_xn_z \\ -2n_xn_y&1-2n_y^2 &-xn_yn_z \\ -2n_xn_z&-2n_zn_y &1-2n_z^2 \end{bmatrix}

 十四.切变

1.二维

H_x(s) = \begin{bmatrix} 1 &0 \\ s&1 \end{bmatrix},H_y = \begin{bmatrix} 1 &s \\ 0&1 \end{bmatrix}        s,为切变因子

2.三维

H_{xy}(s,t) = \begin{bmatrix} 1 &0 &0\\ 0& 1& 0\\ s & t & 1 \end{bmatrix}, H_{xz}(s,t) = \begin{bmatrix} 1 &0 &0\\ s& 1& t\\ 0 & 0 & 1 \end{bmatrix}, H_{yz}(s,t) = \begin{bmatrix} 1 &s &t\\ 0& 1& 0\\ 0 & 0& 1 \end{bmatrix}

猜你喜欢

转载自blog.csdn.net/m0_53377876/article/details/130122225