unity-shader半透明物体Transparent 和 AlphaTest的产生阴影和接收阴影

(1)关于Transparent半透明物体产生的阴影,特别注意半透明物体接受的阴影(不常用)
unity-shader半透明物体Transparent 和 AlphaTest的产生阴影和接收阴影_第1张图片
(2)关于AlphaTest物体产生阴影和接受阴影:(接受阴影的地方透明了,看不到阴影了)
unity-shader半透明物体Transparent 和 AlphaTest的产生阴影和接收阴影_第2张图片
Transparent物体的shader:

Shader "Unlit/shadowCast_Transparent"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        _AlphaScale("AlphaScale",Range(0,1))=0.5
    }
    SubShader
    {
        Tags { "RenderType"="Transparent" "Queue"="AlphaTest+50" "IgnorProjector"="True"}
        LOD 100

        Pass
        {
            ZWrite off
            Blend SrcAlpha OneMinusSrcAlpha
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag


            #include "UnityCG.cginc"
            #include "AutoLight.cginc"
            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;                
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
                SHADOW_COORDS(1)
            };

            sampler2D _MainTex;
            float4 _MainTex_ST;
            float _AlphaScale;

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = TRANSFORM_TEX(v.uv, _MainTex);
                TRANSFER_SHADOW(o);
                return o;
            }TRANSFER_SHADOW(o)

            fixed4 frag (v2f i) : SV_Target
            {
                // sample the texture
                fixed4 col = tex2D(_MainTex, i.uv);
                float shadow = SHADOW_ATTENUATION(i);
                col.a = col.a - _AlphaScale;
                col.rgb = col.rgb * shadow; 
                return col;
            }
            ENDCG
        }
    }
    Fallback "Diffuse"
}

(2)AlphaTest的shader:

Shader "Unlit/shadowCast_AlphaTest"
{
    Properties
    {
        _MainTex("MainTex",2D)="white"{}
		_Color("Diffuse",Color) = (1,1,1,1)
		_Specular("Specular", Color) = (1,1,1,1)
		_Gloss("Gloss", Range(8.0, 256)) = 20
        _Cutoff("alphaTest Cutoff value",Range(0,1))=0.2
    }
    SubShader
    {
        Tags { "RenderType" = "TransparentCutout" "Queue"="AlphaTest" }
		LOD 100
		Pass
		{
			Tags{"LightMode" = "ForwardBase"}
			CGPROGRAM
			#pragma multi_compile_fwdbase
			#pragma vertex vert
			#pragma fragment frag

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

			fixed4 _Color;
			fixed4 _Specular;
			float _Gloss;
            sampler2D _MainTex;
            float4 _MainTex_ST;
            float _Cutoff;
            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
				float3 normal:NORMAL;
            };

            struct v2f
            {
                float4 pos : SV_POSITION;
				float3 worldNormal : TEXCOORD0;
				float3 worldPos:TEXCOORD1;
				float3 vertexLight : TEXCOORD2;
				SHADOW_COORDS(3) //仅仅是阴影
                float2 uv : TEXCOORD4;
            };

            v2f vert (appdata v)
            {
                v2f o;
                o.pos = UnityObjectToClipPos(v.vertex);
				o.worldNormal = UnityObjectToWorldNormal(v.normal);
				o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
				//仅仅是阴影
				TRANSFER_SHADOW(o);
                o.uv = TRANSFORM_TEX(v.uv,_MainTex);
                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
				fixed3 worldNormal = normalize(i.worldNormal);
				fixed3 worldLightDir = normalize(UnityWorldSpaceLightDir(i.worldPos));
				fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;

                float4 cli = tex2D(_MainTex,i.uv);
                clip(cli.a - _Cutoff);

				fixed3 diffuse = cli.rgb* _LightColor0.rgb * _Color.rgb * max(0, dot(worldNormal,worldLightDir));

				fixed3 viewDir = normalize(_WorldSpaceCameraPos.xyz - i.worldPos.xyz);
				fixed3 halfDir = normalize(worldLightDir + viewDir);
				fixed3 specular = _LightColor0.rgb * _Specular.rgb * pow(max(0,dot(worldNormal,halfDir)),_Gloss);

				fixed shadow = SHADOW_ATTENUATION(i);

				//这个函数计算包含了光照衰减已经阴影,因为ForwardBase逐像素光源一般是方向光,衰减为1,atten在这里实际是阴影值
				//UNITY_LIGHT_ATTENUATION(atten, i, i.worldPos);

                return fixed4((ambient + (diffuse + specular) * shadow + i.vertexLight), 1);
            }
            ENDCG
        }
    }
    Fallback "Transparent/Cutout/VertexLit"
}

你可能感兴趣的:(unity-shader半透明物体Transparent 和 AlphaTest的产生阴影和接收阴影)