之前一直觉得传送门挺高大上的,今日抽空看看案例才发现,其实原理只是模型上加了遮罩
学习链接 : https://www.bilibili.com/video/av34740402
一.模型准备工作
在上面的学习链接有工程,我用的是他们的模型,仔细分析了下,主要关键的是两个MaskShader
①.MainShader : 模型材质应选择的shader
Shader "MASK/MainShader"
{
Properties
{
_MainTex("Texture", 2D) = "white" {}
_Color("Main Color", Color) = (1,1,1,1)
_BumpMap("Normalmap", 2D) = "bump" {}
_Glossiness("Smoothness", Range(0,1)) = 0.5
_Metallic("Metallic", Range(0,1)) = 0.0
}
SubShader
{
Tags{ "RenderType" = "Opaque" "Queue" = "Geometry-2" }
LOD 200
Stencil
{
Ref 1
Comp equal
Pass keep
}
CGPROGRAM
#pragma surface surf Standard
sampler2D _MainTex;
sampler2D _BumpMap;
struct Input
{
float2 uv_MainTex;
float2 uv_BumpMap;
};
half _Glossiness;
half _Metallic;
fixed4 _Color;
void surf(Input IN, inout SurfaceOutputStandard o)
{
fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
o.Albedo = c.rgb;
o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
o.Metallic = _Metallic;
o.Smoothness = _Glossiness;
}
ENDCG
}
FallBack "Diffuse"
}
②.MaskShader : 遮罩shader
Shader "MASK/MaskShader"
{
SubShader
{
Tags{ "RenderType" = "Opaque" "Queue" = "Geometry-3" }
ColorMask 0
ZWrite on
CGINCLUDE
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
};
struct v2f
{
float4 pos : SV_POSITION;
};
v2f vert(appdata v)
{
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
return o;
}
half4 frag(v2f i) : SV_Target
{
return half4(0,0,0,0);
}
ENDCG
Pass
{
Stencil
{
Ref 1
Comp always
Pass replace
}
Cull Front
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
ENDCG
}
}
}
例如案例中的模型身上 :
边上墙要增加遮罩 :
但是我没太明白为什么要加上Cude一个大遮罩,如果知道的朋友请赐教,感谢
网友评论