先看效果,后上代码,最后解释:
(1)使用stencil,控制球体显示在墙体Cube上:scene中位置排序:相机->墙体Cube->球体->背景BG
(2)在game中的效果:球体->墙体Cube->背景BG
Shader "Unlit/stencil_BG01"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_refVal("Stencil ref value",int)=0
}
SubShader
{
Tags {"Queue"="Geometry+1" "RenderType"="Opaque" }
LOD 100
Pass
{
Tags{ "LightModel"="OnForwardBase" }
//ZWrite Off
ZTest Always
Stencil{
Ref[_refVal]
Comp Always //默认为 Always
Pass Replace
Fail Replace
}
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
// make fog work
#pragma multi_compile_fog
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
UNITY_FOG_COORDS(1)
float4 vertex : SV_POSITION;
};
sampler2D _MainTex;
float4 _MainTex_ST;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
UNITY_TRANSFER_FOG(o,o.vertex);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
// sample the texture
fixed4 col = tex2D(_MainTex, i.uv);
// apply fog
UNITY_APPLY_FOG(i.fogCoord, col);
return col;
}
ENDCG
}
}
}
Shader "Unlit/stencil_Sphere02"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_refVal("Stencil ref value",int)=10
}
SubShader
{
Tags {"Queue"="Geometry+2" "RenderType"="Opaque" }
LOD 100
Pass
{
Tags{ "LightModel"="ForwardBase" }
ZTest Always
//ColorMask rgb //0代表不输出任何颜色
Stencil
{
Ref[_refVal]
Comp GEqual
Pass Replace
}
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
// make fog work
#pragma multi_compile_fog
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
UNITY_FOG_COORDS(1)
float4 vertex : SV_POSITION;
};
sampler2D _MainTex;
float4 _MainTex_ST;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
UNITY_TRANSFER_FOG(o,o.vertex);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
// sample the texture
fixed4 col = tex2D(_MainTex, i.uv);
// apply fog
UNITY_APPLY_FOG(i.fogCoord, col);
return col;
}
ENDCG
}
}
}
Shader "Unlit/stencil_Cube03"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_refVal("Stencil ref value",int)=5
}
SubShader
{
Tags {"Queue"="Geometry+3" "RenderType"="Opaque" }
LOD 100
Pass
{
Tags{"LightModel"="ForwardBase" }
//ZWrite Off
ZTest Always
Stencil
{
Ref[_refVal]
Comp GEqual
Pass Replace
}
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
// make fog work
#pragma multi_compile_fog
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
UNITY_FOG_COORDS(1)
float4 vertex : SV_POSITION;
};
sampler2D _MainTex;
float4 _MainTex_ST;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
UNITY_TRANSFER_FOG(o,o.vertex);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
// sample the texture
fixed4 col = tex2D(_MainTex, i.uv);
// apply fog
UNITY_APPLY_FOG(i.fogCoord, col);
return col;
}
ENDCG
}
}
}
上面三个shader对应3个物体
(3)下面来解释下stencil命令:
最核心的理解就下面这几句:
Stencil
{
Ref[_refVal]
Comp GEqual
Pass Replace
}
Ref[_refVal]: 将_refVal 和Stencil中的值比较,Stencil缓存中默认为0
Comp GEqual : 比较条件是“大于等于”
Pass Replace:符合比较条件的执行“Replace”操作
以上的效果,也可以通过控制渲染顺序实现,这里不再多说,多动手多思考,FrameDebug是个好东西!!