Search Unity

Losing Lightmap Data upon Playmode or scene reload

Discussion in 'Global Illumination' started by Zylex, Oct 22, 2021.

  1. Zylex

    Zylex

    Joined:
    Nov 25, 2008
    Posts:
    238
    I am not sure when or how this started happening but I am having the trouble that when I bake a map everything looks but fine when going in to playmode I seem to lose all lightmap data and the Uvs become broken. You can see it happening here:

    https://i.gyazo.com/30092de40f469ed1edc5b9b397a46bc6.gif

    Is anyone familiar with this issue and has any clue what might be causing this?

    This was repro-ed on 2020.3.16f1 and 2022.1.0a12.
     
  2. Pema-Malling

    Pema-Malling

    Unity Technologies

    Joined:
    Jul 3, 2020
    Posts:
    327
    Hmm.. I've seen something similar but not quite the same before. Does anything out of the ordinary happen when saving and reloading the scene, or when duplicating it and opening the duplicate? Either way, I suggest filing a bug report, this is definitely wrong behaviour.
     
  3. kristijonas_unity

    kristijonas_unity

    Unity Technologies

    Joined:
    Feb 8, 2018
    Posts:
    1,080
  4. Zylex

    Zylex

    Joined:
    Nov 25, 2008
    Posts:
    238
    This still happens in 2022.1.0a13. Will indeed then have to submit a bug report.
     
    kristijonas_unity likes this.
  5. radiatoryang

    radiatoryang

    Joined:
    Jul 7, 2014
    Posts:
    19
    This happens to me on 2021.2.f01 Windows built-in 3D pipeline deferred with distance shadowmasking and progressive GPU lightmapper (and it happened to me on 2021.2.0b16 too)

    it's like, suddenly, all the meshes mix up their lightmap coordinates, and start sampling from the wrong places on the lightmap atlas. Generally it will stay stable for like 3-4 plays after bake, and if I build out while it's uncorrupted then the build is OK... but if I trigger too many domain reloads or scene reloads or something, that's when all the lightmaps get swapped around and I have to rebake everything to fix it. Very strange.

    (I probably won't be making a bug report / repro project for this though, it would take too long to try to isolate this in my huge project folder, sorry, but I'm just posting this in hopes that someone else can chime in with a similar experience)
     
  6. kristijonas_unity

    kristijonas_unity

    Unity Technologies

    Joined:
    Feb 8, 2018
    Posts:
    1,080
    Have you submitted a bug report? If you've done so already, you can post the bug ID here so that we could take a look ourselves. Otherwise, our customer QA will pick it up eventually.
     
  7. Zylex

    Zylex

    Joined:
    Nov 25, 2008
    Posts:
    238
    The project I am working in is quite big to submit and I can not consistently repro the issue to make a smaller project. However my current hunch is that it is related by triggering a bake via script instead of pressing bake in the bake window. Could still be something completely different though. I will have another look at how to best provide you with a bug report.
     
    kristijonas_unity likes this.
  8. Zylex

    Zylex

    Joined:
    Nov 25, 2008
    Posts:
    238
    Just to provide a bit more information on what happens:

    So when I press play or reload a scene(after a save?) it will corrupt all the lightmap data. Some objects get removed from the map and some strange new objects get added. You can see here that the UV is all over the place:

    https://i.gyazo.com/447e1f82fd63234443040213c7b971a7.png

    What is interesting however is that somehow an object that is marked as Light Probe only gets added to the lightmap UV, see the image for the selected object.
     
  9. Zylex

    Zylex

    Joined:
    Nov 25, 2008
    Posts:
    238
    So I made some more progress on tracking down the issue. It seems that is caused by running a DestroyImmediate on objects after a bake.

    For terrain trees some trees do not get taken in account when baking so therefore we place these trees on the terrain before baking. We then run a bake and when the bake is done we remove the spawned trees. The code looks like this:

    Code (CSharp):
    1.         List<GameObject> treesAsMeshes = PlaceTreesAsMeshesOnTerrain();
    2.  
    3.         Lightmapping.Bake();
    4.  
    5.         foreach (GameObject tree in treesAsMeshes)
    6.             DestroyImmediate(tree);
    When I remove the DestroyImmediate part of the code the issue is no longer happening. I rewrote the last part to a more stable iteration to make sure that wasn't the issue but that did not fix it either.

    Code (CSharp):
    1.         for (int i = treesAsMeshes.Count - 1; i >= 0; i--)
    2.             DestroyImmediate(treesAsMeshes[i]);
     
    Last edited: Nov 12, 2021
  10. Zylex

    Zylex

    Joined:
    Nov 25, 2008
    Posts:
    238
    I tried adding a coroutine so I can frame delay the destruction of the trees but it did not help against the issue. However I did found a workaround and that is by saving the scene before doing the Destroy calls.

    Code (CSharp):
    1.  
    2. List<GameObject> treesAsMeshes = PlaceTreesAsMeshesOnTerrain();
    3.  
    4. Lightmapping.Bake();
    5.  
    6. EditorSceneManager.SaveScene(EditorSceneManager.GetActiveScene());
    7.  
    8. foreach (GameObject tree in treesAsMeshes)
    9.      DestroyImmediate(tree);
    This no longer gives me the issues.
     
    Pema-Malling likes this.
  11. Zylex

    Zylex

    Joined:
    Nov 25, 2008
    Posts:
    238
    I spoke too soon, that did not fix it. It repro-ed it many times and it seemd like it did fix it but now when restarting the scene everything is gone again. I am unsure what is causing it right now.
     
  12. Pema-Malling

    Pema-Malling

    Unity Technologies

    Joined:
    Jul 3, 2020
    Posts:
    327
    A fix which I suspect may be related to your issue just landed very recently. It should available in 2022.1a15, when that becomes available for download. I suggest trying it out at that point.
     
  13. Pema-Malling

    Pema-Malling

    Unity Technologies

    Joined:
    Jul 3, 2020
    Posts:
    327
  14. KarlKarl2000

    KarlKarl2000

    Joined:
    Jan 25, 2016
    Posts:
    606
    @kristijonas_unity
    @Pema-Malling

    I have the unfortunate problem now too. The light map gets mysteriously destroyed once entering playmode. This happens with Unity's Progressive CPU lightmapper as well as the Bakery tool from the asset store.

    I sent a package in for you guys to check out. This happens on 2021.1.28f1 as well as 2021.3.15f1
    Case: IN-29136
    ezgif-5-1aef5dada3.gif

    Hope there's a simple fix to this.
    Thanks for the help
     
    Last edited: Jan 19, 2023
    AldeRoberge likes this.
  15. Dev_Terragame

    Dev_Terragame

    Joined:
    Jan 2, 2018
    Posts:
    8
    We had the same issue. Try clearing your Bakery data (Bakery/Utilities/Clear baked data) and rebake using Unity lightmapper.
     
  16. KarlKarl2000

    KarlKarl2000

    Joined:
    Jan 25, 2016
    Posts:
    606
    I'll give it a shot. Thanks !
     
  17. Jakub_Machowski

    Jakub_Machowski

    Joined:
    Mar 19, 2013
    Posts:
    647
    The same happend on Realtime GI on Unity 2021.3.22
     
  18. Bethsheba_Ashe

    Bethsheba_Ashe

    Joined:
    Feb 11, 2022
    Posts:
    15
    I have the same issue on Unity 2021.3.28f1. I'm using the same lightmap settings with multiple scenes (and streaming them with WorldStreamer). I've used a script to bake the scenes:


    using UnityEngine;
    using UnityEditor;

    [ExecuteInEditMode]
    public class LightmapBaker : EditorWindow
    {
    // Reference to the selected Lighting Settings
    private LightingSettings selectedLightingSettings;

    [MenuItem("MyMenu/Batch Bake Lightmaps")]
    static void BatchBakeLightmaps()
    {
    LightmapBaker window = GetWindow<LightmapBaker>("Batch Bake Lightmaps");
    window.Show();
    }

    private void OnGUI()
    {
    // Get all the Lighting Settings assets available in the project
    LightingSettings[] lightingSettingsAssets = GetAllLightingSettings();

    // Display the drop-down box for selecting Lighting Settings
    selectedLightingSettings = (LightingSettings)EditorGUILayout.ObjectField("Select Lighting Settings", selectedLightingSettings, typeof(LightingSettings), false);

    if (GUILayout.Button("Bake Lightmaps"))
    {
    if (selectedLightingSettings != null)
    {
    // Set the selected Lighting Settings before baking
    UnityEditor.Lightmapping.lightingSettings = selectedLightingSettings;

    // Get the scenes included in the build settings
    EditorBuildSettingsScene[] scenesInBuild = EditorBuildSettings.scenes;

    // Collect scene paths to be baked
    string[] scenesToBake = new string[scenesInBuild.Length];
    for (int i = 0; i < scenesInBuild.Length; i++)
    {
    scenesToBake = scenesInBuild.path;
    }

    // Start the lightmap baking process for scenes in the build
    UnityEditor.Lightmapping.BakeMultipleScenes(scenesToBake);
    }
    else
    {
    Debug.LogError("Please select a Lighting Settings asset before baking.");
    }
    }
    }

    // Helper method to get all Lighting Settings in the project
    private LightingSettings[] GetAllLightingSettings()
    {
    string[] guids = AssetDatabase.FindAssets("t:LightingSettings");
    LightingSettings[] settings = new LightingSettings[guids.Length];
    for (int i = 0; i < guids.Length; i++)
    {
    string path = AssetDatabase.GUIDToAssetPath(guids);
    settings = AssetDatabase.LoadAssetAtPath<LightingSettings>(path);
    }
    return settings;
    }
    }

    The script seems to work great, but after all the lightmaps were baked, when the Gamplay scene was reloaded it lost all the lightmaps, although I've a huge GI cache now. This is the second time its happened on a huge project, so I'm rather miffed to say the least. Going to make a build now, because I'm curious if it will remember them on build since UnityEditor.Lightmapping.BakeMultipleScenes should make an array for all the scenes, according to the docs.

    Note - my GI folder is outside of the Assets and project folder, as I believe this is best practice with both GI and the Build folder. Was I wrong?