Hi,
i need to have a scene with 2 different lightmap (the same scene with and without light) and i need to switch between the lightmaps by c# script.
can anyone help me?
Thanks in advance!
Hi,
i need to have a scene with 2 different lightmap (the same scene with and without light) and i need to switch between the lightmaps by c# script.
can anyone help me?
Thanks in advance!
I found a solution by myself, i poste the code, hope that this can help:
using UnityEngine;
using System.Collections;
public class LightmapController : MonoBehaviour {
LightmapData[] lightmapData;
void Start (){
lightmapData = new LightmapData[1];
}
void setLightmap (int index){
lightmapData[0] = new LightmapData();
lightmapData[0].lightmapFar = Resources.Load( "lightmaps/test/LightmapFar-" + index.ToString(), typeof(Texture2D)) as Texture2D;
LightmapSettings.lightmaps = lightmapData;
}
void Update () {
if (Input.GetKeyDown("t")) setLightmap(0);
if (Input.GetKeyDown("o")) setLightmap(1);
}
}
The way I swap light maps is I have a duplicate scene with different light settings, doing this means you only need to swap the images that change.
m_Index = GetComponent<Renderer>().lightmapIndex;
m_OriginalMap = LightmapSettings.lightmaps[m_Index].lightmapFar;
This is how you can get the current light map data for a renderer (for when you want to swap back).
I couldn’t find any nicer way to access the alternate map than dragging it onto the script as a public variable.
The actual swap code is this:
LightmapData[] data = LightmapSettings.lightmaps;
if(m_UsingOriginal)
data[m_Index].lightmapFar = m_AlternateMap;
else
data[m_Index].lightmapFar = m_OriginalMap;
m_UsingOriginal = (!m_UsingOriginal);
LightmapSettings.lightmaps = data;
My data only included far lightmaps, if yours had near lightmaps I assume you would swap those in the same way.
I would love for someone to come tell me there is a better way but this is how i did it.
Hope this helps.