Search Unity

Water shader graph: Transparency and shadows / universal render pipeline order

Discussion in 'Shader Graph' started by wahntin, Sep 20, 2019.

  1. wahntin

    wahntin

    Joined:
    Sep 17, 2019
    Posts:
    83
    Hi,
    I could use some help with a shadow+transparency related problem.

    I'm working on a water shader graph with the universal render pipeline in unity 2019.3.0b3.

    Since I wan't the water to be partly transparent I have set the surface property of the PBR Master to transparent.
    This however seems to disable zwrite for the water layer (in my case a sphere).

    Is there a way I can enable zwrite, but still have the transparency? Or is there a way to render the water layer after the shadows?


    upload_2019-9-20_15-47-9.png
     
    makaka-org and ROBYER1 like this.
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,339
    Nope.

    At least not yet when using the LWRP / URP. Your shader can be Opaque and ZWrite, or Transparent and not have ZWrite. There's no way to directly control the render state to choose something like opaque w/o zwrite or transparent w/ zwrite.

    However it looks like they've added this to some versions of the HDRP, so I suspect this will come to the URP at some point in the future.


    That said I don't think what you're seeing isn't shadows, it's SSAO. The ZWrite being on or off is a red herring. Changing between opaque and transparent does a couple other things on top of setting the blend mode and zwrite. Mainly it also sets the render queue, which in turn enables and disables the rendering of the depth only pass for the camera depth texture depending on if it's in the opaque or transparent queue range. That depth texture is what the SSAO (and the directional shadows) use. You might get better results by modifying the queue on your material manually to be 2499 (which is at the end of the opaque queue, before the transparent queue).

    I can't tell you for sure what the problem or the fix is. I haven't kept up with the URP lately to know where things stand there. But to me it looks like the SSAO is being applied after the water has been rendered, which to me suggests there's a bug with the URP and it's applying the SSAO after the transparent queue rather than before. I know there's also been a long time bug (several months now) in which transparent lit Shader Graph shaders aren't properly handling shadow receiving and the shadows appear transparent on them. This is also a bug that Unity needs to fix.


    Also, for the future, the Shader Graph subforum is this way -> https://forum.unity.com/forums/shader-graph.346/
     
    JoeGrainger likes this.
  3. wahntin

    wahntin

    Joined:
    Sep 17, 2019
    Posts:
    83
    Thanks for your reply!

    Yes, it looks like SSAO, but I can't find any setting for SSAO and switch form soft shadows to hard shadows makes it look like actual old-school hard shadows. Is there some default for SSAO in the URP?

    Changing the order in the queue did not fix it - it's easier no underwater scene at all or the shadow-transparency problem.

    Guess I will just have to wait and hope this get's fixed.

    Thanks for the hint to on the shader graph subforum - I somehow managed to not find it.

    BTW: This is my preliminary result:
     
    ROBYER1 likes this.
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,339
    Ah. If it really is shadows, then it's a long time bug with lit transparency. I'm not sure there's a fix for that still. But basically transparent objects are reading from the opaque screen space shadows instead of sampling the shadow maps directly (which they used to in some early versions of the LWRP) or not showing shadows at all (which is what the built in rendering paths do).

    It might be fixable if you turn off screen space shadows, but I don't know where that option is, if it exists at all, for the LWRP/URP.
     
  5. wahntin

    wahntin

    Joined:
    Sep 17, 2019
    Posts:
    83
    Hm, I tried but couldn't find any setting for this. Neither in the graphics quality setting, nor in the project settings or camera.
     
  6. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,339
    In the pipeline asset, set Shadows > Cascades to "No Cascades". That'll disable screen space shadows, but also mean shadows look worse in general. It does mean the water & transparencies will properly receive shadows though.

    Otherwise the same issue appears with the built-in LWRP/Lit shader, and is still broken in the latest URP. This is on Unity to fix as it's been broken for over a year now.
     
    hippocoder likes this.
  7. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,900
    you could fix it using a custom lighting function which explicitly samples shadows from the cascades instead of the screen space shadow map:

    #if defined(_MAIN_LIGHT_SHADOWS)
    float4 shadowCoordWS = TransformWorldToShadowCoord(positionWS);
    ShadowSamplingData shadowSamplingData = GetMainLightShadowSamplingData();
    half shadowStrength = GetMainLightShadowStrength();
    mainLight.shadowAttenuation = SampleShadowmap(shadowCoordWS, TEXTURE2D_ARGS(_MainLightShadowmapTexture, sampler_MainLightShadowmapTexture), shadowSamplingData, shadowStrength, false);
    #endif
     
    bgolus likes this.
  8. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,339
    Yeah. I should have specified, this is broken and unfixable by users when using Shader Graph straight. It's totally, and somewhat trivially, solvable by modifying the generated shader. That's nice because this was much more difficult for the built in rendering paths, but frustrating because it is so "easy" it's weird they've left it broken for a year..
     
    hippocoder likes this.
  9. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,900
    actually i thought about creating a custom node and adding a fixed lighting function using hlsl like described here:
    https://medium.com/@larsbertram1/lwrp-and-custom-lighting-in-shader-graph-6a7c48008a1d

    a way to at least get rid of the false shadows might be writing a custom shader gui where you disable the keyword _MAIN_LIGHT_SHADOWS – i haven't tested it tho.
     
  10. wahntin

    wahntin

    Joined:
    Sep 17, 2019
    Posts:
    83
    @boglus YES, that helped. Thanks for letting me know.
    Is there a bug/issue report for this problem? I had a look into the issue tracker but could not find it.

    Also thanks a lot for pointing out the advanced workaround. We are currently porting our game, so I need to go with the quick fixes for now, but will surely come back later!
     
  11. tomweiland

    tomweiland

    Joined:
    Dec 15, 2017
    Posts:
    19
    I've also been playing around with transparent water receiving shadows. The suggestion to turn off shadow cascades was a good one—especially with soft shadows enabled, it doesn't look bad at all.

    However, I'm struggling to get depth effects (shoreline foam) going with the Universal Render pipeline. I made a separate post for it (would appreciate any ideas/answers, as I haven't gotten any :)), but basically my water plane appears to be writing to the depth texture, which results in the "foam" rendering across the entire plane, not just in areas close to an intersection with another object.

    My understanding is that transparent objects shouldn't be writing to the depth texture, so I'm assuming I screwed something up while attempting to convert my built-in RP shader to work with URP.
     
  12. wahntin

    wahntin

    Joined:
    Sep 17, 2019
    Posts:
    83
  13. richard_revesz

    richard_revesz

    Joined:
    Feb 1, 2018
    Posts:
    6
    Hi Guys,
    I am after a transparent shadow receiver shader working in URP, any ideas how to get that working?
    (for AR shadows)

    Thanks,
    Richard
     
  14. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,900
    with urp 7.2. this should work right ouut of the box...
     
  15. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,339
    An AR transparent shadow receiver is a bit different than a shadow receiving transparency. The former doesn't require the later to exist (though it does make it a little easier). And having shadow receiving transparency, which is what URP 7.2.0 should have working, does not mean there'll be an out of the box solution for the AR case either as they're quite different in actual usage.

    There are a ton of examples of different transparent shadow receivers, from alpha tested ones with solid opaque colored shadows, to semi transparent ones that require careful manipulation of when it and the AR projection gets rendered. And these all work with the built in forward rendering path which does not have support for shadow receiving transparency. Most of them should be possible to recreate in the current URP, but will need hand written custom shaders. I'm not sure any of them are possible with Shader Graph, and I've not seen anyone release one yet either. Doesn't mean someone hasn't, I just haven't seen one.
     
  16. richard_revesz

    richard_revesz

    Joined:
    Feb 1, 2018
    Posts:
    6
    Well, it does not (7.2.1), neither in 7.1.8. I can have a transparent plane that is receiving shadows, however, the shadow intensity follows the alpha value. If I create a completely transparent ground plane just to receive shadows, setting alpha to zero also means the shadow disappears.


    Well, I have spent 2 days looking after a working one with no luck at all, this thread was the closeset to the topic. Anyways, thank you for the insight guys!
     
  17. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,339
    Try this:
    Code (CSharp):
    1.  
    2. Shader "URP AR Shadow Receiver"
    3. {
    4.     Properties
    5.     {
    6.         _ShadowColor ("Shadow Color", Color) = (0.35,0.4,0.45,1.0)
    7.     }
    8.  
    9.     SubShader
    10.     {
    11.         Tags
    12.         {
    13.             "RenderPipeline"="UniversalPipeline"
    14.             "RenderType"="Transparent"
    15.             "Queue"="Transparent-1"
    16.         }
    17.  
    18.         Pass
    19.         {
    20.             Name "ForwardLit"
    21.             Tags { "LightMode" = "UniversalForward" }
    22.  
    23.             Blend DstColor Zero, Zero One
    24.             Cull Back
    25.             ZTest LEqual
    26.             ZWrite Off
    27.    
    28.             HLSLPROGRAM
    29.             #pragma vertex vert
    30.             #pragma fragment frag
    31.  
    32.             #pragma prefer_hlslcc gles
    33.             #pragma exclude_renderers d3d11_9x
    34.             #pragma target 2.0
    35.  
    36.             #pragma multi_compile _ _MAIN_LIGHT_SHADOWS
    37.             #pragma multi_compile _ _MAIN_LIGHT_SHADOWS_CASCADE
    38.             #pragma multi_compile _ _SHADOWS_SOFT
    39.             #pragma multi_compile_fog
    40.  
    41.             #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
    42.  
    43.             CBUFFER_START(UnityPerMaterial)
    44.             float4 _ShadowColor;
    45.             CBUFFER_END
    46.  
    47.             struct Attributes
    48.             {
    49.                 float4 positionOS : POSITION;
    50.                 UNITY_VERTEX_INPUT_INSTANCE_ID
    51.             };
    52.  
    53.             struct Varyings
    54.             {
    55.                 float4 positionCS               : SV_POSITION;
    56.                 float3 positionWS               : TEXCOORD0;
    57.                 float fogCoord                  : TEXCOORD1;
    58.                 UNITY_VERTEX_INPUT_INSTANCE_ID
    59.                 UNITY_VERTEX_OUTPUT_STEREO
    60.             };
    61.  
    62.             Varyings vert (Attributes input)
    63.             {
    64.                 Varyings output = (Varyings)0;
    65.  
    66.                 UNITY_SETUP_INSTANCE_ID(input);
    67.                 UNITY_TRANSFER_INSTANCE_ID(input, output);
    68.                 UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
    69.  
    70.                 VertexPositionInputs vertexInput = GetVertexPositionInputs(input.positionOS.xyz);
    71.                 output.positionCS = vertexInput.positionCS;
    72.                 output.positionWS = vertexInput.positionWS;
    73.                 output.fogCoord = ComputeFogFactor(vertexInput.positionCS.z);
    74.  
    75.                 return output;
    76.             }
    77.  
    78.             half4 frag (Varyings input) : SV_Target
    79.             {
    80.                 UNITY_SETUP_INSTANCE_ID(input);
    81.                 UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
    82.  
    83.                 half4 color = half4(1,1,1,1);
    84.  
    85.             #ifdef _MAIN_LIGHT_SHADOWS
    86.                 VertexPositionInputs vertexInput = (VertexPositionInputs)0;
    87.                 vertexInput.positionWS = input.positionWS;
    88.  
    89.                 float4 shadowCoord = GetShadowCoord(vertexInput);
    90.                 half shadowAttenutation = MainLightRealtimeShadow(shadowCoord);
    91.                 color = lerp(half4(1,1,1,1), _ShadowColor, (1.0 - shadowAttenutation) * _ShadowColor.a);
    92.                 color.rgb = MixFogColor(color.rgb, half3(1,1,1), input.fogCoord);
    93.             #endif
    94.                 return color;
    95.             }
    96.  
    97.             ENDHLSL
    98.         }
    99.     }
    100. }
    101.  
    Works with URP 7.2.1, won't work with anything prior to that.* Only supports the main directional light shadows.
    upload_2020-2-25_0-4-24.png
    * May work with older versions of URP if you don't use shadow cascades.

    edit: 4/1/2020 - fix a bug where it shows as a solid color when shadows are disabled rather than being hidden.
     
    Last edited: Apr 2, 2020
  18. richard_revesz

    richard_revesz

    Joined:
    Feb 1, 2018
    Posts:
    6
    You are my hero!
    Thank you very much! =)
     
    zhaozony likes this.
  19. GVUnity

    GVUnity

    Joined:
    May 16, 2018
    Posts:
    5
    You are my hero too!
    I have been trying to do this for ages.
    Thank you!
     
  20. WalterEspinar

    WalterEspinar

    Joined:
    Aug 23, 2011
    Posts:
    84
    Its possible to convert this simple shader to URP?

    Code (CSharp):
    1. Shader "Custom/WaterZWrite" {
    2.     Properties{
    3.         _Color("Main Color", Color) = (1,1,1,1)
    4.         _MainTex("Base (RGB) Trans (A)", 2D) = "white" {}
    5.     }
    6.         SubShader{
    7.             Tags {"Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent"}
    8.             LOD 200
    9.  
    10.         // extra pass that renders to depth buffer only
    11.         Pass {
    12.             ZWrite On
    13.             ColorMask 0
    14.         }
    15.  
    16.         // paste in forward rendering passes from Transparent/Diffuse
    17.         UsePass "Transparent/Diffuse/FORWARD"
    18.     }
    19.         Fallback "Transparent/VertexLit"
    20. }
     
  21. Just_Lukas

    Just_Lukas

    Joined:
    Feb 9, 2019
    Posts:
    34
    That works for URP version > 7.1.8 (was testing it using URP 7.3.1) but sometimes under certain camera angles the entire mesh using this shader goes completely dark / black.

    Do you have any idea why that may be @bgolus? Or even better yet, how to fix that issue?
     
  22. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,339
    Nope! No idea. I tested it in 7.2.1, but things change so often with the SRPs I have no idea what they’ve changed.
     
  23. Just_Lukas

    Just_Lukas

    Joined:
    Feb 9, 2019
    Posts:
    34
    Well, I forgot to mention that bug was not only present in URP 7.3.1 but also in 7.2.0 & 7.2.1 after I've created brand new project from URP template. It just happened when I moved camera further from the world center (like 50-100 units away) and then started looking arbitrary directions, the floor using your shader would go black for some reason...

    A NEW HOPE: After 2 hours of searching I've found out I had disabled shadows casting on the ground plane which was using your transparent shadows shader. Enabling shadow casting fixed this "dark under certain cam angle" issue.

    So thanks for the shader and do you have any idea why re-enabling shadow casting solved this?
     
  24. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,339
    Sounds like a bug! (Not entirely sure if with my shader or the LWRP...)

    For the built in rendering paths, the directional shadow range is determined by the bound extents of both shadow casting and shadow receiving objects. What you described makes it sound like the LWRP is only looking at shadow casters to set the bounds. It’s possible my shader doesn’t properly handle the case where it’s outside the bounds of the shadow map, but as it’s literally just copying the code Unity uses for their lit shader with the lighting related code removed I’m not sure what I missed.

    If you use a transparent lit shader with shadow casting disabled does it also break? If so then it’s not my bug (“yay”?), and if it doesn’t then it is my bug (also “yay”).
     
  25. Just_Lukas

    Just_Lukas

    Joined:
    Feb 9, 2019
    Posts:
    34
    Oh I see I didn't notice you have just taken most of the Unity's Lit stuff good to know. Anyways the built in transparent lit shader w/ shadow casting disabled works just fine so SRP probably shouldn't be guilty of this.

    I am curious, if you can answer these questions it would be much appreciated:
    1. So when you were designing this shader, did you copy its parts from the LitForwardPass.hlsl?
    2. What is the exact meaning of "#ifdef _MAIN_LIGHT_SHADOWS"? I can't find anything. (Hate the poor shader documentation sometimes or am I just blind?)
    3. Is the IF from point 2 still needed if I have only single directional light in the scene which is always enabled & its shadow casting is always enabled?

    And btw I have fully working transparent shadows shader for URP 7.1.8 and lower which is without this shadow casting issue so I will investigate the differences and will post solution once I find it (not that it's critically important but still a small performance gain right?).
     
  26. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,339
    Yep, basically.

    Well, the SRP shaders aren’t really documented at all. Somewhat intentionally at the moment since the expectation is users are using Shader Graph for custom shaders... which is nice unless you’re trying to do something Shader Graph can’t.

    But in this case the keyword is self explanatory. It’s whether or not the main light has shadows.

    Maybe. The built in rendering paths will switch to shadow-less variants when an object is far enough away to be outside the shadow range. Actually, that’s probably the bug with my shader. It defaults to using the shadow color when shadows are off, and it should default to white.
     
  27. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,339
    Fixed and updated the original shader.
     
    olli_vrcoaster likes this.
  28. Just_Lukas

    Just_Lukas

    Joined:
    Feb 9, 2019
    Posts:
    34
    Yea that did the trick, I wish I had such an extended knowledge about shaders in SRPs like you do, thx a lot man.
     
  29. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,339
    Oh no, I barely know anything about the SRPs right now. Most of my knowledge about the URP shader code literally comes from writing the above shader (it was good research for looking at moving some future projects to the URP or a custom fork of it). I know almost nothing about the HDRP, that’s way, way more complex.
     
  30. Just_Lukas

    Just_Lukas

    Joined:
    Feb 9, 2019
    Posts:
    34
    Well apart from the fact that you are risking your custom hand written shaders to break with each new update of URP I can only recommend it. Especially for mobile platforms since you can set the "renderScale" easily. I have it set to 70% and still have nice final render w/ additional performance gain.
     
  31. Schodemeiss

    Schodemeiss

    Joined:
    Jan 4, 2018
    Posts:
    43
    I'm having some issues with depth and this shader. For example, if I use a Depth of Field effect (which I beleive relies on the depth buffer being written to), I get very blurry shadows on this catcher, rather than the sharp ones I'm expecting.

    An example can be seen here, where a slight depth of field effect has been applied to the camera, but you can see the overly blurry shadows in contrast to the otherwise sharp shadows on solid surfaces.

    Example: https://i.ibb.co/2vHr4fT/Example.png
     
  32. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,339
    Then don’t use that shader? It’s not really possible to have a transparent material that isn’t affected by or is correctly affected by depth of field. You’ll need to use an opaque material for this to work.
     
  33. Just_Lukas

    Just_Lukas

    Joined:
    Feb 9, 2019
    Posts:
    34
    Yea well as bgolus wrote, DoF cannot properly process transparent materials since they are not written into depth texture (not to be mistaken with depth buffer). You can see this if you swap this custom shader with for ex. URP's default Lit shader and set its Surface Type to transparent -> same result, then set it to opaque and everything works as you would exepect.

    Simple & quick solution suggested by bgolus will do the trick for you eg: replace your floor's shader to be either SimpleLit or Lit but OPAQUE w/ that light blue color.
     
  34. Schodemeiss

    Schodemeiss

    Joined:
    Jan 4, 2018
    Posts:
    43
    Ok, no worries. Thanks very much for the assistance!
     
  35. Just_Lukas

    Just_Lukas

    Joined:
    Feb 9, 2019
    Posts:
    34
    No problem
     
  36. L_Vi

    L_Vi

    Joined:
    Mar 5, 2015
    Posts:
    9

    Hi,
    Thank you so much for this. Currently I'm using SRP and an amplify shader that does the job perfectly (not mine), but I'm hoping to use URP instead.

    This is very close to being the golden ticket I've been looking for to make the change, except for one issue...

    When I build the project the shadows are present, but then when I capture with OBS the shadows are missing, as if their alpha is set to 0. Any ideas?

    ~LV
     
  37. DDobyns

    DDobyns

    Joined:
    Jan 28, 2018
    Posts:
    7
    Crazy question on this topic - I have a setup where a 3D animated character is being rendered by a secondary camera to a render texture (like a fake 2D sprite effect). End to end it looks like
    1. 3D character camera manually renders the 3D mesh via script to a render texture
    2. That render texture is applied as a texture to a quad
    3. The primary (2D) gameplay camera renders that quad similar to a sprite
    My 3D character is successfully casting a shadow on a transparent plane using the solution posted above (thanks bgolus!), but the resulting shadow doesn't seem to be getting rendered out to the render texture. I'm assuming I'm missing something with render order, like the shadow hasn't been drawn yet when my "character camera" does its render? Any thoughts?
     
  38. Gangsta-Geek

    Gangsta-Geek

    Joined:
    Aug 11, 2012
    Posts:
    32
    I have not gotten this shader to work on URP 10.2.2. Has anyone else tried got it to work?
     
    breghtvanbaelen likes this.
  39. NiteAngyl

    NiteAngyl

    Joined:
    Jun 15, 2017
    Posts:
    1
    Thank you for this! Works like a charm here. 2020.3.1f1 (LTS) with URP 10.3.2. using it together with AR Foundation PlaneOcclusionShader to render shadows on the ground. Really awesome work dude!
     
  40. DavidJang

    DavidJang

    Joined:
    Sep 14, 2019
    Posts:
    4
    Thank you a lot!
     
  41. HEROTECH70

    HEROTECH70

    Joined:
    May 24, 2017
    Posts:
    74
    Any chance getting this to work for Unity 2021.1 URP 11? I can't seem to find the issue

    Edit: I think a define has changed, removing #ifdef _MAIN_LIGHT_SHADOWS works fine
    \o/
     
    Last edited: Apr 22, 2021
  42. tra

    tra

    Joined:
    Mar 21, 2013
    Posts:
    32
    Thank you a lot for this shader!
    I am trying to render shadows with this shader to the temporary RenderTexture and encode PNG's to produce Icons with transparent background.
    I see shadows in the editor and game viewport but the shadows aren't encoded to the PNG's files. Looks like the render camera do not see the shadows during RenderTexture preparation or something.
    I did try with many options. Changing CameraClearFlags to "Depth", "Nothing" and backgroundColor to Color.Clear gives me the transparency in resulting file.
     
  43. tobiascook

    tobiascook

    Joined:
    Aug 29, 2017
    Posts:
    4
    Hey, could you elaborate on your fix here? I'm afraid I'm not familiar with shader language. I tried commenting out the line you mentioned as well as the endif, and also commenting out the whole block, but I still can't see shadows. Thanks!
     
  44. mgrugl

    mgrugl

    Joined:
    Mar 1, 2017
    Posts:
    23
    For what it's worth, got it to work in URP 11 as is. There seems to be a limitation to 1 shadow cascade. If e.g. you are starting a URP project from the template Unity provides, the URP Render Pipeline Asset has more than 1 cascade set. Once reduced to 1, the shadows appear if the camera is within the max shadow distance, also set via that same Asset.
     
    Voronoi likes this.
  45. HEROTECH70

    HEROTECH70

    Joined:
    May 24, 2017
    Posts:
    74
    That is the only thing I did, maybe it depends on some other project settings, you'll have to tinker around with it
     
    Mikeywalsh likes this.
  46. Matze_MP

    Matze_MP

    Joined:
    Dec 4, 2018
    Posts:
    37
    It work on 2021.1.15 for me. I deleted IfDef and endif line. Ialso have to fix a Problem with Z-Layering. To change that. Go to Tags Section (First in SubShader Category) and change "Queue" = "Transparent-1" to "Transparent+1"
     
  47. rabishan-maharjan

    rabishan-maharjan

    Joined:
    Jul 3, 2016
    Posts:
    18
    this worked form me too.
     
  48. cgascons

    cgascons

    Joined:
    Feb 22, 2016
    Posts:
    25
    I'm having a hard time trying to figure out how to make this shader when the 3D model in particular is projected over a render texture, instead of directly on the camera. This only works if the render texture camera has a skybox or a solid-non-alpha color. I need it transparent since what we are trying to achieve is to show a 3D character over a non-visible floor which receive shadows, quite similar to what Fortnite has to show their characters.
    Example attached below.


     
  49. uwdlg

    uwdlg

    Joined:
    Jan 16, 2017
    Posts:
    150
    A bit late to the party, but I just found this when trying to render images with transparency and shadows using the Unity Recorder. Initially had trouble with the shadows not showing up on the images (alpha 0), but looking at the shader code, I changed a few things and got the shadows to write "correct" alpha values. Here is the code with the changes I made, I added comments to the relevant lines:

    Code (CSharp):
    1. Shader "URP Transparent Shadow Receiver"
    2. {
    3.     Properties
    4.     {
    5.         _ShadowColor ("Shadow Color", Color) = (0.35,0.4,0.45,1.0)
    6.     }
    7.     SubShader
    8.     {
    9.         Tags
    10.         {
    11.             "RenderPipeline"="UniversalPipeline"
    12.             "RenderType"="Transparent"
    13.             "Queue"="Transparent-1"
    14.         }
    15.         Pass
    16.         {
    17.             Name "ForwardLit"
    18.             Tags { "LightMode" = "UniversalForward" }
    19.             Blend DstColor Zero, One One   // multiply alpha source value by One instead of Zero to preserve alpha info
    20.             Cull Back
    21.             ZTest LEqual
    22.             ZWrite Off
    23.  
    24.             HLSLPROGRAM
    25.             #pragma vertex vert
    26.             #pragma fragment frag
    27.             #pragma prefer_hlslcc gles
    28.             #pragma exclude_renderers d3d11_9x
    29.             #pragma target 2.0
    30.             #pragma multi_compile _ _MAIN_LIGHT_SHADOWS
    31.             #pragma multi_compile _ _MAIN_LIGHT_SHADOWS_CASCADE
    32.             #pragma multi_compile _ _SHADOWS_SOFT
    33.             #pragma multi_compile_fog
    34.             #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
    35.             CBUFFER_START(UnityPerMaterial)
    36.             float4 _ShadowColor;
    37.             CBUFFER_END
    38.             struct Attributes
    39.             {
    40.                 float4 positionOS : POSITION;
    41.                 UNITY_VERTEX_INPUT_INSTANCE_ID
    42.             };
    43.             struct Varyings
    44.             {
    45.                 float4 positionCS               : SV_POSITION;
    46.                 float3 positionWS               : TEXCOORD0;
    47.                 float fogCoord                  : TEXCOORD1;
    48.                 UNITY_VERTEX_INPUT_INSTANCE_ID
    49.                 UNITY_VERTEX_OUTPUT_STEREO
    50.             };
    51.             Varyings vert (Attributes input)
    52.             {
    53.                 Varyings output = (Varyings)0;
    54.                 UNITY_SETUP_INSTANCE_ID(input);
    55.                 UNITY_TRANSFER_INSTANCE_ID(input, output);
    56.                 UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
    57.                 VertexPositionInputs vertexInput = GetVertexPositionInputs(input.positionOS.xyz);
    58.                 output.positionCS = vertexInput.positionCS;
    59.                 output.positionWS = vertexInput.positionWS;
    60.                 output.fogCoord = ComputeFogFactor(vertexInput.positionCS.z);
    61.                 return output;
    62.             }
    63.             half4 frag (Varyings input) : SV_Target
    64.             {
    65.                 UNITY_SETUP_INSTANCE_ID(input);
    66.                 UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
    67.                 half4 color = half4(1,1,1,0);
    68.             #ifdef _MAIN_LIGHT_SHADOWS
    69.                 VertexPositionInputs vertexInput = (VertexPositionInputs)0;
    70.                 vertexInput.positionWS = input.positionWS;
    71.                 float4 shadowCoord = GetShadowCoord(vertexInput);
    72.                 half shadowAttenutation = MainLightRealtimeShadow(shadowCoord);
    73.                 // lerp from alpha 0 instead of 1 to have the mesh surface be fully transparent:
    74.                 color = lerp(half4(1,1,1,0), _ShadowColor, (1.0 - shadowAttenutation) * _ShadowColor.a);
    75.                 color.rgb = MixFogColor(color.rgb, half3(1,1,1), input.fogCoord);
    76.             #endif
    77.                 return color;
    78.             }
    79.             ENDHLSL
    80.         }
    81.     }
    82. }
    83.  
    Note that the shadows don't have the alpha value from _ShadowColor exactly, but I guess that's due to multiplying by shadowAttenuation.

    Hope that helps others stumbling upon this (I'll tag @tra and @cgascons here as this should solve your problems as well I think, in case it's still a hinderance for you).
     
    Last edited: Oct 25, 2021
    csy_move and PommieGamer like this.
  50. GrossPaul

    GrossPaul

    Joined:
    May 28, 2018
    Posts:
    1
    My Hero!