Vector4Node.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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( "Vector4", "Constants And Properties", "Vector4 property", null, KeyCode.Alpha4 )]
  10. public sealed class Vector4Node : PropertyNode
  11. {
  12. [SerializeField]
  13. private Vector4 m_defaultValue = Vector4.zero;
  14. [SerializeField]
  15. private Vector4 m_materialValue = Vector4.zero;
  16. private const float LabelWidth = 8;
  17. private int m_cachedPropertyId = -1;
  18. private bool m_isEditingFields;
  19. private Vector4 m_previousValue = Vector4.zero;
  20. private string[] m_fieldText = new string[] { "0", "0", "0", "0" };
  21. public Vector4Node() : base() { }
  22. public Vector4Node( 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, 40 );
  28. m_selectedLocation = PreviewLocation.BottomCenter;
  29. AddOutputVectorPorts( WirePortDataType.FLOAT4, "XYZW" );
  30. m_previewShaderGUID = "aac241d0e47a5a84fbd2edcd640788dc";
  31. m_availableAttribs.Add( new PropertyAttributes( "Remap Sliders", "[RemapSlidersFull]" ) );
  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 = EditorGUILayoutVector4Field( Constants.DefaultValueLabel, m_defaultValue );
  42. }
  43. public override void DrawMaterialProperties()
  44. {
  45. if ( m_materialMode )
  46. EditorGUI.BeginChangeCheck();
  47. m_materialValue = EditorGUILayoutVector4Field( 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 ], m_materialValue[ 2 ], m_materialValue[ 3 ] ) );
  58. else
  59. PreviewMaterial.SetVector( m_cachedPropertyId, new Vector4( m_defaultValue[ 0 ], m_defaultValue[ 1 ], m_defaultValue[ 2 ], m_defaultValue[ 3 ] ) );
  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 < 4; 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. //m_propertyNameIsDirty = true;
  118. }
  119. }
  120. else if ( drawInfo.CurrentEventType == EventType.Repaint && ContainerGraph.LodLevel <= ParentGraph.NodeLOD.LOD4 )
  121. {
  122. bool guiEnabled = GUI.enabled;
  123. GUI.enabled = m_currentParameterType != PropertyType.Global;
  124. for( int i = 0; i < 4; i++ )
  125. {
  126. m_propertyDrawPos.y = m_outputPorts[ i + 1 ].Position.y - 2 * drawInfo.InvertedZoom;
  127. Rect fakeField = m_propertyDrawPos;
  128. fakeField.xMin += LabelWidth * drawInfo.InvertedZoom;
  129. if( GUI.enabled )
  130. {
  131. Rect fakeLabel = m_propertyDrawPos;
  132. fakeLabel.xMax = fakeField.xMin;
  133. EditorGUIUtility.AddCursorRect( fakeLabel, MouseCursor.SlideArrow );
  134. EditorGUIUtility.AddCursorRect( fakeField, MouseCursor.Text );
  135. }
  136. if ( m_materialMode && m_currentParameterType != PropertyType.Constant )
  137. {
  138. if ( m_previousValue[ i ] != m_materialValue[ i ] )
  139. {
  140. m_previousValue[ i ] = m_materialValue[ i ];
  141. m_fieldText[ i ] = m_materialValue[ i ].ToString();
  142. }
  143. }
  144. else
  145. {
  146. if ( m_previousValue[ i ] != m_defaultValue[ i ] )
  147. {
  148. m_previousValue[ i ] = m_defaultValue[ i ];
  149. m_fieldText[ i ] = m_defaultValue[ i ].ToString();
  150. }
  151. }
  152. GUI.Label( fakeField, m_fieldText[ i ], UIUtils.MainSkin.textField );
  153. }
  154. GUI.enabled = guiEnabled;
  155. }
  156. }
  157. public override void ConfigureLocalVariable( ref MasterNodeDataCollector dataCollector )
  158. {
  159. Vector4 value = m_defaultValue;
  160. dataCollector.AddLocalVariable( UniqueId, CreateLocalVarDec( value.x + "," + value.y + "," + value.z + "," + value.w ) );
  161. m_outputPorts[ 0 ].SetLocalValue( m_propertyName, dataCollector.PortCategory );
  162. m_outputPorts[ 1 ].SetLocalValue( m_propertyName + ".x" , dataCollector.PortCategory );
  163. m_outputPorts[ 2 ].SetLocalValue( m_propertyName + ".y" , dataCollector.PortCategory );
  164. m_outputPorts[ 3 ].SetLocalValue( m_propertyName + ".z" , dataCollector.PortCategory );
  165. m_outputPorts[ 4 ].SetLocalValue( m_propertyName + ".w", dataCollector.PortCategory );
  166. }
  167. public override string GenerateShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar )
  168. {
  169. base.GenerateShaderForOutput( outputId, ref dataCollector, ignoreLocalvar );
  170. m_precisionString = UIUtils.PrecisionWirePortToCgType( CurrentPrecisionType, m_outputPorts[ 0 ].DataType );
  171. if ( m_currentParameterType != PropertyType.Constant )
  172. return GetOutputVectorItem( 0, outputId, PropertyData( dataCollector.PortCategory ) );
  173. if ( m_outputPorts[ outputId ].IsLocalValue( dataCollector.PortCategory ) )
  174. {
  175. return m_outputPorts[ outputId ].LocalValue( dataCollector.PortCategory );
  176. }
  177. if ( CheckLocalVariable( ref dataCollector ) )
  178. {
  179. return m_outputPorts[ outputId ].LocalValue( dataCollector.PortCategory );
  180. }
  181. Vector4 value = m_defaultValue;
  182. string result = string.Empty;
  183. switch ( outputId )
  184. {
  185. case 0:
  186. {
  187. result = m_precisionString+"(" + value.x + "," + value.y + "," + value.z + "," + value.w + ")";
  188. }
  189. break;
  190. case 1:
  191. {
  192. result = value.x.ToString();
  193. }
  194. break;
  195. case 2:
  196. {
  197. result = value.y.ToString();
  198. }
  199. break;
  200. case 3:
  201. {
  202. result = value.z.ToString();
  203. }
  204. break;
  205. case 4:
  206. {
  207. result = value.w.ToString();
  208. }
  209. break;
  210. }
  211. if ( result.Equals( string.Empty ) )
  212. {
  213. UIUtils.ShowMessage( UniqueId, "Vector4Node generating empty code", MessageSeverity.Warning );
  214. }
  215. return result;
  216. }
  217. public override string GetPropertyValue()
  218. {
  219. string x = UIUtils.PropertyFloatToString( m_defaultValue.x );
  220. string y = UIUtils.PropertyFloatToString( m_defaultValue.y );
  221. string z = UIUtils.PropertyFloatToString( m_defaultValue.z );
  222. string w = UIUtils.PropertyFloatToString( m_defaultValue.w );
  223. return PropertyAttributes + m_propertyName + "(\"" + m_propertyInspectorName + "\", Vector) = (" + x + "," + y + "," + z + "," + w + ")";
  224. }
  225. public override void UpdateMaterial( Material mat )
  226. {
  227. base.UpdateMaterial( mat );
  228. if ( UIUtils.IsProperty( m_currentParameterType ) && !InsideShaderFunction )
  229. {
  230. mat.SetVector( m_propertyName, m_materialValue );
  231. }
  232. }
  233. public override void SetMaterialMode( Material mat , bool fetchMaterialValues )
  234. {
  235. base.SetMaterialMode( mat , fetchMaterialValues );
  236. if ( fetchMaterialValues && m_materialMode && UIUtils.IsProperty( m_currentParameterType ) && mat.HasProperty( m_propertyName ) )
  237. {
  238. m_materialValue = mat.GetVector( m_propertyName );
  239. }
  240. }
  241. public override void ForceUpdateFromMaterial( Material material )
  242. {
  243. if( UIUtils.IsProperty( m_currentParameterType ) && material.HasProperty( m_propertyName ) )
  244. {
  245. m_materialValue = material.GetVector( m_propertyName );
  246. PreviewIsDirty = true;
  247. }
  248. }
  249. public override void ReadFromString( ref string[] nodeParams )
  250. {
  251. base.ReadFromString( ref nodeParams );
  252. m_defaultValue = IOUtils.StringToVector4( GetCurrentParam( ref nodeParams ) );
  253. if( UIUtils.CurrentShaderVersion() > 14101 )
  254. m_materialValue = IOUtils.StringToVector4( GetCurrentParam( ref nodeParams ) );
  255. }
  256. public override void WriteToString( ref string nodeInfo, ref string connectionsInfo )
  257. {
  258. base.WriteToString( ref nodeInfo, ref connectionsInfo );
  259. IOUtils.AddFieldValueToString( ref nodeInfo, IOUtils.Vector4ToString( m_defaultValue ) );
  260. IOUtils.AddFieldValueToString( ref nodeInfo, IOUtils.Vector4ToString( m_materialValue ) );
  261. }
  262. public override void SetGlobalValue() { Shader.SetGlobalVector( m_propertyName, m_defaultValue ); }
  263. public override void FetchGlobalValue() { m_materialValue = Shader.GetGlobalVector( m_propertyName ); }
  264. public override string GetPropertyValStr()
  265. {
  266. return ( m_materialMode && m_currentParameterType != PropertyType.Constant ) ? m_materialValue.x.ToString( Mathf.Abs( m_materialValue.x ) > 1000 ? Constants.PropertyBigVectorFormatLabel : Constants.PropertyVectorFormatLabel ) + IOUtils.VECTOR_SEPARATOR +
  267. m_materialValue.y.ToString( Mathf.Abs( m_materialValue.y ) > 1000 ? Constants.PropertyBigVectorFormatLabel : Constants.PropertyVectorFormatLabel ) + IOUtils.VECTOR_SEPARATOR +
  268. m_materialValue.z.ToString( Mathf.Abs( m_materialValue.z ) > 1000 ? Constants.PropertyBigVectorFormatLabel : Constants.PropertyVectorFormatLabel ) + IOUtils.VECTOR_SEPARATOR +
  269. m_materialValue.w.ToString( Mathf.Abs( m_materialValue.w ) > 1000 ? Constants.PropertyBigVectorFormatLabel : Constants.PropertyVectorFormatLabel ) :
  270. m_defaultValue.x.ToString( Mathf.Abs( m_defaultValue.x ) > 1000 ? Constants.PropertyBigVectorFormatLabel : Constants.PropertyVectorFormatLabel ) + IOUtils.VECTOR_SEPARATOR +
  271. m_defaultValue.y.ToString( Mathf.Abs( m_defaultValue.y ) > 1000 ? Constants.PropertyBigVectorFormatLabel : Constants.PropertyVectorFormatLabel ) + IOUtils.VECTOR_SEPARATOR +
  272. m_defaultValue.z.ToString( Mathf.Abs( m_defaultValue.z ) > 1000 ? Constants.PropertyBigVectorFormatLabel : Constants.PropertyVectorFormatLabel ) + IOUtils.VECTOR_SEPARATOR +
  273. m_defaultValue.w.ToString( Mathf.Abs( m_defaultValue.w ) > 1000 ? Constants.PropertyBigVectorFormatLabel : Constants.PropertyVectorFormatLabel );
  274. }
  275. public Vector4 Value
  276. {
  277. get { return m_defaultValue; }
  278. set { m_defaultValue = value; }
  279. }
  280. }
  281. }