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.

274 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 Fragment Data", "Surface Data", "Select and use available interpolated fragment data from the template" )]
  11. public class TemplateFragmentDataNode : 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. base.OnSubShaderChange();
  112. FetchInterpolator();
  113. FetchDataId();
  114. }
  115. protected override void OnPassChange()
  116. {
  117. FetchInterpolator();
  118. FetchDataId();
  119. }
  120. void DrawMultipassProperties()
  121. {
  122. DrawSubShaderUI();
  123. DrawPassUI();
  124. }
  125. public override void Draw( DrawInfo drawInfo )
  126. {
  127. base.Draw( drawInfo );
  128. if( m_containerGraph.CurrentCanvasMode != NodeAvailability.TemplateShader )
  129. return;
  130. if( m_interpolatorData == null || m_interpolatorData.Count == 0 )
  131. {
  132. MasterNode masterNode = m_containerGraph.CurrentMasterNode;
  133. FetchInterpolator( masterNode );
  134. }
  135. if( m_fetchDataId )
  136. {
  137. m_fetchDataId = false;
  138. FetchDataId();
  139. }
  140. if( m_currentDataIdx > -1 )
  141. {
  142. EditorGUI.BeginChangeCheck();
  143. m_currentDataIdx = m_upperLeftWidgetHelper.DrawWidget( this, m_currentDataIdx, m_dataLabels );
  144. if( EditorGUI.EndChangeCheck() )
  145. {
  146. UpdateFromId();
  147. }
  148. }
  149. }
  150. public override string GenerateShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar )
  151. {
  152. if( dataCollector.MasterNodeCategory != AvailableShaderTypes.Template )
  153. {
  154. UIUtils.ShowMessage( "Template Fragmment Data node is only intended for templates use only" );
  155. return m_outputPorts[ 0 ].ErrorValue;
  156. }
  157. if( !dataCollector.IsFragmentCategory )
  158. {
  159. UIUtils.ShowMessage( "Template Fragment Data node node is only intended for fragment use use only" );
  160. return m_outputPorts[ 0 ].ErrorValue;
  161. }
  162. if( m_multiPassMode )
  163. {
  164. if( dataCollector.TemplateDataCollectorInstance.MultipassSubshaderIdx != SubShaderIdx ||
  165. dataCollector.TemplateDataCollectorInstance.MultipassPassIdx != PassIdx
  166. )
  167. {
  168. UIUtils.ShowMessage( string.Format( "{0} is only intended for subshader {1} and pass {2}", m_dataLabels[ m_currentDataIdx ], SubShaderIdx, PassIdx ) );
  169. return m_outputPorts[ outputId ].ErrorValue;
  170. }
  171. }
  172. return GetOutputVectorItem( 0, outputId, m_inVarName + m_dataName );
  173. }
  174. public override void ReadFromString( ref string[] nodeParams )
  175. {
  176. base.ReadFromString( ref nodeParams );
  177. m_dataName = GetCurrentParam( ref nodeParams );
  178. m_fetchDataId = true;
  179. }
  180. public override void WriteToString( ref string nodeInfo, ref string connectionsInfo )
  181. {
  182. base.WriteToString( ref nodeInfo, ref connectionsInfo );
  183. IOUtils.AddFieldValueToString( ref nodeInfo, m_dataName );
  184. }
  185. public override void OnMasterNodeReplaced( MasterNode newMasterNode )
  186. {
  187. base.OnMasterNodeReplaced( newMasterNode );
  188. if( newMasterNode.CurrentMasterNodeCategory == AvailableShaderTypes.Template )
  189. {
  190. FetchInterpolator( newMasterNode );
  191. }
  192. else
  193. {
  194. m_interpolatorData = null;
  195. m_currentDataIdx = -1;
  196. }
  197. }
  198. protected override bool ValidatePass( int passIdx )
  199. {
  200. return ( m_templateMPData.SubShaders[ SubShaderIdx ].Passes[ passIdx ].FragmentFunctionData != null &&
  201. m_templateMPData.SubShaders[ SubShaderIdx ].Passes[ passIdx ].InterpolatorDataContainer != null );
  202. }
  203. void FetchInterpolator( MasterNode masterNode = null )
  204. {
  205. FetchMultiPassTemplate( masterNode );
  206. if( m_multiPassMode )
  207. {
  208. if( m_templateMPData != null )
  209. {
  210. m_inVarName = m_templateMPData.SubShaders[ SubShaderIdx ].Passes[ PassIdx ].FragmentFunctionData.InVarName + ".";
  211. m_interpolatorData = m_templateMPData.SubShaders[ SubShaderIdx ].Passes[ PassIdx ].InterpolatorDataContainer.RawInterpolators;
  212. m_fetchDataId = true;
  213. }
  214. }
  215. else
  216. {
  217. if( masterNode == null )
  218. masterNode = m_containerGraph.CurrentMasterNode;
  219. TemplateData currentTemplate = ( masterNode as TemplateMasterNode ).CurrentTemplate;
  220. if( currentTemplate != null )
  221. {
  222. m_inVarName = currentTemplate.FragFunctionData.InVarName + ".";
  223. m_interpolatorData = currentTemplate.InterpolatorData.RawInterpolators;
  224. FetchDataId();
  225. }
  226. else
  227. {
  228. m_interpolatorData = null;
  229. m_currentDataIdx = -1;
  230. }
  231. }
  232. }
  233. public override void Destroy()
  234. {
  235. base.Destroy();
  236. m_dataLabels = null;
  237. m_interpolatorData = null;
  238. m_upperLeftWidgetHelper = null;
  239. }
  240. }
  241. }