- Home /
Best way to switch between multiple sets of lightmaps
I have a simple scene with 3 light poles. I have baked out 4 sets of light maps from Vray using IES files (3 sets of light maps for each light being on and a 4th with all 3 lights on). I'm looking for a way to create 4 different GUI buttons to control loading the different sets of light maps to simulate turning the different lights on and off. I have very limited experience with scripting and have seen some different topics on this but I am not sure how to implement them or what would be the best way to do this. Any help would be much appreciated!
Answer by ScottYann · Aug 09, 2013 at 03:22 AM
I have some code that you might be able to adapt to your needs. I wrote a coroutine that downloads an assetbundle that contains lightmaps. The problem I had was lightmaps are scene file specific, so you gotta do some janitorial work to get the mess working when importing your own scenery to the same scene.
AssetBundle bundle = www.assetBundle;
GameObject myBackground;
//print ("originalName " + originalName);
if (bundle) {
GameObject background = bundle.Load (originalName, typeof(GameObject)) as GameObject;
//print (background.name);
myBackground = Instantiate (background, background.transform.position, background.transform.rotation) as GameObject;
myBackground.SetActive (true);
myBackground.name = background.name;
if (myBackground.GetComponent<backgroundBehavior> ().LightMapStorage.Length > 0) {
//print ("got this far");
int idx = 0;
Object[] obj = www.assetBundle.LoadAll ();
Texture2D[] LMfarTexture;
Texture2D[] LMnearTexture;
int numberOfPairs = obj.Length;
LMfarTexture = new Texture2D[numberOfPairs];
foreach (Object o in obj) {
if (o.GetType () == typeof(Texture2D)) {
if (o.name.Contains ("Lightmap")) {
LMfarTexture [idx] = o as Texture2D;
idx++;
}
}
}
int i = 0;
LightmapData lmData = new LightmapData ();
LightmapData[] lmDataSet = new LightmapData [numberOfPairs];
foreach (Texture texture in LMfarTexture) {
lmData = new LightmapData ();
lmData.lightmapFar = LMfarTexture [i];
lmDataSet [i] = lmData;
i++;
}
LightmapSettings.lightmapsMode = LightmapsMode.Single;
LightmapSettings.lightmaps = lmDataSet;
}
I needed to make a behavior script to deal with the fact that unity was not voluntarily saving my lightmaps as I exported the assetbundle. The "backgroundBehavior" script looks like this:
/////////////// using UnityEngine; using System.Collections;
public class backgroundBehavior : MonoBehaviour { public bool useSunShafts = false; public Material mySkyBoxMaterial; public Vector3 myPosition; public Texture2D[] LightMapStorage;//just a var where we force Unity to save our Lightmaps when we make an asset bundle public bool usingSpaceConstructionSet = false; public Color ambientColor = Color.black;
// Use this for initialization
void Start ()
{
}
void OnEnable ()
{
RenderSettings.ambientLight = ambientColor;
if (useSunShafts) {
Camera.main.gameObject.GetComponent<SunShafts> ().enabled = true;
Camera.main.gameObject.GetComponent<SunShafts> ().sunTransform = GameObject.Find ("Sun").transform;
} else {
if (Camera.main.gameObject.GetComponent<SunShafts> () != null) {
Camera.main.gameObject.GetComponent<SunShafts> ().enabled = false;
}
}
if (usingSpaceConstructionSet) {
Camera.main.clearFlags = CameraClearFlags.Depth;
} else {
Camera.main.clearFlags = CameraClearFlags.Skybox;
RenderSettings.skybox = mySkyBoxMaterial;
}
}
}
I decided to use single light maps instead of dual. If you want dual you'll have to figure it out on your own as I've never done anything with it.
I hope this helps because this problem had me on a 6 hour google hunt.
Your answer
![](https://koobas.hobune.stream/wayback/20220613111638im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Unity - Stuck in Build 7/11 Light Transport - 1 Job 13 Answers
Why would my baked lights only render for a few seconds? 0 Answers
Can't edit lightmaps. 0 Answers
Lighting Bug/Problem 1 Answer
I have the weirdest lightbaking ever 1 Answer