Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Question How do i change the shadowmask at runtime?

Discussion in 'Global Illumination' started by LeoOB, Mar 30, 2023.

  1. LeoOB

    LeoOB

    Joined:
    Sep 4, 2017
    Posts:
    1
    Im currently using this script to change my lightmapping data at runtime:
    https://github.com/MahmoudKanbar/Unity-Dynamic-Lightmaps
    I made some changes to it so i can also swap things like ambient color, skyboxes and such, but now im trying to also switch the shadowmasks at runtime.
    Currently i wanna use shadowmasks only for static objects so im using the shadowmask mode in quality settings, and it works well when i switch to the latest lightmap i baked for the scene, but when i try to load the next one its only rendering dynamic shadows for every object in the scene and i see a noticeable increase on the triangle count (going from around 150k to 470k)

    To change the shadowmask i made sure to save its textures to the LightState assets, and to switch lightmaps the method UpdateLightMapsSettings() gets called (also inside the LightState script)

    Code (CSharp):
    1. public void UpdateLightMapsSettings()
    2.         {
    3.             var newLightmapData = new LightmapData[Size];
    4.  
    5.             for (int i = 0; i < Size; i++)
    6.             {
    7.                 newLightmapData[i] = new LightmapData();
    8.                 newLightmapData[i].lightmapColor = colorMaps[i];
    9.                 newLightmapData[i].lightmapDir = directionMaps[i];
    10.                 newLightmapData[i].shadowMask = shadowMasks[i]; //This is the line i added
    11.             }
    12.  
    13.             LightmapSettings.lightmaps = newLightmapData;
    14.             LightmapSettings.lightProbes.bakedProbes = lightProbesData;
    15. ...
    Am i missing something? i thought by adding the shadowmask textures to the new lightmap data they would also get changed
     
  2. Pema-Malling

    Pema-Malling

    Unity Technologies

    Joined:
    Jul 3, 2020
    Posts:
    208
    The code you have looks sane, but this isn't the only data that feeds into shadowmask rendering. There is also Light.lightBakingOutput (https://docs.unity3d.com/ScriptReference/Light-bakingOutput.html). For each light, this struct contains:
    - A flag indicating whether the light has been baked
    - An enum indicating which part of the lights contribution was baked
    - The mixed lighting mode used for the light
    - The channel in the relevant shadowmask to use for the light
    - The channel in the relevant per-probe-occlusion data to use for the light (like shadowmask, but for probes)

    This struct isn't serialized in the editor like most other data on lights - it's ephemeral data which is usually patched into the scene on load or when a bake has finished. Only when building the player is it serialized into the build (iirc).

    Anyways, if you are messing with shadowmasks, you might want to have a look at these values. You might have to patch them up along with the shadowmask texture, depending on your setup.