TemplateLocalVarsNode.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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( "Template Local Var Data" , "Surface Data" , "Select and use available local variable data from the template" )]
  11. public sealed class TemplateLocalVarsNode : TemplateNodeParent
  12. {
  13. private List<TemplateLocalVarData> m_localVarsData = null;
  14. [SerializeField]
  15. private int m_currentDataIdx = -1;
  16. [SerializeField]
  17. private string m_dataName = string.Empty;
  18. private string[] m_dataLabels = null;
  19. private bool m_fetchDataId = false;
  20. private UpperLeftWidgetHelper m_upperLeftWidgetHelper = new UpperLeftWidgetHelper();
  21. protected override void CommonInit( int uniqueId )
  22. {
  23. base.CommonInit( uniqueId );
  24. m_autoWrapProperties = true;
  25. }
  26. void FetchDataId()
  27. {
  28. if( m_localVarsData != null && m_localVarsData.Count > 0 )
  29. {
  30. m_currentDataIdx = 0;
  31. int count = m_localVarsData.Count;
  32. m_dataLabels = new string[ count ];
  33. for( int i = 0 ; i < count ; i++ )
  34. {
  35. m_dataLabels[ i ] = m_localVarsData[ i ].LocalVarName;
  36. if( m_localVarsData[ i ].LocalVarName.Equals( m_dataName ) )
  37. {
  38. m_currentDataIdx = i;
  39. }
  40. }
  41. UpdateFromId();
  42. }
  43. else
  44. {
  45. m_currentDataIdx = -1;
  46. }
  47. }
  48. void UpdateFromId()
  49. {
  50. if( m_localVarsData != null )
  51. {
  52. if( m_localVarsData.Count == 0 )
  53. {
  54. for( int i = 0 ; i < 4 ; i++ )
  55. m_containerGraph.DeleteConnection( false , UniqueId , i , false , true );
  56. m_headerColor = UIUtils.GetColorFromCategory( "Default" );
  57. SetAdditonalTitleText( "<None>" );
  58. m_additionalContent.text = string.Empty;
  59. m_outputPorts[ 0 ].ChangeProperties( "None" , WirePortDataType.OBJECT , false );
  60. ConfigurePorts();
  61. return;
  62. }
  63. bool areCompatible = TemplateHelperFunctions.CheckIfCompatibles( m_outputPorts[ 0 ].DataType , m_localVarsData[ m_currentDataIdx ].DataType );
  64. string category = m_localVarsData[ m_currentDataIdx ].Category == MasterNodePortCategory.Fragment ? "Surface Data" : "Vertex Data";
  65. m_headerColor = UIUtils.GetColorFromCategory( category );
  66. switch( m_localVarsData[ m_currentDataIdx ].DataType )
  67. {
  68. default:
  69. case WirePortDataType.INT:
  70. case WirePortDataType.FLOAT:
  71. m_outputPorts[ 0 ].ChangeProperties( Constants.EmptyPortValue , m_localVarsData[ m_currentDataIdx ].DataType , false );
  72. break;
  73. case WirePortDataType.FLOAT2:
  74. m_outputPorts[ 0 ].ChangeProperties( "XY" , m_localVarsData[ m_currentDataIdx ].DataType , false );
  75. break;
  76. case WirePortDataType.FLOAT3:
  77. m_outputPorts[ 0 ].ChangeProperties( "XYZ" , m_localVarsData[ m_currentDataIdx ].DataType , false );
  78. break;
  79. case WirePortDataType.FLOAT4:
  80. m_outputPorts[ 0 ].ChangeProperties( "XYZW" , m_localVarsData[ m_currentDataIdx ].DataType , false );
  81. break;
  82. case WirePortDataType.COLOR:
  83. m_outputPorts[ 0 ].ChangeProperties( "RGBA" , m_localVarsData[ m_currentDataIdx ].DataType , false );
  84. break;
  85. }
  86. ConfigurePorts();
  87. if( !areCompatible )
  88. {
  89. m_containerGraph.DeleteConnection( false , UniqueId , 0 , false , true );
  90. }
  91. m_dataName = m_localVarsData[ m_currentDataIdx ].LocalVarName;
  92. SetAdditonalTitleText( m_dataName );
  93. m_sizeIsDirty = true;
  94. CheckWarningState();
  95. }
  96. }
  97. public override void DrawProperties()
  98. {
  99. base.DrawProperties();
  100. if( m_containerGraph.CurrentCanvasMode != NodeAvailability.TemplateShader )
  101. {
  102. return;
  103. }
  104. if( m_multiPassMode )
  105. {
  106. DrawMultipassProperties();
  107. }
  108. if( m_currentDataIdx > -1 )
  109. {
  110. EditorGUI.BeginChangeCheck();
  111. m_currentDataIdx = EditorGUILayoutPopup( DataLabelStr , m_currentDataIdx , m_dataLabels );
  112. if( EditorGUI.EndChangeCheck() )
  113. {
  114. UpdateFromId();
  115. }
  116. }
  117. }
  118. protected override void OnSubShaderChange()
  119. {
  120. FetchLocalVarData();
  121. FetchDataId();
  122. }
  123. protected override void OnPassChange()
  124. {
  125. base.OnPassChange();
  126. FetchLocalVarData();
  127. FetchDataId();
  128. }
  129. void DrawMultipassProperties()
  130. {
  131. DrawSubShaderUI();
  132. DrawPassUI();
  133. }
  134. public override void Draw( DrawInfo drawInfo )
  135. {
  136. base.Draw( drawInfo );
  137. if( m_containerGraph.CurrentCanvasMode != NodeAvailability.TemplateShader )
  138. return;
  139. if( m_localVarsData == null || m_localVarsData.Count == 0 )
  140. {
  141. MasterNode masterNode = m_containerGraph.CurrentMasterNode;
  142. if( masterNode.CurrentMasterNodeCategory == AvailableShaderTypes.Template )
  143. {
  144. FetchLocalVarData( masterNode );
  145. }
  146. }
  147. if( m_fetchDataId )
  148. {
  149. m_fetchDataId = false;
  150. FetchDataId();
  151. }
  152. if( m_currentDataIdx > -1 )
  153. {
  154. EditorGUI.BeginChangeCheck();
  155. m_currentDataIdx = m_upperLeftWidgetHelper.DrawWidget( this , m_currentDataIdx , m_dataLabels );
  156. if( EditorGUI.EndChangeCheck() )
  157. {
  158. UpdateFromId();
  159. }
  160. }
  161. }
  162. public override string GenerateShaderForOutput( int outputId , ref MasterNodeDataCollector dataCollector , bool ignoreLocalvar )
  163. {
  164. if( dataCollector.MasterNodeCategory != AvailableShaderTypes.Template )
  165. {
  166. UIUtils.ShowMessage( "Template Local Vars node is only intended for templates use only" , MessageSeverity.Error );
  167. return m_outputPorts[ 0 ].ErrorValue;
  168. }
  169. if( m_localVarsData[ m_currentDataIdx ].Category != dataCollector.PortCategory )
  170. {
  171. UIUtils.ShowMessage( UniqueId , string.Format( "Local Var {0} can only work on ports of type {1}" , m_localVarsData[ m_currentDataIdx ].LocalVarName , m_localVarsData[ m_currentDataIdx ].Category ) );
  172. return m_outputPorts[ 0 ].ErrorValue;
  173. }
  174. if( m_multiPassMode )
  175. {
  176. if( dataCollector.TemplateDataCollectorInstance.MultipassSubshaderIdx != SubShaderIdx ||
  177. dataCollector.TemplateDataCollectorInstance.MultipassPassIdx != PassIdx
  178. )
  179. {
  180. UIUtils.ShowMessage( UniqueId , string.Format( "{0} is only intended for subshader {1} and pass {2}" , m_dataLabels[ m_currentDataIdx ] , SubShaderIdx , PassIdx ) );
  181. return m_outputPorts[ outputId ].ErrorValue;
  182. }
  183. }
  184. return GetOutputVectorItem( 0 , outputId , m_dataName );
  185. }
  186. public override void ReadFromString( ref string[] nodeParams )
  187. {
  188. base.ReadFromString( ref nodeParams );
  189. m_dataName = GetCurrentParam( ref nodeParams );
  190. m_fetchDataId = true;
  191. }
  192. public override void WriteToString( ref string nodeInfo , ref string connectionsInfo )
  193. {
  194. base.WriteToString( ref nodeInfo , ref connectionsInfo );
  195. IOUtils.AddFieldValueToString( ref nodeInfo , m_dataName );
  196. }
  197. public override void OnMasterNodeReplaced( MasterNode newMasterNode )
  198. {
  199. base.OnMasterNodeReplaced( newMasterNode );
  200. if( newMasterNode.CurrentMasterNodeCategory == AvailableShaderTypes.Template )
  201. {
  202. FetchLocalVarData( newMasterNode );
  203. }
  204. else
  205. {
  206. m_localVarsData = null;
  207. m_currentDataIdx = -1;
  208. }
  209. }
  210. void FetchLocalVarData( MasterNode masterNode = null )
  211. {
  212. FetchMultiPassTemplate( masterNode );
  213. if( m_multiPassMode )
  214. {
  215. if( m_templateMPData != null )
  216. {
  217. m_localVarsData = m_templateMPData.SubShaders[ SubShaderIdx ].Passes[ PassIdx ].LocalVarsList;
  218. m_fetchDataId = true;
  219. }
  220. }
  221. else
  222. {
  223. TemplateData currentTemplate = ( masterNode as TemplateMasterNode ).CurrentTemplate;
  224. if( currentTemplate != null )
  225. {
  226. m_localVarsData = currentTemplate.LocalVarsList;
  227. m_fetchDataId = true;
  228. }
  229. else
  230. {
  231. m_localVarsData = null;
  232. m_currentDataIdx = -1;
  233. }
  234. }
  235. }
  236. public override void Destroy()
  237. {
  238. base.Destroy();
  239. m_dataLabels = null;
  240. m_localVarsData = null;
  241. m_upperLeftWidgetHelper = null;
  242. }
  243. }
  244. }