1.shader代码
视口坐标范围 : [-1,1]
Shader "Custom/VertColorClipPos"
{
Properties
{
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
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;
fixed4 vcol : COLOR;
};
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
float x = o.vertex.x / o.vertex.w;
if(x <= -1)
{
o.vcol = fixed4(1,0,0,1);
}
else if(x >= 1)
{
o.vcol = fixed4(0,0,1,1);
}
else
{
float xx = x / 2 + 0.5;
o.vcol = fixed4(xx,xx,xx,1);
}
return o;
}
fixed4 frag (v2f i) : SV_Target
{
return i.vcol;
}
ENDCG
}
}
}
2.Cube例子
x坐标越接近左边越暗,越接近右边越亮
3.quad例子