WeightedBlendNode.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // Amplify Shader Editor - Visual Shader Editing Tool
  2. // Copyright (c) Amplify Creations, Lda <info@amplify.pt>
  3. using UnityEngine;
  4. using System;
  5. using UnityEditor;
  6. namespace AmplifyShaderEditor
  7. {
  8. [Serializable]
  9. [NodeAttributes( "Weighted Blend", "Miscellaneous", "Mix all channels through weighted average sum", null, KeyCode.None, true )]
  10. public sealed class WeightedBlendNode : WeightedAvgNode
  11. {
  12. protected override void CommonInit( int uniqueId )
  13. {
  14. base.CommonInit( uniqueId );
  15. m_inputData = new string[ 6 ];
  16. m_previewShaderGUID = "6076cbeaa41ebb14c85ff81b58df7d88";
  17. }
  18. public override string GenerateShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar )
  19. {
  20. if ( m_outputPorts[ 0 ].IsLocalValue( dataCollector.PortCategory ) )
  21. return m_outputPorts[ 0 ].LocalValue( dataCollector.PortCategory );
  22. GetInputData( ref dataCollector, ignoreLocalvar );
  23. string result = string.Empty;
  24. string avgSum = string.Empty;
  25. string localVarName = "weightedBlendVar" + OutputId;
  26. dataCollector.AddLocalVariable( UniqueId, CurrentPrecisionType, m_inputPorts[ 0 ].DataType, localVarName, m_inputData[ 0 ] );
  27. if ( m_activeCount < 2 )
  28. {
  29. return CreateOutputLocalVariable( 0, m_inputData[ 1 ], ref dataCollector );
  30. }
  31. else
  32. {
  33. for ( int i = 0; i < m_activeCount; i++ )
  34. {
  35. result += localVarName + Constants.VectorSuffixes[ i ] + "*" + m_inputData[ i + 1 ];
  36. avgSum += localVarName + Constants.VectorSuffixes[ i ];
  37. if ( i != ( m_activeCount - 1 ) )
  38. {
  39. result += " + ";
  40. avgSum += " + ";
  41. }
  42. }
  43. }
  44. result = UIUtils.AddBrackets( result ) + "/" + UIUtils.AddBrackets( avgSum );
  45. result = UIUtils.AddBrackets( result );
  46. RegisterLocalVariable( 0, result, ref dataCollector, "weightedAvg" + OutputId );
  47. return m_outputPorts[ 0 ].LocalValue( dataCollector.PortCategory );
  48. }
  49. }
  50. }