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.

207 lines
7.6 KiB

  1. using UnityEngine;
  2. using UnityEditor;
  3. using System;
  4. namespace AmplifyShaderEditor
  5. {
  6. [Serializable]
  7. [NodeAttributes( "Texture Transform", "Textures", "Gives access to texture tiling and offset as set on the material inspector" )]
  8. public sealed class TextureTransformNode : ParentNode
  9. {
  10. private readonly string[] Dummy = { string.Empty };
  11. [SerializeField]
  12. private int m_referenceSamplerId = -1;
  13. [SerializeField]
  14. private int m_referenceNodeId = -1;
  15. [SerializeField]
  16. private TexturePropertyNode m_inputReferenceNode = null;
  17. private TexturePropertyNode m_referenceNode = null;
  18. private UpperLeftWidgetHelper m_upperLeftWidget = new UpperLeftWidgetHelper();
  19. protected override void CommonInit( int uniqueId )
  20. {
  21. base.CommonInit( uniqueId );
  22. AddInputPort( WirePortDataType.SAMPLER2D, false, "Tex" );
  23. m_inputPorts[ 0 ].CreatePortRestrictions( WirePortDataType.SAMPLER1D, WirePortDataType.SAMPLER2D, WirePortDataType.SAMPLER3D, WirePortDataType.SAMPLERCUBE, WirePortDataType.OBJECT );
  24. AddOutputPort( WirePortDataType.FLOAT2, "Tiling" );
  25. AddOutputPort( WirePortDataType.FLOAT2, "Offset" );
  26. m_textLabelWidth = 80;
  27. m_autoWrapProperties = true;
  28. m_hasLeftDropdown = true;
  29. }
  30. public override void AfterCommonInit()
  31. {
  32. base.AfterCommonInit();
  33. if( PaddingTitleLeft == 0 )
  34. {
  35. PaddingTitleLeft = Constants.PropertyPickerWidth + Constants.IconsLeftRightMargin;
  36. if( PaddingTitleRight == 0 )
  37. PaddingTitleRight = Constants.PropertyPickerWidth + Constants.IconsLeftRightMargin;
  38. }
  39. }
  40. public override void OnInputPortConnected( int portId, int otherNodeId, int otherPortId, bool activateNode = true )
  41. {
  42. base.OnInputPortConnected( portId, otherNodeId, otherPortId, activateNode );
  43. m_inputReferenceNode = m_inputPorts[ 0 ].GetOutputNode() as TexturePropertyNode;
  44. }
  45. public override void OnInputPortDisconnected( int portId )
  46. {
  47. base.OnInputPortDisconnected( portId );
  48. m_inputReferenceNode = null;
  49. }
  50. void UpdateTitle()
  51. {
  52. if( m_inputReferenceNode != null )
  53. {
  54. m_additionalContent.text = string.Format( Constants.PropertyValueLabel, m_inputReferenceNode.PropertyInspectorName );
  55. }
  56. else if( m_referenceSamplerId > -1 && m_referenceNode != null )
  57. {
  58. m_additionalContent.text = string.Format( Constants.PropertyValueLabel, m_referenceNode.PropertyInspectorName );
  59. }
  60. else
  61. {
  62. m_additionalContent.text = string.Empty;
  63. }
  64. m_sizeIsDirty = true;
  65. }
  66. public override void DrawProperties()
  67. {
  68. base.DrawProperties();
  69. EditorGUI.BeginChangeCheck();
  70. string[] arr = UIUtils.TexturePropertyNodeArr();
  71. bool guiEnabledBuffer = GUI.enabled;
  72. if( arr != null && arr.Length > 0 )
  73. {
  74. GUI.enabled = true && ( !m_inputPorts[ 0 ].IsConnected );
  75. m_referenceSamplerId = EditorGUILayoutPopup( Constants.AvailableReferenceStr, m_referenceSamplerId, arr );
  76. }
  77. else
  78. {
  79. m_referenceSamplerId = -1;
  80. GUI.enabled = false;
  81. m_referenceSamplerId = EditorGUILayoutPopup( Constants.AvailableReferenceStr, m_referenceSamplerId, Dummy );
  82. }
  83. GUI.enabled = guiEnabledBuffer;
  84. if( EditorGUI.EndChangeCheck() )
  85. {
  86. m_referenceNode = UIUtils.GetTexturePropertyNode( m_referenceSamplerId );
  87. m_referenceNodeId = m_referenceNode.UniqueId;
  88. UpdateTitle();
  89. }
  90. }
  91. public override string GenerateShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar )
  92. {
  93. base.GenerateShaderForOutput( outputId, ref dataCollector, ignoreLocalvar );
  94. m_inputPorts[ 0 ].GeneratePortInstructions( ref dataCollector );
  95. string texTransform = string.Empty;
  96. if( m_inputPorts[ 0 ].IsConnected )
  97. {
  98. texTransform = m_inputPorts[ 0 ].GeneratePortInstructions( ref dataCollector ) + "_ST";
  99. }
  100. else if( m_referenceNode != null )
  101. {
  102. m_referenceNode.BaseGenerateShaderForOutput( outputId, ref dataCollector, ignoreLocalvar );
  103. texTransform = m_referenceNode.PropertyName + "_ST";
  104. }
  105. else
  106. {
  107. texTransform = "_ST";
  108. UIUtils.ShowMessage( "Please specify a texture sample on the Texture Transform Size node", MessageSeverity.Warning );
  109. }
  110. dataCollector.AddToUniforms( UniqueId, "uniform float4 " + texTransform + ";" );
  111. switch( outputId )
  112. {
  113. case 0: return ( texTransform + ".xy" );
  114. case 1: return ( texTransform + ".zw" );
  115. }
  116. return string.Empty;
  117. }
  118. public override void Draw( DrawInfo drawInfo )
  119. {
  120. base.Draw( drawInfo );
  121. EditorGUI.BeginChangeCheck();
  122. {
  123. string[] arr = UIUtils.TexturePropertyNodeArr();
  124. bool guiEnabledBuffer = GUI.enabled;
  125. if( arr != null && arr.Length > 0 )
  126. {
  127. GUI.enabled = true && ( !m_inputPorts[ 0 ].IsConnected );
  128. m_referenceSamplerId = m_upperLeftWidget.DrawWidget( this, m_referenceSamplerId, arr );
  129. }
  130. else
  131. {
  132. m_referenceSamplerId = -1;
  133. GUI.enabled = false;
  134. m_referenceSamplerId = m_upperLeftWidget.DrawWidget( this, m_referenceSamplerId, Dummy );
  135. }
  136. GUI.enabled = guiEnabledBuffer;
  137. }
  138. if( EditorGUI.EndChangeCheck() )
  139. {
  140. m_referenceNode = UIUtils.GetTexturePropertyNode( m_referenceSamplerId );
  141. m_referenceNodeId = m_referenceNode.UniqueId;
  142. UpdateTitle();
  143. }
  144. if( m_referenceNode == null && m_referenceNodeId > -1 )
  145. {
  146. m_referenceNodeId = -1;
  147. m_referenceSamplerId = -1;
  148. UpdateTitle();
  149. }
  150. }
  151. public override void RefreshExternalReferences()
  152. {
  153. base.RefreshExternalReferences();
  154. m_referenceNode = UIUtils.GetNode( m_referenceNodeId ) as TexturePropertyNode;
  155. m_referenceSamplerId = UIUtils.GetTexturePropertyNodeRegisterId( m_referenceNodeId );
  156. UpdateTitle();
  157. }
  158. public override void ReadFromString( ref string[] nodeParams )
  159. {
  160. base.ReadFromString( ref nodeParams );
  161. m_referenceNodeId = Convert.ToInt32( GetCurrentParam( ref nodeParams ) );
  162. }
  163. public override void WriteToString( ref string nodeInfo, ref string connectionsInfo )
  164. {
  165. base.WriteToString( ref nodeInfo, ref connectionsInfo );
  166. IOUtils.AddFieldValueToString( ref nodeInfo, m_referenceNodeId );
  167. }
  168. public override void Destroy()
  169. {
  170. base.Destroy();
  171. m_referenceNode = null;
  172. m_inputReferenceNode = null;
  173. m_upperLeftWidget = null;
  174. }
  175. }
  176. }