URP Shader for Sprites to Cast/Receive Shadows?

Hello everyone,

I’m going crazy trying to do this but I’m still very new to the whole shaders thing and it’s extremely confusing to me. I’m making a 2D game in a 3D environment and I need my sprites to cast and receive shadows using the Universal Rendering Pipeline. So far I’ve found shaders working on the Built-in Pipeline but none on URP, and I’ve no idea how to turn CG into HLSL to make it work. Is there a simple way to modify a shader based on the Sprite-Lit-Default or should I be using the Shader Graph? If so, how?

I know it’s an unusual thing to ask but I’m getting desperate over this, if I can’t figure it out I may well forget about this project.

Thanks!

Okay so I’m gonna just reply to myself as I figured it out by contrasting many sources and trying to understand how shaders work. Please don’t delete this as it could be helpful to anyone finding himself in the same situation. Create a Shader Graph as following:

This shader applied to a Sprite Renderer will Cast Shadow, Receive Shadow, the option to add a Normal Map, to change the Sprite’s Color, and change the Sprite’s Opacity.

It’s not perfect tho, I’d like to add the option to make the Sprite visible through walls and maybe also to add an Outline, if anyone knows how I’m listening!

6 Likes

Man … the exact issue I was having and there’s a helpful post made 3 days ago? You rock.

Small tweaks I had to do to make this work as I wanted:

  • The cog on the PBR master node allows you to set it to render two-sided (so sprites don’t vanish when you flip them)
  • In the same place, you can set the ‘Surface’ to transparent so that opacity will work
1 Like

You are right Aurigan, I forgot to mention that. Glad I could help :slight_smile:

Thanks man! This is exactly what I was looking for!

However my character doesn’t seem to cast a shadow on the world. Do you maybe know what could cause this?

So an update for anyone who’s having the same problem as me, I fixed it! Turns out something went wrong on unity’s end I think. I recreated the shader exactly as I did before, and I put it on a cube to see if it would cast a shadow.
This worked, but the sprites still didn’t work.
To get the shadows working on sprites, open the inspector in debug mode, and in the sprite renderer component you can turn on casting shadows.

5 Likes

Hey there!

I’ve tried setting this shader up, but instead of causing the sprite to cast shadows, it just turns the sprite into a “shadow”. IE: I’m just changing the color/transparency of the sprite that this shader/material is attached to.

Originally my project was in 2D, and I just flipped the switch to set it to 3D.

Can you share the latest URP shader? I found a shader that could receive shadow, but cast rectangle shadow only. Thank you.

You need to enable alpha clip in the shader graph