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.

375 lines
8.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 DuplicatePreventionBuffer
  10. {
  11. private const string VectorNameStr = "Vector ";
  12. private const string TextureSampleNameStr = "Texture Sample ";
  13. private const string MatrixNameStr = "Matrix ";
  14. private const string IntNameStr = "Int ";
  15. private const string FloatNameStr = "Float ";
  16. private const string ColorNameStr = "Color ";
  17. [SerializeField]
  18. private int[] m_availableUVChannelsArray = { -1, -1, -1, -1 };
  19. private string[] m_availableUVChannelsNamesArray = { "null",
  20. "null",
  21. "null",
  22. "null" };
  23. private Dictionary<string, int> m_availablePropertyNames = new Dictionary<string, int>();
  24. private Dictionary<string, int> m_availableUniformNames = new Dictionary<string, int>();
  25. private Dictionary<string, int> m_availableLocalVariableNames = new Dictionary<string, int>();
  26. public void ReleaseAllUVChannels()
  27. {
  28. for ( int i = 0; i < m_availableUVChannelsArray.Length; i++ )
  29. {
  30. m_availableUVChannelsArray[ i ] = -1;
  31. }
  32. }
  33. public bool RegisterUVChannel( int nodeId, int channelId, string name )
  34. {
  35. if ( channelId < 0 ||
  36. channelId > ( m_availableUVChannelsArray.Length - 1 ) ||
  37. m_availableUVChannelsArray[ channelId ] >= 0 )
  38. {
  39. return false;
  40. }
  41. m_availableUVChannelsArray[ channelId ] = nodeId;
  42. m_availableUVChannelsNamesArray[ channelId ] = name;
  43. return true;
  44. }
  45. public bool ReleaseUVChannel( int nodeId, int channelId )
  46. {
  47. if ( channelId < 0 ||
  48. channelId > ( m_availableUVChannelsArray.Length - 1 ) )
  49. {
  50. return false;
  51. }
  52. if ( m_availableUVChannelsArray[ channelId ] == nodeId )
  53. {
  54. m_availableUVChannelsArray[ channelId ] = -1;
  55. return true;
  56. }
  57. return false;
  58. }
  59. public int RegisterFirstAvailableChannel( int nodeId , string name)
  60. {
  61. for ( int i = 0; i < m_availableUVChannelsArray.Length; i++ )
  62. {
  63. if ( m_availableUVChannelsArray[ i ] == -1 )
  64. {
  65. m_availableUVChannelsArray[ i ] = nodeId;
  66. m_availableUVChannelsNamesArray[ i ] = name;
  67. return i;
  68. }
  69. }
  70. return -1;
  71. }
  72. public bool IsChannelAvailable( int channelId )
  73. {
  74. if ( channelId < 0 ||
  75. channelId > ( m_availableUVChannelsArray.Length - 1 ) )
  76. {
  77. return false;
  78. }
  79. return ( m_availableUVChannelsArray[ channelId ] < 0 );
  80. }
  81. public int GetFirstOccupiedChannel()
  82. {
  83. for ( int i = 0; i < 4; i++ )
  84. {
  85. if ( m_availableUVChannelsArray[ i ] > -1 )
  86. return i;
  87. }
  88. return -1;
  89. }
  90. public string GetChannelName( int channelId )
  91. {
  92. if ( channelId < 0 ||
  93. channelId > ( m_availableUVChannelsArray.Length - 1 ) )
  94. {
  95. return string.Empty;
  96. }
  97. return m_availableUVChannelsNamesArray[ channelId ] ;
  98. }
  99. public void SetChannelName( int channelId , string name )
  100. {
  101. if ( channelId < 0 ||
  102. channelId > ( m_availableUVChannelsArray.Length - 1 ) )
  103. {
  104. return;
  105. }
  106. m_availableUVChannelsNamesArray[ channelId ] = name;
  107. }
  108. public bool RegisterLocalVariableName( int nodeId, string name )
  109. {
  110. if ( name.Length == 0 )
  111. return false;
  112. if ( m_availableLocalVariableNames.ContainsKey( name ) )
  113. {
  114. if ( m_availableLocalVariableNames[ name ] > -1 )
  115. {
  116. return false;
  117. }
  118. else
  119. {
  120. m_availableLocalVariableNames[ name ] = nodeId;
  121. return true;
  122. }
  123. }
  124. m_availableLocalVariableNames.Add( name, nodeId );
  125. return true;
  126. }
  127. public int CheckUniformNameOwner( string name )
  128. {
  129. if ( name.Length == 0 )
  130. return -1;
  131. if ( m_availableUniformNames.ContainsKey( name ) )
  132. {
  133. return m_availableUniformNames[ name ];
  134. }
  135. return -1;
  136. }
  137. public bool RegisterUniformName( int nodeId, string name )
  138. {
  139. if ( name.Length == 0 )
  140. return false;
  141. if ( m_availableUniformNames.ContainsKey( name ) )
  142. {
  143. if ( m_availableUniformNames[ name ] > -1 )
  144. {
  145. return false;
  146. }
  147. else
  148. {
  149. m_availableUniformNames[ name ] = nodeId;
  150. return true;
  151. }
  152. }
  153. m_availableUniformNames.Add( name, nodeId );
  154. return true;
  155. }
  156. public void DumpUniformNames()
  157. {
  158. string val = "CONTENTS\n";
  159. foreach ( KeyValuePair<string, int> kvp in m_availableUniformNames )
  160. {
  161. val += ( "key " + kvp.Key + " : value " + kvp.Value + "\n" );
  162. }
  163. }
  164. public void DumpLocalVariableNames()
  165. {
  166. string val = "CONTENTS\n";
  167. foreach ( KeyValuePair<string, int> kvp in m_availableLocalVariableNames )
  168. {
  169. val += ( "key " + kvp.Key + " : value " + kvp.Value + "\n" );
  170. }
  171. }
  172. public bool ReleaseUniformName( int nodeId, string name )
  173. {
  174. if ( !string.IsNullOrEmpty(name) && name.Length == 0 )
  175. return false;
  176. if ( m_availableUniformNames.ContainsKey( name ) )
  177. {
  178. if ( m_availableUniformNames[ name ] == nodeId )
  179. {
  180. m_availableUniformNames.Remove( name );
  181. return true;
  182. }
  183. }
  184. return false;
  185. }
  186. public bool ReleaseLocalVariableName( int nodeId, string name )
  187. {
  188. if ( name.Length == 0 )
  189. return false;
  190. if ( m_availableLocalVariableNames.ContainsKey( name ) )
  191. {
  192. if ( m_availableLocalVariableNames[ name ] == nodeId )
  193. {
  194. m_availableLocalVariableNames.Remove( name );
  195. return true;
  196. }
  197. }
  198. return false;
  199. }
  200. public void ReleaseAllUniformNames()
  201. {
  202. m_availableUniformNames.Clear();
  203. }
  204. public void ReleaseAllLocalVariableNames()
  205. {
  206. m_availableLocalVariableNames.Clear();
  207. }
  208. public void GetFirstAvailableName( int nodeId, WirePortDataType type , out string outProperty , out string outInspector, bool useCustomPrefix = false, string customPrefix = null)
  209. {
  210. string name = string.Empty;
  211. if ( useCustomPrefix && customPrefix != null )
  212. {
  213. name = customPrefix;
  214. }
  215. else
  216. {
  217. switch ( type )
  218. {
  219. case WirePortDataType.OBJECT:
  220. case WirePortDataType.FLOAT:
  221. {
  222. name = FloatNameStr;
  223. }
  224. break;
  225. case WirePortDataType.INT:
  226. {
  227. name = IntNameStr;
  228. }
  229. break;
  230. case WirePortDataType.FLOAT2:
  231. case WirePortDataType.FLOAT3:
  232. case WirePortDataType.FLOAT4:
  233. {
  234. name = VectorNameStr;
  235. }
  236. break;
  237. case WirePortDataType.FLOAT3x3:
  238. case WirePortDataType.FLOAT4x4:
  239. {
  240. name = MatrixNameStr;
  241. }
  242. break;
  243. case WirePortDataType.COLOR:
  244. {
  245. name = ColorNameStr;
  246. }
  247. break;
  248. }
  249. }
  250. int count = 0;
  251. bool foundName = false;
  252. while ( !foundName )
  253. {
  254. string inspectorName = name + count;
  255. string propertyName = UIUtils.GeneratePropertyName( inspectorName , PropertyType.Property );
  256. if ( IsUniformNameAvailable( propertyName ) )
  257. {
  258. outInspector = inspectorName;
  259. outProperty = propertyName;
  260. RegisterUniformName( nodeId, propertyName );
  261. return;
  262. }
  263. count += 1;
  264. }
  265. outProperty = string.Empty;
  266. outInspector = string.Empty;
  267. UIUtils.ShowMessage( "Could not find a valid name " + MessageSeverity.Warning );
  268. }
  269. public bool IsUniformNameAvailable( string name )
  270. {
  271. if ( m_availableUniformNames.ContainsKey( name ) && m_availableUniformNames[ name ] > -1 )
  272. return false;
  273. return true;
  274. }
  275. public bool IsLocalvariableNameAvailable( string name )
  276. {
  277. if ( m_availableLocalVariableNames.ContainsKey( name ) && m_availableLocalVariableNames[ name ] > -1 )
  278. return false;
  279. return true;
  280. }
  281. public bool GetPropertyName( int nodeId, string name )
  282. {
  283. if ( m_availablePropertyNames.ContainsKey( name ) )
  284. {
  285. if ( m_availablePropertyNames[ name ] > -1 )
  286. {
  287. return false;
  288. }
  289. else
  290. {
  291. m_availablePropertyNames[ name ] = nodeId;
  292. return true;
  293. }
  294. }
  295. m_availablePropertyNames.Add( name, nodeId );
  296. return true;
  297. }
  298. public bool ReleasePropertyName( int nodeId, string name )
  299. {
  300. if ( m_availablePropertyNames.ContainsKey( name ) )
  301. {
  302. if ( m_availablePropertyNames[ name ] == nodeId )
  303. {
  304. m_availablePropertyNames[ name ] = -1;
  305. return true;
  306. }
  307. }
  308. return false;
  309. }
  310. public void ReleaseAllPropertyNames()
  311. {
  312. m_availablePropertyNames.Clear();
  313. }
  314. public bool IsPropertyNameAvailable( string name )
  315. {
  316. if ( m_availablePropertyNames.ContainsKey( name ) && m_availablePropertyNames[ name ] > -1 )
  317. return false;
  318. return true;
  319. }
  320. public void ReleaseAllData()
  321. {
  322. ReleaseAllUVChannels();
  323. ReleaseAllUniformNames();
  324. ReleaseAllPropertyNames();
  325. ReleaseAllLocalVariableNames();
  326. }
  327. }
  328. }