You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

272 lines
7.5 KiB

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