Is it possible to change Bakedlightmaps with different types on runtime?

Hi,

I am searching on how baked lightmap system works.
I tried to bake 3 type of light : Baked indirect, subtractive and shadowmask.

Is it possible to load those texture with script?
For example, at trigger 1 the baked indirect will be loaded, at trigger 2 the subtractive will be loaded ( I am thinking of this because it can optimize some scenario).
I recall this is the needed code to load the lightmap

public void loadBakedTexture()
    {
        int length = lightingTextureDir.Length;
        LightmapData[] lightMapList = new LightmapData[length];
        for (int i = 0; i < length; i++)
        {
            LightmapData lightMap = new LightmapData();
            lightMap.lightmapDir = lightingTextureDir[i];
            lightMap.lightmapColor = lightingTextureLight[i];
            lightMapList[i] = lightMap;
        }
        LightmapSettings.lightmaps = lightMapList;
        Lightmapping.lightingDataAsset = lightingDataAsset;
    }

I tried to load the texture and it somehow create saturation effect
Baked indirect


subtractive

Thank you.
Regards.
Yonathan

Hey @yonathankevin !

It is indeed possible to achieve what you need. The piece of the puzzle that you need is the additional data that is stored per light in Light.bakingOutput. Setting this value on your lights, from the bake matching the lightmaps that you’re setting, should make everything come together.

Cheers!

Hello, thank you for the reply.

I managed to change the light settings to baked/indirect/shadowmask.
This is my sample code

private void changeBakedLight(Light light, LightmapBakeType lightmapBakeType, MixedLightingMode mixedLightingMode)
    {
        //Light Baking Output
        //Destroy(light.bakingOutput); //why It can't be destroyed???
        LightBakingOutput lightBakingOutput = new LightBakingOutput();
        lightBakingOutput.isBaked = true;
        lightBakingOutput.lightmapBakeType = lightmapBakeType;
        lightBakingOutput.mixedLightingMode = mixedLightingMode;
        light.lightmapBakeType = lightmapBakeType;
        light.bakingOutput = lightBakingOutput;
        Debug.Log("LightingConfig changeBakedLight Obj " + light.name + " lightmapBakeType : " + lightmapBakeType+ " mixedLightingMode : "+ mixedLightingMode);
    }

public void loadBakedTexture()
    {
        int length = lightingTextureDir.Length;
        LightmapData[] lightMapList = new LightmapData[length];
        for (int i = 0; i < length; i++)
        {
            LightmapData lightMap = new LightmapData();
            lightMap.lightmapDir = lightingTextureDir[i];
            lightMap.lightmapColor = lightingTextureLight[i];
            lightMapList[i] = lightMap;
        }
        LightmapSettings.lightmaps = lightMapList;
        Lightmapping.lightingDataAsset = lightingDataAsset;
        Debug.Log("LightingConfig loadBakedTexture  : " + lightingDataAsset.name + " mixedBakeMode : " + Lightmapping.lightingSettings.mixedBakeMode + " lightmapData Length : " + lightMapList.Length);
    }

But I have more question, why can’t I delete the previous LightBakingOutput lightBakingOutput ?
I tried to call delete but it return error because it’s not an object.
Thank you again in advance

LightBakingOutput is a struct, and therefore not a typical unity object. It simply stores a very small amount of data about the result of the bake for the given light. There isn’t anything to delete, and simply assigning a new one to the light should be sufficient. Is this causing you any troubles?