Vector3Node.cs 11 KB

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