当前位置:   article > 正文

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

alpha test 和 transparent

(1)关于Transparent半透明物体产生的阴影,特别注意半透明物体接受的阴影(不常用)
在这里插入图片描述
(2)关于AlphaTest物体产生阴影和接受阴影:(接受阴影的地方透明了,看不到阴影了)
在这里插入图片描述
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"
}

  • 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

(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"
}

  • 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
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/98253
推荐阅读
相关标签
  

闽ICP备14008679号