长沙做网站设计,模板建站广团,wordpress怎么编辑保存,山西做网站哪个好文章目录 前言一、观察空间矩阵推导1、求观察空间基向量2、求观察空间的基向量在世界空间中的矩阵 的 逆矩阵2、求平移变换矩阵3、相乘得出 观察空间转化矩阵4、得到顶点的世界空间坐标#xff0c;然后转化到观察空间5、把观察空间坐标转化为齐次裁剪坐标输出到屏幕 二、最终效… 文章目录 前言一、观察空间矩阵推导1、求观察空间基向量2、求观察空间的基向量在世界空间中的矩阵 的 逆矩阵2、求平移变换矩阵3、相乘得出 观察空间转化矩阵4、得到顶点的世界空间坐标然后转化到观察空间5、把观察空间坐标转化为齐次裁剪坐标输出到屏幕 二、最终效果1、这是我们用默认Shader在该摄像机坐标下的游戏界面2、使用我们的Shader并且给我们的ViewPos赋值为摄像机坐标3、最终代码 前言
在上篇文章中我们是实现了Shader中的观察空间推导。
Unity中Shader观察空间推导
我们在这篇文章中根据上篇文章的推导在Shader中实现观察空间矩阵的推导。 一、观察空间矩阵推导 Pview [Wview] * Pworld Pview [Vworld]-1 * Pworld Pview [Vworld]T * Pworld 在属性面板定义测试使用到的 摄像机坐标 和 测试顶点坐标 _ViewPos(“View Pos”,vector) (0,0,0,0) _ViewTarget(“View Target”,vector) (0,0,0,0) 1、求观察空间基向量
Z坐标轴基向量 float3 ViewZ normalize(_ViewPos - _ViewTarget); 假设Y坐标轴基向量为010 float3 ViewY float3(0,1,0); 求 X 坐标基向量 float3 ViewX cross(ViewZ,ViewY); 求 Y 坐标基向量 ViewY cross(ViewX,ViewZ); 2、求观察空间的基向量在世界空间中的矩阵 的 逆矩阵 float4x4 M_viewTemp float4x4 ( ViewX.x,ViewX.y,ViewX.z,0, ViewY.x,ViewY.y,ViewY.z,0, ViewZ.x,ViewZ.y,ViewZ.z,0, 0,0,0,1 ); 2、求平移变换矩阵 1 0 0 − T x 0 1 0 − T y 0 0 1 − T z 0 0 0 1 \begin{matrix} 100-T~x~\\ 010-T~y~\\ 001-T~z~\\ 0001\\ \end{matrix} 100001000010−T x −T y −T z 1 float4x4 M_viewTranslate float4x4 ( 1,0,0,-_ViewPos.x, 0,1,0,-_ViewPos.y, 0,0,1,-_ViewPos.z, 0,0,0,1 ); 3、相乘得出 观察空间转化矩阵 float4x4 M_view mul(M_viewTemp,M_viewTranslate); 4、得到顶点的世界空间坐标然后转化到观察空间 float3 vertexWS TransformObjectToWorld(v.vertexOS); float3 vertexVS mul(M_view,float4(vertexWS,1)); 5、把观察空间坐标转化为齐次裁剪坐标输出到屏幕 o.vertexCS TransformWViewToHClip(vertexVS); 二、最终效果
1、这是我们用默认Shader在该摄像机坐标下的游戏界面 2、使用我们的Shader并且给我们的ViewPos赋值为摄像机坐标 3、最终代码
//平移变换
//缩放变换
//旋转变换四维
Shader MyShader/URP/P3_6_5
{Properties{_Translate(Translate(XYZ),Vector) (0,0,0,0)_Scale(Scale(XYZ),Vector) (1,1,1,1)_Rotation(Rotation(XYZ),Vector) (0,0,0,0)[Header(View)]_ViewPos(View Pos,vector) (0,0,0,0)_ViewTarget(View Target,vector) (0,0,0,0)}SubShader{Tags{PenderPipelineUniversalPipelineRenderTypeOpaqueQueueGeometry}Pass{HLSLPROGRAM#pragma vertex vert#pragma fragment frag#include Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl#include Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl#include Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlslstruct Attribute{float4 vertexOS : POSITION;};struct Varying{float4 vertexCS : SV_POSITION;};CBUFFER_START(UnityPerMaterial)float4 _Translate;float4 _Scale;float4 _Rotation;float4 _ViewPos;float4 _ViewTarget;CBUFFER_ENDVarying vert (Attribute v){Varying o;//平移变换float4x4 M_Translate float4x4(1,0,0,_Translate.x,0,1,0,_Translate.y,0,0,1,_Translate.z,0,0,0,1);v.vertexOS mul(M_Translate,v.vertexOS);//缩放交换float4x4 M_Scale float4x4(_Scale.x,0,0,0,0,_Scale.y,0,0,0,0,_Scale.z,0,0,0,0,1);v.vertexOS mul(M_Scale,v.vertexOS);//旋转变换float4x4 M_rotateX float4x4(1,0,0,0,0,cos(_Rotation.x),sin(_Rotation.x),0,0,-sin(_Rotation.x),cos(_Rotation.x),0,0,0,0,1);float4x4 M_rotateY float4x4(cos(_Rotation.y),0,sin(_Rotation.y),0,0,1,0,0,-sin(_Rotation.y),0,cos(_Rotation.y),0,0,0,0,1);float4x4 M_rotateZ float4x4(cos(_Rotation.z),sin(_Rotation.z),0,0,-sin(_Rotation.z),cos(_Rotation.z),0,0,0,0,1,0,0,0,0,1);v.vertexOS mul(M_rotateX,v.vertexOS);v.vertexOS mul(M_rotateY,v.vertexOS);v.vertexOS mul(M_rotateZ,v.vertexOS);//观察空间矩阵推导//P_view [W_view] * P_world//P_view [V_world]^-1 * P_world//P_view [V_world]^T * P_worldfloat3 ViewZ normalize(_ViewPos - _ViewTarget);float3 ViewY float3(0,1,0);float3 ViewX cross(ViewZ,ViewY);ViewY cross(ViewX,ViewZ);float4x4 M_viewTemp float4x4(ViewX.x,ViewX.y,ViewX.z,0,ViewY.x,ViewY.y,ViewY.z,0,ViewZ.x,ViewZ.y,ViewZ.z,0,0,0,0,1);float4x4 M_viewTranslate float4x4(1,0,0,-_ViewPos.x,0,1,0,-_ViewPos.y,0,0,1,-_ViewPos.z,0,0,0,1);float4x4 M_view mul(M_viewTemp,M_viewTranslate);float3 vertexWS TransformObjectToWorld(v.vertexOS);float3 vertexVS mul(M_view,float4(vertexWS,1));o.vertexCS TransformWViewToHClip(vertexVS);//o.vertexCS TransformObjectToHClip(v.vertexOS.xyz);return o;}half4 frag (Varying i) : SV_Target{return 1;}ENDHLSL}}
}