TemplateMultiPassSwitchNode.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. public class InputSwitchMPHelper
  11. {
  12. public int SubShaderIdx;
  13. public int PassIdx;
  14. public InputSwitchMPHelper( int subShaderIdx , int passIdx )
  15. {
  16. SubShaderIdx = subShaderIdx;
  17. PassIdx = passIdx;
  18. }
  19. }
  20. [Serializable]
  21. [NodeAttributes( "Template Multi-Pass Switch" , "Logical Operators" , "Relays, in compile time, the correct input port according to current analyzed sub-shader/pass." )]
  22. public sealed class TemplateMultiPassSwitchNode : TemplateNodeParent
  23. {
  24. private const string InputLabelStr = "SubShader {0} Pass {1}";
  25. [SerializeField]
  26. private List<InputSwitchMPHelper> m_inputHelper = new List<InputSwitchMPHelper>();
  27. [SerializeField]
  28. private int m_inputCountHelper = -1;
  29. protected override void CommonInit( int uniqueId )
  30. {
  31. m_createAllOutputs = false;
  32. base.CommonInit( uniqueId );
  33. }
  34. public override void OnInputPortConnected( int portId , int otherNodeId , int otherPortId , bool activateNode = true )
  35. {
  36. base.OnInputPortConnected( portId , otherNodeId , otherPortId , activateNode );
  37. UpdateConnections();
  38. }
  39. public override void OnConnectedOutputNodeChanges( int inputPortId , int otherNodeId , int otherPortId , string name , WirePortDataType type )
  40. {
  41. base.OnConnectedOutputNodeChanges( inputPortId , otherNodeId , otherPortId , name , type );
  42. UpdateConnections();
  43. }
  44. public override void OnInputPortDisconnected( int portId )
  45. {
  46. base.OnInputPortDisconnected( portId );
  47. UpdateConnections();
  48. }
  49. private void UpdateConnections()
  50. {
  51. WirePortDataType mainType = WirePortDataType.FLOAT;
  52. int highest = UIUtils.GetPriority( mainType );
  53. for( int i = 0 ; i < m_inputPorts.Count ; i++ )
  54. {
  55. if( m_inputPorts[ i ].IsConnected )
  56. {
  57. WirePortDataType portType = m_inputPorts[ i ].GetOutputConnection().DataType;
  58. if( UIUtils.GetPriority( portType ) > highest )
  59. {
  60. mainType = portType;
  61. highest = UIUtils.GetPriority( portType );
  62. }
  63. }
  64. }
  65. for( int i = 0 ; i < m_inputPorts.Count ; i++ )
  66. {
  67. m_inputPorts[ i ].ChangeType( mainType , false );
  68. }
  69. m_outputPorts[ 0 ].ChangeType( mainType , false );
  70. }
  71. public override void Draw( DrawInfo drawInfo )
  72. {
  73. base.Draw( drawInfo );
  74. if( m_templateMPData == null )
  75. {
  76. FetchMultiPassTemplate();
  77. if( m_inputPorts.Count != m_inputCountHelper )
  78. {
  79. CreateInputPorts();
  80. }
  81. else
  82. {
  83. RefreshInputPorts();
  84. }
  85. }
  86. }
  87. public void RefreshInputPorts()
  88. {
  89. if( m_multiPassMode )
  90. {
  91. m_inputHelper.Clear();
  92. if( m_templateMPData != null )
  93. {
  94. int index = 0;
  95. int subShaderCount = m_templateMPData.SubShaders.Count;
  96. for( int subShaderIdx = 0 ; subShaderIdx < subShaderCount ; subShaderIdx++ )
  97. {
  98. int passCount = m_templateMPData.SubShaders[ subShaderIdx ].Passes.Count;
  99. for( int passIdx = 0 ; passIdx < passCount ; passIdx++ )
  100. {
  101. if( m_templateMPData.SubShaders[ subShaderIdx ].Passes[ passIdx ].HasValidFunctionBody )
  102. {
  103. m_inputPorts[ index ].Name = string.Format( InputLabelStr , subShaderIdx , passIdx );
  104. m_inputHelper.Add( new InputSwitchMPHelper( subShaderIdx , passIdx ) );
  105. index += 1;
  106. }
  107. }
  108. }
  109. }
  110. }
  111. else
  112. {
  113. m_inputPorts[ 0 ].Name = "In";
  114. }
  115. }
  116. public int RefreshInputCountHelper()
  117. {
  118. int inputCountHelper = 0;
  119. if( m_multiPassMode )
  120. {
  121. if( m_templateMPData != null )
  122. {
  123. int subShaderCount = m_templateMPData.SubShaders.Count;
  124. for( int subShaderIdx = 0 ; subShaderIdx < subShaderCount ; subShaderIdx++ )
  125. {
  126. int passCount = m_templateMPData.SubShaders[ subShaderIdx ].Passes.Count;
  127. for( int passIdx = 0 ; passIdx < passCount ; passIdx++ )
  128. {
  129. if( m_templateMPData.SubShaders[ subShaderIdx ].Passes[ passIdx ].HasValidFunctionBody )
  130. inputCountHelper += 1;
  131. }
  132. }
  133. }
  134. }
  135. else
  136. {
  137. inputCountHelper += 1;
  138. }
  139. return inputCountHelper;
  140. }
  141. public void CreateInputPorts()
  142. {
  143. m_inputCountHelper = 0;
  144. DeleteAllInputConnections( true );
  145. if( m_multiPassMode )
  146. {
  147. m_inputHelper.Clear();
  148. if( m_templateMPData != null )
  149. {
  150. int subShaderCount = m_templateMPData.SubShaders.Count;
  151. for( int subShaderIdx = 0 ; subShaderIdx < subShaderCount ; subShaderIdx++ )
  152. {
  153. int passCount = m_templateMPData.SubShaders[ subShaderIdx ].Passes.Count;
  154. for( int passIdx = 0 ; passIdx < passCount ; passIdx++ )
  155. {
  156. if( m_templateMPData.SubShaders[ subShaderIdx ].Passes[ passIdx ].HasValidFunctionBody )
  157. {
  158. AddInputPort( WirePortDataType.FLOAT , false , string.Format( InputLabelStr , subShaderIdx , passIdx ) );
  159. m_inputHelper.Add( new InputSwitchMPHelper( subShaderIdx , passIdx ) );
  160. m_inputCountHelper += 1;
  161. }
  162. }
  163. }
  164. }
  165. }
  166. else
  167. {
  168. AddInputPort( WirePortDataType.FLOAT , false , "In" );
  169. m_inputCountHelper += 1;
  170. }
  171. }
  172. public override string GenerateShaderForOutput( int outputId , ref MasterNodeDataCollector dataCollector , bool ignoreLocalvar )
  173. {
  174. if( dataCollector.MasterNodeCategory != AvailableShaderTypes.Template )
  175. {
  176. UIUtils.ShowMessage( "Template Multi-Pass Switch Data node is only intended for templates use only" , MessageSeverity.Error );
  177. return m_outputPorts[ 0 ].ErrorValue;
  178. }
  179. int currSubShaderIdx = dataCollector.TemplateDataCollectorInstance.MultipassSubshaderIdx;
  180. int currPassIdx = dataCollector.TemplateDataCollectorInstance.MultipassPassIdx;
  181. int inputHelperCount = m_inputHelper.Count;
  182. for( int i = 0 ; i < inputHelperCount ; i++ )
  183. {
  184. if( m_inputHelper[ i ].SubShaderIdx == currSubShaderIdx && m_inputHelper[ i ].PassIdx == currPassIdx )
  185. return m_inputPorts[ i ].GeneratePortInstructions( ref dataCollector );
  186. }
  187. UIUtils.ShowMessage( "Invalid subshader or pass on Template Multi-Pass Switch Data" );
  188. return m_outputPorts[ 0 ].ErrorValue;
  189. }
  190. public override void OnMasterNodeReplaced( MasterNode newMasterNode )
  191. {
  192. base.OnMasterNodeReplaced( newMasterNode );
  193. m_autoWrapProperties = false;
  194. if( newMasterNode.CurrentMasterNodeCategory == AvailableShaderTypes.Template )
  195. {
  196. FetchMultiPassTemplate( newMasterNode );
  197. m_inputCountHelper = RefreshInputCountHelper();
  198. if( m_inputPorts.Count != m_inputCountHelper )
  199. {
  200. CreateInputPorts();
  201. }
  202. else
  203. {
  204. RefreshInputPorts();
  205. }
  206. }
  207. else
  208. {
  209. DeleteAllInputConnections( true );
  210. }
  211. }
  212. public override void ReadFromString( ref string[] nodeParams )
  213. {
  214. base.ReadFromString( ref nodeParams );
  215. m_inputCountHelper = Convert.ToInt32( GetCurrentParam( ref nodeParams ) );
  216. // Need to add ports here so read internal data is correct
  217. for( int i = 0 ; i < m_inputCountHelper ; i++ )
  218. {
  219. AddInputPort( WirePortDataType.FLOAT , false , Constants.EmptyPortValue );
  220. }
  221. }
  222. public override void WriteToString( ref string nodeInfo , ref string connectionsInfo )
  223. {
  224. base.WriteToString( ref nodeInfo , ref connectionsInfo );
  225. IOUtils.AddFieldValueToString( ref nodeInfo , m_inputCountHelper );
  226. }
  227. public override void Destroy()
  228. {
  229. base.Destroy();
  230. m_inputHelper.Clear();
  231. m_inputHelper = null;
  232. }
  233. public override void RefreshExternalReferences()
  234. {
  235. base.RefreshExternalReferences();
  236. FetchMultiPassTemplate();
  237. bool create = false;
  238. if( m_inputCountHelper == -1 )
  239. {
  240. create = true;
  241. }
  242. else
  243. {
  244. int newInputCount = RefreshInputCountHelper();
  245. if( newInputCount != m_inputCountHelper )
  246. {
  247. create = true;
  248. }
  249. }
  250. if( m_multiPassMode )
  251. {
  252. if( m_templateMPData != null )
  253. {
  254. if( create )
  255. {
  256. CreateInputPorts();
  257. }
  258. else
  259. {
  260. m_inputHelper.Clear();
  261. int index = 0;
  262. int subShaderCount = m_templateMPData.SubShaders.Count;
  263. for( int subShaderIdx = 0 ; subShaderIdx < subShaderCount ; subShaderIdx++ )
  264. {
  265. int passCount = m_templateMPData.SubShaders[ subShaderIdx ].Passes.Count;
  266. for( int passIdx = 0 ; passIdx < passCount ; passIdx++ )
  267. {
  268. if( m_templateMPData.SubShaders[ subShaderIdx ].Passes[ passIdx ].HasValidFunctionBody )
  269. {
  270. m_inputPorts[ index ].Name = string.Format( InputLabelStr , subShaderIdx , passIdx );
  271. m_inputHelper.Add( new InputSwitchMPHelper( subShaderIdx , passIdx ) );
  272. index += 1;
  273. }
  274. }
  275. }
  276. if( index != m_inputCountHelper )
  277. {
  278. Debug.LogWarning( "Something wrong occured in reading MultiPass Switch node" );
  279. }
  280. }
  281. }
  282. }
  283. else
  284. {
  285. if( create )
  286. {
  287. AddInputPort( WirePortDataType.FLOAT , false , "In" );
  288. }
  289. else
  290. {
  291. m_inputPorts[ 0 ].Name = "In";
  292. }
  293. }
  294. }
  295. }
  296. }