Search Unity

Question URP Shader for Sprites to Cast/Receive Shadows?

Discussion in 'Shaders' started by GerardChalaux, Aug 13, 2020.

  1. GerardChalaux

    GerardChalaux

    Joined:
    Sep 11, 2018
    Posts:
    17
    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!
     
  2. GerardChalaux

    GerardChalaux

    Joined:
    Sep 11, 2018
    Posts:
    17
    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:

    ShaderGraph_SpriteDiffuseShadowTransparency.jpg

    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!
     
  3. Aurigan

    Aurigan

    Joined:
    Jun 30, 2013
    Posts:
    291
    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
     
    GerardChalaux likes this.
  4. GerardChalaux

    GerardChalaux

    Joined:
    Sep 11, 2018
    Posts:
    17
    You are right Aurigan, I forgot to mention that. Glad I could help :)
     
  5. jmaster2710

    jmaster2710

    Joined:
    Apr 25, 2017
    Posts:
    7
    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?
     
  6. jmaster2710

    jmaster2710

    Joined:
    Apr 25, 2017
    Posts:
    7
    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.
     
  7. empath

    empath

    Joined:
    Sep 27, 2013
    Posts:
    2
    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.
     
  8. Solidcomer

    Solidcomer

    Joined:
    Sep 12, 2017
    Posts:
    95
    Can you share the latest URP shader? I found a shader that could receive shadow, but cast rectangle shadow only. Thank you.
     
  9. milkshakegamesmanagement

    milkshakegamesmanagement

    Joined:
    Jun 29, 2023
    Posts:
    1
    You need to enable alpha clip in the shader graph