0
\$\begingroup\$

I am trying to develop a tool for light baking with certain modifications, but I do not understand the difference between the two functions BakeAsync and BakeMultipleScenes.

What is the difference between them if I have one scene or two scenes?

Part of the code that I am using:

private void OnBakeDynamicLightingButtonClicked(ClickEvent evt) { if (SceneManager.sceneCount > 1) { int sceneCount = SceneManager.sceneCount; string[] scenePaths = new string[sceneCount]; for (int i = 0; i < sceneCount; i++) { Scene scene = SceneManager.GetSceneAt(i); if (scene.isLoaded) { scenePaths[i] = scene.path; } } Lightmapping.BakeMultipleScenes(scenePaths); } else { Lightmapping.BakeAsync(); } } 
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

The methods Lightmapping.Bake and Lightmapping.BakeAsync will bake a lightmap for whatever scene is currently loaded.

Lightmapping.BakeMultipleScenes , on the other hand, will load one or more scenes and then bake lightmaps for all of them. As the documentation says:

The function automatically splits baked data by Scene. This means you can bake lightmaps for two adjacent levels and have light and shadows cast onto objects in level A affect objects in level B.

This can be useful if you want to build your game as a modular collection of scenes which are then loaded at runtime via LoadSceneMode.Additive. A potential use-case can be to divide your game environment into chunks which get loaded and unloaded dynamically at runtime to improve performance and reduce memory consumption. Each chunk can then be a separate scene with a baked lightmap. By generating the lightmaps for all of these scenes together via BakeMultipleScenes, you can ensure that a light-cone generated by a baked point-light doesn't get cut off when it crosses a chunk boundary.

\$\endgroup\$
3
  • \$\begingroup\$ Thank you for your answer, but when I press Generate Lighting button in the Lighting window, will Lightmapping.BakeAsync or Lightmapping.BakeMultipleScenes be called? In case I have more than one scene. \$\endgroup\$ Commented Feb 4 at 16:29
  • \$\begingroup\$ @AhmedDyaa The Unity editor doesn't need to go through the same script bindings as your editor scripts do. But as you can easily see when you just try it out, the "Generate Lighting" button will generate one lightmap for each open scene that also accounts for lights in other scenes. So the behavior is consistent with BakeMultipleScenes. \$\endgroup\$ Commented Feb 4 at 16:45
  • \$\begingroup\$ What confuses me is that I tried both methods (BakeAsync and BakeMultipleScenes) and compared them with the result I get when I press the Generate Lighting button. However, what I noticed is that in the case of BakeMultipleScenes, saving the scenes is required, but this does not happen when I press Generate Lighting. \$\endgroup\$ Commented Feb 4 at 19:09

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.