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.

116 lines
4.4 KiB

  1. // Amplify Shader Editor - Visual Shader Editing Tool
  2. // Copyright (c) Amplify Creations, Lda <info@amplify.pt>
  3. using UnityEngine;
  4. using System;
  5. namespace AmplifyShaderEditor
  6. {
  7. [Serializable]
  8. [NodeAttributes( "Lerp", "Math Operators", "Linear interpolation of two scalars or vectors based on a weight", null, KeyCode.L )]
  9. public sealed class LerpOp : ParentNode
  10. {
  11. private const string LertOpFormat = "lerp( {0} , {1} , {2})";
  12. [UnityEngine.SerializeField]
  13. private WirePortDataType m_mainDataType = WirePortDataType.FLOAT;
  14. protected override void CommonInit( int uniqueId )
  15. {
  16. base.CommonInit( uniqueId );
  17. m_textLabelWidth = 55;
  18. AddInputPort( WirePortDataType.FLOAT, false, "A" );
  19. AddInputPort( WirePortDataType.FLOAT, false, "B" );
  20. AddInputPort( WirePortDataType.FLOAT, false, "Alpha" );
  21. AddOutputPort( WirePortDataType.FLOAT,Constants.EmptyPortValue);
  22. m_useInternalPortData = true;
  23. m_previewShaderGUID = "34d9c4cdcf1fadb49af2de3f90bbc57d";
  24. }
  25. public override void OnInputPortConnected( int portId, int otherNodeId, int otherPortId, bool activateNode = true )
  26. {
  27. base.OnInputPortConnected( portId, otherNodeId, otherPortId, activateNode );
  28. UpdateConnection( portId );
  29. }
  30. public override void OnConnectedOutputNodeChanges( int inputPortId, int otherNodeId, int otherPortId, string name, WirePortDataType type )
  31. {
  32. base.OnConnectedOutputNodeChanges( inputPortId, otherNodeId, otherPortId, name, type );
  33. UpdateConnection( inputPortId );
  34. }
  35. public override void OnInputPortDisconnected( int portId )
  36. {
  37. base.OnInputPortDisconnected( portId );
  38. UpdateConnection( portId );
  39. }
  40. void UpdateConnection( int portId )
  41. {
  42. WirePortDataType type1 = WirePortDataType.FLOAT;
  43. if( m_inputPorts[ 0 ].IsConnected )
  44. type1 = m_inputPorts[ 0 ].GetOutputConnection( 0 ).DataType;
  45. WirePortDataType type2 = WirePortDataType.FLOAT;
  46. if( m_inputPorts[ 1 ].IsConnected )
  47. type2 = m_inputPorts[ 1 ].GetOutputConnection( 0 ).DataType;
  48. WirePortDataType typealpha = WirePortDataType.FLOAT;
  49. if( m_inputPorts[ 2 ].IsConnected )
  50. typealpha = m_inputPorts[ 2 ].GetOutputConnection( 0 ).DataType;
  51. m_mainDataType = UIUtils.GetPriority( type1 ) > UIUtils.GetPriority( type2 ) ? type1 : type2;
  52. if( !m_inputPorts[ 0 ].IsConnected && !m_inputPorts[ 1 ].IsConnected && m_inputPorts[ 2 ].IsConnected )
  53. m_mainDataType = m_inputPorts[ 2 ].GetOutputConnection( 0 ).DataType;
  54. m_inputPorts[ 0 ].ChangeType( m_mainDataType, false );
  55. m_inputPorts[ 1 ].ChangeType( m_mainDataType, false );
  56. if( m_inputPorts[ 2 ].IsConnected && ( typealpha == WirePortDataType.FLOAT || typealpha == WirePortDataType.INT ) )
  57. m_inputPorts[ 2 ].ChangeType( WirePortDataType.FLOAT, false );
  58. else
  59. m_inputPorts[ 2 ].ChangeType( m_mainDataType, false );
  60. m_outputPorts[ 0 ].ChangeType( m_mainDataType, false );
  61. }
  62. //void UpdateDisconnection( int portId )
  63. //{
  64. // int otherPortId = ( portId + 1 ) % 2;
  65. // if ( m_inputPorts[ otherPortId ].IsConnected )
  66. // {
  67. // m_mainDataType = m_inputPorts[ otherPortId ].DataType;
  68. // m_inputPorts[ portId ].ChangeType( m_mainDataType, false );
  69. // m_outputPorts[ 0 ].ChangeType( m_mainDataType, false );
  70. // }
  71. //}
  72. public override string GenerateShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalVar )
  73. {
  74. if ( m_outputPorts[ 0 ].IsLocalValue( dataCollector.PortCategory ) )
  75. return m_outputPorts[ 0 ].LocalValue( dataCollector.PortCategory );
  76. string aValue = m_inputPorts[ 0 ].GenerateShaderForOutput( ref dataCollector, m_mainDataType, ignoreLocalVar, true );
  77. string bValue = m_inputPorts[ 1 ].GenerateShaderForOutput( ref dataCollector, m_mainDataType, ignoreLocalVar, true );
  78. string interp = string.Empty;
  79. if( m_inputPorts[ 2 ].DataType == WirePortDataType.FLOAT )
  80. interp = m_inputPorts[ 2 ].GeneratePortInstructions( ref dataCollector );
  81. else
  82. interp = m_inputPorts[ 2 ].GenerateShaderForOutput( ref dataCollector, m_mainDataType, ignoreLocalVar,true );
  83. string result = string.Format( LertOpFormat, aValue,bValue,interp);
  84. RegisterLocalVariable( 0, result, ref dataCollector, "lerpResult"+OutputId );
  85. return m_outputPorts[ 0 ].LocalValue( dataCollector.PortCategory );
  86. }
  87. //public override void RefreshExternalReferences()
  88. //{
  89. // if ( m_inputPorts[ 2 ].DataType != WirePortDataType.FLOAT )
  90. // {
  91. // m_inputPorts[ 2 ].ChangeType( WirePortDataType.FLOAT, false );
  92. // }
  93. //}
  94. }
  95. }