Difference between revisions of "How do I..."
(→Alternative solution) |
(→How do I know my Bakery version?) |
||
(34 intermediate revisions by the same user not shown) | |||
Line 13: | Line 13: | ||
You ''should not'' commit: | You ''should not'' commit: | ||
− | * Bakery folders. | + | * Bakery folders (meaning Assets/Bakery and Assets/Editor/x64/Bakery or wherever they were moved). |
Pre-2017.1 versions of Unity: | Pre-2017.1 versions of Unity: | ||
Line 27: | Line 27: | ||
* Bakery -> Utilities -> Save UV padding to asset. | * Bakery -> Utilities -> Save UV padding to asset. | ||
Files named "modelName_padding.asset" will be created. These should be commited to other machines. When importing padding assets, but with models already present in the project, you'll need to click Reimport on them. | Files named "modelName_padding.asset" will be created. These should be commited to other machines. When importing padding assets, but with models already present in the project, you'll need to click Reimport on them. | ||
+ | |||
+ | ==== Required scripts ==== | ||
+ | |||
+ | Make sure all machines have the minimum amount of scripts needed to store/apply the lightmaps. They are listed in the next section. | ||
=== How do I share a scene with someone who doesn’t have Bakery installed? === | === How do I share a scene with someone who doesn’t have Bakery installed? === | ||
Apart from the scene and its lightmaps you also have to include a few scripts, namely: | Apart from the scene and its lightmaps you also have to include a few scripts, namely: | ||
− | * ftLightmaps.cs | + | * Bakery/'''ftLightmaps'''.cs |
− | * ftLightmapsStorage.cs | + | * Bakery/'''ftLightmapsStorage'''.cs |
− | * ftLocalStorage.cs | + | * Bakery/'''ftLocalStorage'''.cs |
− | * ftGlobalStorage.cs | + | * Bakery/'''ftGlobalStorage'''.cs |
− | * ftModelPostProcessor.cs | + | * Bakery/'''BakeryProjectSettings'''.cs (on v1.9+) |
+ | * Editor/x64/Bakery/scripts/'''ftModelPostProcessor'''.cs | ||
+ | * Editor/x64/Bakery/scripts/'''ftSavedPadding2'''.cs (on v1.7+) | ||
=== How do I bake using my own UVs without any alterations to them? === | === How do I bake using my own UVs without any alterations to them? === | ||
Line 75: | Line 81: | ||
3. Plug the file into the Skylight "Sky texture" slot. | 3. Plug the file into the Skylight "Sky texture" slot. | ||
+ | |||
+ | In this scenario, Skylight color must be kept white and intensity set to 1. | ||
+ | |||
+ | === How do I make an object to cast lightmap shadows/GI without having a lightmap on its own? === | ||
+ | * Enable '''Lightmap Static''' (pre-2019.2) or '''Contribute GI''' (2019.2 or newer) on your GameObject. | ||
+ | * Set '''Scale In Lightmap''' to 0 on Mesh Renderer. Alternatively, on newer Unity versions it is possible to set '''Receive Global Illumination''' to '''Light Probes'''. | ||
+ | Object will receive GI via light probes. | ||
+ | |||
+ | === How do I make an object to receive lightmapped lighting without affecting it (not casting shadows or GI)? === | ||
+ | * Enable '''Lightmap Static''' (pre-2019.2) or '''Contribute GI''' (2019.2 or newer) on your GameObject. | ||
+ | * Set '''Cast Shadows''' to Off on Mesh Renderer. In Bakery this will disable GI from this object as well. | ||
+ | |||
+ | === How do I quickly port my existing scene to Bakery? === | ||
+ | Bakery requires its components to be added to every light, but it does not mean you should select every light and add them one by one. Here is a quick way: | ||
+ | * Type '''t:Light''' into your Hierarchy window search box. All objects having a Light component will be shown. | ||
+ | * Select all point and spot lights. It should be easy if they have similar names. Add [[Manual#Bakery_Point_Light|Bakery Point Light]] component. Click "Match lightmapped to real-time". Now all your point and spot lights should have correct settings. | ||
+ | * Select all directional lights (usually there is one). Add [[Manual#Bakery_Direct_Light|Bakery Direct Light]] component. Click "Match lightmapped to real-time". You directional lights are now set up. | ||
+ | * Select all area lights. Add [[Manual#Bakery_Light_Mesh|Bakery Light Mesh]] component. Click "Match lightmapped to area light". You area lights are now OK. | ||
+ | * Create a [[Manual#Bakery_Sky_Light|Skylight]] (Bakery -> Create -> Skylight), then | ||
+ | ** A) You can set Skylight color to the one used in your Lighting settings (environmental lighting color). | ||
+ | ** B) If you used a skybox or a gradient as environment lighting, you can [[How_do_I...#How_do_I_use_Skylight_with_a_procedural_sky_shader.3F|bake it to an HDRI]] | ||
+ | ** C) Just assign an HDRI texture that you like. | ||
+ | |||
+ | Scene setup is now done. | ||
+ | |||
+ | * Before baking, [[Manual#Quickstart|make sure your objects have lightmap UVs]]. | ||
+ | * Open main Bakery window. | ||
+ | * Set [[Manual#Texels_per_unit|Texels Per Unit (see example values)]] to an appropriate value. | ||
+ | * Use [[Manual#Checker_preview|Checker Preview]] to make sure the chosen texel density is good. When Checker Preview is shown, it will also print full lightmap count to console. Make sure this value is sane (<100). | ||
+ | * Set other settings as you like. | ||
+ | * Bake! | ||
+ | |||
+ | === How do I lightmap a large world? === | ||
+ | There is a point at which simply using the same [[Manual#Texels_per_unit|Texels Per Unit]] value for the whole scene becomes unfeasible. Large worlds may contain a huge amount of objects with a different degree of importance and different requirements. Measuring your texel density by the most important/detailed object and applying it to the whole scene can easily take too much memory. | ||
+ | |||
+ | This is the approach I use and recommend: | ||
+ | |||
+ | '''Separating by importance''' | ||
+ | |||
+ | * Separate objects close to your playable area from the ones far away (distant vistas). Put a [[Manual#Bakery_Lightmap_Group_Selector|Lightmap Group]] on distant objects and use lower resolution for it. You can use [[Manual#Checker_preview|Checker Preview]] to make sure that density is good. | ||
+ | * Separate intricate interiors from exteriors. Use a Lightmap Group on interiors, set higher resolution. | ||
+ | |||
+ | '''Separating by directional mode''' | ||
+ | |||
+ | If you use any directional mode: separate objects with normal maps or objects using baked specular from the rest, especially if there are many objects not taking full advantage of directional data. You can disable directional mode globally but override it for a Lightmap Group affecting selected objects. | ||
+ | |||
+ | '''Using per-vertex lightmaps''' | ||
+ | |||
+ | Use [[Vertex_Lightmaps|per-vertex lightmaps]] for the stuff that is hard or inefficient to unwrap. Includes wires, rails, densely tessellated meshes or even just uniformly tessellated meshes placed in smoothly lit areas. Per-vertex LMs often go well with rocks, foliage, sculptures or generally round things. | ||
+ | |||
+ | {{note|Fun fact: everything except buildings and the ground is vertex-baked in this [https://guycalledfrank.itch.io/island island] demo}} | ||
+ | |||
+ | Note that Unity doesn't support using multiple vertex buffers with static batching, so it might be a problem if you rely on it heavily. However, Unity's batching is not particularly performant anyway. If you can combine multiple meshes into one via scripts or manually, you will get better results. It is also possible to generate a proper new mesh that contains per-vertex lighting in its main vertex buffer, thus making it compatible with static batching via [[Community_extensions#Vertex_Lightmap_To_Mesh_Asset|this script]]. | ||
+ | |||
+ | '''The rest''' | ||
+ | |||
+ | After distant, important, directional and per-vertex objects are manually grouped, ideally you should end up with something like: | ||
+ | * Terrains, roads and walls around playable area. Just use global settings to lightmap them. You want shadows to be readable, but you don't need them to be extremely detailed. | ||
+ | * Some props around playable area that don't work well with per-vertex baking. You can: | ||
+ | ** Simply use light probes for them. | ||
+ | ** Lightmap them with global settings. | ||
+ | ** Possibly increase Mesh Renderer -> Scale In Lightmap on them for better quality. | ||
+ | |||
+ | '''LODs''' | ||
+ | |||
+ | Bakery can generate separate lightmaps per-LOD. However, having lots of objects with many LOD levels may increase memory requirements dramatically, both for you and for the player. If you can make your LODs share the UV layout, it is recommended tou use "lightmap stealing" scripts like this: https://github.com/laurenth-personal/LODLightmapScripts | ||
+ | This way all your LOD levels will use just one lightmap. | ||
+ | |||
+ | '''Multiple scenes''' | ||
+ | |||
+ | It is useful to work with some parts of your world in separate scenes, but keep this in mind: | ||
+ | * Baking multiple scenes together takes the same amount of memory as if it was a single large scene. | ||
+ | * Baking multiple scenes separately and them adding them together is entirely possible. At runtime, you can additively load scenes via [https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager.LoadScene.html SceneManager.LoadScene] or [https://docs.unity3d.com/ScriptReference/Application.LoadLevelAdditive.html Application.LoadLevelAdditive]. Obviously such scenes will not shadow or illuminate each other, but before the bake you can place a few surrounding objects [[How_do_I...#How_do_I_make_an_object_to_cast_lightmap_shadows.2FGI_without_having_a_lightmap_on_its_own.3F|set up like this]] or even just primitives to cast shadows or reflect light accordingly. Such objects may "ground" the scene into its surroundings without having the overhead of keeping these fully lightmapped surroundings in memory during the bake. | ||
+ | * Lightmapped Prefabs are not a reasonable way to separate scene chunks. Their main purpose is runtime instantiating and having multiple copies of the same lightmapped object. | ||
+ | |||
+ | '''Sectors''' | ||
+ | |||
+ | Since v1.9 Bakery Sectors can be used to lightmap separate scenes while they cast shadows/GI on each other, read about [[Partial_scene_baking|Partial Scene Baking]]. | ||
+ | |||
+ | |||
+ | '''GI VRAM Optimization ''' | ||
+ | |||
+ | As of today, it is recommended to set this option to [[Manual#GI_VRAM_optimization|Force Off]]. While this technology was meant to save memory on large scenes, it is too error-prone. It will be eventually replaced with a better algorithm. | ||
+ | |||
+ | Note that even if you follow this advice, there is still a limit. You can't lightmap a Skyrim-sized world. Lightmapping has its limitations. | ||
+ | |||
+ | === How do I switch lightmaps at runtime? === | ||
+ | |||
+ | There are some simple scripts and also more complex assets for lightmap switching, for example: | ||
+ | |||
+ | [[Community_extensions#Lightmap_Swapper_.26_Light_Probe_Storage|A simple one]] | ||
+ | |||
+ | [https://assetstore.unity.com/packages/tools/utilities/magic-lightmap-switcher-built-in-srp-196489 A complex one] | ||
+ | |||
+ | In general, if we ignore light probes and smooth blending, baking and switching multiple lightmap sets is actually not that hard. A possible example: | ||
+ | * Change [[Manual#Output_path|Output Path]] to a different folder before each bake; change active lights and render. You will end up with 2 folders having different lightmaps, but using identical UVs. | ||
+ | * After each bake, make a copy of [https://docs.unity3d.com/ScriptReference/LightmapSettings-lightmaps.html LightmapSettings.lightmaps] array and store it somewhere. This is the global Unity lightmap list which all MeshRenderers refer to. | ||
+ | * At runtime, swap these arrays or the data inside them via a script. | ||
+ | |||
+ | === How do I know my Bakery version? === | ||
+ | [[File:Tooltip.png|thumb|Version tooltip]] | ||
+ | Just hover your mouse cursor over the blue text in the main Bakery window. A tooltip with the current version will appear. | ||
+ | |||
+ | If you used the built-in patcher, the latest installed commit ID is in your project folder/bakery-csharp-cver.txt and bakery-compiled-cver.txt. |
Latest revision as of 09:31, 23 April 2024
Contents
- 1 How do I make a lightmapped prefab?
- 2 How do I use git/collab/other version control system with Bakery?
- 3 How do I share a scene with someone who doesn’t have Bakery installed?
- 4 How do I bake using my own UVs without any alterations to them?
- 5 How do I trigger a bake via scripting?
- 6 How do I add fake ambient lighting?
- 7 How do I use Skylight with a procedural sky shader?
- 8 How do I make an object to cast lightmap shadows/GI without having a lightmap on its own?
- 9 How do I make an object to receive lightmapped lighting without affecting it (not casting shadows or GI)?
- 10 How do I quickly port my existing scene to Bakery?
- 11 How do I lightmap a large world?
- 12 How do I switch lightmaps at runtime?
- 13 How do I know my Bakery version?
How do I make a lightmapped prefab?
See Lightmapped Prefab component.
How do I use git/collab/other version control system with Bakery?
First, make sure all machines have the same version of Unity and Bakery.
Unity 2017.1 or newer:
You should commit:
- Any updated assets unrelated to Bakery.
- All updated *.meta files.
- Lightmap textures.
You should not commit:
- Bakery folders (meaning Assets/Bakery and Assets/Editor/x64/Bakery or wherever they were moved).
Pre-2017.1 versions of Unity:
- Also commit ftGlobalStorage.asset.
If UVs don't look right, right click affected models and click Reimport.
Alternative solution
UV padding settings can be saved into a padding asset instead of a meta file. In this case the UVs are fully locked. They will be identical across machines and can't be further adjusted. To create the asset:
- Select all model assets you want to lock UVs for in Project inspector.
- Bakery -> Utilities -> Save UV padding to asset.
Files named "modelName_padding.asset" will be created. These should be commited to other machines. When importing padding assets, but with models already present in the project, you'll need to click Reimport on them.
Required scripts
Make sure all machines have the minimum amount of scripts needed to store/apply the lightmaps. They are listed in the next section.
Apart from the scene and its lightmaps you also have to include a few scripts, namely:
- Bakery/ftLightmaps.cs
- Bakery/ftLightmapsStorage.cs
- Bakery/ftLocalStorage.cs
- Bakery/ftGlobalStorage.cs
- Bakery/BakeryProjectSettings.cs (on v1.9+)
- Editor/x64/Bakery/scripts/ftModelPostProcessor.cs
- Editor/x64/Bakery/scripts/ftSavedPadding2.cs (on v1.7+)
How do I bake using my own UVs without any alterations to them?
Check out Lightmap Groups ("Original UV" mode).
How do I trigger a bake via scripting?
There are 3 steps involved:
1. Get the render settings object:
2. Modify the settings. Full list can be seen in ftLightmapsStorage.cs. As an example, here is how you can limit the amount of bounces and set the output folder:
3. Create Bakery instance, load the settings and bake:
How do I add fake ambient lighting?
In some non-realistic scenes you may want to add a fake ambient factor that doesn't come from natural GI or sky lighting (i.e. a cave that would be pitch black in reality, but has to be visible in a game).
Bakery doesn't have a built-in fake ambient option, but you can emulate it with 6 shadow-less directional lights:
1. Create 6 axis-aligned Direct Lights.
2. Set their Shadow Samples to 0.
3. Set their colors to desired ambient colors (you can use different top/bottom/side lighting if needed).
How do I use Skylight with a procedural sky shader?
Having baked sky lighting match visible skybox is important for correct look. You can bake any sky shader into a cubemap texture and use it in Skylight. Luckily, Update Skybox Probe button already generates that cubemap for you.
1. Click Update Skybox Probe
2. Locate the cubemap file. It will be in your output folder ("BakeryLightmaps" by default), named YouSceneName_0_sky.exr.
3. Plug the file into the Skylight "Sky texture" slot.
In this scenario, Skylight color must be kept white and intensity set to 1.
How do I make an object to cast lightmap shadows/GI without having a lightmap on its own?
- Enable Lightmap Static (pre-2019.2) or Contribute GI (2019.2 or newer) on your GameObject.
- Set Scale In Lightmap to 0 on Mesh Renderer. Alternatively, on newer Unity versions it is possible to set Receive Global Illumination to Light Probes.
Object will receive GI via light probes.
How do I make an object to receive lightmapped lighting without affecting it (not casting shadows or GI)?
- Enable Lightmap Static (pre-2019.2) or Contribute GI (2019.2 or newer) on your GameObject.
- Set Cast Shadows to Off on Mesh Renderer. In Bakery this will disable GI from this object as well.
How do I quickly port my existing scene to Bakery?
Bakery requires its components to be added to every light, but it does not mean you should select every light and add them one by one. Here is a quick way:
- Type t:Light into your Hierarchy window search box. All objects having a Light component will be shown.
- Select all point and spot lights. It should be easy if they have similar names. Add Bakery Point Light component. Click "Match lightmapped to real-time". Now all your point and spot lights should have correct settings.
- Select all directional lights (usually there is one). Add Bakery Direct Light component. Click "Match lightmapped to real-time". You directional lights are now set up.
- Select all area lights. Add Bakery Light Mesh component. Click "Match lightmapped to area light". You area lights are now OK.
- Create a Skylight (Bakery -> Create -> Skylight), then
- A) You can set Skylight color to the one used in your Lighting settings (environmental lighting color).
- B) If you used a skybox or a gradient as environment lighting, you can bake it to an HDRI
- C) Just assign an HDRI texture that you like.
Scene setup is now done.
- Before baking, make sure your objects have lightmap UVs.
- Open main Bakery window.
- Set Texels Per Unit (see example values) to an appropriate value.
- Use Checker Preview to make sure the chosen texel density is good. When Checker Preview is shown, it will also print full lightmap count to console. Make sure this value is sane (<100).
- Set other settings as you like.
- Bake!
How do I lightmap a large world?
There is a point at which simply using the same Texels Per Unit value for the whole scene becomes unfeasible. Large worlds may contain a huge amount of objects with a different degree of importance and different requirements. Measuring your texel density by the most important/detailed object and applying it to the whole scene can easily take too much memory.
This is the approach I use and recommend:
Separating by importance
- Separate objects close to your playable area from the ones far away (distant vistas). Put a Lightmap Group on distant objects and use lower resolution for it. You can use Checker Preview to make sure that density is good.
- Separate intricate interiors from exteriors. Use a Lightmap Group on interiors, set higher resolution.
Separating by directional mode
If you use any directional mode: separate objects with normal maps or objects using baked specular from the rest, especially if there are many objects not taking full advantage of directional data. You can disable directional mode globally but override it for a Lightmap Group affecting selected objects.
Using per-vertex lightmaps
Use per-vertex lightmaps for the stuff that is hard or inefficient to unwrap. Includes wires, rails, densely tessellated meshes or even just uniformly tessellated meshes placed in smoothly lit areas. Per-vertex LMs often go well with rocks, foliage, sculptures or generally round things.
Note that Unity doesn't support using multiple vertex buffers with static batching, so it might be a problem if you rely on it heavily. However, Unity's batching is not particularly performant anyway. If you can combine multiple meshes into one via scripts or manually, you will get better results. It is also possible to generate a proper new mesh that contains per-vertex lighting in its main vertex buffer, thus making it compatible with static batching via this script.
The rest
After distant, important, directional and per-vertex objects are manually grouped, ideally you should end up with something like:
- Terrains, roads and walls around playable area. Just use global settings to lightmap them. You want shadows to be readable, but you don't need them to be extremely detailed.
- Some props around playable area that don't work well with per-vertex baking. You can:
- Simply use light probes for them.
- Lightmap them with global settings.
- Possibly increase Mesh Renderer -> Scale In Lightmap on them for better quality.
LODs
Bakery can generate separate lightmaps per-LOD. However, having lots of objects with many LOD levels may increase memory requirements dramatically, both for you and for the player. If you can make your LODs share the UV layout, it is recommended tou use "lightmap stealing" scripts like this: https://github.com/laurenth-personal/LODLightmapScripts This way all your LOD levels will use just one lightmap.
Multiple scenes
It is useful to work with some parts of your world in separate scenes, but keep this in mind:
- Baking multiple scenes together takes the same amount of memory as if it was a single large scene.
- Baking multiple scenes separately and them adding them together is entirely possible. At runtime, you can additively load scenes via SceneManager.LoadScene or Application.LoadLevelAdditive. Obviously such scenes will not shadow or illuminate each other, but before the bake you can place a few surrounding objects set up like this or even just primitives to cast shadows or reflect light accordingly. Such objects may "ground" the scene into its surroundings without having the overhead of keeping these fully lightmapped surroundings in memory during the bake.
- Lightmapped Prefabs are not a reasonable way to separate scene chunks. Their main purpose is runtime instantiating and having multiple copies of the same lightmapped object.
Sectors
Since v1.9 Bakery Sectors can be used to lightmap separate scenes while they cast shadows/GI on each other, read about Partial Scene Baking.
GI VRAM Optimization
As of today, it is recommended to set this option to Force Off. While this technology was meant to save memory on large scenes, it is too error-prone. It will be eventually replaced with a better algorithm.
Note that even if you follow this advice, there is still a limit. You can't lightmap a Skyrim-sized world. Lightmapping has its limitations.
How do I switch lightmaps at runtime?
There are some simple scripts and also more complex assets for lightmap switching, for example:
In general, if we ignore light probes and smooth blending, baking and switching multiple lightmap sets is actually not that hard. A possible example:
- Change Output Path to a different folder before each bake; change active lights and render. You will end up with 2 folders having different lightmaps, but using identical UVs.
- After each bake, make a copy of LightmapSettings.lightmaps array and store it somewhere. This is the global Unity lightmap list which all MeshRenderers refer to.
- At runtime, swap these arrays or the data inside them via a script.
How do I know my Bakery version?
Just hover your mouse cursor over the blue text in the main Bakery window. A tooltip with the current version will appear.
If you used the built-in patcher, the latest installed commit ID is in your project folder/bakery-csharp-cver.txt and bakery-compiled-cver.txt.