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â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?
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
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âŚ
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âŚ
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.
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:
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];
}
}
}
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. Any single one.
So I have rebaked now. The nightpath field stays empty in my scene.