Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Casting Shadows when calculating UV's in Fragment shader

Discussion in 'Shaders' started by maxhap, Aug 13, 2019 at 7:50 PM.

  1. maxhap

    maxhap

    Joined:
    Oct 24, 2013
    Posts:
    12
    Hi all,

    I'm not very good with shaders so please bear with me.

    I have a CG vertex and fragment shader that calculates UV coordinates in the fragment shader. The mesh texture (frame of a video blit onto a texture) has an alpha channel and is mostly transparent.

    Based on the transparency and UV's I want to cast a shadow onto objects behind the plane (diagram attached).

    Things I have tried:

    1. Using a basic ShadowCaster pass, this just cast a shadow of the entire plane.
    2. Using standard shader with cutout, leads to incorrect shadows (I believe this is because they are vertex calculated and the whole texture is taken into account)
    3. Calculate UV's also in ShaderCaster pass. Led to shader errors when trying to use unity_ObjectToWorld

    Things I thought about but couldn't find enough info to get it working:

    1. "Pre-process" the texture and blit a "Render Texture" with a 0 to 1 mapping onto the textured plane. I got stuck on this route because vertex data isn't passed into Graphics.Blit. From what I read this is because it's designed for texture processing.

    Any help or speculation on how I might achieve my goal would be great! :D
     

    Attached Files:

  2. Namey5

    Namey5

    Joined:
    Jul 5, 2013
    Posts:
    145
    You're right with the first 3 methods. You want to use a custom shadowcaster pass that calculate UVs and samples the texture in the same way as your normal shader, then use that to discard pixels in the same way as a cutout shader. What kind of error are you receiving in relation to the ObjectToWorld matrix?
     
  3. maxhap

    maxhap

    Joined:
    Oct 24, 2013
    Posts:
    12
    Hi Namey5,

    Thanks for your replay.

    After reading your comment I went back and tried the ShadowCaster again. It seems last night I was having some "late night programming syndrome". I didn't get the error at all and I'm now successfully casting shadows! :D:D

    On a similar note, can you think of a better/more performant way of doing this? Running two near identical passes seems excessive.
     
    Namey5 likes this.
  4. Namey5

    Namey5

    Joined:
    Jul 5, 2013
    Posts:
    145
    They might seem identical in nature, but they work in different ways. A shadowcaster pass only needs to know what pixels are being rendered; so any form of colour information you can simply leave out. Only keep any vertex shader changes/pixel discards because those actually change how the object is rendered, and return 0 (so long as you aren't using point light shadows). As far as more performant options go, the only real way I can think would be to completely ignore shadowmapping and manually reproject the image onto the surface of the plane.
     
  5. maxhap

    maxhap

    Joined:
    Oct 24, 2013
    Posts:
    12
    :D Thanks for the information! I will roll with this for now.

    Thanks again Namey5!