Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Multi scene lightbaking with some scenes being variable

Discussion in 'Global Illumination' started by MiFrilke, Jun 24, 2021.

  1. MiFrilke

    MiFrilke

    Joined:
    Dec 14, 2016
    Posts:
    41
    Intro

    Setup
    We're currently in the process of evaluating our lightbaking options for a new projec, mainly looking into the Progressive GPU Lightmapper.

    We're planning to have the following structure for missions by using a layered multi-scene setup:
    • The location layer consisting of one geometry scene and one logic scene → includes base geometry and logic for the whole level
    • The area layer for that location consisting of multiple sets of geometry and logic scenes → includes small scale geometry and logic that can be swapped out
    • The mission layer has its own specific geometry and logic scenes → includes mission specific geometry and logic
    Multiple missions can share the same location scenes and some or all of that locations area scenes. This provides modularity and reusability and enables designers and artists to work on different parts of our levels simultaneously.

    Scene structure details

    This example shows scaled down representation of our scene structure for demonstration purposes:
    upload_2021-6-24_17-54-17.png
    Loc_geo includes some base geometry and Loc_log is empty here. The Mis_X_geo scenes include the same prefab placed at different positions in the level, the directional light for that mission (one "at dawn" and one during" the day") as well as some emissive materials to better visualize the effects of the light bake. Mis_X_log also includes the camera.

    The Mis_1 example in the hierarchy and a screenshot of the project folder:
    hierarchy_project_view.png

    Issue
    The missions need to be lightmapped in combination with their location and area scenes, so the lightbaking needs to be variable for locations and areas, depending on which mission logic scene is loaded. This is where we immediately run into some issues and are wondering if it is at all possible to achieve what we need to.

    Baking the first mission
    Lightmapping for mission 1 is finished. The bake was started with the Mis_1_log as the active scene and all four scenes used the same "Lighting Settings"-Asset and have the same Environment configurations.
    settings.png

    Project folder after first light bake
    mission1_afterBake.png

    Swap to second mission
    After saving the scenes, the mission scenes are swapped out. After setting Mis_2_log as the new active scene, it seems that Unity automatically tries to "redistribute" the lighting data, which does not work and actually breaks the existing light bake. The lightmaps end up in different folders while Lighting Data-Subassets get deleted and references inside the scenes to the Lighting Data are missing.

    Project structure after Unity's "redistribution" of the first light bake (after setting Mis_2_log as the new active scene):
    mission2_afterBake.png

    Result
    The lightbaking data is in a state where the information and lightmaps are still there but in a "broken" state, with reference between "LightingData"-Assets and scenes not working anymore. Triggering a lightbake for Mis_2 only further does weird things to the lightingdata assets and lightmaps.

    Question(s)
    At this point we're not even sure if the described behavior is intended or a bug that we should report. It is unclear if it would even be possible to achieve our desired result by saving/loading the lightmaps manually, and connecting them on demand as needed.
    If anyone has experience with similar lightbaking setups, we would be happy to hear from you. Maybe we are just missing some obvious settings of workflows that could fix our issues.
     
  2. kristijonas_unity

    kristijonas_unity

    Unity Technologies

    Joined:
    Feb 8, 2018
    Posts:
    1,080
    Which one of the additive loaded scenes contain lights? This could be the main culprit of the issues you are having. I would suggest moving lighting-related game objects to a separate scene, and load that on-demand alongside your mission and game logic scenes.

    Alternatively, if nothing else works, you could open a bug report with a small repro project. We would investigate on our end.
     
  3. MiFrilke

    MiFrilke

    Joined:
    Dec 14, 2016
    Posts:
    41
    Changing in which scenes the lights (and emissive objects) are located did not change anything on this behaviour.

    @kristijonas_unity We have now reported this issue. It has not been veryfied by Unity QA yet, but the case ID is 1347479.
     
    kristijonas_unity likes this.
  4. MiFrilke

    MiFrilke

    Joined:
    Dec 14, 2016
    Posts:
    41
  5. YvainR

    YvainR

    Unity Technologies

    Joined:
    Sep 15, 2021
    Posts:
    3
    The bug has been reproduced we have put it on our roadmap for a partial system rewrite. We can't say when this will be ready for release. But in the meantime here is an explanation of what happens and possible workarounds:
    The currently implemented behaviour in Unity is that when a bake for a multiscene project is done in Unity, the output files of the bake are stored in a folder named after the current active scene (lightmaps and lightingDataAssets files for each scene that are packed into a single file). But before writing the new files, the output files of a previous bake are deleted. Because data is tracked by ID in Unity that deletion will happen even if the files are moved or renamed.
    Where things get confusing is that this cleanup is based on the currently opened scene files. So if these opened scenes have changed inbetweeen bakes only part of the files is going to be deleted. That's the weird behaviour that was reported.

    Workaround 1:
    - You can prevent the deletion of the previous bake output by unassigning the lightingDataAsset before hitting bake. That's in Lighting Window / Baked Lightmaps tab / Hit Del Key on the Lighting Data Asset field.
    - Do that for each scene by changing the active scene and repeating that manipulation
    - Rename the previous bake output folder
    - You can now hit bake and the previous data will be preserved.
    - If a scene is shared between multiple set of scenes that you use you will need to manually reassign the lightingDataAsset to the scene (Lighting Data Asset field in the Lighting Window) whenever you open the other set of scenes.

    Workaround 2:
    If you want to be sure that the files from a bake are left untouched from later bakes, there is actually an easy way to do that: in the project explorer (or your filebrowser) you can simply duplicate the whole bake output folder.
    There is also another way to proceed. The previous baked files deletion (that happens before a bake is written to disk) is based on the lighting data assets that are currently assigned in the various scenes. You can remove these references by hitting del on the lighting data asset field of baked lightmaps tab. (That only modifies the active scene, you need to do the same manipulation for each loaded scene)

    Workaround 3:
    Use a script to automate workaround 2.
    Code (CSharp):
    1. var scenes = SceneManager.GetAllScenes();
    2. for( int i = 0; i < SceneManager.sceneCount; ++i)
    3. {
    4.     var scene = SceneManager.GetSceneAt(i);
    5.     SceneManager.SetActiveScene(scene);
    6.     Lightmapping.lightingDataAsset = null;
    7. }
     
    kristijonas_unity likes this.
  6. Andhil

    Andhil

    Joined:
    Apr 15, 2018
    Posts:
    13
    Hi,

    @YvainR I think the problem is not just related to multiscene.

    1/ If you bake lighting for a scene ALONE (not in additive).
    2/ Then you load another scene and just make it active (with right click=> set active).
    => All lightmap datas are moved from previous folder to new scene folder.
    => The new scene have a new LightingDataAsset where are attached the lightmaps from the previous scene.
    3/ Finally, if you bake the new scene ALONE. You lose all datas from the first scene.

    So the problem is also present when you just bake ONE scene ALONE, not just in additive mode.

    Maybe i'm missing some obvious settings, so please let me know. But lightmapping just seems unusable.

    André
     
    Last edited: May 12, 2022
  7. MiFrilke

    MiFrilke

    Joined:
    Dec 14, 2016
    Posts:
    41
    We did get around to testing this and were kind of successful in setting it up, though it is unbelievably unstable and extremely prone to user error. If doing any of the steps incorrectly or missing one reference or file, everything gets messed up immediately. We might be able alleviate this a bit by adding lots of handling code in our scene loading tools, though i think to reach a level of robustness we'd feel comfortable with will take a lot of effort.

    The real issue though: There is no way to do any of the manual handling of lighting data in the player. So we're basically back to square one where we need to figure out how to do the mapping and assigning of lightmaps manually in the player. There seems to be a possible solution in this thread https://forum.unity.com/threads/solved-changing-lightmap-at-runtime.492830/#post-3260849, but it is several years old and does not handle multi-scene setups with differing scene compositions as far as i'm aware.
    That said, I'm not sure the lightmap data will even be built correctly for the re-used geometry scenes, since the Lightmaps are resolved into the scenes during build time. Our geometry scenes would need several sets of lightmaps resolved into them, but that wont work because only one lightingdata asset can be assigned when triggering the build process.
    Perhaps the reference to the different sets of lightmaps through the mission scenes is enough for the lighting data to be fully resolved into the build?

    As described in the first post of this thread: Our geometry scenes setup looks like this:
    - location1_base_geo
    - location1_area1_geo
    - location1_area2_geo
    - ...

    On top of the geometry, mission scenes are loaded one at a time:
    location1_mission1_logic or location1_mission2_logic or ...

    With this, we can build multiple missions on top of the same geometry. These missions can take place during varying times of day, so the lightbaking would need to be done for each mission. This means lightingdata can only be connected in the mission scenes during build, but the sub-assets for the geometry scenes can not. Which leads me to believe the data will not arrive in the build correctly, though we did not yet test this.
     
  8. kloogens

    kloogens

    Joined:
    Jul 1, 2015
    Posts:
    109
    It is March 2023 is this problem fixed yet? I am having all kinds of strange light mapping issues in 2022.1.10f1.

    I am having a very difficult time moving forward with all the problems.
     
  9. MiFrilke

    MiFrilke

    Joined:
    Dec 14, 2016
    Posts:
    41
    We have did not see any notable improvements in Unity's handling for multi-scene lightbaking. In the bugreport I wrote back in 2021, the end result was the following, linking to the post above by YvainR:

    https://issuetracker.unity3d.com/is...cene-setups-with-different-scene-combinations
    Our evaluation of the mentioned workarounds are also posted above. None of them were satisfactory.

    In the end we did build a system that uses Unity's GPU lightbaking but afterwards handles the saving of the lightmap data completely custom (based on the github project I linked above).