Hello, i am trying to load Lightmaps and Light Probes from a prefabs at runtime.
I already load lightmaps correctly at Runtime, but i am struggling with light probes, this is what i get from my studies.
-
Lightprobes cannot be instantiated, the game scene must already have a set of lightprobes in the scene and the lightmapdata already generated.
-
Lightprobes can be loaded correctly when the editor is not in playmode, but if i load at runtime the only thing that change is the position of every probe. If i change the harmonics value seems to have no effect.
private void CalculateLightProbes(PrefabLightmapData[] lightmapDatas, string configurationId)
{
var bakedProbes = LightProbes.GetSharedLightProbesForScene(SceneManager.GetActiveScene());
var harmonics = new List<SphericalHarmonicsL2>();
var positions = new List<Vector3>();
foreach (var lmData in lightmapDatas)
{
LightmapConfiguration config = lmData.GetLightmapConfiguration(configurationId);
harmonics.AddRange(config.ProbeData.SphericalHarmonics);
positions.AddRange(config.ProbeData.Positions);
}
for (int i = harmonics.Count; i < bakedProbes.bakedProbes.Length; i++)
{
harmonics.Add(bakedProbes.bakedProbes[i]);
}
for (int i = positions.Count; i < bakedProbes.positions.Length; i++)
{
positions.Add(bakedProbes.positions[i]);
}
LightmapSettings.lightProbes.bakedProbes = harmonics.ToArray();
LightmapSettings.lightProbes.SetPositionsSelf(positions.ToArray(), false);
LightProbes.tetrahedralizationCompleted -= OnTetrahedralizeCompleted;
LightProbes.tetrahedralizationCompleted += OnTetrahedralizeCompleted;
LightProbes.Tetrahedralize();
}
private void OnTetrahedralizeCompleted()
{
Debug.Log("Tetrahedralization completed!");
/*int probeCount = LightmapSettings.lightProbes.count;
for (int i = 0; i < probeCount; i++)
{
// Clear the probe and add ambient light
LightmapSettings.lightProbes.bakedProbes[i].Clear();
LightmapSettings.lightProbes.bakedProbes[i].AddAmbientLight(Color.white);
// Add the contribution of directional and point lights
foreach (Light light in FindObjectsByType<Light>(FindObjectsInactive.Exclude,FindObjectsSortMode.None))
{
if (light.type == LightType.Directional)
{
LightmapSettings.lightProbes.bakedProbes[i].AddDirectionalLight(-light.transform.forward, light.color, light.intensity);
}
else if (light.type == LightType.Point)
{
AddPointLight(LightmapSettings.lightProbes.positions[i], light, ref LightmapSettings.lightProbes.bakedProbes[i]);
}
}
}*/
var probes = LightmapSettings.lightProbes;
_occlusionProbes = new Vector4[probes.count];
LightProbes.CalculateInterpolatedLightAndOcclusionProbes(
probes.positions,
probes.bakedProbes,
_occlusionProbes);
foreach (var renderer in FindObjectsByType<Renderer>(FindObjectsInactive.Exclude, FindObjectsSortMode.None))
{
if (renderer.lightProbeUsage == LightProbeUsage.Off) continue;
renderer.SetPropertyBlock(CreatePropertyBlock(probes.bakedProbes));
renderer.UpdateGIMaterials();
}
OnLightmapLoaded?.Invoke();
}
PrefabLightmapData is a structure that stores baked Lightmaps and Light Probes for a prefab. I read and apply this data at runtime when placing rooms in a procedural dungeon.