Search Unity

Baked Lightmap UV unwrapped at different places between two scenes

Discussion in 'Global Illumination' started by Philip_Zhang, Jan 19, 2020.

  1. Philip_Zhang

    Philip_Zhang

    Joined:
    Feb 15, 2017
    Posts:
    25
    Hi, I want to bake two sets of lightmaps for day/night cycle so I can switch between them at runtime.

    I set up a scene for baking day and another scene for baking night. The two scenes have exactly the same gameObjects, Scale In Lightmap settings, Lightmap Resolution, Lightmap Padding, Lightmap Size. The differences are Directional Light settings, Skybox, some other light sources at night and emission.

    The problem here is some of the objects' UVs are at different places between the two sets of lightmaps, like shown in the picture I attached, one of my stairs unwrapped its UV at a different place, so when I switch lightmap at runtime, it gets the wrong lighting information.

    How can I fix this? Thanks.

    I'm using 2018.4.0f1 with Progressive CPU lightmapper.
     

    Attached Files:

    • Day.png
      Day.png
      File size:
      735.3 KB
      Views:
      403
    • Night.png
      Night.png
      File size:
      873.4 KB
      Views:
      397
  2. rasmusn

    rasmusn

    Unity Technologies

    Joined:
    Nov 23, 2017
    Posts:
    103
    If you actually copied the scenes (including the game objects within them) I don't think you can be sure that the UV layout will necessarily be the same for both scenes. In order to make packing deterministic (for a given scene) Unity sorts the objects in the scene based on several properties. One of the properties is an internal object ID that is guaranteed to be stable for a given scene across Editor instances/executions. But for identical objects in separate scenes, I don't think this guarantee holds. Two equivalent objects may have different object IDs and this may lead to different sorting and thus to different UV atlassing/packing.

    One way to solve this is to have a scene with static geometry only (no lights) and then add this scene to a separate "Day Scene" and also add it a separate "Night Scene". The Day/Night scenes should only contain light information and objects (and a reference to the geometry scene). By doing this, you will only have one copy of the geometry objects and they will always belong to the same scene (the geometry scene) so the internal object IDs remains stable and this should lead to stable sorting (assuming you have no geometry in the Day/Night scenes).

    This approach is inspired by the Lightmap Switcher tool by Laurent Harduin. You may be interested in reading the "How it Works" section of the readme for inspiration.

    I hope this helps :)
     
    kristijonas_unity likes this.
  3. Philip_Zhang

    Philip_Zhang

    Joined:
    Feb 15, 2017
    Posts:
    25
    Cool, this is great! This even saved me from needing to make sure the settings of the geometries in these two scenes are the same all the time. Thank you so much!

     
    rasmusn likes this.