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
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
|
// Amplify Shader Editor - Visual Shader vEditing Tool
// Copyright (c) Amplify Creations, Lda <info@amplify.pt>
using UnityEngine;
using UnityEditor;
using System;
namespace AmplifyShaderEditor
{
[Serializable]
[NodeAttributes( "Static Switch", "Logical Operators", "Creates a shader keyword toggle", Available = true )]
public sealed class StaticSwitch : PropertyNode
{
private float InstanceIconWidth = 19;
private float InstanceIconHeight = 19;
private readonly Color ReferenceHeaderColor = new Color( 0f, 0.5f, 0.585f, 1.0f );
[SerializeField]
private int m_defaultValue = 0;
[SerializeField]
private int m_materialValue = 0;
[SerializeField]
private int m_multiCompile = 0;
[SerializeField]
private int m_currentKeywordId = 0;
[SerializeField]
private string m_currentKeyword = string.Empty;
[SerializeField]
private bool m_createToggle = true;
private const string IsLocalStr = "Is Local";
#if UNITY_2019_1_OR_NEWER
[SerializeField]
private bool m_isLocal = true;
#else
[SerializeField]
private bool m_isLocal = false;
#endif
private GUIContent m_checkContent;
private GUIContent m_popContent;
private int m_conditionId = -1;
private const int MinComboSize = 50;
private const int MaxComboSize = 105;
private Rect m_varRect;
private Rect m_imgRect;
private bool m_editing;
public enum KeywordModeType
{
Toggle = 0,
ToggleOff,
KeywordEnum,
}
public enum StaticSwitchVariableMode
{
Create = 0,
Fetch,
Reference
}
[SerializeField]
private KeywordModeType m_keywordModeType = KeywordModeType.Toggle;
[SerializeField]
private StaticSwitch m_reference = null;
private const string StaticSwitchStr = "Static Switch";
private const string MaterialToggleStr = "Material Toggle";
private const string ToggleMaterialValueStr = "Material Value";
private const string ToggleDefaultValueStr = "Default Value";
private const string AmountStr = "Amount";
private const string KeywordStr = "Keyword";
private const string CustomStr = "Custom";
private const string ToggleTypeStr = "Toggle Type";
private const string TypeStr = "Type";
private const string ModeStr = "Mode";
private const string KeywordTypeStr = "Keyword Type";
private const string KeywordNameStr = "Keyword Name";
public readonly static string[] KeywordTypeList = { "Shader Feature", "Multi Compile"/*, "Define Symbol"*/ };
public readonly static int[] KeywordTypeInt = { 0, 1/*, 2*/ };
[SerializeField]
private string[] m_defaultKeywordNames = { "Key0", "Key1", "Key2", "Key3", "Key4", "Key5", "Key6", "Key7", "Key8" };
[SerializeField]
private string[] m_keywordEnumList = { "Key0", "Key1" };
[SerializeField]
private StaticSwitchVariableMode m_staticSwitchVarMode = StaticSwitchVariableMode.Create;
[SerializeField]
private int m_referenceArrayId = -1;
[SerializeField]
private int m_referenceNodeId = -1;
private int m_keywordEnumAmount = 2;
private bool m_isStaticSwitchDirty = false;
private Rect m_iconPos;
protected override void CommonInit( int uniqueId )
{
base.CommonInit( uniqueId );
AddOutputPort( WirePortDataType.FLOAT, Constants.EmptyPortValue );
AddInputPort( WirePortDataType.FLOAT, false, "False", -1, MasterNodePortCategory.Fragment, 1 );
AddInputPort( WirePortDataType.FLOAT, false, "True", -1, MasterNodePortCategory.Fragment, 0 );
for( int i = 2; i < 9; i++ )
{
AddInputPort( WirePortDataType.FLOAT, false, m_defaultKeywordNames[ i ] );
m_inputPorts[ i ].Visible = false;
}
m_headerColor = new Color( 0.0f, 0.55f, 0.45f, 1f );
m_customPrefix = KeywordStr + " ";
m_autoWrapProperties = false;
m_freeType = false;
m_useVarSubtitle = true;
m_allowPropertyDuplicates = true;
m_showTitleWhenNotEditing = false;
m_currentParameterType = PropertyType.Property;
m_checkContent = new GUIContent();
m_checkContent.image = UIUtils.CheckmarkIcon;
m_popContent = new GUIContent();
m_popContent.image = UIUtils.PopupIcon;
m_previewShaderGUID = "0b708c11c68e6a9478ac97fe3643eab1";
m_showAutoRegisterUI = true;
}
public override void SetPreviewInputs()
{
base.SetPreviewInputs();
if( m_conditionId == -1 )
m_conditionId = Shader.PropertyToID( "_Condition" );
StaticSwitch node = ( m_staticSwitchVarMode == StaticSwitchVariableMode.Reference && m_reference != null ) ? m_reference : this;
if( m_createToggle )
PreviewMaterial.SetInt( m_conditionId, node.MaterialValue );
else
PreviewMaterial.SetInt( m_conditionId, node.DefaultValue );
}
protected override void OnUniqueIDAssigned()
{
base.OnUniqueIDAssigned();
if( m_createToggle )
UIUtils.RegisterPropertyNode( this );
else
UIUtils.UnregisterPropertyNode( this );
if( CurrentVarMode != StaticSwitchVariableMode.Reference )
{
ContainerGraph.StaticSwitchNodes.AddNode( this );
}
if( UniqueId > -1 )
ContainerGraph.StaticSwitchNodes.OnReorderEventComplete += OnReorderEventComplete;
}
public override void Destroy()
{
base.Destroy();
UIUtils.UnregisterPropertyNode( this );
if( CurrentVarMode != StaticSwitchVariableMode.Reference )
{
ContainerGraph.StaticSwitchNodes.RemoveNode( this );
}
if( UniqueId > -1 )
ContainerGraph.StaticSwitchNodes.OnReorderEventComplete -= OnReorderEventComplete;
}
void OnReorderEventComplete()
{
if( CurrentVarMode == StaticSwitchVariableMode.Reference )
{
if( m_reference != null )
{
m_referenceArrayId = ContainerGraph.StaticSwitchNodes.GetNodeRegisterIdx( m_reference.UniqueId );
}
}
}
public override void OnInputPortConnected( int portId, int otherNodeId, int otherPortId, bool activateNode = true )
{
base.OnInputPortConnected( portId, otherNodeId, otherPortId, activateNode );
UpdateConnections();
}
public override void OnConnectedOutputNodeChanges( int inputPortId, int otherNodeId, int otherPortId, string name, WirePortDataType type )
{
base.OnConnectedOutputNodeChanges( inputPortId, otherNodeId, otherPortId, name, type );
UpdateConnections();
}
public override void OnInputPortDisconnected( int portId )
{
base.OnInputPortDisconnected( portId );
UpdateConnections();
}
private void UpdateConnections()
{
WirePortDataType mainType = WirePortDataType.FLOAT;
int highest = UIUtils.GetPriority( mainType );
for( int i = 0; i < m_inputPorts.Count; i++ )
{
if( m_inputPorts[ i ].IsConnected )
{
WirePortDataType portType = m_inputPorts[ i ].GetOutputConnection().DataType;
if( UIUtils.GetPriority( portType ) > highest )
{
mainType = portType;
highest = UIUtils.GetPriority( portType );
}
}
}
for( int i = 0; i < m_inputPorts.Count; i++ )
{
m_inputPorts[ i ].ChangeType( mainType, false );
}
m_outputPorts[ 0 ].ChangeType( mainType, false );
}
public override string GetPropertyValue()
{
if( m_createToggle )
if( m_keywordModeType == KeywordModeType.KeywordEnum && m_keywordEnumAmount > 0 )
return PropertyAttributes + "[" + m_keywordModeType.ToString() + "(" + GetKeywordEnumPropertyList() + ")] " + m_propertyName + "(\"" + m_propertyInspectorName + "\", Float) = " + m_defaultValue;
else
return PropertyAttributes + "[" + m_keywordModeType.ToString() + "(" + GetPropertyValStr() + ")] " + m_propertyName + "(\"" + m_propertyInspectorName + "\", Float) = " + m_defaultValue;
else
return string.Empty;
}
public string KeywordEnum( int index )
{
if( m_createToggle )
{
return string.IsNullOrEmpty( PropertyName ) ? KeywordEnumList( index ) : ( PropertyName + "_" + KeywordEnumList( index ) );
}
else
{
return string.IsNullOrEmpty( PropertyName ) ? KeywordEnumList( index ) : ( PropertyName + KeywordEnumList( index ) );
}
}
public string KeywordEnumList( int index )
{
if( CurrentVarMode == StaticSwitchVariableMode.Fetch )
return m_keywordEnumList[ index ];
else
{
return m_createToggle ? m_keywordEnumList[ index ].ToUpper() : m_keywordEnumList[ index ];
}
}
public override string PropertyName
{
get
{
if( CurrentVarMode == StaticSwitchVariableMode.Fetch )
return m_currentKeyword;
else
{
return m_createToggle ? base.PropertyName.ToUpper() : base.PropertyName;
}
}
}
public override string GetPropertyValStr()
{
if( m_keywordModeType == KeywordModeType.KeywordEnum )
return PropertyName;
else if( CurrentVarMode == StaticSwitchVariableMode.Fetch )
return m_currentKeyword;
else
return PropertyName + OnOffStr;
}
private string GetKeywordEnumPropertyList()
{
string result = string.Empty;
for( int i = 0; i < m_keywordEnumList.Length; i++ )
{
if( i == 0 )
result = m_keywordEnumList[ i ];
else
result += "," + m_keywordEnumList[ i ];
}
return result;
}
private string GetKeywordEnumPragmaList()
{
string result = string.Empty;
for( int i = 0; i < m_keywordEnumList.Length; i++ )
{
if( i == 0 )
result = KeywordEnum( i );
else
result += " " + KeywordEnum( i );
}
return result;
}
public override string GetUniformValue()
{
return string.Empty;
}
public override bool GetUniformData( out string dataType, out string dataName, ref bool fullValue )
{
dataType = string.Empty;
dataName = string.Empty;
return false;
}
public override void DrawProperties()
{
//base.DrawProperties();
NodeUtils.DrawPropertyGroup( ref m_propertiesFoldout, Constants.ParameterLabelStr, PropertyGroup );
NodeUtils.DrawPropertyGroup( ref m_visibleCustomAttrFoldout, CustomAttrStr, DrawCustomAttributes, DrawCustomAttrAddRemoveButtons );
CheckPropertyFromInspector();
}
void DrawEnumList()
{
EditorGUI.BeginChangeCheck();
KeywordEnumAmount = EditorGUILayoutIntSlider( AmountStr, KeywordEnumAmount, 2, 9 );
if( EditorGUI.EndChangeCheck() )
{
CurrentSelectedInput = Mathf.Clamp( CurrentSelectedInput, 0, KeywordEnumAmount - 1 );
UpdateLabels();
}
EditorGUI.indentLevel++;
for( int i = 0; i < m_keywordEnumList.Length; i++ )
{
EditorGUI.BeginChangeCheck();
m_keywordEnumList[ i ] = EditorGUILayoutTextField( "Item " + i, m_keywordEnumList[ i ] );
if( EditorGUI.EndChangeCheck() )
{
m_keywordEnumList[ i ] = UIUtils.RemoveInvalidEnumCharacters( m_keywordEnumList[ i ] );
m_keywordEnumList[ i ] = m_keywordEnumList[ i ].Replace( " ", "" ); // sad face :( does not support spaces
m_inputPorts[ i ].Name = m_keywordEnumList[ i ];
m_defaultKeywordNames[ i ] = m_inputPorts[ i ].Name;
}
}
EditorGUI.indentLevel--;
}
public void UpdateLabels()
{
int maxinputs = m_keywordModeType == KeywordModeType.KeywordEnum ? KeywordEnumAmount : 2;
KeywordEnumAmount = Mathf.Clamp( KeywordEnumAmount, 0, maxinputs );
m_keywordEnumList = new string[ maxinputs ];
for( int i = 0; i < maxinputs; i++ )
{
m_keywordEnumList[ i ] = m_defaultKeywordNames[ i ];
m_inputPorts[ i ].Name = m_keywordEnumList[ i ];
}
if( m_keywordModeType != KeywordModeType.KeywordEnum )
{
m_inputPorts[ 0 ].Name = "False";
m_inputPorts[ 1 ].Name = "True";
}
for( int i = 0; i < m_inputPorts.Count; i++ )
{
m_inputPorts[ i ].Visible = ( i < maxinputs );
}
m_sizeIsDirty = true;
m_isStaticSwitchDirty = true;
}
void PropertyGroup()
{
EditorGUI.BeginChangeCheck();
CurrentVarMode = (StaticSwitchVariableMode)EditorGUILayoutEnumPopup( ModeStr, CurrentVarMode );
if( EditorGUI.EndChangeCheck() )
{
if( CurrentVarMode == StaticSwitchVariableMode.Fetch )
{
m_keywordModeType = KeywordModeType.Toggle;
UpdateLabels();
}
if( CurrentVarMode == StaticSwitchVariableMode.Reference )
{
UIUtils.UnregisterPropertyNode( this );
}
else
{
if( m_createToggle )
UIUtils.RegisterPropertyNode( this );
else
UIUtils.UnregisterPropertyNode( this );
}
}
if( CurrentVarMode == StaticSwitchVariableMode.Create )
{
EditorGUI.BeginChangeCheck();
m_multiCompile = EditorGUILayoutIntPopup( KeywordTypeStr, m_multiCompile, KeywordTypeList, KeywordTypeInt );
if( EditorGUI.EndChangeCheck() )
{
BeginPropertyFromInspectorCheck();
}
}
else if( CurrentVarMode == StaticSwitchVariableMode.Reference )
{
string[] arr = ContainerGraph.StaticSwitchNodes.NodesArr;
bool guiEnabledBuffer = GUI.enabled;
if( arr != null && arr.Length > 0 )
{
GUI.enabled = true;
}
else
{
m_referenceArrayId = -1;
GUI.enabled = false;
}
EditorGUI.BeginChangeCheck();
m_referenceArrayId = EditorGUILayoutPopup( Constants.AvailableReferenceStr, m_referenceArrayId, arr );
if( EditorGUI.EndChangeCheck() )
{
m_reference = ContainerGraph.StaticSwitchNodes.GetNode( m_referenceArrayId );
if( m_reference != null )
{
m_referenceNodeId = m_reference.UniqueId;
CheckReferenceValues( true );
}
else
{
m_referenceArrayId = -1;
m_referenceNodeId = -1;
}
}
GUI.enabled = guiEnabledBuffer;
return;
}
if( CurrentVarMode == StaticSwitchVariableMode.Create )
{
EditorGUI.BeginChangeCheck();
m_keywordModeType = (KeywordModeType)EditorGUILayoutEnumPopup( TypeStr, m_keywordModeType );
if( EditorGUI.EndChangeCheck() )
{
UpdateLabels();
}
}
if( m_keywordModeType != KeywordModeType.KeywordEnum )
{
if( CurrentVarMode == StaticSwitchVariableMode.Create )
{
ShowPropertyInspectorNameGUI();
ShowPropertyNameGUI( true );
bool guiEnabledBuffer = GUI.enabled;
GUI.enabled = false;
EditorGUILayout.TextField( KeywordNameStr, GetPropertyValStr() );
GUI.enabled = guiEnabledBuffer;
}
}
else
{
if( CurrentVarMode == StaticSwitchVariableMode.Create )
{
ShowPropertyInspectorNameGUI();
ShowPropertyNameGUI( true );
DrawEnumList();
}
}
if( CurrentVarMode == StaticSwitchVariableMode.Fetch )
{
//ShowPropertyInspectorNameGUI();
EditorGUI.BeginChangeCheck();
m_currentKeywordId = EditorGUILayoutPopup( KeywordStr, m_currentKeywordId, UIUtils.AvailableKeywords );
if( EditorGUI.EndChangeCheck() )
{
if( m_currentKeywordId != 0 )
{
m_currentKeyword = UIUtils.AvailableKeywords[ m_currentKeywordId ];
}
}
if( m_currentKeywordId == 0 )
{
EditorGUI.BeginChangeCheck();
m_currentKeyword = EditorGUILayoutTextField( CustomStr, m_currentKeyword );
if( EditorGUI.EndChangeCheck() )
{
m_currentKeyword = UIUtils.RemoveInvalidCharacters( m_currentKeyword );
}
}
}
#if UNITY_2019_1_OR_NEWER
m_isLocal = EditorGUILayoutToggle( IsLocalStr, m_isLocal );
#endif
if( CurrentVarMode == StaticSwitchVariableMode.Create )
{
ShowAutoRegister();
}
EditorGUI.BeginChangeCheck();
m_createToggle = EditorGUILayoutToggle( MaterialToggleStr, m_createToggle );
if( EditorGUI.EndChangeCheck() )
{
if( m_createToggle )
UIUtils.RegisterPropertyNode( this );
else
UIUtils.UnregisterPropertyNode( this );
}
if( m_createToggle )
{
EditorGUILayout.BeginHorizontal();
GUILayout.Space( 20 );
m_propertyTab = GUILayout.Toolbar( m_propertyTab, LabelToolbarTitle );
EditorGUILayout.EndHorizontal();
switch( m_propertyTab )
{
default:
case 0:
{
EditorGUI.BeginChangeCheck();
if( m_keywordModeType != KeywordModeType.KeywordEnum )
m_materialValue = EditorGUILayoutToggle( ToggleMaterialValueStr, m_materialValue == 1 ) ? 1 : 0;
else
m_materialValue = EditorGUILayoutPopup( ToggleMaterialValueStr, m_materialValue, m_keywordEnumList );
if( EditorGUI.EndChangeCheck() )
m_requireMaterialUpdate = true;
}
break;
case 1:
{
if( m_keywordModeType != KeywordModeType.KeywordEnum )
m_defaultValue = EditorGUILayoutToggle( ToggleDefaultValueStr, m_defaultValue == 1 ) ? 1 : 0;
else
m_defaultValue = EditorGUILayoutPopup( ToggleDefaultValueStr, m_defaultValue, m_keywordEnumList );
}
break;
}
}
//EditorGUILayout.HelpBox( "Keyword Type:\n" +
// "The difference is that unused variants of \"Shader Feature\" shaders will not be included into game build while \"Multi Compile\" variants are included regardless of their usage.\n\n" +
// "So \"Shader Feature\" makes most sense for keywords that will be set on the materials, while \"Multi Compile\" for keywords that will be set from code globally.\n\n" +
// "You can set keywords using the material property using the \"Property Name\" or you can set the keyword directly using the \"Keyword Name\".", MessageType.None );
}
public override void CheckPropertyFromInspector( bool forceUpdate = false )
{
if( m_propertyFromInspector )
{
if( forceUpdate || ( EditorApplication.timeSinceStartup - m_propertyFromInspectorTimestamp ) > MaxTimestamp )
{
m_propertyFromInspector = false;
RegisterPropertyName( true, m_propertyInspectorName, m_autoGlobalName, m_underscoredGlobal );
m_propertyNameIsDirty = true;
if( CurrentVarMode != StaticSwitchVariableMode.Reference )
{
ContainerGraph.StaticSwitchNodes.UpdateDataOnNode( UniqueId, DataToArray );
}
}
}
}
public override void OnNodeLayout( DrawInfo drawInfo )
{
float finalSize = 0;
if( m_keywordModeType == KeywordModeType.KeywordEnum )
{
GUIContent dropdown = new GUIContent( m_inputPorts[ CurrentSelectedInput ].Name );
int cacheSize = UIUtils.GraphDropDown.fontSize;
UIUtils.GraphDropDown.fontSize = 10;
Vector2 calcSize = UIUtils.GraphDropDown.CalcSize( dropdown );
UIUtils.GraphDropDown.fontSize = cacheSize;
finalSize = Mathf.Clamp( calcSize.x, MinComboSize, MaxComboSize );
if( m_insideSize.x != finalSize )
{
m_insideSize.Set( finalSize, 25 );
m_sizeIsDirty = true;
}
}
base.OnNodeLayout( drawInfo );
if( m_keywordModeType != KeywordModeType.KeywordEnum )
{
m_varRect = m_remainingBox;
m_varRect.size = Vector2.one * 22 * drawInfo.InvertedZoom;
m_varRect.center = m_remainingBox.center;
if( m_showPreview )
m_varRect.y = m_remainingBox.y;
}
else
{
m_varRect = m_remainingBox;
m_varRect.width = finalSize * drawInfo.InvertedZoom;
m_varRect.height = 16 * drawInfo.InvertedZoom;
m_varRect.x = m_remainingBox.xMax - m_varRect.width;
m_varRect.y += 1 * drawInfo.InvertedZoom;
m_imgRect = m_varRect;
m_imgRect.x = m_varRect.xMax - 16 * drawInfo.InvertedZoom;
m_imgRect.width = 16 * drawInfo.InvertedZoom;
m_imgRect.height = m_imgRect.width;
}
CheckReferenceValues( false );
if( m_staticSwitchVarMode == StaticSwitchVariableMode.Reference )
{
m_iconPos = m_globalPosition;
m_iconPos.width = InstanceIconWidth * drawInfo.InvertedZoom;
m_iconPos.height = InstanceIconHeight * drawInfo.InvertedZoom;
m_iconPos.y += 10 * drawInfo.InvertedZoom;
m_iconPos.x += /*m_globalPosition.width - m_iconPos.width - */5 * drawInfo.InvertedZoom;
}
}
void CheckReferenceValues( bool forceUpdate )
{
if( m_staticSwitchVarMode == StaticSwitchVariableMode.Reference )
{
if( m_reference == null && m_referenceNodeId > 0 )
{
m_reference = ContainerGraph.GetNode( m_referenceNodeId ) as StaticSwitch;
m_referenceArrayId = ContainerGraph.StaticSwitchNodes.GetNodeRegisterIdx( m_referenceNodeId );
}
if( m_reference != null )
{
if( forceUpdate || m_reference.IsStaticSwitchDirty )
{
int count = m_inputPorts.Count;
for( int i = 0; i < count; i++ )
{
m_inputPorts[ i ].Name = m_reference.InputPorts[ i ].Name;
m_inputPorts[ i ].Visible = m_reference.InputPorts[ i ].Visible;
}
m_sizeIsDirty = true;
}
}
}
else
{
m_isStaticSwitchDirty = false;
}
}
public override void DrawGUIControls( DrawInfo drawInfo )
{
base.DrawGUIControls( drawInfo );
if( drawInfo.CurrentEventType != EventType.MouseDown || !m_createToggle )
return;
if( m_varRect.Contains( drawInfo.MousePosition ) )
{
m_editing = true;
}
else if( m_editing )
{
m_editing = false;
}
}
private int CurrentSelectedInput
{
get
{
return m_materialMode ? m_materialValue : m_defaultValue;
}
set
{
if( m_materialMode )
m_materialValue = value;
else
m_defaultValue = value;
}
}
public override void Draw( DrawInfo drawInfo )
{
base.Draw( drawInfo );
if( m_staticSwitchVarMode == StaticSwitchVariableMode.Reference )
return;
if( m_editing )
{
if( m_keywordModeType != KeywordModeType.KeywordEnum )
{
if( GUI.Button( m_varRect, GUIContent.none, UIUtils.GraphButton ) )
{
CurrentSelectedInput = CurrentSelectedInput == 1 ? 0 : 1;
PreviewIsDirty = true;
m_editing = false;
if( m_materialMode )
m_requireMaterialUpdate = true;
}
if( CurrentSelectedInput == 1 )
{
GUI.Label( m_varRect, m_checkContent, UIUtils.GraphButtonIcon );
}
}
else
{
EditorGUI.BeginChangeCheck();
CurrentSelectedInput = EditorGUIPopup( m_varRect, CurrentSelectedInput, m_keywordEnumList, UIUtils.GraphDropDown );
if( EditorGUI.EndChangeCheck() )
{
PreviewIsDirty = true;
m_editing = false;
if( m_materialMode )
m_requireMaterialUpdate = true;
}
}
}
}
public override void OnNodeRepaint( DrawInfo drawInfo )
{
base.OnNodeRepaint( drawInfo );
if( !m_isVisible )
return;
if( m_staticSwitchVarMode == StaticSwitchVariableMode.Reference )
{
GUI.Label( m_iconPos, string.Empty, UIUtils.GetCustomStyle( CustomStyle.SamplerTextureIcon ) );
return;
}
if( m_createToggle && ContainerGraph.LodLevel <= ParentGraph.NodeLOD.LOD2 )
{
if( !m_editing )
{
if( m_keywordModeType != KeywordModeType.KeywordEnum )
{
GUI.Label( m_varRect, GUIContent.none, UIUtils.GraphButton );
if( CurrentSelectedInput == 1 )
GUI.Label( m_varRect, m_checkContent, UIUtils.GraphButtonIcon );
}
else
{
GUI.Label( m_varRect, m_keywordEnumList[ CurrentSelectedInput ], UIUtils.GraphDropDown );
GUI.Label( m_imgRect, m_popContent, UIUtils.GraphButtonIcon );
}
}
}
}
private string OnOffStr
{
get
{
StaticSwitch node = null;
switch( CurrentVarMode )
{
default:
case StaticSwitchVariableMode.Create:
case StaticSwitchVariableMode.Fetch:
node = this;
break;
case StaticSwitchVariableMode.Reference:
{
node = ( m_reference != null ) ? m_reference : this;
}
break;
}
if( !node.CreateToggle )
return string.Empty;
switch( node.KeywordModeTypeValue )
{
default:
case KeywordModeType.Toggle:
return "_ON";
case KeywordModeType.ToggleOff:
return "_OFF";
}
}
}
string GetStaticSwitchType()
{
string staticSwitchType = ( m_multiCompile == 1 ) ? "multi_compile" : "shader_feature";
#if UNITY_2019_1_OR_NEWER
if( m_isLocal )
staticSwitchType += "_local";
#endif
return staticSwitchType;
}
void RegisterPragmas( ref MasterNodeDataCollector dataCollector )
{
if( CurrentVarMode == StaticSwitchVariableMode.Create )
{
string staticSwitchType = GetStaticSwitchType();
if( m_keywordModeType == KeywordModeType.KeywordEnum )
{
if( m_multiCompile == 1 )
dataCollector.AddToPragmas( UniqueId, staticSwitchType + " " + GetKeywordEnumPragmaList() );
else if( m_multiCompile == 0 )
dataCollector.AddToPragmas( UniqueId, staticSwitchType + " " + GetKeywordEnumPragmaList() );
}
else
{
if( m_multiCompile == 1 )
dataCollector.AddToPragmas( UniqueId, staticSwitchType + " __ " + PropertyName + OnOffStr );
else if( m_multiCompile == 0 )
dataCollector.AddToPragmas( UniqueId, staticSwitchType + " " + PropertyName + OnOffStr );
}
}
}
protected override void RegisterProperty( ref MasterNodeDataCollector dataCollector )
{
if( m_staticSwitchVarMode == StaticSwitchVariableMode.Reference && m_reference != null )
{
m_reference.RegisterProperty( ref dataCollector );
m_reference.RegisterPragmas( ref dataCollector );
}
else
{
if( m_createToggle )
base.RegisterProperty( ref dataCollector );
RegisterPragmas( ref dataCollector );
}
}
public override string GenerateShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar )
{
if( m_outputPorts[ 0 ].IsLocalValue( dataCollector.PortCategory ) )
return m_outputPorts[ 0 ].LocalValue( dataCollector.PortCategory );
base.GenerateShaderForOutput( outputId, ref dataCollector, ignoreLocalvar );
StaticSwitch node = ( m_staticSwitchVarMode == StaticSwitchVariableMode.Reference && m_reference != null ) ? m_reference : this;
this.OrderIndex = node.RawOrderIndex;
this.OrderIndexOffset = node.OrderIndexOffset;
//if( m_keywordModeType == KeywordModeType.KeywordEnum )
//node.RegisterPragmas( ref dataCollector );
string outType = UIUtils.PrecisionWirePortToCgType( CurrentPrecisionType, m_outputPorts[ 0 ].DataType );
if( node.KeywordModeTypeValue == KeywordModeType.KeywordEnum )
{
string defaultKey = "\t" + outType + " staticSwitch" + OutputId + " = " + m_inputPorts[ node.DefaultValue ].GeneratePortInstructions( ref dataCollector ) + ";";
string[] allOutputs = new string[ node.KeywordEnumAmount ];
for( int i = 0; i < node.KeywordEnumAmount; i++ )
allOutputs[ i ] = m_inputPorts[ i ].GeneratePortInstructions( ref dataCollector );
for( int i = 0; i < node.KeywordEnumAmount; i++ )
{
string keyword = node.KeywordEnum( i );
if( i == 0 )
dataCollector.AddLocalVariable( UniqueId, "#if defined(" + keyword + ")", true );
else
dataCollector.AddLocalVariable( UniqueId, "#elif defined(" + keyword + ")", true );
if( node.DefaultValue == i )
dataCollector.AddLocalVariable( UniqueId, defaultKey, true );
else
dataCollector.AddLocalVariable( UniqueId, "\t" + outType + " staticSwitch" + OutputId + " = " + allOutputs[ i ] + ";", true );
}
dataCollector.AddLocalVariable( UniqueId, "#else", true );
dataCollector.AddLocalVariable( UniqueId, defaultKey, true );
dataCollector.AddLocalVariable( UniqueId, "#endif", true );
}
else
{
string falseCode = m_inputPorts[ 0 ].GeneratePortInstructions( ref dataCollector );
string trueCode = m_inputPorts[ 1 ].GeneratePortInstructions( ref dataCollector );
if( node.CurrentVarMode == StaticSwitchVariableMode.Fetch )
dataCollector.AddLocalVariable( UniqueId, "#ifdef " + node.CurrentKeyword, true );
else
dataCollector.AddLocalVariable( UniqueId, "#ifdef " + node.PropertyName + OnOffStr, true );
dataCollector.AddLocalVariable( UniqueId, "\t" + outType + " staticSwitch" + OutputId + " = " + trueCode + ";", true );
dataCollector.AddLocalVariable( UniqueId, "#else", true );
dataCollector.AddLocalVariable( UniqueId, "\t" + outType + " staticSwitch" + OutputId + " = " + falseCode + ";", true );
dataCollector.AddLocalVariable( UniqueId, "#endif", true );
}
m_outputPorts[ 0 ].SetLocalValue( "staticSwitch" + OutputId, dataCollector.PortCategory );
return m_outputPorts[ 0 ].LocalValue( dataCollector.PortCategory );
}
public override void DrawTitle( Rect titlePos )
{
bool referenceMode = m_staticSwitchVarMode == StaticSwitchVariableMode.Reference && m_reference != null;
string subTitle = string.Empty;
string subTitleFormat = string.Empty;
if( referenceMode )
{
subTitle = m_reference.GetPropertyValStr();
subTitleFormat = Constants.SubTitleRefNameFormatStr;
}
else
{
subTitle = GetPropertyValStr();
subTitleFormat = Constants.SubTitleVarNameFormatStr;
}
SetAdditonalTitleTextOnCallback( subTitle, ( instance, newSubTitle ) => instance.AdditonalTitleContent.text = string.Format( subTitleFormat, newSubTitle ) );
if( !m_isEditing && ContainerGraph.LodLevel <= ParentGraph.NodeLOD.LOD3 )
{
GUI.Label( titlePos, StaticSwitchStr, UIUtils.GetCustomStyle( CustomStyle.NodeTitle ) );
}
}
public override void UpdateMaterial( Material mat )
{
base.UpdateMaterial( mat );
if( UIUtils.IsProperty( m_currentParameterType ) && !InsideShaderFunction )
{
if( m_keywordModeType == KeywordModeType.KeywordEnum )
{
for( int i = 0; i < m_keywordEnumAmount; i++ )
{
string key = KeywordEnum( i );
mat.DisableKeyword( key );
}
mat.EnableKeyword( KeywordEnum( m_materialValue ));
mat.SetFloat( m_propertyName, m_materialValue );
}
else
{
int final = m_materialValue;
if( m_keywordModeType == KeywordModeType.ToggleOff )
final = final == 1 ? 0 : 1;
mat.SetFloat( m_propertyName, m_materialValue );
if( final == 1 )
mat.EnableKeyword( GetPropertyValStr() );
else
mat.DisableKeyword( GetPropertyValStr() );
}
}
}
public override void SetMaterialMode( Material mat, bool fetchMaterialValues )
{
base.SetMaterialMode( mat, fetchMaterialValues );
if( fetchMaterialValues && m_materialMode && UIUtils.IsProperty( m_currentParameterType ) && mat.HasProperty( m_propertyName ) )
{
m_materialValue = mat.GetInt( m_propertyName );
}
}
public override void ForceUpdateFromMaterial( Material material )
{
if( UIUtils.IsProperty( m_currentParameterType ) && material.HasProperty( m_propertyName ) )
{
m_materialValue = material.GetInt( m_propertyName );
PreviewIsDirty = true;
}
}
public override void ReadFromString( ref string[] nodeParams )
{
base.ReadFromString( ref nodeParams );
m_multiCompile = Convert.ToInt32( GetCurrentParam( ref nodeParams ) );
if( UIUtils.CurrentShaderVersion() > 14403 )
{
m_defaultValue = Convert.ToInt32( GetCurrentParam( ref nodeParams ) );
if( UIUtils.CurrentShaderVersion() > 14101 )
{
m_materialValue = Convert.ToInt32( GetCurrentParam( ref nodeParams ) );
}
}
else
{
m_defaultValue = Convert.ToBoolean( GetCurrentParam( ref nodeParams ) ) ? 1 : 0;
if( UIUtils.CurrentShaderVersion() > 14101 )
{
m_materialValue = Convert.ToBoolean( GetCurrentParam( ref nodeParams ) ) ? 1 : 0;
}
}
if( UIUtils.CurrentShaderVersion() > 13104 )
{
m_createToggle = Convert.ToBoolean( GetCurrentParam( ref nodeParams ) );
m_currentKeyword = GetCurrentParam( ref nodeParams );
m_currentKeywordId = UIUtils.GetKeywordId( m_currentKeyword );
}
if( UIUtils.CurrentShaderVersion() > 14001 )
{
m_keywordModeType = (KeywordModeType)Enum.Parse( typeof( KeywordModeType ), GetCurrentParam( ref nodeParams ) );
}
if( UIUtils.CurrentShaderVersion() > 14403 )
{
KeywordEnumAmount = Convert.ToInt32( GetCurrentParam( ref nodeParams ) );
for( int i = 0; i < KeywordEnumAmount; i++ )
{
m_defaultKeywordNames[ i ] = GetCurrentParam( ref nodeParams );
}
UpdateLabels();
}
if( UIUtils.CurrentShaderVersion() > 16304 )
{
string currentVarMode = GetCurrentParam( ref nodeParams );
CurrentVarMode = (StaticSwitchVariableMode)Enum.Parse( typeof( StaticSwitchVariableMode ), currentVarMode );
if( CurrentVarMode == StaticSwitchVariableMode.Reference )
{
m_referenceNodeId = Convert.ToInt32( GetCurrentParam( ref nodeParams ) );
}
}
else
{
CurrentVarMode = (StaticSwitchVariableMode)m_variableMode;
}
if( CurrentVarMode == StaticSwitchVariableMode.Reference )
{
UIUtils.UnregisterPropertyNode( this );
}
else
{
if( m_createToggle )
UIUtils.RegisterPropertyNode( this );
else
UIUtils.UnregisterPropertyNode( this );
}
if( UIUtils.CurrentShaderVersion() > 16700 )
{
m_isLocal = Convert.ToBoolean( GetCurrentParam( ref nodeParams ) );
}
SetMaterialToggleRetrocompatibility();
if( !m_isNodeBeingCopied && CurrentVarMode != StaticSwitchVariableMode.Reference )
{
ContainerGraph.StaticSwitchNodes.UpdateDataOnNode( UniqueId, DataToArray );
}
}
void SetMaterialToggleRetrocompatibility()
{
if( UIUtils.CurrentShaderVersion() < 17108 )
{
if( !m_createToggle && m_staticSwitchVarMode == StaticSwitchVariableMode.Create )
{
if( m_keywordModeType != KeywordModeType.KeywordEnum )
{
m_propertyName = m_propertyName.ToUpper() + "_ON";
}
else
{
m_propertyName = m_propertyName.ToUpper();
for( int i = 0; i < m_keywordEnumList.Length; i++ )
{
m_keywordEnumList[ i ] = "_" + m_keywordEnumList[ i ].ToUpper();
}
}
m_autoGlobalName = false;
}
}
}
public override void ReadFromDeprecated( ref string[] nodeParams, Type oldType = null )
{
base.ReadFromDeprecated( ref nodeParams, oldType );
{
m_currentKeyword = GetCurrentParam( ref nodeParams );
m_currentKeywordId = UIUtils.GetKeywordId( m_currentKeyword );
m_createToggle = false;
m_keywordModeType = KeywordModeType.Toggle;
m_variableMode = VariableMode.Fetch;
CurrentVarMode = StaticSwitchVariableMode.Fetch;
}
}
public override void WriteToString( ref string nodeInfo, ref string connectionsInfo )
{
base.WriteToString( ref nodeInfo, ref connectionsInfo );
IOUtils.AddFieldValueToString( ref nodeInfo, m_multiCompile );
IOUtils.AddFieldValueToString( ref nodeInfo, m_defaultValue );
IOUtils.AddFieldValueToString( ref nodeInfo, m_materialValue );
IOUtils.AddFieldValueToString( ref nodeInfo, m_createToggle );
IOUtils.AddFieldValueToString( ref nodeInfo, m_currentKeyword );
IOUtils.AddFieldValueToString( ref nodeInfo, m_keywordModeType );
IOUtils.AddFieldValueToString( ref nodeInfo, KeywordEnumAmount );
for( int i = 0; i < KeywordEnumAmount; i++ )
{
IOUtils.AddFieldValueToString( ref nodeInfo, m_keywordEnumList[ i ] );
}
IOUtils.AddFieldValueToString( ref nodeInfo, CurrentVarMode );
if( CurrentVarMode == StaticSwitchVariableMode.Reference )
{
int referenceId = ( m_reference != null ) ? m_reference.UniqueId : -1;
IOUtils.AddFieldValueToString( ref nodeInfo, referenceId );
}
IOUtils.AddFieldValueToString( ref nodeInfo, m_isLocal );
}
public override void RefreshExternalReferences()
{
base.RefreshExternalReferences();
CheckReferenceValues( true );
}
StaticSwitchVariableMode CurrentVarMode
{
get { return m_staticSwitchVarMode; }
set
{
if( m_staticSwitchVarMode != value )
{
if( value == StaticSwitchVariableMode.Reference )
{
ContainerGraph.StaticSwitchNodes.RemoveNode( this );
m_referenceArrayId = -1;
m_referenceNodeId = -1;
m_reference = null;
m_headerColorModifier = ReferenceHeaderColor;
}
else
{
m_headerColorModifier = Color.white;
ContainerGraph.StaticSwitchNodes.AddNode( this );
UpdateLabels();
}
}
m_staticSwitchVarMode = value;
}
}
public bool IsStaticSwitchDirty { get { return m_isStaticSwitchDirty; } }
public KeywordModeType KeywordModeTypeValue { get { return m_keywordModeType; } }
public int DefaultValue { get { return m_defaultValue; } }
public int MaterialValue { get { return m_materialValue; } }
public string CurrentKeyword { get { return m_currentKeyword; } }
public bool CreateToggle { get { return m_createToggle; } }
public int KeywordEnumAmount
{
get
{
return m_keywordEnumAmount;
}
set
{
m_keywordEnumAmount = value;
m_defaultValue = Mathf.Clamp( m_defaultValue, 0, m_keywordEnumAmount - 1 );
m_materialValue = Mathf.Clamp( m_defaultValue, 0, m_keywordEnumAmount - 1 );
}
}
}
}
|