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.

328 lines
8.7 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. 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" );
  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. if( newMasterNode.CurrentMasterNodeCategory == AvailableShaderTypes.Template )
  194. {
  195. FetchMultiPassTemplate( newMasterNode );
  196. m_inputCountHelper = RefreshInputCountHelper();
  197. if( m_inputPorts.Count != m_inputCountHelper )
  198. {
  199. CreateInputPorts();
  200. }
  201. else
  202. {
  203. RefreshInputPorts();
  204. }
  205. }
  206. else
  207. {
  208. DeleteAllInputConnections( true );
  209. }
  210. }
  211. public override void ReadFromString( ref string[] nodeParams )
  212. {
  213. base.ReadFromString( ref nodeParams );
  214. m_inputCountHelper = Convert.ToInt32( GetCurrentParam( ref nodeParams ) );
  215. // Need to add ports here so read internal data is correct
  216. for( int i = 0; i < m_inputCountHelper; i++ )
  217. {
  218. AddInputPort( WirePortDataType.FLOAT, false, Constants.EmptyPortValue );
  219. }
  220. }
  221. public override void WriteToString( ref string nodeInfo, ref string connectionsInfo )
  222. {
  223. base.WriteToString( ref nodeInfo, ref connectionsInfo );
  224. IOUtils.AddFieldValueToString( ref nodeInfo, m_inputCountHelper );
  225. }
  226. public override void Destroy()
  227. {
  228. base.Destroy();
  229. m_inputHelper.Clear();
  230. m_inputHelper = null;
  231. }
  232. public override void RefreshExternalReferences()
  233. {
  234. base.RefreshExternalReferences();
  235. FetchMultiPassTemplate();
  236. bool create = false;
  237. if( m_inputCountHelper == -1 )
  238. {
  239. create = true;
  240. }
  241. else
  242. {
  243. int newInputCount = RefreshInputCountHelper();
  244. if( newInputCount != m_inputCountHelper )
  245. {
  246. create = true;
  247. }
  248. }
  249. if( m_multiPassMode )
  250. {
  251. if( m_templateMPData != null )
  252. {
  253. if( create )
  254. {
  255. CreateInputPorts();
  256. }
  257. else
  258. {
  259. m_inputHelper.Clear();
  260. int index = 0;
  261. int subShaderCount = m_templateMPData.SubShaders.Count;
  262. for( int subShaderIdx = 0; subShaderIdx < subShaderCount; subShaderIdx++ )
  263. {
  264. int passCount = m_templateMPData.SubShaders[ subShaderIdx ].Passes.Count;
  265. for( int passIdx = 0; passIdx < passCount; passIdx++ )
  266. {
  267. if( m_templateMPData.SubShaders[ subShaderIdx ].Passes[ passIdx ].HasValidFunctionBody )
  268. {
  269. m_inputPorts[ index ].Name = string.Format( InputLabelStr, subShaderIdx, passIdx );
  270. m_inputHelper.Add( new InputSwitchMPHelper( subShaderIdx, passIdx ));
  271. index += 1;
  272. }
  273. }
  274. }
  275. if( index != m_inputCountHelper )
  276. {
  277. Debug.LogWarning( "Something wrong occured in reading MultiPass Switch node" );
  278. }
  279. }
  280. }
  281. }
  282. else
  283. {
  284. if( create )
  285. {
  286. AddInputPort( WirePortDataType.FLOAT, false, "In" );
  287. }
  288. else
  289. {
  290. m_inputPorts[ 0 ].Name = "In";
  291. }
  292. }
  293. }
  294. }
  295. }