IntNode.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. [Serializable]
  9. [NodeAttributes( "Int", "Constants And Properties", "Int property", null, KeyCode.Alpha0 )]
  10. public sealed class IntNode : PropertyNode
  11. {
  12. [SerializeField]
  13. private int m_defaultValue;
  14. [SerializeField]
  15. private int m_materialValue;
  16. [SerializeField]
  17. private bool m_setAsUINT = false;
  18. private const float LabelWidth = 8;
  19. private int m_cachedPropertyId = -1;
  20. private bool m_isEditingFields;
  21. private int m_previousValue;
  22. private string m_fieldText = "0";
  23. public IntNode() : base() { }
  24. public IntNode( int uniqueId, float x, float y, float width, float height ) : base( uniqueId, x, y, width, height ) { }
  25. protected override void CommonInit( int uniqueId )
  26. {
  27. base.CommonInit( uniqueId );
  28. GlobalTypeWarningText = string.Format( GlobalTypeWarningText, "Int" );
  29. AddOutputPort( WirePortDataType.INT, Constants.EmptyPortValue );
  30. m_insideSize.Set( 50, 10 );
  31. m_selectedLocation = PreviewLocation.BottomCenter;
  32. m_drawPrecisionUI = false;
  33. m_showHybridInstancedUI = true;
  34. m_availableAttribs.Add( new PropertyAttributes( "Enum", "[Enum]" ) );
  35. m_previewShaderGUID = "0f64d695b6ffacc469f2dd31432a232a";
  36. m_srpBatcherCompatible = true;
  37. }
  38. protected override void OnUniqueIDAssigned()
  39. {
  40. base.OnUniqueIDAssigned();
  41. UIUtils.RegisterFloatIntNode( this );
  42. }
  43. public override void Destroy()
  44. {
  45. base.Destroy();
  46. UIUtils.UnregisterFloatIntNode( this );
  47. }
  48. public override void OnDirtyProperty()
  49. {
  50. UIUtils.UpdateFloatIntDataNode( UniqueId, PropertyInspectorName );
  51. }
  52. public override void RefreshExternalReferences()
  53. {
  54. base.RefreshExternalReferences();
  55. OnPropertyNameChanged();
  56. OnDirtyProperty();
  57. }
  58. public override void SetPreviewInputs()
  59. {
  60. base.SetPreviewInputs();
  61. if( m_cachedPropertyId == -1 )
  62. m_cachedPropertyId = Shader.PropertyToID( "_InputInt" );
  63. if( m_materialMode && m_currentParameterType != PropertyType.Constant )
  64. PreviewMaterial.SetInt( m_cachedPropertyId, m_materialValue );
  65. else
  66. PreviewMaterial.SetInt( m_cachedPropertyId, m_defaultValue );
  67. }
  68. public override void CopyDefaultsToMaterial()
  69. {
  70. m_materialValue = m_defaultValue;
  71. DrawSetAsUINT();
  72. }
  73. public override void DrawSubProperties()
  74. {
  75. m_defaultValue = EditorGUILayoutIntField( Constants.DefaultValueLabel, m_defaultValue );
  76. DrawSetAsUINT();
  77. }
  78. private void DrawSetAsUINT()
  79. {
  80. EditorGUI.BeginChangeCheck();
  81. m_setAsUINT = EditorGUILayoutToggle( "Set as UINT", m_setAsUINT );
  82. if( EditorGUI.EndChangeCheck() )
  83. {
  84. WirePortDataType portType = m_setAsUINT ? WirePortDataType.UINT : WirePortDataType.INT;
  85. m_outputPorts[ 0 ].ChangeType( portType, false );
  86. }
  87. }
  88. public override void DrawMaterialProperties()
  89. {
  90. if( m_materialMode )
  91. EditorGUI.BeginChangeCheck();
  92. m_materialValue = EditorGUILayoutIntField( Constants.MaterialValueLabel, m_materialValue );
  93. if( m_materialMode && EditorGUI.EndChangeCheck() )
  94. {
  95. m_requireMaterialUpdate = true;
  96. }
  97. }
  98. public override void OnNodeLayout( DrawInfo drawInfo )
  99. {
  100. base.OnNodeLayout( drawInfo );
  101. m_propertyDrawPos = m_remainingBox;
  102. m_propertyDrawPos.x = m_remainingBox.x - LabelWidth * drawInfo.InvertedZoom;
  103. m_propertyDrawPos.width = drawInfo.InvertedZoom * Constants.FLOAT_DRAW_WIDTH_FIELD_SIZE;
  104. m_propertyDrawPos.height = drawInfo.InvertedZoom * Constants.FLOAT_DRAW_HEIGHT_FIELD_SIZE;
  105. }
  106. public override void DrawGUIControls( DrawInfo drawInfo )
  107. {
  108. base.DrawGUIControls( drawInfo );
  109. if( drawInfo.CurrentEventType != EventType.MouseDown )
  110. return;
  111. Rect hitBox = m_remainingBox;
  112. hitBox.xMin -= LabelWidth * drawInfo.InvertedZoom;
  113. bool insideBox = hitBox.Contains( drawInfo.MousePosition );
  114. if( insideBox )
  115. {
  116. GUI.FocusControl( null );
  117. m_isEditingFields = true;
  118. }
  119. else if( m_isEditingFields && !insideBox )
  120. {
  121. GUI.FocusControl( null );
  122. m_isEditingFields = false;
  123. }
  124. }
  125. public override void Draw( DrawInfo drawInfo )
  126. {
  127. base.Draw( drawInfo );
  128. if( !m_isVisible )
  129. return;
  130. if( m_isEditingFields && m_currentParameterType != PropertyType.Global )
  131. {
  132. float labelWidth = EditorGUIUtility.labelWidth;
  133. EditorGUIUtility.labelWidth = LabelWidth * drawInfo.InvertedZoom;
  134. if( m_materialMode && m_currentParameterType != PropertyType.Constant )
  135. {
  136. EditorGUI.BeginChangeCheck();
  137. m_materialValue = EditorGUIIntField( m_propertyDrawPos, " ", m_materialValue, UIUtils.MainSkin.textField );
  138. if( EditorGUI.EndChangeCheck() )
  139. {
  140. PreviewIsDirty = true;
  141. m_requireMaterialUpdate = true;
  142. if( m_currentParameterType != PropertyType.Constant )
  143. BeginDelayedDirtyProperty();
  144. }
  145. }
  146. else
  147. {
  148. EditorGUI.BeginChangeCheck();
  149. m_defaultValue = EditorGUIIntField( m_propertyDrawPos, " ", m_defaultValue, UIUtils.MainSkin.textField );
  150. if( EditorGUI.EndChangeCheck() )
  151. {
  152. PreviewIsDirty = true;
  153. BeginDelayedDirtyProperty();
  154. }
  155. }
  156. EditorGUIUtility.labelWidth = labelWidth;
  157. }
  158. else if( drawInfo.CurrentEventType == EventType.Repaint )
  159. {
  160. bool guiEnabled = GUI.enabled;
  161. GUI.enabled = m_currentParameterType != PropertyType.Global;
  162. Rect fakeField = m_propertyDrawPos;
  163. fakeField.xMin += LabelWidth * drawInfo.InvertedZoom;
  164. if( GUI.enabled )
  165. {
  166. Rect fakeLabel = m_propertyDrawPos;
  167. fakeLabel.xMax = fakeField.xMin;
  168. EditorGUIUtility.AddCursorRect( fakeLabel, MouseCursor.SlideArrow );
  169. EditorGUIUtility.AddCursorRect( fakeField, MouseCursor.Text );
  170. }
  171. bool currMode = m_materialMode && m_currentParameterType != PropertyType.Constant;
  172. int value = currMode ? m_materialValue : m_defaultValue;
  173. if( m_previousValue != value )
  174. {
  175. m_previousValue = value;
  176. m_fieldText = value.ToString();
  177. }
  178. GUI.Label( fakeField, m_fieldText, UIUtils.MainSkin.textField );
  179. GUI.enabled = guiEnabled;
  180. }
  181. }
  182. public override string GenerateShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar )
  183. {
  184. base.GenerateShaderForOutput( outputId, ref dataCollector, ignoreLocalvar );
  185. if( m_currentParameterType != PropertyType.Constant )
  186. return PropertyData( dataCollector.PortCategory );
  187. return m_defaultValue.ToString();
  188. }
  189. public override string GetPropertyValue()
  190. {
  191. return PropertyAttributes + m_propertyName + "(\"" + m_propertyInspectorName + "\", Int) = " + m_defaultValue;
  192. }
  193. public override void UpdateMaterial( Material mat )
  194. {
  195. base.UpdateMaterial( mat );
  196. if( UIUtils.IsProperty( m_currentParameterType ) && !InsideShaderFunction )
  197. {
  198. mat.SetInt( m_propertyName, m_materialValue );
  199. }
  200. }
  201. public override void SetMaterialMode( Material mat, bool fetchMaterialValues )
  202. {
  203. base.SetMaterialMode( mat, fetchMaterialValues );
  204. if( fetchMaterialValues && m_materialMode && UIUtils.IsProperty( m_currentParameterType ) && mat.HasProperty( m_propertyName ) )
  205. {
  206. m_materialValue = mat.GetInt( m_propertyName );
  207. }
  208. }
  209. public override void ForceUpdateFromMaterial( Material material )
  210. {
  211. if( UIUtils.IsProperty( m_currentParameterType ) && material.HasProperty( m_propertyName ) )
  212. {
  213. m_materialValue = material.GetInt( m_propertyName );
  214. PreviewIsDirty = true;
  215. }
  216. }
  217. public override void ReadFromString( ref string[] nodeParams )
  218. {
  219. base.ReadFromString( ref nodeParams );
  220. m_defaultValue = Convert.ToInt32( GetCurrentParam( ref nodeParams ) );
  221. if( UIUtils.CurrentShaderVersion() > 14101 )
  222. m_materialValue = Convert.ToInt32( GetCurrentParam( ref nodeParams ) );
  223. if( UIUtils.CurrentShaderVersion() > 18500 )
  224. m_setAsUINT = Convert.ToBoolean( GetCurrentParam( ref nodeParams ) );
  225. }
  226. public override void WriteToString( ref string nodeInfo, ref string connectionsInfo )
  227. {
  228. base.WriteToString( ref nodeInfo, ref connectionsInfo );
  229. IOUtils.AddFieldValueToString( ref nodeInfo, m_defaultValue );
  230. IOUtils.AddFieldValueToString( ref nodeInfo, m_materialValue );
  231. IOUtils.AddFieldValueToString( ref nodeInfo, m_setAsUINT );
  232. }
  233. public override string GetPropertyValStr()
  234. {
  235. return ( m_materialMode && m_currentParameterType != PropertyType.Constant ) ?
  236. m_materialValue.ToString( Mathf.Abs( m_materialValue ) > 1000 ? Constants.PropertyBigIntFormatLabel : Constants.PropertyIntFormatLabel ) :
  237. m_defaultValue.ToString( Mathf.Abs( m_defaultValue ) > 1000 ? Constants.PropertyBigIntFormatLabel : Constants.PropertyIntFormatLabel );
  238. }
  239. public override void SetGlobalValue() { Shader.SetGlobalInt( m_propertyName, m_defaultValue ); }
  240. public override void FetchGlobalValue() { m_materialValue = Shader.GetGlobalInt( m_propertyName ); }
  241. public int Value
  242. {
  243. get { return m_defaultValue; }
  244. set { m_defaultValue = value; }
  245. }
  246. public void SetMaterialValueFromInline( int val )
  247. {
  248. m_materialValue = val;
  249. m_requireMaterialUpdate = true;
  250. }
  251. }
  252. }