BlendNormalsNode.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // Amplify Shader Editor - Visual Shader Editing Tool
  2. // Copyright (c) Amplify Creations, Lda <info@amplify.pt>
  3. using System;
  4. using UnityEngine;
  5. using UnityEditor;
  6. namespace AmplifyShaderEditor
  7. {
  8. [Serializable]
  9. [NodeAttributes( "Blend Normals", "Textures", "Blend Normals" )]
  10. public class BlendNormalsNode : ParentNode
  11. {
  12. public readonly static string[] ModeListStr = { "Tangent Normals", "Reoriented Tangent Normals", "Reoriented World Normals" };
  13. public readonly static int[] ModeListInt = { 0, 1, 2 };
  14. [SerializeField]
  15. public int m_selectedMode = 0;
  16. protected override void CommonInit( int uniqueId )
  17. {
  18. base.CommonInit( uniqueId );
  19. AddInputPort( WirePortDataType.FLOAT3, false, "Normal A" );
  20. AddInputPort( WirePortDataType.FLOAT3, false, "Normal B" );
  21. AddInputPort( WirePortDataType.FLOAT3, false, "Vertex Normal" );
  22. m_inputPorts[ 2 ].Visible = false;
  23. AddOutputPort( WirePortDataType.FLOAT3, "XYZ" );
  24. m_useInternalPortData = true;
  25. m_previewShaderGUID = "bcdf750ff5f70444f98b8a3efa50dc6f";
  26. }
  27. public override void SetPreviewInputs()
  28. {
  29. base.SetPreviewInputs();
  30. m_previewMaterialPassId = m_selectedMode;
  31. }
  32. public override string GenerateShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar )
  33. {
  34. if( !( dataCollector.IsTemplate && dataCollector.IsSRP ) )
  35. dataCollector.AddToIncludes( UniqueId, Constants.UnityStandardUtilsLibFuncs );
  36. string _inputA = m_inputPorts[ 0 ].GeneratePortInstructions( ref dataCollector );
  37. string _inputB = m_inputPorts[ 1 ].GeneratePortInstructions( ref dataCollector );
  38. string result = "BlendNormals( " + _inputA + " , " + _inputB + " )";
  39. if( dataCollector.IsTemplate && dataCollector.IsSRP )
  40. {
  41. switch( m_selectedMode )
  42. {
  43. default:
  44. case 0:
  45. result = "BlendNormal( " + _inputA + " , " + _inputB + " )";
  46. break;
  47. case 1:
  48. result = "BlendNormalRNM( " + _inputA + " , " + _inputB + " )";
  49. break;
  50. case 2:
  51. string inputC = m_inputPorts[ 2 ].GeneratePortInstructions( ref dataCollector );
  52. result = "BlendNormalWorldspaceRNM( " + _inputA + " , " + _inputB + ", " + inputC + " )";
  53. break;
  54. }
  55. }
  56. return CreateOutputLocalVariable( 0, result, ref dataCollector );
  57. }
  58. public override void DrawProperties()
  59. {
  60. base.DrawProperties();
  61. if( ContainerGraph.IsSRP )
  62. {
  63. NodeUtils.DrawPropertyGroup( ref m_propertiesFoldout, Constants.ParameterLabelStr, () =>
  64. {
  65. EditorGUI.BeginChangeCheck();
  66. m_selectedMode = EditorGUILayoutIntPopup( "Mode", m_selectedMode, ModeListStr, ModeListInt );
  67. if( EditorGUI.EndChangeCheck() )
  68. {
  69. if( m_selectedMode == 2 )
  70. {
  71. m_inputPorts[ 2 ].Visible = true;
  72. }
  73. else
  74. {
  75. m_inputPorts[ 2 ].Visible = false;
  76. }
  77. m_sizeIsDirty = true;
  78. }
  79. } );
  80. }
  81. }
  82. public override void ReadFromString( ref string[] nodeParams )
  83. {
  84. base.ReadFromString( ref nodeParams );
  85. if( UIUtils.CurrentShaderVersion() > 14503 )
  86. m_selectedMode = Convert.ToInt32( GetCurrentParam( ref nodeParams ) );
  87. }
  88. public override void WriteToString( ref string nodeInfo, ref string connectionsInfo )
  89. {
  90. base.WriteToString( ref nodeInfo, ref connectionsInfo );
  91. IOUtils.AddFieldValueToString( ref nodeInfo, m_selectedMode );
  92. }
  93. }
  94. }