TemplateOptionsPort.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. // Amplify Shader Editor - Visual Shader Editing Tool
  2. // Copyright (c) Amplify Creations, Lda <info@amplify.pt>
  3. using System;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. namespace AmplifyShaderEditor
  7. {
  8. // PORT CONTROLLERS
  9. [Serializable]
  10. public class TemplateOptionPortItem
  11. {
  12. [SerializeField]
  13. private int m_portId = -1;
  14. [SerializeField]
  15. private TemplateOptionsItem m_options;
  16. public TemplateOptionPortItem( TemplateMultiPassMasterNode owner, TemplateOptionsItem options )
  17. {
  18. m_options = options;
  19. InputPort port = owner.InputPorts.Find( x => x.Name.Equals( options.Name ) );
  20. if( port != null )
  21. {
  22. m_portId = port.PortId;
  23. }
  24. }
  25. public void FillDataCollector( TemplateMultiPassMasterNode owner, ref MasterNodeDataCollector dataCollector )
  26. {
  27. InputPort port = null;
  28. if( m_portId > -1 )
  29. {
  30. port = owner.GetInputPortByUniqueId( m_portId );
  31. }
  32. else
  33. {
  34. port = owner.InputPorts.Find( x => x.Name.Equals( m_options.Name ) );
  35. }
  36. if( port != null )
  37. {
  38. int optionId = port.HasOwnOrLinkConnection ? 0 : 1;
  39. for( int i = 0; i < m_options.ActionsPerOption[ optionId ].Length; i++ )
  40. {
  41. switch( m_options.ActionsPerOption[ optionId ][ i ].ActionType )
  42. {
  43. case AseOptionsActionType.SetDefine:
  44. {
  45. List<TemplateMultiPassMasterNode> nodes = owner.ContainerGraph.GetMultiPassMasterNodes( owner.LODIndex );
  46. int count = nodes.Count;
  47. for( int nodeIdx = 0; nodeIdx < count; nodeIdx++ )
  48. {
  49. string defineValue = string.Empty;
  50. bool isPragma = false;
  51. if( m_options.ActionsPerOption[ optionId ][ i ].ActionData.StartsWith( "pragma" ) )
  52. {
  53. defineValue = "#" + m_options.ActionsPerOption[ optionId ][ i ].ActionData;
  54. isPragma = true;
  55. }
  56. else
  57. {
  58. defineValue = "#define " + m_options.ActionsPerOption[ optionId ][ i ].ActionData;
  59. }
  60. nodes[ nodeIdx ].OptionsDefineContainer.AddDirective( defineValue ,false, isPragma );
  61. }
  62. //dataCollector.AddToDefines( -1, m_options.ActionsPerOption[ optionId ][ i ].ActionData );
  63. }
  64. break;
  65. case AseOptionsActionType.SetUndefine:
  66. {
  67. List<TemplateMultiPassMasterNode> nodes = owner.ContainerGraph.GetMultiPassMasterNodes( owner.LODIndex );
  68. int count = nodes.Count;
  69. for( int nodeIdx = 0; nodeIdx < count; nodeIdx++ )
  70. {
  71. nodes[ nodeIdx ].OptionsDefineContainer.AddDirective( "#undef " + m_options.ActionsPerOption[ optionId ][ i ].ActionData, false );
  72. }
  73. //dataCollector.AddToDefines( -1, m_options.ActionsPerOption[ optionId ][ i ].ActionData, false );
  74. }
  75. break;
  76. case AseOptionsActionType.SetShaderProperty:
  77. {
  78. TemplateShaderPropertyData data = owner.CurrentTemplate.GetShaderPropertyData( m_options.ActionsPerOption[ optionId ][ i ].ActionData );
  79. if( data != null )
  80. {
  81. string newPropertyValue = data.CreatePropertyForValue( m_options.ActionsPerOption[ optionId ][ i ].ActionBuffer );
  82. owner.CurrentTemplate.IdManager.SetReplacementText( data.FullValue, newPropertyValue );
  83. }
  84. }
  85. break;
  86. }
  87. }
  88. }
  89. }
  90. public void SubShaderFillDataCollector( TemplateMultiPassMasterNode owner, ref MasterNodeDataCollector dataCollector )
  91. {
  92. //TemplateMultiPassMasterNode targetNode = string.IsNullOrEmpty(m_options.Id) ? owner:owner.ContainerGraph.GetMasterNodeOfPass( m_options.Id , owner.LODIndex );
  93. TemplateMultiPassMasterNode targetNode = string.IsNullOrEmpty( m_options.Id ) ?
  94. owner.ContainerGraph.GetMainMasterNodeOfLOD( owner.LODIndex ) :
  95. owner.ContainerGraph.GetMasterNodeOfPass( m_options.Id , owner.LODIndex );
  96. InputPort port = null;
  97. if( m_portId > -1 )
  98. {
  99. port = targetNode.GetInputPortByUniqueId( m_portId );
  100. }
  101. else
  102. {
  103. port = targetNode.InputPorts.Find( x => x.Name.Equals( m_options.Name ) );
  104. }
  105. if( port != null )
  106. {
  107. int optionId = port.HasOwnOrLinkConnection ? 0 : 1;
  108. for( int i = 0; i < m_options.ActionsPerOption[ optionId ].Length; i++ )
  109. {
  110. if( string.IsNullOrEmpty( m_options.ActionsPerOption[ optionId ][ i ].PassName ) ||
  111. m_options.ActionsPerOption[ optionId ][ i ].PassName.Equals( owner.PassName ) )
  112. {
  113. switch( m_options.ActionsPerOption[ optionId ][ i ].ActionType )
  114. {
  115. case AseOptionsActionType.SetDefine:
  116. {
  117. string defineValue = string.Empty;
  118. bool isPragma = false;
  119. if( m_options.ActionsPerOption[ optionId ][ i ].ActionData.StartsWith( "pragma" ) )
  120. {
  121. defineValue = "#" + m_options.ActionsPerOption[ optionId ][ i ].ActionData;
  122. isPragma = true;
  123. }
  124. else
  125. {
  126. defineValue = "#define " + m_options.ActionsPerOption[ optionId ][ i ].ActionData;
  127. }
  128. owner.OptionsDefineContainer.AddDirective( defineValue ,true, isPragma );
  129. }
  130. break;
  131. case AseOptionsActionType.SetUndefine:
  132. {
  133. owner.OptionsDefineContainer.AddDirective( "#undef " + m_options.ActionsPerOption[ optionId ][ i ].ActionData, true );
  134. }
  135. break;
  136. case AseOptionsActionType.SetShaderProperty:
  137. {
  138. TemplateShaderPropertyData data = owner.CurrentTemplate.GetShaderPropertyData( m_options.ActionsPerOption[ optionId ][ i ].ActionData );
  139. if( data != null )
  140. {
  141. string newPropertyValue = data.CreatePropertyForValue( m_options.ActionsPerOption[ optionId ][ i ].ActionBuffer );
  142. owner.CurrentTemplate.IdManager.SetReplacementText( data.FullValue, newPropertyValue );
  143. }
  144. }
  145. break;
  146. }
  147. }
  148. }
  149. }
  150. }
  151. public void CheckImediateActionsForPort( TemplateMultiPassMasterNode owner, int portId )
  152. {
  153. if( portId != m_portId )
  154. return;
  155. InputPort port = null;
  156. if( m_portId > -1 )
  157. {
  158. port = owner.GetInputPortByUniqueId( m_portId );
  159. }
  160. else
  161. {
  162. port = owner.InputPorts.Find( x => x.Name.Equals( m_options.Name ) );
  163. }
  164. if( port != null )
  165. {
  166. int optionId = port.HasOwnOrLinkConnection ? 0 : 1;
  167. for( int i = 0; i < m_options.ActionsPerOption[ optionId ].Length; i++ )
  168. {
  169. switch( m_options.ActionsPerOption[ optionId ][ i ].ActionType )
  170. {
  171. case AseOptionsActionType.SetPortName:
  172. {
  173. port.Name = m_options.ActionsPerOption[ optionId ][ i ].ActionData;
  174. owner.SizeIsDirty = true;
  175. }
  176. break;
  177. }
  178. }
  179. }
  180. }
  181. public TemplateOptionsItem Options { get { return m_options; } }
  182. }
  183. }