Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Transparent materials still casting shadows

Discussion in 'Shaders' started by HonoraryBob, May 22, 2016.

  1. HonoraryBob

    HonoraryBob

    Joined:
    May 26, 2011
    Posts:
    1,210
    For a 3rd person game, I've put in a script which makes objects between the camera and player mostly transparent so they don't block the view. To keep the interiors of buildings from suddenly having sunshine coming in when the roof or walls are made transparent, I'm using a surface shader which (nominally) still blocks light even when at full transparency; but it only blocks light if the fallback is set to "VertexLit" rather than "Alpha/Diffuse". That makes no sense, unless it either A) is falling back to these shaders rather than running mine, which also makes no sense; or B) maybe the surface shader is doing something differently underneath the hood depending on the fallback shader. Does anyone know what's going on? I need it to work consistently on other machines, so I need to know what's going on.
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,209
    Shadows are done by a shadow caster pass. The shadow caster pass for the vertex lit shader is always opaque regardless of the alpha value. By default a surface shader doesn't have its own shadow caster pass and is going to use the vertex lit shader's shadow caster pass, but if you set it to use alpha/diffuse as the fallback it'll use that shader's shadow caster pass. Alternately if you use the addshadow option it'll construct a shadow caster pass using your surface shader function rather than using a generic one.
     
    HonoraryBob likes this.
  3. HonoraryBob

    HonoraryBob

    Joined:
    May 26, 2011
    Posts:
    1,210
    Thank you. Is this documented anywhere?
     
  4. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Probably not.
     
  5. HonoraryBob

    HonoraryBob

    Joined:
    May 26, 2011
    Posts:
    1,210

    I added "#pragma addshadow" but that didn't work.
     
  6. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,209
    The addshadow goes in the #pragma surface line, not by itself. Even then with a transparent shader I don't know if it'll cast a shadow since transparent shaders don't generally cast shadows. Using Fallback "VertexLit" really is the correct solution.
     
  7. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,209
    HonoraryBob and hippocoder like this.