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

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.

2 Likes

The link below worked in 4.x for me, I’ve not tested it in 5.

http://wiki.unity3d.com/index.php/LightMapSwitcher

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: Unity - Scripting API: LightmapData

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?

1 Like

Bump, same problem. Switched lighmaps dynamically in Unity 4, trying to find out how to do it in Unity 5.

1 Like

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

3 Likes

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

2 Likes

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.

1 Like

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…

1 Like

anybody can upload project sample on this…!!

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?

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

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.

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.

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:2873800--210663--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.

using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using UnityEngine.Rendering;
using System.Collections;

public class LightmapsSwap : MonoBehaviour {
    //GUI dropdown
    public Dropdown dropdown;

    private LightmapData[] Daydata;
    private LightmapData[] Nightdata;

    private LightmapData[][] allLightmaps;

    private Texture[] DayReflections;
    private Texture[] NightReflections;

    private Texture[][] allReflections;

    private ReflectionProbe[] allReflectionProbes;
    //path to the night lightmap and reflection substitudes
    public string nightpath = "";



    // Use this for initialization
    void Start () {
        //ypu can duplicate lightmap folder to break references, move to Resources, and rename back to the scene name
        if (nightpath == "") nightpath = SceneManager.GetActiveScene().name;

        Daydata = LightmapSettings.lightmaps;

        allLightmaps = new LightmapData[2][];
        allLightmaps[0] = Daydata;
     
        Nightdata = new LightmapData[Daydata.Length];

        for (int i = 0; i < Daydata.Length; i++)
        {
            Nightdata[i] = new LightmapData();
            Nightdata[i].lightmapFar = Resources.Load(nightpath + "/" + Daydata[i].lightmapFar.name) as Texture2D;
            Nightdata[i].lightmapNear = Resources.Load(nightpath + "/" + Daydata[i].lightmapNear.name) as Texture2D;
        }

        allLightmaps[1] = Nightdata;

        allReflectionProbes = FindObjectsOfType<ReflectionProbe>();

        DayReflections = new Texture[allReflectionProbes.Length];
        NightReflections = new Texture[allReflectionProbes.Length];

        for (int i = 0; i < allReflectionProbes.Length; i++ )
        {
            DayReflections[i] = allReflectionProbes[i].bakedTexture;
            NightReflections[i] = Resources.Load(nightpath + "/" + DayReflections[i].name) as Texture;
            allReflectionProbes[i].mode = ReflectionProbeMode.Custom;
            allReflectionProbes[i].customBakedTexture = DayReflections[i];
        }

        allReflections = new Texture[2][];
        allReflections[0] = DayReflections;
        allReflections[1] = NightReflections;

        dropdown.onValueChanged.AddListener(delegate
        {
            SwapLightmaps(dropdown.value);
        });
    }

    public void SwapLightmaps (int option) {

        LightmapSettings.lightmaps = allLightmaps[option];
        for (int i = 0; i < allReflectionProbes.Length; i++)
        {
            allReflectionProbes[i].customBakedTexture = allReflections[option][i];
        }
    }
}
5 Likes

Look Awesome

I agree.

What about light probes? any idea?

1 Like

Light probes are not stored in distinctive files like lightmaps or reflection probes, so you would have to serialize and save them from their respective scenes before loading to others. More info in this thread:
https://forum.unity3d.com/threads/how-to-load-lightmaps-in-unity5.311834/#post-2027549

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?

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/” 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).

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 [quote=“MarioRocket, post:19, topic: 577133, username:MarioRocket”]
on really EVERY object?
[/quote]
Not every but any - like in my original post.:slight_smile: 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