I’m trying to create a system that will allow me to turn baked/mixed lights on and off while affecting the lightmap. My solution is to have one default lightmap where lights that aren’t dynamic get baked into, and for each dynamic light baking a separate lightmap influenced only by that light. When a light is on, just take its lightmap and add it to the default lightmap. Whether it looks accurate or not is completely irrelevant to me. This has been much harder to implement than I originally thought, and searching around has led me to a few solutions, each with their own difficulties and problems. Maybe I’m not using the right keywords?
Solution 1: Using a lightmap switcher.
This yielded very great results, I was able to switch the lightmap, light probes, and reflection probes (I’m only looking for lightmap changes). The only problem is that this is something that is great for large lightmap changes (i.e day/night), but not for what I’m wanting to achieve. In order to achieve the effect I’m looking for, I would need to bake each lighting condition, and this would increase exponentially with each dynamic light added (8 dynamic lights would result in about 65 different lighting conditions, default condition included), and would then need to find out how to tell the lightmap changer which condition it should currently be at.
Solution 2: Combining lightmaps on the GPU via custom shader.
Haven’t attempted to implement this solution, but thinking about its implementation has me doubtful about its effort vs results turnout and how optimal it’ll be. Since light probes likely wont be viable for something like this I was thinking of simply sampling the objects position and transferring it to UV coordinates to sample the lightmap from and setting its ambient color to that color. Since this solution composites the combined lightmap on the GPU, I would have to composite the lightmap per object and sample the color. This seems like a more preferable solution that would get close to the effect I’m looking for, but I would like to get some input before I pour a couple weeks into developing a system like this. I’d likely have to build a custom lightmap shader solution instead of using Unitys.
Solution 3: Combining lightmaps on the CPU via script (the only one I have screenshots and code for).
This one was the most appealing to me, simply combine the lightmaps via script and apply them to the scenes lightmap settings. I wasn’t able to apply the results to the scene lightmap, but I was able to apply it to the materials texture as a test. It combined them, but it also resulted in a much darker lightmap. I’m pretty sure it has to do with using .GetPixels() on lightmaps. This would be a near perfect solution for me besides those two issues.
Lightmap applied as a texture, no combination function.
Ran through combination function, with no texture to add.
Ran through combination function, with one texture added.
Here are the lightmaps


Here is the script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AdditiveLighting : MonoBehaviour
{
[SerializeField]
private Texture2D[] defaultLtMaps;
private Color[] ltMapPixels;
public Texture2D[] additiveLights;
private Texture2D[] finalDest;
private int arrayLength;
public Material testMat;
// Start is called before the first frame update
void Start()
{
//defaultLtMaps = LightmapData.lightmapColor;
testMat.SetTexture("_MainTex", defaultLtMaps[0]);
ltMapPixels = defaultLtMaps[0].GetPixels(0);
arrayLength = ltMapPixels.Length;
}
void Update()
{
if (Input.GetKeyDown("r"))
{
AddLighting();
}
}
private void AddLighting()
{
Color[] pixels = new Color[arrayLength];
Color[] mixedColors = new Color[arrayLength];
foreach (Texture2D dM in defaultLtMaps) {
ltMapPixels = dM.GetPixels(0);
if(additiveLights.Length > 0) {
foreach (Texture2D tx in additiveLights)
{
pixels = tx.GetPixels(0);
for (int i = 0; i < arrayLength; i++)
{
Color c1 = ltMapPixels[i];
Color c2 = pixels[i];
Color result;
result.a = 1;
result.r = (c2.r + c1.r);
result.g = (c2.g + c1.g);
result.b = (c2.b + c1.b);
mixedColors[i] = result;
}
}
} else
{
mixedColors = ltMapPixels;
}
}
Texture2D mixedImage = new Texture2D(256, 256);
mixedImage.SetPixels(mixedColors);
mixedImage.Apply(true);
LightmapData data = new LightmapData();
data.lightmapColor = mixedImage;
testMat.SetTexture("_MainTex", mixedImage);
//LightmapSettings.lightmaps = lightDatas;
}
}
If anyone knows places I can learn more about systems like this, please point me towards them.
I’ve also been thinking whether or not additive combination is also viable for light probes and potentially reflection probes (just isolate each lights contribution to a duplicate light/reflection probe). I have my doubts on if it’ll work or not, but I will read up on light and reflection probes to learn how they function and how to alter them at runtime. Right now I’m more focused on lightmaps.








