StaticSwitch.cs 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391
  1. // Amplify Shader Editor - Visual Shader vEditing Tool
  2. // Copyright (c) Amplify Creations, Lda <info@amplify.pt>
  3. using UnityEngine;
  4. using UnityEditor;
  5. using System;
  6. namespace AmplifyShaderEditor
  7. {
  8. [Serializable]
  9. [NodeAttributes( "Switch", "Logical Operators", "Creates a shader keyword toggle", Available = true )]
  10. public sealed class StaticSwitch : PropertyNode
  11. {
  12. public enum ShaderStage
  13. {
  14. All,
  15. Vertex,
  16. Fragment,
  17. Hull,
  18. Domain,
  19. Geometry,
  20. Raytracing
  21. };
  22. private float InstanceIconWidth = 19;
  23. private float InstanceIconHeight = 19;
  24. private readonly Color ReferenceHeaderColor = new Color( 0f, 0.5f, 0.585f, 1.0f );
  25. [SerializeField]
  26. private int m_defaultValue = 0;
  27. [SerializeField]
  28. private int m_materialValue = 0;
  29. [SerializeField]
  30. private int m_multiCompile = 0;
  31. [SerializeField]
  32. private int m_currentKeywordId = 0;
  33. [SerializeField]
  34. private string m_currentKeyword = string.Empty;
  35. [SerializeField]
  36. private bool m_createToggle = true;
  37. [SerializeField]
  38. private bool m_lockKeyword = true;
  39. private const string IsLocalStr = "Is Local";
  40. private const string StageStr = "Stage";
  41. [SerializeField]
  42. private bool m_isLocal = true;
  43. [SerializeField]
  44. private ShaderStage m_shaderStage = ShaderStage.All;
  45. private GUIContent m_checkContent;
  46. private GUIContent m_popContent;
  47. private int m_conditionId = -1;
  48. private const int MinComboSize = 50;
  49. private const int MaxComboSize = 105;
  50. private Rect m_varRect;
  51. private Rect m_imgRect;
  52. private bool m_editing;
  53. public enum KeywordModeType
  54. {
  55. Toggle = 0,
  56. ToggleOff = 1,
  57. KeywordEnum = 2,
  58. }
  59. public enum StaticSwitchVariableMode
  60. {
  61. Create = 0,
  62. Fetch = 1,
  63. Reference = 2
  64. }
  65. public enum KeywordType
  66. {
  67. ShaderFeature = 0,
  68. MultiCompile = 1,
  69. DynamicBranch = 2,
  70. }
  71. [SerializeField]
  72. private KeywordModeType m_keywordModeType = KeywordModeType.Toggle;
  73. [SerializeField]
  74. private StaticSwitch m_reference = null;
  75. private const string StaticSwitchStr = "Switch";
  76. private const string MaterialToggleStr = "Material Toggle";
  77. private const string ToggleMaterialValueStr = "Material Value";
  78. private const string ToggleDefaultValueStr = "Default Value";
  79. private const string AmountStr = "Amount";
  80. private const string KeywordStr = "Keyword";
  81. private const string CustomStr = "Custom";
  82. private const string ToggleTypeStr = "Toggle Type";
  83. private const string TypeStr = "Type";
  84. private const string ModeStr = "Mode";
  85. private const string KeywordTypeStr = "Keyword Type";
  86. private const string KeywordNameStr = "Keyword Name";
  87. public readonly static string[] KeywordTypeList = { "Shader Feature", "Multi Compile", "Dynamic Branch" };
  88. public readonly static int[] KeywordTypeInt = { ( int )KeywordType.ShaderFeature, ( int )KeywordType.MultiCompile, ( int )KeywordType.DynamicBranch };
  89. [SerializeField]
  90. private string[] m_defaultKeywordNames = { "Key0", "Key1", "Key2", "Key3", "Key4", "Key5", "Key6", "Key7", "Key8" };
  91. [SerializeField]
  92. private string[] m_keywordEnumList = { "Key0", "Key1" };
  93. [SerializeField]
  94. private StaticSwitchVariableMode m_staticSwitchVarMode = StaticSwitchVariableMode.Create;
  95. [SerializeField]
  96. private int m_referenceArrayId = -1;
  97. [SerializeField]
  98. private int m_referenceNodeId = -1;
  99. private int m_keywordEnumAmount = 2;
  100. private bool m_isStaticSwitchDirty = false;
  101. private Rect m_iconPos;
  102. public const string DynamicBranchErrorMsg = "dynamic_branch requires Unity 2022.1+";
  103. public const string KeywordModifierSurfaceWarning = "Keyword stage ignored in Surface Shaders due to Unity bug.";
  104. protected override void CommonInit( int uniqueId )
  105. {
  106. base.CommonInit( uniqueId );
  107. AddOutputPort( WirePortDataType.FLOAT, Constants.EmptyPortValue );
  108. AddInputPort( WirePortDataType.FLOAT, false, "False", -1, MasterNodePortCategory.Fragment, 1 );
  109. AddInputPort( WirePortDataType.FLOAT, false, "True", -1, MasterNodePortCategory.Fragment, 0 );
  110. for( int i = 2; i < 9; i++ )
  111. {
  112. AddInputPort( WirePortDataType.FLOAT, false, m_defaultKeywordNames[ i ] );
  113. m_inputPorts[ i ].Visible = false;
  114. }
  115. m_headerColor = new Color( 0.0f, 0.55f, 0.45f, 1f );
  116. m_customPrefix = KeywordStr + " ";
  117. m_autoWrapProperties = false;
  118. m_freeType = false;
  119. m_useVarSubtitle = true;
  120. m_allowPropertyDuplicates = true;
  121. m_showTitleWhenNotEditing = false;
  122. m_currentParameterType = PropertyType.Property;
  123. m_checkContent = new GUIContent();
  124. m_checkContent.image = UIUtils.CheckmarkIcon;
  125. m_popContent = new GUIContent();
  126. m_popContent.image = UIUtils.PopupIcon;
  127. m_previewShaderGUID = "0b708c11c68e6a9478ac97fe3643eab1";
  128. m_showAutoRegisterUI = true;
  129. m_errorMessageTooltip = DynamicBranchErrorMsg;
  130. m_errorMessageTypeIsError = NodeMessageType.Error;
  131. }
  132. public override void SetPreviewInputs()
  133. {
  134. base.SetPreviewInputs();
  135. if( m_conditionId == -1 )
  136. m_conditionId = Shader.PropertyToID( "_Condition" );
  137. StaticSwitch node = ( m_staticSwitchVarMode == StaticSwitchVariableMode.Reference && m_reference != null ) ? m_reference : this;
  138. if ( m_createToggle && m_materialMode )
  139. PreviewMaterial.SetInt( m_conditionId, node.MaterialValue );
  140. else
  141. PreviewMaterial.SetInt( m_conditionId, node.DefaultValue );
  142. }
  143. protected override void OnUniqueIDAssigned()
  144. {
  145. base.OnUniqueIDAssigned();
  146. if( m_createToggle )
  147. UIUtils.RegisterPropertyNode( this );
  148. else
  149. UIUtils.UnregisterPropertyNode( this );
  150. if( CurrentVarMode != StaticSwitchVariableMode.Reference )
  151. {
  152. ContainerGraph.StaticSwitchNodes.AddNode( this );
  153. }
  154. if( UniqueId > -1 )
  155. ContainerGraph.StaticSwitchNodes.OnReorderEventComplete += OnReorderEventComplete;
  156. }
  157. public override void Destroy()
  158. {
  159. base.Destroy();
  160. UIUtils.UnregisterPropertyNode( this );
  161. if( CurrentVarMode != StaticSwitchVariableMode.Reference )
  162. {
  163. ContainerGraph.StaticSwitchNodes.RemoveNode( this );
  164. }
  165. if( UniqueId > -1 )
  166. ContainerGraph.StaticSwitchNodes.OnReorderEventComplete -= OnReorderEventComplete;
  167. }
  168. void OnReorderEventComplete()
  169. {
  170. if( CurrentVarMode == StaticSwitchVariableMode.Reference )
  171. {
  172. if( m_reference != null )
  173. {
  174. m_referenceArrayId = ContainerGraph.StaticSwitchNodes.GetNodeRegisterIdx( m_reference.UniqueId );
  175. }
  176. }
  177. }
  178. public override void OnInputPortConnected( int portId, int otherNodeId, int otherPortId, bool activateNode = true )
  179. {
  180. base.OnInputPortConnected( portId, otherNodeId, otherPortId, activateNode );
  181. UpdateConnections();
  182. }
  183. public override void OnConnectedOutputNodeChanges( int inputPortId, int otherNodeId, int otherPortId, string name, WirePortDataType type )
  184. {
  185. base.OnConnectedOutputNodeChanges( inputPortId, otherNodeId, otherPortId, name, type );
  186. UpdateConnections();
  187. }
  188. public override void OnInputPortDisconnected( int portId )
  189. {
  190. base.OnInputPortDisconnected( portId );
  191. UpdateConnections();
  192. }
  193. private void UpdateConnections()
  194. {
  195. WirePortDataType mainType = WirePortDataType.FLOAT;
  196. int highest = UIUtils.GetPriority( mainType );
  197. for( int i = 0; i < m_inputPorts.Count; i++ )
  198. {
  199. if( m_inputPorts[ i ].IsConnected )
  200. {
  201. WirePortDataType portType = m_inputPorts[ i ].GetOutputConnection().DataType;
  202. if( UIUtils.GetPriority( portType ) > highest )
  203. {
  204. mainType = portType;
  205. highest = UIUtils.GetPriority( portType );
  206. }
  207. }
  208. }
  209. for( int i = 0; i < m_inputPorts.Count; i++ )
  210. {
  211. m_inputPorts[ i ].ChangeType( mainType, false );
  212. }
  213. m_outputPorts[ 0 ].ChangeType( mainType, false );
  214. }
  215. public override string GetPropertyValue()
  216. {
  217. if( m_createToggle )
  218. {
  219. string value = UIUtils.PropertyFloatToString( m_defaultValue );
  220. if ( m_keywordModeType == KeywordModeType.KeywordEnum && m_keywordEnumAmount > 0 )
  221. {
  222. return PropertyAttributes + "[" + m_keywordModeType.ToString() + "(" + GetKeywordEnumPropertyList() + ")] " + m_propertyName + "(\"" + m_propertyInspectorName + "\", Float) = " + value;
  223. }
  224. else
  225. {
  226. return PropertyAttributes + "[" + m_keywordModeType.ToString() + "(" + GetPropertyValStr() + ")] " + m_propertyName + "(\"" + m_propertyInspectorName + "\", Float) = " + value;
  227. }
  228. }
  229. return string.Empty;
  230. }
  231. public string KeywordEnum( int index )
  232. {
  233. if( m_createToggle )
  234. {
  235. return string.IsNullOrEmpty( PropertyName ) ? KeywordEnumList( index ) : ( PropertyName + "_" + KeywordEnumList( index ) );
  236. }
  237. else
  238. {
  239. return string.IsNullOrEmpty( PropertyName ) ? KeywordEnumList( index ) : ( PropertyName + KeywordEnumList( index ) );
  240. }
  241. }
  242. public string KeywordEnumList( int index )
  243. {
  244. if( CurrentVarMode == StaticSwitchVariableMode.Fetch )
  245. return m_keywordEnumList[ index ];
  246. else
  247. {
  248. return m_createToggle ? m_keywordEnumList[ index ].ToUpper() : m_keywordEnumList[ index ];
  249. }
  250. }
  251. public override string PropertyName
  252. {
  253. get
  254. {
  255. if( CurrentVarMode == StaticSwitchVariableMode.Fetch )
  256. return m_currentKeyword;
  257. else
  258. {
  259. return m_createToggle ? base.PropertyName.ToUpper() : base.PropertyName;
  260. }
  261. }
  262. }
  263. public override string GetPropertyValStr()
  264. {
  265. if ( m_keywordModeType == KeywordModeType.KeywordEnum )
  266. return PropertyName;
  267. else if( !m_lockKeyword )
  268. return CurrentKeyword;
  269. else if( CurrentVarMode == StaticSwitchVariableMode.Fetch )
  270. return m_currentKeyword;
  271. else
  272. return PropertyName + OnOffStr;
  273. }
  274. public override string GetSubTitleVarNameFormatStr()
  275. {
  276. if ( m_multiCompile == ( int )KeywordType.DynamicBranch )
  277. {
  278. return "Dynamic( {0} )";
  279. }
  280. else
  281. {
  282. return "Static( {0} )";
  283. }
  284. }
  285. private string GetKeywordEnumPropertyList()
  286. {
  287. string result = string.Empty;
  288. for( int i = 0; i < m_keywordEnumList.Length; i++ )
  289. {
  290. if( i == 0 )
  291. result = m_keywordEnumList[ i ];
  292. else
  293. result += "," + m_keywordEnumList[ i ];
  294. }
  295. return result;
  296. }
  297. private string GetKeywordEnumPragmaList()
  298. {
  299. string result = string.Empty;
  300. for( int i = 0; i < m_keywordEnumList.Length; i++ )
  301. {
  302. if( i == 0 )
  303. result = KeywordEnum( i );
  304. else
  305. result += " " + KeywordEnum( i );
  306. }
  307. return result;
  308. }
  309. public override string GetUniformValue()
  310. {
  311. return string.Empty;
  312. }
  313. public override bool GetUniformData( out string dataType, out string dataName, ref bool fullValue )
  314. {
  315. dataType = string.Empty;
  316. dataName = string.Empty;
  317. return false;
  318. }
  319. public override void OnNodeLogicUpdate( DrawInfo drawInfo )
  320. {
  321. base.OnNodeLogicUpdate( drawInfo );
  322. m_showErrorMessage = ( m_multiCompile == ( int )KeywordType.DynamicBranch && TemplateHelperFunctions.GetUnityVersion() < 20220100 );
  323. }
  324. public override void DrawProperties()
  325. {
  326. //base.DrawProperties();
  327. NodeUtils.DrawPropertyGroup( ref m_propertiesFoldout, Constants.ParameterLabelStr, PropertyGroup );
  328. NodeUtils.DrawPropertyGroup( ref m_visibleCustomAttrFoldout, CustomAttrStr, DrawCustomAttributes, DrawCustomAttrAddRemoveButtons );
  329. CheckPropertyFromInspector();
  330. }
  331. void DrawEnumList()
  332. {
  333. EditorGUI.BeginChangeCheck();
  334. KeywordEnumAmount = EditorGUILayoutIntSlider( AmountStr, KeywordEnumAmount, 2, 9 );
  335. if( EditorGUI.EndChangeCheck() )
  336. {
  337. CurrentSelectedInput = Mathf.Clamp( CurrentSelectedInput, 0, KeywordEnumAmount - 1 );
  338. UpdateLabels();
  339. }
  340. EditorGUI.indentLevel++;
  341. for( int i = 0; i < m_keywordEnumList.Length; i++ )
  342. {
  343. EditorGUI.BeginChangeCheck();
  344. m_keywordEnumList[ i ] = EditorGUILayoutTextField( "Item " + i, m_keywordEnumList[ i ] );
  345. if( EditorGUI.EndChangeCheck() )
  346. {
  347. m_keywordEnumList[ i ] = UIUtils.RemoveInvalidEnumCharacters( m_keywordEnumList[ i ] );
  348. m_keywordEnumList[ i ] = m_keywordEnumList[ i ].Replace( " ", "" ); // sad face :( does not support spaces
  349. m_inputPorts[ i ].Name = m_keywordEnumList[ i ];
  350. m_defaultKeywordNames[ i ] = m_inputPorts[ i ].Name;
  351. }
  352. }
  353. EditorGUI.indentLevel--;
  354. }
  355. public void UpdateLabels()
  356. {
  357. int maxinputs = m_keywordModeType == KeywordModeType.KeywordEnum ? KeywordEnumAmount : 2;
  358. KeywordEnumAmount = Mathf.Clamp( KeywordEnumAmount, 0, maxinputs );
  359. m_keywordEnumList = new string[ maxinputs ];
  360. for( int i = 0; i < maxinputs; i++ )
  361. {
  362. m_keywordEnumList[ i ] = m_defaultKeywordNames[ i ];
  363. m_inputPorts[ i ].Name = m_keywordEnumList[ i ];
  364. }
  365. if( m_keywordModeType != KeywordModeType.KeywordEnum )
  366. {
  367. m_inputPorts[ 0 ].Name = "False";
  368. m_inputPorts[ 1 ].Name = "True";
  369. }
  370. for( int i = 0; i < m_inputPorts.Count; i++ )
  371. {
  372. m_inputPorts[ i ].Visible = ( i < maxinputs );
  373. }
  374. m_sizeIsDirty = true;
  375. m_isStaticSwitchDirty = true;
  376. }
  377. void PropertyGroup()
  378. {
  379. EditorGUI.BeginChangeCheck();
  380. CurrentVarMode = (StaticSwitchVariableMode)EditorGUILayoutEnumPopup( ModeStr, CurrentVarMode );
  381. if( EditorGUI.EndChangeCheck() )
  382. {
  383. if( CurrentVarMode == StaticSwitchVariableMode.Fetch )
  384. {
  385. m_keywordModeType = KeywordModeType.Toggle;
  386. UpdateLabels();
  387. }
  388. if( CurrentVarMode == StaticSwitchVariableMode.Reference )
  389. {
  390. UIUtils.UnregisterPropertyNode( this );
  391. }
  392. else
  393. {
  394. if( m_createToggle )
  395. UIUtils.RegisterPropertyNode( this );
  396. else
  397. UIUtils.UnregisterPropertyNode( this );
  398. }
  399. }
  400. if( CurrentVarMode == StaticSwitchVariableMode.Create )
  401. {
  402. EditorGUI.BeginChangeCheck();
  403. m_multiCompile = EditorGUILayoutIntPopup( KeywordTypeStr, m_multiCompile, KeywordTypeList, KeywordTypeInt );
  404. if( EditorGUI.EndChangeCheck() )
  405. {
  406. BeginPropertyFromInspectorCheck();
  407. }
  408. if ( m_showErrorMessage )
  409. {
  410. EditorGUILayout.HelpBox( DynamicBranchErrorMsg, MessageType.Error );
  411. }
  412. }
  413. else if( CurrentVarMode == StaticSwitchVariableMode.Reference )
  414. {
  415. string[] arr = ContainerGraph.StaticSwitchNodes.NodesArr;
  416. bool guiEnabledBuffer = GUI.enabled;
  417. if( arr != null && arr.Length > 0 )
  418. {
  419. GUI.enabled = true;
  420. }
  421. else
  422. {
  423. m_referenceArrayId = -1;
  424. GUI.enabled = false;
  425. }
  426. EditorGUI.BeginChangeCheck();
  427. m_referenceArrayId = EditorGUILayoutPopup( Constants.AvailableReferenceStr, m_referenceArrayId, arr );
  428. if( EditorGUI.EndChangeCheck() )
  429. {
  430. m_reference = ContainerGraph.StaticSwitchNodes.GetNode( m_referenceArrayId );
  431. if( m_reference != null )
  432. {
  433. m_referenceNodeId = m_reference.UniqueId;
  434. CheckReferenceValues( true );
  435. }
  436. else
  437. {
  438. m_referenceArrayId = -1;
  439. m_referenceNodeId = -1;
  440. }
  441. }
  442. GUI.enabled = guiEnabledBuffer;
  443. return;
  444. }
  445. if( CurrentVarMode == StaticSwitchVariableMode.Create || m_createToggle )
  446. {
  447. EditorGUI.BeginChangeCheck();
  448. m_keywordModeType = (KeywordModeType)EditorGUILayoutEnumPopup( TypeStr, m_keywordModeType );
  449. if( EditorGUI.EndChangeCheck() )
  450. {
  451. UpdateLabels();
  452. }
  453. }
  454. if( m_keywordModeType != KeywordModeType.KeywordEnum )
  455. {
  456. if( CurrentVarMode == StaticSwitchVariableMode.Create || m_createToggle )
  457. {
  458. ShowPropertyInspectorNameGUI();
  459. ShowPropertyNameGUI( true );
  460. if( CurrentVarMode == StaticSwitchVariableMode.Create )
  461. {
  462. EditorGUILayout.BeginHorizontal();
  463. bool guiEnabledBuffer = GUI.enabled;
  464. GUI.enabled = !m_lockKeyword;
  465. if( m_lockKeyword )
  466. EditorGUILayout.TextField( KeywordNameStr, GetPropertyValStr() );
  467. else
  468. m_currentKeyword = EditorGUILayoutTextField( KeywordNameStr, m_currentKeyword );
  469. GUI.enabled = guiEnabledBuffer;
  470. m_lockKeyword = GUILayout.Toggle( m_lockKeyword, ( m_lockKeyword ? UIUtils.LockIconOpen : UIUtils.LockIconClosed ), "minibutton", GUILayout.Width( 22 ) );
  471. EditorGUILayout.EndHorizontal();
  472. }
  473. }
  474. }
  475. else
  476. {
  477. if( CurrentVarMode == StaticSwitchVariableMode.Create || m_createToggle )
  478. {
  479. ShowPropertyInspectorNameGUI();
  480. ShowPropertyNameGUI( true );
  481. DrawEnumList();
  482. }
  483. }
  484. if( CurrentVarMode == StaticSwitchVariableMode.Fetch )
  485. {
  486. //ShowPropertyInspectorNameGUI();
  487. EditorGUI.BeginChangeCheck();
  488. m_currentKeywordId = EditorGUILayoutPopup( KeywordStr, m_currentKeywordId, UIUtils.AvailableKeywords );
  489. if( EditorGUI.EndChangeCheck() )
  490. {
  491. if( m_currentKeywordId != 0 )
  492. {
  493. m_currentKeyword = UIUtils.AvailableKeywords[ m_currentKeywordId ];
  494. }
  495. }
  496. if( m_currentKeywordId == 0 )
  497. {
  498. EditorGUI.BeginChangeCheck();
  499. m_currentKeyword = EditorGUILayoutTextField( CustomStr, m_currentKeyword );
  500. if( EditorGUI.EndChangeCheck() )
  501. {
  502. m_currentKeyword = UIUtils.RemoveInvalidCharacters( m_currentKeyword );
  503. }
  504. }
  505. }
  506. GUI.enabled = ( m_multiCompile != ( int )KeywordType.DynamicBranch );
  507. {
  508. m_isLocal = EditorGUILayoutToggle( IsLocalStr, m_isLocal );
  509. m_shaderStage = ( ShaderStage )EditorGUILayoutEnumPopup( StageStr, m_shaderStage );
  510. bool isFunction = ( m_containerGraph.CurrentCanvasMode == NodeAvailability.ShaderFunction );
  511. bool isTemplate = ( m_containerGraph.CurrentCanvasMode == NodeAvailability.TemplateShader );
  512. if ( ( !isTemplate || isFunction ) && ( m_keywordModeType == KeywordModeType.KeywordEnum ) && ( m_shaderStage != ShaderStage.All ) )
  513. {
  514. EditorGUILayout.HelpBox( KeywordModifierSurfaceWarning, isFunction ? MessageType.Info : MessageType.Warning );
  515. }
  516. //if( CurrentVarMode == StaticSwitchVariableMode.Create )
  517. {
  518. ShowAutoRegister();
  519. }
  520. }
  521. GUI.enabled = true;
  522. EditorGUI.BeginChangeCheck();
  523. m_createToggle = EditorGUILayoutToggle( MaterialToggleStr, m_createToggle );
  524. if( EditorGUI.EndChangeCheck() )
  525. {
  526. if( m_createToggle )
  527. UIUtils.RegisterPropertyNode( this );
  528. else
  529. UIUtils.UnregisterPropertyNode( this );
  530. }
  531. if( m_createToggle )
  532. {
  533. EditorGUILayout.BeginHorizontal();
  534. GUILayout.Space( 20 );
  535. m_propertyTab = GUILayout.Toolbar( m_propertyTab, LabelToolbarTitle );
  536. EditorGUILayout.EndHorizontal();
  537. switch( m_propertyTab )
  538. {
  539. default:
  540. case 0:
  541. {
  542. EditorGUI.BeginChangeCheck();
  543. if( m_keywordModeType != KeywordModeType.KeywordEnum )
  544. m_materialValue = EditorGUILayoutToggle( ToggleMaterialValueStr, m_materialValue == 1 ) ? 1 : 0;
  545. else
  546. m_materialValue = EditorGUILayoutPopup( ToggleMaterialValueStr, m_materialValue, m_keywordEnumList );
  547. if( EditorGUI.EndChangeCheck() )
  548. m_requireMaterialUpdate = true;
  549. }
  550. break;
  551. case 1:
  552. {
  553. if( m_keywordModeType != KeywordModeType.KeywordEnum )
  554. m_defaultValue = EditorGUILayoutToggle( ToggleDefaultValueStr, m_defaultValue == 1 ) ? 1 : 0;
  555. else
  556. m_defaultValue = EditorGUILayoutPopup( ToggleDefaultValueStr, m_defaultValue, m_keywordEnumList );
  557. }
  558. break;
  559. }
  560. }
  561. //EditorGUILayout.HelpBox( "Keyword Type:\n" +
  562. // "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" +
  563. // "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" +
  564. // "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 );
  565. }
  566. public override void CheckPropertyFromInspector( bool forceUpdate = false )
  567. {
  568. if( m_propertyFromInspector )
  569. {
  570. if( forceUpdate || ( EditorApplication.timeSinceStartup - m_propertyFromInspectorTimestamp ) > MaxTimestamp )
  571. {
  572. m_propertyFromInspector = false;
  573. RegisterPropertyName( true, m_propertyInspectorName, m_autoGlobalName, m_underscoredGlobal );
  574. m_propertyNameIsDirty = true;
  575. if( CurrentVarMode != StaticSwitchVariableMode.Reference )
  576. {
  577. ContainerGraph.StaticSwitchNodes.UpdateDataOnNode( UniqueId, DataToArray );
  578. }
  579. }
  580. }
  581. }
  582. public override void OnNodeLayout( DrawInfo drawInfo )
  583. {
  584. float finalSize = 0;
  585. if( m_keywordModeType == KeywordModeType.KeywordEnum )
  586. {
  587. GUIContent dropdown = new GUIContent( m_inputPorts[ CurrentSelectedInput ].Name );
  588. int cacheSize = UIUtils.GraphDropDown.fontSize;
  589. UIUtils.GraphDropDown.fontSize = 10;
  590. Vector2 calcSize = UIUtils.GraphDropDown.CalcSize( dropdown );
  591. UIUtils.GraphDropDown.fontSize = cacheSize;
  592. finalSize = Mathf.Clamp( calcSize.x, MinComboSize, MaxComboSize );
  593. if( m_insideSize.x != finalSize )
  594. {
  595. m_insideSize.Set( finalSize, 25 );
  596. m_sizeIsDirty = true;
  597. }
  598. }
  599. base.OnNodeLayout( drawInfo );
  600. if( m_keywordModeType != KeywordModeType.KeywordEnum )
  601. {
  602. m_varRect = m_remainingBox;
  603. m_varRect.size = Vector2.one * 22 * drawInfo.InvertedZoom;
  604. m_varRect.center = m_remainingBox.center;
  605. if( m_showPreview )
  606. m_varRect.y = m_remainingBox.y;
  607. }
  608. else
  609. {
  610. m_varRect = m_remainingBox;
  611. m_varRect.width = finalSize * drawInfo.InvertedZoom;
  612. m_varRect.height = 16 * drawInfo.InvertedZoom;
  613. m_varRect.x = m_remainingBox.xMax - m_varRect.width;
  614. m_varRect.y += 1 * drawInfo.InvertedZoom;
  615. m_imgRect = m_varRect;
  616. m_imgRect.x = m_varRect.xMax - 16 * drawInfo.InvertedZoom;
  617. m_imgRect.width = 16 * drawInfo.InvertedZoom;
  618. m_imgRect.height = m_imgRect.width;
  619. }
  620. CheckReferenceValues( false );
  621. if( m_staticSwitchVarMode == StaticSwitchVariableMode.Reference )
  622. {
  623. m_iconPos = m_globalPosition;
  624. m_iconPos.width = InstanceIconWidth * drawInfo.InvertedZoom;
  625. m_iconPos.height = InstanceIconHeight * drawInfo.InvertedZoom;
  626. m_iconPos.y += 10 * drawInfo.InvertedZoom;
  627. m_iconPos.x += /*m_globalPosition.width - m_iconPos.width - */5 * drawInfo.InvertedZoom;
  628. }
  629. }
  630. void CheckReferenceValues( bool forceUpdate )
  631. {
  632. if( m_staticSwitchVarMode == StaticSwitchVariableMode.Reference )
  633. {
  634. if( m_reference == null && m_referenceNodeId > 0 )
  635. {
  636. m_reference = ContainerGraph.GetNode( m_referenceNodeId ) as StaticSwitch;
  637. m_referenceArrayId = ContainerGraph.StaticSwitchNodes.GetNodeRegisterIdx( m_referenceNodeId );
  638. }
  639. if( m_reference != null )
  640. {
  641. if( forceUpdate || m_reference.IsStaticSwitchDirty )
  642. {
  643. int count = m_inputPorts.Count;
  644. for( int i = 0; i < count; i++ )
  645. {
  646. m_inputPorts[ i ].Name = m_reference.InputPorts[ i ].Name;
  647. m_inputPorts[ i ].Visible = m_reference.InputPorts[ i ].Visible;
  648. }
  649. m_sizeIsDirty = true;
  650. }
  651. }
  652. }
  653. else
  654. {
  655. m_isStaticSwitchDirty = false;
  656. }
  657. }
  658. public override void DrawGUIControls( DrawInfo drawInfo )
  659. {
  660. base.DrawGUIControls( drawInfo );
  661. if( drawInfo.CurrentEventType != EventType.MouseDown || !m_createToggle )
  662. return;
  663. if( m_varRect.Contains( drawInfo.MousePosition ) )
  664. {
  665. m_editing = true;
  666. }
  667. else if( m_editing )
  668. {
  669. m_editing = false;
  670. }
  671. }
  672. private int CurrentSelectedInput
  673. {
  674. get
  675. {
  676. return m_materialMode ? m_materialValue : m_defaultValue;
  677. }
  678. set
  679. {
  680. if( m_materialMode )
  681. m_materialValue = value;
  682. else
  683. m_defaultValue = value;
  684. }
  685. }
  686. public override void Draw( DrawInfo drawInfo )
  687. {
  688. base.Draw( drawInfo );
  689. if( m_staticSwitchVarMode == StaticSwitchVariableMode.Reference )
  690. return;
  691. if( m_editing )
  692. {
  693. if( m_keywordModeType != KeywordModeType.KeywordEnum )
  694. {
  695. if( GUI.Button( m_varRect, GUIContent.none, UIUtils.GraphButton ) )
  696. {
  697. CurrentSelectedInput = CurrentSelectedInput == 1 ? 0 : 1;
  698. PreviewIsDirty = true;
  699. m_editing = false;
  700. if( m_materialMode )
  701. m_requireMaterialUpdate = true;
  702. }
  703. if( CurrentSelectedInput == 1 )
  704. {
  705. GUI.Label( m_varRect, m_checkContent, UIUtils.GraphButtonIcon );
  706. }
  707. }
  708. else
  709. {
  710. EditorGUI.BeginChangeCheck();
  711. CurrentSelectedInput = EditorGUIPopup( m_varRect, CurrentSelectedInput, m_keywordEnumList, UIUtils.GraphDropDown );
  712. if( EditorGUI.EndChangeCheck() )
  713. {
  714. PreviewIsDirty = true;
  715. m_editing = false;
  716. if( m_materialMode )
  717. m_requireMaterialUpdate = true;
  718. }
  719. }
  720. }
  721. }
  722. public override void OnNodeRepaint( DrawInfo drawInfo )
  723. {
  724. base.OnNodeRepaint( drawInfo );
  725. if( !m_isVisible )
  726. return;
  727. if( m_staticSwitchVarMode == StaticSwitchVariableMode.Reference )
  728. {
  729. GUI.Label( m_iconPos, string.Empty, UIUtils.GetCustomStyle( CustomStyle.SamplerTextureIcon ) );
  730. return;
  731. }
  732. if( m_createToggle && ContainerGraph.LodLevel <= ParentGraph.NodeLOD.LOD2 )
  733. {
  734. if( !m_editing )
  735. {
  736. if( m_keywordModeType != KeywordModeType.KeywordEnum )
  737. {
  738. GUI.Label( m_varRect, GUIContent.none, UIUtils.GraphButton );
  739. if( CurrentSelectedInput == 1 )
  740. GUI.Label( m_varRect, m_checkContent, UIUtils.GraphButtonIcon );
  741. }
  742. else
  743. {
  744. GUI.Label( m_varRect, m_keywordEnumList[ CurrentSelectedInput ], UIUtils.GraphDropDown );
  745. GUI.Label( m_imgRect, m_popContent, UIUtils.GraphButtonIcon );
  746. }
  747. }
  748. }
  749. }
  750. private string OnOffStr
  751. {
  752. get
  753. {
  754. if( !m_lockKeyword )
  755. return string.Empty;
  756. StaticSwitch node = null;
  757. switch( CurrentVarMode )
  758. {
  759. default:
  760. case StaticSwitchVariableMode.Create:
  761. case StaticSwitchVariableMode.Fetch:
  762. node = this;
  763. break;
  764. case StaticSwitchVariableMode.Reference:
  765. {
  766. node = ( m_reference != null ) ? m_reference : this;
  767. }
  768. break;
  769. }
  770. if( !node.CreateToggle )
  771. return string.Empty;
  772. switch( node.KeywordModeTypeValue )
  773. {
  774. default:
  775. case KeywordModeType.Toggle:
  776. return "_ON";
  777. case KeywordModeType.ToggleOff:
  778. return "_OFF";
  779. }
  780. }
  781. }
  782. string GetStaticSwitchType( bool allowStages )
  783. {
  784. string staticSwitchType;
  785. bool allowModifiers = true;
  786. switch ( m_multiCompile )
  787. {
  788. case 1:
  789. staticSwitchType = "multi_compile";
  790. break;
  791. case 2:
  792. staticSwitchType = "dynamic_branch";
  793. allowModifiers = false;
  794. break;
  795. default:
  796. staticSwitchType = "shader_feature";
  797. break;
  798. }
  799. if ( allowModifiers )
  800. {
  801. if ( m_isLocal )
  802. {
  803. staticSwitchType += "_local";
  804. }
  805. if ( allowStages )
  806. {
  807. switch ( m_shaderStage )
  808. {
  809. default:
  810. case ShaderStage.All: break;
  811. case ShaderStage.Vertex: staticSwitchType += "_vertex"; break;
  812. case ShaderStage.Fragment: staticSwitchType += "_fragment"; break;
  813. case ShaderStage.Hull: staticSwitchType += "_hull"; break;
  814. case ShaderStage.Domain: staticSwitchType += "_domain"; break;
  815. case ShaderStage.Geometry: staticSwitchType += "_geometry"; break;
  816. case ShaderStage.Raytracing: staticSwitchType += "_raytracing"; break;
  817. }
  818. }
  819. }
  820. return staticSwitchType;
  821. }
  822. void RegisterPragmas( ref MasterNodeDataCollector dataCollector )
  823. {
  824. if( CurrentVarMode == StaticSwitchVariableMode.Create )
  825. {
  826. string staticSwitchType = GetStaticSwitchType( allowStages: dataCollector.IsTemplate || ( m_keywordModeType != KeywordModeType.KeywordEnum ) );
  827. if( m_keywordModeType == KeywordModeType.KeywordEnum )
  828. {
  829. dataCollector.AddToPragmas( UniqueId, staticSwitchType + " " + GetKeywordEnumPragmaList() );
  830. }
  831. else
  832. {
  833. if( m_multiCompile == 1 )
  834. dataCollector.AddToPragmas( UniqueId, staticSwitchType + " __ " + CurrentKeyword );
  835. else
  836. dataCollector.AddToPragmas( UniqueId, staticSwitchType + " " + CurrentKeyword );
  837. }
  838. }
  839. }
  840. protected override void RegisterProperty( ref MasterNodeDataCollector dataCollector )
  841. {
  842. if( m_staticSwitchVarMode == StaticSwitchVariableMode.Reference && m_reference != null )
  843. {
  844. m_reference.RegisterProperty( ref dataCollector );
  845. m_reference.RegisterPragmas( ref dataCollector );
  846. }
  847. else
  848. {
  849. if( m_createToggle )
  850. base.RegisterProperty( ref dataCollector );
  851. RegisterPragmas( ref dataCollector );
  852. }
  853. }
  854. public override string GenerateShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar )
  855. {
  856. if( m_outputPorts[ 0 ].IsLocalValue( dataCollector.PortCategory ) )
  857. return m_outputPorts[ 0 ].LocalValue( dataCollector.PortCategory );
  858. if ( m_showErrorMessage )
  859. {
  860. UIUtils.ShowMessage( DynamicBranchErrorMsg );
  861. return GenerateErrorValue();
  862. }
  863. base.GenerateShaderForOutput( outputId, ref dataCollector, ignoreLocalvar );
  864. StaticSwitch node = ( m_staticSwitchVarMode == StaticSwitchVariableMode.Reference && m_reference != null ) ? m_reference : this;
  865. this.OrderIndex = node.RawOrderIndex;
  866. this.OrderIndexOffset = node.OrderIndexOffset;
  867. string outType = UIUtils.PrecisionWirePortToCgType( CurrentPrecisionType, m_outputPorts[ 0 ].DataType );
  868. if( node.KeywordModeTypeValue == KeywordModeType.KeywordEnum )
  869. {
  870. string[] allOutputs = new string[ node.KeywordEnumAmount ];
  871. for ( int i = 0; i < node.KeywordEnumAmount; i++ )
  872. allOutputs[ i ] = m_inputPorts[ i ].GeneratePortInstructions( ref dataCollector );
  873. if ( m_multiCompile == ( int )KeywordType.DynamicBranch )
  874. {
  875. dataCollector.AddLocalVariable( UniqueId, outType + " dynamicSwitch" + OutputId + " = ( " + outType + " )0;", true );
  876. for ( int i = 0; i < node.KeywordEnumAmount; i++ )
  877. {
  878. string keyword = node.KeywordEnum( i );
  879. if ( i == 0 )
  880. dataCollector.AddLocalVariable( UniqueId, "UNITY_BRANCH if ( " + keyword + " )", true );
  881. else
  882. dataCollector.AddLocalVariable( UniqueId, "else if ( " + keyword + " )", true );
  883. dataCollector.AddLocalVariable( UniqueId, "{", true );
  884. dataCollector.AddLocalVariable( UniqueId, "\tdynamicSwitch" + OutputId + " = " + allOutputs[ i ] + ";", true );
  885. dataCollector.AddLocalVariable( UniqueId, "}", true );
  886. }
  887. dataCollector.AddLocalVariable( UniqueId, "else", true );
  888. dataCollector.AddLocalVariable( UniqueId, "{", true );
  889. dataCollector.AddLocalVariable( UniqueId, "\tdynamicSwitch" + OutputId + " = " + allOutputs[ node.DefaultValue ] + ";", true );
  890. dataCollector.AddLocalVariable( UniqueId, "}", true );
  891. }
  892. else
  893. {
  894. for ( int i = 0; i < node.KeywordEnumAmount; i++ )
  895. {
  896. string keyword = node.KeywordEnum( i );
  897. if ( i == 0 )
  898. dataCollector.AddLocalVariable( UniqueId, "#if defined( " + keyword + " )", true );
  899. else
  900. dataCollector.AddLocalVariable( UniqueId, "#elif defined( " + keyword + " )", true );
  901. dataCollector.AddLocalVariable( UniqueId, "\t" + outType + " staticSwitch" + OutputId + " = " + allOutputs[ i ] + ";", true );
  902. }
  903. dataCollector.AddLocalVariable( UniqueId, "#else", true );
  904. dataCollector.AddLocalVariable( UniqueId, "\t" + outType + " staticSwitch" + OutputId + " = " + allOutputs[ node.DefaultValue ] + ";", true );
  905. dataCollector.AddLocalVariable( UniqueId, "#endif", true );
  906. }
  907. }
  908. else
  909. {
  910. string falseCode = m_inputPorts[ 0 ].GeneratePortInstructions( ref dataCollector );
  911. string trueCode = m_inputPorts[ 1 ].GeneratePortInstructions( ref dataCollector );
  912. if ( m_multiCompile == ( int )KeywordType.DynamicBranch )
  913. {
  914. dataCollector.AddLocalVariable( UniqueId, outType + " dynamicSwitch" + OutputId + " = ( " + outType + " )0;", true );
  915. dataCollector.AddLocalVariable( UniqueId, "UNITY_BRANCH if ( " + node.CurrentKeyword + " )", true );
  916. dataCollector.AddLocalVariable( UniqueId, "{", true );
  917. dataCollector.AddLocalVariable( UniqueId, "\tdynamicSwitch" + OutputId + " = " + trueCode + ";", true );
  918. dataCollector.AddLocalVariable( UniqueId, "}", true );
  919. dataCollector.AddLocalVariable( UniqueId, "else", true );
  920. dataCollector.AddLocalVariable( UniqueId, "{", true );
  921. dataCollector.AddLocalVariable( UniqueId, "\tdynamicSwitch" + OutputId + " = " + falseCode + ";", true );
  922. dataCollector.AddLocalVariable( UniqueId, "}", true );
  923. m_outputPorts[ 0 ].SetLocalValue( "dynamicSwitch" + OutputId, dataCollector.PortCategory );
  924. }
  925. else
  926. {
  927. //if( node.CurrentVarMode == StaticSwitchVariableMode.Fetch )
  928. dataCollector.AddLocalVariable( UniqueId, "#ifdef " + node.CurrentKeyword, true );
  929. //else
  930. // dataCollector.AddLocalVariable( UniqueId, "#ifdef " + node.PropertyName + OnOffStr, true );
  931. dataCollector.AddLocalVariable( UniqueId, "\t" + outType + " staticSwitch" + OutputId + " = " + trueCode + ";", true );
  932. dataCollector.AddLocalVariable( UniqueId, "#else", true );
  933. dataCollector.AddLocalVariable( UniqueId, "\t" + outType + " staticSwitch" + OutputId + " = " + falseCode + ";", true );
  934. dataCollector.AddLocalVariable( UniqueId, "#endif", true );
  935. m_outputPorts[ 0 ].SetLocalValue( "staticSwitch" + OutputId, dataCollector.PortCategory );
  936. }
  937. }
  938. if ( m_multiCompile == ( int )KeywordType.DynamicBranch )
  939. {
  940. m_outputPorts[ 0 ].SetLocalValue( "dynamicSwitch" + OutputId, dataCollector.PortCategory );
  941. }
  942. else
  943. {
  944. m_outputPorts[ 0 ].SetLocalValue( "staticSwitch" + OutputId, dataCollector.PortCategory );
  945. }
  946. return m_outputPorts[ 0 ].LocalValue( dataCollector.PortCategory );
  947. }
  948. public override void DrawTitle( Rect titlePos )
  949. {
  950. bool referenceMode = m_staticSwitchVarMode == StaticSwitchVariableMode.Reference && m_reference != null;
  951. string subTitle = string.Empty;
  952. string subTitleFormat = string.Empty;
  953. if( referenceMode )
  954. {
  955. subTitle = m_reference.GetPropertyValStr();
  956. subTitleFormat = Constants.SubTitleRefNameFormatStr;
  957. }
  958. else
  959. {
  960. subTitle = GetPropertyValStr();
  961. subTitleFormat = GetSubTitleVarNameFormatStr();
  962. }
  963. SetAdditonalTitleTextOnCallback( subTitle, subTitleFormat, ( instance, newSubTitle ) => instance.AdditonalTitleContent.text = string.Format( subTitleFormat, newSubTitle ) );
  964. if( !m_isEditing && ContainerGraph.LodLevel <= ParentGraph.NodeLOD.LOD3 )
  965. {
  966. GUI.Label( titlePos, StaticSwitchStr, UIUtils.GetCustomStyle( CustomStyle.NodeTitle ) );
  967. }
  968. }
  969. public override void UpdateMaterial( Material mat )
  970. {
  971. base.UpdateMaterial( mat );
  972. if( UIUtils.IsProperty( m_currentParameterType ) && !InsideShaderFunction )
  973. {
  974. if( m_keywordModeType == KeywordModeType.KeywordEnum )
  975. {
  976. for( int i = 0; i < m_keywordEnumAmount; i++ )
  977. {
  978. string key = KeywordEnum( i );
  979. mat.DisableKeyword( key );
  980. }
  981. mat.EnableKeyword( KeywordEnum( m_materialValue ));
  982. mat.SetFloat( m_propertyName, m_materialValue );
  983. }
  984. else
  985. {
  986. int final = m_materialValue;
  987. if( m_keywordModeType == KeywordModeType.ToggleOff )
  988. final = final == 1 ? 0 : 1;
  989. mat.SetFloat( m_propertyName, m_materialValue );
  990. if( final == 1 )
  991. mat.EnableKeyword( GetPropertyValStr() );
  992. else
  993. mat.DisableKeyword( GetPropertyValStr() );
  994. }
  995. }
  996. }
  997. public override void SetMaterialMode( Material mat, bool fetchMaterialValues )
  998. {
  999. base.SetMaterialMode( mat, fetchMaterialValues );
  1000. if( fetchMaterialValues && m_materialMode && UIUtils.IsProperty( m_currentParameterType ) && mat.HasProperty( m_propertyName ) )
  1001. {
  1002. m_materialValue = mat.GetInt( m_propertyName );
  1003. }
  1004. }
  1005. public override void ForceUpdateFromMaterial( Material material )
  1006. {
  1007. if( UIUtils.IsProperty( m_currentParameterType ) && material.HasProperty( m_propertyName ) )
  1008. {
  1009. m_materialValue = material.GetInt( m_propertyName );
  1010. PreviewIsDirty = true;
  1011. }
  1012. }
  1013. public override void ReadFromString( ref string[] nodeParams )
  1014. {
  1015. base.ReadFromString( ref nodeParams );
  1016. m_multiCompile = Convert.ToInt32( GetCurrentParam( ref nodeParams ) );
  1017. if( UIUtils.CurrentShaderVersion() > 14403 )
  1018. {
  1019. m_defaultValue = Convert.ToInt32( GetCurrentParam( ref nodeParams ) );
  1020. if( UIUtils.CurrentShaderVersion() > 14101 )
  1021. {
  1022. m_materialValue = Convert.ToInt32( GetCurrentParam( ref nodeParams ) );
  1023. }
  1024. }
  1025. else
  1026. {
  1027. m_defaultValue = Convert.ToBoolean( GetCurrentParam( ref nodeParams ) ) ? 1 : 0;
  1028. if( UIUtils.CurrentShaderVersion() > 14101 )
  1029. {
  1030. m_materialValue = Convert.ToBoolean( GetCurrentParam( ref nodeParams ) ) ? 1 : 0;
  1031. }
  1032. }
  1033. if( UIUtils.CurrentShaderVersion() > 13104 )
  1034. {
  1035. m_createToggle = Convert.ToBoolean( GetCurrentParam( ref nodeParams ) );
  1036. m_currentKeyword = GetCurrentParam( ref nodeParams );
  1037. m_currentKeywordId = UIUtils.GetKeywordId( m_currentKeyword );
  1038. }
  1039. if( UIUtils.CurrentShaderVersion() > 14001 )
  1040. {
  1041. m_keywordModeType = (KeywordModeType)Enum.Parse( typeof( KeywordModeType ), GetCurrentParam( ref nodeParams ) );
  1042. }
  1043. if( UIUtils.CurrentShaderVersion() > 14403 )
  1044. {
  1045. KeywordEnumAmount = Convert.ToInt32( GetCurrentParam( ref nodeParams ) );
  1046. for( int i = 0; i < KeywordEnumAmount; i++ )
  1047. {
  1048. m_defaultKeywordNames[ i ] = GetCurrentParam( ref nodeParams );
  1049. }
  1050. UpdateLabels();
  1051. }
  1052. if( UIUtils.CurrentShaderVersion() > 16304 )
  1053. {
  1054. string currentVarMode = GetCurrentParam( ref nodeParams );
  1055. CurrentVarMode = (StaticSwitchVariableMode)Enum.Parse( typeof( StaticSwitchVariableMode ), currentVarMode );
  1056. if( CurrentVarMode == StaticSwitchVariableMode.Reference )
  1057. {
  1058. m_referenceNodeId = Convert.ToInt32( GetCurrentParam( ref nodeParams ) );
  1059. }
  1060. }
  1061. else
  1062. {
  1063. CurrentVarMode = (StaticSwitchVariableMode)m_variableMode;
  1064. //Resetting m_variableMode to its default value since it will no longer be used and interfere released ransom properties behavior
  1065. m_variableMode = VariableMode.Create;
  1066. }
  1067. if( CurrentVarMode == StaticSwitchVariableMode.Reference )
  1068. {
  1069. UIUtils.UnregisterPropertyNode( this );
  1070. }
  1071. else
  1072. {
  1073. if( m_createToggle )
  1074. UIUtils.RegisterPropertyNode( this );
  1075. else
  1076. UIUtils.UnregisterPropertyNode( this );
  1077. }
  1078. if( UIUtils.CurrentShaderVersion() > 16700 )
  1079. {
  1080. m_isLocal = Convert.ToBoolean( GetCurrentParam( ref nodeParams ) );
  1081. }
  1082. if( UIUtils.CurrentShaderVersion() > 18401 )
  1083. m_lockKeyword = Convert.ToBoolean( GetCurrentParam( ref nodeParams ) );
  1084. if( UIUtils.CurrentShaderVersion() > 18928 )
  1085. m_shaderStage = (ShaderStage)Enum.Parse( typeof(ShaderStage), GetCurrentParam( ref nodeParams ) );
  1086. SetMaterialToggleRetrocompatibility();
  1087. if( !m_isNodeBeingCopied && CurrentVarMode != StaticSwitchVariableMode.Reference )
  1088. {
  1089. ContainerGraph.StaticSwitchNodes.UpdateDataOnNode( UniqueId, DataToArray );
  1090. }
  1091. }
  1092. public override void ReleaseRansomedProperty()
  1093. {
  1094. //on old ASE, the property node m_variableMode was used on defining the static switch type, now we have a specific m_staticSwitchVarMode over here
  1095. //the problem with this is the fix made to release ransomend property names( hash deb232819fff0f1aeaf029a21c55ef597b3424de ) uses m_variableMode and
  1096. //makes old static switches to attempt and register an already registered name when doing this:
  1097. //CurrentVariableMode = VariableMode.Create;
  1098. //So we need to disable this release ransom property behavior as m_variableMode should never be on VariableMode.Create
  1099. //The m_variableMode is set to its default value over the ReadFromString method after its value as been set over the new m_staticSwitchVarMode variable
  1100. }
  1101. void SetMaterialToggleRetrocompatibility()
  1102. {
  1103. if( UIUtils.CurrentShaderVersion() < 17108 )
  1104. {
  1105. if( !m_createToggle && m_staticSwitchVarMode == StaticSwitchVariableMode.Create )
  1106. {
  1107. if( m_keywordModeType != KeywordModeType.KeywordEnum )
  1108. {
  1109. m_propertyName = m_propertyName.ToUpper() + "_ON";
  1110. }
  1111. else
  1112. {
  1113. m_propertyName = m_propertyName.ToUpper();
  1114. for( int i = 0; i < m_keywordEnumList.Length; i++ )
  1115. {
  1116. m_keywordEnumList[ i ] = "_" + m_keywordEnumList[ i ].ToUpper();
  1117. }
  1118. }
  1119. m_autoGlobalName = false;
  1120. }
  1121. }
  1122. }
  1123. public override void ReadFromDeprecated( ref string[] nodeParams, Type oldType = null )
  1124. {
  1125. base.ReadFromDeprecated( ref nodeParams, oldType );
  1126. {
  1127. m_currentKeyword = GetCurrentParam( ref nodeParams );
  1128. m_currentKeywordId = UIUtils.GetKeywordId( m_currentKeyword );
  1129. m_createToggle = false;
  1130. m_keywordModeType = KeywordModeType.Toggle;
  1131. m_variableMode = VariableMode.Fetch;
  1132. CurrentVarMode = StaticSwitchVariableMode.Fetch;
  1133. }
  1134. }
  1135. public override void WriteToString( ref string nodeInfo, ref string connectionsInfo )
  1136. {
  1137. base.WriteToString( ref nodeInfo, ref connectionsInfo );
  1138. IOUtils.AddFieldValueToString( ref nodeInfo, m_multiCompile );
  1139. IOUtils.AddFieldValueToString( ref nodeInfo, m_defaultValue );
  1140. IOUtils.AddFieldValueToString( ref nodeInfo, m_materialValue );
  1141. IOUtils.AddFieldValueToString( ref nodeInfo, m_createToggle );
  1142. IOUtils.AddFieldValueToString( ref nodeInfo, m_currentKeyword );
  1143. IOUtils.AddFieldValueToString( ref nodeInfo, m_keywordModeType );
  1144. IOUtils.AddFieldValueToString( ref nodeInfo, KeywordEnumAmount );
  1145. for( int i = 0; i < KeywordEnumAmount; i++ )
  1146. {
  1147. IOUtils.AddFieldValueToString( ref nodeInfo, m_keywordEnumList[ i ] );
  1148. }
  1149. IOUtils.AddFieldValueToString( ref nodeInfo, CurrentVarMode );
  1150. if( CurrentVarMode == StaticSwitchVariableMode.Reference )
  1151. {
  1152. int referenceId = ( m_reference != null ) ? m_reference.UniqueId : -1;
  1153. IOUtils.AddFieldValueToString( ref nodeInfo, referenceId );
  1154. }
  1155. IOUtils.AddFieldValueToString( ref nodeInfo, m_isLocal );
  1156. IOUtils.AddFieldValueToString( ref nodeInfo, m_lockKeyword );
  1157. IOUtils.AddFieldValueToString( ref nodeInfo , m_shaderStage );
  1158. }
  1159. public override void RefreshExternalReferences()
  1160. {
  1161. base.RefreshExternalReferences();
  1162. CheckReferenceValues( true );
  1163. }
  1164. StaticSwitchVariableMode CurrentVarMode
  1165. {
  1166. get { return m_staticSwitchVarMode; }
  1167. set
  1168. {
  1169. if( m_staticSwitchVarMode != value )
  1170. {
  1171. if( value == StaticSwitchVariableMode.Reference )
  1172. {
  1173. ContainerGraph.StaticSwitchNodes.RemoveNode( this );
  1174. m_referenceArrayId = -1;
  1175. m_referenceNodeId = -1;
  1176. m_reference = null;
  1177. m_headerColorModifier = ReferenceHeaderColor;
  1178. }
  1179. else
  1180. {
  1181. m_headerColorModifier = Color.white;
  1182. ContainerGraph.StaticSwitchNodes.AddNode( this );
  1183. UpdateLabels();
  1184. }
  1185. }
  1186. m_staticSwitchVarMode = value;
  1187. }
  1188. }
  1189. public bool IsStaticSwitchDirty { get { return m_isStaticSwitchDirty; } }
  1190. public KeywordModeType KeywordModeTypeValue { get { return m_keywordModeType; } }
  1191. public int DefaultValue { get { return m_defaultValue; } }
  1192. public int MaterialValue { get { return m_materialValue; } }
  1193. //public string CurrentKeyword { get { return m_currentKeyword; } }
  1194. public string CurrentKeyword
  1195. {
  1196. get
  1197. {
  1198. if( CurrentVarMode == StaticSwitchVariableMode.Fetch )
  1199. return m_currentKeyword;
  1200. return ( m_lockKeyword || string.IsNullOrEmpty( m_currentKeyword ) ? PropertyName + OnOffStr : m_currentKeyword );
  1201. }
  1202. }
  1203. public bool CreateToggle { get { return m_createToggle; } }
  1204. public int KeywordEnumAmount
  1205. {
  1206. get
  1207. {
  1208. return m_keywordEnumAmount;
  1209. }
  1210. set
  1211. {
  1212. m_keywordEnumAmount = value;
  1213. m_defaultValue = Mathf.Clamp( m_defaultValue, 0, m_keywordEnumAmount - 1 );
  1214. m_materialValue = Mathf.Clamp( m_defaultValue, 0, m_keywordEnumAmount - 1 );
  1215. }
  1216. }
  1217. }
  1218. }