Search Unity

[Solved] Changing lightmap at runtime?

Discussion in 'Editor & General Support' started by CloudyVR, Sep 3, 2017.

  1. CloudyVR

    CloudyVR

    Joined:
    Mar 26, 2017
    Posts:
    715
    I have baked two separate lightmaps and stored all data in separate folders.

    These two folders each consist of:

    LightingData
    Lightmap-0_comp_dir
    Lightmap-0_comp_light
    ReflectionProbe-0


    What do I do now in order to switch between the two?

    I have searched the internet and found "Lightmap Manager 2" on the asset store which was conveniently removed and no longer exists...

    How is this done?

    Unity 2017.1 - windows 7
     
    koizin, ph3rin and AntonioModer like this.
  2. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,799
    FM-Productions and CloudyVR like this.
  3. CloudyVR

    CloudyVR

    Joined:
    Mar 26, 2017
    Posts:
    715
    Thanks, that was actually very helpful learning about the lightmap arrays and data struct.

    !!!!This example is flawed, please see next post.!!!!

    Turns out what I was trying to accomplish was very elementary stuff:

    • Just bake your lightmap and rename the folder (containing the lightmap) to something unique.
    • Adjust your scene's lighting scenario a little and again bake then rename folder.
      • Now you should have two distinct folders each with separate lightmap data (both paths should have unique names that are not default so Unity doesn't overwrite lightmap data during next bake - unless that's what you prefer).
    • Then create a new gameobject in the scene and attach this script:
    Code (csharp):
    1. using UnityEngine;
    2.  
    3. public class ChangeLightmap : MonoBehaviour {
    4.  
    5.     [SerializeField] public Texture2D _dir; // The directional lights texture.
    6.     [SerializeField] public Texture2D _light; // The color lightmap texture.
    7.     [SerializeField] public Texture2D _shadow; // The shadow mask texture.
    8.  
    9.     public void Load () {
    10.         LightmapData[] lightmaparray = LightmapSettings.lightmaps;
    11.         LightmapData mapdata = new LightmapData();
    12.         for (var i=0; i < lightmaparray.Length; i++) {
    13.  
    14.             mapdata.lightmapDir = _dir;
    15.             mapdata.lightmapColor = _light;
    16.             mapdata.shadowMask = _shadow;
    17.  
    18.             lightmaparray[i] = mapdata;
    19.         }
    20.         LightmapSettings.lightmaps = lightmaparray;
    21.     }
    22. }
    • Finally set the color and direction textures in the _color, _dir and _shadow fields then invoke Load().
      • My textures were named: Lightmap-0_comp_dir | Lightmap-0_comp_light | Lightmap-0_comp_shadowmask

    Optional editor script that presents a Load button in the inspector:

    Code (csharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3.  
    4. [CustomEditor(typeof(ChangeLightmap))]
    5. public class ChangeLightmapInspector : Editor
    6. {
    7.     public override void OnInspectorGUI()
    8.     {
    9.         base.OnInspectorGUI ();
    10.  
    11.         ChangeLightmap lightmapData = (ChangeLightmap)target;
    12.  
    13.         if (GUILayout.Button("Load"))
    14.         {
    15.             lightmapData.Load ();
    16.         }
    17.     }
    18. }
    Note about lightprobes:
    I do not know how the above solution will affect light probes in your scene but it probably won't work very well in most instances. For another solution I would suggest taking a look at the lightmap-switching-tool from Laurenth on GitHub.

    !!!!This example is flawed, please see next post.!!!!
     
    Last edited: Sep 5, 2017
  4. CloudyVR

    CloudyVR

    Joined:
    Mar 26, 2017
    Posts:
    715
    Solved!

    Turns out it wasn't as trivial as I first thought. And the above example is flawed by assuming that all static mesh renderer offsets don't change when baking the various lightmaps - which only works for simple scenes. However in a more complicated scene where enabling/disabling static mesh objects (like lights for "bright" and "dark" lightmaps) occurs then the renderers offsets may change making lightmaps not always line up correctly.

    I have created this solution which works pretty well (based on Laurenth's example) and should even transfer lightprobes! However it differs from hers by taking the currently loaded lightmap and copies its textures to a specified Resources folder. It also creates a .Json file in that folder which contains all of the renderer offsets and lightprobe coefficients, as well as references to the textures. The data in the .Json file is used to correctly align the lightmap to the corresponding mesh renderers - all on demand!

    There is no limit to the number of lightmaps you can create for a single scene. But I recommend using a good naming convention for the Resources folder names.

    NOTE: Static Batching - I found that if I enable Project Settings>Player>Static Batching then the lightmap takes on different offsets at runtime causing misalignment. I have disabled static batching in my project, If I figure out how to compensate for the batch offsets I will update the example with a fix.

    To use this tool:
    • Place script somewhere in Assets and place the editor script in the Editor folder.
    • Attach ChangeLightmap script to an empty gameobject in the scene.
    • Add and bake some lightning in the scene (daytime lighting).
    • Change the "resourceFolder" directory name field to something unique (eg. LightMapData_Bright).
    • Press "Save" button to create a Resources folder and all the data files.
    • Adjust your lights a little and bake scene lightning again (nighttime lighting).
    • Change "resourceFolder" name field to another unique directory name(eg. LightMapData_Dark).
    • Press "Save" button again to create the second folder and lightmap data files.
    • Finally you may type any of those unique directory names in the name field then press "Load" button to switch between lightmaps!!
    Lightmap Loader/Saver Tool:

    Code (csharp):
    1. using UnityEditor;
    2. using UnityEngine;
    3. using UnityEngine.Rendering;
    4. using System;
    5. using System.IO;
    6. using System.Collections.Generic;
    7.  
    8. public class ChangeLightmap : MonoBehaviour
    9. {
    10.     private string jsonFileName = "mapconfig.txt"; // Name of the json data file.
    11.     [SerializeField] private string m_resourceFolder = "LightMapData_1";
    12.     public string resourceFolder {get { return m_resourceFolder; }}
    13.  
    14.     private string absoluteName;
    15.  
    16.     [System.Serializable]
    17.     private class SphericalHarmonics
    18.     {
    19.         public float[] coefficients = new float[27];
    20.     }
    21.  
    22.     [System.Serializable]
    23.     private class RendererInfo
    24.     {
    25.         public Renderer renderer;
    26.         public int lightmapIndex;
    27.         public Vector4 lightmapOffsetScale;
    28.     }
    29.  
    30.     [System.Serializable]
    31.     private class LightingScenarioData
    32.     {
    33.         public RendererInfo[] rendererInfos;
    34.         public Texture2D[] lightmaps;
    35.         public Texture2D[] lightmapsDir;
    36.         public Texture2D[] lightmapsShadow;
    37.         public LightmapsMode lightmapsMode;
    38.         public SphericalHarmonics[] lightProbes;
    39.     }
    40.  
    41.     [SerializeField]
    42.     private LightingScenarioData lightingScenariosData;
    43.  
    44.     [SerializeField] private bool loadOnAwake = false; // Load the selected lighmap when this script wakes up (aka when game starts).
    45.  
    46.     //TODO : enable logs only when verbose enabled
    47.     [SerializeField] private  bool verbose = false;
    48.  
    49.     public string GetResourcesDirectory (string dir)
    50.     {
    51.         return Application.dataPath + "/Resources/"+dir+"/"; // The directory where the lightmap data resides.
    52.     }
    53.  
    54.     public bool CheckResourcesDirectoryExists (string dir)
    55.     {
    56.         return Directory.Exists (GetResourcesDirectory(dir));
    57.     }
    58.  
    59.     private void CreateResourcesDirectory(string dir) {
    60.         if (!CheckResourcesDirectoryExists (m_resourceFolder))
    61.         {
    62.             Directory.CreateDirectory (GetResourcesDirectory(dir));
    63.         }
    64.     }
    65.  
    66.     public void Load (string folderName)
    67.     {
    68.         m_resourceFolder = folderName;
    69.         Load ();
    70.     }
    71.     public void Load ()
    72.     {
    73.         lightingScenariosData = LoadJsonData ();
    74.  
    75.         var newLightmaps = new LightmapData[lightingScenariosData.lightmaps.Length];
    76.  
    77.         for (int i = 0; i < newLightmaps.Length; i++)
    78.         {
    79.             newLightmaps[i] = new LightmapData();
    80.             newLightmaps[i].lightmapLight = Resources.Load<Texture2D>(m_resourceFolder+"/" + lightingScenariosData.lightmaps[i].name);
    81.  
    82.             if (lightingScenariosData.lightmapsMode != LightmapsMode.NonDirectional)
    83.             {
    84.                 newLightmaps [i].lightmapDir = Resources.Load<Texture2D>(m_resourceFolder+"/" + lightingScenariosData.lightmapsDir[i].name);
    85.                 if (lightingScenariosData.lightmapsShadow [i] != null) { // If the textuer existed and was set in the data file.
    86.                     newLightmaps [i].shadowMask = Resources.Load<Texture2D> (m_resourceFolder + "/" + lightingScenariosData.lightmapsShadow [i].name);
    87.                 }
    88.             }
    89.         }
    90.  
    91.         LoadLightProbes();
    92.         ApplyRendererInfo(lightingScenariosData.rendererInfos);
    93.  
    94.         LightmapSettings.lightmaps = newLightmaps;
    95.     }
    96.  
    97.     private void LoadLightProbes()
    98.     {
    99.         var sphericalHarmonicsArray = new SphericalHarmonicsL2[lightingScenariosData.lightProbes.Length];
    100.  
    101.         for (int i = 0; i < lightingScenariosData.lightProbes.Length; i++)
    102.         {
    103.             var sphericalHarmonics = new SphericalHarmonicsL2();
    104.  
    105.             // j is coefficient
    106.             for (int j = 0; j < 3; j++)
    107.             {
    108.                 //k is channel ( r g b )
    109.                 for (int k = 0; k < 9; k++)
    110.                 {
    111.                     sphericalHarmonics[j, k] = lightingScenariosData.lightProbes[i].coefficients[j * 9 + k];
    112.                 }
    113.             }
    114.  
    115.             sphericalHarmonicsArray[i] = sphericalHarmonics;
    116.         }
    117.  
    118.         try
    119.         {
    120.             LightmapSettings.lightProbes.bakedProbes = sphericalHarmonicsArray;
    121.         }
    122.         catch { Debug.LogWarning("Warning, error when trying to load lightprobes for scenario "); }
    123.     }
    124.  
    125.     private void ApplyRendererInfo (RendererInfo[] infos)
    126.     {
    127.         try
    128.         {
    129.             for (int i = 0; i < infos.Length; i++)
    130.             {
    131.                 var info = infos[i];
    132.                 info.renderer.lightmapIndex = infos[i].lightmapIndex;
    133.                 if (!info.renderer.isPartOfStaticBatch)
    134.                 {
    135.                     info.renderer.lightmapScaleOffset = infos[i].lightmapOffsetScale;
    136.                 }
    137.                 if (info.renderer.isPartOfStaticBatch && verbose == true)
    138.                 {
    139.                     Debug.Log("Object " + info.renderer.gameObject.name + " is part of static batch, skipping lightmap offset and scale.");
    140.                 }
    141.             }
    142.         }
    143.  
    144.         catch (Exception e)
    145.         {
    146.             Debug.LogError("Error in ApplyRendererInfo:" + e.GetType().ToString());
    147.         }
    148.     }
    149.  
    150.  
    151.  
    152.     public static ChangeLightmap instance;
    153.     private void Awake ()
    154.     {
    155.         if (instance != null)
    156.         { // Singleton pattern.
    157.             Destroy(gameObject);
    158.         }else{
    159.             instance = this;
    160.         }
    161.  
    162.         if (loadOnAwake) {
    163.             Load ();
    164.         }
    165.     }
    166.  
    167.     private void WriteJsonFile (string path, string json)
    168.     {
    169.         absoluteName = path + jsonFileName;
    170.         File.WriteAllText (absoluteName, json); // Write all the data to the file.
    171.     }
    172.  
    173.     private string GetJsonFile (string f)
    174.     {
    175.         if (!File.Exists(f))
    176.         {
    177.             return "";
    178.         }
    179.         return File.ReadAllText (f); // Write all the data to the file.
    180.     }
    181.  
    182.     private LightingScenarioData LoadJsonData ()
    183.     {
    184.         absoluteName = GetResourcesDirectory(m_resourceFolder) + jsonFileName;
    185.         string json = GetJsonFile (absoluteName);
    186.         return lightingScenariosData = JsonUtility.FromJson<LightingScenarioData> (json);
    187.     }
    188.  
    189.     public void GenerateLightmapInfoStore ()
    190.     {
    191.         lightingScenariosData = new LightingScenarioData ();
    192.         var newRendererInfos = new List<RendererInfo>();
    193.         var newLightmapsTextures = new List<Texture2D>();
    194.         var newLightmapsTexturesDir = new List<Texture2D>();
    195.         var newLightmapsTexturesShadow = new List<Texture2D>();
    196.         var newLightmapsMode = new LightmapsMode();
    197.         var newSphericalHarmonicsList = new List<SphericalHarmonics>();
    198.  
    199.         newLightmapsMode = LightmapSettings.lightmapsMode;
    200.  
    201.         var renderers = FindObjectsOfType(typeof(MeshRenderer));
    202.         Debug.Log("stored info for "+renderers.Length+" meshrenderers");
    203.         foreach (MeshRenderer renderer in renderers)
    204.         {
    205.  
    206.             if (renderer.lightmapIndex != -1)
    207.             {
    208.                 RendererInfo info = new RendererInfo();
    209.                 info.renderer = renderer;
    210.                 info.lightmapOffsetScale = renderer.lightmapScaleOffset;
    211.  
    212.                 Texture2D lightmaplight = LightmapSettings.lightmaps[renderer.lightmapIndex].lightmapLight;
    213.                 info.lightmapIndex = newLightmapsTextures.IndexOf(lightmaplight);
    214.                 if (info.lightmapIndex == -1)
    215.                 {
    216.                     info.lightmapIndex = newLightmapsTextures.Count;
    217.                     newLightmapsTextures.Add(lightmaplight);
    218.                 }
    219.  
    220.                 if (newLightmapsMode != LightmapsMode.NonDirectional)
    221.                 {
    222.                     //first directional lighting
    223.                     Texture2D lightmapdir = LightmapSettings.lightmaps[renderer.lightmapIndex].lightmapDir;
    224.                     info.lightmapIndex = newLightmapsTexturesDir.IndexOf(lightmapdir);
    225.                     if (info.lightmapIndex == -1)
    226.                     {
    227.                         info.lightmapIndex = newLightmapsTexturesDir.Count;
    228.                         newLightmapsTexturesDir.Add(lightmapdir);
    229.                     }
    230.                     //now the shadowmask
    231.                     Texture2D lightmapshadow = LightmapSettings.lightmaps[renderer.lightmapIndex].shadowMask;
    232.                     info.lightmapIndex = newLightmapsTexturesShadow.IndexOf(lightmapshadow);
    233.                     if (info.lightmapIndex == -1)
    234.                     {
    235.                         info.lightmapIndex = newLightmapsTexturesShadow.Count;
    236.                         newLightmapsTexturesShadow.Add(lightmapshadow);
    237.                     }
    238.  
    239.                 }
    240.                 newRendererInfos.Add(info);
    241.             }
    242.         }
    243.  
    244.  
    245.         lightingScenariosData.lightmapsMode = newLightmapsMode;
    246.         lightingScenariosData.lightmaps = newLightmapsTextures.ToArray();
    247.  
    248.         if (newLightmapsMode != LightmapsMode.NonDirectional)
    249.         {
    250.             lightingScenariosData.lightmapsDir = newLightmapsTexturesDir.ToArray();
    251.             lightingScenariosData.lightmapsShadow = newLightmapsTexturesShadow.ToArray();
    252.         }
    253.  
    254.         lightingScenariosData.rendererInfos = newRendererInfos.ToArray();
    255.  
    256.         var scene_LightProbes = new SphericalHarmonicsL2[LightmapSettings.lightProbes.bakedProbes.Length];
    257.         scene_LightProbes = LightmapSettings.lightProbes.bakedProbes;
    258.  
    259.         for (int i = 0; i < scene_LightProbes.Length; i++)
    260.         {
    261.             var SHCoeff = new SphericalHarmonics();
    262.  
    263.             // j is coefficient
    264.             for (int j = 0; j < 3; j++)
    265.             {
    266.                 //k is channel ( r g b )
    267.                 for (int k = 0; k < 9; k++)
    268.                 {
    269.                     SHCoeff.coefficients[j*9+k] = scene_LightProbes[i][j, k];
    270.                 }
    271.             }
    272.  
    273.             newSphericalHarmonicsList.Add(SHCoeff);
    274.         }
    275.  
    276.         lightingScenariosData.lightProbes = newSphericalHarmonicsList.ToArray ();
    277.  
    278.         // write the files and map config data.
    279.         CreateResourcesDirectory (m_resourceFolder);
    280.  
    281.         string resourcesDir = GetResourcesDirectory(m_resourceFolder);
    282.         CopyTextureToResources (resourcesDir, lightingScenariosData.lightmaps);
    283.         CopyTextureToResources (resourcesDir, lightingScenariosData.lightmapsDir);
    284.         CopyTextureToResources (resourcesDir, lightingScenariosData.lightmapsShadow);
    285.  
    286.         string json = JsonUtility.ToJson(lightingScenariosData);
    287.         WriteJsonFile (resourcesDir, json);
    288.     }
    289.  
    290.     private void CopyTextureToResources (string toPath, Texture2D[] textures)
    291.     {
    292.         for (int i = 0; i < textures.Length; i++)
    293.         {
    294.             Texture2D texture = textures [i];
    295.             if (texture != null) // Maybe the optional shadowmask didn't exist?
    296.             {
    297.                 FileUtil.ReplaceFile (
    298.                     AssetDatabase.GetAssetPath (texture),
    299.                     toPath + Path.GetFileName (AssetDatabase.GetAssetPath (texture))
    300.                 );
    301.                 AssetDatabase.Refresh (); // Refresh so the newTexture file can be found and loaded.
    302.                 Texture2D newTexture = Resources.Load<Texture2D> (m_resourceFolder + "/" + texture.name); // Load the new texture as an object.
    303.  
    304.                 CopyTextureImporterProperties (textures [i], newTexture); // Ensure new texture takes on same properties as origional texture.
    305.  
    306.                 AssetDatabase.ImportAsset( AssetDatabase.GetAssetPath( newTexture ) ); // Re-import texture file so it will be successfully compressed to desired format.
    307.                 EditorUtility.CompressTexture (newTexture, textures[i].format, TextureCompressionQuality.Best); // Now compress the texture.
    308.  
    309.                 textures [i] = newTexture; // Set the new texture as the reference in the Json file.
    310.             }
    311.         }
    312.     }
    313.  
    314.     private void CopyTextureImporterProperties (Texture2D fromTexture, Texture2D toTexture)
    315.     {
    316.         TextureImporter fromTextureImporter = GetTextureImporter (fromTexture);
    317.         TextureImporter toTextureImporter = GetTextureImporter (toTexture);
    318.  
    319.         toTextureImporter.wrapMode = fromTextureImporter.wrapMode;
    320.         toTextureImporter.anisoLevel = fromTextureImporter.anisoLevel;
    321.         toTextureImporter.sRGBTexture = fromTextureImporter.sRGBTexture;
    322.         toTextureImporter.textureType = fromTextureImporter.textureType;
    323.         toTextureImporter.textureCompression = fromTextureImporter.textureCompression;
    324.     }
    325.  
    326.     private TextureImporter GetTextureImporter (Texture2D texture)
    327.     {
    328.         string newTexturePath =  AssetDatabase.GetAssetPath (texture);
    329.         TextureImporter importer = AssetImporter.GetAtPath (newTexturePath) as TextureImporter;
    330.         return importer;
    331.     }
    332.  
    333. }
    Editor script (presents the Load and Save buttons for the inspector):
    Code (csharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3.  
    4. [CustomEditor(typeof(ChangeLightmap))]
    5. public class ChangeLightmapInspector : Editor
    6. {
    7.     public override void OnInspectorGUI()
    8.     {
    9.         base.OnInspectorGUI ();
    10.  
    11.         ChangeLightmap lightmapData = (ChangeLightmap)target;
    12.  
    13.         if (GUILayout.Button("Load"))
    14.         {
    15.             lightmapData.Load ();
    16.         }
    17.         if (GUILayout.Button("Save"))
    18.         {
    19.             if (lightmapData.CheckResourcesDirectoryExists (lightmapData.resourceFolder)) {
    20.                 if (!EditorUtility.DisplayDialog ("Overwrite Lightmap Resources?", "Lighmap Resources folder with name: \"" + lightmapData.resourceFolder + "\" already exists.\n\nPress OK to overwrite existing lightmap data.", "OK", "Cancel")) {
    21.                     return;
    22.                 }
    23.             } else {
    24.                 if (!EditorUtility.DisplayDialog ("Create Lightmap Resources?", "Create new lighmap Resources folder: \"" + lightmapData.resourceFolder + "?", "OK", "Cancel")) {
    25.                     return;
    26.                 }
    27.             }
    28.             lightmapData.GenerateLightmapInfoStore ();
    29.         }
    30.     }
    31. }

    If you find these scripts useful - please give this post a like.
     
    Last edited: Mar 16, 2020
    shemlink02, Riiich, alienide and 28 others like this.
  5. CloudyVR

    CloudyVR

    Joined:
    Mar 26, 2017
    Posts:
    715
    Example how to change scene's lightmap when a gameobject is enabled (using above ChangeLightmap.cs script):

    Code (csharp):
    1. using UnityEngine;
    2.  
    3. public class ChangeLightmapController : MonoBehaviour {
    4.  
    5.     [SerializeField] ChangeLightmap lightmapChanger;
    6.     [SerializeField] string resourceFolder;
    7.  
    8.     void OnEnable() {
    9.         lightmapChanger.Load(resourceFolder);
    10.     }
    11.  
    12. }


    First you must bake and save two lightmaps with one of them being named "LightMapData_Dark".

    Then attach this script to a disable gameobject in the scene.

    Once that object is enabled the scene's lightmap will change and the "LightMapData_Dark" lightmap textures will be loaded.

    Enjoy!
     
    Last edited: Sep 5, 2017
  6. rambambulli

    rambambulli

    Joined:
    May 31, 2017
    Posts:
    3
    This is really great stuff!

    Can I add the dark Lightmap to only one object in the scene?

    So I don't switch the lightmaps but load both. Add the light one to some object and hook up the dark one to some other object?

    Maybe it a bit off topic but I ask because I am looking for a while now a method to load external lightmaps, add them to an array in unity and add (index) them to objects in my scene. I read a lot of posts but cannot figure it out how to do it.

    Your posts are the most explanatory thus far... I would appreciate if you could help me.
     
    CloudyVR likes this.
  7. CloudyVR

    CloudyVR

    Joined:
    Mar 26, 2017
    Posts:
    715
    I think this would be possible if you loaded the light and dark (and any other) lightmaps at the same time as new LightmapData with different indexes. Then you would see those lightmaps in your editor's lighting options. You could set the lightmapindex for each meshrenderer to point to the lightmapindex you wish them to use.

    Note that the lightmapscaleoffsets for each lightmap may differ, so when you change the lightmap index for a meshrender you will also have to ensure that the correct offsets were set.

    ---------

    Also I have started working on a more robust tool for switching lightmaps which doesn't rely on Unity's InstanceIDs and now uses UniqueIDs to ensure proper synchronization. Check it out here
     
    Last edited: Sep 13, 2017
    Markstam291 likes this.
  8. morphinegames

    morphinegames

    Joined:
    Mar 13, 2015
    Posts:
    33
    This is really great. I wanted to make a note for anyone that is trying to use this with mixed lighting. You are going to want to do exactly what is outlined above with 2 additional steps

    1. For each "lighting scenario" that you have, use a different light, e.g. have a Day light and a Night light with all the settings you want.
    2. At runtime, you want to make sure to enable the active mixed light and disable the other one. Otherwise the lighting will look wonky.

    here's an example of the script I used

    Code (CSharp):
    1.    public class TimeOfDay : MonoBehaviour
    2.     {
    3.         public ChangeLightmap lightmapChanger;
    4.         public Light day;
    5.         public Light night;
    6.  
    7.         [Inspector]
    8.         public void Day()
    9.         {
    10.             lightmapChanger.Load("Daytime");
    11.             night.gameObject.SetActive(false);
    12.             day.gameObject.SetActive(true);
    13.         }
    14.  
    15.         [Inspector]
    16.         public void Night()
    17.         {
    18.             lightmapChanger.Load("Nighttime");
    19.             day.gameObject.SetActive(false);
    20.             night.gameObject.SetActive(true);
    21.         }
    22.     }
     
  9. CloudyVR

    CloudyVR

    Joined:
    Mar 26, 2017
    Posts:
    715
    Hi all, I just wanted to drop an update since I haven't been on this post for a while.

    so, in the end I believe the example I posted above is not exactly bullet proof. And the trouble arose when restarting the editor application or building the project. I noticed Unity would loose track of the instance IDs and things didn't work quite as expected.

    Anyway, with haste I wrote a solution to this annoyance, and expanding on the example outline in this thread I was able to add persistent unique IDs so that scene object can always be referenced.

    Please take a glance at this post for details, or go straight to the download page for the latest tool:

    Lighting Scenario Switcher on GitHub

     
    Last edited: Oct 20, 2017
  10. BanzBowinkel

    BanzBowinkel

    Joined:
    Jun 17, 2015
    Posts:
    16
    Dear Trappist 1,
    thanx for the work! Looks quite promising since other solutions deprecated with version 5.6
    :p
    can you tell, how reflection probes are going to be handled with this setup?
    I guess the'd have to switch as well.
    Best
     
    CloudyVR likes this.
  11. CloudyVR

    CloudyVR

    Joined:
    Mar 26, 2017
    Posts:
    715
    Thanks! When I originally wrote the scripts I did not take into account the reflection probes (and I am not familiar with how to access them programmatically). I have made a mental note to try to include them in a future update if there is interest in the project.

    If reflection probes are anything similar to lightprobes then this could be a very easy addition.

    Take a look at the way the lightprobe coeffieients are stored on line #370 in LSS_FrontEnd.cs

    It could be a simple matter of just including an additional list (similar to newSphericalHarmonicsModelList on line #224)

    Add the reflection probe list to the lightingScenariosData array so it will be serialized into the json file (eg. line #382).

    Then just be sure to deserialize it similar to line #99.

    Finally, you would need to create a basic model to apply the data to the scene. Check out line #14 in LSS_Models as an example.

    In this way, just about any serializable data could be stored/restored using this tool..
     
    Last edited: Nov 9, 2017
  12. Eternity774

    Eternity774

    Joined:
    Jan 30, 2018
    Posts:
    5
    This is amazing, you just save a lot of my time. Thanks you a lot. I'm creating low poly 3d game for mobile, and obviously I will create many levels and all of these I want to place in 2 scenes: menu and game. Excellent job!
     
  13. ufo_zj

    ufo_zj

    Joined:
    Nov 7, 2018
    Posts:
    6
    Excellent work! this code works fine for me, Unity 2018.2.20f1 (64-bit)
     
    NeatWolf and CloudyVR like this.
  14. Jakub_Machowski

    Jakub_Machowski

    Joined:
    Mar 19, 2013
    Posts:
    647
    ANyone knows if that works also for Realtime GI Lightning Asset Data? I try to change Lighting Data Asset but unity dont have api for In ENgine us of it, it works only in Editor. ?
     
    MrWangzzzz likes this.
  15. unitylepi

    unitylepi

    Joined:
    May 21, 2018
    Posts:
    34
    Pretty interested to know what we need to assigned any loaded reflection probes to. Reflection probes don't seem to go in the LightmapSettings or LightmapData :( The Unity manual only seems to talk about using them in the editor, not assigning them programatically. Anybody any ideas?
     
  16. Opeth001

    Opeth001

    Joined:
    Jan 28, 2017
    Posts:
    1,117
    Hello Guys,
    im new to all this rendering stuff and trying to create my own Hybred Renderer using ECS.
    i want it to support LightProbs and baked Lightmaps and for this im going to use the DrawMeshInstanced api.

    Can you please show any example here on how to include the Light probs and Lightmaps to the MaterialPropertyBlock and use it with the DrawMeshInstanced function?

    also, im trying to create prebaked prefabs, baked in a custom scene and instantiated with their lightmap ... in other scenes.
    the problem im facing now is i cant add Lightmap data to the current scene LightmapSettings.lightmaps. is it event possible ? if so please point me to the right direction.

    Thank you !!
     
  17. vijayv03

    vijayv03

    Joined:
    Dec 16, 2016
    Posts:
    15
    With just "Baked" lights everything is working fine, but could not get it to work with "Mixed" lights. With the above code, I am able to swap between lightmaps, the materials are using the proper lightmaps but the shadows are still missing.

    Thanks @trappist-1 btw.
     
    CloudyVR likes this.
  18. MrWangzzzz

    MrWangzzzz

    Joined:
    Apr 21, 2020
    Posts:
    2
    Hi,did you find an answer to solve this question? i really need it, thanks
     
  19. aheydeck

    aheydeck

    Joined:
    Dec 17, 2014
    Posts:
    8
    This was a life saver! =)
    Works for me in version 19.4.3
     
    CloudyVR likes this.
  20. loonybao

    loonybao

    Joined:
    Mar 8, 2021
    Posts:
    1
    Hi, any solution for this?