TextureTransformNode.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System;
  4. using System.Collections.Generic;
  5. namespace AmplifyShaderEditor
  6. {
  7. [Serializable]
  8. [NodeAttributes( "Texture Transform", "Textures", "Gives access to texture tiling and offset as set on the material inspector" )]
  9. public sealed class TextureTransformNode : ParentNode
  10. {
  11. private readonly string[] Dummy = { string.Empty };
  12. private const string InstancedLabelStr = "Instanced";
  13. [SerializeField]
  14. private bool m_instanced = false;
  15. [SerializeField]
  16. private int m_referenceSamplerId = -1;
  17. [SerializeField]
  18. private int m_referenceNodeId = -1;
  19. [SerializeField]
  20. private TexturePropertyNode m_inputReferenceNode = null;
  21. private TexturePropertyNode m_referenceNode = null;
  22. private Vector4Node m_texCoordsHelper;
  23. private UpperLeftWidgetHelper m_upperLeftWidget = new UpperLeftWidgetHelper();
  24. private int m_cachedSamplerId = -1;
  25. private int m_cachedSamplerIdArray = -1;
  26. private int m_cachedSamplerIdCube = -1;
  27. private int m_cachedSamplerId3D = -1;
  28. protected override void CommonInit( int uniqueId )
  29. {
  30. base.CommonInit( uniqueId );
  31. AddInputPort( WirePortDataType.SAMPLER2D, false, "Tex" );
  32. m_inputPorts[ 0 ].CreatePortRestrictions( WirePortDataType.SAMPLER1D, WirePortDataType.SAMPLER2D, WirePortDataType.SAMPLER3D, WirePortDataType.SAMPLERCUBE, WirePortDataType.SAMPLER2DARRAY, WirePortDataType.OBJECT );
  33. AddOutputPort( WirePortDataType.FLOAT2, "Tiling" );
  34. AddOutputPort( WirePortDataType.FLOAT2, "Offset" );
  35. m_textLabelWidth = 80;
  36. m_autoWrapProperties = true;
  37. m_hasLeftDropdown = true;
  38. m_previewShaderGUID = "25ba2903568b00343ae06788994cab54";
  39. }
  40. public override void AfterCommonInit()
  41. {
  42. base.AfterCommonInit();
  43. if( PaddingTitleLeft == 0 )
  44. {
  45. PaddingTitleLeft = Constants.PropertyPickerWidth + Constants.IconsLeftRightMargin;
  46. if( PaddingTitleRight == 0 )
  47. PaddingTitleRight = Constants.PropertyPickerWidth + Constants.IconsLeftRightMargin;
  48. }
  49. }
  50. public override void RenderNodePreview()
  51. {
  52. //Runs at least one time
  53. if( !m_initialized )
  54. {
  55. // nodes with no preview don't update at all
  56. PreviewIsDirty = false;
  57. return;
  58. }
  59. if( !PreviewIsDirty )
  60. return;
  61. SetPreviewInputs();
  62. if( !Preferences.User.DisablePreviews )
  63. {
  64. RenderTexture temp = RenderTexture.active;
  65. RenderTexture.active = m_outputPorts[ 0 ].OutputPreviewTexture;
  66. PreviewMaterial.SetInt( "_PreviewID" , 0 );
  67. Graphics.Blit( null , m_outputPorts[ 0 ].OutputPreviewTexture , PreviewMaterial , m_previewMaterialPassId );
  68. RenderTexture.active = m_outputPorts[ 1 ].OutputPreviewTexture;
  69. PreviewMaterial.SetInt( "_PreviewID" , 1 );
  70. Graphics.Blit( null , m_outputPorts[ 1 ].OutputPreviewTexture , PreviewMaterial , m_previewMaterialPassId );
  71. RenderTexture.active = temp;
  72. }
  73. PreviewIsDirty = m_continuousPreviewRefresh;
  74. FinishPreviewRender = true;
  75. }
  76. void SetPreviewTexture( Texture newValue )
  77. {
  78. if( newValue is Cubemap )
  79. {
  80. m_previewMaterialPassId = 3;
  81. if( m_cachedSamplerIdCube == -1 )
  82. m_cachedSamplerIdCube = Shader.PropertyToID( "_Cube" );
  83. PreviewMaterial.SetTexture( m_cachedSamplerIdCube, newValue as Cubemap );
  84. }
  85. else if( newValue is Texture2DArray )
  86. {
  87. m_previewMaterialPassId = 2;
  88. if( m_cachedSamplerIdArray == -1 )
  89. m_cachedSamplerIdArray = Shader.PropertyToID( "_Array" );
  90. PreviewMaterial.SetTexture( m_cachedSamplerIdArray, newValue as Texture2DArray );
  91. }
  92. else if( newValue is Texture3D )
  93. {
  94. m_previewMaterialPassId = 1;
  95. if( m_cachedSamplerId3D == -1 )
  96. m_cachedSamplerId3D = Shader.PropertyToID( "_Sampler3D" );
  97. PreviewMaterial.SetTexture( m_cachedSamplerId3D, newValue as Texture3D );
  98. }
  99. else
  100. {
  101. m_previewMaterialPassId = 0;
  102. if( m_cachedSamplerId == -1 )
  103. m_cachedSamplerId = Shader.PropertyToID( "_Sampler" );
  104. PreviewMaterial.SetTexture( m_cachedSamplerId, newValue );
  105. }
  106. }
  107. public override void SetPreviewInputs()
  108. {
  109. base.SetPreviewInputs();
  110. if( m_inputPorts[ 0 ].IsConnected )
  111. {
  112. SetPreviewTexture( m_inputPorts[ 0 ].InputPreviewTexture( ContainerGraph ) );
  113. }
  114. else if( m_referenceNode != null )
  115. {
  116. if( m_referenceNode.Value != null )
  117. {
  118. SetPreviewTexture( m_referenceNode.Value );
  119. }
  120. else
  121. {
  122. SetPreviewTexture( m_referenceNode.PreviewTexture );
  123. }
  124. }
  125. }
  126. public override void OnInputPortConnected( int portId, int otherNodeId, int otherPortId, bool activateNode = true )
  127. {
  128. base.OnInputPortConnected( portId, otherNodeId, otherPortId, activateNode );
  129. m_inputPorts[ 0 ].MatchPortToConnection();
  130. m_inputReferenceNode = m_inputPorts[ 0 ].GetOutputNodeWhichIsNotRelay() as TexturePropertyNode;
  131. UpdateTitle();
  132. }
  133. public override void OnInputPortDisconnected( int portId )
  134. {
  135. base.OnInputPortDisconnected( portId );
  136. m_inputReferenceNode = null;
  137. UpdateTitle();
  138. }
  139. public override void OnConnectedOutputNodeChanges( int outputPortId, int otherNodeId, int otherPortId, string name, WirePortDataType type )
  140. {
  141. base.OnConnectedOutputNodeChanges( outputPortId, otherNodeId, otherPortId, name, type );
  142. m_inputPorts[ 0 ].MatchPortToConnection();
  143. UpdateTitle();
  144. }
  145. void UpdateTitle()
  146. {
  147. if( m_inputReferenceNode != null )
  148. {
  149. m_additionalContent.text = string.Format( Constants.PropertyValueLabel, m_inputReferenceNode.PropertyInspectorName );
  150. }
  151. else if( m_referenceSamplerId > -1 && m_referenceNode != null )
  152. {
  153. m_additionalContent.text = string.Format( Constants.PropertyValueLabel, m_referenceNode.PropertyInspectorName );
  154. }
  155. else
  156. {
  157. m_additionalContent.text = string.Empty;
  158. }
  159. m_sizeIsDirty = true;
  160. }
  161. public override void DrawProperties()
  162. {
  163. base.DrawProperties();
  164. bool guiEnabledBuffer = GUI.enabled;
  165. EditorGUI.BeginChangeCheck();
  166. List<string> arr = new List<string>( UIUtils.TexturePropertyNodeArr() );
  167. if( arr != null && arr.Count > 0 )
  168. {
  169. arr.Insert( 0, "None" );
  170. GUI.enabled = true && ( !m_inputPorts[ 0 ].IsConnected );
  171. m_referenceSamplerId = EditorGUILayoutPopup( Constants.AvailableReferenceStr, m_referenceSamplerId + 1, arr.ToArray() ) - 1;
  172. }
  173. else
  174. {
  175. m_referenceSamplerId = -1;
  176. GUI.enabled = false;
  177. EditorGUILayoutPopup( Constants.AvailableReferenceStr, m_referenceSamplerId, Dummy );
  178. }
  179. GUI.enabled = guiEnabledBuffer;
  180. if( EditorGUI.EndChangeCheck() )
  181. {
  182. m_referenceNode = UIUtils.GetTexturePropertyNode( m_referenceSamplerId );
  183. if( m_referenceNode != null )
  184. {
  185. m_referenceNodeId = m_referenceNode.UniqueId;
  186. }
  187. else
  188. {
  189. m_referenceNodeId = -1;
  190. m_referenceSamplerId = -1;
  191. }
  192. UpdateTitle();
  193. }
  194. m_instanced = EditorGUILayoutToggle( InstancedLabelStr, m_instanced );
  195. }
  196. public override string GenerateShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar )
  197. {
  198. if( !m_outputPorts[ 0 ].IsLocalValue( dataCollector.PortCategory ) )
  199. {
  200. base.GenerateShaderForOutput( outputId, ref dataCollector, ignoreLocalvar );
  201. string texTransform = string.Empty;
  202. if( m_inputPorts[ 0 ].IsConnected )
  203. {
  204. texTransform = m_inputPorts[ 0 ].GeneratePortInstructions( ref dataCollector ) + "_ST";
  205. }
  206. else if( m_referenceNode != null )
  207. {
  208. m_referenceNode.BaseGenerateShaderForOutput( outputId, ref dataCollector, ignoreLocalvar );
  209. texTransform = m_referenceNode.PropertyName + "_ST";
  210. }
  211. else
  212. {
  213. texTransform = "_ST";
  214. UIUtils.ShowMessage( UniqueId, "Please specify a texture sample on the Texture Transform Size node", MessageSeverity.Warning );
  215. }
  216. //bool excludeUniformKeyword = UIUtils.CurrentWindow.OutsideGraph.IsInstancedShader || UIUtils.CurrentWindow.OutsideGraph.IsSRP;
  217. //string uniformRegister = UIUtils.GenerateUniformName( excludeUniformKeyword, WirePortDataType.FLOAT4, texTransform );
  218. //dataCollector.AddToUniforms( UniqueId, uniformRegister, true );
  219. if( m_texCoordsHelper == null )
  220. {
  221. m_texCoordsHelper = CreateInstance<Vector4Node>();
  222. m_texCoordsHelper.ContainerGraph = ContainerGraph;
  223. m_texCoordsHelper.SetBaseUniqueId( UniqueId, true );
  224. m_texCoordsHelper.RegisterPropertyOnInstancing = false;
  225. m_texCoordsHelper.AddGlobalToSRPBatcher = true;
  226. }
  227. if( m_instanced )
  228. {
  229. m_texCoordsHelper.CurrentParameterType = PropertyType.InstancedProperty;
  230. }
  231. else
  232. {
  233. m_texCoordsHelper.CurrentParameterType = PropertyType.Global;
  234. }
  235. m_texCoordsHelper.ResetOutputLocals();
  236. m_texCoordsHelper.SetRawPropertyName( texTransform );
  237. texTransform = m_texCoordsHelper.GenerateShaderForOutput( 0, ref dataCollector, false );
  238. m_outputPorts[ 0 ].SetLocalValue( texTransform + ".xy", dataCollector.PortCategory );
  239. m_outputPorts[ 1 ].SetLocalValue( texTransform + ".zw", dataCollector.PortCategory );
  240. }
  241. return m_outputPorts[ outputId ].LocalValue( dataCollector.PortCategory );
  242. }
  243. public override void Draw( DrawInfo drawInfo )
  244. {
  245. base.Draw( drawInfo );
  246. EditorGUI.BeginChangeCheck();
  247. {
  248. List<string> arr = new List<string>( UIUtils.TexturePropertyNodeArr() );
  249. bool guiEnabledBuffer = GUI.enabled;
  250. if( arr != null && arr.Count > 0 )
  251. {
  252. arr.Insert( 0, "None" );
  253. GUI.enabled = true && ( !m_inputPorts[ 0 ].IsConnected );
  254. m_referenceSamplerId = m_upperLeftWidget.DrawWidget( this, m_referenceSamplerId + 1, arr.ToArray() ) - 1;
  255. }
  256. else
  257. {
  258. m_referenceSamplerId = -1;
  259. GUI.enabled = false;
  260. m_upperLeftWidget.DrawWidget( this, m_referenceSamplerId, Dummy );
  261. }
  262. GUI.enabled = guiEnabledBuffer;
  263. }
  264. if( EditorGUI.EndChangeCheck() )
  265. {
  266. m_referenceNode = UIUtils.GetTexturePropertyNode( m_referenceSamplerId );
  267. if( m_referenceNode != null )
  268. {
  269. m_referenceNodeId = m_referenceNode.UniqueId;
  270. }
  271. else
  272. {
  273. m_referenceNodeId = -1;
  274. m_referenceSamplerId = -1;
  275. }
  276. UpdateTitle();
  277. }
  278. }
  279. public override void RefreshExternalReferences()
  280. {
  281. base.RefreshExternalReferences();
  282. m_referenceNode = UIUtils.GetNode( m_referenceNodeId ) as TexturePropertyNode;
  283. m_referenceSamplerId = UIUtils.GetTexturePropertyNodeRegisterId( m_referenceNodeId );
  284. UpdateTitle();
  285. }
  286. public override void ReadFromString( ref string[] nodeParams )
  287. {
  288. base.ReadFromString( ref nodeParams );
  289. m_referenceNodeId = Convert.ToInt32( GetCurrentParam( ref nodeParams ) );
  290. if( UIUtils.CurrentShaderVersion() > 17200 )
  291. {
  292. m_instanced = Convert.ToBoolean( GetCurrentParam( ref nodeParams ) );
  293. }
  294. }
  295. public override void WriteToString( ref string nodeInfo, ref string connectionsInfo )
  296. {
  297. base.WriteToString( ref nodeInfo, ref connectionsInfo );
  298. IOUtils.AddFieldValueToString( ref nodeInfo, m_referenceNodeId );
  299. IOUtils.AddFieldValueToString( ref nodeInfo, m_instanced );
  300. }
  301. public override void Destroy()
  302. {
  303. base.Destroy();
  304. m_referenceNode = null;
  305. m_inputReferenceNode = null;
  306. m_upperLeftWidget = null;
  307. if( m_texCoordsHelper != null )
  308. {
  309. //Not calling m_texCoordsHelper.Destroy() on purpose so UIUtils does not incorrectly unregister stuff
  310. DestroyImmediate( m_texCoordsHelper );
  311. m_texCoordsHelper = null;
  312. }
  313. }
  314. }
  315. }