Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

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

Discussion in 'Global Illumination' started by yonathankevin, Feb 9, 2021.

  1. yonathankevin

    yonathankevin

    Joined:
    Aug 6, 2017
    Posts:
    2
    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

    Code (CSharp):
    1. public void loadBakedTexture()
    2.     {
    3.         int length = lightingTextureDir.Length;
    4.         LightmapData[] lightMapList = new LightmapData[length];
    5.         for (int i = 0; i < length; i++)
    6.         {
    7.             LightmapData lightMap = new LightmapData();
    8.             lightMap.lightmapDir = lightingTextureDir[i];
    9.             lightMap.lightmapColor = lightingTextureLight[i];
    10.             lightMapList[i] = lightMap;
    11.         }
    12.         LightmapSettings.lightmaps = lightMapList;
    13.         Lightmapping.lightingDataAsset = lightingDataAsset;
    14.     }
    I tried to load the texture and it somehow create saturation effect
    Baked indirect
    upload_2021-2-9_14-57-18.png
    subtractive
    upload_2021-2-9_14-57-52.png

    Thank you.
    Regards.
    Yonathan
     
  2. Kuba

    Kuba

    Moderator

    Joined:
    Jan 13, 2009
    Posts:
    416
    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!
     
  3. yonathankevin

    yonathankevin

    Joined:
    Aug 6, 2017
    Posts:
    2
    Hello, thank you for the reply.

    I managed to change the light settings to baked/indirect/shadowmask.
    This is my sample code
    Code (CSharp):
    1. private void changeBakedLight(Light light, LightmapBakeType lightmapBakeType, MixedLightingMode mixedLightingMode)
    2.     {
    3.         //Light Baking Output
    4.         //Destroy(light.bakingOutput); //why It can't be destroyed???
    5.         LightBakingOutput lightBakingOutput = new LightBakingOutput();
    6.         lightBakingOutput.isBaked = true;
    7.         lightBakingOutput.lightmapBakeType = lightmapBakeType;
    8.         lightBakingOutput.mixedLightingMode = mixedLightingMode;
    9.         light.lightmapBakeType = lightmapBakeType;
    10.         light.bakingOutput = lightBakingOutput;
    11.         Debug.Log("LightingConfig changeBakedLight Obj " + light.name + " lightmapBakeType : " + lightmapBakeType+ " mixedLightingMode : "+ mixedLightingMode);
    12.     }
    13.  
    14. public void loadBakedTexture()
    15.     {
    16.         int length = lightingTextureDir.Length;
    17.         LightmapData[] lightMapList = new LightmapData[length];
    18.         for (int i = 0; i < length; i++)
    19.         {
    20.             LightmapData lightMap = new LightmapData();
    21.             lightMap.lightmapDir = lightingTextureDir[i];
    22.             lightMap.lightmapColor = lightingTextureLight[i];
    23.             lightMapList[i] = lightMap;
    24.         }
    25.         LightmapSettings.lightmaps = lightMapList;
    26.         Lightmapping.lightingDataAsset = lightingDataAsset;
    27.         Debug.Log("LightingConfig loadBakedTexture  : " + lightingDataAsset.name + " mixedBakeMode : " + Lightmapping.lightingSettings.mixedBakeMode + " lightmapData Length : " + lightMapList.Length);
    28.     }
    29.  
    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
     
  4. Pema-Malling

    Pema-Malling

    Unity Technologies

    Joined:
    Jul 3, 2020
    Posts:
    307
    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?