TexCoordVertexDataNode.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. // Amplify Shader Editor - Visual Shader Editing Tool
  2. // Copyright (c) Amplify Creations, Lda <info@amplify.pt>
  3. using System;
  4. using UnityEngine;
  5. using UnityEditor;
  6. namespace AmplifyShaderEditor
  7. {
  8. [Serializable]
  9. [NodeAttributes( "Vertex TexCoord", "Vertex Data", "Vertex texture coordinates, can be used in both local vertex offset and fragment outputs", tags: "uv" )]
  10. public sealed class TexCoordVertexDataNode : VertexDataNode
  11. {
  12. [SerializeField]
  13. private int m_texcoordSize = 2;
  14. [SerializeField]
  15. private int m_index = 0;
  16. protected override void CommonInit( int uniqueId )
  17. {
  18. base.CommonInit( uniqueId );
  19. m_currentVertexData = "texcoord";
  20. ChangeOutputProperties( 0, "UV", WirePortDataType.FLOAT2, false );
  21. m_outputPorts[ 1 ].Name = "U";
  22. m_outputPorts[ 2 ].Name = "V";
  23. m_outputPorts[ 3 ].Visible = false;
  24. m_outputPorts[ 4 ].Visible = false;
  25. m_outputPorts[ 3 ].Name = "W";
  26. m_outputPorts[ 4 ].Name = "T";
  27. m_autoWrapProperties = true;
  28. m_hasLeftDropdown = true;
  29. m_previewShaderGUID = "6c1bee77276896041bbb73b1b9e7f8ac";
  30. }
  31. public override void DrawProperties()
  32. {
  33. base.DrawProperties();
  34. EditorGUI.BeginChangeCheck();
  35. m_texcoordSize = EditorGUILayoutIntPopup( Constants.AvailableUVSizesLabel, m_texcoordSize, Constants.AvailableUVSizesStr, Constants.AvailableUVSizes );
  36. if( EditorGUI.EndChangeCheck() )
  37. {
  38. UpdateOutput();
  39. }
  40. EditorGUI.BeginChangeCheck();
  41. m_index = EditorGUILayoutIntPopup( Constants.AvailableUVChannelLabel, m_index, Constants.AvailableUVSetsStr, Constants.AvailableUVChannels );
  42. if( EditorGUI.EndChangeCheck() )
  43. {
  44. m_currentVertexData = ( m_index == 0 ) ? "texcoord" : "texcoord" + Constants.AvailableUVChannelsStr[ m_index ];
  45. }
  46. }
  47. public override void Draw( DrawInfo drawInfo )
  48. {
  49. base.Draw( drawInfo );
  50. if( m_dropdownEditing )
  51. {
  52. EditorGUI.BeginChangeCheck();
  53. m_texcoordSize = EditorGUIIntPopup( m_dropdownRect, m_texcoordSize, Constants.AvailableUVSizesStr, Constants.AvailableUVSizes, UIUtils.PropertyPopUp );
  54. if( EditorGUI.EndChangeCheck() )
  55. {
  56. UpdateOutput();
  57. DropdownEditing = false;
  58. }
  59. }
  60. }
  61. private void UpdateOutput()
  62. {
  63. if( m_texcoordSize == 3 )
  64. {
  65. m_outputPorts[ 0 ].ChangeType( WirePortDataType.FLOAT3, false );
  66. m_outputPorts[ 0 ].Name = "UVW";
  67. m_outputPorts[ 3 ].Visible = true;
  68. m_outputPorts[ 4 ].Visible = false;
  69. }
  70. else if( m_texcoordSize == 4 )
  71. {
  72. m_outputPorts[ 0 ].ChangeType( WirePortDataType.FLOAT4, false );
  73. m_outputPorts[ 0 ].Name = "UVWT";
  74. m_outputPorts[ 3 ].Visible = true;
  75. m_outputPorts[ 4 ].Visible = true;
  76. }
  77. else
  78. {
  79. m_texcoordSize = 2;
  80. m_outputPorts[ 0 ].ChangeType( WirePortDataType.FLOAT2, false );
  81. m_outputPorts[ 0 ].Name = "UV";
  82. m_outputPorts[ 3 ].Visible = false;
  83. m_outputPorts[ 4 ].Visible = false;
  84. }
  85. m_sizeIsDirty = true;
  86. }
  87. public override string GenerateShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalVar )
  88. {
  89. if( dataCollector.IsTemplate )
  90. {
  91. if( !dataCollector.TemplateDataCollectorInstance.HasUV( m_index ) )
  92. {
  93. dataCollector.TemplateDataCollectorInstance.RegisterUV( m_index, m_outputPorts[ 0 ].DataType );
  94. }
  95. string result = string.Empty;
  96. if( dataCollector.TemplateDataCollectorInstance.GetCustomInterpolatedData( TemplateHelperFunctions.IntToUVChannelInfo[ m_index ], m_outputPorts[ 0 ].DataType, PrecisionType.Float, ref result, false, dataCollector.PortCategory ) )
  97. {
  98. return GetOutputVectorItem( 0, outputId, result );
  99. }
  100. else
  101. if( dataCollector.TemplateDataCollectorInstance.HasUV( m_index ) )
  102. {
  103. InterpDataHelper info = dataCollector.TemplateDataCollectorInstance.GetUVInfo( m_index );
  104. if( outputId == 0 )
  105. {
  106. return dataCollector.TemplateDataCollectorInstance.GetUVName( m_index, m_outputPorts[ 0 ].DataType );
  107. }
  108. else if( outputId <= TemplateHelperFunctions.DataTypeChannelUsage[ info.VarType ] )
  109. {
  110. return GetOutputVectorItem( 0, outputId, info.VarName );
  111. }
  112. Debug.LogWarning( "Attempting to access inexisting UV channel" );
  113. }
  114. else
  115. {
  116. Debug.LogWarning( "Attempting to access non-registered UV" );
  117. }
  118. return "0";
  119. }
  120. if( dataCollector.PortCategory == MasterNodePortCategory.Fragment || dataCollector.PortCategory == MasterNodePortCategory.Debug )
  121. {
  122. if( m_texcoordSize > 2 )
  123. dataCollector.UsingHigherSizeTexcoords = true;
  124. }
  125. WirePortDataType size = (WirePortDataType)( 1 << ( m_texcoordSize + 1 ) );
  126. string texcoords = GeneratorUtils.GenerateAutoUVs( ref dataCollector, UniqueId, m_index, null, size );
  127. return GetOutputVectorItem( 0, outputId, texcoords );
  128. }
  129. /// <summary>
  130. /// Generates UV properties and uniforms and returns the varible name to use in the fragment shader
  131. /// </summary>
  132. /// <param name="dataCollector"></param>
  133. /// <param name="uniqueId"></param>
  134. /// <param name="index"></param>
  135. /// <returns>frag variable name</returns>
  136. static public string GenerateFragUVs( ref MasterNodeDataCollector dataCollector, int uniqueId, int index, string propertyName = null, WirePortDataType size = WirePortDataType.FLOAT2 )
  137. {
  138. string dummyPropUV = "_texcoord" + ( index > 0 ? ( index + 1 ).ToString() : "" );
  139. string dummyUV = "uv" + ( index > 0 ? ( index + 1 ).ToString() : "" ) + dummyPropUV;
  140. dataCollector.AddToProperties( uniqueId, "[HideInInspector] " + dummyPropUV + "( \"\", 2D ) = \"white\" {}", 100 );
  141. dataCollector.AddToInput( uniqueId, dummyUV, size );
  142. string result = Constants.InputVarStr + "." + dummyUV;
  143. if( !string.IsNullOrEmpty( propertyName ) )
  144. {
  145. dataCollector.AddToUniforms( uniqueId, "uniform float4 " + propertyName + "_ST;" );
  146. dataCollector.AddToLocalVariables( uniqueId, PrecisionType.Float, size, "uv" + propertyName, result + " * " + propertyName + "_ST.xy + " + propertyName + "_ST.zw" );
  147. result = "uv" + propertyName;
  148. }
  149. return result;
  150. }
  151. static public string GenerateVertexUVs( ref MasterNodeDataCollector dataCollector, int uniqueId, int index, string propertyName = null, WirePortDataType size = WirePortDataType.FLOAT2 )
  152. {
  153. string result = Constants.VertexShaderInputStr + ".texcoord";
  154. if( index > 0 )
  155. {
  156. result += index.ToString();
  157. }
  158. switch( size )
  159. {
  160. default:
  161. case WirePortDataType.FLOAT2:
  162. {
  163. result += ".xy";
  164. }
  165. break;
  166. case WirePortDataType.FLOAT3:
  167. {
  168. result += ".xyz";
  169. }
  170. break;
  171. case WirePortDataType.FLOAT4: break;
  172. }
  173. if( !string.IsNullOrEmpty( propertyName ) )
  174. {
  175. dataCollector.AddToUniforms( uniqueId, "uniform float4 " + propertyName + "_ST;" );
  176. dataCollector.AddToVertexLocalVariables( uniqueId, UIUtils.WirePortToCgType( size ) + " uv" + propertyName + " = " + Constants.VertexShaderInputStr + ".texcoord" + ( index > 0 ? index.ToString() : string.Empty ) + " * " + propertyName + "_ST.xy + " + propertyName + "_ST.zw;" );
  177. result = "uv" + propertyName;
  178. }
  179. return result;
  180. }
  181. public override void ReadFromString( ref string[] nodeParams )
  182. {
  183. base.ReadFromString( ref nodeParams );
  184. if( UIUtils.CurrentShaderVersion() > 2502 )
  185. {
  186. m_index = Convert.ToInt32( GetCurrentParam( ref nodeParams ) );
  187. }
  188. if( UIUtils.CurrentShaderVersion() > 5111 )
  189. {
  190. m_texcoordSize = Convert.ToInt32( GetCurrentParam( ref nodeParams ) );
  191. UpdateOutput();
  192. }
  193. }
  194. public override void WriteToString( ref string nodeInfo, ref string connectionsInfo )
  195. {
  196. base.WriteToString( ref nodeInfo, ref connectionsInfo );
  197. IOUtils.AddFieldValueToString( ref nodeInfo, m_index );
  198. IOUtils.AddFieldValueToString( ref nodeInfo, m_texcoordSize );
  199. }
  200. public override void PropagateNodeData( NodeData nodeData, ref MasterNodeDataCollector dataCollector )
  201. {
  202. base.PropagateNodeData( nodeData, ref dataCollector );
  203. if( dataCollector.IsTemplate )
  204. {
  205. dataCollector.TemplateDataCollectorInstance.SetUVUsage( m_index , m_texcoordSize );
  206. }
  207. else
  208. {
  209. dataCollector.SetTextureChannelSize( m_index , m_outputPorts[ 0 ].DataType );
  210. if( m_index > 3 )
  211. {
  212. dataCollector.AddCustomAppData( string.Format( TemplateHelperFunctions.TexUVFullSemantic , m_index ) );
  213. }
  214. }
  215. }
  216. }
  217. }