ComponentMaskNode.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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( "Component Mask", "Vector Operators", "Mask certain channels from vectors/color components", null, KeyCode.K )]
  10. public sealed class ComponentMaskNode : ParentNode
  11. {
  12. private const string OutputLocalVarName = "componentMask";
  13. [SerializeField]
  14. private bool[] m_selection = { true, true, true, true };
  15. [SerializeField]
  16. private int m_outputPortCount = 4;
  17. [SerializeField]
  18. private string[] m_labels;
  19. private int m_cachedOrderId = -1;
  20. private int m_cachedSingularId = -1;
  21. protected override void CommonInit( int uniqueId )
  22. {
  23. base.CommonInit( uniqueId );
  24. AddInputPort( WirePortDataType.FLOAT4, false, Constants.EmptyPortValue );
  25. AddOutputPort( WirePortDataType.FLOAT4, Constants.EmptyPortValue );
  26. m_useInternalPortData = true;
  27. m_autoWrapProperties = true;
  28. m_selectedLocation = PreviewLocation.TopCenter;
  29. m_labels = new string[] { "X", "Y", "Z", "W" };
  30. m_previewShaderGUID = "b78e2b295c265cd439c80d218fb3e88e";
  31. SetAdditonalTitleText( "Value( XYZW )" );
  32. }
  33. public override void SetPreviewInputs()
  34. {
  35. base.SetPreviewInputs();
  36. Vector4 order = new Vector4(-1,-1,-1,-1);
  37. int lastIndex = 0;
  38. int singularId = -1;
  39. var datatype = m_inputPorts[ 0 ].DataType;
  40. if( m_selection[ 0 ] )
  41. {
  42. order.Set( lastIndex, order.y, order.z, order.w );
  43. lastIndex++;
  44. singularId = 0;
  45. }
  46. if( m_selection[ 1 ] && datatype >= WirePortDataType.FLOAT2 )
  47. {
  48. order.Set( order.x, lastIndex, order.z, order.w );
  49. lastIndex++;
  50. singularId = 1;
  51. }
  52. if( m_selection[ 2 ] && datatype >= WirePortDataType.FLOAT3 )
  53. {
  54. order.Set( order.x, order.y, lastIndex, order.w );
  55. lastIndex++;
  56. singularId = 2;
  57. }
  58. if( m_selection[ 3 ] && ( datatype == WirePortDataType.FLOAT4 || datatype == WirePortDataType.COLOR ) )
  59. {
  60. order.Set( order.x, order.y, order.z, lastIndex );
  61. lastIndex++;
  62. singularId = 3;
  63. }
  64. if ( lastIndex != 1 )
  65. singularId = -1;
  66. if ( m_cachedOrderId == -1 )
  67. m_cachedOrderId = Shader.PropertyToID( "_Order" );
  68. if ( m_cachedSingularId == -1 )
  69. m_cachedSingularId = Shader.PropertyToID( "_Singular" );
  70. PreviewMaterial.SetVector( m_cachedOrderId, order );
  71. PreviewMaterial.SetFloat( m_cachedSingularId, singularId );
  72. }
  73. public override void OnInputPortConnected( int portId, int otherNodeId, int otherPortId, bool activateNode = true )
  74. {
  75. base.OnInputPortConnected( portId, otherNodeId, otherPortId, activateNode );
  76. UpdatePorts();
  77. UpdateTitle();
  78. }
  79. public override void OnConnectedOutputNodeChanges( int outputPortId, int otherNodeId, int otherPortId, string name, WirePortDataType type )
  80. {
  81. base.OnConnectedOutputNodeChanges( outputPortId, otherNodeId, otherPortId, name, type );
  82. UpdatePorts();
  83. UpdateTitle();
  84. }
  85. public override void OnInputPortDisconnected( int portId )
  86. {
  87. base.OnInputPortDisconnected( portId );
  88. UpdateTitle();
  89. }
  90. void UpdatePorts()
  91. {
  92. m_inputPorts[ 0 ].MatchPortToConnection();
  93. int count = 0;
  94. switch ( m_inputPorts[ 0 ].DataType )
  95. {
  96. case WirePortDataType.FLOAT4:
  97. case WirePortDataType.OBJECT:
  98. case WirePortDataType.COLOR:
  99. {
  100. count = 4;
  101. }
  102. break;
  103. case WirePortDataType.FLOAT3:
  104. {
  105. count = 3;
  106. }
  107. break;
  108. case WirePortDataType.FLOAT2:
  109. {
  110. count = 2;
  111. }
  112. break;
  113. case WirePortDataType.FLOAT:
  114. case WirePortDataType.INT:
  115. case WirePortDataType.FLOAT3x3:
  116. case WirePortDataType.FLOAT4x4:
  117. { }
  118. break;
  119. }
  120. int activeCount = 0;
  121. if ( count > 0 )
  122. {
  123. for ( int i = 0; i < count; i++ )
  124. {
  125. if ( m_selection[ i ] )
  126. activeCount += 1;
  127. }
  128. }
  129. m_outputPortCount = activeCount;
  130. switch ( activeCount )
  131. {
  132. case 0: ChangeOutputType( m_inputPorts[ 0 ].DataType, false ); break;
  133. case 1: ChangeOutputType( WirePortDataType.FLOAT, false ); break;
  134. case 2: ChangeOutputType( WirePortDataType.FLOAT2, false ); break;
  135. case 3: ChangeOutputType( WirePortDataType.FLOAT3, false ); break;
  136. case 4: ChangeOutputType( m_inputPorts[ 0 ].DataType, false ); break;
  137. }
  138. }
  139. private void UpdateTitle()
  140. {
  141. int count = 0;
  142. string additionalText = string.Empty;
  143. switch ( m_inputPorts[ 0 ].DataType )
  144. {
  145. case WirePortDataType.FLOAT4:
  146. case WirePortDataType.OBJECT:
  147. case WirePortDataType.COLOR:
  148. {
  149. count = 4;
  150. }
  151. break;
  152. case WirePortDataType.FLOAT3:
  153. {
  154. count = 3;
  155. }
  156. break;
  157. case WirePortDataType.FLOAT2:
  158. {
  159. count = 2;
  160. }
  161. break;
  162. case WirePortDataType.FLOAT:
  163. case WirePortDataType.INT:
  164. {
  165. count = 0;
  166. }
  167. break;
  168. case WirePortDataType.FLOAT3x3:
  169. case WirePortDataType.FLOAT4x4:
  170. { }
  171. break;
  172. }
  173. if ( count > 0 )
  174. {
  175. for ( int i = 0; i < count; i++ )
  176. {
  177. if ( m_selection[ i ] )
  178. {
  179. additionalText += UIUtils.GetComponentForPosition( i, m_inputPorts[ 0 ].DataType ).ToUpper();
  180. }
  181. }
  182. }
  183. if ( additionalText.Length > 0 )
  184. SetAdditonalTitleText( "Value( " + additionalText + " )" );
  185. else
  186. SetAdditonalTitleText( string.Empty );
  187. }
  188. public override void DrawProperties()
  189. {
  190. base.DrawProperties();
  191. EditorGUILayout.BeginVertical();
  192. int count = 0;
  193. switch ( m_inputPorts[ 0 ].DataType )
  194. {
  195. case WirePortDataType.FLOAT4:
  196. case WirePortDataType.OBJECT:
  197. case WirePortDataType.COLOR:
  198. {
  199. count = 4;
  200. }
  201. break;
  202. case WirePortDataType.FLOAT3:
  203. {
  204. count = 3;
  205. }
  206. break;
  207. case WirePortDataType.FLOAT2:
  208. {
  209. count = 2;
  210. }
  211. break;
  212. case WirePortDataType.FLOAT:
  213. case WirePortDataType.INT:
  214. case WirePortDataType.FLOAT3x3:
  215. case WirePortDataType.FLOAT4x4:
  216. { }
  217. break;
  218. }
  219. int activeCount = 0;
  220. if ( count > 0 )
  221. {
  222. for ( int i = 0; i < count; i++ )
  223. {
  224. m_selection[ i ] = EditorGUILayoutToggleLeft( m_labels[i], m_selection[ i ] );
  225. m_labels[ i ] = UIUtils.GetComponentForPosition( i, m_inputPorts[ 0 ].DataType ).ToUpper();
  226. if ( m_selection[ i ] )
  227. {
  228. activeCount += 1;
  229. }
  230. }
  231. }
  232. if ( activeCount != m_outputPortCount )
  233. {
  234. m_outputPortCount = activeCount;
  235. switch ( activeCount )
  236. {
  237. case 0: ChangeOutputType( m_inputPorts[ 0 ].DataType, false ); break;
  238. case 1: ChangeOutputType( WirePortDataType.FLOAT, false ); break;
  239. case 2: ChangeOutputType( WirePortDataType.FLOAT2, false ); break;
  240. case 3: ChangeOutputType( WirePortDataType.FLOAT3, false ); break;
  241. case 4: ChangeOutputType( m_inputPorts[ 0 ].DataType, false ); break;
  242. }
  243. UpdateTitle();
  244. SetSaveIsDirty();
  245. }
  246. EditorGUILayout.EndVertical();
  247. }
  248. public override string GenerateShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalVar )
  249. {
  250. if( m_outputPorts[ 0 ].IsLocalValue( dataCollector.PortCategory ) )
  251. return m_outputPorts[ 0 ].LocalValue( dataCollector.PortCategory );
  252. string value = m_inputPorts[ 0 ].GeneratePortInstructions( ref dataCollector );
  253. int count = 0;
  254. switch ( m_inputPorts[ 0 ].DataType )
  255. {
  256. case WirePortDataType.FLOAT4:
  257. case WirePortDataType.OBJECT:
  258. case WirePortDataType.COLOR:
  259. {
  260. count = 4;
  261. }
  262. break;
  263. case WirePortDataType.FLOAT3:
  264. {
  265. count = 3;
  266. }
  267. break;
  268. case WirePortDataType.FLOAT2:
  269. {
  270. count = 2;
  271. }
  272. break;
  273. case WirePortDataType.FLOAT:
  274. case WirePortDataType.INT:
  275. {
  276. count = 0;
  277. }
  278. break;
  279. case WirePortDataType.FLOAT3x3:
  280. case WirePortDataType.FLOAT4x4:
  281. { }
  282. break;
  283. }
  284. if ( count > 0 )
  285. {
  286. bool firstElement = true;
  287. value = string.Format("({0})",value);
  288. for ( int i = 0; i < count; i++ )
  289. {
  290. if ( m_selection[ i ] )
  291. {
  292. if( firstElement )
  293. {
  294. firstElement = false;
  295. value += ".";
  296. }
  297. value += UIUtils.GetComponentForPosition( i, m_inputPorts[ 0 ].DataType );
  298. }
  299. }
  300. }
  301. return CreateOutputLocalVariable( 0, value, ref dataCollector );
  302. }
  303. public string GetComponentForPosition( int i )
  304. {
  305. switch ( i )
  306. {
  307. case 0:
  308. {
  309. return ( ( m_outputPorts[ 0 ].DataType == WirePortDataType.COLOR ) ? "r" : "x" );
  310. }
  311. case 1:
  312. {
  313. return ( ( m_outputPorts[ 0 ].DataType == WirePortDataType.COLOR ) ? "g" : "y" );
  314. }
  315. case 2:
  316. {
  317. return ( ( m_outputPorts[ 0 ].DataType == WirePortDataType.COLOR ) ? "b" : "z" );
  318. }
  319. case 3:
  320. {
  321. return ( ( m_outputPorts[ 0 ].DataType == WirePortDataType.COLOR ) ? "a" : "w" );
  322. }
  323. }
  324. return string.Empty;
  325. }
  326. public override void ReadFromString( ref string[] nodeParams )
  327. {
  328. base.ReadFromString( ref nodeParams );
  329. for ( int i = 0; i < 4; i++ )
  330. {
  331. m_selection[ i ] = Convert.ToBoolean( GetCurrentParam( ref nodeParams ) );
  332. }
  333. UpdateTitle();
  334. }
  335. public override void WriteToString( ref string nodeInfo, ref string connectionsInfo )
  336. {
  337. base.WriteToString( ref nodeInfo, ref connectionsInfo );
  338. for ( int i = 0; i < 4; i++ )
  339. {
  340. IOUtils.AddFieldValueToString( ref nodeInfo, m_selection[ i ] );
  341. }
  342. }
  343. }
  344. }