@@ -52,6 +52,8 @@ public class RendererInfo
52
52
//TODO : enable logs only when verbose enabled
53
53
public bool verbose = false ;
54
54
55
+ static string messagePrefix = "Lightmap Switching Tool - " ;
56
+
55
57
public void LoadLightingScenario ( int index )
56
58
{
57
59
if ( index != currentLightingScenario )
@@ -83,7 +85,7 @@ public void LoadLightingScenario(string name)
83
85
var data = lightingScenariosData . Find ( x => x . name . Equals ( name ) ) ;
84
86
if ( data == null )
85
87
{
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 ) ;
87
89
return ;
88
90
}
89
91
LoadLightingScenario ( data ) ;
@@ -131,14 +133,14 @@ public void LoadLightingScenarioData(LightingScenarioData data)
131
133
public void LoadAssetBundleByName ( string name )
132
134
{
133
135
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" ) ;
135
137
assetBundle . LoadAllAssets ( ) ;
136
138
}
137
139
138
140
public void RefreshLightingScenarios ( )
139
141
{
140
142
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." ) ;
142
144
foreach ( var scene in lightingScenariosData )
143
145
{
144
146
Debug . Log ( scene . name ) ;
@@ -158,7 +160,7 @@ public void OnEnteredPlayMode_EditorOnly()
158
160
if ( LightmapSettings . lightProbes != null )
159
161
{
160
162
cachedBakedProbeData = LightmapSettings . lightProbes . bakedProbes ;
161
- Debug . Log ( "Lightmap swtching tool - Caching editor lightProbes") ;
163
+ Debug . Log ( messagePrefix + " Caching editor lightProbes") ;
162
164
}
163
165
}
164
166
@@ -168,7 +170,7 @@ public void OnExitingPlayMode_EditorOnly()
168
170
if ( cachedBakedProbeData != null && LightmapSettings . lightProbes . bakedProbes . Length == cachedBakedProbeData . Length )
169
171
{
170
172
LightmapSettings . lightProbes . bakedProbes = cachedBakedProbeData ;
171
- Debug . Log ( "Lightmap swtching tool - Restoring editor lightProbes") ;
173
+ Debug . Log ( messagePrefix + " Restoring editor lightProbes") ;
172
174
}
173
175
}
174
176
@@ -204,7 +206,7 @@ LightmapData[] LoadLightmaps(int index)
204
206
if ( lightingScenariosData [ index ] . lightmaps == null
205
207
|| lightingScenariosData [ index ] . lightmaps . Length == 0 )
206
208
{
207
- Debug . LogWarning ( "No lightmaps stored in scenario " + index ) ;
209
+ Debug . LogWarning ( messagePrefix + "No lightmaps stored in scenario " + index ) ;
208
210
return null ;
209
211
}
210
212
@@ -257,54 +259,10 @@ LightmapData[] LoadLightmaps(LightingScenarioData data)
257
259
return newLightmaps ;
258
260
}
259
261
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
-
294
262
public void ApplyDataRendererInfo ( RendererInfo [ ] infos )
295
263
{
296
264
try
297
265
{
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
-
308
266
var hashRendererPairs = new Dictionary < int , RendererInfo > ( ) ;
309
267
310
268
//Fill with lighting scenario to load renderer infos
@@ -321,14 +279,34 @@ public void ApplyDataRendererInfo(RendererInfo[] infos)
321
279
{
322
280
var infoToApply = new RendererInfo ( ) ;
323
281
324
- //int transformHash = render.gameObject.transform.position
325
-
326
282
if ( hashRendererPairs . TryGetValue ( GetStableHash ( render . gameObject . transform ) , out infoToApply ) )
327
283
{
328
284
if ( render . gameObject . name == infoToApply . name )
329
285
{
330
286
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 ;
332
310
}
333
311
}
334
312
}
@@ -340,10 +318,20 @@ public void ApplyDataRendererInfo(RendererInfo[] infos)
340
318
Debug . LogError ( "Error in ApplyDataRendererInfo:" + e . GetType ( ) . ToString ( ) ) ;
341
319
}
342
320
}
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
+ }
343
328
344
329
public void LoadLightProbes ( int index )
345
330
{
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 ) ;
347
335
}
348
336
349
337
public void LoadLightProbes ( LightingScenarioData data )
@@ -389,7 +377,7 @@ public void StoreLightmapInfos(int index)
389
377
newLightingScenarioData . lightingSceneName = lightingScenariosScenes [ index ] . name ;
390
378
#endif
391
379
newLightingScenarioData . geometrySceneName = gameObject . scene . name ;
392
- newLightingScenarioData . storeRendererInfos = applyLightmapScaleAndOffset ;
380
+ newLightingScenarioData . storeRendererInfos = true ;
393
381
394
382
GenerateLightmapInfo ( gameObject , newRendererInfos , newLightmapsTextures , newLightmapsTexturesDir , newLightmapsShadowMasks , newLightmapsMode ) ;
395
383
@@ -443,45 +431,49 @@ public void StoreLightmapInfos(int index)
443
431
444
432
static void GenerateLightmapInfo ( GameObject root , List < RendererInfo > newRendererInfos , List < Texture2D > newLightmapsLight , List < Texture2D > newLightmapsDir , List < Texture2D > newLightmapsShadow , LightmapsMode newLightmapsMode )
445
433
{
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 )
449
436
{
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 )
456
438
{
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 ) ;
460
443
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 ) ;
465
446
if ( terrainRendererInfo . lightmapIndex == - 1 )
466
447
{
467
- terrainRendererInfo . lightmapIndex = newLightmapsDir . Count ;
468
- newLightmapsDir . Add ( lightmapdir ) ;
448
+ terrainRendererInfo . lightmapIndex = newLightmapsLight . Count ;
449
+ newLightmapsLight . Add ( lightmaplight ) ;
469
450
}
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 )
476
453
{
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
+ }
479
461
}
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 ) ;
482
473
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
+ }
485
477
}
486
478
487
479
var renderers = FindObjectsOfType ( typeof ( Renderer ) ) ;
0 commit comments