@@ -19,16 +19,31 @@ public static class ShaderGenerator
19
19
public static void GenerateShader ( string path , ModularShader shader , bool hideVariants = false )
20
20
{
21
21
var modules = FindAllModules ( shader ) ;
22
+
23
+ // Countermeasure for unity dogshit scriptedImporter assets reimport/update system
24
+ var freshAssets = new Dictionary < TemplateAsset , TemplateAsset > ( ) ;
25
+
26
+ freshAssets . AddFreshShaderToList ( shader . ShaderTemplate ) ;
27
+ freshAssets . AddFreshShaderToList ( shader . ShaderPropertiesTemplate ) ;
28
+
29
+ foreach ( var template in modules . SelectMany ( x => x . Templates ) )
30
+ freshAssets . AddFreshShaderToList ( template . Template ) ;
31
+
32
+ foreach ( var function in modules . SelectMany ( x => x . Functions ) )
33
+ freshAssets . AddFreshShaderToList ( function . ShaderFunctionCode ) ;
34
+
35
+
22
36
var possibleVariants = GetShaderVariants ( modules ) ;
23
37
var contexts = new List < ShaderContext > ( ) ;
24
- var completePropertiesBlock = GetPropertiesBlock ( shader , modules ) ;
38
+ var completePropertiesBlock = GetPropertiesBlock ( shader , modules , freshAssets ) ;
25
39
26
40
foreach ( var variant in possibleVariants )
27
41
{
28
42
contexts . Add ( new ShaderContext
29
43
{
30
44
Shader = shader ,
31
45
ActiveEnablers = variant ,
46
+ FreshAssets = freshAssets ,
32
47
FilePath = path ,
33
48
PropertiesBlock = completePropertiesBlock ,
34
49
AreVariantsHidden = true
@@ -120,6 +135,39 @@ public static List<ShaderContext> EnqueueShadersToGenerate(string path, ModularS
120
135
public static void GenerateMinimalShaders ( this List < ShaderContext > contexts )
121
136
{
122
137
if ( contexts == null || contexts . Count == 0 ) return ;
138
+
139
+ // Still Countermeasure for unity dogshit scriptedImporter assets reimport/update system
140
+ var alreadyDoneShaders = new List < ModularShader > ( ) ;
141
+
142
+ var freshAssets = new Dictionary < TemplateAsset , TemplateAsset > ( ) ;
143
+
144
+ foreach ( var context in contexts )
145
+ {
146
+ context . FreshAssets = freshAssets ;
147
+ if ( alreadyDoneShaders . Contains ( context . Shader ) ) continue ;
148
+
149
+ var shader = context . Shader ;
150
+ var modules = FindAllModules ( shader ) ;
151
+
152
+ string assetPath = AssetDatabase . GetAssetPath ( shader . ShaderTemplate ) ;
153
+ freshAssets . Add ( shader . ShaderTemplate , AssetDatabase . LoadAssetAtPath < TemplateAsset > ( assetPath ) ) ;
154
+ assetPath = AssetDatabase . GetAssetPath ( shader . ShaderPropertiesTemplate ) ;
155
+ freshAssets . Add ( shader . ShaderPropertiesTemplate , AssetDatabase . LoadAssetAtPath < TemplateAsset > ( assetPath ) ) ;
156
+
157
+ foreach ( var template in modules . SelectMany ( x => x . Templates ) )
158
+ {
159
+ assetPath = AssetDatabase . GetAssetPath ( template . Template ) ;
160
+ freshAssets . Add ( template . Template , AssetDatabase . LoadAssetAtPath < TemplateAsset > ( assetPath ) ) ;
161
+ }
162
+
163
+ foreach ( var function in modules . SelectMany ( x => x . Functions ) )
164
+ {
165
+ assetPath = AssetDatabase . GetAssetPath ( function . ShaderFunctionCode ) ;
166
+ freshAssets . Add ( function . ShaderFunctionCode , AssetDatabase . LoadAssetAtPath < TemplateAsset > ( assetPath ) ) ;
167
+ }
168
+
169
+ alreadyDoneShaders . Add ( shader ) ;
170
+ }
123
171
124
172
EditorUtility . DisplayProgressBar ( "Generating Optimized Shaders" , "generating shader files" , 1 / ( contexts . Count + 3 ) ) ;
125
173
contexts . AsParallel ( ) . ForAll ( x => x . GenerateShader ( ) ) ;
@@ -236,11 +284,27 @@ public static string GetVariantCode(Dictionary<string, int> activeEnablers)
236
284
237
285
return isAllZeroes ? "" : b . ToString ( ) ;
238
286
}
287
+
288
+ // Loads A new version of the asset into the dictionary if not already available
289
+ private static void AddFreshShaderToList ( this Dictionary < TemplateAsset , TemplateAsset > dictionary , TemplateAsset asset )
290
+ {
291
+ if ( ( object ) asset == null ) return ;
292
+ if ( dictionary . ContainsKey ( asset ) ) return ;
293
+ string assetPath = AssetDatabase . GetAssetPath ( asset ) ;
294
+ dictionary . Add ( asset , AssetDatabase . LoadAssetAtPath < TemplateAsset > ( assetPath ) ) ;
295
+ }
296
+
297
+ // Retrieves the new version of the asset from the dictionary if available
298
+ private static TemplateAsset GetTemplate ( this Dictionary < TemplateAsset , TemplateAsset > dictionary , TemplateAsset asset )
299
+ {
300
+ return dictionary . TryGetValue ( asset , out TemplateAsset result ) ? result : null ;
301
+ }
239
302
240
303
public class ShaderContext
241
304
{
242
305
public ModularShader Shader ;
243
306
public Dictionary < string , int > ActiveEnablers ;
307
+ public Dictionary < TemplateAsset , TemplateAsset > FreshAssets ;
244
308
private List < EnableProperty > _liveUpdateEnablers ;
245
309
public string FilePath ;
246
310
public string VariantFileName ;
@@ -287,7 +351,7 @@ public void GenerateShader()
287
351
288
352
// If the properties block value is empty, assume that the context is generating an optimised shader.
289
353
if ( string . IsNullOrEmpty ( PropertiesBlock ) )
290
- ShaderFile . Append ( GetPropertiesBlock ( Shader , _modules , false ) ) ;
354
+ ShaderFile . Append ( GetPropertiesBlock ( Shader , _modules , FreshAssets , false ) ) ;
291
355
else
292
356
ShaderFile . Append ( PropertiesBlock ) ;
293
357
@@ -363,7 +427,7 @@ private void WriteShaderSkeleton()
363
427
ShaderFile . AppendLine ( "SubShader" ) ;
364
428
ShaderFile . AppendLine ( "{" ) ;
365
429
366
- ShaderFile . AppendLine ( Shader . ShaderTemplate . Template ) ;
430
+ ShaderFile . AppendLine ( FreshAssets . GetTemplate ( Shader . ShaderTemplate ) . Template ) ;
367
431
368
432
Dictionary < ModuleTemplate , ShaderModule > moduleByTemplate = new Dictionary < ModuleTemplate , ShaderModule > ( ) ;
369
433
Dictionary < ( string , string ) , string > convertedKeyword = new Dictionary < ( string , string ) , string > ( ) ;
@@ -377,23 +441,24 @@ private void WriteShaderSkeleton()
377
441
//{
378
442
foreach ( var template in _modules . SelectMany ( x => x . Templates ) . OrderBy ( x => x . Queue ) )
379
443
{
444
+ var freshTemplate = FreshAssets . GetTemplate ( template . Template ) ;
380
445
var module = moduleByTemplate [ template ] ;
381
- if ( template . Template is null ) continue ;
446
+ if ( freshTemplate == null ) continue ;
382
447
bool hasEnabler = module . Enabled != null && ! string . IsNullOrEmpty ( module . Enabled . Name ) ;
383
448
bool isFilteredIn = hasEnabler && ActiveEnablers . TryGetValue ( module . Enabled . Name , out _ ) ;
384
449
bool needsIf = hasEnabler && ! isFilteredIn && ! template . NeedsVariant ;
385
450
var tmp = new StringBuilder ( ) ;
386
451
387
452
if ( ! needsIf )
388
453
{
389
- tmp . AppendLine ( template . Template . ToString ( ) ) ;
454
+ tmp . AppendLine ( freshTemplate . Template ) ;
390
455
}
391
456
392
457
else
393
458
{
394
459
tmp . AppendLine ( $ "if({ module . Enabled . Name } == { module . Enabled . EnableValue } )") ;
395
460
tmp . AppendLine ( "{" ) ;
396
- tmp . AppendLine ( template . Template . ToString ( ) ) ;
461
+ tmp . AppendLine ( freshTemplate . Template ) ;
397
462
tmp . AppendLine ( "}" ) ;
398
463
}
399
464
@@ -480,22 +545,23 @@ private void WriteFunctionsToKeywords()
480
545
481
546
foreach ( ShaderFunction function in _reorderedFunctions )
482
547
{
548
+ var freshAsset = FreshAssets . GetTemplate ( function . ShaderFunctionCode ) ;
483
549
if ( function . CodeKeywords . Count > 0 )
484
550
{
485
551
foreach ( string keyword in function . CodeKeywords )
486
552
{
487
553
if ( ! keywordedCode . ContainsKey ( keyword ) )
488
554
keywordedCode . Add ( keyword , new StringBuilder ( ) ) ;
489
555
490
- keywordedCode [ keyword ] . AppendLine ( function . ShaderFunctionCode . Template ) ;
556
+ keywordedCode [ keyword ] . AppendLine ( freshAsset . Template ) ;
491
557
}
492
558
}
493
559
else
494
560
{
495
561
if ( ! keywordedCode . ContainsKey ( MSSConstants . DEFAULT_CODE_KEYWORD ) )
496
562
keywordedCode . Add ( MSSConstants . DEFAULT_CODE_KEYWORD , new StringBuilder ( ) ) ;
497
563
498
- keywordedCode [ MSSConstants . DEFAULT_CODE_KEYWORD ] . AppendLine ( function . ShaderFunctionCode . Template ) ;
564
+ keywordedCode [ MSSConstants . DEFAULT_CODE_KEYWORD ] . AppendLine ( freshAsset . Template ) ;
499
565
}
500
566
}
501
567
@@ -614,16 +680,17 @@ private static StringBuilder CleanupShaderFile(StringBuilder shaderVariant)
614
680
}
615
681
616
682
// Retrieves properties block based on given modules
617
- private static string GetPropertiesBlock ( ModularShader shader , List < ShaderModule > modules , bool includeEnablers = true )
683
+ private static string GetPropertiesBlock ( ModularShader shader , List < ShaderModule > modules , Dictionary < TemplateAsset , TemplateAsset > freshAssets , bool includeEnablers = true )
618
684
{
619
685
var block = new StringBuilder ( ) ;
620
686
block . AppendLine ( "Properties" ) ;
621
687
block . AppendLine ( "{" ) ;
622
688
623
689
if ( shader . UseTemplatesForProperties )
624
690
{
625
- if ( shader . ShaderPropertiesTemplate != null )
626
- block . AppendLine ( shader . ShaderPropertiesTemplate . Template ) ;
691
+ var freshTemplate = freshAssets . GetTemplate ( shader . ShaderPropertiesTemplate ) ;
692
+ if ( freshTemplate != null )
693
+ block . AppendLine ( freshTemplate . Template ) ;
627
694
628
695
block . AppendLine ( $ "#K#{ MSSConstants . TEMPLATE_PROPERTIES_KEYWORD } ") ;
629
696
}
0 commit comments