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.
     
    afshinunity and AntonioModer like this.
  2. topofsteel

    topofsteel

    Joined:
    Dec 2, 2011
    Posts:
    969
  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:
    312
    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 Unity Technologies

    Joined:
    Apr 12, 2012
    Posts:
    251
    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:
    161
    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,473
    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.
     
    afshinunity 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:
    6
    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:
    6
    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:
    481
    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:
    875
    Look Awesome
     
  16. u3gt6o8

    u3gt6o8

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

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

    tomekkie2

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

    TinyCobra

    Joined:
    Jan 19, 2015
    Posts:
    2
    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:
    481
    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,
     
    trappist-1 likes this.
  24. orkungo

    orkungo

    Joined:
    May 3, 2014
    Posts:
    61
    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:
    61
    Does anyone have any idea how to bundle lightmaps and load them from AssetBundles with LightmapSwitcher?
     
  26. Skelly1983

    Skelly1983

    Joined:
    Mar 9, 2017
    Posts:
    13
    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. afshinunity

    afshinunity

    Joined:
    Apr 26, 2015
    Posts:
    19
    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,109
    It is on our roadmap, but it is still too early to give an ETA.
     
    thelebaron likes this.