向任意平面的投影矩阵的推导

概述

方法来自于《3D数学基础,图形与游戏开发》这本书。

基本原理

这个方法其实有点思维跳跃,原理是首先推导出一个任意方向n缩放比例为k的缩放矩阵,然后将k变成0,这就变成了向垂直于n的投影平面的投影矩阵

缩放矩阵的推导

对于坐标轴上的缩放,我们很容易就能获得缩放矩阵:
[ k x 0 0 0 k y 0 0 0 k z ] \begin{bmatrix} kx&0&0\\ 0&ky&0\\ 0&0&kz\\ \end{bmatrix}
然而对于任意方向的缩放,则有另一种方法来计算
在这里插入图片描述
如上图所示,v沿着方向n来以缩放系数为k来进行缩放,缩放得到的结果为v’,则有下列的一系列公式:
v=v∥+v⊥
v∥=(v* n)n
v⊥’=v⊥=v-v∥=v-(v* n)n 因为垂直方向没有缩放
v∥’=kv∥=k(v* n)n k倍缩放
v’ = v⊥’+v∥’ = v-(v* n)n+k(v* n)n = v+(k-l)(v* n)n
至此,我们已经计算出来了最后的v’,那么,要怎么根据v’来获取缩放矩阵呢?
只要以此代入(1,0,0),(0,1,0),(0,0,1)这三个坐标,把得到的结果写入矩阵的三行即可,具体的原理只需要拿这些点乘一下最后得到的结果就知道了。前面的任意轴的旋转矩阵的推导也是很类似的,最后得到的结果如下所示:
[ 1 + ( k 1 ) n x 2 ( k 1 ) n x n y ( k 1 ) n x n z ( k 1 ) n x n y 1 + ( k 1 ) n y 2 ( k 1 ) n y n z ( k 1 ) n x n z ( k 1 ) n z n y 1 + ( k 1 ) n z 2 ] \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_xn_z&(k-1)n_zn_y&1+(k-1)n_z^2 \end{bmatrix}

向任意平面的投影矩阵的推导

这个推导就很简单了,只需要把上面矩阵的k变成0,就能得到我们需要的投影矩阵了,如下所示:
[ 1 n x 2 n x n y n x n z n x n y 1 n y 2 n y n z n x n z n z n y 1 n z 2 ] \begin{bmatrix} 1-n_x^2&-n_xn_y&-n_xn_z\\ -n_xn_y&1-n_y^2&-n_yn_z\\ -n_xn_z&-n_zn_y&1-n_z^2\\ \end{bmatrix}

在unity实现

只要传入矩阵然后在顶点着色器对顶点进行变换即可,可以看到,立方体和房子都投影成了一个面。
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
脚本如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RotateMatrixTest : MonoBehaviour
{
    public Material mat;
    public Vector3 n;
    private Matrix4x4 calculateScaleMatrix(Vector3 n)
    {
        Matrix4x4 RotationMatrix = new Matrix4x4();
        RotationMatrix[0, 0] = 1 - n.x * n.x;
        RotationMatrix[0, 1] = -n.x * n.y;
        RotationMatrix[0, 2] = -n.x * n.z;
        RotationMatrix[0, 3] = 0;
        RotationMatrix[1, 0] = -n.x * n.y;
        RotationMatrix[1, 1] = 1 - n.y * n.y;
        RotationMatrix[1, 2] = -n.y * n.z;
        RotationMatrix[1, 3] = 0;
        RotationMatrix[2, 0] = -n.x * n.z;
        RotationMatrix[2, 1] = -n.z * n.y;
        RotationMatrix[2, 2] = 1 - n.z * n.z;
        RotationMatrix[2, 3] = 0;
        RotationMatrix[3, 0] = 0;
        RotationMatrix[3, 1] = 0;
        RotationMatrix[3, 2] = 0;
        RotationMatrix[3, 3] = 1;
        return RotationMatrix;
    }  
    private void Update()
    {
        Vector3 normalizedN = n.normalized;
        mat.SetMatrix("_RotationMatrix", calculateScaleMatrix(normalizedN));
    }
}

shader:

Shader "Unlit/rotateTest"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #include "UnityCG.cginc"
            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };
            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
				float4 worldPos : TEXCOORD1;
            };

            sampler2D _MainTex;
            float4 _MainTex_ST;
			float4x4 _RotationMatrix;
            v2f vert (appdata v)
            {
                v2f o;
				fixed4 rotPos = mul(_RotationMatrix,v.vertex);
                o.vertex = UnityObjectToClipPos(rotPos);
                o.uv = TRANSFORM_TEX(v.uv, _MainTex);
                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                // sample the texture
				
                fixed4 col = tex2D(_MainTex, i.uv);
                return col;
            }
            ENDCG
        }
    }
}

发布了31 篇原创文章 · 获赞 4 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_43813453/article/details/102828158
今日推荐