Search Unity

Add semitransparent shadow pass to a custom shader.

Discussion in 'Shaders' started by tomekkie2, May 12, 2017.

  1. tomekkie2

    tomekkie2

    Joined:
    Jul 6, 2012
    Posts:
    973
    I want to modify the ".FX/Glass/Stained..." shader from standard assets.
    I am wondering how could I add a faint shadow casting to it?
    I mean a faint shadow based on some color.alpha value.

    I was hoping to use a shadow casting pass from standard shader, because it supports transparency.
    I have added the color property, hoping to use alpha from it, and the
    Code (CSharp):
    1. UsePass "Standard/ShadowCaster"
    statement.
    But this doesn't seem to work that way.
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    The shadow caster pass from the standard shader is capable of doing semi-transparent shadows, but it requires specific keywords to be enabled on the material to make it work. By default the shadow is opaque and while you can force keywords on in the shader code using a #define, but those don't seem to work when you use UsePass.

    Instead you'll want to look at the standard.shader file and copy the shadowcaster pass from there into your own file and add this near the top of the CGPROGRAM block for it:
    #define _ALPHABLEND_ON

    It won't be a lot of code, as most of the shadow caster shader code is in a separate file (which there's going to be an #include for). As long as your custom shader uses the alpha of _MainTex and _Color to control the opacity of the shadow, it should "just work".
     
  3. tomekkie2

    tomekkie2

    Joined:
    Jul 6, 2012
    Posts:
    973
    Thanks.
    I have just pasted the whole shadowcaster pass from standard shader.
    Just replaced
    #pragma shader_feature _ _ALPHATEST_ON _ALPHABLEND_ON _ALPHAPREMULTIPLY_ON
    with
    #define _ALPHABLEND_ON
    and it works.