Search Unity

HDRP shadows changing based on camera position

Discussion in 'High Definition Render Pipeline' started by DGordon, Oct 25, 2020.

  1. DGordon

    DGordon

    Joined:
    Dec 8, 2013
    Posts:
    649
    Hello,

    I am using Unity 2020.1.10f1 HDRP, rendering set to both (deferred/forward), shadow filtering medium. I have a scene with 12 connected terrains at 300x300. When turning Cast Shadows on and a single directional light, the shadows on the terrain actually change depending on where the camera is looking. Moving the camera a tiny bit to the left or right can mean no shadows, to shadows in different places. Changing the shadow cascades also affects this. I have tried bumping the draw distance and shadow distance very high, to very low. Nothing seems to solve this. Additionally, turning Draw Instanced off removes all shadows from the terrain (not that I want to do that, just mentioning I tried and saw that behavior).

    What am I doing wrong? Is this a known bug? I've experienced some weird shadow artifacts in HDRP in previous versions as well (shadows moving or turning on/off) for non-terrain stuff, but in this project, I'm only noticing it at the moment for this.

    Any advice on what this is and how to fix it? I've played with all the settings I know of, and nothing is making this stable.
     
  2. DGordon

    DGordon

    Joined:
    Dec 8, 2013
    Posts:
    649
    Anyone? I'm happy to hear I'm doing something stupid ... but this is baffling me. Shouldn't terrain shadows work in HDRP?
     
  3. pierred_unity

    pierred_unity

    Unity Technologies

    Joined:
    May 25, 2018
    Posts:
    433
    Hello, are your sun shadows set to refresh every frame?

    upload_2020-10-26_17-35-51.png
     
    See_Sharp likes this.
  4. snacktime

    snacktime

    Joined:
    Apr 15, 2013
    Posts:
    3,356
    Are any of the camera coords negative when this happens?

    This sounds a lot like relative to absolute world transform gone wrong, negative coords being one thing I've found that reliably triggers it.
     
  5. foonix

    foonix

    Joined:
    Dec 15, 2019
    Posts:
    20
    I noticed something like this after enabling PCSS shadows recently. Pretty much the same problem when a shadow from a high radius light source to a distant object. Shadows are set to refresh every frame. I think what is happening has to do with using a large shadow area with multiple splits and the angle of the blocker search (which is based on light source radius) involves going outside of the split boundary.

    Try setting the sun light radius to 0 degrees (hobbles PCSS algorithm), or in the HDRP render pipeline asset Lighting->Shadows->Filtering Quality, set it to "Medium" (disables PCSS). If either of those make the problem go away then it's probably to do with PCSS.
     
  6. kite3h

    kite3h

    Joined:
    Aug 27, 2012
    Posts:
    197
    Same , There are many small terrains and instanceDraw activated.
     
  7. DGordon

    DGordon

    Joined:
    Dec 8, 2013
    Posts:
    649
    I'm actually noticing similar behavior now with a point light. Please note for all above comments: I am using deferred and/or both, but not specifically Forward, so I do not have the high quality shadows turned on. This is using medium quality ... nothing fancy to my knowledge, should just work?

    In the attached photo, the only thing changing is the rotation of the camera. Very, very slightly. I mean, a microscopic rotation while the game is playing with my first person controller changes those shadows.

    I don't know if this is the same bug, but its the same consistent behavior I've seen in multiple projects now.

    For reference, the light is the players "torch" casting the shadow in the room. Toggling the shadowmap off on the light looks the same as the bug in the photo.

    I've played with having the near place much closer and further, so don't pay attention to the exact numbers you see.

    [Edit -- Added two more, c and d, so you can see the overhead of the scene while thats happening]
     

    Attached Files:

    Last edited: Oct 29, 2020
  8. DGordon

    DGordon

    Joined:
    Dec 8, 2013
    Posts:
    649
    Finding this happening all over with lights as I pay attention. Again, nothing fancy ... its just a normal point light for this torch.
     

    Attached Files:

  9. DGordon

    DGordon

    Joined:
    Dec 8, 2013
    Posts:
    649
    Okay, maximum shadows on screen is what caused the point light. However, even setting that number to 10000 doesnt change this behavior for the terrain shadows. There's probably another setting I need to change, but no idea what it would be!
     
  10. pierred_unity

    pierred_unity

    Unity Technologies

    Joined:
    May 25, 2018
    Posts:
    433
    Hey, did you try to disable these terrains one by one, trying to identify if specific ones created this issue?
    upload_2020-10-31_0-14-4.png
    Without a test scene, it's difficult to help you. If you could report a bug and attach a small sample project to it, that would be great (in Unity, Help > Report Bug).

    Regarding your other problems with the shadows, it seems you had too many lights rendered in general: try to use the "Light fade distance" to cull the lights in your scene beyond a certain distance from the active camera. This will ensure the engine doesn't waste resources trying to render their shadows when they aren't necessarily on screen. There is a similar option for Shadow fade distances too. It is a way better alternative than brute-forcing the maximum number of lights/shadows rendered at any given time in the HDRP asset.
    upload_2020-10-31_0-17-42.png
     
    DGordon likes this.
  11. ducceschioa

    ducceschioa

    Joined:
    Nov 1, 2020
    Posts:
    9
    I'm having the same exact issue on Unity 2020.1.11f1.
     
  12. DGordon

    DGordon

    Joined:
    Dec 8, 2013
    Posts:
    649
    Thanks!! I didn't notice you replied until now ... very much appreciated. Great tip on the fade distance.

    Quick question along these lines: does occlusion culling have any effect on lights being rendered? Meaning, if I am in room A looking at room B, and room B has a light but is also not rendered because of OC, will that light still be doing anything given that in theory it could be reaching something that isn't culled I guess? My initial tests seem to show it is still on and affecting performance (and assumably the max light threshold), but I could have done something wrong so confirming with you that was expected behavior and not more user error.

    Basically, in a standard dungeon type set up, is fade distance + OC the best way to handle this, understanding that if the fade distance still covers an area which shouldn't be affected by the light (ie: outside of the room with no visibility into it nor of anything affected by it), it will still be active?

    If so, would it be beneficial to create my own project-specific system to know if lights should be turned on / off?

    Thanks!
     
    Last edited: Nov 4, 2020
  13. ducceschioa

    ducceschioa

    Joined:
    Nov 1, 2020
    Posts:
    9
    Hi, I've discovered what triggers this bug (At least in my case), it happens only and exclusively when adding large structures to the scene (or scaling an object up by a lot). I haven't found a solution though. This problem does not occur with small object shadows.
     
  14. ducceschioa

    ducceschioa

    Joined:
    Nov 1, 2020
    Posts:
    9
    Update: I've discovered that using custom Shader Graphs instead of the HDRP/Lit shader for the materials caused the problem. Adding a material with the default shader instantly fixes the issue. I'm going to find out if it's something specific of the shader graph that's causing the bug.
     
  15. ducceschioa

    ducceschioa

    Joined:
    Nov 1, 2020
    Posts:
    9
    FOUND THE SOLUTION. Apparently HDRP doesn't really like using standard shader graphs for large objects. The solution: USE A HDRP SHADER GRAPH. To create one Right Click on the Project window > Shader > HDRP > Lit Shader. And that's pretty much it. Can I get the confirmation that a Unity developer has read this? If I don't I will file a bug report.
     
  16. DGordon

    DGordon

    Joined:
    Dec 8, 2013
    Posts:
    649
    Can you explain with more detail what problem you solved? I listed two issues: one was terrain shadows were having issues, and one was point lights not showing up. This sounds like you are having a completely different issue, except you said "I'm having the same exact issue on Unity 2020.1.11f1.".

    Glad you solved the problem, but would be great to know the exact problem solved :). I had an issue with my entire screen going black when using a model with a custom shader built with ASE (noticed the normals went wonky too) after hitting save when its been working for months, then when I rebuilt basically the same exact shader it stopped doing that. Switching the model to a standard HDRP/Lit shader also made the issue go away. It sounds like _maybe_ you are talking about something similar to that ... but that's a completely different issue than the lighting issues described above.

    If so, you should probably make a new thread for that!
     
  17. pierred_unity

    pierred_unity

    Unity Technologies

    Joined:
    May 25, 2018
    Posts:
    433
    The best would be to file a bug report with a mini repro project. Thanks in advance!
     
  18. Gametyme

    Gametyme

    Joined:
    May 7, 2014
    Posts:
    618
    I figured it out. Distance shadowmask is broken.
     
  19. ducceschioa

    ducceschioa

    Joined:
    Nov 1, 2020
    Posts:
    9
    I fixed the terrain shadows bug. If I moved the camera or rotated it, close shadows would appear and disappear depending on the camera rotation, while the far away shadows always looked ok. This only happened to me with large objects.
     
  20. ducceschioa

    ducceschioa

    Joined:
    Nov 1, 2020
    Posts:
    9
    Thanks, I'll do it right away!
     
    pierred_unity likes this.
  21. unity_fi6nk-TShZd_cA

    unity_fi6nk-TShZd_cA

    Joined:
    Dec 7, 2020
    Posts:
    1
    Whilst editing the scene, in Gamemode the shadow appear fine.

    does anyone have an answer as to how to fix the problem ? (In an easy way I mean...) The shadows look horrible on the base scene of HDRP... Based on where the camera is located, they sometimes completely disappear. It makes it pretty tough to work on a scene via the editor
     

    Attached Files:

  22. pierred_unity

    pierred_unity

    Unity Technologies

    Joined:
    May 25, 2018
    Posts:
    433
    What you are seeing in this image are the "Contact Shadows". You can add a volume override to control it or disable it. And you can control if the directional light can produce such shadows in its inspector.
     
  23. Passeridae

    Passeridae

    Joined:
    Jun 16, 2019
    Posts:
    395
    And are there any plans to add blur to them?:)
    Even without high quality pcss filtering realtime shadows have slightly blurred edges now which look really nice. What doesn't look really nice are these contact shadows which stand out way too much when they are mixed with the regular shadows. This gets even worse if the pcss filtering is applied.
     
  24. pierred_unity

    pierred_unity

    Unity Technologies

    Joined:
    May 25, 2018
    Posts:
    433
    Hey, it won't look grainy as you have, if you use temporal antialiasing.

    You can also increase the number of samples. I highly recommend you look at the new HDRP template to see how it can be tuned and used.

    Contact shadows are great for distant shadows, when the shadow cascades cannot provide a good enough amount of details. Your first cascade should already provide a good enough amount of details without having to rely on the contact shadows. So, your image that seems to only rely on the contact shadow at very short range is not really a good use case for them.
     
  25. Passeridae

    Passeridae

    Joined:
    Jun 16, 2019
    Posts:
    395
    Thanks for the response!
    To illustrate my point here's a picture. It's taken from HDRP documentation and these are actually ray-traced shadows.

    Blue lines highlight real-time shadows with blurred edges. Red lines are for sharp contact shadows. I understand that this picture is about these nice tiny shadows from leaves (and they are great), but it does illustrate incosistency as well. Light is just not supposed to cast shadows with different amount of blurriness. And again, this makes them virtually incompatible with PCSS filtering because you'll get this:

    And there are many situations where you need blurred shadows. And you can't tweak this or blur with TAA somehow. It also doesn't look like a good idea to shut them off at a close distance, because what's the point then? As the screenshot from the docs above suggests they are very good at capturing tiny details that by definition can be explored only at a close distance. A potential workaround would be to allow to switch them off on the per-object basis. A better approach would be to dither them somehow and synchronize with the PCSS filtering of the main shadowmaps.
     
  26. pierred_unity

    pierred_unity

    Unity Technologies

    Joined:
    May 25, 2018
    Posts:
    433
    Hey, contact shadows are indeed perfect for distance shadows, for instance trees in the distance, when you don't want to waste your shadow maps over 2 miles. It's also great when trying to get sharp results at mid range because your second or third cascade isn't as sharp anymore.

    Again, they're not necessarily meant to be used at very short range for the "primary" source of shadows, simply because as soon as the shadow caster will leave the frame, they'll disappear (something less likely when the shadow caster is far away).

    Also, if you use PCSS shadows (High quality filtering), you won't really need contact shadows at short range anyway, because you're already getting great shadowing quality (which will indeed diverge in spread compared to the "straight" contact shadows) if you use a sufficient number of samples, TAA and a high enough resolution. When using the medium filtering quality, the divergence is a lot less obvious, which might be a better fit for close range contact shadows.

    In any case, if you want perfect shadowing quality, you should look into the ray traced shadows. In the meantime, you'll need to find a compromise with the rasterized techniques on offer. ;)
     
    Passeridae likes this.
  27. olavrv

    olavrv

    Joined:
    May 26, 2015
    Posts:
    515
    Is it possible to get terrains to cast contact shadows? this would be fantastic!
     
  28. koirat

    koirat

    Joined:
    Jul 7, 2012
    Posts:
    2,073
    Casting contact shadows on itself ?
     
  29. olavrv

    olavrv

    Joined:
    May 26, 2015
    Posts:
    515
    Yes!
     
  30. pierred_unity

    pierred_unity

    Unity Technologies

    Joined:
    May 25, 2018
    Posts:
    433
    Yes, it already works. Just make sure the "Max Distance" for the contact shadows is several thousand meters, depending on the size of your terrain.

    Very dirty example:
    upload_2022-1-18_16-48-7.png
     

    Attached Files:

  31. olavrv

    olavrv

    Joined:
    May 26, 2015
    Posts:
    515
    Thank you very much for quick response! I am having an issue where contact shadows from terrain to terrain doesn't seem to work outside the range of shadow maps on directional light. It works fine on geometry to geometry. I have had this issue for a long while, it doesn't happen in a clean project - and I have not been able to find the setting in my current (large) project that can be causing this.

    Appreciate any of your input on what can be causing this.