Sprite receive shadow

Hi People!

After a lot of research I finally manage to make sprites cast shadows, but I can’t make it receive.

Like you can see in the image, the box can receive and cast shadow, but the sprite only cast.

I have castShadow and Receive shadow checked.

Perhaps is my Shader?

Can someone help me?

2314569--155999--shadow.png

Hi! May I know what shader are you using?

Hi!

I get the shader here in forum, it’s the default sprite with support to cast shadow.

I try legacy/transparent/cutout with modifications, it gave good results, but still not good enough.

Some suggestions?

Hi!

I managed to get.

This shader, make 2d sprites cast and receive shadows, BUT you can’t have negative order in layer (sortingOrder).

And you still have to active in renderer.

GetComponent<Renderer>().shadowCastingMode =  UnityEngine.Rendering.ShadowCastingMode.On;
GetComponent<Renderer>().receiveShadows = true;

It’s real unity3d shadow.

I hope it help others.

Shader "Sprites/Beat/Diffuse-Shadow"
{
    Properties
    {
        [PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
        _Color ("Tint", Color) = (1,1,1,1)
        _Cutoff ("Shadow alpha cutoff", Range(0,1)) = 0.5
    }

    SubShader
    {
        Tags
        {
            "Queue"="AlphaTest"
            "IgnoreProjector"="True"
            "RenderType"="TransparentCutout"
            "PreviewType"="Plane"
            "CanUseSpriteAtlas"="True"
        }

        LOD 200
        Cull Off
        Lighting On
        ZWrite Off

        CGPROGRAM
        #pragma surface surf Lambert addshadow alphatest:_Cutoff

        sampler2D _MainTex;
        fixed4 _Color;

        struct Input
        {
            float2 uv_MainTex;
        };

        void surf (Input IN, inout SurfaceOutput o)
        {
            fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
            o.Albedo = c.rgb;
            o.Alpha = c.a;
        }
        ENDCG
    }

    Fallback "Legacy Shaders/Transparent/Cutout/VertexLit"
}
4 Likes

Ive done the above but still dont see shadows? any ideas? :frowning:

edit: All i have is two sprites, a directional light, and a terrain. I see no shadow casted by the sprite on to the terrain, but the terrain itself has shading.

edit: turns out my shadow distance in the settings was too low!

Well I got it working in editor, but doesn’t work on iOS for some reason. Trial you ever try this in mobile

Edit: nvm, turns out to be the quality settings again, though I didn’t notice the green check marks.

Did it work? I’m glad I can help.

It worked Trial, and thanks! Right now I’m trying to get it to cast on an invisible plane though, because my sprites ‘face’ at an angle (like isometric, but not so much). I need the shadow to basically start from their legs not bottom of the feet (otherwise they look like they are floating). I was looking at this shader, and it works on OSX but doesn’t on iOS:

http://forum.unity3d.com/threads/matte-shadow.14438/#post-2321690

[quote=“trialforce, post:7, topic: 599284, username:trialforce”]
Did it work? I’m glad I can help.
[/quote]
I won’t say it doesn’t work, but still despite the fact I’ve done everything according to instructions, it does not.
Do you know what’s the problem?
Shadows cast only on 3D objects, but not on 2D sprites.

I’ve placed an

  • Object sprite
  • Cube
  • Background sprite

As you can see (pic related) Object’s shadow is only on the cube.
Every element has it’s cast/receiveShadow On (I’ve turned it on in the Inspector Debug mode). Spotlight has enabled shadows as well.
I would appreciate any help :slight_smile:

P.S. I have no transparency on any of my PNGs, but they are still semitransparent.

Hi!

Sorry, I only see your post now. can you send a zip with your project, I can have a look for you.

I’m expriencing a similar issue, my sprites are able to cast shadows but not receive them. Could it be a 5.2.3 problem?

Hi!

I will make a working example. Please wait a couple days.

Regards.

2 Likes

Hello and thanks for sharing!

Can you explain me a bit where did you write these lines of code you are talking about here:

Is it on the shader itselft?

Thanks a lot

Put it in a script attached to the sprite, in the Awake() or Start() callback. You need to enable the shadows via code since it isn’t exposed in the editor.

1 Like

Thx I’ll try it asap!

Trialforce, have you find any solution yet? Using your shader with Sprite and Directional light make all my sprites simply disappear )

Hi, you can set shadow receiver/caster even without script. If you click on small menu icon in almost top-right corner (next to pad lock) and select Debug then you can set both these properties:

8 Likes

so did anyone get the solution of shadow receiving?

You should use a cutout shader if you want to receive shadows. Because transparent pass cannot due to being rendered after geometry pass for sorting purposes. It’s a well known common issue with GPU design.

what do u mean under cutout shader?