FunctionNode.cs 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262
  1. // Amplify Shader Editor - Visual Shader Editing Tool
  2. // Copyright (c) Amplify Creations, Lda <info@amplify.pt>
  3. //#define ADD_SHADER_FUNCTION_HEADERS
  4. using UnityEngine;
  5. using UnityEditor;
  6. using System.Collections.Generic;
  7. using System;
  8. namespace AmplifyShaderEditor
  9. {
  10. [Serializable]
  11. [NodeAttributes( "Function Node", "Functions", "Function Node", KeyCode.None, false, 0, int.MaxValue, typeof( AmplifyShaderFunction ) )]
  12. public class FunctionNode : ParentNode
  13. {
  14. [SerializeField]
  15. private AmplifyShaderFunction m_function;
  16. [SerializeField]
  17. private ParentGraph m_functionGraph;
  18. [SerializeField]
  19. private int m_functionGraphId = -1;
  20. [SerializeField]
  21. private List<FunctionInput> m_allFunctionInputs;
  22. private Dictionary<int, FunctionInput> m_allFunctionInputsDict = new Dictionary<int, FunctionInput>();
  23. [SerializeField]
  24. private List<FunctionOutput> m_allFunctionOutputs;
  25. private Dictionary<int, FunctionOutput> m_allFunctionOutputsDict = new Dictionary<int, FunctionOutput>();
  26. [SerializeField]
  27. private List<FunctionSwitch> m_allFunctionSwitches;
  28. private Dictionary<int, FunctionSwitch> m_allFunctionSwitchesDict = new Dictionary<int, FunctionSwitch>();
  29. [SerializeField]
  30. private ReordenatorNode m_reordenator;
  31. [SerializeField]
  32. private string m_filename;
  33. [SerializeField]
  34. private string m_headerTitle = string.Empty;
  35. [SerializeField]
  36. private int m_orderIndex;
  37. [SerializeField]
  38. private string m_functionCheckSum;
  39. [SerializeField]
  40. private string m_functionGUID = string.Empty;
  41. //[SerializeField]
  42. //private List<string> m_includes = new List<string>();
  43. //[SerializeField]
  44. //private List<string> m_pragmas = new List<string>();
  45. [SerializeField]
  46. private List<AdditionalDirectiveContainer> m_directives = new List<AdditionalDirectiveContainer>();
  47. private bool m_parametersFoldout = true;
  48. [SerializeField]
  49. private ParentGraph m_outsideGraph = null;
  50. [SerializeField]
  51. private FunctionOutput m_mainPreviewNode;
  52. bool m_portsChanged = false;
  53. //[SerializeField]
  54. bool m_initialGraphDraw = false;
  55. private bool m_refreshIdsRequired = false;
  56. public string[] ReadOptionsHelper = new string[] { };
  57. private bool m_lateRefresh = false;
  58. private Texture2D m_headerIcon = null;
  59. public Texture2D HeaderIcon
  60. {
  61. get
  62. {
  63. if ( m_headerIcon == null )
  64. {
  65. m_headerIcon = AssetDatabase.LoadAssetAtPath<Texture2D>( AssetDatabase.GUIDToAssetPath( "5b59dcde73b12b746af7ff7c47a1195d" ) );
  66. }
  67. return m_headerIcon;
  68. }
  69. }
  70. public const int HeaderIconSize = 32;
  71. private Dictionary<int, bool> m_duplicatesBuffer = new Dictionary<int, bool>();
  72. string LastLine( string text )
  73. {
  74. string[] lines = text.Replace( "\r", "" ).Split( '\n' );
  75. return lines[ lines.Length - 1 ];
  76. }
  77. public void CommonInit( AmplifyShaderFunction function, int uniqueId )
  78. {
  79. SetBaseUniqueId( uniqueId );
  80. if( function == null )
  81. return;
  82. m_refreshIdsRequired = UIUtils.IsLoading && ( UIUtils.CurrentShaderVersion() < 14004 );
  83. m_function = function;
  84. if( Function.FunctionName.Length > 1 )
  85. {
  86. SetTitleText( GraphContextMenu.AddSpacesToSentence( Function.FunctionName ) );
  87. }
  88. else
  89. {
  90. SetTitleText( Function.FunctionName );
  91. }
  92. m_tooltipText = Function.Description;
  93. m_hasTooltipLink = false;
  94. if( m_functionGraph == null )
  95. {
  96. //m_functionGraph = new ParentGraph();
  97. m_functionGraph = CreateInstance<ParentGraph>();
  98. m_functionGraph.Init();
  99. m_functionGraph.ParentWindow = ContainerGraph.ParentWindow;
  100. }
  101. if( string.IsNullOrEmpty( m_functionGUID ) )
  102. {
  103. m_functionGUID = AssetDatabase.AssetPathToGUID( AssetDatabase.GetAssetPath( m_function ) );
  104. }
  105. m_functionGraphId = Mathf.Max( m_functionGraphId, ContainerGraph.ParentWindow.GraphCount );
  106. ContainerGraph.ParentWindow.GraphCount = m_functionGraphId + 1;
  107. m_functionGraph.SetGraphId( m_functionGraphId );
  108. ParentGraph cachedGraph = ContainerGraph.ParentWindow.CustomGraph;
  109. ContainerGraph.ParentWindow.CustomGraph = m_functionGraph;
  110. AmplifyShaderEditorWindow.LoadFromMeta( ref m_functionGraph, ContainerGraph.ParentWindow.ContextMenuInstance, Function.FunctionInfo );
  111. //m_functionCheckSum = LastLine( m_function.FunctionInfo );
  112. m_functionCheckSum = AssetDatabase.GetAssetDependencyHash( AssetDatabase.GetAssetPath( m_function ) ).ToString();
  113. List<PropertyNode> propertyList = UIUtils.PropertyNodesList();
  114. m_allFunctionInputs = UIUtils.FunctionInputList();
  115. m_allFunctionOutputs = UIUtils.FunctionOutputList();
  116. m_allFunctionSwitches = UIUtils.FunctionSwitchList();
  117. ContainerGraph.ParentWindow.CustomGraph = cachedGraph;
  118. m_allFunctionInputs.Sort( ( x, y ) => { return x.OrderIndex.CompareTo( y.OrderIndex ); } );
  119. m_allFunctionOutputs.Sort( ( x, y ) => { return x.OrderIndex.CompareTo( y.OrderIndex ); } );
  120. m_allFunctionSwitches.Sort( ( x, y ) => { return x.OrderIndex.CompareTo( y.OrderIndex ); } );
  121. int inputCount = m_allFunctionInputs.Count;
  122. for( int i = 0; i < inputCount; i++ )
  123. {
  124. if( m_refreshIdsRequired )
  125. {
  126. AddInputPort( m_allFunctionInputs[ i ].SelectedInputType, false, m_allFunctionInputs[ i ].InputName );
  127. }
  128. else
  129. {
  130. AddInputPort( m_allFunctionInputs[ i ].SelectedInputType, false, m_allFunctionInputs[ i ].InputName, -1, MasterNodePortCategory.Fragment, m_allFunctionInputs[ i ].UniqueId );
  131. }
  132. InputPortSwitchRestriction( m_inputPorts[ i ] );
  133. if( !m_allFunctionInputs[ i ].InputPorts[ 0 ].IsConnected )
  134. {
  135. m_inputPorts[ i ].AutoDrawInternalData = true;
  136. m_inputPorts[ i ].InternalData = m_allFunctionInputs[ i ].InputPorts[ 0 ].InternalData;
  137. }
  138. m_allFunctionInputs[ i ].Fnode = this;
  139. }
  140. int outputCount = m_allFunctionOutputs.Count;
  141. FunctionOutput first = null;
  142. for( int i = 0; i < outputCount; i++ )
  143. {
  144. if( i == 0 )
  145. first = m_allFunctionOutputs[ i ];
  146. if( m_allFunctionOutputs[ i ].PreviewNode )
  147. {
  148. m_mainPreviewNode = m_allFunctionOutputs[ i ];
  149. }
  150. if( m_refreshIdsRequired )
  151. {
  152. AddOutputPort( m_allFunctionOutputs[ i ].AutoOutputType, m_allFunctionOutputs[ i ].OutputName );
  153. }
  154. else
  155. {
  156. AddOutputPort( m_allFunctionOutputs[ i ].AutoOutputType, m_allFunctionOutputs[ i ].OutputName, m_allFunctionOutputs[ i ].UniqueId );
  157. }
  158. OutputPortSwitchRestriction( m_outputPorts[ i ] );
  159. }
  160. // make sure to hide the ports properly
  161. CheckPortVisibility();
  162. if( m_mainPreviewNode == null )
  163. m_mainPreviewNode = first;
  164. //create reordenator to main graph
  165. bool inside = false;
  166. if( ContainerGraph.ParentWindow.CustomGraph != null )
  167. inside = true;
  168. if( /*hasConnectedProperties*/propertyList.Count > 0 )
  169. {
  170. m_reordenator = ScriptableObject.CreateInstance<ReordenatorNode>();
  171. m_reordenator.Init( "_" + Function.FunctionName, Function.FunctionName, propertyList, false );
  172. m_reordenator.OrderIndex = m_orderIndex;
  173. m_reordenator.HeaderTitle = Function.FunctionName;
  174. m_reordenator.IsInside = inside;
  175. }
  176. if( m_reordenator != null )
  177. {
  178. cachedGraph = ContainerGraph.ParentWindow.CustomGraph;
  179. ContainerGraph.ParentWindow.CustomGraph = null;
  180. UIUtils.RegisterPropertyNode( m_reordenator );
  181. ContainerGraph.ParentWindow.CustomGraph = cachedGraph;
  182. if( inside )
  183. {
  184. UIUtils.RegisterPropertyNode( m_reordenator );
  185. }
  186. }
  187. UIUtils.RegisterFunctionNode( this );
  188. RefreshUIHeaderColor();
  189. m_previewShaderGUID = "aca70c900c50c004e8ef0b47c4fac4d4";
  190. m_useInternalPortData = false;
  191. m_selectedLocation = function.PreviewPosition;
  192. UIUtils.CurrentWindow.OutsideGraph.OnLODMasterNodesAddedEvent += OnLODMasterNodesAddedEvent;
  193. }
  194. public InputPort GetInput( FunctionInput input )
  195. {
  196. int index = m_allFunctionInputs.FindIndex( ( x ) => { return x.Equals( input ); } );
  197. if( index >= 0 )
  198. return InputPorts[ index ];
  199. else
  200. return null;
  201. }
  202. private void OnLODMasterNodesAddedEvent( int lod )
  203. {
  204. AddShaderFunctionDirectivesInternal( lod );
  205. }
  206. public void SetPreviewInput( InputPort input )
  207. {
  208. if( !HasPreviewShader || !m_initialized )
  209. return;
  210. if( input.IsConnected && input.InputNodeHasPreview( ContainerGraph ) )
  211. {
  212. input.SetPreviewInputTexture( ContainerGraph );
  213. }
  214. else
  215. {
  216. input.SetPreviewInputValue( ContainerGraph );
  217. }
  218. }
  219. public override bool RecursivePreviewUpdate( Dictionary<string, bool> duplicatesDict = null )
  220. {
  221. if( duplicatesDict == null )
  222. {
  223. duplicatesDict = ContainerGraph.ParentWindow.VisitedChanged;
  224. }
  225. if( m_allFunctionOutputs == null || m_allFunctionOutputs.Count == 0 )
  226. return false;
  227. for( int i = 0; i < m_allFunctionOutputs.Count; i++ )
  228. {
  229. ParentNode outNode = m_allFunctionOutputs[ i ];
  230. if( outNode != null )
  231. {
  232. if( !duplicatesDict.ContainsKey( outNode.OutputId ) )
  233. {
  234. bool result = outNode.RecursivePreviewUpdate();
  235. if( result )
  236. PreviewIsDirty = true;
  237. }
  238. else if( duplicatesDict[ outNode.OutputId ] )
  239. {
  240. PreviewIsDirty = true;
  241. }
  242. }
  243. }
  244. bool needsUpdate = PreviewIsDirty;
  245. RenderNodePreview();
  246. if( !duplicatesDict.ContainsKey( OutputId ) )
  247. duplicatesDict.Add( OutputId, needsUpdate );
  248. return needsUpdate;
  249. }
  250. public override void RenderNodePreview()
  251. {
  252. if( m_outputPorts == null )
  253. return;
  254. if( !PreviewIsDirty && !m_continuousPreviewRefresh )
  255. return;
  256. // this is in the wrong place??
  257. if( m_drawPreviewAsSphere != m_mainPreviewNode.SpherePreview )
  258. {
  259. m_drawPreviewAsSphere = m_mainPreviewNode.SpherePreview;
  260. OnNodeChange();
  261. }
  262. int count = m_outputPorts.Count;
  263. for( int i = 0; i < count; i++ )
  264. {
  265. m_outputPorts[ i ].OutputPreviewTexture = m_allFunctionOutputs[ i ].PreviewTexture;
  266. }
  267. if( PreviewIsDirty )
  268. FinishPreviewRender = true;
  269. PreviewIsDirty = false;
  270. }
  271. public override RenderTexture PreviewTexture
  272. {
  273. get
  274. {
  275. if( m_mainPreviewNode != null )
  276. return m_mainPreviewNode.PreviewTexture;
  277. else
  278. return base.PreviewTexture;
  279. }
  280. }
  281. private void AddShaderFunctionDirectivesInternal( int lod )
  282. {
  283. List<TemplateMultiPassMasterNode> nodes = ContainerGraph.ParentWindow.OutsideGraph.GetMultiPassMasterNodes( lod );
  284. int count = nodes.Count;
  285. for( int i = 0; i < count; i++ )
  286. {
  287. nodes[ i ].PassModule.AdditionalDirectives.AddShaderFunctionItems( OutputId, Function.AdditionalDirectives.DirectivesList );
  288. }
  289. }
  290. public override void RefreshExternalReferences()
  291. {
  292. base.RefreshExternalReferences();
  293. if( Function == null )
  294. return;
  295. //Debug.Log( "RefreshExternalReferences " + m_function.FunctionName + " " + UIUtils.CurrentWindow.IsShaderFunctionWindow );
  296. Function.UpdateDirectivesList();
  297. MasterNode masterNode = UIUtils.CurrentWindow.OutsideGraph.CurrentMasterNode;
  298. StandardSurfaceOutputNode surface = masterNode as StandardSurfaceOutputNode;
  299. if( surface != null )
  300. {
  301. //for( int i = 0; i < Function.AdditionalIncludes.IncludeList.Count; i++ )
  302. //{
  303. // //ContainerGraph.ParentWindow.OutsideGraph.CurrentStandardSurface.AdditionalIncludes.OutsideList.Add( Function.AdditionalIncludes.IncludeList[ i ] );
  304. // ContainerGraph.ParentWindow.OutsideGraph.CurrentStandardSurface.AdditionalDirectives.AddShaderFunctionItem( AdditionalLineType.Include, Function.AdditionalIncludes.IncludeList[ i ] );
  305. // m_includes.Add( Function.AdditionalIncludes.IncludeList[ i ] );
  306. //}
  307. //for( int i = 0; i < Function.AdditionalPragmas.PragmaList.Count; i++ )
  308. //{
  309. // //ContainerGraph.ParentWindow.OutsideGraph.CurrentStandardSurface.AdditionalPragmas.OutsideList.Add( Function.AdditionalPragmas.PragmaList[ i ] );
  310. // ContainerGraph.ParentWindow.OutsideGraph.CurrentStandardSurface.AdditionalDirectives.AddShaderFunctionItem(AdditionalLineType.Pragma, Function.AdditionalPragmas.PragmaList[ i ] );
  311. // m_pragmas.Add( Function.AdditionalPragmas.PragmaList[ i ] );
  312. //}
  313. surface.AdditionalDirectives.AddShaderFunctionItems( OutputId, Function.AdditionalDirectives.DirectivesList );
  314. }
  315. else
  316. {
  317. if( ContainerGraph.ParentWindow.OutsideGraph.MultiPassMasterNodes.Count > 0 )
  318. {
  319. for( int lod = -1; lod < ContainerGraph.ParentWindow.OutsideGraph.LodMultiPassMasternodes.Count; lod++ )
  320. {
  321. AddShaderFunctionDirectivesInternal( lod );
  322. }
  323. }
  324. else
  325. {
  326. // Assuring that we're not editing a Shader Function, as directives setup is not needed there
  327. if( !UIUtils.CurrentWindow.IsShaderFunctionWindow )
  328. {
  329. // This function is nested inside a shader function itself and this method
  330. // was called before the main output node was created.
  331. // This is possible since all nodes RefreshExternalReferences(...) are called at the end
  332. // of a LoadFromMeta
  333. // Need to delay this setup to after all nodes are loaded to then setup the directives
  334. m_lateRefresh = true;
  335. return;
  336. }
  337. }
  338. }
  339. m_directives.AddRange( Function.AdditionalDirectives.DirectivesList );
  340. if( m_refreshIdsRequired )
  341. {
  342. m_refreshIdsRequired = false;
  343. int inputCount = m_inputPorts.Count;
  344. for( int i = 0; i < inputCount; i++ )
  345. {
  346. m_inputPorts[ i ].ChangePortId( m_allFunctionInputs[ i ].UniqueId );
  347. }
  348. int outputCount = m_outputPorts.Count;
  349. for( int i = 0; i < outputCount; i++ )
  350. {
  351. m_outputPorts[ i ].ChangePortId( m_allFunctionOutputs[ i ].UniqueId );
  352. }
  353. }
  354. if( ContainerGraph.ParentWindow.CurrentGraph != m_functionGraph )
  355. ContainerGraph.ParentWindow.CurrentGraph.InstancePropertyCount += m_functionGraph.InstancePropertyCount;
  356. ParentGraph cachedGraph = ContainerGraph.ParentWindow.CustomGraph;
  357. ContainerGraph.ParentWindow.CustomGraph = m_functionGraph;
  358. if( ReadOptionsHelper.Length > 2 )
  359. {
  360. for( int i = 1; i < ReadOptionsHelper.Length; i += 2 )
  361. {
  362. int optionId = Convert.ToInt32( ReadOptionsHelper[ i ] );
  363. int optionValue = Convert.ToInt32( ReadOptionsHelper[ i + 1 ] );
  364. for( int j = 0; j < m_allFunctionSwitches.Count; j++ )
  365. {
  366. if( m_allFunctionSwitches[ j ].UniqueId == optionId )
  367. {
  368. m_allFunctionSwitches[ j ].SetCurrentSelectedInput( optionValue, m_allFunctionSwitches[ j ].GetCurrentSelectedInput() );
  369. break;
  370. }
  371. }
  372. }
  373. }
  374. ContainerGraph.ParentWindow.CustomGraph = cachedGraph;
  375. m_portsChanged = true;
  376. }
  377. void InputPortSwitchRestriction( WirePort port )
  378. {
  379. switch( port.DataType )
  380. {
  381. case WirePortDataType.OBJECT:
  382. break;
  383. case WirePortDataType.FLOAT:
  384. case WirePortDataType.FLOAT2:
  385. case WirePortDataType.FLOAT3:
  386. case WirePortDataType.FLOAT4:
  387. case WirePortDataType.COLOR:
  388. case WirePortDataType.INT:
  389. {
  390. port.CreatePortRestrictions( WirePortDataType.FLOAT, WirePortDataType.FLOAT2, WirePortDataType.FLOAT3, WirePortDataType.FLOAT4, WirePortDataType.COLOR, WirePortDataType.INT, WirePortDataType.OBJECT );
  391. }
  392. break;
  393. case WirePortDataType.FLOAT3x3:
  394. case WirePortDataType.FLOAT4x4:
  395. {
  396. port.CreatePortRestrictions( WirePortDataType.FLOAT3x3, WirePortDataType.FLOAT4x4, WirePortDataType.OBJECT );
  397. }
  398. break;
  399. case WirePortDataType.SAMPLER1D:
  400. case WirePortDataType.SAMPLER2D:
  401. case WirePortDataType.SAMPLER3D:
  402. case WirePortDataType.SAMPLERCUBE:
  403. case WirePortDataType.SAMPLER2DARRAY:
  404. {
  405. port.CreatePortRestrictions( WirePortDataType.SAMPLER1D, WirePortDataType.SAMPLER2D, WirePortDataType.SAMPLER3D, WirePortDataType.SAMPLERCUBE, WirePortDataType.SAMPLER2DARRAY, WirePortDataType.OBJECT );
  406. }
  407. break;
  408. case WirePortDataType.SAMPLERSTATE:
  409. {
  410. port.CreatePortRestrictions( WirePortDataType.SAMPLERSTATE );
  411. }
  412. break;
  413. default:
  414. break;
  415. }
  416. }
  417. void OutputPortSwitchRestriction( WirePort port )
  418. {
  419. switch( port.DataType )
  420. {
  421. case WirePortDataType.OBJECT:
  422. break;
  423. case WirePortDataType.FLOAT:
  424. case WirePortDataType.FLOAT2:
  425. case WirePortDataType.FLOAT3:
  426. case WirePortDataType.FLOAT4:
  427. case WirePortDataType.COLOR:
  428. case WirePortDataType.INT:
  429. case WirePortDataType.FLOAT3x3:
  430. case WirePortDataType.FLOAT4x4:
  431. {
  432. port.AddPortForbiddenTypes( WirePortDataType.SAMPLER1D, WirePortDataType.SAMPLER2D, WirePortDataType.SAMPLER3D, WirePortDataType.SAMPLERCUBE, WirePortDataType.SAMPLER2DARRAY );
  433. }
  434. break;
  435. case WirePortDataType.SAMPLER1D:
  436. case WirePortDataType.SAMPLER2D:
  437. case WirePortDataType.SAMPLER3D:
  438. case WirePortDataType.SAMPLERCUBE:
  439. case WirePortDataType.SAMPLER2DARRAY:
  440. {
  441. port.CreatePortRestrictions( WirePortDataType.SAMPLER1D, WirePortDataType.SAMPLER2D, WirePortDataType.SAMPLER3D, WirePortDataType.SAMPLERCUBE, WirePortDataType.SAMPLER2DARRAY, WirePortDataType.OBJECT );
  442. }
  443. break;
  444. case WirePortDataType.SAMPLERSTATE:
  445. {
  446. port.CreatePortRestrictions( WirePortDataType.SAMPLERSTATE );
  447. }
  448. break;
  449. default:
  450. break;
  451. }
  452. }
  453. public override void PropagateNodeData( NodeData nodeData, ref MasterNodeDataCollector dataCollector )
  454. {
  455. ParentGraph cachedGraph = ContainerGraph.ParentWindow.CustomGraph;
  456. m_outsideGraph = cachedGraph;
  457. ContainerGraph.ParentWindow.CustomGraph = m_functionGraph;
  458. for( int i = 0; i < m_allFunctionOutputs.Count; i++ )
  459. {
  460. m_allFunctionOutputs[ i ].PropagateNodeData( nodeData, ref dataCollector );
  461. }
  462. ContainerGraph.ParentWindow.CustomGraph = cachedGraph;
  463. base.PropagateNodeData( nodeData, ref dataCollector );
  464. }
  465. protected override void OnUniqueIDAssigned()
  466. {
  467. base.OnUniqueIDAssigned();
  468. UIUtils.RegisterFunctionNode( this );
  469. }
  470. public override void SetupFromCastObject( UnityEngine.Object obj )
  471. {
  472. base.SetupFromCastObject( obj );
  473. AmplifyShaderFunction function = obj as AmplifyShaderFunction;
  474. CommonInit( function, UniqueId );
  475. RefreshExternalReferences();
  476. }
  477. public override void OnInputPortConnected( int portId, int otherNodeId, int otherPortId, bool activateNode = true )
  478. {
  479. base.OnInputPortConnected( portId, otherNodeId, otherPortId, activateNode );
  480. FunctionInput functionInput = m_refreshIdsRequired ? m_allFunctionInputs[ portId ] : GetFunctionInputByUniqueId( portId );
  481. functionInput.PreviewIsDirty = true;
  482. if( functionInput.AutoCast )
  483. {
  484. InputPort inputPort = m_refreshIdsRequired ? m_inputPorts[ portId ] : GetInputPortByUniqueId( portId );
  485. inputPort.MatchPortToConnection();
  486. ParentGraph cachedGraph = ContainerGraph.ParentWindow.CustomGraph;
  487. ContainerGraph.ParentWindow.CustomGraph = m_functionGraph;
  488. functionInput.ChangeOutputType( inputPort.DataType, false );
  489. ContainerGraph.ParentWindow.CustomGraph = cachedGraph;
  490. }
  491. for( int i = 0; i < m_allFunctionOutputs.Count; i++ )
  492. {
  493. m_outputPorts[ i ].ChangeType( m_allFunctionOutputs[ i ].InputPorts[ 0 ].DataType, false );
  494. }
  495. }
  496. public override void OnInputPortDisconnected( int portId )
  497. {
  498. base.OnInputPortDisconnected( portId );
  499. FunctionInput functionInput = m_refreshIdsRequired ? m_allFunctionInputs[ portId ] : GetFunctionInputByUniqueId( portId );
  500. functionInput.PreviewIsDirty = true;
  501. }
  502. public override void OnConnectedOutputNodeChanges( int inputPortId, int otherNodeId, int otherPortId, string name, WirePortDataType type )
  503. {
  504. base.OnConnectedOutputNodeChanges( inputPortId, otherNodeId, otherPortId, name, type );
  505. FunctionInput functionInput = m_refreshIdsRequired ? m_allFunctionInputs[ inputPortId ] : GetFunctionInputByUniqueId( inputPortId );
  506. functionInput.PreviewIsDirty = true;
  507. if( functionInput.AutoCast )
  508. {
  509. InputPort inputPort = m_refreshIdsRequired ? m_inputPorts[ inputPortId ] : GetInputPortByUniqueId( inputPortId );
  510. inputPort.MatchPortToConnection();
  511. ParentGraph cachedGraph = ContainerGraph.ParentWindow.CustomGraph;
  512. ContainerGraph.ParentWindow.CustomGraph = m_functionGraph;
  513. functionInput.ChangeOutputType( inputPort.DataType, false );
  514. ContainerGraph.ParentWindow.CustomGraph = cachedGraph;
  515. }
  516. for( int i = 0; i < m_allFunctionOutputs.Count; i++ )
  517. {
  518. m_outputPorts[ i ].ChangeType( m_allFunctionOutputs[ i ].InputPorts[ 0 ].DataType, false );
  519. }
  520. }
  521. public override void DrawProperties()
  522. {
  523. base.DrawProperties();
  524. if( Function == null )
  525. return;
  526. if( Function.Description.Length > 0 || m_allFunctionSwitches.Count > 0 )
  527. NodeUtils.DrawPropertyGroup( ref m_parametersFoldout, "Parameters", DrawDescription );
  528. bool drawInternalDataUI = false;
  529. int inputCount = m_inputPorts.Count;
  530. if( inputCount > 0 )
  531. {
  532. for( int i = 0; i < inputCount; i++ )
  533. {
  534. if( m_inputPorts[ i ].Available && m_inputPorts[ i ].ValidInternalData && !m_inputPorts[ i ].IsConnected && m_inputPorts[ i ].AutoDrawInternalData /*&& ( m_inputPorts[ i ].AutoDrawInternalData || ( m_autoDrawInternalPortData && m_useInternalPortData ) )*/ /*&& m_inputPorts[ i ].AutoDrawInternalData*/ )
  535. {
  536. drawInternalDataUI = true;
  537. break;
  538. }
  539. }
  540. }
  541. if( drawInternalDataUI )
  542. NodeUtils.DrawPropertyGroup( ref m_internalDataFoldout, Constants.InternalDataLabelStr, () =>
  543. {
  544. for( int i = 0; i < m_inputPorts.Count; i++ )
  545. {
  546. if( m_inputPorts[ i ].ValidInternalData && !m_inputPorts[ i ].IsConnected && m_inputPorts[ i ].Visible && m_inputPorts[ i ].AutoDrawInternalData )
  547. {
  548. EditorGUI.BeginChangeCheck();
  549. m_inputPorts[ i ].ShowInternalData( this );
  550. if( EditorGUI.EndChangeCheck() )
  551. {
  552. m_allFunctionInputs[ i ].PreviewIsDirty = true;
  553. }
  554. }
  555. }
  556. } );
  557. }
  558. private void DrawDescription()
  559. {
  560. if( Function.Description.Length > 0 )
  561. EditorGUILayout.HelpBox( Function.Description, MessageType.Info );
  562. ParentGraph cachedGraph = ContainerGraph.ParentWindow.CustomGraph;
  563. ContainerGraph.ParentWindow.CustomGraph = m_functionGraph;
  564. for( int i = 0; i < m_allFunctionSwitches.Count; i++ )
  565. {
  566. m_allFunctionSwitches[ i ].AsDrawn = false;
  567. }
  568. for( int i = 0; i < m_allFunctionSwitches.Count; i++ )
  569. {
  570. if( m_allFunctionSwitches[ i ].DrawOption( this ) )
  571. {
  572. m_portsChanged = true;
  573. }
  574. }
  575. ContainerGraph.ParentWindow.CustomGraph = cachedGraph;
  576. }
  577. private void RemoveShaderFunctionDirectivesInternal( int lod )
  578. {
  579. List<TemplateMultiPassMasterNode> nodes = ContainerGraph.ParentWindow.OutsideGraph.GetMultiPassMasterNodes( lod );
  580. int count = nodes.Count;
  581. for( int i = 0; i < count; i++ )
  582. {
  583. nodes[ i ].PassModule.AdditionalDirectives.RemoveShaderFunctionItems( OutputId );
  584. }
  585. }
  586. public override void Destroy()
  587. {
  588. m_mainPreviewNode = null;
  589. base.Destroy();
  590. m_duplicatesBuffer.Clear();
  591. m_duplicatesBuffer = null;
  592. if( m_functionGraph != null && ContainerGraph.ParentWindow.CurrentGraph != m_functionGraph )
  593. ContainerGraph.ParentWindow.CurrentGraph.InstancePropertyCount -= m_functionGraph.InstancePropertyCount;
  594. if( ContainerGraph.ParentWindow.OutsideGraph.CurrentStandardSurface != null )
  595. {
  596. //for( int i = 0; i < m_includes.Count; i++ )
  597. //{
  598. // //if( ContainerGraph.ParentWindow.OutsideGraph.CurrentStandardSurface.AdditionalIncludes.OutsideList.Contains( m_includes[ i ] ) )
  599. // //{
  600. // // ContainerGraph.ParentWindow.OutsideGraph.CurrentStandardSurface.AdditionalIncludes.OutsideList.Remove( m_includes[ i ] );
  601. // //}
  602. // ContainerGraph.ParentWindow.OutsideGraph.CurrentStandardSurface.AdditionalDirectives.RemoveShaderFunctionItem( AdditionalLineType.Include, m_includes[ i ] );
  603. //}
  604. //for( int i = 0; i < m_pragmas.Count; i++ )
  605. //{
  606. // //if( ContainerGraph.ParentWindow.OutsideGraph.CurrentStandardSurface.AdditionalPragmas.OutsideList.Contains( m_pragmas[ i ] ) )
  607. // //{
  608. // // ContainerGraph.ParentWindow.OutsideGraph.CurrentStandardSurface.AdditionalPragmas.OutsideList.Remove( m_pragmas[ i ] );
  609. // //}
  610. // ContainerGraph.ParentWindow.OutsideGraph.CurrentStandardSurface.AdditionalDirectives.RemoveShaderFunctionItem( AdditionalLineType.Pragma, m_pragmas[ i ] );
  611. //}
  612. ContainerGraph.ParentWindow.OutsideGraph.CurrentStandardSurface.AdditionalDirectives.RemoveShaderFunctionItems( OutputId/*, m_directives */);
  613. }
  614. else
  615. {
  616. if( ContainerGraph.ParentWindow.OutsideGraph.MultiPassMasterNodes.Count > 0 )
  617. {
  618. for( int lod = -1; lod < ContainerGraph.ParentWindow.OutsideGraph.LodMultiPassMasternodes.Count; lod++ )
  619. {
  620. RemoveShaderFunctionDirectivesInternal( lod );
  621. }
  622. }
  623. }
  624. // Cannot GameObject.Destroy(m_directives[i]) since we would be removing them from
  625. // the shader function asset itself
  626. m_directives.Clear();
  627. m_directives = null;
  628. if( m_reordenator != null )
  629. {
  630. ParentGraph cachedGraph = ContainerGraph.ParentWindow.CustomGraph;
  631. ContainerGraph.ParentWindow.CustomGraph = null;
  632. UIUtils.UnregisterPropertyNode( m_reordenator );
  633. ContainerGraph.ParentWindow.CustomGraph = cachedGraph;
  634. m_reordenator.Destroy();
  635. m_reordenator = null;
  636. }
  637. UIUtils.UnregisterFunctionNode( this );
  638. ParentGraph cachedGraph2 = ContainerGraph.ParentWindow.CustomGraph;
  639. ContainerGraph.ParentWindow.CustomGraph = m_functionGraph;
  640. if( m_allFunctionInputs != null )
  641. m_allFunctionInputs.Clear();
  642. m_allFunctionInputs = null;
  643. if( m_allFunctionOutputs != null )
  644. m_allFunctionOutputs.Clear();
  645. m_allFunctionOutputs = null;
  646. if( m_functionGraph != null )
  647. m_functionGraph.SoftDestroy();
  648. m_functionGraph = null;
  649. ContainerGraph.ParentWindow.CustomGraph = cachedGraph2;
  650. m_function = null;
  651. m_allFunctionOutputsDict.Clear();
  652. m_allFunctionOutputsDict = null;
  653. m_allFunctionSwitchesDict.Clear();
  654. m_allFunctionSwitchesDict = null;
  655. m_allFunctionInputsDict.Clear();
  656. m_allFunctionInputsDict = null;
  657. UIUtils.CurrentWindow.OutsideGraph.OnLODMasterNodesAddedEvent -= OnLODMasterNodesAddedEvent;
  658. }
  659. public override void OnNodeLogicUpdate( DrawInfo drawInfo )
  660. {
  661. if( m_lateRefresh )
  662. {
  663. m_lateRefresh = false;
  664. RefreshExternalReferences();
  665. }
  666. CheckForChangesRecursively();
  667. base.OnNodeLogicUpdate( drawInfo );
  668. ParentGraph cachedGraph = ContainerGraph.ParentWindow.CustomGraph;
  669. ContainerGraph.ParentWindow.CustomGraph = m_functionGraph;
  670. if( m_functionGraph != null )
  671. {
  672. int nodeCount = m_functionGraph.AllNodes.Count;
  673. for( int i = 0; i < nodeCount; i++ )
  674. {
  675. m_functionGraph.AllNodes[ i ].OnNodeLogicUpdate( drawInfo );
  676. }
  677. //if( !string.IsNullOrEmpty( FunctionGraph.CurrentFunctionOutput.SubTitle ) )
  678. {
  679. SetAdditonalTitleText( FunctionGraph.CurrentFunctionOutput.SubTitle );
  680. }
  681. }
  682. ContainerGraph.ParentWindow.CustomGraph = cachedGraph;
  683. if( m_portsChanged )
  684. {
  685. m_portsChanged = false;
  686. for( int i = 0; i < m_allFunctionOutputs.Count; i++ )
  687. {
  688. m_outputPorts[ i ].ChangeType( m_allFunctionOutputs[ i ].InputPorts[ 0 ].DataType, false );
  689. }
  690. CheckPortVisibility();
  691. }
  692. }
  693. public override void Draw( DrawInfo drawInfo )
  694. {
  695. //CheckForChangesRecursively();
  696. if( !m_initialGraphDraw && drawInfo.CurrentEventType == EventType.Repaint )
  697. {
  698. m_initialGraphDraw = true;
  699. ParentGraph cachedGraph = ContainerGraph.ParentWindow.CustomGraph;
  700. ContainerGraph.ParentWindow.CustomGraph = m_functionGraph;
  701. if( m_functionGraph != null )
  702. {
  703. for( int i = 0; i < m_functionGraph.AllNodes.Count; i++ )
  704. {
  705. ParentNode node = m_functionGraph.AllNodes[ i ];
  706. if( node != null )
  707. {
  708. node.OnNodeLayout( drawInfo );
  709. }
  710. }
  711. }
  712. ContainerGraph.ParentWindow.CustomGraph = cachedGraph;
  713. }
  714. base.Draw( drawInfo );
  715. }
  716. public bool CheckForChanges( bool forceCheck = false, bool forceChange = false )
  717. {
  718. if( ( ContainerGraph.ParentWindow.CheckFunctions || forceCheck || forceChange ) && m_function != null )
  719. {
  720. //string newCheckSum = LastLine( m_function.FunctionInfo );
  721. string newCheckSum = AssetDatabase.GetAssetDependencyHash( AssetDatabase.GetAssetPath( m_function ) ).ToString();
  722. if( !m_functionCheckSum.Equals( newCheckSum ) || forceChange )
  723. {
  724. m_functionCheckSum = newCheckSum;
  725. ContainerGraph.OnDuplicateEvent += DuplicateMe;
  726. return true;
  727. }
  728. }
  729. return false;
  730. }
  731. public bool CheckForChangesRecursively()
  732. {
  733. if( m_functionGraph == null )
  734. return false;
  735. bool result = false;
  736. for( int i = 0; i < m_functionGraph.FunctionNodes.NodesList.Count; i++ )
  737. {
  738. if( m_functionGraph.FunctionNodes.NodesList[ i ].CheckForChangesRecursively() )
  739. result = true;
  740. }
  741. if( CheckForChanges( false, result ) )
  742. result = true;
  743. return result;
  744. }
  745. public void DuplicateMe()
  746. {
  747. bool previewOpen = m_showPreview;
  748. string allOptions = m_allFunctionSwitches.Count.ToString();
  749. for( int i = 0; i < m_allFunctionSwitches.Count; i++ )
  750. {
  751. allOptions += "," + m_allFunctionSwitches[ i ].UniqueId + "," + m_allFunctionSwitches[ i ].GetCurrentSelectedInput();
  752. }
  753. ReadOptionsHelper = allOptions.Split( ',' );
  754. ParentGraph cachedGraph = ContainerGraph.ParentWindow.CustomGraph;
  755. ContainerGraph.ParentWindow.CustomGraph = null;
  756. MasterNode masterNode = ContainerGraph.ParentWindow.CurrentGraph.CurrentMasterNode;
  757. if( masterNode != null )
  758. masterNode.InvalidateMaterialPropertyCount();
  759. ContainerGraph.ParentWindow.CustomGraph = cachedGraph;
  760. ParentNode newNode = ContainerGraph.CreateNode( m_function, false, Vec2Position );
  761. newNode.ShowPreview = previewOpen;
  762. ( newNode as FunctionNode ).ReadOptionsHelper = ReadOptionsHelper;
  763. newNode.RefreshExternalReferences();
  764. if( ( newNode as FunctionNode ).m_reordenator && m_reordenator )
  765. ( newNode as FunctionNode ).m_reordenator.OrderIndex = m_reordenator.OrderIndex;
  766. for( int i = 0; i < m_outputPorts.Count; i++ )
  767. {
  768. if( m_outputPorts[ i ].IsConnected )
  769. {
  770. OutputPort newOutputPort = newNode.GetOutputPortByUniqueId( m_outputPorts[ i ].PortId );
  771. if( newNode.OutputPorts != null && newOutputPort != null )
  772. {
  773. for( int j = m_outputPorts[ i ].ExternalReferences.Count - 1; j >= 0; j-- )
  774. {
  775. ContainerGraph.CreateConnection( m_outputPorts[ i ].ExternalReferences[ j ].NodeId, m_outputPorts[ i ].ExternalReferences[ j ].PortId, newOutputPort.NodeId, newOutputPort.PortId );
  776. }
  777. }
  778. }
  779. //else
  780. //{
  781. //if( newNode.OutputPorts != null && newNode.OutputPorts[ i ] != null )
  782. //{
  783. // ContainerGraph.DeleteConnection( false, newNode.UniqueId, newNode.OutputPorts[ i ].PortId, false, false, false );
  784. //}
  785. //}
  786. }
  787. for( int i = 0; i < m_inputPorts.Count; i++ )
  788. {
  789. if( m_inputPorts[ i ].IsConnected )
  790. {
  791. InputPort newInputPort = newNode.GetInputPortByUniqueId( m_inputPorts[ i ].PortId );
  792. if( newNode.InputPorts != null && newInputPort != null )
  793. {
  794. ContainerGraph.CreateConnection( newInputPort.NodeId, newInputPort.PortId, m_inputPorts[ i ].ExternalReferences[ 0 ].NodeId, m_inputPorts[ i ].ExternalReferences[ 0 ].PortId );
  795. }
  796. }
  797. }
  798. ContainerGraph.OnDuplicateEvent -= DuplicateMe;
  799. if( Selected )
  800. {
  801. ContainerGraph.DeselectNode( this );
  802. ContainerGraph.SelectNode( newNode, true, false );
  803. }
  804. ContainerGraph.DestroyNode( this, false );
  805. }
  806. private FunctionOutput GetFunctionOutputByUniqueId( int uniqueId )
  807. {
  808. int listCount = m_allFunctionOutputs.Count;
  809. if( m_allFunctionOutputsDict.Count != m_allFunctionOutputs.Count )
  810. {
  811. m_allFunctionOutputsDict.Clear();
  812. for( int i = 0; i < listCount; i++ )
  813. {
  814. m_allFunctionOutputsDict.Add( m_allFunctionOutputs[ i ].UniqueId, m_allFunctionOutputs[ i ] );
  815. }
  816. }
  817. if( m_allFunctionOutputsDict.ContainsKey( uniqueId ) )
  818. return m_allFunctionOutputsDict[ uniqueId ];
  819. return null;
  820. }
  821. private FunctionInput GetFunctionInputByUniqueId( int uniqueId )
  822. {
  823. int listCount = m_allFunctionInputs.Count;
  824. if( m_allFunctionInputsDict.Count != m_allFunctionInputs.Count )
  825. {
  826. m_allFunctionInputsDict.Clear();
  827. for( int i = 0; i < listCount; i++ )
  828. {
  829. m_allFunctionInputsDict.Add( m_allFunctionInputs[ i ].UniqueId, m_allFunctionInputs[ i ] );
  830. }
  831. }
  832. if( m_allFunctionInputsDict.ContainsKey( uniqueId ) )
  833. return m_allFunctionInputsDict[ uniqueId ];
  834. return null;
  835. }
  836. public override string GenerateShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar )
  837. {
  838. OutputPort outputPort = GetOutputPortByUniqueId( outputId );
  839. FunctionOutput functionOutput = GetFunctionOutputByUniqueId( outputId );
  840. if( outputPort.IsLocalValue( dataCollector.PortCategory ) )
  841. return outputPort.LocalValue( dataCollector.PortCategory );
  842. m_functionGraph.CurrentPrecision = ContainerGraph.ParentWindow.CurrentGraph.CurrentPrecision;
  843. ParentGraph cachedGraph = ContainerGraph.ParentWindow.CustomGraph;
  844. m_outsideGraph = cachedGraph;
  845. ContainerGraph.ParentWindow.CustomGraph = m_functionGraph;
  846. #if ADD_SHADER_FUNCTION_HEADERS
  847. if( m_reordenator != null && m_reordenator.RecursiveCount() > 0 && m_reordenator.HasTitle )
  848. {
  849. dataCollector.AddToProperties( UniqueId, "[Header(" + m_reordenator.HeaderTitle.Replace( "-", " " ) + ")]", m_reordenator.OrderIndex );
  850. }
  851. #endif
  852. string result = string.Empty;
  853. for( int i = 0; i < m_allFunctionInputs.Count; i++ )
  854. {
  855. if( !m_allFunctionInputs[ i ].InputPorts[ 0 ].IsConnected || m_inputPorts[ i ].IsConnected )
  856. m_allFunctionInputs[ i ].OnPortGeneration += FunctionNodeOnPortGeneration;
  857. }
  858. result += functionOutput.GenerateShaderForOutput( outputId, ref dataCollector, ignoreLocalvar );
  859. for( int i = 0; i < m_allFunctionInputs.Count; i++ )
  860. {
  861. if( !m_allFunctionInputs[ i ].InputPorts[ 0 ].IsConnected || m_inputPorts[ i ].IsConnected )
  862. m_allFunctionInputs[ i ].OnPortGeneration -= FunctionNodeOnPortGeneration;
  863. }
  864. ContainerGraph.ParentWindow.CustomGraph = cachedGraph;
  865. if( outputPort.ConnectionCount > 1 )
  866. RegisterLocalVariable( outputId, result, ref dataCollector );
  867. else
  868. outputPort.SetLocalValue( result, dataCollector.PortCategory );
  869. return outputPort.LocalValue( dataCollector.PortCategory );
  870. }
  871. private string FunctionNodeOnPortGeneration( ref MasterNodeDataCollector dataCollector, int index, ParentGraph graph )
  872. {
  873. ParentGraph cachedGraph = ContainerGraph.ParentWindow.CustomGraph;
  874. ContainerGraph.ParentWindow.CustomGraph = m_outsideGraph;
  875. string result = m_inputPorts[ index ].GeneratePortInstructions( ref dataCollector );
  876. ContainerGraph.ParentWindow.CustomGraph = cachedGraph;
  877. return result;
  878. }
  879. public override void WriteToString( ref string nodeInfo, ref string connectionsInfo )
  880. {
  881. base.WriteToString( ref nodeInfo, ref connectionsInfo );
  882. if( Function != null )
  883. IOUtils.AddFieldValueToString( ref nodeInfo, m_function.name );
  884. else
  885. IOUtils.AddFieldValueToString( ref nodeInfo, m_filename );
  886. IOUtils.AddFieldValueToString( ref nodeInfo, m_reordenator != null ? m_reordenator.RawOrderIndex : -1 );
  887. IOUtils.AddFieldValueToString( ref nodeInfo, m_headerTitle );
  888. IOUtils.AddFieldValueToString( ref nodeInfo, m_functionGraphId );
  889. IOUtils.AddFieldValueToString( ref nodeInfo, AssetDatabase.AssetPathToGUID( AssetDatabase.GetAssetPath( m_function ) ) );
  890. int functionSwitchCount = m_allFunctionSwitches != null ? m_allFunctionSwitches.Count : 0;
  891. string allOptions = functionSwitchCount.ToString();
  892. for( int i = 0; i < functionSwitchCount; i++ )
  893. {
  894. allOptions += "," + m_allFunctionSwitches[ i ].UniqueId + "," + m_allFunctionSwitches[ i ].GetCurrentSelectedInput();
  895. }
  896. IOUtils.AddFieldValueToString( ref nodeInfo, allOptions );
  897. }
  898. public override void ReadFromString( ref string[] nodeParams )
  899. {
  900. base.ReadFromString( ref nodeParams );
  901. m_filename = GetCurrentParam( ref nodeParams );
  902. m_orderIndex = Convert.ToInt32( GetCurrentParam( ref nodeParams ) );
  903. m_headerTitle = GetCurrentParam( ref nodeParams );
  904. if( UIUtils.CurrentShaderVersion() > 7203 )
  905. {
  906. m_functionGraphId = Convert.ToInt32( GetCurrentParam( ref nodeParams ) );
  907. }
  908. if( UIUtils.CurrentShaderVersion() > 13704 )
  909. {
  910. m_functionGUID = GetCurrentParam( ref nodeParams );
  911. }
  912. AmplifyShaderFunction loaded = AssetDatabase.LoadAssetAtPath<AmplifyShaderFunction>( AssetDatabase.GUIDToAssetPath( m_functionGUID ) );
  913. if( loaded != null )
  914. {
  915. CommonInit( loaded, UniqueId );
  916. }
  917. else
  918. {
  919. string[] guids = AssetDatabase.FindAssets( "t:AmplifyShaderFunction " + m_filename );
  920. if( guids.Length > 0 )
  921. {
  922. string sfGuid = null;
  923. foreach( string guid in guids )
  924. {
  925. string assetPath = AssetDatabase.GUIDToAssetPath( guid );
  926. string name = System.IO.Path.GetFileNameWithoutExtension( assetPath );
  927. if( name.Equals( m_filename, StringComparison.OrdinalIgnoreCase ) )
  928. {
  929. sfGuid = guid;
  930. break;
  931. }
  932. }
  933. loaded = AssetDatabase.LoadAssetAtPath<AmplifyShaderFunction>( AssetDatabase.GUIDToAssetPath( sfGuid ) );
  934. if( loaded != null )
  935. {
  936. CommonInit( loaded, UniqueId );
  937. }
  938. else
  939. {
  940. SetTitleText( "ERROR" );
  941. UIUtils.ShowMessage( UniqueId, string.Format( "Error loading {0} shader function from project folder", m_filename ), MessageSeverity.Error );
  942. }
  943. }
  944. else
  945. {
  946. SetTitleText( "Missing Function" );
  947. UIUtils.ShowMessage( UniqueId, string.Format( "Missing {0} shader function on project folder", m_filename ), MessageSeverity.Error );
  948. }
  949. }
  950. if( UIUtils.CurrentShaderVersion() > 14203 )
  951. {
  952. ReadOptionsHelper = GetCurrentParam( ref nodeParams ).Split( ',' );
  953. }
  954. }
  955. public override void ReadOutputDataFromString( ref string[] nodeParams )
  956. {
  957. if( Function == null )
  958. return;
  959. base.ReadOutputDataFromString( ref nodeParams );
  960. ConfigureInputportsAfterRead();
  961. }
  962. public override void OnNodeDoubleClicked( Vector2 currentMousePos2D )
  963. {
  964. if( Function == null )
  965. return;
  966. ContainerGraph.DeSelectAll();
  967. this.Selected = true;
  968. ContainerGraph.ParentWindow.OnLeftMouseUp();
  969. AmplifyShaderEditorWindow.LoadShaderFunctionToASE( Function, true );
  970. this.Selected = false;
  971. }
  972. private void ConfigureInputportsAfterRead()
  973. {
  974. if( InputPorts != null )
  975. {
  976. int inputCount = InputPorts.Count;
  977. for( int i = 0; i < inputCount; i++ )
  978. {
  979. InputPorts[ i ].ChangeProperties( m_allFunctionInputs[ i ].InputName, m_allFunctionInputs[ i ].SelectedInputType, false );
  980. }
  981. }
  982. if( OutputPorts != null )
  983. {
  984. int outputCount = OutputPorts.Count;
  985. for( int i = 0; i < outputCount; i++ )
  986. {
  987. OutputPorts[ i ].ChangeProperties( m_allFunctionOutputs[ i ].OutputName, m_allFunctionOutputs[ i ].AutoOutputType, false );
  988. }
  989. }
  990. }
  991. private void CheckPortVisibility()
  992. {
  993. bool changes = false;
  994. if( InputPorts != null )
  995. {
  996. for( int i = 0; i < m_allFunctionInputs.Count; i++ )
  997. {
  998. if( m_inputPorts[ i ].Visible != m_allFunctionInputs[ i ].IsConnected )
  999. {
  1000. m_inputPorts[ i ].Visible = m_allFunctionInputs[ i ].IsConnected;
  1001. changes = true;
  1002. }
  1003. }
  1004. }
  1005. if( changes )
  1006. m_sizeIsDirty = true;
  1007. }
  1008. public bool HasProperties { get { return m_reordenator != null; } }
  1009. public ParentGraph FunctionGraph
  1010. {
  1011. get { return m_functionGraph; }
  1012. set { m_functionGraph = value; }
  1013. }
  1014. public AmplifyShaderFunction Function
  1015. {
  1016. get { return m_function; }
  1017. set { m_function = value; }
  1018. }
  1019. public override void RecordObjectOnDestroy( string Id )
  1020. {
  1021. base.RecordObjectOnDestroy( Id );
  1022. if( m_reordenator != null )
  1023. m_reordenator.RecordObject( Id );
  1024. if( m_functionGraph != null )
  1025. {
  1026. UndoUtils.RegisterCompleteObjectUndo( m_functionGraph, Id );
  1027. for( int i = 0; i < m_functionGraph.AllNodes.Count; i++ )
  1028. {
  1029. m_functionGraph.AllNodes[ i ].RecordObject( Id );
  1030. }
  1031. }
  1032. }
  1033. public override void SetContainerGraph( ParentGraph newgraph )
  1034. {
  1035. base.SetContainerGraph( newgraph );
  1036. if( m_functionGraph == null )
  1037. return;
  1038. for( int i = 0; i < m_functionGraph.AllNodes.Count; i++ )
  1039. {
  1040. m_functionGraph.AllNodes[ i ].SetContainerGraph( m_functionGraph );
  1041. }
  1042. }
  1043. public override void OnMasterNodeReplaced( MasterNode newMasterNode )
  1044. {
  1045. base.OnMasterNodeReplaced( newMasterNode );
  1046. if( m_functionGraph == null )
  1047. return;
  1048. m_functionGraph.FireMasterNodeReplacedEvent( newMasterNode );
  1049. StandardSurfaceOutputNode surface = newMasterNode as StandardSurfaceOutputNode;
  1050. if( surface != null )
  1051. {
  1052. surface.AdditionalDirectives.AddShaderFunctionItems( OutputId, Function.AdditionalDirectives.DirectivesList );
  1053. }
  1054. else
  1055. {
  1056. if( ContainerGraph.ParentWindow.OutsideGraph.MultiPassMasterNodes.Count > 0 )
  1057. {
  1058. for( int lod = -1; lod < ContainerGraph.ParentWindow.OutsideGraph.LodMultiPassMasternodes.Count; lod++ )
  1059. {
  1060. AddShaderFunctionDirectivesInternal( lod );
  1061. }
  1062. }
  1063. }
  1064. }
  1065. }
  1066. }