Search Unity

Not baking directional lights when switching lightmap data...

Discussion in 'Global Illumination' started by bkeesing, Sep 13, 2017.

  1. bkeesing

    bkeesing

    Joined:
    Apr 10, 2017
    Posts:
    15
    I'm trying to swap out lightmap data to do different weather lighting, but I'm having trouble getting the baked data to persist between scene changes.
    At the moment, I am storing the lightmapColor, lightmapDir and shadowMask in LightmapData, and I'm keeping track of all the Renderers that are affected by the bake (including how it was baked).
    This is the code I've been using:

    Code (CSharp):
    1.  
    2. // This is a serializable version of LightmapData
    3. [System.Serializable]
    4. public class LightmapDataInfo
    5. {
    6.     public Texture2D lightmapColor;
    7.     public Texture2D lightmapDir;
    8.     public Texture2D shadowMask;
    9.  
    10.     public LightmapDataInfo(LightmapData data)
    11.     {
    12.         lightmapColor = data.lightmapColor;
    13.         lightmapDir = data.lightmapDir;
    14.         shadowMask = data.shadowMask;
    15.     }
    16.  
    17.     public LightmapData CreateLightmapData()
    18.     {
    19.         LightmapData data = new LightmapData();
    20.         data.lightmapColor = lightmapColor;
    21.         data.lightmapDir = lightmapDir;
    22.         data.shadowMask = shadowMask;
    23.         return data;
    24.     }
    25. }
    26.  
    27. // This records how the lightmap is applied to an individual renderer
    28. [System.Serializable]
    29. public struct LightmapRendererInfo
    30. {
    31.     public Renderer renderer;
    32.     public int lightmapIndex;
    33.     public Vector4 lightmapOffsetScale;
    34. }
    35.  
    36. // A snapshot of all lightmapping information and data
    37. [System.Serializable]
    38. public class LightmapSnapshot
    39. {
    40.     public LightmapsMode lightmapsMode;
    41.     public LightmapDataInfo[] lightmaps;
    42.     public LightmapRendererInfo[] rendererInfos;
    43.     public LightProbes lightProbes;
    44.  
    45.     // Applies this snapshot to the current lighting settings
    46.     public void ApplyLightmaps()
    47.     {
    48.         if (lightmaps == null || rendererInfos == null || rendererInfos.Length == 0)
    49.             return;
    50.  
    51.         // apply lightmaps
    52.         LightmapData[] lightmapdata = new LightmapData[lightmaps.Length];
    53.         for (int i = 0; i < lightmaps.Length; ++i)
    54.             lightmapdata[i] = lightmaps[i].CreateLightmapData();
    55.  
    56.         // apply renderinfos
    57.         foreach (LightmapRendererInfo info in rendererInfos)
    58.         {
    59.             info.renderer.lightmapIndex = info.lightmapIndex;
    60.             info.renderer.lightmapScaleOffset = info.lightmapOffsetScale;
    61.         }
    62.  
    63.         LightmapSettings.lightmaps = lightmapdata;
    64.         LightmapSettings.lightmapsMode = lightmapsMode;
    65.         LightmapSettings.lightProbes = lightProbes;
    66.     }
    67.  
    68. #if UNITY_EDITOR
    69.     // Retrieves all of the lighting information in the current bake
    70.     public void Bake(bool dobake = true)
    71.     {
    72.         if (UnityEditor.Lightmapping.giWorkflowMode != UnityEditor.Lightmapping.GIWorkflowMode.OnDemand)
    73.         {
    74.             Debug.LogError("ExtractLightmapData requires that you have baked your lightmaps and Auto mode is disabled.");
    75.             return;
    76.         }
    77.  
    78.         // perform bake
    79.         if (dobake)
    80.             UnityEditor.Lightmapping.Bake();
    81.  
    82.         // get lightmaps
    83.         lightmaps = new LightmapDataInfo[LightmapSettings.lightmaps.Length];
    84.         for (int i = 0; i < lightmaps.Length; ++i)
    85.             lightmaps[i] = new LightmapDataInfo(LightmapSettings.lightmaps[i]);
    86.  
    87.         // get other lightmap info
    88.         lightmapsMode = LightmapSettings.lightmapsMode;
    89.         lightProbes = LightmapSettings.lightProbes;
    90.  
    91.         // get renderinfos
    92.         List<LightmapRendererInfo> renderinfolist = new List<LightmapRendererInfo>();
    93.         foreach (MeshRenderer renderer in Object.FindObjectsOfType<MeshRenderer>())
    94.         {
    95.             if (renderer.lightmapIndex == -1)
    96.                 continue;
    97.  
    98.             LightmapRendererInfo info = new LightmapRendererInfo();
    99.             info.renderer = renderer;
    100.             info.lightmapOffsetScale = renderer.lightmapScaleOffset;
    101.             info.lightmapIndex = renderer.lightmapIndex;
    102.             renderinfolist.Add(info);
    103.         }
    104.         rendererInfos = renderinfolist.ToArray();
    105.     }
    106. #endif
    107. }
    108.  
    The interesting this is that it works quite well. The problem is that when you switch scenes in the editor, or close and reopen unity, the bake changes. All spot lights are saved fine, but directional lights that were in the bake don't seem to be there.

    So, has anyone tried this before? Does anyone know if the lightmapping is treating directional lights different?

    Btw, I'm using Unity 5.6.1, baking with Enlighten set to distance shadowmask.

    Cheers.