BreakToComponentsNode.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. // Amplify Shader Editor - Visual Shader Editing Tool
  2. // Copyright (c) Amplify Creations, Lda <info@amplify.pt>
  3. using System;
  4. using UnityEngine;
  5. using UnityEditor;
  6. namespace AmplifyShaderEditor
  7. {
  8. [Serializable]
  9. [NodeAttributes( "Split", "Vector Operators", "Formerly known as Break To Components. Breaks the input data into its individual components", null, KeyCode.B, tags: "split Break To Components" )]
  10. public sealed class BreakToComponentsNode : ParentNode
  11. {
  12. private const string RenameInfo = "This node was formerly known as Break To Components and was renamed to Split to decrease its canvas size.";
  13. private WirePortDataType m_currentType = WirePortDataType.FLOAT;
  14. private readonly string[] ColorPortNames = { "R", "G", "B", "A" };
  15. private readonly string[] VectorPortNames = { "X", "Y", "Z", "W" };
  16. protected override void CommonInit( int uniqueId )
  17. {
  18. base.CommonInit( uniqueId );
  19. AddInputPort( WirePortDataType.FLOAT, false, Constants.EmptyPortValue );
  20. for( int i = 0; i < 16; i++ )
  21. {
  22. AddOutputPort( WirePortDataType.FLOAT, Constants.EmptyPortValue );
  23. m_outputPorts[ i ].IndexPreviewOffset = 1;
  24. if( i != 0 )
  25. {
  26. m_outputPorts[ i ].Visible = false;
  27. }
  28. }
  29. m_previewShaderGUID = "5f58f74a202ba804daddec838b75207d";
  30. }
  31. public override void RenderNodePreview()
  32. {
  33. //Runs at least one time
  34. if( !m_initialized )
  35. {
  36. // nodes with no preview don't update at all
  37. PreviewIsDirty = false;
  38. return;
  39. }
  40. if( !PreviewIsDirty )
  41. return;
  42. SetPreviewInputs();
  43. if( !Preferences.User.DisablePreviews )
  44. {
  45. int count = m_outputPorts.Count;
  46. for( int i = 0 ; i < count ; i++ )
  47. {
  48. RenderTexture temp = RenderTexture.active;
  49. RenderTexture.active = m_outputPorts[ i ].OutputPreviewTexture;
  50. Graphics.Blit( null , m_outputPorts[ i ].OutputPreviewTexture , PreviewMaterial , Mathf.Min( i , 3 ) );
  51. RenderTexture.active = temp;
  52. }
  53. }
  54. PreviewIsDirty = m_continuousPreviewRefresh;
  55. }
  56. public override RenderTexture PreviewTexture
  57. {
  58. get
  59. {
  60. return m_inputPorts[ 0 ].InputPreviewTexture( ContainerGraph );
  61. }
  62. }
  63. public override void DrawProperties()
  64. {
  65. base.DrawProperties();
  66. EditorGUILayout.HelpBox( RenameInfo, MessageType.Warning );
  67. }
  68. void UpdateOutputs( WirePortDataType newType )
  69. {
  70. //this only happens when on initial load
  71. if( newType == WirePortDataType.OBJECT )
  72. return;
  73. m_currentType = newType;
  74. switch( newType )
  75. {
  76. case WirePortDataType.OBJECT:
  77. {
  78. m_outputPorts[ 0 ].ChangeProperties( Constants.EmptyPortValue, WirePortDataType.OBJECT, false );
  79. m_outputPorts[ 0 ].Visible = true;
  80. for( int i = 1; i < m_outputPorts.Count; i++ )
  81. {
  82. m_outputPorts[ i ].Visible = false;
  83. }
  84. }
  85. break;
  86. case WirePortDataType.FLOAT:
  87. {
  88. m_outputPorts[ 0 ].ChangeProperties( Constants.EmptyPortValue, WirePortDataType.FLOAT, false );
  89. m_outputPorts[ 0 ].Visible = true;
  90. for( int i = 1; i < m_outputPorts.Count; i++ )
  91. {
  92. m_outputPorts[ i ].Visible = false;
  93. }
  94. }
  95. break;
  96. case WirePortDataType.FLOAT2:
  97. {
  98. for( int i = 0; i < 2; i++ )
  99. {
  100. m_outputPorts[ i ].ChangeProperties( VectorPortNames[ i ], WirePortDataType.FLOAT, false );
  101. m_outputPorts[ i ].Visible = true;
  102. }
  103. for( int i = 2; i < m_outputPorts.Count; i++ )
  104. {
  105. m_outputPorts[ i ].Visible = false;
  106. }
  107. }
  108. break;
  109. case WirePortDataType.FLOAT3:
  110. {
  111. for( int i = 0; i < 3; i++ )
  112. {
  113. m_outputPorts[ i ].ChangeProperties( VectorPortNames[ i ], WirePortDataType.FLOAT, false );
  114. m_outputPorts[ i ].Visible = true;
  115. }
  116. for( int i = 3; i < m_outputPorts.Count; i++ )
  117. {
  118. m_outputPorts[ i ].Visible = false;
  119. }
  120. }
  121. break;
  122. case WirePortDataType.FLOAT4:
  123. {
  124. for( int i = 0; i < 4; i++ )
  125. {
  126. m_outputPorts[ i ].ChangeProperties( VectorPortNames[ i ], WirePortDataType.FLOAT, false );
  127. m_outputPorts[ i ].Visible = true;
  128. }
  129. for( int i = 4; i < m_outputPorts.Count; i++ )
  130. {
  131. m_outputPorts[ i ].Visible = false;
  132. }
  133. }
  134. break;
  135. case WirePortDataType.FLOAT3x3:
  136. {
  137. for( int i = 0; i < 9; i++ )
  138. {
  139. m_outputPorts[ i ].ChangeProperties( "[" + (int)( i / 3 ) + "][" + i % 3 + "]", WirePortDataType.FLOAT, false );
  140. m_outputPorts[ i ].Visible = true;
  141. }
  142. for( int i = 9; i < m_outputPorts.Count; i++ )
  143. {
  144. m_outputPorts[ i ].Visible = false;
  145. }
  146. }
  147. break;
  148. case WirePortDataType.FLOAT4x4:
  149. {
  150. for( int i = 0; i < 16; i++ )
  151. {
  152. m_outputPorts[ i ].ChangeProperties( "[" + (int)( i / 4 ) + "][" + i % 4 + "]", WirePortDataType.FLOAT, false );
  153. m_outputPorts[ i ].Visible = true;
  154. }
  155. }
  156. break;
  157. case WirePortDataType.COLOR:
  158. {
  159. for( int i = 0; i < 4; i++ )
  160. {
  161. m_outputPorts[ i ].ChangeProperties( ColorPortNames[ i ], WirePortDataType.FLOAT, false );
  162. m_outputPorts[ i ].Visible = true;
  163. }
  164. for( int i = 4; i < m_outputPorts.Count; i++ )
  165. {
  166. m_outputPorts[ i ].Visible = false;
  167. }
  168. }
  169. break;
  170. case WirePortDataType.INT:
  171. {
  172. m_outputPorts[ 0 ].Visible = true;
  173. m_outputPorts[ 0 ].ChangeProperties( Constants.EmptyPortValue, WirePortDataType.INT, false );
  174. for( int i = 1; i < m_outputPorts.Count; i++ )
  175. {
  176. m_outputPorts[ i ].Visible = false;
  177. }
  178. }
  179. break;
  180. }
  181. m_sizeIsDirty = true;
  182. }
  183. public override void OnConnectedOutputNodeChanges( int outputPortId, int otherNodeId, int otherPortId, string name, WirePortDataType type )
  184. {
  185. base.OnConnectedOutputNodeChanges( outputPortId, otherNodeId, otherPortId, name, type );
  186. if( UIUtils.IsLoading || m_isNodeBeingCopied )
  187. return;
  188. m_inputPorts[ 0 ].MatchPortToConnection();
  189. UpdateOutputs( m_inputPorts[ 0 ].DataType );
  190. }
  191. public override void OnInputPortConnected( int portId, int otherNodeId, int otherPortId, bool activateNode = true )
  192. {
  193. base.OnInputPortConnected( portId, otherNodeId, otherPortId, activateNode );
  194. if( UIUtils.IsLoading || m_isNodeBeingCopied )
  195. return;
  196. m_inputPorts[ 0 ].MatchPortToConnection();
  197. UpdateOutputs( m_inputPorts[ 0 ].DataType );
  198. }
  199. public override void WriteToString( ref string nodeInfo, ref string connectionsInfo )
  200. {
  201. base.WriteToString( ref nodeInfo, ref connectionsInfo );
  202. IOUtils.AddFieldValueToString( ref nodeInfo, m_currentType );
  203. }
  204. public override void ReadFromString( ref string[] nodeParams )
  205. {
  206. base.ReadFromString( ref nodeParams );
  207. UpdateOutputs( (WirePortDataType)Enum.Parse( typeof( WirePortDataType ), GetCurrentParam( ref nodeParams ) ) );
  208. }
  209. public override string GenerateShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar )
  210. {
  211. if( m_outputPorts[ 0 ].IsLocalValue( dataCollector.PortCategory ) )
  212. {
  213. return ReturnByType( m_outputPorts[ 0 ].LocalValue( dataCollector.PortCategory ), outputId );
  214. }
  215. string value = string.Empty;
  216. value = m_inputPorts[ 0 ].GeneratePortInstructions( ref dataCollector );
  217. int channelsUsed = 0;
  218. for( int i = 0; i < m_outputPorts.Count; i++ )
  219. {
  220. if( m_outputPorts[ i ].IsConnected )
  221. channelsUsed++;
  222. }
  223. string varName = "break" + OutputId;
  224. if( channelsUsed > 1 )
  225. {
  226. //RegisterLocalVariable( 0, value, ref dataCollector, varName );
  227. dataCollector.AddLocalVariable( UniqueId, CurrentPrecisionType, m_inputPorts[ 0 ].DataType, varName, value );
  228. m_outputPorts[ 0 ].SetLocalValue( varName, dataCollector.PortCategory );
  229. value = varName;
  230. }
  231. return ReturnByType( value, outputId );
  232. }
  233. private string ReturnByType( string value, int outputId )
  234. {
  235. switch( m_inputPorts[ 0 ].DataType )
  236. {
  237. case WirePortDataType.OBJECT:
  238. case WirePortDataType.FLOAT:
  239. case WirePortDataType.INT:
  240. {
  241. return value;
  242. }
  243. case WirePortDataType.FLOAT2:
  244. case WirePortDataType.FLOAT3:
  245. case WirePortDataType.FLOAT4:
  246. {
  247. return GetOutputVectorItem( 0, outputId + 1, value );
  248. }
  249. case WirePortDataType.COLOR:
  250. {
  251. return GetOutputColorItem( 0, outputId + 1, value );
  252. }
  253. case WirePortDataType.FLOAT3x3:
  254. {
  255. return value + "[ " + ( (int)( outputId / 3 ) ) + " ][ " + ( outputId % 3 ) + " ]";
  256. }
  257. case WirePortDataType.FLOAT4x4:
  258. {
  259. return value + "[ " + ( (int)( outputId / 4 ) ) + " ][ " + ( outputId % 4 ) + " ]";
  260. }
  261. }
  262. return value;
  263. }
  264. }
  265. }