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.

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