Search Unity

Bug Renderer.lightmapScaleOffset behaving different in Unity 2021+

Discussion in 'Global Illumination' started by allenwp, May 31, 2023.

  1. allenwp

    allenwp

    Joined:
    Sep 4, 2013
    Posts:
    46
    Hi there,

    I have recently upgraded my project from Unity 2017 to 2021/2022 and it appears that Renderer.lightmapScaleOffset is behaving differently. The project uses Baked NonDirectional Enlighten lightmaps that are loaded to the scene at runtime by setting LightmapSettings.lightmaps with a LightmapData that has the lightmapColor property assigned to the lightmap texture.

    We use the Mobile/Unlit (Supports Lightmap) shader to display these lightmaps. In Unity 2021 and 2022 the lightmap is loaded, but setting the Renderer.lightmapScaleOffset property results in a different part of the lightmap shown on the object than it did in Unity 2017.

    I've inspected the Renderer.lightmapScaleOffset at runtime and it appears that each renderer has this value correctly set, so I'm surprised that the shader is not rendering the correct part of the lightmap texture.

    Any thoughts on why this might have stopped working when moving from Unity 2017 to 2021/2022?
     

    Attached Files:

    Last edited: Jun 1, 2023
  2. allenwp

    allenwp

    Joined:
    Sep 4, 2013
    Posts:
    46
    Well, upon further inspection, it looks like some of the objects are getting the correct part of the lightmap rendered. Specifically, those that have simple UVs, like the UVs of a quad or plane. Objects that have more complex UVs (like a textured cube) have the wrong part of the lightmap rendered. So maybe something has changed with the way that UVs from the object are interpreted when calculating lightmap UVs based on the Renderer.lightmapScaleOffset property?
     
  3. allenwp

    allenwp

    Joined:
    Sep 4, 2013
    Posts:
    46
    In the end, I was able to update our Enlighten lightmapping script to bake the lightmaps in Unity 2022, resulting in different Renderer.lightmapScaleOffset values that seem to work with Unity 2022's interpretation of the object UVs. Not sure why this was needed, but I guess the workaround turned out to be simple.