Search Unity

Issue with shadows

Discussion in 'General Graphics' started by matiasges, Aug 25, 2021.

  1. matiasges

    matiasges

    Joined:
    Jan 24, 2021
    Posts:
    142
    I am using a flashlight in the interior of a dark building, and the problem is that when I modify the "Near Plane" range on the Spot Light of my flashlight so that the light does not pass through the wall, it does not work at all distances where the player is. At a certain distance the Near Plane range value works, but then I move closer and the light goes through the wall again, I adjust it for that position, but when I move backwards the same thing happens. I have also tried setting the bias up and down with no results.
    Here I'm behind a wall but the light goes through:


    It also gives me a weird shadow of the game objects:


    Don't know what else could it be...

    I'm using built-in render pipeline, Unity 2020.3.5.
     
  2. DEEnvironment

    DEEnvironment

    Joined:
    Dec 30, 2018
    Posts:
    437
    check you shadow quality setting for soft shadow / hard shadow and verify that the scene objects have cast shadows on
     
  3. matiasges

    matiasges

    Joined:
    Jan 24, 2021
    Posts:
    142
    Yes it has enabled soft/hard shadows. My objects also have the cast shadows on.

    The problem is that when I adjust the "Near Plane" range in the Spot Light of my flashlight it works and the light doesn't pass through the wall, but when I go closer to the wall it does pass so I have to adjust it again to make it work at that distance. But then again if I move backwards it will pass through the wall.

     
    Last edited: Aug 25, 2021
  4. matiasges

    matiasges

    Joined:
    Jan 24, 2021
    Posts:
    142
    Also the shadows look like they are floating while pointing with the flashlight

     
  5. DEEnvironment

    DEEnvironment

    Joined:
    Dec 30, 2018
    Posts:
    437
    I have to agree the Unity spot light is not very good with shadows
    the shadow is hard regardless of settings

    the only ideai have is to override unity shadows and use a custom shader that does soft shadow casting upload_2021-8-25_17-36-1.png

    example below casted shadow forward pass bias from the unity ball
    its still not perfect to override the default unity shadows but perhaps better that the very hard edges from point lights upload_2021-8-25_17-39-50.png

    also for the flashlight, a good cookie texture may help a lot
     
    matiasges likes this.
  6. matiasges

    matiasges

    Joined:
    Jan 24, 2021
    Posts:
    142
    Right, I'll see what I can do.
    Thanks.

    I added a Point light and the shadows are normal:



    But I just changed the spot light of the flashlight to a point light and it has the same weird shadow effect
     
    Last edited: Aug 26, 2021
  7. matiasges

    matiasges

    Joined:
    Jan 24, 2021
    Posts:
    142
  8. kristijonas_unity

    kristijonas_unity

    Unity Technologies

    Joined:
    Feb 8, 2018
    Posts:
    1,080
    I presume that you are using the built-in render pipeline? Keep in mind that shadow mapping in BiRP has not seen much improvement since the introduction of URP and HDRP.

    What is the problem here, exactly? From the looks of it, you are facing two distinct issues:
    • Spotlight attenuation clipping through walls as you get close
    • Floaty looking shadows when objects are illuminated by spotlights
    As for the first problem, adjusting the Near Plane parameter should do the trick. Play around with different values, and see if that helps.
    Alternatively, you could try the following suggestion by one of our former developers. Keep in mind that the fix applied to an older Unity version. I will copy and paste the entire message below:

    Navigate to the following path: Unity installation directory\Editor\Data\CGIncludes. Find UnityCG.cginc shader in that folder. Inside that shader, find change the whole UnityApplyLinearShadowBias() function with the following:
    Code (CSharp):
    1. float4 UnityApplyLinearShadowBias(float4 clipPos)
    2.  
    3. {
    4.     // For point lights that support depth cube map, the bias is applied in the fragment shader sampling the shadow map.
    5.     // This is because the legacy behaviour for point light shadow map cannot be implemented by offseting the vertex position
    6.     // in the vertex shader generating the shadow map.
    7. #if !(defined(SHADOWS_CUBE) && defined(SHADOWS_CUBE_IN_DEPTH_TEX))
    8.     #if defined(UNITY_REVERSED_Z)
    9.         // We use max/min instead of clamp to ensure proper handling of the rare case
    10.         // where both numerator and denominator are zero and the fraction becomes NaN.
    11.         //clipPos.z += max(-1, min(unity_LightShadowBias.x / clipPos.w, 0));
    12.         clipPos.z += max(-1, min(unity_LightShadowBias.x / max( clipPos.w,1.0 ), 0));
    13.     #else
    14.         clipPos.z += saturate(unity_LightShadowBias.x/max( clipPos.w,1.0 ));
    15.     #endif
    16. #endif
    17.  
    That should fix the shadow leaking problem.

    As for the floaty shadows; chances are that your current cascades setup does not allow for high precision. I would recommend reducing the number of cascades. Experiment with different values until you'd find a right fit.
    Also, I would suggest reducing the Shadow Distance value. High values will "stretch" your cascades resulting in poorer shadow precision.
     
    DEEnvironment and matiasges like this.
  9. isotian

    isotian

    Joined:
    Mar 8, 2015
    Posts:
    6
  10. matiasges

    matiasges

    Joined:
    Jan 24, 2021
    Posts:
    142
    Thanks a lot!
    That solved my issue with the light passing through the walls. Yes I'm using built-in render pipeline but when I was adjusting the near plane it didn't work at all of the distances.

    However I tried a lot of tweaking with the cascade and shadow distance but it gives me the same result when with the floating shadows. Actually when tweaking the cascades I don't see any changes to the shadows.
     
    Last edited: Aug 26, 2021
  11. matiasges

    matiasges

    Joined:
    Jan 24, 2021
    Posts:
    142
    Here is a video to explain it better:

     
  12. DEEnvironment

    DEEnvironment

    Joined:
    Dec 30, 2018
    Posts:
    437
    hello sir
    would it be possible in future to expose this thru a progrma / keyword in future
     
  13. Pixelith

    Pixelith

    Joined:
    Jun 24, 2014
    Posts:
    578
    This is the only thread I've seen talking about floaty shadows.

    Sorry to necro post. I've been searching for what feels like hours now. Anyone have a fix for the floaty shadows? I'm having the EXACT same problem as OP. I don't know how to fix it.
     
  14. c0d3_m0nk3y

    c0d3_m0nk3y

    Joined:
    Oct 21, 2021
    Posts:
    666
    Can't see anything wrong on this picture. If the light was at the camera position, you wouldn't see any shadows because they'd be hidden behind what you see. Since the flash light position is slightly different from the camera position, you can see the shadows but they'll always be near to what you look at. The shadows will look like a silhouette of the shadow casters, like on the picture. You can also see the same effect here at 0:59:


    The only thing that could be improved are
    - Soft shadows instead of hard shadows
    - Maybe add some other light sources, some kind of global illumination or just a brighter ambient color so that they are not entirely black.
     
  15. Pixelith

    Pixelith

    Joined:
    Jun 24, 2014
    Posts:
    578
    Why does it do that though?

    Here's a small video on what is happening. Similar to the previous issues talked about and even what shows in the video posted above.


    But then

    Here is what it looks like in Scene View
    Screenshot 2022-09-28 213425.png

    It looks a little weird when turning around a humanoid model


    Screenshot 2022-09-28 215148.png

    What causes this? I'm just curious if its purely a POV type thing and different angles would show it rendering "correctly" or if this is actually an issue with shadow rendering due to using multiple cameras for a fps?
     

    Attached Files:

    Last edited: Sep 29, 2022
  16. c0d3_m0nk3y

    c0d3_m0nk3y

    Joined:
    Oct 21, 2021
    Posts:
    666
    In the video it looks as if some objects (trees, fence) are only casting shadows but not receiving any (I think, there is a checkbox for that). But hard to tell. Your screenshots look fine to me - especially the last one. This is what I would expect. What would you expect?

    Is it the lack of soft shadows that is bothering you?
     
  17. Pixelith

    Pixelith

    Joined:
    Jun 24, 2014
    Posts:
    578
    It's hard to tell without it in motion.

    And I think that's my issue. its perspective that's messing me up.

    The video is basically whats happening. If I look in scene view, your image is what I get. A normal shadow. If I place a point light outside of the character object (like a lamp or street pole) then shadows appear fine. Especially when moving away from it. Only when I have the light be a child of my player object do they appear as they do in the video.

    I've even moved the same light away from my player object while attached to my character and shadows move accordingly. I've just never paid attention to flashlight shadows.

    I've never been more confused by a subject than shadows.