Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Bug [Case IN-48089] Lightning is not working when one of the entities is very far away

Discussion in 'Graphics for ECS' started by iamarugin, Jul 17, 2023.

  1. iamarugin

    iamarugin

    Joined:
    Dec 17, 2014
    Posts:
    866
    Basically, if you have two entites with rendering components in the scene, one near the scene origin and one 1e12 units away, lightning completely breaks. If you turn off shadows on the Direction light, then everything is working again. Lightning breaks even if all entites has shadow casting disabled.

    upload_2023-7-17_18-26-5.png
    Scene, when one of the entities is far away

    upload_2023-7-17_18-31-32.png
    Scene, when you bring second object to the scene origin

    Entities.Graphics 1.0.11
    Unity 2022.3.4

    Why I need such distances? I am making a space simulator in the Solar system in real scale. I am using floating origin, so camera is always at the scene origin. But some objects could be such distance away (like Pluto is away from Earth).

    Any temporary workaround will be appreciated while bug is not fixed (like patch for entites.graphics).
     
  2. JussiKnuuttila

    JussiKnuuttila

    Unity Technologies

    Joined:
    Jun 7, 2019
    Posts:
    335
    Does this issue happen only with Entities, or does it also happen with GameObjects?

    In general, the floating point computations used in shaders will suffer from bad floating point precision issues when values as large as 1e12 are being used (one bit of difference at this scale corresponds to over 30000 units of position difference), which could explain why lighting is not working correctly.

    It's also worth trying out whether setting the "receiveShadows" property to false is useful for you in this case.

    In general, if you are rendering at cosmic scales, it might be useful to switch your position unit from meters to kilometers or even larger like a hundred or thousand kilometers, so you could use floating point values that are not as large, while still having the same relative sizes for everything.
     
  3. iamarugin

    iamarugin

    Joined:
    Dec 17, 2014
    Posts:
    866
    It is happening only, when using entities. When using GameObjects in HDRP everything is working as expected (I've checked it on an attached repro project).
    ReceiveShadows option doesn't make any difference.
    Maybe it is related to shadows culling in the hybrid renderer?
     
  4. Fribur

    Fribur

    Joined:
    Jan 5, 2019
    Posts:
    127
    Maybe related to IN-23655 I submitted a while back? Was closed as “won’t fix, not enough users affected”
     
  5. iamarugin

    iamarugin

    Joined:
    Dec 17, 2014
    Posts:
    866
    This is very sad if it is true, considering that everything works as expected in HDRP on GameObjects
     
  6. iamarugin

    iamarugin

    Joined:
    Dec 17, 2014
    Posts:
    866
    Moreover, the shadows starts flickering when something is further than 1e6 units, which is pretty usable distance not only for space simulators.
    So this bug needs to be fixed and I would be greatfull if you will suggest a quick fix for that.
     
  7. iamarugin

    iamarugin

    Joined:
    Dec 17, 2014
    Posts:
    866
    Hey @JussiKnuuttila

    The problem is, that directional shadows stop working as soon as Rendering Entity is just exists at that distance. It doesn't matter if shadow casting is tunred for it on or off, or camera sees this entity or not. Even placing this entity on separate rendering layer and turning it off on the camera doesn't help. Something wrong is definetely happening in the shadow culling.

    At least turning off shadow casting/receiving should exlude it from culling, then I could mark all the distant entities to not contribute to shadow casting and the problem will be solved.
     
    Last edited: Aug 12, 2023
  8. iamarugin

    iamarugin

    Joined:
    Dec 17, 2014
    Posts:
    866
    I saw that bug was confirmed by QA: https://issuetracker.unity3d.com/is...ing-when-one-of-the-entities-is-very-far-away but it still has a status "Under considiration"/

    Currently it is blocking my "Rocket Science" game update release. I can't roll back to Unity 2021 and Entities 0.51 because I've spent almost two months upgrading to stable Entities and it was not trivial task. And besides this bug, everything is working as expected.
    @JussiKnuuttila could you please give me any hint, when this bug may be fixed? Alternatively you can give me a direction where to look, so I could fix it my self in a local copy of Entities.Graphics.
     
  9. iamarugin

    iamarugin

    Joined:
    Dec 17, 2014
    Posts:
    866
    So the bug was closed as "By design". Even though everything is working as expected in plane HDRP. Also everything woked fine in entities v0.51. Could you please at least add this information somewhere in the docs, so developers will not spend 5 years of their life trying to build a space simulators using Unity and ECS.
     
  10. arnaud-carre

    arnaud-carre

    Unity Technologies

    Joined:
    Jun 23, 2016
    Posts:
    97
    Hi!
    Unity transform inspector is displaying a float precision warning around 1e5. Anything above could result in un-defined behavior.

    However I understand your frustration about a far away object breaking shadow of a perfectly fine position object. Let me get back to you next week, I'll try to re-open this ticket with some fix so all "reasonable" range objects won't be broken by far way objects.
     
  11. iamarugin

    iamarugin

    Joined:
    Dec 17, 2014
    Posts:
    866
    @arnaud-carre Thank you very much!
    I just don't get it why lightning breaks, even if I disable shadow casting/receving for an entity and entity is beyond camera far clip plane / view frustum. In this case it should be just culled and don't contribute into lightning systems in any way. I've also checked and can see, that these far away objects are indeed culled by Entity.Graphics system. Also I've created a far away entity outside the subscene and lightning still breaks. And also if I disable directional light shadows, everything works (spot and point lights and shadows are not broken).
    Even far away objects will not be ever supported, it would be great to have an ability to exclude far away entities manually from the lightning/shadow systems.
     
    Last edited: Sep 29, 2023
  12. iamarugin

    iamarugin

    Joined:
    Dec 17, 2014
    Posts:
    866
    So, I've spend a weekend debugging and found that lightning breaks in EntitiesGraphicsSystem.UpdateGlobalAABB function, when m_BatchRendererGroup.SetGlobalBounds called. If I clamp bound extents there to some value (for examaple float3(1e7)), everything works as expected including shadows and distant rendering.

    It is worth noting, that m_BatchRendererGroup.SetGlobalBounds with big value break lightning even if you turn off lightning processing by this BatchRendererGroup (which I think is a bug).

    Hope it will help someone with the same problem.
     
  13. arnaud-carre

    arnaud-carre

    Unity Technologies

    Joined:
    Jun 23, 2016
    Posts:
    97
    Yes as you also discovered the root cause of issue is large bounding box resulting in bad computation of shadow matrices (even gameobject shadows).
    I re-opened the ticket and we have a fix in flight. We also clamp the bounding box. Behavior of very far objects are still undefined as before, but at least it won't break shadow behavior of other objects.
    Backports up to 2022.3 are in flights.
     
  14. iamarugin

    iamarugin

    Joined:
    Dec 17, 2014
    Posts:
    866
    This is a great news! Thank you very much.