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.

92 lines
3.0 KiB

  1. // Amplify Shader Editor - Visual Shader Editing Tool
  2. // Copyright (c) Amplify Creations, Lda <info@amplify.pt>
  3. using System;
  4. namespace AmplifyShaderEditor
  5. {
  6. [Serializable]
  7. [NodeAttributes( "Refract", "Vector Operators", "Computes a refraction vector" )]
  8. public sealed class RefractOpVec : ParentNode
  9. {
  10. [UnityEngine.SerializeField]
  11. private WirePortDataType m_mainDataType = WirePortDataType.FLOAT;
  12. protected override void CommonInit( int uniqueId )
  13. {
  14. base.CommonInit( uniqueId );
  15. AddInputPort( WirePortDataType.FLOAT4, false , "Incident" );
  16. AddInputPort( WirePortDataType.FLOAT4, false , "Normal" );
  17. AddInputPort( WirePortDataType.FLOAT, false, "Eta" );
  18. AddOutputPort( WirePortDataType.FLOAT4, Constants.EmptyPortValue );
  19. m_textLabelWidth = 67;
  20. m_previewShaderGUID = "5ab44ca484bed8b4884b03b1c00fdc3d";
  21. }
  22. public override void OnInputPortConnected( int portId, int otherNodeId, int otherPortId, bool activateNode = true )
  23. {
  24. base.OnInputPortConnected( portId, otherNodeId, otherPortId, activateNode );
  25. UpdateConnection( portId );
  26. }
  27. public override void OnConnectedOutputNodeChanges( int inputPortId, int otherNodeId, int otherPortId, string name, WirePortDataType type )
  28. {
  29. base.OnConnectedOutputNodeChanges( inputPortId, otherNodeId, otherPortId, name, type );
  30. UpdateConnection( inputPortId );
  31. }
  32. public override void OnInputPortDisconnected( int portId )
  33. {
  34. base.OnInputPortDisconnected( portId );
  35. UpdateConnection( portId );
  36. }
  37. void UpdateConnection( int portId )
  38. {
  39. if( portId == 2 )
  40. return;
  41. bool hasConnection = false;
  42. WirePortDataType type1 = WirePortDataType.FLOAT;
  43. if( m_inputPorts[ 0 ].IsConnected )
  44. {
  45. type1 = m_inputPorts[ 0 ].GetOutputConnection( 0 ).DataType;
  46. hasConnection = true;
  47. }
  48. WirePortDataType type2 = WirePortDataType.FLOAT;
  49. if( m_inputPorts[ 1 ].IsConnected )
  50. {
  51. type2 = m_inputPorts[ 1 ].GetOutputConnection( 0 ).DataType;
  52. hasConnection = true;
  53. }
  54. if( hasConnection )
  55. {
  56. m_mainDataType = UIUtils.GetPriority( type1 ) > UIUtils.GetPriority( type2 ) ? type1 : type2;
  57. }
  58. else
  59. {
  60. m_mainDataType = WirePortDataType.FLOAT4;
  61. }
  62. m_inputPorts[ 0 ].ChangeType( m_mainDataType, false );
  63. m_inputPorts[ 1 ].ChangeType( m_mainDataType, false );
  64. m_outputPorts[ 0 ].ChangeType( m_mainDataType, false );
  65. }
  66. public override string GenerateShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar )
  67. {
  68. if( m_outputPorts[ 0 ].IsLocalValue( dataCollector.PortCategory ) )
  69. return m_outputPorts[ 0 ].LocalValue( dataCollector.PortCategory );
  70. string incident = m_inputPorts[ 0 ].GeneratePortInstructions( ref dataCollector );
  71. string normal = m_inputPorts[ 1 ].GeneratePortInstructions( ref dataCollector );
  72. string interp = m_inputPorts[ 2 ].GeneratePortInstructions( ref dataCollector );
  73. string result = "refract( " + incident + " , " + normal + " , " + interp + " )";
  74. return CreateOutputLocalVariable( 0, result, ref dataCollector );
  75. }
  76. }
  77. }