Search Unity

Shadow and distance attenuation do not work in URP Unlit Graph

Discussion in 'Universal Render Pipeline' started by CogumeloSoft, Jan 3, 2020.

  1. CogumeloSoft

    CogumeloSoft

    Joined:
    Dec 28, 2012
    Posts:
    88
    Hi!
    I'm using Unity 2019.3.0f3 and URP 7.1.7
    I'm trying to follow this tutorial:
    https://blogs.unity3d.com/pt/2019/0...n-shader-graph-expanding-your-graphs-in-2019/

    But the "mainLight.shadowAttenuation" and "mainLight.distanceAttenuation" always return white if using a "Unlit Shader" as Base. It works with a "PBR" shader but this is not what i need (since i need a fully controllable light model.

    Is it a bug or there's something new that is not documented about it?
     
    TheOnePoly and MrPaparoz like this.
  2. phil_lira

    phil_lira

    Unity Technologies

    Joined:
    Dec 17, 2014
    Posts:
    584
    The unlit master node was never really meant to support shadows. However I'm almost certain that you only need to add the following lines to support it:

    Code (CSharp):
    1. #pragma multi_compile _ _MAIN_LIGHT_SHADOWS
    2. #pragma multi_compile _ _MAIN_LIGHT_SHADOWS_CASCADE
     
    sergiomlp likes this.
  3. CogumeloSoft

    CogumeloSoft

    Joined:
    Dec 28, 2012
    Posts:
    88
    So your tutorial is just wrong and there's no way to use custom light model anyway?
    I've added this to the mainlight.hlsl (the custom function file) but it's not working too. Any other solution?
     
    Cynicat and TextusGames like this.
  4. MrPaparoz

    MrPaparoz

    Joined:
    Apr 14, 2018
    Posts:
    157
    I don't know yours but somehow my unlit shaders cast shadows in 2019.3.0f3 URP 7.1.6
     
  5. CogumeloSoft

    CogumeloSoft

    Joined:
    Dec 28, 2012
    Posts:
    88
    Did you tested with 7.1.7?
     
  6. MrPaparoz

    MrPaparoz

    Joined:
    Apr 14, 2018
    Posts:
    157
    no there is no package update in package manager.
     
  7. phil_lira

    phil_lira

    Unity Technologies

    Joined:
    Dec 17, 2014
    Posts:
    584
    You need to add multi_compile to .shader files. However seems like ShaderGraph now supports declaring keywords. So you can declare those by using the keyword mechanism instead: https://docs.unity3d.com/Packages/com.unity.shadergraph@7.1/manual/Keywords.html

    Ideally you should not need to worry about declaring keywords for this. We can improve this quite a bit.
     
  8. CogumeloSoft

    CogumeloSoft

    Joined:
    Dec 28, 2012
    Posts:
    88
    Hi. This is my shader graph and the mainlight.hlsl file. It's yet not working and i'm getting this error:

    Shader error in 'Shader Graphs/Base': invalid subscript 'shadowCoord' at /Vitor/Trabalho/Cogumelo/Projetos/Game2020/Game2020/Library/PackageCache/com.unity.render-pipelines.universal@7.1.7/Editor/ShaderGraph/Includes/Varyings.hlsl(118) (on d3d11)



    What i'm doing wrong?
     
    CyRaid likes this.
  9. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,902
    CogumeloSoft likes this.
  10. CogumeloSoft

    CogumeloSoft

    Joined:
    Dec 28, 2012
    Posts:
    88
    Tibor0991 likes this.
  11. xHammy

    xHammy

    Joined:
    Aug 11, 2017
    Posts:
    27
    That solution still only half works, as even when you set all the PRB parameters to 0 (URP 2019.3.0f3) you still don't remove all of what PBR seems to do. So that trick which worked in LWRP doesn't work in URP.
     
  12. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,902
    i justed had a quick look into the complied shader code: only one light loop survives the shader compiler which is the custom one. tested with urp 7.1.7.
     
  13. xHammy

    xHammy

    Joined:
    Aug 11, 2017
    Posts:
    27
    Is that in specular or metallic mode? In metallic mode I can clearly see that the metallic lighting is surviving, but not in specular mode, can't say anything about the compilers.

    Also, this
    presentation references the article at the top of this thread, but it's more recent and shadows are quite clearly working on an unlit node so that might be worth looking into.
     
    cirocontinisio likes this.
  14. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,902
    you have to use specular mode.
    metallic does not really exist. it is just an intermediate step which converts metallic to specular. and this step will never set spec to 0,0,0 rather than to a fixed spec value. so in this case some lighting code actually survives...
     
  15. Prodigga

    Prodigga

    Joined:
    Apr 13, 2011
    Posts:
    1,123
    @phil_lira please dont leave us in the dark here.

    I watched the video above to learn how to do custom lighting.

    The resources for the video above are found here:
    https://github.com/Unity-Technologies/ShaderGraph-Custom-Lighting

    These steps do not work anymore. As @CogumeloSoft pointed out, you get an error. It seems you need to declare another keyword to make it work - REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR. If you do not define this keyword, then the Varyings struct does not contain shadowcoord interpolator. But there is no way to define keywords in the shadergraph?

    The shader graph lets you add a 'predefined' keyword.. For example, I enter this:
    upload_2020-3-22_20-20-7.png

    But it still doesn't seem to work.

    How do we just get main light information, including shadows?

    I still get the same error as him:
    upload_2020-3-22_20-26-25.png
     
    chrismarch likes this.
  16. alexanderameye

    alexanderameye

    Joined:
    Nov 27, 2013
    Posts:
    1,383
    edupo, lasiaylo, OneratGames and 2 others like this.
  17. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,902
    you can wipe out all:
    #if SHADOWS_SCREEN
    7.2. does not support screen space shadows any more.
     
    alexanderameye likes this.
  18. alexanderameye

    alexanderameye

    Joined:
    Nov 27, 2013
    Posts:
    1,383
    Cool thank you for the tip!
     
  19. xHammy

    xHammy

    Joined:
    Aug 11, 2017
    Posts:
    27
    That gives you shadows, but they're low Rez and seem to disagree with the shadow Cascades
     
  20. jakeymg

    jakeymg

    Joined:
    Mar 11, 2020
    Posts:
    1
    This worked for me, including part underneath about removing shadow screen etc
     
  21. melos_han_tani

    melos_han_tani

    Joined:
    Jan 11, 2018
    Posts:
    79
    I was able to use that pastebin to get shadow attenuation working, but like xHammy the cascade info seems to be working incorrectly - my shadows are a lot blockier than they should be. Any solutions?
     
  22. TextusGames

    TextusGames

    Joined:
    Dec 8, 2016
    Posts:
    429
    1. Unlit means not lighten. So the word suggests it receives no lighting (no light nor shadow).

    2. PBR is a Physically Based Rendering model. A completely custom lighting model should not be used in another lightning model (PBR).

    3. There is no custom lightning master node in shader graph.


    I personally think there should be some custom lighting master node that is similar to the unlit graph.

    If there is no custom lightening maser node, I think an unlit shader should be used then you trying to implement a custom lighting model. (because it is the simplest master node). And in order to make a custom lighting model, an unlit shader graph should be able to receive at least a light and shadow data.

    If there is no normal way to implement the simplest custom lighting, a scripted build pipeline is not enough customizable at least. And not good enough in general. For me.


    Taking into account the above sentences,
    Answer please, how do you think one can do performant and precise simple custom lighting model in shader graph without getting a base lighting element as shadow data in unlit master node and without custom lightning master node(and without dancing with PBR graph which is already different lighting model)?
     
  23. chrismarch

    chrismarch

    Joined:
    Jul 24, 2013
    Posts:
    472
    If you are using hard shadows without cascades, you can reduce your URP setting shadow distance.

    If you are using cascades and hard shadows, you may need to monkey around more with keywords and settings, not sure.

    However, if you are using soft shadows, this helps:
    upload_2020-4-20_18-6-14.png
     
    TextusGames likes this.
  24. TextusGames

    TextusGames

    Joined:
    Dec 8, 2016
    Posts:
    429
    And if you define shadow cascade keyword now it will receive cascades ( just be shure those are keywords and not the properties).
    upload_2020-4-21_12-26-1.png

    Thank you it is working in URP 7.3.1
     
    chrismarch likes this.
  25. melos_han_tani

    melos_han_tani

    Joined:
    Jan 11, 2018
    Posts:
    79
    Thanks so much for your help all!

    Defining both of those keywords - _SHADOWS_SOFT (as multicompile global default) and _MAIN_LIGHT_SHADOWS_CASCADE (as shader feature global default), combined with this custom lighting function worked for me.

    Code (CSharp):
    1. void MainLight_half(float3 WorldPos, out half3 Direction, out half3 Color, out half DistanceAtten, out half ShadowAtten)
    2. {
    3.     #if SHADERGRAPH_PREVIEW
    4.         Direction = half3(0.5, 0.5, 0);
    5.         Color = 1;
    6.         DistanceAtten = 1;
    7.         ShadowAtten = 1;
    8.     #else
    9.         #if SHADOWS_SCREEN
    10.             half4 clipPos = TransformWorldToHClip(WorldPos);
    11.             half4 shadowCoord = ComputeScreenPos(clipPos);
    12.         #else
    13.             half4 shadowCoord = TransformWorldToShadowCoord(WorldPos);
    14.         #endif
    15.             Light mainLight = GetMainLight(shadowCoord);
    16.             Direction = mainLight.direction;
    17.             Color = mainLight.color;
    18.             DistanceAtten = mainLight.distanceAttenuation;
    19.  
    20.         #if !defined(_MAIN_LIGHT_SHADOWS) || defined(_RECEIVE_SHADOWS_OFF)
    21.             ShadowAtten = 1.0h;
    22.         #endif
    23.  
    24.         #if SHADOWS_SCREEN
    25.             ShadowAtten = SampleScreenSpaceShadowmap(shadowCoord);
    26.         #else
    27.             ShadowSamplingData shadowSamplingData = GetMainLightShadowSamplingData();
    28.             half shadowStrength = GetMainLightShadowStrength();
    29.             ShadowAtten = SampleShadowmap(shadowCoord, TEXTURE2D_ARGS(_MainLightShadowmapTexture,
    30.             sampler_MainLightShadowmapTexture),
    31.             shadowSamplingData, shadowStrength, false);
    32.         #endif
    33.     #endif
    34. }
     
    y_fujita, TextusGames and chrismarch like this.
  26. decept

    decept

    Joined:
    Sep 16, 2013
    Posts:
    9
    Hi guys! I followed all this steps, but i got this error. Any ideas? Unity 2019.3.5f1 URP 7.3.1
    upload_2020-5-13_13-52-27.png
     
  27. TextusGames

    TextusGames

    Joined:
    Dec 8, 2016
    Posts:
    429
    Change precision to half.
     
  28. decept

    decept

    Joined:
    Sep 16, 2013
    Posts:
    9
    Nothing happens
     
  29. decept

    decept

    Joined:
    Sep 16, 2013
    Posts:
    9
    I changed precision to half and added an additional Vector3 input. Now custom function looks similar to the one shown here. But the material now has only its own shadows. But I need to make this unlit material capable of getting drop shadows.

    upload_2020-5-13_20-21-33.png
     
    Last edited: May 13, 2020
  30. decept

    decept

    Joined:
    Sep 16, 2013
    Posts:
    9
    If we are talking about self shadows - I made it with _WorldSpaceLightPos0 parameter reference. Simple and works great. But i need drop shadows...
    upload_2020-5-13_20-28-45.png
     
  31. TextusGames

    TextusGames

    Joined:
    Dec 8, 2016
    Posts:
    429
    You need to multiply shadow attenuation(from custom function) with other nodes, for example with your remap node. Shadow attenuation contains data about external shadows (ranging from 0 - 1). There 0 means full shadow and 1 means no shadow (or vice verse, do not remember).
    Look at my post with the screenshot above.
     
    decept likes this.
  32. decept

    decept

    Joined:
    Sep 16, 2013
    Posts:
    9
    Here is my scene now. And there's no drop shadows. And also when directional light is strait top down everything becomes black. Sorry I don't have much experience in programming, what am I missing?

    upload_2020-5-14_12-54-22.png
     
  33. TextusGames

    TextusGames

    Joined:
    Dec 8, 2016
    Posts:
    429
    You are missing object that casts shadows. Make some big object and put it before objects you want to get shadows on (move big object around and change direction of directional right until you get the shadows on small objects). The position of direction light does not matter. Only rotation describes the direction of the light.
    Or make some plane and put it beneath small objects. Make shure the plain also has same material applied. And shadows should appear on that plain.
     
  34. decept

    decept

    Joined:
    Sep 16, 2013
    Posts:
    9
    The fact is that both spheres are aligned, so the far sphere should get a shadow from the first sphere. Besides, the gray rectangle in the background is a large square positioned right after the spheres and has the same material.
     
    TextusGames likes this.
  35. TextusGames

    TextusGames

    Joined:
    Dec 8, 2016
    Posts:
    429
    If your direction light shadows are enabled and shadows are enabled in general (do default materials receive shadows?). And if your used thiscode.
    Code (CSharp):
    1. void MainLight_half(float3 WorldPos, out half3 Direction, out half3 Color, out half DistanceAtten, out half ShadowAtten)
    2. {
    3.     #if SHADERGRAPH_PREVIEW
    4.         Direction = half3(0.5, 0.5, 0);
    5.         Color = 1;
    6.         DistanceAtten = 1;
    7.         ShadowAtten = 1;
    8.     #else
    9.         #if SHADOWS_SCREEN
    10.             half4 clipPos = TransformWorldToHClip(WorldPos);
    11.             half4 shadowCoord = ComputeScreenPos(clipPos);
    12.         #else
    13.             half4 shadowCoord = TransformWorldToShadowCoord(WorldPos);
    14.         #endif
    15.             Light mainLight = GetMainLight(shadowCoord);
    16.             Direction = mainLight.direction;
    17.             Color = mainLight.color;
    18.             DistanceAtten = mainLight.distanceAttenuation;
    19.         #if !defined(_MAIN_LIGHT_SHADOWS) || defined(_RECEIVE_SHADOWS_OFF)
    20.             ShadowAtten = 1.0h;
    21.         #endif
    22.         #if SHADOWS_SCREEN
    23.             ShadowAtten = SampleScreenSpaceShadowmap(shadowCoord);
    24.         #else
    25.             ShadowSamplingData shadowSamplingData = GetMainLightShadowSamplingData();
    26.             half shadowStrength = GetMainLightShadowStrength();
    27.             ShadowAtten = SampleShadowmap(shadowCoord, TEXTURE2D_ARGS(_MainLightShadowmapTexture,
    28.             sampler_MainLightShadowmapTexture),
    29.             shadowSamplingData, shadowStrength, false);
    30.         #endif
    31.     #endif
    32. }
    than I do not know why there is no shadows.

    ALso as I remember parameter names should be exactly named in code and in custom function node.
     
    decept likes this.
  36. decept

    decept

    Joined:
    Sep 16, 2013
    Posts:
    9
    If I rename the function everything becomes pink. Function working only with MainLight name. As you can see on the screenshot shadows are enabled. Objects cast shadows on URP lit mtl, but not on each other. Also I get an error invalid subscript 'shadow Coord', when the _MAIN_LIGHT_SHADOWS keyword added.
    upload_2020-5-15_14-56-24.png
     
    TextusGames likes this.
  37. TextusGames

    TextusGames

    Joined:
    Dec 8, 2016
    Posts:
    429
    Your custom function node has one input parameter. That parameter must be Vector3 and named "WordPos".
    You vurrently have it as Vector1 and named New.
     
    decept likes this.
  38. decept

    decept

    Joined:
    Sep 16, 2013
    Posts:
    9
    Uff...How could i miss that..Of course position should be vector3. Thanks a lot! It finally works! I'm sorry to bother you, but can there be a way to control the color of the shadows? =)
     
    TextusGames likes this.
  39. TextusGames

    TextusGames

    Joined:
    Dec 8, 2016
    Posts:
    429
    Yes.
    Basically you need the shadow attenuation and possibly self shadows as a mask.
    Then you use this mask to control which color will it be. For example you can just use a lerp node. You put that mask in the alpha of the lerp node and 2 other nodes will be shadowed and lighted colors. (just put wire that you connected to Unlit color node into new lerp alpha node). (also after you make dot product of light and normal you need to add saturate node becouse dot product is in rang of -1 to 1 and you most likely need it to be in range of 0 to 1.

    That way color of shadow is controlod by material of object itself. But,
    You can also mix your desired shadow color with environmental color in order for environment to affect shadow color.
     
    decept likes this.
  40. decept

    decept

    Joined:
    Sep 16, 2013
    Posts:
    9
    Awesome! Thank you! I'll download your app and click on ads =)
     
    TextusGames likes this.
  41. TextusGames

    TextusGames

    Joined:
    Dec 8, 2016
    Posts:
    429
    )))) Enjoy ))))
     
  42. Mrgame545

    Mrgame545

    Joined:
    Mar 7, 2019
    Posts:
    2
    @TextusGames Hi I'm using Unity 2019.1.f1 and HDRP. I try to follow your advice but I can't. I have one last error who I don't arrive debug "TransformWorldToShadowCoord". If you can help me, you will save me.
     

    Attached Files:

    • bug.png
      bug.png
      File size:
      16.7 KB
      Views:
      482
  43. TextusGames

    TextusGames

    Joined:
    Dec 8, 2016
    Posts:
    429
    Most likely in HDRP or in unity prior to 2019.3 you need the different code to access shadow data ("TransformWorldToShadowCoordinate" this function is not exists in HDRP engine code it seems..)
    My advice is to google for a code that can return shadow data in HDRP specifically and create a custom node with that code.
     
  44. Radagasd

    Radagasd

    Joined:
    Mar 1, 2015
    Posts:
    6
    Hey,

    I've implemented these same steps and it works perfectly in the editor, but I've noticed that once I actually build it the shader does not receive/use the shadowcascade information. Below I've added some images of the shader, and the scene in playmode and a build version. This is in Unity 2019.4.0f1. Has anyone had the same problems? And did you manage to fix it?
    EDIT: I made a separate post with a clearer description of my problem: https://forum.unity.com/threads/lig...r-playmode-but-not-in-a-build-version.915560/

    Shader (the custom function is the one linked in this thread)
    CascadesShader.png Playmode (It works as intended, the custom material at the bottom receives shadows and their resolution differs with distance from the camera. The last one does not receive a shadow because it is beyond the shadow distance)
    Cascades_EditorPlaymode.png
    The Build version (the custom material no longer receives shadow information ???, notice that the standard URP Lit shader does receive shadow information on the plane)
    Cascades_Build.png
    Build version with shadow cascades turned off (Now it does receive shadows but they are not cascaded, nor are they soft, while soft shadows is enabled)
    Cascades_BuildOff.png
     
    Last edited: Jun 23, 2020
  45. SniperED007

    SniperED007

    Joined:
    Sep 29, 2013
    Posts:
    345
    The Distance Attenuation in a stand-alone build does not appear to work (yet works fine in the editor).

    Anybody else seeing this issue?

    Tested in:
    Unity 2019.4.2f1
    URP - 7.4.1
     
  46. Sutanq

    Sutanq

    Joined:
    Dec 23, 2018
    Posts:
    1

    I have same problem. And decide to use Two or Four Cascades mandatory.
    Because I don't use No Cascades.

    So i can change my custom function code from
    half4 shadowCoord = TransformWorldToShadowCoord(WorldPos);
    to
    half cascadeIndex = ComputeCascadeIndex(WorldPos);
    half4 shadowCoord = mul(_MainLightWorldToShadow[cascadeIndex], float4(WorldPos, 1.0));


    It didn't need to check _MAIN_LIGHT_SHADOWS_CASCADES keywords
    So it can run in build version.
     
  47. OneratGames

    OneratGames

    Joined:
    Feb 4, 2018
    Posts:
    3
    This also works for me
     
  48. OneratGames

    OneratGames

    Joined:
    Feb 4, 2018
    Posts:
    3
    This allowed for me to access the other cascades
     
    OneGiantPanda333 likes this.
  49. OneGiantPanda333

    OneGiantPanda333

    Joined:
    Dec 13, 2013
    Posts:
    7
    I have the same issue, and this works for me.
     
  50. TextusGames

    TextusGames

    Joined:
    Dec 8, 2016
    Posts:
    429
    In 2019.4.8f Once again shadow data is not working correctly with cascades. It is greatly reduces shadow disance and produces artifacts. Is there a new way to get shadows?
     
    Ziplock9000 likes this.