Skip to content

Commit 22f7d9a

Browse files
author
Laurent
committed
Fix workflow for multiple terrains
replace single slot with an array
1 parent e3a1bbb commit 22f7d9a

File tree

1 file changed

+77
-85
lines changed

1 file changed

+77
-85
lines changed

Packages/LightingTools.LightmapSwitcher/Runtime/LevelLightmapData.cs

+77-85
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ public class RendererInfo
5252
//TODO : enable logs only when verbose enabled
5353
public bool verbose = false;
5454

55+
static string messagePrefix = "Lightmap Switching Tool - ";
56+
5557
public void LoadLightingScenario(int index)
5658
{
5759
if(index != currentLightingScenario)
@@ -83,7 +85,7 @@ public void LoadLightingScenario(string name)
8385
var data = lightingScenariosData.Find(x => x.name.Equals(name));
8486
if(data == null)
8587
{
86-
Debug.LogError("Lightmap switching tool - Can't find lighting scenario with name (case sensitive) " + name);
88+
Debug.LogError(messagePrefix+"Can't find lighting scenario with name (case sensitive) " + name);
8789
return;
8890
}
8991
LoadLightingScenario(data);
@@ -131,14 +133,14 @@ public void LoadLightingScenarioData(LightingScenarioData data)
131133
public void LoadAssetBundleByName(string name)
132134
{
133135
AssetBundle assetBundle = AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/" + name);
134-
Debug.Log(assetBundle == null ? "Lightmap switching tool - Failed to load Asset Bundle" : "Lightmap switching tool - Asset bundle loaded succesfully");
136+
Debug.Log(assetBundle == null ? messagePrefix+"Failed to load Asset Bundle" : "Lightmap switching tool - Asset bundle loaded succesfully");
135137
assetBundle.LoadAllAssets();
136138
}
137139

138140
public void RefreshLightingScenarios()
139141
{
140142
lightingScenariosData = Resources.FindObjectsOfTypeAll<LightingScenarioData>().Where(x => x.geometrySceneName == gameObject.scene.name).ToList();
141-
Debug.Log("Lightmap switching tool - Loaded " + lightingScenariosData.Count + " suitable lighting scenarios.");
143+
Debug.Log(messagePrefix + "Loaded " + lightingScenariosData.Count + " suitable lighting scenarios.");
142144
foreach (var scene in lightingScenariosData)
143145
{
144146
Debug.Log(scene.name);
@@ -158,7 +160,7 @@ public void OnEnteredPlayMode_EditorOnly()
158160
if(LightmapSettings.lightProbes != null)
159161
{
160162
cachedBakedProbeData = LightmapSettings.lightProbes.bakedProbes;
161-
Debug.Log("Lightmap swtching tool - Caching editor lightProbes");
163+
Debug.Log(messagePrefix+"Caching editor lightProbes");
162164
}
163165
}
164166

@@ -168,7 +170,7 @@ public void OnExitingPlayMode_EditorOnly()
168170
if (cachedBakedProbeData != null && LightmapSettings.lightProbes.bakedProbes.Length == cachedBakedProbeData.Length)
169171
{
170172
LightmapSettings.lightProbes.bakedProbes = cachedBakedProbeData;
171-
Debug.Log("Lightmap swtching tool - Restoring editor lightProbes");
173+
Debug.Log(messagePrefix+"Restoring editor lightProbes");
172174
}
173175
}
174176

@@ -204,7 +206,7 @@ LightmapData[] LoadLightmaps(int index)
204206
if (lightingScenariosData[index].lightmaps == null
205207
|| lightingScenariosData[index].lightmaps.Length == 0)
206208
{
207-
Debug.LogWarning("No lightmaps stored in scenario " + index);
209+
Debug.LogWarning( messagePrefix + "No lightmaps stored in scenario " + index);
208210
return null;
209211
}
210212

@@ -257,54 +259,10 @@ LightmapData[] LoadLightmaps(LightingScenarioData data)
257259
return newLightmaps;
258260
}
259261

260-
public void ApplyRendererInfo(RendererInfo[] infos)
261-
{
262-
try
263-
{
264-
//TODO : Fin better solution for terrain. This is not compatible with several terrains.
265-
Terrain terrain = FindObjectOfType<Terrain>();
266-
int i = 0;
267-
if (terrain != null)
268-
{
269-
terrain.lightmapIndex = infos[i].lightmapIndex;
270-
terrain.lightmapScaleOffset = infos[i].lightmapScaleOffset;
271-
i++;
272-
}
273-
274-
for (int j = i; j < infos.Length; j++)
275-
{
276-
RendererInfo info = infos[j];
277-
info.renderer.lightmapIndex = infos[j].lightmapIndex;
278-
if (!info.renderer.isPartOfStaticBatch)
279-
{
280-
info.renderer.lightmapScaleOffset = infos[j].lightmapScaleOffset;
281-
}
282-
if (info.renderer.isPartOfStaticBatch && verbose == true && Application.isEditor)
283-
{
284-
Debug.Log("Object " + info.renderer.gameObject.name + " is part of static batch, skipping lightmap offset and scale.");
285-
}
286-
}
287-
}
288-
catch (Exception e)
289-
{
290-
Debug.LogError("Error in ApplyRendererInfo:" + e.GetType().ToString());
291-
}
292-
}
293-
294262
public void ApplyDataRendererInfo(RendererInfo[] infos)
295263
{
296264
try
297265
{
298-
//TODO : find better way to handle terrain. This doesn't support multiple terrains.
299-
Terrain terrain = FindObjectOfType<Terrain>();
300-
int i = 0;
301-
if (terrain != null)
302-
{
303-
terrain.lightmapIndex = infos[i].lightmapIndex;
304-
terrain.lightmapScaleOffset = infos[i].lightmapScaleOffset;
305-
i++;
306-
}
307-
308266
var hashRendererPairs = new Dictionary<int, RendererInfo>();
309267

310268
//Fill with lighting scenario to load renderer infos
@@ -321,14 +279,34 @@ public void ApplyDataRendererInfo(RendererInfo[] infos)
321279
{
322280
var infoToApply = new RendererInfo();
323281

324-
//int transformHash = render.gameObject.transform.position
325-
326282
if (hashRendererPairs.TryGetValue(GetStableHash(render.gameObject.transform), out infoToApply))
327283
{
328284
if (render.gameObject.name == infoToApply.name)
329285
{
330286
render.lightmapIndex = infoToApply.lightmapIndex;
331-
render.lightmapScaleOffset = infoToApply.lightmapScaleOffset;
287+
if(applyLightmapScaleAndOffset)
288+
render.lightmapScaleOffset = infoToApply.lightmapScaleOffset;
289+
}
290+
}
291+
}
292+
293+
//Find all renderers
294+
var terrains = FindObjectsOfType<Terrain>();
295+
296+
//Apply stored scale and offset if transform and mesh hashes match
297+
foreach (var terrain in terrains)
298+
{
299+
var infoToApply = new RendererInfo();
300+
301+
//int transformHash = render.gameObject.transform.position
302+
303+
if (hashRendererPairs.TryGetValue(GetStableHash(terrain.gameObject.transform), out infoToApply))
304+
{
305+
if (terrain.gameObject.name == infoToApply.name)
306+
{
307+
terrain.lightmapIndex = infoToApply.lightmapIndex;
308+
if (applyLightmapScaleAndOffset)
309+
terrain.lightmapScaleOffset = infoToApply.lightmapScaleOffset;
332310
}
333311
}
334312
}
@@ -340,10 +318,20 @@ public void ApplyDataRendererInfo(RendererInfo[] infos)
340318
Debug.LogError("Error in ApplyDataRendererInfo:" + e.GetType().ToString());
341319
}
342320
}
321+
public void ApplyDataRendererInfo(int index)
322+
{
323+
if (lightingScenariosData[index] != null)
324+
ApplyDataRendererInfo(lightingScenariosData[index].rendererInfos);
325+
else
326+
Debug.LogWarning(messagePrefix + "Trying to load null lighting scenario data at index " + index);
327+
}
343328

344329
public void LoadLightProbes(int index)
345330
{
346-
LoadLightProbes(lightingScenariosData[index]);
331+
if (lightingScenariosData[index] != null)
332+
LoadLightProbes(lightingScenariosData[index]);
333+
else
334+
Debug.LogWarning(messagePrefix + "Trying to load null lighting scenario data at index " + index);
347335
}
348336

349337
public void LoadLightProbes(LightingScenarioData data)
@@ -389,7 +377,7 @@ public void StoreLightmapInfos(int index)
389377
newLightingScenarioData.lightingSceneName = lightingScenariosScenes[index].name;
390378
#endif
391379
newLightingScenarioData.geometrySceneName = gameObject.scene.name;
392-
newLightingScenarioData.storeRendererInfos = applyLightmapScaleAndOffset;
380+
newLightingScenarioData.storeRendererInfos = true;
393381

394382
GenerateLightmapInfo(gameObject, newRendererInfos, newLightmapsTextures, newLightmapsTexturesDir, newLightmapsShadowMasks, newLightmapsMode);
395383

@@ -443,45 +431,49 @@ public void StoreLightmapInfos(int index)
443431

444432
static void GenerateLightmapInfo(GameObject root, List<RendererInfo> newRendererInfos, List<Texture2D> newLightmapsLight, List<Texture2D> newLightmapsDir, List<Texture2D> newLightmapsShadow, LightmapsMode newLightmapsMode)
445433
{
446-
//TODO : Fin better solution for terrain. This is not compatible with several terrains.
447-
Terrain terrain = FindObjectOfType<Terrain>();
448-
if (terrain != null && terrain.lightmapIndex != -1 && terrain.lightmapIndex != 65534)
434+
Terrain[] terrains = FindObjectsOfType<Terrain>();
435+
foreach (Terrain terrain in terrains)
449436
{
450-
RendererInfo terrainRendererInfo = new RendererInfo();
451-
terrainRendererInfo.lightmapScaleOffset = terrain.lightmapScaleOffset;
452-
453-
Texture2D lightmaplight = LightmapSettings.lightmaps[terrain.lightmapIndex].lightmapColor;
454-
terrainRendererInfo.lightmapIndex = newLightmapsLight.IndexOf(lightmaplight);
455-
if (terrainRendererInfo.lightmapIndex == -1)
437+
if (terrain != null && terrain.lightmapIndex != -1 && terrain.lightmapIndex != 65534)
456438
{
457-
terrainRendererInfo.lightmapIndex = newLightmapsLight.Count;
458-
newLightmapsLight.Add(lightmaplight);
459-
}
439+
RendererInfo terrainRendererInfo = new RendererInfo();
440+
terrainRendererInfo.name = terrain.gameObject.name;
441+
terrainRendererInfo.lightmapScaleOffset = terrain.lightmapScaleOffset;
442+
terrainRendererInfo.transformHash = GetStableHash(terrain.gameObject.transform);
460443

461-
if (newLightmapsMode != LightmapsMode.NonDirectional)
462-
{
463-
Texture2D lightmapdir = LightmapSettings.lightmaps[terrain.lightmapIndex].lightmapDir;
464-
terrainRendererInfo.lightmapIndex = newLightmapsDir.IndexOf(lightmapdir);
444+
Texture2D lightmaplight = LightmapSettings.lightmaps[terrain.lightmapIndex].lightmapColor;
445+
terrainRendererInfo.lightmapIndex = newLightmapsLight.IndexOf(lightmaplight);
465446
if (terrainRendererInfo.lightmapIndex == -1)
466447
{
467-
terrainRendererInfo.lightmapIndex = newLightmapsDir.Count;
468-
newLightmapsDir.Add(lightmapdir);
448+
terrainRendererInfo.lightmapIndex = newLightmapsLight.Count;
449+
newLightmapsLight.Add(lightmaplight);
469450
}
470-
}
471-
if (LightmapSettings.lightmaps[terrain.lightmapIndex].shadowMask != null)
472-
{
473-
Texture2D lightmapShadow = LightmapSettings.lightmaps[terrain.lightmapIndex].shadowMask;
474-
terrainRendererInfo.lightmapIndex = newLightmapsShadow.IndexOf(lightmapShadow);
475-
if (terrainRendererInfo.lightmapIndex == -1)
451+
452+
if (newLightmapsMode != LightmapsMode.NonDirectional)
476453
{
477-
terrainRendererInfo.lightmapIndex = newLightmapsShadow.Count;
478-
newLightmapsShadow.Add(lightmapShadow);
454+
Texture2D lightmapdir = LightmapSettings.lightmaps[terrain.lightmapIndex].lightmapDir;
455+
terrainRendererInfo.lightmapIndex = newLightmapsDir.IndexOf(lightmapdir);
456+
if (terrainRendererInfo.lightmapIndex == -1)
457+
{
458+
terrainRendererInfo.lightmapIndex = newLightmapsDir.Count;
459+
newLightmapsDir.Add(lightmapdir);
460+
}
479461
}
480-
}
481-
newRendererInfos.Add(terrainRendererInfo);
462+
if (LightmapSettings.lightmaps[terrain.lightmapIndex].shadowMask != null)
463+
{
464+
Texture2D lightmapShadow = LightmapSettings.lightmaps[terrain.lightmapIndex].shadowMask;
465+
terrainRendererInfo.lightmapIndex = newLightmapsShadow.IndexOf(lightmapShadow);
466+
if (terrainRendererInfo.lightmapIndex == -1)
467+
{
468+
terrainRendererInfo.lightmapIndex = newLightmapsShadow.Count;
469+
newLightmapsShadow.Add(lightmapShadow);
470+
}
471+
}
472+
newRendererInfos.Add(terrainRendererInfo);
482473

483-
if (Application.isEditor)
484-
Debug.Log("Terrain lightmap stored in" + terrainRendererInfo.lightmapIndex.ToString());
474+
if (Application.isEditor)
475+
Debug.Log(messagePrefix + "Terrain lightmap stored in RendererInfo index " + (newRendererInfos.Count - 1));
476+
}
485477
}
486478

487479
var renderers = FindObjectsOfType(typeof(Renderer));

0 commit comments

Comments
 (0)