1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
|
// Amplify Shader Editor - Visual Shader Editing Tool
// Copyright (c) Amplify Creations, Lda <info@amplify.pt>
using System;
using System.IO;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEditorInternal;
namespace AmplifyShaderEditor
{
public enum AdditionalLineType
{
Include,
Define,
Pragma,
Custom
}
public enum AdditionalContainerOrigin
{
Native,
ShaderFunction,
Custom
}
[Serializable]
public class AdditionalDirectiveContainerSaveItem
{
public AdditionalLineType LineType = AdditionalLineType.Include;
public string LineValue = string.Empty;
public bool GUIDToggle = false;
public string GUIDValue = string.Empty;
public AdditionalContainerOrigin Origin = AdditionalContainerOrigin.Custom;
public AdditionalDirectiveContainerSaveItem( AdditionalLineType lineType, string lineValue, bool guidToggle, string guidValue, AdditionalContainerOrigin origin )
{
LineType = lineType;
LineValue = lineValue;
GUIDToggle = guidToggle;
GUIDValue = guidValue;
Origin = origin;
}
public AdditionalDirectiveContainerSaveItem( AdditionalDirectiveContainer container )
{
LineType = container.LineType;
LineValue = container.LineValue;
GUIDToggle = container.GUIDToggle;
GUIDValue = container.GUIDValue;
Origin = container.Origin;
}
}
[Serializable]
public class AdditionalDirectiveContainer : ScriptableObject
{
public AdditionalLineType LineType = AdditionalLineType.Include;
public string LineValue = string.Empty;
public bool GUIDToggle = false;
public string GUIDValue = string.Empty;
public AdditionalContainerOrigin Origin = AdditionalContainerOrigin.Custom;
public TextAsset LibObject = null;
public string OwnerId = string.Empty;
public void Init( string ownerId, AdditionalDirectiveContainer item )
{
LineType = item.LineType;
LineValue = item.LineValue;
GUIDToggle = item.GUIDToggle;
GUIDValue = item.GUIDValue;
Origin = item.Origin;
LibObject = item.LibObject;
OwnerId = ownerId;
}
public void Init( AdditionalDirectiveContainerSaveItem item )
{
LineType = item.LineType;
LineValue = item.LineValue;
GUIDToggle = item.GUIDToggle;
GUIDValue = item.GUIDValue;
Origin = item.Origin;
if( GUIDToggle )
{
LibObject = AssetDatabase.LoadAssetAtPath<TextAsset>( AssetDatabase.GUIDToAssetPath( GUIDValue ) );
}
}
public void OnDestroy()
{
//Debug.Log( "Destoying directives" );
LibObject = null;
}
public string Value
{
get
{
switch( LineType )
{
case AdditionalLineType.Include:
{
if( GUIDToggle )
{
string shaderPath = AssetDatabase.GUIDToAssetPath( GUIDValue );
if( !string.IsNullOrEmpty( shaderPath ) )
return shaderPath;
}
return LineValue;
}
case AdditionalLineType.Define: return LineValue;
case AdditionalLineType.Pragma: return LineValue;
}
return LineValue;
}
}
public string FormattedValue
{
get
{
switch( LineType )
{
case AdditionalLineType.Include:
{
if( GUIDToggle )
{
string shaderPath = AssetDatabase.GUIDToAssetPath( GUIDValue );
if( !string.IsNullOrEmpty( shaderPath ) )
return string.Format( Constants.IncludeFormat, shaderPath );
}
return string.Format( Constants.IncludeFormat, LineValue );
}
case AdditionalLineType.Define:
return string.Format( Constants.DefineFormat, LineValue );
case AdditionalLineType.Pragma:
return string.Format( Constants.PragmaFormat, LineValue );
}
return LineValue;
}
}
}
public enum ReordableAction
{
None,
Add,
Remove
}
[Serializable]
public sealed class TemplateAdditionalDirectivesHelper : TemplateModuleParent
{
private string NativeFoldoutStr = "Native";
[SerializeField]
private List<AdditionalDirectiveContainer> m_additionalDirectives = new List<AdditionalDirectiveContainer>();
[SerializeField]
private List<AdditionalDirectiveContainer> m_shaderFunctionDirectives = new List<AdditionalDirectiveContainer>();
[SerializeField]
private List<string> m_nativeDirectives = new List<string>();
[SerializeField]
private int m_nativeDirectivesIndex = -1;
[SerializeField]
private bool m_nativeDirectivesFoldout = false;
//ONLY USED BY SHADER FUNCTIONS
// Since AdditionalDirectiveContainer must be a ScriptableObject because of serialization shenanigans it will not serialize the info correctly into the shader function when saving it into a file ( it only saves the id )
// For it to properly work, each AdditionalDirectiveContainer should be added to the SF asset, but that would make it to have children ( which are seen on the project inspector )
// Must revisit this later on and come up with a proper solution
[SerializeField]
private List<AdditionalDirectiveContainerSaveItem> m_directivesSaveItems = new List<AdditionalDirectiveContainerSaveItem>();
private ReordableAction m_actionType = ReordableAction.None;
private int m_actionIndex = 0;
private ReorderableList m_reordableList = null;
private GUIStyle m_propertyAdjustment;
private UndoParentNode m_currOwner;
private Rect m_nativeRect = Rect.zero;
public TemplateAdditionalDirectivesHelper( string moduleName ) : base( moduleName ) { }
//public void AddShaderFunctionItem( AdditionalLineType type, string item )
//{
// UpdateShaderFunctionDictionary();
// string id = type + item;
// if( !m_shaderFunctionDictionary.ContainsKey( id ) )
// {
// AdditionalDirectiveContainer newItem = ScriptableObject.CreateInstance<AdditionalDirectiveContainer>();
// newItem.LineType = type;
// newItem.LineValue = item;
// newItem.hideFlags = HideFlags.HideAndDontSave;
// m_shaderFunctionDirectives.Add( newItem );
// m_shaderFunctionDictionary.Add( id, newItem );
// }
//}
public void AddShaderFunctionItems( string ownerOutputId, List<AdditionalDirectiveContainer> functionList )
{
RemoveShaderFunctionItems( ownerOutputId );
if( functionList.Count > 0 )
{
for( int i = 0; i < functionList.Count; i++ )
{
AdditionalDirectiveContainer item = ScriptableObject.CreateInstance<AdditionalDirectiveContainer>();
item.Init( ownerOutputId, functionList[ i ] );
m_shaderFunctionDirectives.Add( item );
}
}
//if( functionList.Count > 0 )
//{
// m_shaderFunctionDirectives.AddRange( functionList );
//}
}
public void RemoveShaderFunctionItems( string ownerOutputId/*, List<AdditionalDirectiveContainer> functionList */)
{
List<AdditionalDirectiveContainer> list = m_shaderFunctionDirectives.FindAll( ( x ) => x.OwnerId.Equals( ownerOutputId ));
for( int i = 0; i < list.Count; i++ )
{
m_shaderFunctionDirectives.Remove( list[ i ] );
ScriptableObject.DestroyImmediate( list[ i ] );
}
list.Clear();
list = null;
//for( int i = 0; i < functionList.Count; i++ )
//{
// m_shaderFunctionDirectives.Remove( functionList[ i ] );
//}
}
//public void RemoveShaderFunctionItem( AdditionalLineType type, string item )
//{
// m_shaderFunctionDirectives.RemoveAll( x => x.LineType == type && x.LineValue.Equals( item ) );
//}
public void AddItems( AdditionalLineType type, List<string> items )
{
int count = items.Count;
for( int i = 0; i < count; i++ )
{
AdditionalDirectiveContainer newItem = ScriptableObject.CreateInstance<AdditionalDirectiveContainer>();
newItem.LineType = type;
newItem.LineValue = items[ i ];
newItem.hideFlags = HideFlags.HideAndDontSave;
m_additionalDirectives.Add( newItem );
}
UpdateNativeIndex();
}
public void AddNativeContainer()
{
if( m_nativeDirectives.Count > 0 )
{
if( m_additionalDirectives.FindIndex( x => x.Origin.Equals( AdditionalContainerOrigin.Native ) ) == -1 )
{
AdditionalDirectiveContainer newItem = ScriptableObject.CreateInstance<AdditionalDirectiveContainer>();
newItem.Origin = AdditionalContainerOrigin.Native;
newItem.hideFlags = HideFlags.HideAndDontSave;
//m_additionalDirectives.Add( newItem );
//m_nativeDirectivesIndex = m_additionalDirectives.Count - 1;
m_additionalDirectives.Insert( 0, newItem );
m_nativeDirectivesIndex = 0;
}
}
}
public void FillNativeItems( List<string> nativeItems )
{
m_nativeDirectives.Clear();
m_nativeDirectives.AddRange( nativeItems );
AddNativeContainer();
}
void DrawNativeItems()
{
EditorGUILayout.Separator();
EditorGUI.indentLevel++;
int count = m_nativeDirectives.Count;
for( int i = 0; i < count; i++ )
{
EditorGUILayout.LabelField( m_nativeDirectives[ i ] );
}
EditorGUI.indentLevel--;
EditorGUILayout.Separator();
}
void DrawNativeItemsRect()
{
int count = m_nativeDirectives.Count;
m_nativeRect.y += EditorGUIUtility.singleLineHeight;
for( int i = 0; i < count; i++ )
{
EditorGUI.LabelField( m_nativeRect, m_nativeDirectives[ i ] );
m_nativeRect.y += EditorGUIUtility.singleLineHeight;
}
}
void DrawButtons()
{
EditorGUILayout.Separator();
// Add keyword
if( GUILayout.Button( string.Empty, UIUtils.PlusStyle, GUILayout.Width( Constants.PlusMinusButtonLayoutWidth ) ) )
{
AdditionalDirectiveContainer newItem = ScriptableObject.CreateInstance<AdditionalDirectiveContainer>();
newItem.hideFlags = HideFlags.HideAndDontSave;
m_additionalDirectives.Add( newItem );
UpdateNativeIndex();
EditorGUI.FocusTextInControl( null );
m_isDirty = true;
}
//Remove keyword
if( GUILayout.Button( string.Empty, UIUtils.MinusStyle, GUILayout.Width( Constants.PlusMinusButtonLayoutWidth ) ) )
{
if( m_additionalDirectives.Count > 0 )
{
AdditionalDirectiveContainer itemToDelete = m_additionalDirectives[ m_additionalDirectives.Count - 1 ];
m_additionalDirectives.RemoveAt( m_additionalDirectives.Count - 1 );
ScriptableObject.DestroyImmediate( itemToDelete );
EditorGUI.FocusTextInControl( null );
}
m_isDirty = true;
}
}
public override void Draw( UndoParentNode currOwner, bool style = true )
{
m_currOwner = currOwner;
if( m_reordableList == null )
{
m_reordableList = new ReorderableList( m_additionalDirectives, typeof( AdditionalDirectiveContainer ), true, false, false, false )
{
headerHeight = 0,
footerHeight = 0,
showDefaultBackground = false,
elementHeightCallback = ( index ) =>
{
if( m_additionalDirectives[ index ].Origin == AdditionalContainerOrigin.Native && m_nativeDirectivesFoldout )
{
return ( m_nativeDirectives.Count + 1 ) * ( EditorGUIUtility.singleLineHeight ) + 5;
}
return EditorGUIUtility.singleLineHeight + 5;
},
drawElementCallback = ( Rect rect, int index, bool isActive, bool isFocused ) =>
{
if( m_additionalDirectives[ index ].Origin == AdditionalContainerOrigin.Native && m_nativeDirectivesFoldout )
{
rect.height = ( m_nativeDirectives.Count + 1 ) * ( EditorGUIUtility.singleLineHeight ) + 5;
}
if( m_additionalDirectives[ index ] != null )
{
float labelWidthStyleAdjust = 0;
if( style )
{
rect.xMin -= 10;
labelWidthStyleAdjust = 15;
}
else
{
rect.xMin -= 1;
}
float popUpWidth = style ? 75 : 60f;
float widthAdjust = m_additionalDirectives[ index ].LineType == AdditionalLineType.Include ? -14 : 0;
Rect popupPos = new Rect( rect.x, rect.y, popUpWidth, EditorGUIUtility.singleLineHeight );
Rect GUIDTogglePos = m_additionalDirectives[ index ].LineType == AdditionalLineType.Include ? new Rect( rect.x + rect.width - 3 * Constants.PlusMinusButtonLayoutWidth, rect.y, Constants.PlusMinusButtonLayoutWidth, Constants.PlusMinusButtonLayoutWidth ) : new Rect();
Rect buttonPlusPos = new Rect( rect.x + rect.width - 2 * Constants.PlusMinusButtonLayoutWidth, rect.y - 2, Constants.PlusMinusButtonLayoutWidth, Constants.PlusMinusButtonLayoutWidth );
Rect buttonMinusPos = new Rect( rect.x + rect.width - Constants.PlusMinusButtonLayoutWidth, rect.y - 2, Constants.PlusMinusButtonLayoutWidth, Constants.PlusMinusButtonLayoutWidth );
float labelWidthBuffer = EditorGUIUtility.labelWidth;
Rect labelPos = new Rect( rect.x + popupPos.width - labelWidthStyleAdjust, rect.y, labelWidthStyleAdjust + rect.width - popupPos.width - buttonPlusPos.width - buttonMinusPos.width + widthAdjust, EditorGUIUtility.singleLineHeight );
if( m_additionalDirectives[ index ].Origin == AdditionalContainerOrigin.Native )
{
m_nativeRect = rect;
#if UNITY_2019_3_OR_NEWER
m_nativeRect.y -= ( m_nativeRect.height - ( EditorGUIUtility.singleLineHeight + 5 ) ) * 0.5f;
#endif
m_nativeRect.xMin += 2;
m_nativeRect.xMax -= 2;
m_nativeRect.yMax -= 2;
NodeUtils.DrawNestedPropertyGroup( ref m_nativeDirectivesFoldout, rect, NativeFoldoutStr, DrawNativeItemsRect, 4 );
return;
}
m_additionalDirectives[ index ].LineType = (AdditionalLineType)m_currOwner.EditorGUIEnumPopup( popupPos, m_additionalDirectives[ index ].LineType );
if( m_additionalDirectives[ index ].LineType == AdditionalLineType.Include )
{
if( m_additionalDirectives[ index ].GUIDToggle )
{
//if( m_additionalDirectives[ index ].LibObject == null && !string.IsNullOrEmpty( m_additionalDirectives[ index ].GUIDValue ) )
//{
// m_additionalDirectives[ index ].LibObject = AssetDatabase.LoadAssetAtPath<TextAsset>( AssetDatabase.GUIDToAssetPath( m_additionalDirectives[ index ].GUIDValue ) );
//}
EditorGUI.BeginChangeCheck();
TextAsset obj = m_currOwner.EditorGUIObjectField( labelPos, m_additionalDirectives[ index ].LibObject, typeof( TextAsset ), false ) as TextAsset;
if( EditorGUI.EndChangeCheck() )
{
string pathName = AssetDatabase.GetAssetPath( obj );
string extension = Path.GetExtension( pathName );
extension = extension.ToLower();
if( extension.Equals( ".cginc" ) || extension.Equals( ".hlsl" ) )
{
m_additionalDirectives[ index ].LibObject = obj;
m_additionalDirectives[ index ].GUIDValue = AssetDatabase.AssetPathToGUID( pathName );
}
}
}
else
{
m_additionalDirectives[ index ].LineValue = m_currOwner.EditorGUITextField( labelPos, string.Empty, m_additionalDirectives[ index ].LineValue );
}
if( GUI.Button( GUIDTogglePos, m_additionalDirectives[ index ].GUIDToggle ? UIUtils.FloatIntIconOFF : UIUtils.FloatIntIconON, UIUtils.FloatIntPickerONOFF ) )
m_additionalDirectives[ index ].GUIDToggle = !m_additionalDirectives[ index ].GUIDToggle;
}
else
{
m_additionalDirectives[ index ].LineValue = m_currOwner.EditorGUITextField( labelPos, string.Empty, m_additionalDirectives[ index ].LineValue );
}
if( GUI.Button( buttonPlusPos, string.Empty, UIUtils.PlusStyle ) )
{
m_actionType = ReordableAction.Add;
m_actionIndex = index;
}
if( GUI.Button( buttonMinusPos, string.Empty, UIUtils.MinusStyle ) )
{
m_actionType = ReordableAction.Remove;
m_actionIndex = index;
}
}
},
onReorderCallback = ( ReorderableList list ) =>
{
UpdateNativeIndex();
}
};
}
if( m_actionType != ReordableAction.None )
{
switch( m_actionType )
{
case ReordableAction.Add:
{
AdditionalDirectiveContainer newItem = ScriptableObject.CreateInstance<AdditionalDirectiveContainer>();
newItem.hideFlags = HideFlags.HideAndDontSave;
m_additionalDirectives.Insert( m_actionIndex + 1, newItem );
}
break;
case ReordableAction.Remove:
AdditionalDirectiveContainer itemToDelete = m_additionalDirectives[ m_actionIndex ];
m_additionalDirectives.RemoveAt( m_actionIndex );
ScriptableObject.DestroyImmediate( itemToDelete );
break;
}
m_isDirty = true;
m_actionType = ReordableAction.None;
EditorGUI.FocusTextInControl( null );
}
bool foldoutValue = currOwner.ContainerGraph.ParentWindow.InnerWindowVariables.ExpandedAdditionalDirectives;
if( style )
{
NodeUtils.DrawPropertyGroup( ref foldoutValue, m_moduleName, DrawReordableList, DrawButtons );
}
else
{
NodeUtils.DrawNestedPropertyGroup( ref foldoutValue, m_moduleName, DrawReordableList, DrawButtons );
}
currOwner.ContainerGraph.ParentWindow.InnerWindowVariables.ExpandedAdditionalDirectives = foldoutValue;
}
void DrawReordableList()
{
if( m_reordableList != null )
{
if( m_propertyAdjustment == null )
{
m_propertyAdjustment = new GUIStyle();
m_propertyAdjustment.padding.left = 17;
}
//EditorGUILayout.BeginVertical( m_propertyAdjustment );
EditorGUILayout.Space();
if( m_nativeDirectives.Count > 0 )
{
//NodeUtils.DrawNestedPropertyGroup( ref m_nativeDirectivesFoldout, NativeFoldoutStr, DrawNativeItems, 4 );
}
if( m_additionalDirectives.Count == 0 )
{
EditorGUILayout.HelpBox( "Your list is Empty!\nUse the plus button to add one.", MessageType.Info );
}
else
{
m_reordableList.DoLayoutList();
}
EditorGUILayout.Space();
//EditorGUILayout.EndVertical();
}
}
public void AddAllToDataCollector( ref MasterNodeDataCollector dataCollector, TemplateIncludePragmaContainter nativesContainer )
{
//List<AdditionalDirectiveContainer> list = m_additionalDirectives;
//int count = list.FindIndex( x => x.Origin.Equals( AdditionalContainerOrigin.Native ) );
//for( int i = 0; i < count; i++ )
//{
// switch( list[ i ].LineType )
// {
// case AdditionalLineType.Include:
// {
// string value = list[ i ].Value;
// if( !string.IsNullOrEmpty( value ) &&
// !nativesContainer.HasInclude( value ) )
// {
// dataCollector.AddToMisc( list[ i ].FormattedValue );
// }
// }
// break;
// case AdditionalLineType.Define:
// {
// if( !string.IsNullOrEmpty( list[ i ].LineValue ) &&
// !nativesContainer.HasDefine( list[ i ].LineValue ) )
// {
// dataCollector.AddToMisc( list[ i ].FormattedValue );
// }
// }
// break;
// case AdditionalLineType.Pragma:
// {
// if( !string.IsNullOrEmpty( list[ i ].LineValue ) &&
// !nativesContainer.HasPragma( list[ i ].LineValue ) )
// {
// dataCollector.AddToMisc( list[ i ].FormattedValue );
// }
// }
// break;
// default:
// case AdditionalLineType.Custom:
// dataCollector.AddToMisc( list[ i ].LineValue );
// break;
// }
//}
AddToDataCollector( ref dataCollector, nativesContainer, false );
AddToDataCollector( ref dataCollector, nativesContainer, true );
}
public void AddAllToDataCollector( ref MasterNodeDataCollector dataCollector )
{
AddToDataCollector( ref dataCollector, false );
AddToDataCollector( ref dataCollector, true );
}
void AddToDataCollector( ref MasterNodeDataCollector dataCollector, TemplateIncludePragmaContainter nativesContainer, bool fromSF )
{
List<AdditionalDirectiveContainer> list = fromSF ? m_shaderFunctionDirectives : m_additionalDirectives;
int count = list.Count;
for( int i = 0; i < count; i++ )
{
int orderIdx = fromSF ? 1 : ( i > m_nativeDirectivesIndex ? 1 : -1 );
switch( list[ i ].LineType )
{
case AdditionalLineType.Include:
{
string value = list[ i ].Value;
if( !string.IsNullOrEmpty( value ) &&
!nativesContainer.HasInclude( value ) )
{
dataCollector.AddToDirectives( list[ i ].FormattedValue, orderIdx );
}
}
break;
case AdditionalLineType.Define:
{
if( !string.IsNullOrEmpty( list[ i ].LineValue ) &&
!nativesContainer.HasDefine( list[ i ].LineValue ) )
{
dataCollector.AddToDirectives( list[ i ].FormattedValue, orderIdx );
}
}
break;
case AdditionalLineType.Pragma:
{
if( !string.IsNullOrEmpty( list[ i ].LineValue ) &&
!nativesContainer.HasPragma( list[ i ].LineValue ) )
{
dataCollector.AddToDirectives( list[ i ].FormattedValue, orderIdx );
}
}
break;
default:
case AdditionalLineType.Custom:
dataCollector.AddToDirectives( list[ i ].LineValue, orderIdx );
break;
}
}
}
void AddToDataCollector( ref MasterNodeDataCollector dataCollector, bool fromSF )
{
List<AdditionalDirectiveContainer> list = fromSF ? m_shaderFunctionDirectives : m_additionalDirectives;
int orderIdx = 1;
int count = list.Count;
for( int i = 0; i < count; i++ )
{
switch( list[ i ].LineType )
{
case AdditionalLineType.Include:
{
string value = list[ i ].FormattedValue;
if( !string.IsNullOrEmpty( value ) )
{
dataCollector.AddToDirectives( value, orderIdx );
}
}
break;
case AdditionalLineType.Define:
{
if( !string.IsNullOrEmpty( list[ i ].LineValue ) )
{
dataCollector.AddToDirectives( list[ i ].FormattedValue, orderIdx );
}
}
break;
case AdditionalLineType.Pragma:
{
if( !string.IsNullOrEmpty( list[ i ].LineValue ) )
{
dataCollector.AddToDirectives( list[ i ].FormattedValue, orderIdx );
}
}
break;
default:
case AdditionalLineType.Custom:
dataCollector.AddToDirectives( list[ i ].LineValue, orderIdx );
break;
}
}
}
public override void ReadFromString( ref uint index, ref string[] nodeParams )
{
try
{
m_nativeDirectivesIndex = -1;
int count = Convert.ToInt32( nodeParams[ index++ ] );
m_additionalDirectives.Clear();
for( int i = 0; i < count; i++ )
{
AdditionalLineType lineType = (AdditionalLineType)Enum.Parse( typeof( AdditionalLineType ), nodeParams[ index++ ] );
string lineValue = nodeParams[ index++ ];
AdditionalDirectiveContainer newItem = ScriptableObject.CreateInstance<AdditionalDirectiveContainer>();
newItem.hideFlags = HideFlags.HideAndDontSave;
newItem.LineType = lineType;
newItem.LineValue = lineValue.Replace( Constants.SemiColonSeparator, ';' );
if( UIUtils.CurrentShaderVersion() > 15607 )
{
newItem.GUIDToggle = Convert.ToBoolean( nodeParams[ index++ ] );
newItem.GUIDValue = nodeParams[ index++ ];
if( newItem.GUIDToggle )
{
newItem.LibObject = AssetDatabase.LoadAssetAtPath<TextAsset>( AssetDatabase.GUIDToAssetPath( newItem.GUIDValue ) );
if( newItem.LibObject == null )
{
Debug.LogWarning( "Include file not found with GUID " + newItem.GUIDValue );
}
}
}
AdditionalContainerOrigin origin = AdditionalContainerOrigin.Custom;
if( UIUtils.CurrentShaderVersion() > 16902 )
{
origin = (AdditionalContainerOrigin)Enum.Parse( typeof( AdditionalContainerOrigin ), nodeParams[ index++ ] );
newItem.Origin = origin;
}
m_additionalDirectives.Add( newItem );
if( origin == AdditionalContainerOrigin.Native )
{
m_nativeDirectivesIndex = i;
}
}
AddNativeContainer();
}
catch( Exception e )
{
Debug.LogException( e );
}
}
public override void WriteToString( ref string nodeInfo )
{
if( m_additionalDirectives.Count == 1 && m_additionalDirectives[ 0 ].Origin == AdditionalContainerOrigin.Native )
{
IOUtils.AddFieldValueToString( ref nodeInfo, 0 );
return;
}
IOUtils.AddFieldValueToString( ref nodeInfo, m_additionalDirectives.Count );
for( int i = 0; i < m_additionalDirectives.Count; i++ )
{
IOUtils.AddFieldValueToString( ref nodeInfo, m_additionalDirectives[ i ].LineType );
IOUtils.AddFieldValueToString( ref nodeInfo, m_additionalDirectives[ i ].LineValue.Replace( ';', Constants.SemiColonSeparator ) );
IOUtils.AddFieldValueToString( ref nodeInfo, m_additionalDirectives[ i ].GUIDToggle );
IOUtils.AddFieldValueToString( ref nodeInfo, m_additionalDirectives[ i ].GUIDValue );
IOUtils.AddFieldValueToString( ref nodeInfo, m_additionalDirectives[ i ].Origin );
}
}
// read comment on m_directivesSaveItems declaration
public void UpdateSaveItemsFromDirectives()
{
bool foundNull = false;
m_directivesSaveItems.Clear();
for( int i = 0; i < m_additionalDirectives.Count; i++ )
{
if( m_additionalDirectives[ i ] != null )
{
m_directivesSaveItems.Add( new AdditionalDirectiveContainerSaveItem( m_additionalDirectives[ i ] ) );
}
else
{
foundNull = true;
}
}
if( foundNull )
{
m_additionalDirectives.RemoveAll( item => item == null );
}
}
public void CleanNullDirectives()
{
m_additionalDirectives.RemoveAll( item => item == null );
}
public void ResetDirectivesOrigin()
{
for( int i = 0; i < m_directivesSaveItems.Count; i++ )
{
m_directivesSaveItems[ i ].Origin = AdditionalContainerOrigin.Custom;
}
}
// read comment on m_directivesSaveItems declaration
public void UpdateDirectivesFromSaveItems()
{
if( m_directivesSaveItems.Count > 0 )
{
for( int i = 0; i < m_additionalDirectives.Count; i++ )
{
if( m_additionalDirectives[ i ] != null )
ScriptableObject.DestroyImmediate( m_additionalDirectives[ i ] );
}
m_additionalDirectives.Clear();
for( int i = 0; i < m_directivesSaveItems.Count; i++ )
{
AdditionalDirectiveContainer newItem = ScriptableObject.CreateInstance<AdditionalDirectiveContainer>();
newItem.hideFlags = HideFlags.HideAndDontSave;
newItem.Init( m_directivesSaveItems[ i ] );
m_additionalDirectives.Add( newItem );
}
UpdateNativeIndex();
//m_directivesSaveItems.Clear();
}
}
void UpdateNativeIndex()
{
m_nativeDirectivesIndex = -1;
int count = m_additionalDirectives.Count;
for( int i = 0; i < count; i++ )
{
if( m_additionalDirectives[ i ].Origin == AdditionalContainerOrigin.Native )
{
m_nativeDirectivesIndex = i;
break;
}
}
}
public override void Destroy()
{
base.Destroy();
m_nativeDirectives.Clear();
m_nativeDirectives = null;
for( int i = 0; i < m_additionalDirectives.Count; i++ )
{
ScriptableObject.DestroyImmediate( m_additionalDirectives[ i ] );
}
m_additionalDirectives.Clear();
m_additionalDirectives = null;
for( int i = 0; i < m_shaderFunctionDirectives.Count; i++ )
{
ScriptableObject.DestroyImmediate( m_shaderFunctionDirectives[ i ] );
}
m_shaderFunctionDirectives.Clear();
m_shaderFunctionDirectives = null;
m_propertyAdjustment = null;
m_reordableList = null;
}
public List<AdditionalDirectiveContainer> DirectivesList { get { return m_additionalDirectives; } }
public bool IsValid { get { return m_validData; } set { m_validData = value; } }
}
}
|