Vector2Node.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. namespace AmplifyShaderEditor
  7. {
  8. [System.Serializable]
  9. [NodeAttributes( "Vector2", "Constants And Properties", "Vector2 property", null, KeyCode.Alpha2 )]
  10. public sealed class Vector2Node : PropertyNode
  11. {
  12. [SerializeField]
  13. private Vector2 m_defaultValue = Vector2.zero;
  14. [SerializeField]
  15. private Vector2 m_materialValue = Vector2.zero;
  16. private const float LabelWidth = 8;
  17. private int m_cachedPropertyId = -1;
  18. private bool m_isEditingFields;
  19. private Vector2 m_previousValue = Vector2.zero;
  20. private string[] m_fieldText = new string[] { "0", "0" };
  21. public Vector2Node() : base() { }
  22. public Vector2Node( int uniqueId, float x, float y, float width, float height ) : base( uniqueId, x, y, width, height ) { }
  23. protected override void CommonInit( int uniqueId )
  24. {
  25. base.CommonInit( uniqueId );
  26. GlobalTypeWarningText = string.Format( GlobalTypeWarningText, "Vector" );
  27. m_insideSize.Set(50,20);
  28. m_selectedLocation = PreviewLocation.BottomCenter;
  29. AddOutputVectorPorts( WirePortDataType.FLOAT2, "XY" );
  30. m_availableAttribs.Add( new PropertyAttributes( "Remap Sliders", "[RemapSliders]" ) );
  31. m_previewShaderGUID = "88b4191eb06084d4da85d1dd2f984085";
  32. m_srpBatcherCompatible = true;
  33. m_showHybridInstancedUI = true;
  34. }
  35. public override void CopyDefaultsToMaterial()
  36. {
  37. m_materialValue = m_defaultValue;
  38. }
  39. public override void DrawSubProperties()
  40. {
  41. m_defaultValue = EditorGUILayoutVector2Field( Constants.DefaultValueLabel, m_defaultValue );
  42. }
  43. public override void DrawMaterialProperties()
  44. {
  45. if ( m_materialMode )
  46. EditorGUI.BeginChangeCheck();
  47. m_materialValue = EditorGUILayoutVector2Field( Constants.MaterialValueLabel, m_materialValue );
  48. if ( m_materialMode && EditorGUI.EndChangeCheck() )
  49. m_requireMaterialUpdate = true;
  50. }
  51. public override void SetPreviewInputs()
  52. {
  53. base.SetPreviewInputs();
  54. if ( m_cachedPropertyId == -1 )
  55. m_cachedPropertyId = Shader.PropertyToID( "_InputVector" );
  56. if ( m_materialMode && m_currentParameterType != PropertyType.Constant )
  57. PreviewMaterial.SetVector( m_cachedPropertyId, new Vector4( m_materialValue[ 0 ], m_materialValue[ 1 ], 0, 0 ) );
  58. else
  59. PreviewMaterial.SetVector( m_cachedPropertyId, new Vector4( m_defaultValue[ 0 ], m_defaultValue[ 1 ], 0, 0 ) );
  60. }
  61. public override void OnNodeLayout( DrawInfo drawInfo )
  62. {
  63. base.OnNodeLayout( drawInfo );
  64. m_propertyDrawPos = m_remainingBox;
  65. m_propertyDrawPos.x = m_remainingBox.x - LabelWidth * drawInfo.InvertedZoom;
  66. m_propertyDrawPos.width = drawInfo.InvertedZoom * Constants.FLOAT_DRAW_WIDTH_FIELD_SIZE;
  67. m_propertyDrawPos.height = drawInfo.InvertedZoom * Constants.FLOAT_DRAW_HEIGHT_FIELD_SIZE;
  68. }
  69. public override void DrawGUIControls( DrawInfo drawInfo )
  70. {
  71. base.DrawGUIControls( drawInfo );
  72. if ( drawInfo.CurrentEventType != EventType.MouseDown )
  73. return;
  74. Rect hitBox = m_remainingBox;
  75. hitBox.xMin -= LabelWidth * drawInfo.InvertedZoom;
  76. bool insideBox = hitBox.Contains( drawInfo.MousePosition );
  77. if ( insideBox )
  78. {
  79. GUI.FocusControl( null );
  80. m_isEditingFields = true;
  81. }
  82. else if ( m_isEditingFields && !insideBox )
  83. {
  84. GUI.FocusControl( null );
  85. m_isEditingFields = false;
  86. }
  87. }
  88. public override void Draw( DrawInfo drawInfo )
  89. {
  90. base.Draw( drawInfo );
  91. if ( !m_isVisible )
  92. return;
  93. if ( m_isEditingFields && m_currentParameterType != PropertyType.Global)
  94. {
  95. EditorGUI.BeginChangeCheck();
  96. for ( int i = 0; i < 2; i++ )
  97. {
  98. m_propertyDrawPos.y = m_outputPorts[ i + 1 ].Position.y - 2 * drawInfo.InvertedZoom;
  99. if ( m_materialMode && m_currentParameterType != PropertyType.Constant )
  100. {
  101. float val = m_materialValue[ i ];
  102. UIUtils.DrawFloat( this, ref m_propertyDrawPos, ref val, LabelWidth * drawInfo.InvertedZoom );
  103. m_materialValue[ i ] = val;
  104. }
  105. else
  106. {
  107. float val = m_defaultValue[ i ];
  108. UIUtils.DrawFloat( this, ref m_propertyDrawPos, ref val, LabelWidth * drawInfo.InvertedZoom );
  109. m_defaultValue[ i ] = val;
  110. }
  111. }
  112. if ( EditorGUI.EndChangeCheck() )
  113. {
  114. PreviewIsDirty = true;
  115. m_requireMaterialUpdate = m_materialMode;
  116. BeginDelayedDirtyProperty();
  117. }
  118. }
  119. else if ( drawInfo.CurrentEventType == EventType.Repaint && ContainerGraph.LodLevel <= ParentGraph.NodeLOD.LOD4 )
  120. {
  121. bool guiEnabled = GUI.enabled;
  122. GUI.enabled = m_currentParameterType != PropertyType.Global;
  123. for ( int i = 0; i < 2; i++ )
  124. {
  125. m_propertyDrawPos.y = m_outputPorts[ i + 1 ].Position.y - 2 * drawInfo.InvertedZoom;
  126. Rect fakeField = m_propertyDrawPos;
  127. fakeField.xMin += LabelWidth * drawInfo.InvertedZoom;
  128. if( GUI.enabled )
  129. {
  130. Rect fakeLabel = m_propertyDrawPos;
  131. fakeLabel.xMax = fakeField.xMin;
  132. EditorGUIUtility.AddCursorRect( fakeLabel, MouseCursor.SlideArrow );
  133. EditorGUIUtility.AddCursorRect( fakeField, MouseCursor.Text );
  134. }
  135. if ( m_materialMode && m_currentParameterType != PropertyType.Constant )
  136. {
  137. if ( m_previousValue[ i ] != m_materialValue[ i ] )
  138. {
  139. m_previousValue[ i ] = m_materialValue[ i ];
  140. m_fieldText[ i ] = m_materialValue[ i ].ToString();
  141. }
  142. }
  143. else
  144. {
  145. if ( m_previousValue[ i ] != m_defaultValue[ i ] )
  146. {
  147. m_previousValue[ i ] = m_defaultValue[ i ];
  148. m_fieldText[ i ] = m_defaultValue[ i ].ToString();
  149. }
  150. }
  151. GUI.Label( fakeField, m_fieldText[ i ], UIUtils.MainSkin.textField );
  152. }
  153. GUI.enabled = guiEnabled;
  154. }
  155. }
  156. public override void ConfigureLocalVariable( ref MasterNodeDataCollector dataCollector )
  157. {
  158. Vector2 value = m_defaultValue;
  159. dataCollector.AddLocalVariable( UniqueId, CreateLocalVarDec( value.x + "," + value.y ) );
  160. m_outputPorts[ 0 ].SetLocalValue( m_propertyName, dataCollector.PortCategory );
  161. m_outputPorts[ 1 ].SetLocalValue( m_propertyName + ".x" , dataCollector.PortCategory);
  162. m_outputPorts[ 2 ].SetLocalValue( m_propertyName + ".y", dataCollector.PortCategory );
  163. }
  164. public override string GenerateShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar )
  165. {
  166. base.GenerateShaderForOutput( outputId,ref dataCollector, ignoreLocalvar );
  167. m_precisionString = UIUtils.PrecisionWirePortToCgType( CurrentPrecisionType, m_outputPorts[ 0 ].DataType );
  168. if ( m_currentParameterType != PropertyType.Constant )
  169. return GetOutputVectorItem( 0, outputId, PropertyData( dataCollector.PortCategory ) );
  170. if ( m_outputPorts[ outputId ].IsLocalValue( dataCollector.PortCategory ) )
  171. {
  172. return m_outputPorts[ outputId ].LocalValue( dataCollector.PortCategory );
  173. }
  174. if ( CheckLocalVariable( ref dataCollector ) )
  175. {
  176. return m_outputPorts[ outputId ].LocalValue( dataCollector.PortCategory );
  177. }
  178. Vector2 value = m_defaultValue;
  179. string result = string.Empty;
  180. switch ( outputId )
  181. {
  182. case 0:
  183. {
  184. result = m_precisionString+"( " + value.x + "," + value.y + " )";
  185. }
  186. break;
  187. case 1:
  188. {
  189. result = value.x.ToString();
  190. }
  191. break;
  192. case 2:
  193. {
  194. result = value.y.ToString();
  195. }
  196. break;
  197. }
  198. if ( result.Equals( string.Empty ) )
  199. {
  200. UIUtils.ShowMessage( UniqueId, "Vector2Node generating empty code", MessageSeverity.Warning );
  201. }
  202. return result;
  203. }
  204. public override string GetPropertyValue()
  205. {
  206. string x = UIUtils.PropertyFloatToString( m_defaultValue.x );
  207. string y = UIUtils.PropertyFloatToString( m_defaultValue.y );
  208. return PropertyAttributes + m_propertyName + "(\"" + m_propertyInspectorName + "\", Vector) = (" + x + "," + y + ",0,0)";
  209. }
  210. public override void UpdateMaterial( Material mat )
  211. {
  212. base.UpdateMaterial( mat );
  213. if ( UIUtils.IsProperty( m_currentParameterType ) && !InsideShaderFunction )
  214. {
  215. mat.SetVector( m_propertyName, m_materialValue );
  216. }
  217. }
  218. public override void SetMaterialMode( Material mat , bool fetchMaterialValues )
  219. {
  220. base.SetMaterialMode( mat , fetchMaterialValues );
  221. if ( fetchMaterialValues && m_materialMode && UIUtils.IsProperty( m_currentParameterType ) && mat.HasProperty( m_propertyName ) )
  222. {
  223. m_materialValue = mat.GetVector( m_propertyName );
  224. }
  225. }
  226. public override void ForceUpdateFromMaterial( Material material )
  227. {
  228. if( UIUtils.IsProperty( m_currentParameterType ) && material.HasProperty( m_propertyName ) )
  229. {
  230. m_materialValue = material.GetVector( m_propertyName );
  231. PreviewIsDirty = true;
  232. }
  233. }
  234. public override void ReadFromString( ref string[] nodeParams )
  235. {
  236. base.ReadFromString( ref nodeParams );
  237. m_defaultValue = IOUtils.StringToVector2( GetCurrentParam( ref nodeParams ) );
  238. if( UIUtils.CurrentShaderVersion() > 14101 )
  239. m_materialValue = IOUtils.StringToVector2( GetCurrentParam( ref nodeParams ) );
  240. }
  241. public override void SetGlobalValue() { Shader.SetGlobalVector( m_propertyName, m_defaultValue ); }
  242. public override void FetchGlobalValue() { m_materialValue = Shader.GetGlobalVector( m_propertyName ); }
  243. public override void WriteToString( ref string nodeInfo, ref string connectionsInfo )
  244. {
  245. base.WriteToString( ref nodeInfo, ref connectionsInfo );
  246. IOUtils.AddFieldValueToString( ref nodeInfo, IOUtils.Vector2ToString( m_defaultValue ) );
  247. IOUtils.AddFieldValueToString( ref nodeInfo, IOUtils.Vector2ToString( m_materialValue ) );
  248. }
  249. public override string GetPropertyValStr()
  250. {
  251. return ( m_materialMode && m_currentParameterType != PropertyType.Constant ) ? m_materialValue.x.ToString( Mathf.Abs( m_materialValue.x ) > 1000 ? Constants.PropertyBigVectorFormatLabel : Constants.PropertyVectorFormatLabel ) + IOUtils.VECTOR_SEPARATOR +
  252. m_materialValue.y.ToString( Mathf.Abs( m_materialValue.y ) > 1000 ? Constants.PropertyBigVectorFormatLabel : Constants.PropertyVectorFormatLabel ) :
  253. m_defaultValue.x.ToString( Mathf.Abs( m_defaultValue.x ) > 1000 ? Constants.PropertyBigVectorFormatLabel : Constants.PropertyVectorFormatLabel ) + IOUtils.VECTOR_SEPARATOR +
  254. m_defaultValue.y.ToString( Mathf.Abs( m_defaultValue.y ) > 1000 ? Constants.PropertyBigVectorFormatLabel : Constants.PropertyVectorFormatLabel );
  255. }
  256. public Vector2 Value
  257. {
  258. get { return m_defaultValue; }
  259. set { m_defaultValue = value; }
  260. }
  261. }
  262. }