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.

254 lines
7.4 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. namespace AmplifyShaderEditor
  7. {
  8. [Serializable]
  9. [NodeAttributes( "[Old]Append", "Vector Operators", "Append channels to create a new component",null,KeyCode.V,true,true,"Append",typeof(DynamicAppendNode))]
  10. public sealed class AppendNode : ParentNode
  11. {
  12. private const string OutputTypeStr = "Output type";
  13. [SerializeField]
  14. private WirePortDataType m_selectedOutputType = WirePortDataType.FLOAT4;
  15. [SerializeField]
  16. private int m_selectedOutputTypeInt = 2;
  17. [SerializeField]
  18. private float[] m_defaultValues = { 0, 0, 0, 0 };
  19. private string[] m_defaultValuesStr = { "[0]", "[1]", "[2]", "[3]" };
  20. private readonly string[] m_outputValueTypes ={ "Vector2",
  21. "Vector3",
  22. "Vector4",
  23. "Color"};
  24. protected override void CommonInit( int uniqueId )
  25. {
  26. base.CommonInit( uniqueId );
  27. AddInputPort( WirePortDataType.FLOAT, false, "[0]" );
  28. AddInputPort( WirePortDataType.FLOAT, false, "[1]" );
  29. AddInputPort( WirePortDataType.FLOAT, false, "[2]" );
  30. AddInputPort( WirePortDataType.FLOAT, false, "[3]" );
  31. AddOutputPort( m_selectedOutputType, Constants.EmptyPortValue );
  32. m_textLabelWidth = 90;
  33. m_autoWrapProperties = true;
  34. m_previewShaderGUID = "d80ac81aabf643848a4eaa76f2f88d65";
  35. }
  36. public override void Draw( DrawInfo drawInfo )
  37. {
  38. base.Draw( drawInfo );
  39. if ( m_dropdownEditing )
  40. {
  41. EditorGUI.BeginChangeCheck();
  42. m_selectedOutputTypeInt = EditorGUIPopup( m_dropdownRect, m_selectedOutputTypeInt, m_outputValueTypes, UIUtils.PropertyPopUp );
  43. if ( EditorGUI.EndChangeCheck() )
  44. {
  45. SetupPorts();
  46. m_dropdownEditing = false;
  47. }
  48. }
  49. }
  50. void SetupPorts()
  51. {
  52. switch ( m_selectedOutputTypeInt )
  53. {
  54. case 0: m_selectedOutputType = WirePortDataType.FLOAT2; break;
  55. case 1: m_selectedOutputType = WirePortDataType.FLOAT3; break;
  56. case 2: m_selectedOutputType = WirePortDataType.FLOAT4; break;
  57. case 3: m_selectedOutputType = WirePortDataType.COLOR; break;
  58. }
  59. UpdatePorts();
  60. }
  61. public override void DrawProperties()
  62. {
  63. base.DrawProperties();
  64. EditorGUILayout.BeginVertical();
  65. EditorGUI.BeginChangeCheck();
  66. m_selectedOutputTypeInt = EditorGUILayoutPopup( OutputTypeStr, m_selectedOutputTypeInt, m_outputValueTypes );
  67. if ( EditorGUI.EndChangeCheck() )
  68. {
  69. SetupPorts();
  70. }
  71. int count = 0;
  72. switch ( m_selectedOutputType )
  73. {
  74. case WirePortDataType.FLOAT4:
  75. case WirePortDataType.COLOR:
  76. {
  77. count = 4;
  78. }
  79. break;
  80. case WirePortDataType.FLOAT3:
  81. {
  82. count = 3;
  83. }
  84. break;
  85. case WirePortDataType.FLOAT2:
  86. {
  87. count = 2;
  88. }
  89. break;
  90. case WirePortDataType.OBJECT:
  91. case WirePortDataType.FLOAT:
  92. case WirePortDataType.INT:
  93. case WirePortDataType.FLOAT3x3:
  94. case WirePortDataType.FLOAT4x4:
  95. { }
  96. break;
  97. }
  98. for ( int i = 0; i < count; i++ )
  99. {
  100. if ( !m_inputPorts[ i ].IsConnected )
  101. m_defaultValues[ i ] = EditorGUILayoutFloatField( m_defaultValuesStr[ i ], m_defaultValues[ i ] );
  102. }
  103. EditorGUILayout.EndVertical();
  104. }
  105. void UpdatePorts()
  106. {
  107. m_sizeIsDirty = true;
  108. ChangeOutputType( m_selectedOutputType, false );
  109. switch ( m_selectedOutputType )
  110. {
  111. case WirePortDataType.FLOAT4:
  112. case WirePortDataType.OBJECT:
  113. case WirePortDataType.COLOR:
  114. {
  115. m_inputPorts[ 0 ].Visible = true;
  116. m_inputPorts[ 1 ].Visible = true;
  117. m_inputPorts[ 2 ].Visible = true;
  118. m_inputPorts[ 3 ].Visible = true;
  119. }
  120. break;
  121. case WirePortDataType.FLOAT3:
  122. {
  123. m_inputPorts[ 0 ].Visible = true;
  124. m_inputPorts[ 1 ].Visible = true;
  125. m_inputPorts[ 2 ].Visible = true;
  126. m_inputPorts[ 3 ].Visible = false;
  127. if ( m_inputPorts[ 3 ].IsConnected )
  128. UIUtils.DeleteConnection( true, UniqueId, 3, false, true );
  129. }
  130. break;
  131. case WirePortDataType.FLOAT2:
  132. {
  133. m_inputPorts[ 0 ].Visible = true;
  134. m_inputPorts[ 1 ].Visible = true;
  135. m_inputPorts[ 2 ].Visible = false;
  136. if ( m_inputPorts[ 2 ].IsConnected )
  137. UIUtils.DeleteConnection( true, UniqueId, 2, false, true );
  138. m_inputPorts[ 3 ].Visible = false;
  139. if ( m_inputPorts[ 3 ].IsConnected )
  140. UIUtils.DeleteConnection( true, UniqueId, 3, false, true );
  141. }
  142. break;
  143. case WirePortDataType.FLOAT:
  144. case WirePortDataType.INT:
  145. case WirePortDataType.FLOAT3x3:
  146. case WirePortDataType.FLOAT4x4:
  147. { }
  148. break;
  149. }
  150. }
  151. public override string GenerateShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalVar )
  152. {
  153. if ( m_outputPorts[ 0 ].IsLocalValue( dataCollector.PortCategory ) )
  154. return m_outputPorts[ 0 ].LocalValue( dataCollector.PortCategory );
  155. string value = string.Empty;
  156. switch ( m_selectedOutputType )
  157. {
  158. case WirePortDataType.FLOAT4:
  159. case WirePortDataType.OBJECT:
  160. case WirePortDataType.COLOR:
  161. {
  162. value = "float4( ";
  163. for ( int i = 0; i < 4; i++ )
  164. {
  165. value += m_inputPorts[ i ].IsConnected ? InputPorts[ i ].GenerateShaderForOutput( ref dataCollector, WirePortDataType.FLOAT, ignoreLocalVar, true ) : m_defaultValues[ i ].ToString();
  166. if ( i != 3 )
  167. value += " , ";
  168. }
  169. value += " )";
  170. }
  171. break;
  172. case WirePortDataType.FLOAT3:
  173. {
  174. value = "float3( ";
  175. for ( int i = 0; i < 3; i++ )
  176. {
  177. value += m_inputPorts[ i ].IsConnected ? InputPorts[ i ].GenerateShaderForOutput( ref dataCollector, WirePortDataType.FLOAT, ignoreLocalVar, true ) : m_defaultValues[ i ].ToString();
  178. if ( i != 2 )
  179. value += " , ";
  180. }
  181. value += " )";
  182. }
  183. break;
  184. case WirePortDataType.FLOAT2:
  185. {
  186. value = "float2( ";
  187. for ( int i = 0; i < 2; i++ )
  188. {
  189. value += m_inputPorts[ i ].IsConnected ? InputPorts[ i ].GenerateShaderForOutput( ref dataCollector, WirePortDataType.FLOAT, ignoreLocalVar, true ) : m_defaultValues[ i ].ToString();
  190. if ( i != 1 )
  191. value += " , ";
  192. }
  193. value += " )";
  194. }
  195. break;
  196. case WirePortDataType.FLOAT:
  197. case WirePortDataType.INT:
  198. case WirePortDataType.FLOAT3x3:
  199. case WirePortDataType.FLOAT4x4:
  200. { }
  201. break;
  202. }
  203. RegisterLocalVariable( 0, value, ref dataCollector, "appendResult" + OutputId );
  204. return m_outputPorts[ 0 ].LocalValue( dataCollector.PortCategory );
  205. }
  206. public override void ReadFromString( ref string[] nodeParams )
  207. {
  208. base.ReadFromString( ref nodeParams );
  209. m_selectedOutputType = ( WirePortDataType ) Enum.Parse( typeof( WirePortDataType ), GetCurrentParam( ref nodeParams ) );
  210. switch ( m_selectedOutputType )
  211. {
  212. case WirePortDataType.FLOAT2: m_selectedOutputTypeInt = 0; break;
  213. case WirePortDataType.FLOAT3: m_selectedOutputTypeInt = 1; break;
  214. case WirePortDataType.FLOAT4: m_selectedOutputTypeInt = 2; break;
  215. case WirePortDataType.COLOR: m_selectedOutputTypeInt = 3; break;
  216. }
  217. for ( int i = 0; i < m_defaultValues.Length; i++ )
  218. {
  219. m_defaultValues[ i ] = Convert.ToSingle( GetCurrentParam( ref nodeParams ) );
  220. }
  221. UpdatePorts();
  222. }
  223. public override void WriteToString( ref string nodeInfo, ref string connectionsInfo )
  224. {
  225. base.WriteToString( ref nodeInfo, ref connectionsInfo );
  226. IOUtils.AddFieldValueToString( ref nodeInfo, m_selectedOutputType );
  227. for ( int i = 0; i < m_defaultValues.Length; i++ )
  228. {
  229. IOUtils.AddFieldValueToString( ref nodeInfo, m_defaultValues[ i ] );
  230. }
  231. }
  232. }
  233. }