Unity UNITY_MATRIX_MVP resterizing calculation


렌더링 파이프라인의 좌표 공간들 – Rapapa Dev Story


Name
Value
UNITY_MATRIX_MVPCurrent model * view * projection matrix.
UNITY_MATRIX_MVCurrent model * view matrix.
UNITY_MATRIX_VCurrent view matrix.
UNITY_MATRIX_PCurrent projection matrix.
UNITY_MATRIX_VPCurrent view * projection matrix.
UNITY_MATRIX_T_MVTranspose of model * view matrix.
UNITY_MATRIX_IT_MVInverse transpose of model * view matrix.
unity_ObjectToWorldCurrent model matrix.
unity_WorldToObjectInverse of current world matrix.

UNITY_MATRIX_MVP는 Local Space의 Vertex를 Clip space로 옮겨주는 Matrix이다. 

Here is my shader:


Code (csharp):
  1.  
  2. Shader "Custom/Test" {
  3.     Properties {
  4.         _MainTex("Texture", 2D) = "white" { }  
  5.     }
  6.    
  7.     SubShader{ 
  8.    
  9.     Pass{
  10.    
  11.     Cull Back
  12.    
  13.     CGPROGRAM
  14.     #pragma vertex vert
  15.     #pragma fragment frag
  16.    
  17.     #include "UnityCG.cginc"
  18.    
  19.     sampler2D _MainTex;
  20.     float4x4 _MATRIX_MVP;
  21.  
  22.     struct v2f{
  23.         float4  pos : SV_POSITION;
  24.         float2  uv : TEXCOORD0;
  25.     };
  26.  
  27.     v2f vert(appdata_base v){
  28.        
  29.         v2f o;
  30.         float2 screenSpacePos;
  31.         float4 clipPos;
  32.    
  33.         //Convert position from world space to clip space.
  34.         //Only the UV coordinates should be frozen, so use a different matrix
  35.         clipPos = mul(_MATRIX_MVP, v.vertex);
  36.        
  37.         //Convert position from clip space to screen space.
  38.         //Screen space has range x=-1 to x=1
  39.         screenSpacePos.x = clipPos.x / clipPos.w;
  40.         screenSpacePos.y = clipPos.y / clipPos.w;
  41.  
  42.         //the screen space range (-1 to 1) has to be converted to
  43.         //the UV range 0 to 1
  44.         o.uv.x = (0.5f*screenSpacePos.x) + 0.5f;
  45.         o.uv.y = (0.5f*screenSpacePos.y) + 0.5f;
  46.    
  47.         //The position of the vertex should not be frozen, so use
  48.         //the standard UNITY_MATRIX_MVP matrix
  49.         o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
  50.  
  51.         return o;
  52.     }
  53.    
  54.     half4 frag(v2f i) : COLOR{
  55.    
  56.         half4 texcol = tex2D(_MainTex, i.uv);
  57.         return texcol;
  58.     }
  59.    
  60.     ENDCG
  61.    
  62.     }
  63.     }
  64. }
  65.  
From Unity I call:

Code (csharp):
  1.  
  2. Matrix4x4 P = GL.GetGPUProjectionMatrix(Camera.main.projectionMatrixfalse);
  3. Matrix4x4 V = Camera.main.worldToCameraMatrix;
  4. Matrix4x4 M = debugObject.renderer.localToWorldMatrix;
  5. Matrix4x4 MVP = P * V * M;
  6. debugObject.renderer.material.SetMatrix("_MATRIX_MVP", MVP);
  7.  

댓글

이 블로그의 인기 게시물

About AActor!!! "UObject" has no member "BeginPlay"

UNREAL Android build information

Shader informations nice blog ~ ~