unity-shader模板测试-遮罩
实验效果
场景中有个 人物a, 隐藏看不见,  使用一个 平面b 当显示面, 当 平面b 移到与 人物a 重叠时, 显示 平面b 范围内的 人物a.

原理
- 先绘制 平面b, 同时把 模板缓冲区 填一个参考值1 (Ref 1, 值可以随意)
 
- 再绘制 人物a, 设置 参考值为1, 比较 模板缓冲区 的参考值 相同 才通过. 
 
- 由绘制的先后顺序可知, 平面b 的 RenderQueue 值 < 人物a. (越大越后绘制). shader代码
 
平面b 加上透明效果
平面b 的 RenderQueue  设为透明度类型之上, 假设为 3000. alpha 值调为 0即可
则 人物a 的为 3000+, 假设为 3001

效果

shader代码
平面b 上的shader - testStencilByMaskPlane.shader
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
   | Shader "ITS/test/testStencilByMaskPlane" { 	Properties { 		_MainTex ("Base (RGB)", 2D) = "white" {} 		_MainColor ("Color", Color) = (1, 1, 1, 1) 	}
  	SubShader { 		 		Tags {"Queue"="Transparent" "RenderType"="Transparent"} 		LOD 100 		Blend SrcAlpha OneMinusSrcAlpha 
  		Pass {   			Stencil 			{ 				Ref 1 				Comp Always 				Pass Replace 				ZFail Replace 			}
  			CGPROGRAM 				#pragma vertex vert 				#pragma fragment frag 				#pragma multi_compile_fog 				 				#include "UnityCG.cginc"
  				struct appdata_t { 					float4 vertex : POSITION; 					float2 texcoord : TEXCOORD0; 				};
  				struct v2f { 					float4 vertex : SV_POSITION; 					half2 texcoord : TEXCOORD0; 					UNITY_FOG_COORDS(1) 				};
  				sampler2D _MainTex; 				float4 _MainTex_ST; 				float4 _MainColor;
  				v2f vert (appdata_t v) 				{ 					v2f o; 					o.vertex = UnityObjectToClipPos(v.vertex); 					o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex); 					UNITY_TRANSFER_FOG(o,o.vertex); 					return o; 				} 				 				fixed4 frag (v2f i) : SV_Target 				{ 					fixed4 col = tex2D(_MainTex, i.texcoord); 					col *= _MainColor; 					UNITY_APPLY_FOG(i.fogCoord, col); 					 					return col; 				} 			ENDCG 		} 	} }
 
  | 
 
人物a 上的shader - testStencilByMaskChar .shader
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
   | Shader "ITS/test/testStencilByMaskChar" { 	Properties { 		_MainTex ("Base (RGB)", 2D) = "white" {} 		_MainColor ("Color", Color) = (1, 1, 1, 1) 	}
  	SubShader { 		Tags { "RenderType"="Opaque" } 		LOD 100 		 		Pass {   			ZTest LEqual 			Stencil 			{ 				Ref 1 				Comp Equal 			}
  			CGPROGRAM 				#pragma vertex vert 				#pragma fragment frag 				#pragma multi_compile_fog 				 				#include "UnityCG.cginc"
  				struct appdata_t { 					float4 vertex : POSITION; 					float2 texcoord : TEXCOORD0; 				};
  				struct v2f { 					float4 vertex : SV_POSITION; 					half2 texcoord : TEXCOORD0; 					UNITY_FOG_COORDS(1) 				};
  				sampler2D _MainTex; 				float4 _MainTex_ST; 				float4 _MainColor;
  				v2f vert (appdata_t v) 				{ 					v2f o; 					o.vertex = UnityObjectToClipPos(v.vertex); 					o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex); 					UNITY_TRANSFER_FOG(o,o.vertex); 					return o; 				} 				 				fixed4 frag (v2f i) : SV_Target 				{ 					fixed4 col = tex2D(_MainTex, i.texcoord); 					col *= _MainColor; 					UNITY_APPLY_FOG(i.fogCoord, col); 					UNITY_OPAQUE_ALPHA(col.a); 					return col; 				} 			ENDCG 		} 	} }
  |