FunctionSwitchByPipeline.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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( "Switch by Pipeline", "Functions", "Executes branch according to current pipeline", NodeAvailabilityFlags = (int)NodeAvailability.ShaderFunction )]
  10. public sealed class FunctionSwitchByPipeline : ParentNode
  11. {
  12. protected override void CommonInit( int uniqueId )
  13. {
  14. base.CommonInit( uniqueId );
  15. AddInputPort( WirePortDataType.FLOAT, false, "BiRP Surface", -1, MasterNodePortCategory.Fragment, 0 );
  16. AddInputPort( WirePortDataType.FLOAT, false, "BiRP VertFrag", -1, MasterNodePortCategory.Fragment, 3 );
  17. AddInputPort( WirePortDataType.FLOAT, false, "URP", -1, MasterNodePortCategory.Fragment, 1 );
  18. AddInputPort( WirePortDataType.FLOAT, false, "HDRP", -1, MasterNodePortCategory.Fragment, 2 );
  19. AddOutputPort( WirePortDataType.FLOAT, Constants.EmptyPortValue );
  20. }
  21. public override void OnInputPortConnected( int portId, int otherNodeId, int otherPortId, bool activateNode = true )
  22. {
  23. base.OnInputPortConnected( portId, otherNodeId, otherPortId, activateNode );
  24. GetInputPortByUniqueId( portId ).MatchPortToConnection();
  25. UpdateOutputPort();
  26. }
  27. public override void OnConnectedOutputNodeChanges( int outputPortId, int otherNodeId, int otherPortId, string name, WirePortDataType type )
  28. {
  29. base.OnConnectedOutputNodeChanges( outputPortId, otherNodeId, otherPortId, name, type );
  30. GetInputPortByUniqueId( outputPortId ).MatchPortToConnection();
  31. UpdateOutputPort();
  32. }
  33. void UpdateOutputPort()
  34. {
  35. switch( UIUtils.CurrentWindow.OutsideGraph.CurrentSRPType )
  36. {
  37. case TemplateSRPType.BiRP:
  38. {
  39. InputPort port = UIUtils.CurrentWindow.OutsideGraph.IsStandardSurface ? GetInputPortByUniqueId( 0 ) : GetInputPortByUniqueId( 3 );
  40. m_outputPorts[ 0 ].ChangeType( port.DataType, false );
  41. }
  42. break;
  43. case TemplateSRPType.URP:
  44. {
  45. InputPort port = GetInputPortByUniqueId( 1 );
  46. m_outputPorts[ 0 ].ChangeType( port.DataType, false );
  47. }
  48. break;
  49. case TemplateSRPType.HDRP:
  50. {
  51. InputPort port = GetInputPortByUniqueId( 2 );
  52. m_outputPorts[ 0 ].ChangeType( port.DataType, false );
  53. }
  54. break;
  55. }
  56. }
  57. public override string GenerateShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar )
  58. {
  59. base.GenerateShaderForOutput( outputId, ref dataCollector, ignoreLocalvar );
  60. switch( dataCollector.CurrentSRPType )
  61. {
  62. case TemplateSRPType.BiRP:
  63. {
  64. InputPort port = UIUtils.CurrentWindow.OutsideGraph.IsStandardSurface ? GetInputPortByUniqueId( 0 ) : GetInputPortByUniqueId( 3 );
  65. return port.GeneratePortInstructions( ref dataCollector );
  66. }
  67. case TemplateSRPType.URP:
  68. {
  69. InputPort port = GetInputPortByUniqueId( 1 );
  70. return port.GeneratePortInstructions( ref dataCollector );
  71. }
  72. case TemplateSRPType.HDRP:
  73. {
  74. InputPort port = GetInputPortByUniqueId( 2 );
  75. return port.GeneratePortInstructions( ref dataCollector );
  76. }
  77. }
  78. return "0";
  79. }
  80. public override void RefreshExternalReferences()
  81. {
  82. base.RefreshExternalReferences();
  83. if( UIUtils.CurrentShaderVersion() < 16303 )
  84. {
  85. InputPort standardPort = GetInputPortByUniqueId( 0 );
  86. if( standardPort.IsConnected )
  87. {
  88. UIUtils.SetConnection( UniqueId, 3, standardPort.GetConnection().NodeId, standardPort.GetConnection().PortId );
  89. }
  90. }
  91. }
  92. }
  93. }