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.

558 lines
18 KiB

  1. // Amplify Shader Editor - Visual Shader Editing Tool
  2. // Copyright (c) Amplify Creations, Lda <info@amplify.pt>
  3. using UnityEngine;
  4. using UnityEditor;
  5. using System;
  6. using System.Collections.Generic;
  7. namespace AmplifyShaderEditor
  8. {
  9. [Serializable]
  10. [NodeAttributes( "Texture Coordinates", "UV Coordinates", "Texture UV coordinates set, if <b>Tex</b> is connected to a texture object it will use that texture scale factors, otherwise uses <b>Tilling</b> and <b>Offset</b> port values", null, KeyCode.U )]
  11. public sealed class TextureCoordinatesNode : ParentNode
  12. {
  13. private const string DummyPropertyDec = "[HideInInspector] _DummyTex{0}( \"\", 2D ) = \"white\"";
  14. private const string DummyUniformDec = "uniform sampler2D _DummyTex{0};";
  15. private const string DummyTexCoordDef = "uv{0}_DummyTex{0}";
  16. private const string DummyTexCoordSurfDef = "float2 texCoordDummy{0} = {1}.uv{2}_DummyTex{2}*{3} + {4};";
  17. private const string DummyTexCoordSurfVar = "texCoordDummy{0}";
  18. private readonly string[] Dummy = { string.Empty };
  19. private const string TilingStr = "Tiling";
  20. private const string OffsetStr = "Offset";
  21. private const string TexCoordStr = "texcoord_";
  22. [SerializeField]
  23. private int m_referenceArrayId = -1;
  24. [SerializeField]
  25. private int m_referenceNodeId = -1;
  26. [SerializeField]
  27. private int m_textureCoordChannel = 0;
  28. //[SerializeField]
  29. //private int m_texcoordId = -1;
  30. [SerializeField]
  31. private int m_texcoordSize = 2;
  32. [SerializeField]
  33. private string m_surfaceTexcoordName = string.Empty;
  34. [SerializeField]
  35. private TexturePropertyNode m_inputReferenceNode = null;
  36. private TexturePropertyNode m_referenceNode = null;
  37. private InputPort m_texPort = null;
  38. private InputPort m_tilingPort = null;
  39. private InputPort m_offsetPort = null;
  40. protected override void CommonInit( int uniqueId )
  41. {
  42. base.CommonInit( uniqueId );
  43. AddInputPort( WirePortDataType.SAMPLER2D, false, "Tex", -1, MasterNodePortCategory.Fragment, 2 );
  44. m_texPort = m_inputPorts[ m_inputPorts.Count - 1 ];
  45. m_texPort.CreatePortRestrictions( WirePortDataType.SAMPLER1D, WirePortDataType.SAMPLER2D, WirePortDataType.SAMPLER3D, WirePortDataType.SAMPLERCUBE, WirePortDataType.OBJECT );
  46. AddInputPort( WirePortDataType.FLOAT2, false, "Tiling", -1, MasterNodePortCategory.Fragment, 0 );
  47. m_tilingPort = m_inputPorts[ m_inputPorts.Count - 1 ];
  48. m_tilingPort.Vector2InternalData = new Vector2( 1, 1 );
  49. AddInputPort( WirePortDataType.FLOAT2, false, "Offset", -1, MasterNodePortCategory.Fragment, 1 );
  50. m_offsetPort = m_inputPorts[ m_inputPorts.Count - 1 ];
  51. AddOutputVectorPorts( WirePortDataType.FLOAT2, "UV" );
  52. m_outputPorts[ 1 ].Name = "U";
  53. m_outputPorts[ 2 ].Name = "V";
  54. AddOutputPort( WirePortDataType.FLOAT, "W" );
  55. AddOutputPort( WirePortDataType.FLOAT, "T" );
  56. m_textLabelWidth = 90;
  57. m_useInternalPortData = true;
  58. m_autoWrapProperties = true;
  59. m_tilingPort.Category = MasterNodePortCategory.Vertex;
  60. m_offsetPort.Category = MasterNodePortCategory.Vertex;
  61. UpdateOutput();
  62. m_previewShaderGUID = "085e462b2de441a42949be0e666cf5d2";
  63. }
  64. public override void Reset()
  65. {
  66. m_surfaceTexcoordName = string.Empty;
  67. }
  68. public override void OnInputPortConnected( int portId, int otherNodeId, int otherPortId, bool activateNode = true )
  69. {
  70. base.OnInputPortConnected( portId, otherNodeId, otherPortId, activateNode );
  71. if( portId == 2 )
  72. {
  73. m_inputReferenceNode = m_texPort.GetOutputNode() as TexturePropertyNode;
  74. UpdatePorts();
  75. }
  76. }
  77. public override void OnInputPortDisconnected( int portId )
  78. {
  79. base.OnInputPortDisconnected( portId );
  80. if( portId == 2 )
  81. {
  82. m_inputReferenceNode = null;
  83. UpdatePorts();
  84. }
  85. }
  86. void UpdateTitle()
  87. {
  88. if( m_inputReferenceNode != null )
  89. {
  90. m_additionalContent.text = string.Format( "Value( {0} )", m_inputReferenceNode.PropertyInspectorName );
  91. }
  92. else if( m_referenceArrayId > -1 && m_referenceNode != null )
  93. {
  94. m_referenceNode = UIUtils.GetTexturePropertyNode( m_referenceArrayId );
  95. m_additionalContent.text = string.Format( "Value( {0} )", m_referenceNode.PropertyInspectorName );
  96. }
  97. else
  98. {
  99. m_additionalContent.text = string.Empty;
  100. }
  101. m_sizeIsDirty = true;
  102. }
  103. void UpdatePorts()
  104. {
  105. if( m_inputReferenceNode != null || m_texPort.IsConnected )
  106. {
  107. m_tilingPort.Locked = true;
  108. m_offsetPort.Locked = true;
  109. }
  110. else if( m_referenceArrayId > -1 )
  111. {
  112. m_tilingPort.Locked = true;
  113. m_offsetPort.Locked = true;
  114. }
  115. else
  116. {
  117. m_tilingPort.Locked = false;
  118. m_offsetPort.Locked = false;
  119. }
  120. }
  121. public override void DrawProperties()
  122. {
  123. bool guiEnabledBuffer = GUI.enabled;
  124. EditorGUI.BeginChangeCheck();
  125. List<string> arr = ( m_inputReferenceNode != null ) ? null : new List<string>( UIUtils.TexturePropertyNodeArr() );
  126. if( arr != null && arr.Count > 0 )
  127. {
  128. arr.Insert( 0, "None" );
  129. GUI.enabled = true;
  130. m_referenceArrayId = EditorGUILayoutPopup( Constants.AvailableReferenceStr, m_referenceArrayId + 1, arr.ToArray() ) - 1;
  131. }
  132. else
  133. {
  134. m_referenceArrayId = -1;
  135. GUI.enabled = false;
  136. EditorGUILayoutPopup( Constants.AvailableReferenceStr, 0, Dummy );
  137. }
  138. GUI.enabled = guiEnabledBuffer;
  139. if( EditorGUI.EndChangeCheck() )
  140. {
  141. m_referenceNode = UIUtils.GetTexturePropertyNode( m_referenceArrayId );
  142. if( m_referenceNode != null )
  143. {
  144. m_referenceNodeId = m_referenceNode.UniqueId;
  145. }
  146. else
  147. {
  148. m_referenceNodeId = -1;
  149. m_referenceArrayId = -1;
  150. }
  151. UpdateTitle();
  152. UpdatePorts();
  153. }
  154. EditorGUI.BeginChangeCheck();
  155. m_texcoordSize = EditorGUILayoutIntPopup( Constants.AvailableUVSizesLabel, m_texcoordSize, Constants.AvailableUVSizesStr, Constants.AvailableUVSizes );
  156. if( EditorGUI.EndChangeCheck() )
  157. {
  158. UpdateOutput();
  159. }
  160. m_textureCoordChannel = EditorGUILayoutIntPopup( Constants.AvailableUVSetsLabel, m_textureCoordChannel, Constants.AvailableUVSetsStr, Constants.AvailableUVSets );
  161. if( m_referenceArrayId > -1 )
  162. GUI.enabled = false;
  163. base.DrawProperties();
  164. GUI.enabled = guiEnabledBuffer;
  165. }
  166. private void UpdateOutput()
  167. {
  168. if( m_texcoordSize == 3 )
  169. {
  170. m_outputPorts[ 0 ].ChangeType( WirePortDataType.FLOAT3, false );
  171. m_outputPorts[ 0 ].Name = "UVW";
  172. m_outputPorts[ 3 ].Visible = true;
  173. m_outputPorts[ 4 ].Visible = false;
  174. }
  175. else if( m_texcoordSize == 4 )
  176. {
  177. m_outputPorts[ 0 ].ChangeType( WirePortDataType.FLOAT4, false );
  178. m_outputPorts[ 0 ].Name = "UVWT";
  179. m_outputPorts[ 3 ].Visible = true;
  180. m_outputPorts[ 4 ].Visible = true;
  181. }
  182. else
  183. {
  184. m_outputPorts[ 0 ].ChangeType( WirePortDataType.FLOAT2, false );
  185. m_outputPorts[ 0 ].Name = "UV";
  186. m_outputPorts[ 3 ].Visible = false;
  187. m_outputPorts[ 4 ].Visible = false;
  188. }
  189. m_sizeIsDirty = true;
  190. }
  191. public override void OnNodeLogicUpdate( DrawInfo drawInfo )
  192. {
  193. base.OnNodeLogicUpdate( drawInfo );
  194. CheckReference();
  195. }
  196. //public override void Draw( DrawInfo drawInfo )
  197. //{
  198. // base.Draw( drawInfo );
  199. // //CheckReference();
  200. //}
  201. void CheckReference()
  202. {
  203. if( m_referenceArrayId > -1 )
  204. {
  205. ParentNode newNode = UIUtils.GetTexturePropertyNode( m_referenceArrayId );
  206. if( newNode == null || newNode.UniqueId != m_referenceNodeId )
  207. {
  208. m_referenceNode = null;
  209. int count = UIUtils.GetTexturePropertyNodeAmount();
  210. for( int i = 0; i < count; i++ )
  211. {
  212. ParentNode node = UIUtils.GetTexturePropertyNode( i );
  213. if( node.UniqueId == m_referenceNodeId )
  214. {
  215. m_referenceNode = node as TexturePropertyNode;
  216. m_referenceArrayId = i;
  217. break;
  218. }
  219. }
  220. }
  221. }
  222. if( m_referenceNode == null && m_referenceNodeId > -1 )
  223. {
  224. m_referenceNodeId = -1;
  225. m_referenceArrayId = -1;
  226. UpdateTitle();
  227. UpdatePorts();
  228. }
  229. }
  230. public override void ReadFromString( ref string[] nodeParams )
  231. {
  232. base.ReadFromString( ref nodeParams );
  233. m_textureCoordChannel = Convert.ToInt32( GetCurrentParam( ref nodeParams ) );
  234. if( UIUtils.CurrentShaderVersion() > 2402 )
  235. {
  236. if( UIUtils.CurrentShaderVersion() > 2404 )
  237. {
  238. m_referenceNodeId = Convert.ToInt32( GetCurrentParam( ref nodeParams ) );
  239. }
  240. else
  241. {
  242. m_referenceArrayId = Convert.ToInt32( GetCurrentParam( ref nodeParams ) );
  243. }
  244. }
  245. if( UIUtils.CurrentShaderVersion() > 5001 )
  246. {
  247. m_texcoordSize = Convert.ToInt32( GetCurrentParam( ref nodeParams ) );
  248. UpdateOutput();
  249. }
  250. }
  251. public override void RefreshExternalReferences()
  252. {
  253. base.RefreshExternalReferences();
  254. if( UIUtils.CurrentShaderVersion() > 2402 )
  255. {
  256. if( UIUtils.CurrentShaderVersion() > 2404 )
  257. {
  258. m_referenceNode = UIUtils.GetNode( m_referenceNodeId ) as TexturePropertyNode;
  259. if( m_referenceNodeId > -1 )
  260. m_referenceArrayId = UIUtils.GetTexturePropertyNodeRegisterId( m_referenceNodeId );
  261. }
  262. else
  263. {
  264. m_referenceNode = UIUtils.GetTexturePropertyNode( m_referenceArrayId );
  265. if( m_referenceNode != null )
  266. {
  267. m_referenceNodeId = m_referenceNode.UniqueId;
  268. }
  269. }
  270. UpdateTitle();
  271. UpdatePorts();
  272. }
  273. }
  274. public override void PropagateNodeData( NodeData nodeData, ref MasterNodeDataCollector dataCollector )
  275. {
  276. if( dataCollector != null && dataCollector.TesselationActive )
  277. {
  278. base.PropagateNodeData( nodeData, ref dataCollector );
  279. return;
  280. }
  281. if( dataCollector.IsTemplate )
  282. {
  283. dataCollector.TemplateDataCollectorInstance.SetUVUsage( m_textureCoordChannel, m_texcoordSize );
  284. }
  285. else if( m_textureCoordChannel > 3 )
  286. {
  287. dataCollector.AddCustomAppData( string.Format( TemplateHelperFunctions.TexUVFullSemantic, m_textureCoordChannel ) );
  288. }
  289. UIUtils.SetCategoryInBitArray( ref m_category, nodeData.Category );
  290. MasterNodePortCategory propagateCategory = ( nodeData.Category != MasterNodePortCategory.Vertex && nodeData.Category != MasterNodePortCategory.Tessellation ) ? MasterNodePortCategory.Vertex : nodeData.Category;
  291. nodeData.Category = propagateCategory;
  292. nodeData.GraphDepth += 1;
  293. if( nodeData.GraphDepth > m_graphDepth )
  294. {
  295. m_graphDepth = nodeData.GraphDepth;
  296. }
  297. int count = m_inputPorts.Count;
  298. for( int i = 0; i < count; i++ )
  299. {
  300. if( m_inputPorts[ i ].IsConnected )
  301. {
  302. //m_inputPorts[ i ].GetOutputNode().PropagateNodeCategory( category );
  303. m_inputPorts[ i ].GetOutputNode().PropagateNodeData( nodeData, ref dataCollector );
  304. }
  305. }
  306. }
  307. public override void WriteToString( ref string nodeInfo, ref string connectionsInfo )
  308. {
  309. base.WriteToString( ref nodeInfo, ref connectionsInfo );
  310. IOUtils.AddFieldValueToString( ref nodeInfo, m_textureCoordChannel );
  311. IOUtils.AddFieldValueToString( ref nodeInfo, ( ( m_referenceNode != null ) ? m_referenceNode.UniqueId : -1 ) );
  312. IOUtils.AddFieldValueToString( ref nodeInfo, m_texcoordSize );
  313. }
  314. string GetValidPropertyName()
  315. {
  316. string propertyName = string.Empty;
  317. if( m_inputReferenceNode != null )
  318. {
  319. propertyName = m_inputReferenceNode.PropertyName;
  320. }
  321. else if( m_referenceArrayId > -1 )
  322. {
  323. m_referenceNode = UIUtils.GetTexturePropertyNode( m_referenceArrayId );
  324. if( m_referenceNode != null )
  325. {
  326. propertyName = m_referenceNode.PropertyName;
  327. }
  328. }
  329. return propertyName;
  330. }
  331. public override string GenerateShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalVar )
  332. {
  333. if( dataCollector.PortCategory == MasterNodePortCategory.Tessellation )
  334. {
  335. UIUtils.ShowMessage( m_nodeAttribs.Name + " cannot be used on Master Node Tessellation port" );
  336. return "-1";
  337. }
  338. //bool isVertex = ( dataCollector.PortCategory == MasterNodePortCategory.Vertex || dataCollector.PortCategory == MasterNodePortCategory.Tessellation );
  339. string tiling = string.Empty;
  340. string offset = string.Empty;
  341. string portProperty = string.Empty;
  342. if( m_texPort.IsConnected )
  343. portProperty = m_texPort.GeneratePortInstructions( ref dataCollector );
  344. if( m_referenceArrayId > -1 )
  345. {
  346. TexturePropertyNode temp = UIUtils.GetTexturePropertyNode( m_referenceArrayId );
  347. if( temp != null )
  348. {
  349. portProperty = temp.BaseGenerateShaderForOutput( outputId, ref dataCollector, ignoreLocalVar );
  350. }
  351. }
  352. //TEMPLATES
  353. if( dataCollector.MasterNodeCategory == AvailableShaderTypes.Template )
  354. {
  355. if( m_outputPorts[ 0 ].IsLocalValue( dataCollector.PortCategory ) )
  356. return GetOutputVectorItem( 0, outputId, m_outputPorts[ 0 ].LocalValue( dataCollector.PortCategory ) );
  357. string uvName = string.Empty;
  358. if( dataCollector.TemplateDataCollectorInstance.HasUV( m_textureCoordChannel ) )
  359. {
  360. uvName = dataCollector.TemplateDataCollectorInstance.GetUVName( m_textureCoordChannel, m_outputPorts[ 0 ].DataType );
  361. }
  362. else
  363. {
  364. uvName = dataCollector.TemplateDataCollectorInstance.RegisterUV( m_textureCoordChannel, m_outputPorts[ 0 ].DataType );
  365. }
  366. string currPropertyName = GetValidPropertyName();
  367. if( !string.IsNullOrEmpty( portProperty ) && portProperty != "0.0" )
  368. {
  369. currPropertyName = portProperty;
  370. }
  371. if( !string.IsNullOrEmpty( currPropertyName ) )
  372. {
  373. string finalTexCoordName = "uv" + currPropertyName;
  374. string dummyPropertyTexcoords = currPropertyName + "_ST";
  375. dataCollector.AddToUniforms( UniqueId, "float4", dummyPropertyTexcoords );
  376. if( m_texcoordSize > 2 )
  377. {
  378. dataCollector.AddLocalVariable( UniqueId, m_currentPrecisionType, m_outputPorts[ 0 ].DataType, finalTexCoordName, uvName );
  379. dataCollector.AddLocalVariable( UniqueId, finalTexCoordName + ".xy", string.Format( Constants.TilingOffsetFormat, uvName + ".xy", dummyPropertyTexcoords + ".xy", dummyPropertyTexcoords + ".zw" ) + ";" );
  380. m_outputPorts[ 0 ].SetLocalValue( finalTexCoordName, dataCollector.PortCategory );
  381. }
  382. else
  383. {
  384. RegisterLocalVariable( 0, string.Format( Constants.TilingOffsetFormat, uvName, dummyPropertyTexcoords + ".xy", dummyPropertyTexcoords + ".zw" ), ref dataCollector, finalTexCoordName );
  385. }
  386. //RegisterLocalVariable( 0, string.Format( Constants.TilingOffsetFormat, uvName, dummyPropertyTexcoords+".xy", dummyPropertyTexcoords+".zw" ), ref dataCollector, finalTexCoordName );
  387. }
  388. else
  389. {
  390. string finalTexCoordName = "uv" + OutputId;
  391. tiling = m_tilingPort.GeneratePortInstructions( ref dataCollector );
  392. offset = m_offsetPort.GeneratePortInstructions( ref dataCollector );
  393. if( m_texcoordSize > 2 )
  394. {
  395. dataCollector.AddLocalVariable( UniqueId, m_currentPrecisionType, m_outputPorts[ 0 ].DataType, finalTexCoordName, uvName );
  396. dataCollector.AddLocalVariable( UniqueId, finalTexCoordName + ".xy", string.Format( Constants.TilingOffsetFormat, uvName + ".xy", tiling, offset ) + ";" );
  397. m_outputPorts[ 0 ].SetLocalValue( finalTexCoordName, dataCollector.PortCategory );
  398. }
  399. else
  400. {
  401. RegisterLocalVariable( 0, string.Format( Constants.TilingOffsetFormat, uvName, tiling, offset ), ref dataCollector, finalTexCoordName );
  402. }
  403. //RegisterLocalVariable( 0, string.Format( Constants.TilingOffsetFormat, uvName, tiling, offset ), ref dataCollector, finalTexCoordName );
  404. }
  405. return GetOutputVectorItem( 0, outputId, m_outputPorts[ 0 ].LocalValue( dataCollector.PortCategory ) );
  406. }
  407. //SURFACE
  408. string propertyName = GetValidPropertyName();
  409. if( !string.IsNullOrEmpty( portProperty ) && portProperty != "0.0" )
  410. {
  411. propertyName = portProperty;
  412. }
  413. if( m_outputPorts[ 0 ].IsLocalValue( dataCollector.PortCategory ) )
  414. return GetOutputVectorItem( 0, outputId, m_outputPorts[ 0 ].LocalValue( dataCollector.PortCategory ) );
  415. if( !m_tilingPort.IsConnected && m_tilingPort.Vector2InternalData == Vector2.one )
  416. tiling = null;
  417. else
  418. tiling = m_tilingPort.GeneratePortInstructions( ref dataCollector );
  419. if( !m_offsetPort.IsConnected && m_offsetPort.Vector2InternalData == Vector2.zero )
  420. offset = null;
  421. else
  422. offset = m_offsetPort.GeneratePortInstructions( ref dataCollector );
  423. if( !string.IsNullOrEmpty( propertyName ) /*m_referenceArrayId > -1*/ )
  424. {
  425. m_surfaceTexcoordName = GeneratorUtils.GenerateAutoUVs( ref dataCollector, UniqueId, m_textureCoordChannel, propertyName, m_outputPorts[ 0 ].DataType, tiling, offset, OutputId );
  426. }
  427. else
  428. {
  429. m_surfaceTexcoordName = GeneratorUtils.GenerateAutoUVs( ref dataCollector, UniqueId, m_textureCoordChannel, null, m_outputPorts[ 0 ].DataType, tiling, offset, OutputId );
  430. }
  431. m_outputPorts[ 0 ].SetLocalValue( m_surfaceTexcoordName, dataCollector.PortCategory );
  432. return GetOutputVectorItem( 0, outputId, m_outputPorts[ 0 ].LocalValue( dataCollector.PortCategory ) );
  433. }
  434. public override void ReadInputDataFromString( ref string[] nodeParams )
  435. {
  436. if( UIUtils.CurrentShaderVersion() > 7003 )
  437. {
  438. base.ReadInputDataFromString( ref nodeParams );
  439. }
  440. else
  441. {
  442. for( int i = 0; i < 2 && i < nodeParams.Length && m_currentReadParamIdx < nodeParams.Length; i++ )
  443. {
  444. if( UIUtils.CurrentShaderVersion() < 5003 )
  445. {
  446. int newId = VersionConvertInputPortId( i ) + 1;
  447. if( UIUtils.CurrentShaderVersion() > 23 )
  448. {
  449. m_inputPorts[ newId ].DataType = (WirePortDataType)Enum.Parse( typeof( WirePortDataType ), nodeParams[ m_currentReadParamIdx++ ] );
  450. }
  451. m_inputPorts[ newId ].InternalData = nodeParams[ m_currentReadParamIdx++ ];
  452. if( m_inputPorts[ newId ].IsEditable && UIUtils.CurrentShaderVersion() >= 3100 && m_currentReadParamIdx < nodeParams.Length )
  453. {
  454. m_inputPorts[ newId ].Name = nodeParams[ m_currentReadParamIdx++ ];
  455. }
  456. }
  457. else
  458. {
  459. int portId = Convert.ToInt32( nodeParams[ m_currentReadParamIdx++ ] );
  460. WirePortDataType DataType = (WirePortDataType)Enum.Parse( typeof( WirePortDataType ), nodeParams[ m_currentReadParamIdx++ ] );
  461. string InternalData = nodeParams[ m_currentReadParamIdx++ ];
  462. bool isEditable = Convert.ToBoolean( nodeParams[ m_currentReadParamIdx++ ] );
  463. string Name = string.Empty;
  464. if( isEditable && m_currentReadParamIdx < nodeParams.Length )
  465. {
  466. Name = nodeParams[ m_currentReadParamIdx++ ];
  467. }
  468. InputPort inputPort = GetInputPortByUniqueId( portId );
  469. if( inputPort != null )
  470. {
  471. inputPort.DataType = DataType;
  472. inputPort.InternalData = InternalData;
  473. if( !string.IsNullOrEmpty( Name ) )
  474. {
  475. inputPort.Name = Name;
  476. }
  477. }
  478. }
  479. }
  480. }
  481. }
  482. public override void Destroy()
  483. {
  484. base.Destroy();
  485. m_referenceNode = null;
  486. }
  487. }
  488. }