SwizzleNode.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. // Amplify Shader Editor - Visual Shader Editing Tool
  2. // Copyright (c) Amplify Creations, Lda <info@amplify.pt>
  3. //
  4. // Custom Node Swizzle
  5. // Donated by Tobias Pott - @ Tobias Pott
  6. // www.tobiaspott.de
  7. using System;
  8. using UnityEditor;
  9. using UnityEngine;
  10. namespace AmplifyShaderEditor
  11. {
  12. [Serializable]
  13. [NodeAttributes( "Swizzle", "Vector Operators", "Swizzle components of vector types", null, KeyCode.Z, true, false, null, null, "Tobias Pott - @TobiasPott" )]
  14. public sealed class SwizzleNode : SingleInputOp
  15. {
  16. private const string OutputTypeStr = "Output type";
  17. [SerializeField]
  18. private WirePortDataType m_selectedOutputType = WirePortDataType.FLOAT4;
  19. [SerializeField]
  20. private int m_selectedOutputTypeInt = 3;
  21. [SerializeField]
  22. private int[] m_selectedOutputSwizzleTypes = new int[] { 0, 1, 2, 3 };
  23. [SerializeField]
  24. private int m_maskId;
  25. [SerializeField]
  26. private Vector4 m_maskValue = Vector4.one;
  27. private readonly string[] SwizzleVectorChannels = { "x", "y", "z", "w" };
  28. private readonly string[] SwizzleColorChannels = { "r", "g", "b", "a" };
  29. private readonly string[] SwizzleChannelLabels = { "Channel 0", "Channel 1", "Channel 2", "Channel 3" };
  30. private readonly string[] m_outputValueTypes ={ "Float",
  31. "Vector 2",
  32. "Vector 3",
  33. "Vector 4"};
  34. protected override void CommonInit( int uniqueId )
  35. {
  36. base.CommonInit( uniqueId );
  37. m_inputPorts[ 0 ].CreatePortRestrictions( WirePortDataType.FLOAT,
  38. WirePortDataType.FLOAT2,
  39. WirePortDataType.FLOAT3,
  40. WirePortDataType.FLOAT4,
  41. WirePortDataType.COLOR,
  42. WirePortDataType.INT );
  43. m_inputPorts[ 0 ].DataType = WirePortDataType.FLOAT4;
  44. m_outputPorts[ 0 ].DataType = m_selectedOutputType;
  45. m_textLabelWidth = 90;
  46. m_autoWrapProperties = true;
  47. m_autoUpdateOutputPort = false;
  48. m_hasLeftDropdown = true;
  49. m_previewShaderGUID = "d20531704ce28b14bafb296f291f6608";
  50. m_previewMaterialPassId = 0;
  51. SetAdditonalTitleText( "Value( XYZW )" );
  52. CalculatePreviewData();
  53. }
  54. public override void OnEnable()
  55. {
  56. base.OnEnable();
  57. m_maskId = Shader.PropertyToID( "_Mask" );
  58. }
  59. public override void SetPreviewInputs()
  60. {
  61. base.SetPreviewInputs();
  62. PreviewMaterial.SetVector( m_maskId, m_maskValue );
  63. }
  64. void CalculatePreviewData()
  65. {
  66. int inputMaxChannelId = 0;
  67. switch ( m_inputPorts[ 0 ].DataType )
  68. {
  69. case WirePortDataType.FLOAT4:
  70. case WirePortDataType.COLOR:
  71. inputMaxChannelId = 3;
  72. break;
  73. case WirePortDataType.FLOAT3:
  74. inputMaxChannelId = 2;
  75. break;
  76. case WirePortDataType.FLOAT2:
  77. inputMaxChannelId = 1;
  78. break;
  79. case WirePortDataType.INT:
  80. case WirePortDataType.FLOAT:
  81. inputMaxChannelId = 0;
  82. break;
  83. case WirePortDataType.OBJECT:
  84. case WirePortDataType.FLOAT3x3:
  85. case WirePortDataType.FLOAT4x4:
  86. break;
  87. }
  88. for ( int i = 0; i < 4; i++ )
  89. {
  90. int channel = m_selectedOutputSwizzleTypes[ Mathf.Min( m_selectedOutputTypeInt, i ) ];
  91. m_maskValue[ i ] = Mathf.Min( inputMaxChannelId, channel );
  92. }
  93. }
  94. public override void AfterCommonInit()
  95. {
  96. base.AfterCommonInit();
  97. if ( PaddingTitleLeft == 0 )
  98. {
  99. PaddingTitleLeft = Constants.PropertyPickerWidth + Constants.IconsLeftRightMargin;
  100. if ( PaddingTitleRight == 0 )
  101. PaddingTitleRight = Constants.PropertyPickerWidth + Constants.IconsLeftRightMargin;
  102. }
  103. }
  104. public override void OnConnectedOutputNodeChanges( int outputPortId, int otherNodeId, int otherPortId, string name, WirePortDataType type )
  105. {
  106. base.OnConnectedOutputNodeChanges( outputPortId, otherNodeId, otherPortId, name, type );
  107. UpdatePorts();
  108. }
  109. public override void OnInputPortConnected( int portId, int otherNodeId, int otherPortId, bool activateNode = true )
  110. {
  111. base.OnInputPortConnected( portId, otherNodeId, otherPortId, activateNode );
  112. UpdatePorts();
  113. }
  114. public override void OnInputPortDisconnected( int portId )
  115. {
  116. base.OnInputPortDisconnected( portId );
  117. UpdatePorts();
  118. }
  119. public override void Draw( DrawInfo drawInfo )
  120. {
  121. base.Draw( drawInfo );
  122. if ( m_dropdownEditing )
  123. {
  124. EditorGUI.BeginChangeCheck();
  125. m_selectedOutputTypeInt = EditorGUIPopup( m_dropdownRect, m_selectedOutputTypeInt, m_outputValueTypes, UIUtils.PropertyPopUp );
  126. if ( EditorGUI.EndChangeCheck() )
  127. {
  128. switch ( m_selectedOutputTypeInt )
  129. {
  130. case 0: m_selectedOutputType = WirePortDataType.FLOAT; break;
  131. case 1: m_selectedOutputType = WirePortDataType.FLOAT2; break;
  132. case 2: m_selectedOutputType = WirePortDataType.FLOAT3; break;
  133. case 3: m_selectedOutputType = WirePortDataType.FLOAT4; break;
  134. }
  135. UpdatePorts();
  136. DropdownEditing = false;
  137. }
  138. }
  139. }
  140. public override void DrawProperties()
  141. {
  142. EditorGUILayout.BeginVertical();
  143. EditorGUI.BeginChangeCheck();
  144. m_selectedOutputTypeInt = EditorGUILayoutPopup( OutputTypeStr, m_selectedOutputTypeInt, m_outputValueTypes );
  145. if ( EditorGUI.EndChangeCheck() )
  146. {
  147. switch ( m_selectedOutputTypeInt )
  148. {
  149. case 0: m_selectedOutputType = WirePortDataType.FLOAT; break;
  150. case 1: m_selectedOutputType = WirePortDataType.FLOAT2; break;
  151. case 2: m_selectedOutputType = WirePortDataType.FLOAT3; break;
  152. case 3: m_selectedOutputType = WirePortDataType.FLOAT4; break;
  153. }
  154. UpdatePorts();
  155. }
  156. EditorGUILayout.EndVertical();
  157. // Draw base properties
  158. base.DrawProperties();
  159. EditorGUILayout.BeginVertical();
  160. int count = 0;
  161. switch ( m_selectedOutputType )
  162. {
  163. case WirePortDataType.FLOAT4:
  164. case WirePortDataType.COLOR:
  165. count = 4;
  166. break;
  167. case WirePortDataType.FLOAT3:
  168. count = 3;
  169. break;
  170. case WirePortDataType.FLOAT2:
  171. count = 2;
  172. break;
  173. case WirePortDataType.INT:
  174. case WirePortDataType.FLOAT:
  175. count = 1;
  176. break;
  177. case WirePortDataType.OBJECT:
  178. case WirePortDataType.FLOAT3x3:
  179. case WirePortDataType.FLOAT4x4:
  180. break;
  181. }
  182. EditorGUI.BeginChangeCheck();
  183. if ( m_inputPorts[ 0 ].DataType == WirePortDataType.COLOR )
  184. {
  185. for ( int i = 0; i < count; i++ )
  186. {
  187. m_selectedOutputSwizzleTypes[ i ] = EditorGUILayoutPopup( SwizzleChannelLabels[ i ], m_selectedOutputSwizzleTypes[ i ], SwizzleColorChannels );
  188. }
  189. }
  190. else
  191. {
  192. for ( int i = 0; i < count; i++ )
  193. {
  194. m_selectedOutputSwizzleTypes[ i ] = EditorGUILayoutPopup( SwizzleChannelLabels[ i ], m_selectedOutputSwizzleTypes[ i ], SwizzleVectorChannels );
  195. }
  196. }
  197. if ( EditorGUI.EndChangeCheck() )
  198. {
  199. UpdatePorts();
  200. }
  201. EditorGUILayout.EndVertical();
  202. }
  203. void UpdatePorts()
  204. {
  205. ChangeOutputType( m_selectedOutputType, false );
  206. int count = 0;
  207. switch ( m_selectedOutputType )
  208. {
  209. case WirePortDataType.FLOAT4:
  210. case WirePortDataType.COLOR:
  211. count = 4;
  212. break;
  213. case WirePortDataType.FLOAT3:
  214. count = 3;
  215. break;
  216. case WirePortDataType.FLOAT2:
  217. count = 2;
  218. break;
  219. case WirePortDataType.INT:
  220. case WirePortDataType.FLOAT:
  221. count = 1;
  222. break;
  223. case WirePortDataType.OBJECT:
  224. case WirePortDataType.FLOAT3x3:
  225. case WirePortDataType.FLOAT4x4:
  226. break;
  227. }
  228. int inputMaxChannelId = 0;
  229. switch ( m_inputPorts[ 0 ].DataType )
  230. {
  231. case WirePortDataType.FLOAT4:
  232. case WirePortDataType.COLOR:
  233. inputMaxChannelId = 3;
  234. break;
  235. case WirePortDataType.FLOAT3:
  236. inputMaxChannelId = 2;
  237. break;
  238. case WirePortDataType.FLOAT2:
  239. inputMaxChannelId = 1;
  240. break;
  241. case WirePortDataType.INT:
  242. case WirePortDataType.FLOAT:
  243. inputMaxChannelId = 0;
  244. break;
  245. case WirePortDataType.OBJECT:
  246. case WirePortDataType.FLOAT3x3:
  247. case WirePortDataType.FLOAT4x4:
  248. break;
  249. }
  250. //for ( int i = 0; i < count; i++ )
  251. //{
  252. //m_selectedOutputSwizzleTypes[ i ] = Mathf.Clamp( m_selectedOutputSwizzleTypes[ i ], 0, inputMaxChannelId );
  253. //}
  254. // Update Title
  255. string additionalText = string.Empty;
  256. for ( int i = 0; i < count; i++ )
  257. {
  258. int currentSwizzle = Mathf.Min( inputMaxChannelId, m_selectedOutputSwizzleTypes[ i ] );
  259. additionalText += GetSwizzleComponentForChannel( currentSwizzle ).ToUpper();
  260. }
  261. if ( additionalText.Length > 0 )
  262. SetAdditonalTitleText( "Value( " + additionalText + " )" );
  263. else
  264. SetAdditonalTitleText( string.Empty );
  265. CalculatePreviewData();
  266. m_sizeIsDirty = true;
  267. }
  268. public string GetSwizzleComponentForChannel( int channel )
  269. {
  270. if ( m_inputPorts[ 0 ].DataType == WirePortDataType.COLOR )
  271. {
  272. return SwizzleColorChannels[ channel ];
  273. }
  274. else
  275. {
  276. return SwizzleVectorChannels[ channel ];
  277. }
  278. }
  279. public override string GenerateShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalVar )
  280. {
  281. if ( m_outputPorts[ 0 ].IsLocalValue( dataCollector.PortCategory ) )
  282. return m_outputPorts[ 0 ].LocalValue( dataCollector.PortCategory );
  283. string value = string.Format( "({0}).", m_inputPorts[ 0 ].GeneratePortInstructions( ref dataCollector ) );
  284. int inputMaxChannelId = 0;
  285. switch( m_inputPorts[ 0 ].DataType )
  286. {
  287. case WirePortDataType.FLOAT4:
  288. case WirePortDataType.COLOR:
  289. inputMaxChannelId = 3;
  290. break;
  291. case WirePortDataType.FLOAT3:
  292. inputMaxChannelId = 2;
  293. break;
  294. case WirePortDataType.FLOAT2:
  295. inputMaxChannelId = 1;
  296. break;
  297. case WirePortDataType.INT:
  298. case WirePortDataType.FLOAT:
  299. inputMaxChannelId = 0;
  300. break;
  301. case WirePortDataType.OBJECT:
  302. case WirePortDataType.FLOAT3x3:
  303. case WirePortDataType.FLOAT4x4:
  304. break;
  305. }
  306. int count = 0;
  307. switch ( m_selectedOutputType )
  308. {
  309. case WirePortDataType.FLOAT4:
  310. case WirePortDataType.COLOR:
  311. count = 4;
  312. break;
  313. case WirePortDataType.FLOAT3:
  314. count = 3;
  315. break;
  316. case WirePortDataType.FLOAT2:
  317. count = 2;
  318. break;
  319. case WirePortDataType.INT:
  320. case WirePortDataType.FLOAT:
  321. count = 1;
  322. break;
  323. case WirePortDataType.OBJECT:
  324. case WirePortDataType.FLOAT3x3:
  325. case WirePortDataType.FLOAT4x4:
  326. break;
  327. }
  328. for ( int i = 0; i < count; i++ )
  329. {
  330. int currentSwizzle = Mathf.Min( inputMaxChannelId, m_selectedOutputSwizzleTypes[ i ] );
  331. value += GetSwizzleComponentForChannel( currentSwizzle );
  332. }
  333. return CreateOutputLocalVariable( 0, value, ref dataCollector );
  334. }
  335. public override void ReadFromString( ref string[] nodeParams )
  336. {
  337. base.ReadFromString( ref nodeParams );
  338. m_selectedOutputType = ( WirePortDataType ) Enum.Parse( typeof( WirePortDataType ), GetCurrentParam( ref nodeParams ) );
  339. switch ( m_selectedOutputType )
  340. {
  341. case WirePortDataType.FLOAT: m_selectedOutputTypeInt = 0; break;
  342. case WirePortDataType.FLOAT2: m_selectedOutputTypeInt = 1; break;
  343. case WirePortDataType.FLOAT3: m_selectedOutputTypeInt = 2; break;
  344. case WirePortDataType.COLOR:
  345. case WirePortDataType.FLOAT4: m_selectedOutputTypeInt = 3; break;
  346. }
  347. for ( int i = 0; i < m_selectedOutputSwizzleTypes.Length; i++ )
  348. {
  349. m_selectedOutputSwizzleTypes[ i ] = Convert.ToInt32( GetCurrentParam( ref nodeParams ) );
  350. }
  351. UpdatePorts();
  352. }
  353. public override void WriteToString( ref string nodeInfo, ref string connectionsInfo )
  354. {
  355. base.WriteToString( ref nodeInfo, ref connectionsInfo );
  356. IOUtils.AddFieldValueToString( ref nodeInfo, m_selectedOutputType );
  357. for ( int i = 0; i < m_selectedOutputSwizzleTypes.Length; i++ )
  358. {
  359. IOUtils.AddFieldValueToString( ref nodeInfo, m_selectedOutputSwizzleTypes[ i ] );
  360. }
  361. }
  362. }
  363. }