t1k:unity:rendering:light-baking
| Field | Value |
|---|---|
| Module | rendering |
| Version | 3.1.7 |
| Effort | high |
| Tools | — |
Keywords: global illumination, light baking, lightmap, unity
How to invoke
Section titled “How to invoke”/t1k:unity:rendering:light-bakingUnity Light Baking Skill (DOTS-Safe)
Section titled “Unity Light Baking Skill (DOTS-Safe)”Triggers
Section titled “Triggers”- lightmap, lightmap bake, bake lighting, generate lighting
- Progressive GPU, Progressive CPU, lightmapper
- mixed lighting, baked indirect, IndirectOnly
- light probe baking, lightmap resolution
- ChunkWorldRenderBounds, NaN corruption, invisible entities after baking
Reusable Module
Section titled “Reusable Module”Package: Packages/[your-package]/Editor/LightBakeUtility.cs
Namespace: [YourPackage].Editor
Menu Items
Section titled “Menu Items”| Menu Path | Action |
|---|---|
Tools/[YourPackage]/Bake Lighting (GPU) | Progressive GPU bake with DOTS-safe workflow |
Tools/[YourPackage]/Bake Lighting (CPU) | Progressive CPU fallback |
Tools/[YourPackage]/Cancel Lighting Bake | Cancel in-progress bake |
Tools/[YourPackage]/Clear Entity Scene Cache | Delete Library/EntityScenes/ |
Tools/[YourPackage]/Revert Light to Realtime | Emergency revert + cache clear |
// Full safe bake (sets Mixed, configures, bakes, auto-clears cache on completion)LightBakeUtility.ConfigureAndBake(LightingSettings.Lightmapper.ProgressiveGPU);
// Individual stepsLightBakeUtility.SetDirectionalLightToMixed();LightBakeUtility.ClearEntitySceneCache();LightBakeUtility.RevertDirectionalLightToRealtime();DOTS-Safe Bake Workflow
Section titled “DOTS-Safe Bake Workflow”The Problem
Section titled “The Problem”Unity Entities Graphics stores ChunkWorldRenderBounds as chunk components. When lightmaps are baked with SubScenes closed, the lightmap data cannot be properly converted into SubScene entity data, corrupting bounds with NaN values. NaN propagates through bounds aggregation — entire chunk gets frustum-culled — ALL entities in that chunk become invisible.
⚠️ CRITICAL: SubScene Must Be OPEN During Bake
Section titled “⚠️ CRITICAL: SubScene Must Be OPEN During Bake”Per official Entities Graphics 1.4 docs:
“You will need to bake with subscenes open, upon closing the lightmaps will be converted into the subscene.”
Baking with SubScene closed = NaN corruption in ChunkWorldRenderBounds.
Correct Bake Workflow
Section titled “Correct Bake Workflow”- Open SubScene for editing (click “Edit SubScene” or double-click in Hierarchy)
- Set directional light to
LightmapBakeType.Mixed - Configure lightmapper (Progressive GPU preferred, CPU fallback)
- Bake asynchronously (with SubScene still open)
- On completion: Close SubScene — lightmaps auto-convert into SubScene data
- Clear
Library/EntityScenes/cache (auto-done by LightBakeUtility) - Re-enter Play mode to verify entities visible in Game view
- If broken: Run
Tools/[YourPackage]/Revert Light to Realtime
LightBakeUtility Workflow (automated)
Section titled “LightBakeUtility Workflow (automated)”- Call
LightBakeUtility.ConfigureAndBake()— sets Mixed, configures, bakes, auto-clears cache - User must manually: Open SubScene before baking, close after completion
- Re-enter Play mode to verify
→ See references/lightmapper-config.md for default settings, prerequisites, and troubleshooting.
Companion Module: MeshLightmapHelper
Section titled “Companion Module: MeshLightmapHelper”File: Packages/[your-package]/Editor/MeshLightmapHelper.cs
Reusable UV2 generation for lightmap baking:
// Planar XZ projection for procedural terrain meshes (no UV0)MeshLightmapHelper.GeneratePlanarUV2(mesh);
// Unity Unwrapping for imported meshes (FBX/OBJ with UV0)MeshLightmapHelper.GenerateSecondaryUVs(mesh);
// Auto-process all static children (copies mesh, generates UV2)MeshLightmapHelper.EnsureUV2ForStaticChildren(rootGameObject);LightBakeUtility.EnsureSceneUV2() auto-calls this for ALL static root objects before baking.
Gotchas
Section titled “Gotchas”- ⚠️ CRITICAL: SubScene MUST be OPEN during lightmap bake — Per official docs: “You will need to bake with subscenes open, upon closing the lightmaps will be converted into the subscene.” Baking with SubScene closed corrupts
ChunkWorldRenderBoundswith NaN values in the SCENE FILE itself. ClearingLibrary/EntityScenes/is NOT sufficient — the scene file itself is corrupted. Always open SubScene before baking, close after completion. Recovery (Blocker — destructive op safety): if you hit the corruption, do NOT rawgit checkout -- <scene>.unity— that silently discards any unrelated in-progress work in the same file. Correct sequence: (a)git stash push -m "pre-recover" -- <scene>.unity(preserves your uncommitted work), (b)git checkout HEAD -- <scene>.unity(restores known-good), (c) verify scene loads cleanly in Unity, (d)git stash popif you need to recover non-bake edits. If the file is also dirty in another collaborator’s worktree (rare), confirm with them before anygit checkout. - MCP temp camera bypasses culling —
manage_scene(screenshot)creates a temp camera that doesn’t respect frustum culling. Always verify in Game view, not MCP screenshots - Shadowmask NOT supported by Entities Graphics — use
MixedLightingMode.IndirectOnlyonly - GPU lightmapper needs OpenCL — Falls back to CPU if unavailable (much slower)
- SubScene = 1 lightmap per SubScene — fine for single SubScene setups
- Forward+ has no GBuffer — doesn’t affect lightmap baking, only impostor baking
- Bake with scene saved — Always save scene before baking to prevent data loss on crash
MCP Integration
Section titled “MCP Integration”# Trigger bake via MCPexecute_menu_item("Tools/[YourPackage]/Bake Lighting (GPU)")
# Check progressread_console(filter_text="LightBakeUtility")
# Emergency revertexecute_menu_item("Tools/[YourPackage]/Revert Light to Realtime")
# Verify entity visibility after bakemanage_editor(action="play") # Enter play moderendering_stats(action="get_stats") # Check draw calls→ See references/lightmapper-config.md for troubleshooting steps.