Search Unity

How to create lightmap for day and for night and switch them in runtime.

Discussion in 'Global Illumination' started by satween, Apr 19, 2015.

  1. satween

    satween

    Joined:
    Apr 19, 2015
    Posts:
    1
    Hi everyone!
    I am newby in unity5 and right now i am working on some project for mobile platforms, that should switch to Day and Night mode.

    I did setup scene, lights. And i know how to create lightmap.
    I want to create 2 lightmaps: one for day light and another fot night light.
    Then, i want to be able to switch them in runtime.

    But when i create a lightmap it override previous snapshot.
    Can anyone tell me how i can create 2 or more lightmap for same scene and switch them in runtime?

    I am using Unity 5.0.1

    Thanks.
     
    afshin_a_1 and AntonioModer like this.
  2. topofsteel

    topofsteel

    Joined:
    Dec 2, 2011
    Posts:
    999
  3. Bollard

    Bollard

    Joined:
    Aug 12, 2013
    Posts:
    1
    I'd like to bump this topic, as in Unity 4 I had a project where I would swap lightmaps in code by changing the LightMapData.lightmapNear and lightmapFar, as can be seen here: http://docs.unity3d.com/ScriptReference/LightmapData.html

    Now it seems the new GI does not use the near and far lightmap model - changing these lightmaps in code has no effect, and baking maps no longer produces a separate near and far lightmap, rather just a single lightmap (I can't remember the name of it right now, don't have access to Unity 5).

    What I wanted to ask is why the documentation still has reference to the LightmapData when it appears to do nothing, or am I using it wrong now with Unity 5?
     
    Chris_Covington951 likes this.
  4. iivo_k

    iivo_k

    Joined:
    Jan 28, 2013
    Posts:
    314
    Bump, same problem. Switched lighmaps dynamically in Unity 4, trying to find out how to do it in Unity 5.
     
    Dmonty likes this.
  5. SpiriTx

    SpiriTx

    Graphics QA

    Joined:
    Apr 12, 2012
    Posts:
    252
    It' still the same. Just checked with Lightmap switcher linked previously and it works, simply have in mind that:
    Near - directional
    Far - light

    Poked devs to clean up scripting API to avoid this confusion
     
    Dmonty, pixxelbob and robstardotstar like this.
  6. nawash

    nawash

    Joined:
    Jan 29, 2010
    Posts:
    166
    Hi,
    I don't get it... How can http://wiki.unity3d.com/index.php/LightMapSwitcher still work because the lightmaps are not accessible anymore in the project asset folder ? at least in version 5.1.2f1 I'm currently using.

    OOOPsss.. got it. Just got to uncheck the auto check box and build manually
     
    Last edited: Sep 1, 2015
    IgorAherne and robstardotstar like this.
  7. lmbarns

    lmbarns

    Joined:
    Jul 14, 2011
    Posts:
    1,628
    Do you have to call something to refresh things? Im using the script, have my 2 sets of lightmaps, and in the lighting panel after I "switch to day" and click on the lightmaps showing up in the lighting panel it's correct, day version, when I "switch to night" the maps do change to the night version, but there's no visual effect within the scene.......

    Verified dir is near, light is far.
     
    afshin_a_1 likes this.
  8. dishmop

    dishmop

    Joined:
    Sep 5, 2014
    Posts:
    5
    Blimey! I have been banging my head on my keyboard for hours trying to figure out how to get hold of my light map files as they were magically not appearing any more like they used to...then cam across this small comment - and it all becomes clear..
     
    IgorAherne likes this.
  9. avi2012bhoyar

    avi2012bhoyar

    Joined:
    May 31, 2016
    Posts:
    2
    anybody can upload project sample on this..!!
     
  10. KeithDuck

    KeithDuck

    Joined:
    May 11, 2016
    Posts:
    4
    I'm confused. First time using Light Map Switcher and I am in Unity 5. I have two lightmaps made for a room in a scene.

    When I built for day it produced 3 files:

    LightingData.asset
    Lightmap-0_comp_light.exr
    RefectionProbe-0.exr

    I moved these into a different folder and then I built for night.

    So now I've attached the LightMapSwitcher to an object in the scene. What exactly do I set Day Near/Far too and Night Near/Far to?
     
  11. andrew_2992

    andrew_2992

    Joined:
    Jun 27, 2015
    Posts:
    8
    I'm at the same point, spiritx mentioned above, near is directional and far is light. the files that get generated when you build your maps will be the files you drag in there... you should have 4 though... at least I have 4.

    I have Lightmap-0_comp_dir 1 - Directional
    &
    Lightmap-0_comp_light 1 - Light

    so the dir one is near, and the light one is the far... I haven't tried, yet. I'll let you know how it goes.

    I also have the light probe file and the lighting data.asset file. It would be awesome if changine the lighting was as easy as setting the lightingdata.asset file to whichever one was generated from your light build
     
  12. andrew_2992

    andrew_2992

    Joined:
    Jun 27, 2015
    Posts:
    8
    It worked beautifully for me. Just make sure you put in the right maps... and that they're actually different. Im on 5.3. I had a problem at first but that was because i had another script setting the baked lightmap for each renderer in the scene at start().
    I just link the script on the unity wiki 'LightmapSwitcher' to another script by having an instance of the lightmapswitcher in the class, then after i drag it on in the editor, when I play I can call the function from my script and all the lightmaps apear to be properly switched.
     
  13. KeithDuck

    KeithDuck

    Joined:
    May 11, 2016
    Posts:
    4
    Oh I have been baking with directional mode set to Non-Directional. Maybe that explains the missing file.

    Going to try it later and see if a 4th file pops up.
     
  14. tomekkie2

    tomekkie2

    Joined:
    Jul 6, 2012
    Posts:
    973
    I have made new script, taking into account ReflectionProbes, which can be present in Unity 5.0 lighting system.
    It is simpler than the wiki one, but assumes some workflow steps.
    Create two separate sets of baked lights; frist for the night, the second for the day.
    Set the day set inactive and bake the lighting.
    Editor will create the folder named after the scene next to the scene file and will put all the lightmap and reflection maps there.
    Duplicate this folder and move the duplicate (not original, because the original still holds the references) to the Resouces folder.
    Rename this folder back to scene name after being renamed when duplicating by Unity.
    Set the day lights set active and the night set inactive.
    Bake the lightmaps again.
    Attach this script to any GameObject in the scene.
    The effects are like that: lightmap_swap.png

    The script and script setup in the scene is extremely simple.

    The lighting maps instead of being packed inside Resouces could also alternatively be packed into AssetBundles and loaded from there.

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.SceneManagement;
    3. using UnityEngine.UI;
    4. using UnityEngine.Rendering;
    5. using System.Collections;
    6.  
    7. public class LightmapsSwap : MonoBehaviour {
    8.     //GUI dropdown
    9.     public Dropdown dropdown;
    10.  
    11.     private LightmapData[] Daydata;
    12.     private LightmapData[] Nightdata;
    13.  
    14.     private LightmapData[][] allLightmaps;
    15.  
    16.     private Texture[] DayReflections;
    17.     private Texture[] NightReflections;
    18.  
    19.     private Texture[][] allReflections;
    20.  
    21.     private ReflectionProbe[] allReflectionProbes;
    22.     //path to the night lightmap and reflection substitudes
    23.     public string nightpath = "";
    24.  
    25.  
    26.  
    27.     // Use this for initialization
    28.     void Start () {
    29.         //ypu can duplicate lightmap folder to break references, move to Resources, and rename back to the scene name
    30.         if (nightpath == "") nightpath = SceneManager.GetActiveScene().name;
    31.  
    32.         Daydata = LightmapSettings.lightmaps;
    33.  
    34.         allLightmaps = new LightmapData[2][];
    35.         allLightmaps[0] = Daydata;
    36.      
    37.         Nightdata = new LightmapData[Daydata.Length];
    38.  
    39.         for (int i = 0; i < Daydata.Length; i++)
    40.         {
    41.             Nightdata[i] = new LightmapData();
    42.             Nightdata[i].lightmapFar = Resources.Load(nightpath + "/" + Daydata[i].lightmapFar.name) as Texture2D;
    43.             Nightdata[i].lightmapNear = Resources.Load(nightpath + "/" + Daydata[i].lightmapNear.name) as Texture2D;
    44.         }
    45.  
    46.         allLightmaps[1] = Nightdata;
    47.  
    48.         allReflectionProbes = FindObjectsOfType<ReflectionProbe>();
    49.  
    50.         DayReflections = new Texture[allReflectionProbes.Length];
    51.         NightReflections = new Texture[allReflectionProbes.Length];
    52.  
    53.         for (int i = 0; i < allReflectionProbes.Length; i++ )
    54.         {
    55.             DayReflections[i] = allReflectionProbes[i].bakedTexture;
    56.             NightReflections[i] = Resources.Load(nightpath + "/" + DayReflections[i].name) as Texture;
    57.             allReflectionProbes[i].mode = ReflectionProbeMode.Custom;
    58.             allReflectionProbes[i].customBakedTexture = DayReflections[i];
    59.         }
    60.  
    61.         allReflections = new Texture[2][];
    62.         allReflections[0] = DayReflections;
    63.         allReflections[1] = NightReflections;
    64.  
    65.         dropdown.onValueChanged.AddListener(delegate
    66.         {
    67.             SwapLightmaps(dropdown.value);
    68.         });
    69.     }
    70.  
    71.     public void SwapLightmaps (int option) {
    72.  
    73.         LightmapSettings.lightmaps = allLightmaps[option];
    74.         for (int i = 0; i < allReflectionProbes.Length; i++)
    75.         {
    76.             allReflectionProbes[i].customBakedTexture = allReflections[option][i];
    77.         }
    78.     }
    79. }
     
    Last edited: Dec 4, 2016
  15. kilik128

    kilik128

    Joined:
    Jul 15, 2013
    Posts:
    909
    Look Awesome
     
  16. u3gt6o8

    u3gt6o8

    Joined:
    Jun 9, 2014
    Posts:
    17
    I agree.

    What about light probes? any idea?
     
    afshin_a_1 likes this.
  17. tomekkie2

    tomekkie2

    Joined:
    Jul 6, 2012
    Posts:
    973
  18. TinyCobra

    TinyCobra

    Joined:
    Jan 19, 2015
    Posts:
    3
    I got this working with one scene, but now the issue is I have two scenes that get loaded together, with their own lightmaps. When I use this script to switch lightmaps in one of the scenes, it erases the lightmaps of the other scene. Any ideas on how to stop the script from doing that?
     
  19. MarioRocket

    MarioRocket

    Joined:
    Nov 2, 2016
    Posts:
    3
    Hello, and thanks for that script. Unfortunately it does not run (Unity 5.5.1f1), some deprecated code, and also I do not know how to use it (and I really wonder how takanik123 got it work). You write "put it on any game object" -> on really EVERY object? Do I have to do do something with the dropdown-field in the inspector, and how to enter the path ("/Assets/Resources/" or "Assets/Resources" or "Assets/Resources/<mySceneName>" or nothing at all?)? Do I have to manually copy the lightmaps every time I bake them to the second lightmap folder? I tried every variation and also read the code, but the script does absolutely nothing. Thanks for your help in advance. The screenshots look promising, and after hours of searching the internet it seems that this script is the only one working (many say they found a solution, but no one else could get their scripts running it seems).
     
  20. tomekkie2

    tomekkie2

    Joined:
    Jul 6, 2012
    Posts:
    973
    I can't see any recent changes that could affect (deprecate) that.
    So - at lest for me - it does work in 5.5.1. Provided you follow the steps precisely. And:
    But
    Not every but any - like in my original post.:) Any single one.
    So I have rebaked now. The nightpath field stays empty in my scene.
    1. I have switched the night lighting on, day off.
    2. Baked
    3. Duplicated lightmaps. (duplicate folder got renamed)
    4. Moved duplicated lightmaps folder into Resources folder
    5. Renamed lightmaps folder back
    6. switched the day lighting on, night off
    7. Baked scene again
    8, Checked and works
     
    Last edited: Feb 16, 2017
  21. kemalakay

    kemalakay

    Unity Technologies

    Joined:
    Jul 12, 2016
    Posts:
    224
  22. BonsaTech

    BonsaTech

    Joined:
    Dec 22, 2015
    Posts:
    73
  23. Dmonty

    Dmonty

    Joined:
    Nov 15, 2013
    Posts:
    2
    I'm actually trying to accomplish something similar, but want to toggle a light in a room using mixed lighting. In Unity 5, is the lightmap switcher script still needed or does Unity 5 use some other technique? If so, could someone explain it?

    Thank you,
     
    CloudyVR likes this.
  24. orkungo

    orkungo

    Joined:
    May 3, 2014
    Posts:
    62
    Hi,

    I am using Lightmap Switching tool with asset bundles. My scenario is, I would like to bundle baked scenes (day/night) and upload to an FTP server then download them to load in Additive mode on the top of my main scene. Say SceneName.unity3d, SceneName_Day.unity3d and SceneName_Night.unity3d. I can download all scenes however at the device receiving NulReferenceException.

    I assume it is because of the .exr files are not being bundled correctly therefore they can not be found. Did you experience any kind of problems? And what would be the best workflow to use lightmaps and assetbundles to updload-download and load dynamically?
     
  25. orkungo

    orkungo

    Joined:
    May 3, 2014
    Posts:
    62
    Does anyone have any idea how to bundle lightmaps and load them from AssetBundles with LightmapSwitcher?
     
  26. Skelly1983

    Skelly1983

    Joined:
    Mar 9, 2017
    Posts:
    17
    This still works fine with 2018.2, only addition i have made is an ExecuteInEditMode attribute for the context menus to work.
     
    sandboks and tomekkie2 like this.
  27. afshin_a_1

    afshin_a_1

    Joined:
    Apr 26, 2015
    Posts:
    52
    why this feature(lightmap switching) with supporting different scenarios(including lightprobes support ect) are not officially coming to unity? this is a long time requested feature and still we can't see in 2019 and unity didn't any step forward about multiple lightmaps and switching feature. hope we see it in unity 2019.2
     
    Last edited: Jul 1, 2019
  28. KEngelstoft

    KEngelstoft

    Unity Technologies

    Joined:
    Aug 13, 2013
    Posts:
    1,366
    It is on our roadmap, but it is still too early to give an ETA.
     
    thelebaron likes this.
  29. Komikom

    Komikom

    Joined:
    May 20, 2015
    Posts:
    38
    Bump, are there any news regarding the ,,built-in" lightmap switcher? Is there a chance it will come out in 2020.x?
     
  30. rasmusn

    rasmusn

    Unity Technologies

    Joined:
    Nov 23, 2017
    Posts:
    103
    @Komikom No, I'm afraid that there is no news on this. It is something that we are still actively discussing. It is a design consideration that potentially touches a lot of different areas (lightmaps, light probes, dynamic lighting, etc.) so a lot of coordination is required to get this right. We cannot say yet whether it will be part of 2020.x.
     
  31. Thall33

    Thall33

    Joined:
    Sep 18, 2013
    Posts:
    134
    Our team is developing a VR fire fighting trainer. Flaim Systems. Currently were using light baking for a lot of scenarios as this gives us the gpu bandwidth to create realistic fire and fx.

    Lots of our scenarios like a couch, bed, kitchen fire need model swaps for the same environment, but because we cant light map swap while retaining a 'master light map UV' (as the lightmaps uvs rearrange when geo is changed in a scene) our only solution is to duplicate scenes and light bake them separately. Obviously this creates a bigger, longer and stricter management of assets per scene to keep things consistent, and as we are finding out, the room for error is big.

    The introduction of adding a light map swapper for a single scene would save 1000's of man hours per year and our users wouldn't have to load in new scenes. Obviously the benefits would be felt across a lot of VR/game production that needs light baking to maximize performance.

    I hope it gets some serious consideration. Thank you.
     
    Last edited: Feb 20, 2020
    Bordeaux_Fox likes this.
  32. Bordeaux_Fox

    Bordeaux_Fox

    Joined:
    Nov 14, 2018
    Posts:
    589
    Let's face it, they had five years of making such a lightmap switcher included as a feature in the editor. I doubt they will ever implement it. If so, not in the development cycle of an average game, which might be yours. There is always the "not the highest priority" answer or "we had plans but ..." or "we still investigate ...". So my advice: If you want it, do it yourself or hire a good programmer. Other than that, I just can support the ask for a lightmap switcher despite I would not rely on a good outcome.
     
    florianBrn likes this.
  33. KEngelstoft

    KEngelstoft

    Unity Technologies

    Joined:
    Aug 13, 2013
    Posts:
    1,366
    Hi, we are already working on something that can help your use case. In a scenario where you both change prebaked lighting and change geometry topology completely, lightmaps is not necessarily the best solution. We are building a fully probe based solution with the irradiance from the probe cloud getting evaluated per pixel. Swapping geometry and irradiance probe cloud can be done without worrying about UVs.
     
    Rich_A and Thall33 like this.
  34. enhawk

    enhawk

    Joined:
    Aug 22, 2013
    Posts:
    833
    at this point, what would be the difference between this and additive scene loading?

    I think people are after a simple lighting texture swap, so they can do flickering lights or open / close doors, or a night / day bake.
     
    shaktip2014 and Rich_A like this.
  35. Reanimate_L

    Reanimate_L

    Joined:
    Oct 10, 2009
    Posts:
    2,788
    Out of curiosity, is there a way to blend lightprobe sets? Lightmap Switching Tool are literally swap the all of the lighting data but how about blending between 2 lighting data, and in this case only the lightprobes.
     
  36. thefranke

    thefranke

    Unity Technologies

    Joined:
    Jun 20, 2015
    Posts:
    153
    There is no built-in blending tool, however you can load an additional LightProbes object from an asset for instance and do e.g. a linear blend between the probes in the scene and new ones, then overwrite the array with the blended version.
     
  37. Reanimate_L

    Reanimate_L

    Joined:
    Oct 10, 2009
    Posts:
    2,788
    and that's doable in term of performance?
     
  38. Pema-Malling

    Pema-Malling

    Unity Technologies

    Joined:
    Jul 3, 2020
    Posts:
    327
    @Reanimate_L As long as the probe positions are exactly the same between the two sets, I think it should be feasible to do continuous blending how @thefranke suggests, since you shouldn't have to call
    Tetrahedralize()
    . You would probably just do in-place interpolation on
    LightmapSettings.lightProbes.bakedProbes
    .

    Hard to say more without trying it out and profiling.
     
    Reanimate_L likes this.
  39. Reanimate_L

    Reanimate_L

    Joined:
    Oct 10, 2009
    Posts:
    2,788
    Ah i see, that make sense about no need to call Tetraheralize(). I'll do some test
     
  40. soleron

    soleron

    Joined:
    Apr 21, 2013
    Posts:
    582
    Is there any such solution that is artist friendly and let us bake two or more different lightmaps without having to create different scenes or managing hundreds of files?

    (obviously without having to use code...)
     
  41. Pema-Malling

    Pema-Malling

    Unity Technologies

    Joined:
    Jul 3, 2020
    Posts:
    327
    Nothing built-in, unfortunately, though it is in the roadmap. Does the lightmap switcher tool from Laurent H, mentioned in this thread, not suit your needs?
    https://github.com/laurenth-unity/lightmap-switching-tool
     
  42. Eugene-B

    Eugene-B

    Joined:
    Sep 7, 2014
    Posts:
    322
    Hello everybody!

    I just want to say that now there is such a tool in the store. In its development, I went much further and now you can not only blend and switch lightmaps in real time, but also light probes, reflection probes, skybox and any other data of yours. Minimum coding included. This also works with multi-scene.

    The tool is currently available for Built-In, but SRP is on the way.

    It's here - Magic Lightmap Switcher (Built-In)
     
  43. soleron

    soleron

    Joined:
    Apr 21, 2013
    Posts:
    582

    Thank you for the suggestion. The solution is too complicated and error prone. Also too time consuming especially since the lighting is usually taken care of after the level is at a progressed stage and separating static objects etc etc can become too time consuming.

    These solutions may look good in the lab with a small test scene, but not in a real world project.
     
  44. hanisherif1997_unity

    hanisherif1997_unity

    Joined:
    Jun 25, 2021
    Posts:
    14
    Its been almost 7 years and still no in built feature. That's kinda sad.
    This is such an important feature, especially when you develop for mobile platforms. Right now, it seems the only option are the Github project and the Magic lightmap switcher asset as mentioned above
     
    progara likes this.