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.

190 lines
5.5 KiB

  1. // Amplify Shader Editor - Visual Shader Editing Tool
  2. // Copyright (c) Amplify Creations, Lda <info@amplify.pt>
  3. using System;
  4. using UnityEngine;
  5. namespace AmplifyShaderEditor
  6. {
  7. [Serializable]
  8. public class TemplateVertexData
  9. {
  10. [SerializeField]
  11. private TemplateSemantics m_semantics = TemplateSemantics.NONE;
  12. [SerializeField]
  13. private WirePortDataType m_dataType = WirePortDataType.OBJECT;
  14. [SerializeField]
  15. private string m_varName = string.Empty;
  16. [SerializeField]
  17. private TemplateInfoOnSematics m_dataInfo = TemplateInfoOnSematics.NONE;
  18. [SerializeField]
  19. private string m_dataSwizzle = string.Empty;
  20. [SerializeField]
  21. private bool m_available = false;
  22. [SerializeField]
  23. private string m_varNameWithSwizzle = string.Empty;
  24. [SerializeField]
  25. private bool m_isSingleComponent = true;
  26. [SerializeField]
  27. private bool m_excludeStructPrefix = false;
  28. [SerializeField]
  29. private string[] m_components = { "0", "0", "0", "0" };
  30. [SerializeField]
  31. private bool[] m_componentUsage = { false, false,false,false };
  32. public TemplateVertexData( TemplateSemantics semantics, WirePortDataType dataType, string varName )
  33. {
  34. m_semantics = semantics;
  35. m_dataType = dataType;
  36. m_varName = varName;
  37. m_varNameWithSwizzle = varName;
  38. }
  39. public TemplateVertexData( TemplateSemantics semantics, WirePortDataType dataType, string varName, string dataSwizzle )
  40. {
  41. m_semantics = semantics;
  42. m_dataType = dataType;
  43. m_varName = varName;
  44. m_dataSwizzle = dataSwizzle;
  45. m_varNameWithSwizzle = varName + dataSwizzle;
  46. }
  47. public TemplateVertexData( TemplateVertexData other )
  48. {
  49. m_semantics = other.m_semantics;
  50. m_dataType = other.m_dataType;
  51. m_varName = other.m_varName;
  52. m_dataInfo = other.m_dataInfo;
  53. m_dataSwizzle = other.m_dataSwizzle;
  54. m_available = other.m_available;
  55. m_varNameWithSwizzle = other.m_varNameWithSwizzle;
  56. m_isSingleComponent = other.IsSingleComponent;
  57. m_excludeStructPrefix = other.ExcludeStructPrefix;
  58. for( int i = 0; i < 4; i++ )
  59. {
  60. m_components[ i ] = other.Components[ i ];
  61. }
  62. }
  63. public void RegisterComponent( char channelId, string value )
  64. {
  65. int channelIdInt = -1;
  66. switch( channelId )
  67. {
  68. case 'r':
  69. case 'x': channelIdInt = 0; break;
  70. case 'g':
  71. case 'y': channelIdInt = 1; break;
  72. case 'b':
  73. case 'z': channelIdInt = 2; break;
  74. case 'a':
  75. case 'w': channelIdInt = 3; break;
  76. }
  77. if( channelId < 0 )
  78. {
  79. Debug.LogWarning( "Attempting to create interpolated data from invalid channel " + channelId );
  80. return;
  81. }
  82. RegisterComponent( channelIdInt, value );
  83. }
  84. public void RegisterComponent( int channelId, string value )
  85. {
  86. channelId = Mathf.Clamp( channelId, 0, 3 );
  87. m_components[ channelId ] = value;
  88. m_componentUsage[ channelId ] = true;
  89. m_isSingleComponent = false;
  90. }
  91. public void BuildVar( PrecisionType precisionType = PrecisionType.Float )
  92. {
  93. if( m_isSingleComponent )
  94. return;
  95. WirePortDataType dataType = WirePortDataType.FLOAT;
  96. if( m_componentUsage[ 3 ] )
  97. {
  98. dataType = WirePortDataType.FLOAT4;
  99. }
  100. else if( m_componentUsage[ 2 ] )
  101. {
  102. dataType = WirePortDataType.FLOAT3;
  103. }
  104. else if( m_componentUsage[ 1 ] )
  105. {
  106. dataType = WirePortDataType.FLOAT2;
  107. }
  108. string newVar = UIUtils.PrecisionWirePortToCgType( precisionType, dataType );
  109. newVar += "( ";
  110. switch( dataType )
  111. {
  112. default: newVar += "0"; break;
  113. case WirePortDataType.INT:
  114. case WirePortDataType.FLOAT:
  115. {
  116. newVar += "{0}."+Components[ 0 ];
  117. }
  118. break;
  119. case WirePortDataType.FLOAT2:
  120. {
  121. newVar += "{0}." + Components[ 0 ] + ", " +
  122. "{0}." + Components[ 1 ];
  123. }
  124. break;
  125. case WirePortDataType.FLOAT3:
  126. {
  127. newVar += "{0}." + Components[ 0 ] + ", " +
  128. "{0}." + Components[ 1 ] + ", " +
  129. "{0}." + Components[ 2 ];
  130. }
  131. break;
  132. case WirePortDataType.FLOAT4:
  133. case WirePortDataType.COLOR:
  134. {
  135. newVar += "{0}." + Components[ 0 ] + ", " +
  136. "{0}." + Components[ 1 ] + ", " +
  137. "{0}." + Components[ 2 ] + ", " +
  138. "{0}." + Components[ 3 ];
  139. }
  140. break;
  141. }
  142. newVar += " )";
  143. m_varName = newVar;
  144. m_varNameWithSwizzle = newVar;
  145. }
  146. public bool ExcludeStructPrefix { get { return m_excludeStructPrefix; } set { m_excludeStructPrefix = value; } }
  147. public bool IsSingleComponent { get { return m_isSingleComponent; } }
  148. public string[] Components { get { return m_components; } }
  149. public TemplateSemantics Semantics { get { return m_semantics; } }
  150. public WirePortDataType DataType { get { return m_dataType; } }
  151. public string VarName { get { return m_varName; } set { m_varName = value; m_varNameWithSwizzle = value + m_dataSwizzle; } }
  152. public string DataSwizzle { get { return m_dataSwizzle; } set { m_dataSwizzle = value; m_varNameWithSwizzle = m_varName + value; } }
  153. public TemplateInfoOnSematics DataInfo { get { return m_dataInfo; } set { m_dataInfo = value; } }
  154. public bool Available { get { return m_available; } set { m_available = value; } }
  155. public string VarNameWithSwizzle { get { return m_varNameWithSwizzle; } }
  156. public WirePortDataType SwizzleType
  157. {
  158. get
  159. {
  160. if ( string.IsNullOrEmpty( m_dataSwizzle ) )
  161. return m_dataType;
  162. WirePortDataType newType = m_dataType;
  163. switch ( m_dataSwizzle.Length )
  164. {
  165. case 2: newType = WirePortDataType.FLOAT;break;
  166. case 3: newType = WirePortDataType.FLOAT2; break;
  167. case 4: newType = WirePortDataType.FLOAT3; break;
  168. case 5: newType = WirePortDataType.FLOAT4; break;
  169. }
  170. return newType;
  171. }
  172. }
  173. }
  174. }