FunctionInput.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. // Amplify Shader Editor - Visual Shader Editing Tool
  2. // Copyright (c) Amplify Creations, Lda <info@amplify.pt>
  3. using UnityEngine;
  4. using UnityEditor;
  5. using System;
  6. using System.Collections.Generic;
  7. namespace AmplifyShaderEditor
  8. {
  9. [Serializable]
  10. [NodeAttributes( "Function Input", "Functions", "Function Input adds an input port to the shader function", NodeAvailabilityFlags = (int)NodeAvailability.ShaderFunction )]
  11. public sealed class FunctionInput : ParentNode
  12. {
  13. private const string InputTypeStr = "Input Type";
  14. private readonly string[] m_inputValueTypes ={ "Int",
  15. "Float",
  16. "Vector2",
  17. "Vector3",
  18. "Vector4",
  19. "Color",
  20. "Matrix 3x3",
  21. "Matrix 4x4",
  22. "Sampler 1D",
  23. "Sampler 2D",
  24. "Sampler 3D",
  25. "Sampler Cube",
  26. "Sampler 2D Array",
  27. "Sampler State",
  28. "Custom"};
  29. [SerializeField]
  30. private int m_selectedInputTypeInt = 1;
  31. private WirePortDataType m_selectedInputType = WirePortDataType.FLOAT;
  32. [SerializeField]
  33. private FunctionNode m_functionNode;
  34. [SerializeField]
  35. private string m_inputName = "Input";
  36. [SerializeField]
  37. private bool m_autoCast = false;
  38. [SerializeField]
  39. private int m_orderIndex = -1;
  40. private int m_typeId = -1;
  41. public bool m_ignoreConnection = false;
  42. public delegate string PortGeneration( ref MasterNodeDataCollector dataCollector, int index, ParentGraph graph );
  43. public PortGeneration OnPortGeneration = null;
  44. //Title editing
  45. [SerializeField]
  46. private string m_uniqueName;
  47. private bool m_isEditing;
  48. private bool m_stopEditing;
  49. private bool m_startEditing;
  50. private double m_clickTime;
  51. private double m_doubleClickTime = 0.3;
  52. private Rect m_titleClickArea;
  53. private bool m_showTitleWhenNotEditing = true;
  54. protected override void CommonInit( int uniqueId )
  55. {
  56. base.CommonInit( uniqueId );
  57. AddInputPort( WirePortDataType.FLOAT, false, Constants.EmptyPortValue );
  58. m_inputPorts[ 0 ].AutoDrawInternalData = true;
  59. //m_inputPorts[ 0 ].Visible = false;
  60. AddOutputPort( WirePortDataType.FLOAT, Constants.EmptyPortValue );
  61. m_autoWrapProperties = true;
  62. m_textLabelWidth = 100;
  63. SetTitleText( m_inputName );
  64. UpdatePorts();
  65. SetAdditonalTitleText( "( " + m_inputValueTypes[ m_selectedInputTypeInt ] + " )" );
  66. m_previewShaderGUID = "04bc8e7b317dccb4d8da601680dd8140";
  67. }
  68. public override void SetPreviewInputs()
  69. {
  70. if( Fnode == null )
  71. {
  72. m_ignoreConnection = false;
  73. CheckSpherePreview();
  74. }
  75. else
  76. {
  77. var input = Fnode.GetInput( this );
  78. if( input != null && ( !InputPorts[ 0 ].IsConnected || input.IsConnected ) )
  79. {
  80. m_ignoreConnection = true;
  81. InputPorts[ 0 ].PreparePortCacheID();
  82. Fnode.SetPreviewInput( input );
  83. if( input.ExternalReferences.Count > 0 )
  84. {
  85. SpherePreview = Fnode.ContainerGraph.GetNode( input.ExternalReferences[ 0 ].NodeId ).SpherePreview;
  86. }
  87. else
  88. {
  89. SpherePreview = false;
  90. }
  91. PreviewMaterial.SetTexture( InputPorts[ 0 ].CachedPropertyId, input.InputPreviewTexture( Fnode.ContainerGraph ) );
  92. }
  93. else
  94. {
  95. m_ignoreConnection = false;
  96. CheckSpherePreview();
  97. }
  98. }
  99. if( !m_ignoreConnection )
  100. base.SetPreviewInputs();
  101. for( int i = 0; i < OutputPorts[ 0 ].ExternalReferences.Count; i++ )
  102. {
  103. ContainerGraph.GetNode( OutputPorts[ 0 ].ExternalReferences[ i ].NodeId ).OnNodeChange();
  104. }
  105. if( m_typeId == -1 )
  106. m_typeId = Shader.PropertyToID( "_Type" );
  107. if( m_inputPorts[ 0 ].DataType == WirePortDataType.FLOAT || m_inputPorts[ 0 ].DataType == WirePortDataType.INT )
  108. PreviewMaterial.SetInt( m_typeId, 1 );
  109. else if( m_inputPorts[ 0 ].DataType == WirePortDataType.FLOAT2 )
  110. PreviewMaterial.SetInt( m_typeId, 2 );
  111. else if( m_inputPorts[ 0 ].DataType == WirePortDataType.FLOAT3 )
  112. PreviewMaterial.SetInt( m_typeId, 3 );
  113. else
  114. PreviewMaterial.SetInt( m_typeId, 0 );
  115. }
  116. public override bool RecursivePreviewUpdate( Dictionary<string, bool> duplicatesDict = null )
  117. {
  118. if( duplicatesDict == null )
  119. {
  120. duplicatesDict = ContainerGraph.ParentWindow.VisitedChanged;
  121. }
  122. for( int i = 0; i < InputPorts.Count; i++ )
  123. {
  124. ParentNode outNode = null;
  125. if( Fnode != null )
  126. {
  127. var input = Fnode.GetInput( this );
  128. if( input.ExternalReferences.Count > 0 )
  129. {
  130. outNode = Fnode.ContainerGraph.GetNode( input.ExternalReferences[ 0 ].NodeId );
  131. }
  132. else if( InputPorts[ i ].ExternalReferences.Count > 0 )
  133. {
  134. outNode = ContainerGraph.GetNode( InputPorts[ i ].ExternalReferences[ 0 ].NodeId );
  135. }
  136. }
  137. else
  138. {
  139. if( InputPorts[ i ].ExternalReferences.Count > 0 )
  140. {
  141. outNode = ContainerGraph.GetNode( InputPorts[ i ].ExternalReferences[ 0 ].NodeId );
  142. }
  143. }
  144. if( outNode != null )
  145. {
  146. if( !duplicatesDict.ContainsKey( outNode.OutputId ) )
  147. {
  148. bool result = outNode.RecursivePreviewUpdate();
  149. if( result )
  150. PreviewIsDirty = true;
  151. }
  152. else if( duplicatesDict[ outNode.OutputId ] )
  153. {
  154. PreviewIsDirty = true;
  155. }
  156. }
  157. }
  158. bool needsUpdate = PreviewIsDirty;
  159. RenderNodePreview();
  160. if( !duplicatesDict.ContainsKey( OutputId ) )
  161. duplicatesDict.Add( OutputId, needsUpdate );
  162. return needsUpdate;
  163. }
  164. protected override void OnUniqueIDAssigned()
  165. {
  166. base.OnUniqueIDAssigned();
  167. UIUtils.RegisterFunctionInputNode( this );
  168. if( m_nodeAttribs != null )
  169. m_uniqueName = m_nodeAttribs.Name + UniqueId;
  170. }
  171. public override void Destroy()
  172. {
  173. base.Destroy();
  174. OnPortGeneration = null;
  175. UIUtils.UnregisterFunctionInputNode( this );
  176. }
  177. public override void OnInputPortConnected( int portId, int otherNodeId, int otherPortId, bool activateNode = true )
  178. {
  179. base.OnInputPortConnected( portId, otherNodeId, otherPortId, activateNode );
  180. if( AutoCast )
  181. {
  182. m_inputPorts[ 0 ].MatchPortToConnection();
  183. SetIntTypeFromPort();
  184. UpdatePorts();
  185. SetAdditonalTitleText( "( " + m_inputValueTypes[ m_selectedInputTypeInt ] + " )" );
  186. }
  187. }
  188. public override void OnConnectedOutputNodeChanges( int portId, int otherNodeId, int otherPortId, string name, WirePortDataType type )
  189. {
  190. base.OnConnectedOutputNodeChanges( portId, otherNodeId, otherPortId, name, type );
  191. if( AutoCast )
  192. {
  193. m_inputPorts[ 0 ].MatchPortToConnection();
  194. SetIntTypeFromPort();
  195. UpdatePorts();
  196. SetAdditonalTitleText( "( " + m_inputValueTypes[ m_selectedInputTypeInt ] + " )" );
  197. }
  198. }
  199. public void SetIntTypeFromPort()
  200. {
  201. switch( m_inputPorts[ 0 ].DataType )
  202. {
  203. case WirePortDataType.INT: m_selectedInputTypeInt = 0; break;
  204. default:
  205. case WirePortDataType.FLOAT: m_selectedInputTypeInt = 1; break;
  206. case WirePortDataType.FLOAT2: m_selectedInputTypeInt = 2; break;
  207. case WirePortDataType.FLOAT3: m_selectedInputTypeInt = 3; break;
  208. case WirePortDataType.FLOAT4: m_selectedInputTypeInt = 4; break;
  209. case WirePortDataType.COLOR: m_selectedInputTypeInt = 5; break;
  210. case WirePortDataType.FLOAT3x3: m_selectedInputTypeInt = 6; break;
  211. case WirePortDataType.FLOAT4x4: m_selectedInputTypeInt = 7; break;
  212. case WirePortDataType.SAMPLER1D: m_selectedInputTypeInt = 8; break;
  213. case WirePortDataType.SAMPLER2D: m_selectedInputTypeInt = 9; break;
  214. case WirePortDataType.SAMPLER3D: m_selectedInputTypeInt = 10; break;
  215. case WirePortDataType.SAMPLERCUBE: m_selectedInputTypeInt = 11; break;
  216. case WirePortDataType.SAMPLER2DARRAY: m_selectedInputTypeInt = 12; break;
  217. case WirePortDataType.SAMPLERSTATE: m_selectedInputTypeInt = 13; break;
  218. case WirePortDataType.OBJECT: m_selectedInputTypeInt = 14; break;
  219. }
  220. }
  221. public override void Draw( DrawInfo drawInfo )
  222. {
  223. base.Draw( drawInfo );
  224. // Custom Editable Title
  225. if( ContainerGraph.LodLevel <= ParentGraph.NodeLOD.LOD3 )
  226. {
  227. if( !m_isEditing && ( ( !ContainerGraph.ParentWindow.MouseInteracted && drawInfo.CurrentEventType == EventType.MouseDown && m_titleClickArea.Contains( drawInfo.MousePosition ) ) ) )
  228. {
  229. if( ( EditorApplication.timeSinceStartup - m_clickTime ) < m_doubleClickTime )
  230. m_startEditing = true;
  231. else
  232. GUI.FocusControl( null );
  233. m_clickTime = EditorApplication.timeSinceStartup;
  234. }
  235. else if( m_isEditing && ( ( drawInfo.CurrentEventType == EventType.MouseDown && !m_titleClickArea.Contains( drawInfo.MousePosition ) ) || !EditorGUIUtility.editingTextField ) )
  236. {
  237. m_stopEditing = true;
  238. }
  239. if( m_isEditing || m_startEditing )
  240. {
  241. EditorGUI.BeginChangeCheck();
  242. GUI.SetNextControlName( m_uniqueName );
  243. m_inputName = EditorGUITextField( m_titleClickArea, string.Empty, m_inputName, UIUtils.GetCustomStyle( CustomStyle.NodeTitle ) );
  244. if( EditorGUI.EndChangeCheck() )
  245. {
  246. SetTitleText( m_inputName );
  247. UIUtils.UpdateFunctionInputData( UniqueId, m_inputName );
  248. }
  249. if( m_startEditing )
  250. EditorGUI.FocusTextInControl( m_uniqueName );
  251. }
  252. if( drawInfo.CurrentEventType == EventType.Repaint )
  253. {
  254. if( m_startEditing )
  255. {
  256. m_startEditing = false;
  257. m_isEditing = true;
  258. }
  259. if( m_stopEditing )
  260. {
  261. m_stopEditing = false;
  262. m_isEditing = false;
  263. GUI.FocusControl( null );
  264. }
  265. }
  266. }
  267. }
  268. public override void OnNodeLayout( DrawInfo drawInfo )
  269. {
  270. // RUN LAYOUT CHANGES AFTER TITLES CHANGE
  271. base.OnNodeLayout( drawInfo );
  272. m_titleClickArea = m_titlePos;
  273. m_titleClickArea.height = Constants.NODE_HEADER_HEIGHT;
  274. }
  275. public override void OnNodeRepaint( DrawInfo drawInfo )
  276. {
  277. base.OnNodeRepaint( drawInfo );
  278. if( !m_isVisible )
  279. return;
  280. // Fixed Title ( only renders when not editing )
  281. if( m_showTitleWhenNotEditing && !m_isEditing && !m_startEditing && ContainerGraph.LodLevel <= ParentGraph.NodeLOD.LOD3 )
  282. {
  283. GUI.Label( m_titleClickArea, m_content, UIUtils.GetCustomStyle( CustomStyle.NodeTitle ) );
  284. }
  285. }
  286. public override void OnNodeDoubleClicked( Vector2 currentMousePos2D )
  287. {
  288. if( currentMousePos2D.y - m_globalPosition.y > ( Constants.NODE_HEADER_HEIGHT + Constants.NODE_HEADER_EXTRA_HEIGHT ) * ContainerGraph.ParentWindow.CameraDrawInfo.InvertedZoom )
  289. {
  290. ContainerGraph.ParentWindow.ParametersWindow.IsMaximized = !ContainerGraph.ParentWindow.ParametersWindow.IsMaximized;
  291. }
  292. }
  293. public override void DrawProperties()
  294. {
  295. base.DrawProperties();
  296. EditorGUILayout.BeginVertical();
  297. EditorGUI.BeginChangeCheck();
  298. m_inputName = EditorGUILayoutTextField( "Name", m_inputName );
  299. if( EditorGUI.EndChangeCheck() )
  300. {
  301. SetTitleText( m_inputName );
  302. UIUtils.UpdateFunctionInputData( UniqueId, m_inputName );
  303. }
  304. EditorGUI.BeginChangeCheck();
  305. m_selectedInputTypeInt = EditorGUILayoutPopup( InputTypeStr, m_selectedInputTypeInt, m_inputValueTypes );
  306. if( EditorGUI.EndChangeCheck() )
  307. {
  308. UpdatePorts();
  309. SetAdditonalTitleText( "( " + m_inputValueTypes[ m_selectedInputTypeInt ] + " )" );
  310. }
  311. m_autoCast = EditorGUILayoutToggle( "Auto Cast", m_autoCast );
  312. EditorGUILayout.Separator();
  313. if( !m_inputPorts[ 0 ].IsConnected && m_inputPorts[ 0 ].ValidInternalData )
  314. {
  315. m_inputPorts[ 0 ].ShowInternalData( this, true, "Default Value" );
  316. }
  317. EditorGUILayout.EndVertical();
  318. }
  319. void UpdatePorts()
  320. {
  321. switch( m_selectedInputTypeInt )
  322. {
  323. case 0: m_selectedInputType = WirePortDataType.INT; break;
  324. default:
  325. case 1: m_selectedInputType = WirePortDataType.FLOAT; break;
  326. case 2: m_selectedInputType = WirePortDataType.FLOAT2; break;
  327. case 3: m_selectedInputType = WirePortDataType.FLOAT3; break;
  328. case 4: m_selectedInputType = WirePortDataType.FLOAT4; break;
  329. case 5: m_selectedInputType = WirePortDataType.COLOR; break;
  330. case 6: m_selectedInputType = WirePortDataType.FLOAT3x3; break;
  331. case 7: m_selectedInputType = WirePortDataType.FLOAT4x4; break;
  332. case 8: m_selectedInputType = WirePortDataType.SAMPLER1D; break;
  333. case 9: m_selectedInputType = WirePortDataType.SAMPLER2D; break;
  334. case 10: m_selectedInputType = WirePortDataType.SAMPLER3D; break;
  335. case 11: m_selectedInputType = WirePortDataType.SAMPLERCUBE; break;
  336. case 12: m_selectedInputType = WirePortDataType.SAMPLER2DARRAY; break;
  337. case 13: m_selectedInputType = WirePortDataType.SAMPLERSTATE; break;
  338. case 14: m_selectedInputType = WirePortDataType.OBJECT; break;
  339. }
  340. ChangeInputType( m_selectedInputType, false );
  341. //This node doesn't have any restrictions but changing types should be restricted to prevent invalid connections
  342. m_outputPorts[ 0 ].ChangeTypeWithRestrictions( m_selectedInputType, PortCreateRestriction( m_selectedInputType ) );
  343. m_sizeIsDirty = true;
  344. }
  345. public static int PortCreateRestriction( WirePortDataType dataType )
  346. {
  347. int restrictions = 0;
  348. WirePortDataType[] types = null;
  349. switch( dataType )
  350. {
  351. case WirePortDataType.OBJECT:
  352. break;
  353. case WirePortDataType.FLOAT:
  354. case WirePortDataType.FLOAT2:
  355. case WirePortDataType.FLOAT3:
  356. case WirePortDataType.FLOAT4:
  357. case WirePortDataType.COLOR:
  358. case WirePortDataType.INT:
  359. {
  360. types = new WirePortDataType[] { WirePortDataType.FLOAT, WirePortDataType.FLOAT2, WirePortDataType.FLOAT3, WirePortDataType.FLOAT4, WirePortDataType.COLOR, WirePortDataType.INT, WirePortDataType.OBJECT };
  361. }
  362. break;
  363. case WirePortDataType.FLOAT3x3:
  364. case WirePortDataType.FLOAT4x4:
  365. {
  366. types = new WirePortDataType[] { WirePortDataType.FLOAT3x3, WirePortDataType.FLOAT4x4, WirePortDataType.OBJECT };
  367. }
  368. break;
  369. case WirePortDataType.SAMPLER1D:
  370. case WirePortDataType.SAMPLER2D:
  371. case WirePortDataType.SAMPLER3D:
  372. case WirePortDataType.SAMPLERCUBE:
  373. case WirePortDataType.SAMPLER2DARRAY:
  374. {
  375. types = new WirePortDataType[] { WirePortDataType.SAMPLER1D, WirePortDataType.SAMPLER2D, WirePortDataType.SAMPLER3D, WirePortDataType.SAMPLERCUBE, WirePortDataType.SAMPLER2DARRAY, WirePortDataType.OBJECT };
  376. }
  377. break;
  378. case WirePortDataType.SAMPLERSTATE:
  379. {
  380. types = new WirePortDataType[] { WirePortDataType.SAMPLERSTATE };
  381. }
  382. break;
  383. default:
  384. break;
  385. }
  386. if( types != null )
  387. {
  388. for( int i = 0; i < types.Length; i++ )
  389. {
  390. restrictions = restrictions | (int)types[ i ];
  391. }
  392. }
  393. return restrictions;
  394. }
  395. public override string GenerateShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar )
  396. {
  397. if( m_outputPorts[ outputId ].IsLocalValue( dataCollector.PortCategory ) )
  398. return m_outputPorts[ outputId ].LocalValue( dataCollector.PortCategory );
  399. string result = string.Empty;
  400. if( OnPortGeneration != null )
  401. result = OnPortGeneration( ref dataCollector, m_orderIndex, ContainerGraph.ParentWindow.CustomGraph );
  402. else
  403. result = m_inputPorts[ 0 ].GeneratePortInstructions( ref dataCollector );
  404. if( m_outputPorts[ outputId ].ConnectionCount > 1 )
  405. RegisterLocalVariable( outputId, result, ref dataCollector );
  406. else
  407. m_outputPorts[ outputId ].SetLocalValue( result, dataCollector.PortCategory );
  408. return m_outputPorts[ outputId ].LocalValue( dataCollector.PortCategory );
  409. }
  410. public override void WriteToString( ref string nodeInfo, ref string connectionsInfo )
  411. {
  412. base.WriteToString( ref nodeInfo, ref connectionsInfo );
  413. IOUtils.AddFieldValueToString( ref nodeInfo, m_inputName );
  414. IOUtils.AddFieldValueToString( ref nodeInfo, m_selectedInputTypeInt );
  415. IOUtils.AddFieldValueToString( ref nodeInfo, m_orderIndex );
  416. IOUtils.AddFieldValueToString( ref nodeInfo, m_autoCast );
  417. }
  418. public override void ReadFromString( ref string[] nodeParams )
  419. {
  420. base.ReadFromString( ref nodeParams );
  421. m_inputName = GetCurrentParam( ref nodeParams );
  422. m_selectedInputTypeInt = Convert.ToInt32( GetCurrentParam( ref nodeParams ) );
  423. m_orderIndex = Convert.ToInt32( GetCurrentParam( ref nodeParams ) );
  424. m_autoCast = Convert.ToBoolean( GetCurrentParam( ref nodeParams ) );
  425. SetTitleText( m_inputName );
  426. UpdatePorts();
  427. SetAdditonalTitleText( "( " + m_inputValueTypes[ m_selectedInputTypeInt ] + " )" );
  428. }
  429. public override void RefreshExternalReferences()
  430. {
  431. base.RefreshExternalReferences();
  432. SetTitleText( m_inputName );
  433. UpdatePorts();
  434. SetAdditonalTitleText( "( " + m_inputValueTypes[ m_selectedInputTypeInt ] + " )" );
  435. }
  436. public WirePortDataType SelectedInputType
  437. {
  438. get { return m_selectedInputType; }
  439. }
  440. public string InputName
  441. {
  442. get { return m_inputName; }
  443. }
  444. public int OrderIndex
  445. {
  446. get { return m_orderIndex; }
  447. set { m_orderIndex = value; }
  448. }
  449. public bool AutoCast
  450. {
  451. get { return m_autoCast; }
  452. set { m_autoCast = value; }
  453. }
  454. public FunctionNode Fnode
  455. {
  456. get { return m_functionNode; }
  457. set { m_functionNode = value; }
  458. }
  459. }
  460. }