Search Unity

No shadows visible on Transparency Shaders?!!

Discussion in 'Shaders' started by seon, Apr 3, 2008.

  1. Reanimate_L

    Reanimate_L

    Joined:
    Oct 10, 2009
    Posts:
    2,788
    No no, i'm not talking about the water casting shadow, but receiving shadow on transparent surface. In 3D Gamekit they actually modify the shadow rendering pass by adding an additional shadow buffer for trasparent object, which in this case the lake water surface.
    Those are realtime shadow, not lightmap
    upload_2018-6-6_0-37-8.png
    upload_2018-6-6_0-38-25.png
     
    Last edited: Jun 5, 2018
    rob_ice likes this.
  2. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,549
    I was talking about it receiving shadows too... I said that the issue people talk about isn't about merely receiving shadows on a transparent surface, that is relatively easy to do. What is hard to do is do BOTH, to receive AND cast shadows. With a water surface, there is no need to cast shadow, so RECEIVING shadows isn't hard to implement.
     
  3. Reanimate_L

    Reanimate_L

    Joined:
    Oct 10, 2009
    Posts:
    2,788
    But transparent shadow casting are supported already, i think transparent shader casting shadow already supported since 2017 release
     
  4. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,549
    I didn't say it wasn't. I am guessing English isn't your first language? As there seems to be some real misinterpretation here. Having BOTH work at the same time is the tough part. BOTH. It's easy to add receiving shadows if you give up casting shadows.
     
  5. Reanimate_L

    Reanimate_L

    Joined:
    Oct 10, 2009
    Posts:
    2,788
    True, english is not my first language. regarding both receive and casting shadow, there's a reason why i recommend anyone in this thread to check 3D GameKit. Because the water are using a builtin casting shadow support with additional custom shadow receiver, which mean Both are works no need to give up anything, instead you adding something new.
     
  6. rob_ice

    rob_ice

    Joined:
    Nov 11, 2016
    Posts:
    112
  7. lauraaa

    lauraaa

    Joined:
    Dec 30, 2016
    Posts:
    16
    Hello! I've tried with the instruction to duplicate the pass and change the tag accordingly it doesn't work.
    Would you take a look while you can and shed some light on this? Still really new to shader stuff, thanks!

    Code (CSharp):
    1. Shader "MobileARShadow Include Other Lights"
    2. {
    3.     SubShader {
    4.         Pass {
    5.        
    6.             // 1.) This will be the base forward rendering pass in which ambient, vertex, and
    7.             // main directional light will be applied. Additional lights will need additional passes
    8.             // using the "ForwardAdd" lightmode.
    9.             // see: http://docs.unity3d.com/Manual/SL-PassTags.html
    10.             Tags { "LightMode" = "ForwardBase" "RenderType"="Opaque" "Queue"="Geometry+1" "ForceNoShadowCasting"="True"  }
    11.             LOD 150
    12.             Blend Zero SrcColor
    13.             ZWrite On
    14.        
    15.             CGPROGRAM
    16.             #pragma vertex vert
    17.             #pragma fragment frag
    18.             #include "UnityCG.cginc"
    19.             // 2.) This matches the "forward base" of the LightMode tag to ensure the shader compiles
    20.             // properly for the forward bass pass. As with the LightMode tag, for any additional lights
    21.             // this would be changed from _fwdbase to _fwdadd.
    22.             #pragma multi_compile_fwdbase
    23.             // 3.) Reference the Unity library that includes all the lighting shadow macros
    24.             #include "AutoLight.cginc"
    25.             struct v2f
    26.             {
    27.                 float4 pos : SV_POSITION;
    28.                
    29.                 // 4.) The LIGHTING_COORDS macro (defined in AutoLight.cginc) defines the parameters needed to sample
    30.                 // the shadow map. The (0,1) specifies which unused TEXCOORD semantics to hold the sampled values -
    31.                 // As I'm not using any texcoords in this shader, I can use TEXCOORD0 and TEXCOORD1 for the shadow
    32.                 // sampling. If I was already using TEXCOORD for UV coordinates, say, I could specify
    33.                 // LIGHTING_COORDS(1,2) instead to use TEXCOORD1 and TEXCOORD2.
    34.                 LIGHTING_COORDS(0,1)
    35.             };
    36.             v2f vert(appdata_base v) {
    37.                 v2f o;
    38.                 o.pos = UnityObjectToClipPos (v.vertex);
    39.                
    40.                 // 5.) The TRANSFER_VERTEX_TO_FRAGMENT macro populates the chosen LIGHTING_COORDS in the v2f structure
    41.                 // with appropriate values to sample from the shadow/lighting map
    42.                 TRANSFER_VERTEX_TO_FRAGMENT(o);
    43.                
    44.                 return o;
    45.             }
    46.             fixed4 frag(v2f i) : COLOR {
    47.            
    48.                 // 6.) The LIGHT_ATTENUATION samples the shadowmap (using the coordinates calculated by TRANSFER_VERTEX_TO_FRAGMENT
    49.                 // and stored in the structure defined by LIGHTING_COORDS), and returns the value as a float.
    50.                 float attenuation = LIGHT_ATTENUATION(i);
    51.                 return fixed4(1.0,1.0,1.0,1.0) * attenuation;
    52.             }
    53.             ENDCG
    54.         }
    55.  
    56.         Pass {
    57.        
    58.             Tags { "LightMode" = "ForwardAdd" "RenderType"="Opaque" "Queue"="Geometry+1" "ForceNoShadowCasting"="True"  }
    59.             LOD 150
    60.             Blend Zero SrcColor
    61.             ZWrite On
    62.        
    63.             CGPROGRAM
    64.             #pragma vertex vert
    65.             #pragma fragment frag
    66.             #include "UnityCG.cginc"
    67.             #pragma multi_compile_fwdadd
    68.             #include "AutoLight.cginc"
    69.             struct v2f
    70.             {
    71.                 float4 pos : SV_POSITION;
    72.                
    73.                 LIGHTING_COORDS(0,1)
    74.             };
    75.             v2f vert(appdata_base v) {
    76.                 v2f o;
    77.                 o.pos = UnityObjectToClipPos (v.vertex);
    78.                
    79.                 TRANSFER_VERTEX_TO_FRAGMENT(o);
    80.                
    81.                 return o;
    82.             }
    83.             fixed4 frag(v2f i) : COLOR {
    84.            
    85.                 float attenuation = LIGHT_ATTENUATION(i);
    86.                 return fixed4(1.0,1.0,1.0,1.0) * attenuation;
    87.             }
    88.             ENDCG
    89.         }
    90.     }
    91.    
    92.     // 7.) To receive or cast a shadow, shaders must implement the appropriate "Shadow Collector" or "Shadow Caster" pass.
    93.     // Although we haven't explicitly done so in this shader, if these passes are missing they will be read from a fallback
    94.     // shader instead, so specify one here to import the collector/caster passes used in that fallback.
    95.     Fallback "VertexLit"
    96. }
     
    rob_ice likes this.
  8. Amitloaf

    Amitloaf

    Joined:
    Jan 30, 2012
    Posts:
    97
    Wow this is an old one. Yeah, I have this problem too. No sure solution yet?
     
  9. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,549
    Use the new HDRP or one of the few full render pipeline replacements that have existed on the asset store.
     
  10. Reanimate_L

    Reanimate_L

    Joined:
    Oct 10, 2009
    Posts:
    2,788
    Huh? there's a renderpipeline in asset store already? or you mean a custom legacy renderer?
     
  11. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,549
    Custom legacy.
     
  12. fengkan

    fengkan

    Joined:
    Jul 10, 2018
    Posts:
    82
    Hi, which asset are you referring to exactly? Thank you.
     
  13. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,549
    I wasn't referring to a specific one, just the older ones that modified the legacy render pipeline (i.e: not HDRP or LWRP)
     
  14. tomweiland

    tomweiland

    Joined:
    Dec 15, 2017
    Posts:
    19
    @Invertex you mentioned that "with a water surface, there is no need to cast shadow, so RECEIVING shadows isn't hard to implement."

    I'm having this issue as well. I have a transparent water surface, but I'm in need of shadows (or at the very least a way of getting rid of specular reflections in areas that should be shadowed). I don't need the water to receive and cast shadows, but I haven't been able to find anything useful online. All shader examples I've tried don't work.

    Anyways, if you could provide me with some pointers as to how I could get the water to receive shadows while remaining transparent (preferably using a vertex/fragment shader, not a surface shader), I would greatly appreciate it!
     
  15. lpiljek

    lpiljek

    Joined:
    Nov 11, 2015
    Posts:
    12
    Well receiving shadows should never be a problem, since it's just reading the shadow map and casting shadows is only a problem if you want the actual shadows to be transparent (which I don't). So I don't know what Unity does to make this so difficult but I guess I'll take a look at modifying the rendering pipeline.
    (don't know why you would need an additional shadow buffer either)
     
  16. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,342
    Because on PC and console Unity casts the main directional shadows onto the camera depth texture to generate a screen space shadow mask, which is what the base forward pass actually samples, not the shadow map itself. And Unity decided long ago to not pass the shadow map or the values necessary to get the shadow map sampling position to transparent objects.

    In the last few years they stopped clearing the values needed after doing the screen space shadows, so a command buffer to keep a reference to the original directional light shadow maps and some custom shaders can allow for shadow receiving on transparent objects.

    See this project:
    https://github.com/Gaxil/Unity-InteriorMapping
     
    Shin_S, lpiljek and Invertex like this.
  17. Shin_S

    Shin_S

    Joined:
    Jan 22, 2015
    Posts:
    14
    Can you elaborate on how can you get and use that reference of the shadow maps please?
     
  18. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,342
  19. Chaosed0

    Chaosed0

    Joined:
    Jun 19, 2013
    Posts:
    23
    @bgolus, thanks so much for digging that up. Just implemented it, following the examples you'd posted, and it worked perfectly.
     
    AlejUb likes this.
  20. manutoo

    manutoo

    Joined:
    Jul 13, 2010
    Posts:
    523
  21. BlindsidedGames

    BlindsidedGames

    Joined:
    Aug 24, 2020
    Posts:
    9
    If I bake a lightmap into a scene and then put a transparent plane over the ground and want it to recieve only the shadows from the character how would I go about that?
     
  22. DeclanBarrett

    DeclanBarrett

    Joined:
    Dec 28, 2018
    Posts:
    1
    ...Its 2021 and Im still having this problem lmao
     
  23. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,342
    Technically they have solved this problem. Both the URP and HDRP support transparent materials receiving shadows from all lights. They also stopped updating the built in rendering paths outside of major bug fixes several years ago now.

    The short version is this will never be fixed for the existing built in rendering paths, because eventually the URP and/or the HDRP will completely replace them.
     
    noio likes this.
  24. UlfvonEschlauer

    UlfvonEschlauer

    Joined:
    Dec 3, 2014
    Posts:
    127
    Yeah, unfortunately my shader, which works well (except for the shadowmaps) in LWRP and BIRP, does not work at all in HDRP...
    Half of the built in functions are not there anymore, or something like that. So I would have to start all over again.
    This is frustrating, to say the least.
     
    Last edited: Aug 4, 2021