TemplateVertexDataNode.cs 7.8 KB

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