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.

258 lines
7.1 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 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. void FetchDataId()
  22. {
  23. if( m_localVarsData != null && m_localVarsData.Count > 0 )
  24. {
  25. m_currentDataIdx = 0;
  26. int count = m_localVarsData.Count;
  27. m_dataLabels = new string[ count ];
  28. for( int i = 0; i < count; i++ )
  29. {
  30. m_dataLabels[ i ] = m_localVarsData[ i ].LocalVarName;
  31. if( m_localVarsData[ i ].LocalVarName.Equals( m_dataName ) )
  32. {
  33. m_currentDataIdx = i;
  34. }
  35. }
  36. UpdateFromId();
  37. }
  38. else
  39. {
  40. m_currentDataIdx = -1;
  41. }
  42. }
  43. void UpdateFromId()
  44. {
  45. if( m_localVarsData != null )
  46. {
  47. if( m_localVarsData.Count == 0 )
  48. {
  49. for( int i = 0; i < 4; i++ )
  50. m_containerGraph.DeleteConnection( false, UniqueId, i, false, true );
  51. m_headerColor = UIUtils.GetColorFromCategory( "Default" );
  52. m_content.text = "None";
  53. m_additionalContent.text = string.Empty;
  54. m_outputPorts[ 0 ].ChangeProperties( "None", WirePortDataType.OBJECT, false );
  55. ConfigurePorts();
  56. return;
  57. }
  58. bool areCompatible = TemplateHelperFunctions.CheckIfCompatibles( m_outputPorts[ 0 ].DataType, m_localVarsData[ m_currentDataIdx ].DataType );
  59. string category = m_localVarsData[ m_currentDataIdx ].Category == MasterNodePortCategory.Fragment ? "Surface Data" : "Vertex Data";
  60. m_headerColor = UIUtils.GetColorFromCategory( category );
  61. switch( m_localVarsData[ m_currentDataIdx ].DataType )
  62. {
  63. default:
  64. case WirePortDataType.INT:
  65. case WirePortDataType.FLOAT:
  66. m_outputPorts[ 0 ].ChangeProperties( Constants.EmptyPortValue, m_localVarsData[ m_currentDataIdx ].DataType, false );
  67. break;
  68. case WirePortDataType.FLOAT2:
  69. m_outputPorts[ 0 ].ChangeProperties( "XY", m_localVarsData[ m_currentDataIdx ].DataType, false );
  70. break;
  71. case WirePortDataType.FLOAT3:
  72. m_outputPorts[ 0 ].ChangeProperties( "XYZ", m_localVarsData[ m_currentDataIdx ].DataType, false );
  73. break;
  74. case WirePortDataType.FLOAT4:
  75. m_outputPorts[ 0 ].ChangeProperties( "XYZW", m_localVarsData[ m_currentDataIdx ].DataType, false );
  76. break;
  77. case WirePortDataType.COLOR:
  78. m_outputPorts[ 0 ].ChangeProperties( "RGBA", m_localVarsData[ 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_localVarsData[ m_currentDataIdx ].LocalVarName;
  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. FetchLocalVarData();
  112. FetchDataId();
  113. }
  114. protected override void OnPassChange()
  115. {
  116. base.OnPassChange();
  117. FetchLocalVarData();
  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_localVarsData == null || m_localVarsData.Count == 0 )
  131. {
  132. MasterNode masterNode = m_containerGraph.CurrentMasterNode;
  133. if( masterNode.CurrentMasterNodeCategory == AvailableShaderTypes.Template )
  134. {
  135. FetchLocalVarData( masterNode );
  136. }
  137. }
  138. if( m_fetchDataId )
  139. {
  140. m_fetchDataId = false;
  141. FetchDataId();
  142. }
  143. if( m_currentDataIdx > -1 )
  144. {
  145. EditorGUI.BeginChangeCheck();
  146. m_currentDataIdx = m_upperLeftWidgetHelper.DrawWidget( this, m_currentDataIdx, m_dataLabels );
  147. if( EditorGUI.EndChangeCheck() )
  148. {
  149. UpdateFromId();
  150. }
  151. }
  152. }
  153. public override string GenerateShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar )
  154. {
  155. if( m_localVarsData[ m_currentDataIdx ].Category != dataCollector.PortCategory )
  156. {
  157. UIUtils.ShowMessage( string.Format( "Local Var {0} can only work on ports of type {1}", m_localVarsData[ m_currentDataIdx ].LocalVarName, m_localVarsData[ m_currentDataIdx ].Category ) );
  158. return m_outputPorts[ 0 ].ErrorValue;
  159. }
  160. if( m_multiPassMode )
  161. {
  162. if( dataCollector.TemplateDataCollectorInstance.MultipassSubshaderIdx != SubShaderIdx ||
  163. dataCollector.TemplateDataCollectorInstance.MultipassPassIdx != PassIdx
  164. )
  165. {
  166. UIUtils.ShowMessage( string.Format( "{0} is only intended for subshader {1} and pass {2}", m_dataLabels[ m_currentDataIdx ], SubShaderIdx, PassIdx ) );
  167. return m_outputPorts[ outputId ].ErrorValue;
  168. }
  169. }
  170. return GetOutputVectorItem( 0, outputId, m_dataName );
  171. }
  172. public override void ReadFromString( ref string[] nodeParams )
  173. {
  174. base.ReadFromString( ref nodeParams );
  175. m_dataName = GetCurrentParam( ref nodeParams );
  176. m_fetchDataId = true;
  177. }
  178. public override void WriteToString( ref string nodeInfo, ref string connectionsInfo )
  179. {
  180. base.WriteToString( ref nodeInfo, ref connectionsInfo );
  181. IOUtils.AddFieldValueToString( ref nodeInfo, m_dataName );
  182. }
  183. public override void OnMasterNodeReplaced( MasterNode newMasterNode )
  184. {
  185. base.OnMasterNodeReplaced( newMasterNode );
  186. if( newMasterNode.CurrentMasterNodeCategory == AvailableShaderTypes.Template )
  187. {
  188. FetchLocalVarData( newMasterNode );
  189. }
  190. else
  191. {
  192. m_localVarsData = null;
  193. m_currentDataIdx = -1;
  194. }
  195. }
  196. void FetchLocalVarData( MasterNode masterNode = null )
  197. {
  198. FetchMultiPassTemplate( masterNode );
  199. if( m_multiPassMode )
  200. {
  201. if( m_templateMPData != null )
  202. {
  203. m_localVarsData = m_templateMPData.SubShaders[ SubShaderIdx ].Passes[ PassIdx ].LocalVarsList;
  204. m_fetchDataId = true;
  205. }
  206. }
  207. else
  208. {
  209. TemplateData currentTemplate = ( masterNode as TemplateMasterNode ).CurrentTemplate;
  210. if( currentTemplate != null )
  211. {
  212. m_localVarsData = currentTemplate.LocalVarsList;
  213. m_fetchDataId = true;
  214. }
  215. else
  216. {
  217. m_localVarsData = null;
  218. m_currentDataIdx = -1;
  219. }
  220. }
  221. }
  222. public override void Destroy()
  223. {
  224. base.Destroy();
  225. m_dataLabels = null;
  226. m_localVarsData = null;
  227. m_upperLeftWidgetHelper = null;
  228. }
  229. }
  230. }