Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Shadows being received by non-shadow collectors that are z-test occluded by a shadow collector.

Discussion in 'Editor & General Support' started by AlkisFortuneFish, Jun 3, 2014.

  1. AlkisFortuneFish

    AlkisFortuneFish

    Joined:
    Apr 26, 2013
    Posts:
    964
    Massive update in post 10.

    Hey all,

    we've been having a bit of a problem with shadows and I've been wondering if anyone has had anything like this happen before.

    Let's assume you have a project with:
    • A ground plane that z-writes but that does not accept shadows at Geometry-1 (actually several flat geometries in the game itself).
    • A global shadow collector plane that does not z-write and accepts shadows at Geometry+1, just above the ground. This uses a stripped down surface shader with a custom lighting function that does nothing but add the shadow.
    • Several opaque objects above and below the ground, all of which z-write, some of which are shadow casters and none of which are shadow receivers. The underground objects can be pulled through the ground and cannot be occlusion culled by the ground due to reasons beyond the scope of this bug.
    When the objects are under the shadow collector plane, they always seem to be receiving and rendering shadows, regardless of whether "Receive Shadows" is enabled on their renderer and whether the objects themselves are actually z-tested away.

    With the ground on:
    Example1-GroundPlaneOn.PNG

    With the ground off:
    Example2-GroundPlaneOff.PNG

    These objects also end up self-shadowing, as can be seen here:
    Example3-SideView-SelfShadowing.PNG

    If the shadow collector shader's addshadow surface pragma is removed, the shadow plane does not render shadows but the objects do, as can be seen here:
    Example4-NoAddShadowInShader.PNG

    If the objects are not under the shadow collector plane, these odd shadows do not appear. See below for a screenshow, I cannot attach another one to this post.

    Even more oddly, altering *anything* in the project, such as saving the scene, recompiling any shader or changing the drawing order of any object in the debug inspector can toggle this. If the problem does not appear, pressing control-s a few times makes it appear again and vice-versa.

    I have submitted this as bug 611333. I haven't had a response yet but I am wondering if anyone else has seen anything like it before.

    I have attached the example project I used to create these screenshots that I have also sent in with my bug report.
     

    Attached Files:

    Last edited: Jul 15, 2014
  2. AlkisFortuneFish

    AlkisFortuneFish

    Joined:
    Apr 26, 2013
    Posts:
    964
    I don't generally like bumping threads but I still haven't heard back from Unity and I'm wondering if *anyone* has seen anything similar before!
     
  3. AlkisFortuneFish

    AlkisFortuneFish

    Joined:
    Apr 26, 2013
    Posts:
    964
    Just to add to this, these shadows only render if the objects are under the shadow plane. I have updated the post to illustrate this.
    Example5-ObjectPokingThrough.PNG
     
  4. AlkisFortuneFish

    AlkisFortuneFish

    Joined:
    Apr 26, 2013
    Posts:
    964
    Still haven't heard back from Unity for my bug report... This is getting really frustrating as even a negative response, that there's no bug and I'm doing something wrong, would get us going into working around it. As it is, we have a problem, no response from Unity, no ideas by anyone on the forum, no ideas by anyone on answers... :-/
     
  5. AlkisFortuneFish

    AlkisFortuneFish

    Joined:
    Apr 26, 2013
    Posts:
    964
    Bump! I'm now completely running out of ideas as to what could be causing this, it's been 25 days since the bug was reported and we have not had any communication from Unity at all, neither confirming nor denying this.
     
  6. AlkisFortuneFish

    AlkisFortuneFish

    Joined:
    Apr 26, 2013
    Posts:
    964
    Bump! Still no response to bug report 611333, it's been 36 days... And we have some other bug report which hasn't seen a response in several months, both of which referring to things that still happen in the current version of Unity.
     
  7. shaderbytes

    shaderbytes

    Joined:
    Nov 11, 2010
    Posts:
    900
    Hi AlkisFortuneFish

    I downloaded you example to have a look,

    When opening I saw the bug as you have posted,

    And now for some twilight zone stuff .. stay tuned,

    I then opened the shader ShadowCollector to see whats going on in there,

    I first quickly tested out changing the tags : ,

    First I changed Queue to "Overlay" and RenderType"="transparent" ,

    compiled and bug gone but wait there are no shadows on the plane..so ok,

    go back and change only rendertype tag back to opague ,

    complied and still no shadows on plane.. so ok,

    Go back to shader and use controlZ to undo the changes I made and start again with some other tests..

    compiled just to see we are back at the beginning , but wait a minute.. now its working as it should!!!! ???

    So just in case you are not following , the shader is now back in the exact first state the project opened with.. and the bug is gone and the plane is receiving shadows.

    ( other than Mono develop complaining about converting line endings , but that has nothing to do with it Im very sure as I get that time and time again with many other files )

    freaky!
     
  8. AlkisFortuneFish

    AlkisFortuneFish

    Joined:
    Apr 26, 2013
    Posts:
    964
    Yep, it does that with practically any change to anything. If you spam control-s to save the scene or if you open the debug inspector up and just flick between two queue position values, it will toggle the problem on and off. It's really odd!

    It makes this a nightmare to debug, as you change something and think that it fixed it, only for it to come back shortly afterwards.
     
  9. AlkisFortuneFish

    AlkisFortuneFish

    Joined:
    Apr 26, 2013
    Posts:
    964
    I can confirm that this is still happening in 4.5.2. Bumped the bug report as well.
     
  10. AlkisFortuneFish

    AlkisFortuneFish

    Joined:
    Apr 26, 2013
    Posts:
    964
    Edit: Unity have now confirmed this as a bug. No estimates about fixing it.

    While trying to find what on earth is going on, the fact that the shadow map seemed to contain shadow information for those objects led me to check what on earth is being rendered in the profiler.

    This revealed something quite disturbing in performance terms. As I said above, one of the three spheres is a shadow caster and none of the three spheres are shadow collectors. In the profiler, I see:

    • RenderForwardOpaque.Render()->Shadows.RenderShadowmap() -> ... -> Mesh.DrawVBO(): 1 call, rendering my shadow caster sphere, as expected.
    • RenderForwardOpaque.Render()->RenderForwardOpaque.CollectShadows() -> Mesh.DrawVBO() :1 call, rendering my shadow collector plane to collect shadows, as expected.
    • RenderForwardOpaque.Render()->RenderForwardOpaque.CollectShadows() -> Mesh.DrawVBO(): 3 calls, rendering the three spheres, to collect shadows, none of which are shadow collectors.
    • RenderForwardOpaque.Render()->DrawVBO(): 5 calls, rendering my objects, as expected.

    In this example, this is 10 draw calls instead of the expected 7, to draw 5 objects and have only one of them cast a shadow on a single other one of them.

    As a further check, I put a further 8 non-collecting spheres in the scene, which brings the object tally and expected draw call count with one directional light to:
    • 1 Shadow Caster Sphere (expecting 2 draw calls, one to cast and one to render)
    • 1 Shadow Collector Plane (expected 2 draw calls, one to collect and one to render)
    • 10 Spheres (expecting 10 draw calls, without batching)
    • 1 Ground Plane (expecting 1 draw call).

    These 13 objects should bring us to 15 draw calls, but they produce 26 draw calls instead.

    So this brings us to a an issue with exactly the same title as the original bug but without the visible artifacts...

    I have updated my bug report on the issue with all this extra information.

    Edit: Now that I realised how Unity samples shadows, using an extra pass to collect shadows from the cascades onto the screen space buffer, it makes sense that there is this number of draw calls, it just shows what we already knew, that Unity somehow decides to sample the shadow map on those objects even though it shouldn't.
     
    Last edited: Sep 5, 2014
  11. Shutter

    Shutter

    Joined:
    Feb 6, 2016
    Posts:
    22
    Hey there, although your posts are almost 2 years old, I wonder if you had any luck in dealing with these problems. I myself have been finding something similar lately. In my case, I have thousands of objects leading to performance problems in the RenderForwardOpaque.CollectShadows step no matter if such objects are set not cast nor receive shadows.

    I would love to hear more of your experience on that.
     
  12. AlkisFortuneFish

    AlkisFortuneFish

    Joined:
    Apr 26, 2013
    Posts:
    964
    Hi Shutter,

    Unfortunately, I never got to the bottom of it, my repro stopped reproducing the issue on Unity 5, where the shadow rendering got rewritten.

    On Unity 4, we actually had to switch to a different method of faking shadows.

    Sorry!