简单来说就是这张图:
下面的程序可以用来验证:
// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
// This shader fills the mesh shape with a color predefined in the code.
Shader "Example/URPUnlitShaderBasic"
{
Properties
{}
SubShader
{
Tags { "RenderType" = "Opaque" "RenderPipeline" = "UniversalPipeline" }
Pass
{
HLSLPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/DeclareDepthTexture.hlsl"
struct Attributes
{
// Object Space 模型空间位置
float4 positionOS : POSITION;
};
struct Varyings
{
// Windows Space 窗口空间位置
float4 positionWS : SV_POSITION;
float4 positionHCS : TEXCOORD1;
float4 positionOS : TEXCOORD2;
};
Varyings vert(Attributes IN)
{
Varyings OUT;
OUT.positionOS = IN.positionOS;
// 进行 MVP 变换
OUT.positionWS = mul(UNITY_MATRIX_MVP,IN.positionOS);
OUT.positionHCS = OUT.positionWS;
return OUT;
}
// The fragment shader definition.
half4 frag(Varyings IN) : SV_Target
{
// 去掉注释观察模型空间上色情况
//return half4(IN.positionOS.xyz * 0.5 + 0.5,1);
// 去掉注释观察 HCS 空间上色情况
//return half4(IN.positionHCS.x/IN.positionHCS.w * 0.5 + 0.5,0,0,1); // 将物体在 x 轴移动,现象如图 1
//return half4(IN.positionHCS.y * 0.5 + 0.5/IN.positionHCS.w,0,0,1); // 将物体在 y 轴移动,现象类似图 1,只不过是在 y 轴上变化
// 去掉注释观察屏幕空间上色情况
return half4(IN.positionWS.xy/_ScaledScreenParams.xy,0,1); // 现象如图 2
}
ENDHLSL
}
}
}