- Home /
How to add new baked lightmaps to lightmap array?
I want to create several separate set of lightmaps for the same scene using different lighting settings in order to depict different times of day outdoors.
From reading the documentation, it looks like you're supposed to be able to put additional lightmaps into the Map Array and then point to a particular index for a particular renderer or terrain at runtime.
However, whether or not I check Lock Atlas before I bake, my new lightmap textures replace the previous set.
What am I doing wrong?
Answer by DocSWAB · Oct 15, 2010 at 02:30 PM
The lightmap array is actually fixed by the lightmapper based on how many lightmaps are needed in the atlas to cover all the lightmaps at the specified resolution.
What I wanted to do was substitute different lightmap textures in existing array slots at runtime. Here's how:
- Set up your realtime lighting for the time of day you want to represent.
- Bake your lightmaps.
- Rename the lightmap folder (which will be next to the scene file in the Project) to represent the time of day and move it to the Resources folder.
- Change your lighting again.
- Bake again.
- Save your lightmaps off to the side again.
This code will load a lightmap texture set from Resources into the lightmapdata at runtime:
function SetLightMaps (pathString: String) {
// pass in the path to the location of the lightmaps, starting below the "Resources" folder.
var lightmaparray : LightmapData[] = LightmapSettings.lightmaps;
var mapdata : LightmapData = new LightmapData();
for (var i=0; i<lightmaparray.length; i++) {
mapdata.lightmapFar = UnityEngine.Resources.Load(pathString + "LightmapFar-" + i.ToString(), Texture2D) as Texture2D;
mapdata.lightmapNear = UnityEngine.Resources.Load(pathString + "LightmapNear-" + i.ToString(), Texture2D) as Texture2D;
lightmaparray[i] = mapdata;
}
LightmapSettings.lightmaps = lightmaparray;
}
Hi DocSWAB, this script works great but somehow, when the lightmap array is updated via script, then the lightmaps don't come in right (maybe tiling and offset are wrong). Can you please give some more details as, for example, how you bake your maps (e.g. Lock Atlas = on or off) etc? An answer will be hugely appreciated!
The code doesn't show up properly in my browsers, probably due to the migration to the new answers platform? Can someone fix or provide again please?
function SetLight$$anonymous$$aps (pathString: String) { // pass in the path to the location of the lightmaps, starting below the "Resources" folder. var lightmaparray : LightmapData[] = LightmapSettings.lightmaps; var mapdata : LightmapData = new LightmapData(); for (var i=0; i
"LightmapFar-" + i.ToString(), Texture2D) as Texture2D; mapdata.lightmapNear = UnityEngine.Resources.Load(pathString
"LightmapNear-" + i.ToString(), Texture2D) as Texture2D; lightmaparray[i] = mapdata; } LightmapSettings.lightmaps = lightmaparray;
Answer by xxxxxxxx · Aug 26, 2011 at 01:48 PM
function SetLightMaps (pathString: String) { // pass in the path to the location of the lightmaps, starting below the "Resources" folder. var lightmaparray : LightmapData[] = LightmapSettings.lightmaps; var mapdata : LightmapData = new LightmapData(); for (var i=0; i
Hello,
With the code I can load the lightmap from Resources folder, but it appear strange. I think the UVs are incorrect, or somethink like that.
How to fix this?
Thanks.