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.

152 lines
4.7 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. namespace AmplifyShaderEditor
  7. {
  8. [Serializable]
  9. [NodeAttributes( "Scale", "Math Operators", "Scales input value by a float factor" )]
  10. public sealed class ScaleNode : ParentNode
  11. {
  12. private const string ScaleFactorStr = "Scale";
  13. [SerializeField]
  14. private float m_scaleFactor = 1;
  15. private int m_cachedPropertyId = -1;
  16. private const float LabelWidth = 8;
  17. protected override void CommonInit( int uniqueId )
  18. {
  19. base.CommonInit( uniqueId );
  20. AddInputPort( WirePortDataType.FLOAT, false, " " );
  21. AddOutputPort( WirePortDataType.FLOAT, " " );
  22. m_insideSize.Set( 50, 10 );
  23. m_autoWrapProperties = true;
  24. m_previewShaderGUID = "6d8ec9d9dab62c44aa2dcc0e3987760d";
  25. }
  26. public override void SetPreviewInputs()
  27. {
  28. base.SetPreviewInputs();
  29. if ( m_cachedPropertyId == -1 )
  30. m_cachedPropertyId = Shader.PropertyToID( "_ScaleFloat" );
  31. PreviewMaterial.SetFloat( m_cachedPropertyId, m_scaleFactor );
  32. }
  33. public override void DrawProperties()
  34. {
  35. base.DrawProperties();
  36. m_scaleFactor = EditorGUILayoutFloatField( ScaleFactorStr, m_scaleFactor );
  37. }
  38. public override void OnInputPortConnected( int portId, int otherNodeId, int otherPortId, bool activateNode = true )
  39. {
  40. base.OnInputPortConnected( portId, otherNodeId, otherPortId, activateNode );
  41. m_inputPorts[ 0 ].MatchPortToConnection();
  42. m_outputPorts[ 0 ].ChangeType( m_inputPorts[ 0 ].DataType, false );
  43. }
  44. public override void OnConnectedOutputNodeChanges( int outputPortId, int otherNodeId, int otherPortId, string name, WirePortDataType type )
  45. {
  46. base.OnConnectedOutputNodeChanges( outputPortId, otherNodeId, otherPortId, name, type );
  47. m_inputPorts[ 0 ].MatchPortToConnection();
  48. m_outputPorts[ 0 ].ChangeType( InputPorts[ 0 ].DataType, false );
  49. }
  50. public override void OnNodeLayout( DrawInfo drawInfo )
  51. {
  52. base.OnNodeLayout( drawInfo );
  53. m_propertyDrawPos.x = m_remainingBox.x + Constants.FLOAT_WIDTH_SPACING * drawInfo.InvertedZoom * 0.5f;
  54. m_propertyDrawPos.y = m_remainingBox.y + Constants.INPUT_PORT_DELTA_Y * drawInfo.InvertedZoom * 0.5f;
  55. m_propertyDrawPos.width = drawInfo.InvertedZoom * Constants.FLOAT_DRAW_WIDTH_FIELD_SIZE;
  56. m_propertyDrawPos.height = drawInfo.InvertedZoom * Constants.FLOAT_DRAW_HEIGHT_FIELD_SIZE;
  57. }
  58. public override void DrawGUIControls( DrawInfo drawInfo )
  59. {
  60. base.DrawGUIControls( drawInfo );
  61. if ( drawInfo.CurrentEventType != EventType.MouseDown )
  62. return;
  63. Rect hitBox = m_remainingBox;
  64. hitBox.xMin -= LabelWidth * drawInfo.InvertedZoom;
  65. bool insideBox = hitBox.Contains( drawInfo.MousePosition );
  66. if ( insideBox )
  67. {
  68. GUI.FocusControl( null );
  69. m_isEditingFields = true;
  70. }
  71. else if ( m_isEditingFields && !insideBox )
  72. {
  73. GUI.FocusControl( null );
  74. m_isEditingFields = false;
  75. }
  76. }
  77. private bool m_isEditingFields;
  78. private float m_previousValue;
  79. private string m_fieldText = "0";
  80. public override void Draw( DrawInfo drawInfo )
  81. {
  82. base.Draw( drawInfo );
  83. if ( !m_isVisible )
  84. return;
  85. if ( m_isEditingFields )
  86. {
  87. UIUtils.DrawFloat( this, ref m_propertyDrawPos, ref m_scaleFactor, LabelWidth );
  88. }
  89. else if ( drawInfo.CurrentEventType == EventType.Repaint )
  90. {
  91. Rect fakeField = m_propertyDrawPos;
  92. fakeField.xMin += LabelWidth * drawInfo.InvertedZoom;
  93. Rect fakeLabel = m_propertyDrawPos;
  94. fakeLabel.xMax = fakeField.xMin;
  95. EditorGUIUtility.AddCursorRect( fakeLabel, MouseCursor.SlideArrow );
  96. EditorGUIUtility.AddCursorRect( fakeField, MouseCursor.Text );
  97. if ( m_previousValue != m_scaleFactor )
  98. {
  99. m_previousValue = m_scaleFactor;
  100. m_fieldText = m_scaleFactor.ToString();
  101. }
  102. GUI.Label( fakeField, m_fieldText, UIUtils.MainSkin.textField );
  103. }
  104. }
  105. public override string GenerateShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar )
  106. {
  107. if ( m_outputPorts[ 0 ].IsLocalValue( dataCollector.PortCategory ) )
  108. return m_outputPorts[ 0 ].LocalValue( dataCollector.PortCategory );
  109. string portResult = m_inputPorts[ 0 ].GeneratePortInstructions( ref dataCollector );
  110. string result = "( " + portResult + " * " + m_scaleFactor + " )";
  111. return CreateOutputLocalVariable( 0, result, ref dataCollector );
  112. }
  113. public override void WriteToString( ref string nodeInfo, ref string connectionsInfo )
  114. {
  115. base.WriteToString( ref nodeInfo, ref connectionsInfo );
  116. IOUtils.AddFieldValueToString( ref nodeInfo, m_scaleFactor );
  117. }
  118. public override void ReadFromString( ref string[] nodeParams )
  119. {
  120. base.ReadFromString( ref nodeParams );
  121. m_scaleFactor = Convert.ToSingle( GetCurrentParam( ref nodeParams ) );
  122. }
  123. }
  124. }