Assignment for RMIT Mixed Reality in 2020
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.

234 lines
6.4 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 bool HasRawInterpolatorOfName( string name )
  124. {
  125. return RawInterpolators.Exists( ( x ) => x.VarName.Equals( name ));
  126. }
  127. public TemplateInterpData( TemplateInterpData other )
  128. {
  129. m_dynamicMax = other.DynamicMax;
  130. foreach ( TemplateInterpElement data in other.AvailableInterpolators )
  131. {
  132. AvailableInterpolators.Add( new TemplateInterpElement( data ) );
  133. }
  134. for ( int i = 0; i < other.Interpolators.Count; i++ )
  135. {
  136. Interpolators.Add( new TemplateVertexData( other.Interpolators[ i ] ) );
  137. }
  138. for( int i = 0; i < other.RawInterpolators.Count; i++ )
  139. {
  140. RawInterpolators.Add( new TemplateVertexData( other.RawInterpolators[ i ] ) );
  141. }
  142. }
  143. public void RecalculateAvailableInterpolators( int newMax )
  144. {
  145. if( m_dynamicMax )
  146. {
  147. if( !TemplateHelperFunctions.IntToSemantic.ContainsKey( ( newMax - 1 ) ) )
  148. {
  149. Debug.LogWarning( "Attempting to add inexisting available interpolators" );
  150. return;
  151. }
  152. if( AvailableInterpolators.Count > 0 )
  153. {
  154. int currMax = 1 + TemplateHelperFunctions.SemanticToInt[ AvailableInterpolators[ AvailableInterpolators.Count - 1 ].Semantic ];
  155. if( newMax > currMax )
  156. {
  157. int count = newMax - currMax;
  158. for( int i = 0; i < count; i++ )
  159. {
  160. AvailableInterpolators.Add( new TemplateInterpElement( TemplateHelperFunctions.IntToSemantic[ currMax + i ] ));
  161. }
  162. }
  163. else if( newMax < currMax )
  164. {
  165. int min = TemplateHelperFunctions.SemanticToInt[ AvailableInterpolators[ 0 ].Semantic ];
  166. if( newMax > min )
  167. {
  168. int count = currMax - newMax;
  169. for( int i = 0; i < count; i++ )
  170. {
  171. AvailableInterpolators.RemoveAt( AvailableInterpolators.Count - 1 );
  172. }
  173. }
  174. }
  175. }
  176. }
  177. }
  178. public void ReplaceNameOnInterpolator( TemplateSemantics semantic, string newName )
  179. {
  180. for ( int i = 0; i < AvailableInterpolators.Count; i++ )
  181. {
  182. if ( AvailableInterpolators[ i ].Semantic == semantic )
  183. {
  184. AvailableInterpolators[ i ].Name = newName;
  185. break;
  186. }
  187. }
  188. }
  189. public void Destroy()
  190. {
  191. AvailableInterpolators.Clear();
  192. AvailableInterpolators = null;
  193. Interpolators.Clear();
  194. Interpolators = null;
  195. RawInterpolators.Clear();
  196. RawInterpolators = null;
  197. }
  198. public string InterpDataId { get { return m_interpDataId; } set { m_interpDataId = value; } }
  199. public int InterpDataStartIdx { get { return m_interpDataStartIdx; } set { m_interpDataStartIdx = value; } }
  200. public bool DynamicMax { get { return m_dynamicMax; } set { m_dynamicMax = value; } }
  201. }
  202. }