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

Question Directional light for baking lightmap on multiple scene

Discussion in 'Global Illumination' started by MetaDOS, Feb 7, 2023.

  1. MetaDOS

    MetaDOS

    Joined:
    Nov 10, 2013
    Posts:
    156
    Hi all.

    Because our terrain is too big (4x4km), we have to split them into 5 scenes:
    • 1 scene for the terrain only (we discussed about split the terrain but then it will be complicated to put the object in to the terrain, like which object belong to which scene)
    • 4 scenes for the objects on them, each is 1/4 of the map.
    Now I want to bake the lightmap for them. What I have setup:
    • 1 directional light on first scene
    • All scene use the same Light settings asset
    Here are my questions:
    • Can I bake the lightmap of each scene separately? If yes, how can I deal with the light casting from the object into terrain (since they are in different scene)?
    • Do I have to put the directional light on every scene when baking, and turn them off after I finish?
    • Or I have to open them all in Hierarchy? If yes, my Unity (2022.2.4f1) always tuck at "Preparing bake"
    upload_2023-2-8_5-17-6.png

    Thanks.
     
  2. kristijonas_unity

    kristijonas_unity

    Unity Technologies

    Joined:
    Feb 8, 2018
    Posts:
    1,080
    You can do that. However, one downside of this that scenes which are unloaded will not influence the scene that is currently being baked. You might run into issues with missing shadows/GI.

    I think it would make sense to have a dedicated "Lighting" scene where you'd keep your directional light(s). That scene could remain loaded at all times. Only scenes containing your level geometry would need to be loaded/unloaded during baking.

    You don't need to have them all loaded at once during baking. You can load/unload them prior to baking either manually or via an API.
     
  3. MetaDOS

    MetaDOS

    Joined:
    Nov 10, 2013
    Posts:
    156
    Thank you. I will put the Directional Light on the terrain (first scene) and bake each scene with that terrain (so 2 scene per time) and see the result.
     
  4. gilley033

    gilley033

    Joined:
    Jul 10, 2012
    Posts:
    1,181
    Can you please please please add some way for us to define which additively loaded scenes are included/excluded from a bake? It would be tremendously useful for performing partial bakes when working with large game Worlds that are broken down into small chunks.

    As you say, you have to load all scenes which should contribute to global illumination, or else issues will arise with missing shadows/GI. The ideal solution to this would be to allow scenes to be loaded and contribute to GI, but not actually be included in the baking process (i.e., not have lightmaps baked for them). This would allow you to load a larger area of the game world than just the area that needs to be baked, to ensure there are no light seems, missing shadows, etc.

    I tried to hack my way into a system like this by setting the scaleInLightmap value of all MeshRenderers in the scenes I want to contribute GI but not be baked to 0. This works, but it clears out any existing lightmaps the renderers in those scenes are using, even if you unload the scenes without saving them. I would love an explanation for why that happens.
     
    MetaDOS likes this.
  5. Pema-Malling

    Pema-Malling

    Unity Technologies

    Joined:
    Jul 3, 2020
    Posts:
    307
    I believe your request is covered by a combination of these roadmap items:
    https://portal.productboard.com/uni...cts/c/1247-bake-only-selected-sets-of-objects
    https://portal.productboard.com/uni...-scene-independent-lighting-data-bake-prefabs
    https://portal.productboard.com/uni...s/c/1246-light-baking-management-batch-baking
    You might consider to go vote on them :)


    > I tried to hack my way into a system like this by setting the scaleInLightmap value of all MeshRenderers in the scenes I want to contribute GI but not be baked to 0. This works, but it clears out any existing lightmaps the renderers in those scenes are using, even if you unload the scenes without saving them. I would love an explanation for why that happens.

    References to lighting data aren't stored directly in the scene at all, they are stored in the Lighting Data Asset (https://docs.unity3d.com/Manual/LightmapSnapshot.html) which is a separate asset. The scene only contains a reference to this asset. It then uses the asset when loading a scene in editor, and when building the player, to populate the scene with the actual lighting data.

    When you bake multiple scenes together, you'll get a single Lighting Data Asset for the bake, and any scene can only refer to a single Lighting Data Asset. So when you include those scenes you don't intend to include in the bake, they are still included in the bake, and have their Lighting Data Asset replaced with the one produced by the new bake (which will effectively delete some lightmaps, as you point out). We're aware it's a pretty limited system, which is why intend to rework it - that work is covered by the aforementioned roadmap items.
     
  6. gilley033

    gilley033

    Joined:
    Jul 10, 2012
    Posts:
    1,181
    Thanks so much for your response!

    The first two items you linked are very interesting and would definitely help with my current issue. Fortunately the third is not needed, as I basically have an automated system for performing multiple bakes already. I'm sure it would be very useful for others though.

    Thankfully, I have figured out that the issue with my workflow wasn't that the reference to the Lighting Data asset is replaced in the baked scene, because if you close the scene without saving it, it will revert to its previous Lighting Data asset.

    The issue was that during the bake, Unity iterates over all the open scenes, finds the Lighting Data asset those scenes reference, then removes the data about the scene in the Lighting Data asset itself. I get why it does this and I doubt this behavior will change, however I did manage to find a workaround.

    You simply need to move all of the objects you want to contribute GI out of their original scenes and into a new temporary scene or the main scene, then close the original scenes. This way Unity will not modify the Lighting Data assets referenced by those scenes during the bake! You still need to set the scaleInLightmap values to 0, but that's not a big deal.