PowerNode.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // Amplify Shader Editor - Visual Shader Editing Tool
  2. // Copyright (c) Amplify Creations, Lda <info@amplify.pt>
  3. using System;
  4. using UnityEngine;
  5. namespace AmplifyShaderEditor
  6. {
  7. [Serializable]
  8. [NodeAttributes( "Power", "Math Operators", "Base to the Exp-th power of scalars and vectors", null, KeyCode.E )]
  9. public sealed class PowerNode : ParentNode
  10. {
  11. public const string SafePowerStr = "abs( {0} )";//"max( {0} , 0.0001 )";
  12. public const string SafePowerLabel = "Safe Power";
  13. [SerializeField]
  14. private bool m_safePower = false;
  15. protected override void CommonInit( int uniqueId )
  16. {
  17. base.CommonInit( uniqueId );
  18. AddInputPort( WirePortDataType.FLOAT, false, "Base" );
  19. AddInputPort( WirePortDataType.FLOAT, false, "Exp" );
  20. m_inputPorts[ 1 ].FloatInternalData = 1;
  21. AddOutputPort( WirePortDataType.FLOAT, Constants.EmptyPortValue );
  22. m_useInternalPortData = true;
  23. m_autoWrapProperties = true;
  24. m_textLabelWidth = 85;
  25. m_previewShaderGUID = "758cc2f8b537b4e4b93d9833075d138c";
  26. }
  27. public override void OnInputPortConnected( int portId, int otherNodeId, int otherPortId, bool activateNode = true )
  28. {
  29. base.OnInputPortConnected( portId, otherNodeId, otherPortId, activateNode );
  30. UpdateConnections( portId );
  31. }
  32. public override void OnConnectedOutputNodeChanges( int inputPortId, int otherNodeId, int otherPortId, string name, WirePortDataType type )
  33. {
  34. base.OnConnectedOutputNodeChanges( inputPortId, otherNodeId, otherPortId, name, type );
  35. UpdateConnections( inputPortId );
  36. }
  37. void UpdateConnections( int inputPort )
  38. {
  39. m_inputPorts[ inputPort ].MatchPortToConnection();
  40. if ( inputPort == 0 )
  41. {
  42. m_outputPorts[ 0 ].ChangeType( m_inputPorts[ 0 ].DataType, false );
  43. }
  44. }
  45. public override void DrawProperties()
  46. {
  47. base.DrawProperties();
  48. m_safePower = EditorGUILayoutToggle( SafePowerLabel, m_safePower );
  49. }
  50. public override string GenerateShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar )
  51. {
  52. if ( m_outputPorts[ 0 ].IsLocalValue( dataCollector.PortCategory ) )
  53. return m_outputPorts[ 0 ].LocalValue( dataCollector.PortCategory );
  54. string x = m_inputPorts[ 0 ].GeneratePortInstructions( ref dataCollector );
  55. if( m_safePower )
  56. {
  57. string safePowerName = "saferPower" + OutputId;
  58. string safePowerValue = string.Format( SafePowerStr, x );
  59. dataCollector.AddLocalVariable( UniqueId, CurrentPrecisionType, m_inputPorts[ 0 ].DataType, safePowerName, safePowerValue );
  60. x = safePowerName;
  61. }
  62. string y = m_inputPorts[ 1 ].GenerateShaderForOutput( ref dataCollector, m_inputPorts[ 0 ].DataType, ignoreLocalvar, true );
  63. string result = "pow( " + x + " , " + y + " )";
  64. return CreateOutputLocalVariable( 0, result, ref dataCollector );
  65. }
  66. public override void ReadFromString( ref string[] nodeParams )
  67. {
  68. base.ReadFromString( ref nodeParams );
  69. if( UIUtils.CurrentShaderVersion() > 17502 )
  70. m_safePower = Convert.ToBoolean( GetCurrentParam( ref nodeParams ) );
  71. }
  72. public override void WriteToString( ref string nodeInfo, ref string connectionsInfo )
  73. {
  74. base.WriteToString( ref nodeInfo, ref connectionsInfo );
  75. IOUtils.AddFieldValueToString( ref nodeInfo, m_safePower );
  76. }
  77. }
  78. }