Unity里实现Sprite Renderer的阴影

最近有个2D游戏要做,有一个要做的点是,让Sprite Renderer产生阴影。
将以下脚本附到产生Shadow的物体上:

voidOnEnable(){

    GetComponent().receiveShadows =true;

    GetComponent().castShadows =true;

}

但是这是不够的,还需要Shader帮忙,下面的Shader请放到产生Shadow的物体上:

Shader "Custom/SpriteDiffuse"  
{  
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}

        _AlphaCutOff ("AlphaCutOff", Range(0,1)) = 0.05
    }

    SubShader
    {
        Pass
        {
            Tags {"LightMode"="ForwardBase"}

            CGPROGRAM

            #pragma vertex vert
            #pragma fragment frag

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

            sampler2D _MainTex;
            fixed _AlphaCutOff;

            struct appdata
            {
                half3 normal : NORMAL;
                float4 vertex : POSITION;
                float2 uv : TEXCOORD;
                fixed4 color : COLOR;
            };

            struct v2f
            {
                float2 uv : TEXCOORD;
                fixed4 color : COLOR;
                float4 vertex : SV_POSITION;
            };

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
                o.uv = v.uv;
                o.color = v.color;

                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                fixed4 col = tex2D(_MainTex, i.uv) * i.color;
                clip(col.a - _AlphaCutOff);
                return col;
            }
            ENDCG
        }

        Pass
        {
            Tags {"LightMode"="ShadowCaster"}

            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #pragma multi_compile_shadowcaster

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

            sampler2D _MainTex;
            fixed _AlphaCutOff;

            struct v2f { 
                V2F_SHADOW_CASTER;
                float4 texcoord : TEXCOORD1;
                fixed4 color : COLOR;
            };

            v2f vert(appdata_full v)
            {
                v2f o;
                o.texcoord = v.texcoord;
                o.color = v.color;
                TRANSFER_SHADOW_CASTER_NORMALOFFSET(o)
                return o;
            }

            float4 frag(v2f i) : SV_Target
            {
                fixed4 col = tex2D(_MainTex, i.texcoord) * i.color;
                clip(col.a - _AlphaCutOff);
                SHADOW_CASTER_FRAGMENT(i)
            }
            ENDCG
        }
    }
}  

此外还要有一个Shader,来接受阴影:

Shader "Custom/Diffuse2DTexture" {
    Properties {
        _MainTex ("Base (RGB)", 2D) = "white" {}
    }
    SubShader {
        Tags { "RenderType"="Opaque" }
        LOD 200
        
        CGPROGRAM
        #pragma surface surf Lambert

        sampler2D _MainTex;

        struct Input {
            float2 uv_MainTex;
        };

        void surf (Input IN, inout SurfaceOutput o) {
            half4 c = tex2D (_MainTex, IN.uv_MainTex);
            o.Albedo = c.rgb;
            o.Alpha = c.a;
        }
        ENDCG
    } 
    FallBack "Diffuse"
}

增加光源之后,运行Scene即可见阴影(不运行的话脚本的内容没有运行会导致不开启阴影)。

你可能感兴趣的:(Unity里实现Sprite Renderer的阴影)