透明度測試的正確陰影

Shader "_MyShader/6_Light/3_AlphaTestWithShadow"
{
Properties
{
_Color ("Color ", COLOR) = (1,1,1,1)
_MainTex ("Texture", 2D) = "white" {}
_Cutoff ("CutOff", Range(0,0.9)) = 0.6
}
SubShader
{
Tags {"Queue" = "AlphaTest" "IgnoreProjecter" = "true" "RenderType" = "TransparentCutout"}

    pass {
        Tags {"LightMode" = "ForwardBase"}
        CGPROGRAM

        #pragma vertex vert
        #pragma fragment frag

        #include "Lighting.cginc"
        #include "UnityCG.cginc"
        #include "AutoLight.cginc"

        fixed4 _Color;
        sampler2D _MainTex;
        float4 _MainTex_ST;
        fixed _Cutoff;

        struct a2v {
            float4 vertex: POSITION;
            float3 normal: NORMAL;
            float4 texcoord: TEXCOORD0;
        };

        struct v2f {
            float4 pos: SV_POSITION;
            float3 worldNormalDir: TEXCOORD0;
            float3 worldLightDir: TEXCOORD1;
            float3 worldPos: TEXCOORD2;
            float2 uv: TEXCOORD3;
            SHADOW_COORDS(4)
        };

        v2f vert(a2v v){
            v2f o;
            o.pos = mul(UNITY_MATRIX_MVP,v.vertex);
            
            o.worldPos = mul(v.vertex,_Object2World).xyz;

            o.worldNormalDir = UnityObjectToWorldNormal(v.normal);
            o.worldLightDir = WorldSpaceLightDir(v.vertex);

            o.uv = TRANSFORM_TEX(v.texcoord,_MainTex);

            TRANSFER_SHADOW(o);

            return o;
        }

        fixed4 frag(v2f i):SV_Target{
            UNITY_LIGHT_ATTENUATION(atten,i,i.worldPos);

            fixed3 normalDir = normalize(i.worldNormalDir);
            fixed3 lightDir = normalize(i.worldLightDir);
            fixed4 tex = tex2D(_MainTex,i.uv);
            
            //AlphaTest
            //if(tex.a - _Cutoff <= 0)
            //  discard;
            clip(tex.a - _Cutoff);

            fixed3 albedo = tex.rgb *_Color.rgb;
            
            fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz * albedo;

            fixed3 diffuseC = _LightColor0.rgb * albedo * saturate(dot(normalDir,lightDir));

            fixed4 col = fixed4(ambient + diffuseC * atten,1);
            return col;
        }

        ENDCG
    }

}

FallBack "Transparent/Cutout/VertexLit"

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容