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.

230 lines
6.3 KiB

  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. [Serializable]
  9. public class TemplateInterpElement
  10. {
  11. public TemplateSemantics Semantic;
  12. public bool[] AvailableChannels = { true, true, true, true };
  13. public bool IsFull = false;
  14. public int Usage = 0;
  15. public string Name;
  16. public TemplateInterpElement( TemplateInterpElement other )
  17. {
  18. Semantic = other.Semantic;
  19. for ( int i = 0; i < AvailableChannels.Length; i++ )
  20. {
  21. AvailableChannels[ i ] = other.AvailableChannels[ i ];
  22. }
  23. IsFull = other.IsFull;
  24. Usage = other.Usage;
  25. Name = other.Name;
  26. }
  27. public TemplateInterpElement( TemplateSemantics semantic )
  28. {
  29. Semantic = semantic;
  30. int semanticId = TemplateHelperFunctions.SemanticToInt[ Semantic ];
  31. Name = ( semanticId == 0 ) ? TemplateHelperFunctions.BaseInterpolatorName : TemplateHelperFunctions.BaseInterpolatorName + semanticId.ToString();
  32. }
  33. public void SetAvailableChannelsFromString( string channels )
  34. {
  35. for ( int i = 0; i < AvailableChannels.Length; i++ )
  36. {
  37. AvailableChannels[ i ] = false;
  38. }
  39. Usage = AvailableChannels.Length;
  40. for ( int i = 0; i < channels.Length; i++ )
  41. {
  42. switch ( channels[ i ] )
  43. {
  44. case 'x': if ( !AvailableChannels[ 0 ] ) { AvailableChannels[ 0 ] = true; Usage--; } break;
  45. case 'y': if ( !AvailableChannels[ 1 ] ) { AvailableChannels[ 1 ] = true; Usage--; } break;
  46. case 'z': if ( !AvailableChannels[ 2 ] ) { AvailableChannels[ 2 ] = true; Usage--; } break;
  47. case 'w': if ( !AvailableChannels[ 3 ] ) { AvailableChannels[ 3 ] = true; Usage--; } break;
  48. }
  49. }
  50. }
  51. public TemplateVertexData RequestChannels( WirePortDataType type, bool isColor, string customName = null )
  52. {
  53. if ( IsFull )
  54. return null;
  55. int channelsRequired = TemplateHelperFunctions.DataTypeChannelUsage[ type ];
  56. if ( channelsRequired == 0 )
  57. return null;
  58. int firstChannel = -1;
  59. for ( int i = 0; i < AvailableChannels.Length; i++ )
  60. {
  61. if ( AvailableChannels[ i ] )
  62. {
  63. if ( firstChannel < 0 )
  64. {
  65. firstChannel = i;
  66. }
  67. channelsRequired -= 1;
  68. if ( channelsRequired == 0 )
  69. break;
  70. }
  71. }
  72. //did not found enough channels to fill request
  73. if ( channelsRequired > 0 )
  74. return null;
  75. if( Usage == 0 && customName != null )
  76. {
  77. Name = customName;
  78. }
  79. Usage += 1;
  80. TemplateVertexData data = null;
  81. if ( type == WirePortDataType.COLOR || type == WirePortDataType.FLOAT4 )
  82. {
  83. // Automatically lock all channels
  84. for ( int i = firstChannel; i < ( firstChannel + channelsRequired ); i++ )
  85. {
  86. AvailableChannels[ i ] = false;
  87. }
  88. IsFull = true;
  89. data = new TemplateVertexData( Semantic, type, Name );
  90. }
  91. else
  92. {
  93. string[] swizzleArray = ( isColor ) ? TemplateHelperFunctions.ColorSwizzle : TemplateHelperFunctions.VectorSwizzle;
  94. string channels = ".";
  95. int count = firstChannel + TemplateHelperFunctions.DataTypeChannelUsage[ type ];
  96. for ( int i = firstChannel; i < count; i++ )
  97. {
  98. AvailableChannels[ i ] = false;
  99. channels += swizzleArray[ i ];
  100. if ( i == ( AvailableChannels.Length - 1 ) )
  101. {
  102. IsFull = true;
  103. }
  104. }
  105. data = new TemplateVertexData( Semantic, type, Name, channels );
  106. }
  107. return data;
  108. }
  109. }
  110. [Serializable]
  111. public class TemplateInterpData
  112. {
  113. [SerializeField]
  114. private string m_interpDataId = string.Empty;
  115. [SerializeField]
  116. private int m_interpDataStartIdx = -1;
  117. [SerializeField]
  118. private bool m_dynamicMax = false;
  119. public List<TemplateInterpElement> AvailableInterpolators = new List<TemplateInterpElement>();
  120. public List<TemplateVertexData> Interpolators = new List<TemplateVertexData>();
  121. public List<TemplateVertexData> RawInterpolators = new List<TemplateVertexData>();
  122. public TemplateInterpData() { }
  123. public TemplateInterpData( TemplateInterpData other )
  124. {
  125. m_dynamicMax = other.DynamicMax;
  126. foreach ( TemplateInterpElement data in other.AvailableInterpolators )
  127. {
  128. AvailableInterpolators.Add( new TemplateInterpElement( data ) );
  129. }
  130. for ( int i = 0; i < other.Interpolators.Count; i++ )
  131. {
  132. Interpolators.Add( new TemplateVertexData( other.Interpolators[ i ] ) );
  133. }
  134. for( int i = 0; i < other.RawInterpolators.Count; i++ )
  135. {
  136. RawInterpolators.Add( new TemplateVertexData( other.RawInterpolators[ i ] ) );
  137. }
  138. }
  139. public void RecalculateAvailableInterpolators( int newMax )
  140. {
  141. if( m_dynamicMax )
  142. {
  143. if( !TemplateHelperFunctions.IntToSemantic.ContainsKey( ( newMax - 1 ) ) )
  144. {
  145. Debug.LogWarning( "Attempting to add inexisting available interpolators" );
  146. return;
  147. }
  148. if( AvailableInterpolators.Count > 0 )
  149. {
  150. int currMax = 1 + TemplateHelperFunctions.SemanticToInt[ AvailableInterpolators[ AvailableInterpolators.Count - 1 ].Semantic ];
  151. if( newMax > currMax )
  152. {
  153. int count = newMax - currMax;
  154. for( int i = 0; i < count; i++ )
  155. {
  156. AvailableInterpolators.Add( new TemplateInterpElement( TemplateHelperFunctions.IntToSemantic[ currMax + i ] ));
  157. }
  158. }
  159. else if( newMax < currMax )
  160. {
  161. int min = TemplateHelperFunctions.SemanticToInt[ AvailableInterpolators[ 0 ].Semantic ];
  162. if( newMax > min )
  163. {
  164. int count = currMax - newMax;
  165. for( int i = 0; i < count; i++ )
  166. {
  167. AvailableInterpolators.RemoveAt( AvailableInterpolators.Count - 1 );
  168. }
  169. }
  170. }
  171. }
  172. }
  173. }
  174. public void ReplaceNameOnInterpolator( TemplateSemantics semantic, string newName )
  175. {
  176. for ( int i = 0; i < AvailableInterpolators.Count; i++ )
  177. {
  178. if ( AvailableInterpolators[ i ].Semantic == semantic )
  179. {
  180. AvailableInterpolators[ i ].Name = newName;
  181. break;
  182. }
  183. }
  184. }
  185. public void Destroy()
  186. {
  187. AvailableInterpolators.Clear();
  188. AvailableInterpolators = null;
  189. Interpolators.Clear();
  190. Interpolators = null;
  191. RawInterpolators.Clear();
  192. RawInterpolators = null;
  193. }
  194. public string InterpDataId { get { return m_interpDataId; } set { m_interpDataId = value; } }
  195. public int InterpDataStartIdx { get { return m_interpDataStartIdx; } set { m_interpDataStartIdx = value; } }
  196. public bool DynamicMax { get { return m_dynamicMax; } set { m_dynamicMax = value; } }
  197. }
  198. }