Skip to content

Commit bfe1703

Browse files
svc-reach-platform-supportEvergreen
authored and
Evergreen
committed
[Port] [6000.0] UUM-78573: "Bake Probe Volumes" button does not store that lights are baked
https://jira.unity3d.com/browse/UUM-78573 This fix addresses a bug that caused the various buttons for baking only Adaptive Probe Volumes to malfunction - they would update the lights in the scene to act as baked, to avoid double-contribution from realtime lighting, but would do so in a way that wasn't persistent across scene reloads. The root cause is not properly respecting the Lighting Data Asset (LDA). The baked state of each light is stored in the LDA, _not_ on the lights themselves. Changing only the state on the lights themselves will not make a persistent change, and reloading the scene will reload the old unmodified data. To make the change persistent, we need to modify the LDA. To fix this, I've taken an approach similar to what we do with reflection probes, which have the same issue: - When you bake only probe volumes, and the scene has no LDA, we create a new empty LDA, assign it to the scene, and fill in the up-to-date states. - When you bake only probes volumes, and the scene already has a LDA, we modify this LDA to contain up-to-date states for each light. I had to add a few pieces of public API to achieve this.
1 parent 7df184c commit bfe1703

File tree

3 files changed

+63
-6
lines changed

3 files changed

+63
-6
lines changed

Packages/com.unity.render-pipelines.core/Editor/Lighting/ProbeVolume/ProbeGIBaking.LightTransport.cs

+58-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.IO;
34
using System.Runtime.CompilerServices;
45
using Unity.Collections;
56
using UnityEditor;
@@ -8,7 +9,7 @@
89
using UnityEngine.LightTransport.PostProcessing;
910
using UnityEngine.Rendering.Sampling;
1011
using UnityEngine.Rendering.UnifiedRayTracing;
11-
12+
using UnityEngine.SceneManagement;
1213
using TouchupVolumeWithBoundsList = System.Collections.Generic.List<(UnityEngine.Rendering.ProbeReferenceVolume.Volume obb, UnityEngine.Bounds aabb, UnityEngine.Rendering.ProbeAdjustmentVolume volume)>;
1314

1415
namespace UnityEngine.Rendering
@@ -584,13 +585,16 @@ public void Dispose()
584585
}
585586
}
586587

588+
// The contribution from all Baked and Mixed lights in the scene should be disabled to avoid double contribution.
587589
static void UpdateLightStatus()
588590
{
589591
var lightingSettings = ProbeVolumeLightingTab.GetLightingSettings();
590592

591-
// The contribution from all Baked and Mixed lights in the scene should be disabled to avoid double contribution.
592-
var lights = Object.FindObjectsByType<Light>(FindObjectsSortMode.None);
593-
foreach (var light in lights)
593+
var sceneLights = new Dictionary<Scene, List<Light>>();
594+
595+
// Modify each baked light, take note of which scenes they belong to.
596+
var allLights = Object.FindObjectsByType<Light>(FindObjectsSortMode.None);
597+
foreach (var light in allLights)
594598
{
595599
if (light.lightmapBakeType != LightmapBakeType.Realtime)
596600
{
@@ -600,6 +604,56 @@ static void UpdateLightStatus()
600604
bakingOutput.mixedLightingMode = lightingSettings.mixedBakeMode;
601605
light.bakingOutput = bakingOutput;
602606
}
607+
608+
// Take note of the lights from each scene
609+
var scene = light.gameObject.scene;
610+
if (!sceneLights.TryGetValue(scene, out var sceneLightList))
611+
{
612+
sceneLightList = new List<Light>();
613+
sceneLights.Add(scene, sceneLightList);
614+
}
615+
sceneLightList.Add(light);
616+
}
617+
618+
// Now we make the modifications persistent by modifying Lighting Data Assets (LDA) on disk.
619+
string ldaFolderPath = Path.GetDirectoryName(AssetDatabase.GetAssetPath(m_BakingSet));
620+
for (int i = 0; i < m_BakingSet.sceneGUIDs.Count; i++)
621+
{
622+
string guid = m_BakingSet.sceneGUIDs[i];
623+
Scene scene = SceneManager.GetSceneByPath(AssetDatabase.GUIDToAssetPath(guid));
624+
if (!scene.isLoaded)
625+
continue;
626+
627+
LightingDataAsset prevLDA = Lightmapping.GetLightingDataAssetForScene(scene);
628+
LightingDataAsset newLDA = prevLDA;
629+
630+
// If the scene has no (modifiable) LDA, create a new one.
631+
bool isDefaultLDA = prevLDA && prevLDA.hideFlags.HasFlag(HideFlags.NotEditable);
632+
if (prevLDA == null || isDefaultLDA)
633+
{
634+
newLDA = new LightingDataAsset(scene);
635+
}
636+
637+
// Update the LDA with the new light settings
638+
if (sceneLights.TryGetValue(scene, out var lights))
639+
newLDA.SetLights(lights.ToArray());
640+
else
641+
newLDA.SetLights(Array.Empty<Light>());
642+
643+
// If the scene was using the builtin/default LDA before, copy over environment lighting, so it doesn't change.
644+
if (prevLDA != null)
645+
{
646+
newLDA.SetAmbientProbe(prevLDA.GetAmbientProbe());
647+
newLDA.SetDefaultReflectionCubemap(prevLDA.GetDefaultReflectionCubemap());
648+
}
649+
650+
// Save the LDA to disk and assign it to the scene.
651+
if (newLDA != prevLDA)
652+
{
653+
string ldaPath = $"{ldaFolderPath}/LightingData-{i}.asset".Replace('\\', '/');
654+
AssetDatabase.CreateAsset(newLDA, ldaPath);
655+
Lightmapping.SetLightingDataAssetForScene(scene, newLDA);
656+
}
603657
}
604658
}
605659

Packages/com.unity.render-pipelines.core/Editor/Lighting/ProbeVolume/ProbeGIBaking.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1234,7 +1234,7 @@ static void FixSeams(NativeArray<int> positionRemap, NativeArray<Vector3> positi
12341234
// the dilation process consits in doing a trilinear sample of the higher subdivision brick and override the lower subdiv with that
12351235
// We have to mark the probes on the boundary as valid otherwise leak reduction at runtime will interfere with this method
12361236

1237-
1237+
12381238
// Use an indirection structure to ensure mem usage stays reasonable
12391239
VoxelToBrickCache cache = new VoxelToBrickCache();
12401240

Packages/com.unity.render-pipelines.core/Editor/Lighting/ProbeVolume/ProbeVolumeLightingTab.cs

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Linq;
33
using System.Reflection;
44
using System.Collections.Generic;
5+
using System.IO;
56
using UnityEditor;
67
using UnityEditor.Rendering;
78
using UnityEditor.SceneManagement;
@@ -510,7 +511,9 @@ void SaveTempBakingSetIfNeeded()
510511
string path = string.IsNullOrEmpty(scene.path) ?
511512
ProbeVolumeBakingSet.GetDirectory("Assets/", "Untitled") :
512513
ProbeVolumeBakingSet.GetDirectory(scene.path, scene.name);
513-
path = System.IO.Path.Combine(path, activeSet.name + ".asset");
514+
if (!Directory.Exists(path))
515+
Directory.CreateDirectory(path);
516+
path = Path.Combine(path, activeSet.name + ".asset");
514517
path = AssetDatabase.GenerateUniqueAssetPath(path);
515518

516519
AssetDatabase.CreateAsset(activeSet, path);

0 commit comments

Comments
 (0)