UnityShader – Vertex-Clipping-Transparenz basierend auf der Modell-Rendering-Ebene

//i.objPos.x/y/z 代表沿着哪个方向上进行裁剪
Shader "Custom/ModelMask"
{
	Properties{
		_Diffuse("Diffuse", Color) = (1,1,1,1)
		_DissolveColor("Dissolve Color", Color) = (0,0,0,0)
		_MainTex("Base 2D", 2D) = "white"{}
		_ColorFactor("ColorFactor", Range(0,1)) = 0.7           //这个值什么时候光圈在最下边呢,当模型比例为1时最下端点到中心点的Y值差
		_DissolveThreshold("DissolveThreshold", Range(0,1)) = 0 //裁剪比例阈值
	}

	SubShader
	{
		Tags{ "RenderType" = "Opaque" }
		Pass
		{
			Cull Off        //关闭背面裁剪,避免模型穿帮

			CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "Lighting.cginc"


            uniform fixed4 _Diffuse;
            uniform fixed4 _DissolveColor;
            uniform sampler2D _MainTex;
            uniform float4 _MainTex_ST;
            uniform float _ColorFactor;
            uniform float _DissolveThreshold;
        
            struct v2f
            {
                float4 pos : SV_POSITION;
                float3 worldNormal : TEXCOORD0;
                float2 uv : TEXCOORD1;
                float4 objPos : TEXCOORD2;
            };
        
            v2f vert(appdata_base v)
            {
                v2f o;
                o.pos = UnityObjectToClipPos(v.vertex);
                o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
                o.worldNormal = mul(v.normal, (float3x3)unity_WorldToObject);
                o.objPos = v.vertex;//以模型坐标为标准
                //o.objPos = mul(unity_ObjectToWorld, v.vertex);//以世界坐标为标准
                return o;
            }
        
            fixed4 frag(v2f i) : SV_Target
            {
                float factor = i.objPos.x - _DissolveThreshold + 0.5;//相对于中心在方向上的值减去阈值
                clip(factor);
                //Diffuse + Ambient光照计算
                fixed3 worldNormal = normalize(i.worldNormal);
                fixed3 worldLightDir = normalize(_WorldSpaceLightPos0.xyz);
                fixed3 lambert = saturate(dot(worldNormal, worldLightDir));
                fixed3 albedo = lambert * _Diffuse.xyz * _LightColor0.xyz + UNITY_LIGHTMODEL_AMBIENT.xyz;
                fixed3 color = tex2D(_MainTex, i.uv).rgb * albedo;
                //等价于下面注释代码
                fixed lerpFactor = saturate(sign(_ColorFactor - factor));
                /*
                if (factor < _ColorFactor)
                {
                return _DissolveColor;
                }
                return fixed4(color, 1);*/
                return lerpFactor * _DissolveColor + (1 - lerpFactor) * fixed4(color, 1);
            }
			ENDCG
		}
	}
	FallBack "Diffuse"
}

Guess you like

Origin blog.csdn.net/smile_otl/article/details/134370158