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.

68 lines
2.1 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. [NodeAttributes( "Unpack Scale Normal", "Textures", "Applies UnpackNormal/UnpackScaleNormal function" )]
  7. [Serializable]
  8. public class UnpackScaleNormalNode : ParentNode
  9. {
  10. protected override void CommonInit( int uniqueId )
  11. {
  12. base.CommonInit( uniqueId );
  13. AddInputPort( WirePortDataType.FLOAT4, false, "Value" );
  14. AddInputPort( WirePortDataType.FLOAT, false, "Scale" );
  15. m_inputPorts[ 1 ].FloatInternalData = 1;
  16. AddOutputVectorPorts( WirePortDataType.FLOAT3, "XYZ" );
  17. m_useInternalPortData = true;
  18. m_previewShaderGUID = "8b0ae05e25d280c45af81ded56f8012e";
  19. }
  20. public override string GenerateShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar )
  21. {
  22. string src = m_inputPorts[ 0 ].GeneratePortInstructions( ref dataCollector );
  23. bool isScaledNormal = false;
  24. if ( m_inputPorts[ 1 ].IsConnected )
  25. {
  26. isScaledNormal = true;
  27. }
  28. else
  29. {
  30. if ( m_inputPorts[ 1 ].FloatInternalData != 1 )
  31. {
  32. isScaledNormal = true;
  33. }
  34. }
  35. string normalMapUnpackMode = string.Empty;
  36. string scaleValue = isScaledNormal?m_inputPorts[ 1 ].GeneratePortInstructions( ref dataCollector ):"1.0";
  37. normalMapUnpackMode = string.Format( TemplateHelperFunctions.CreateUnpackNormalStr( dataCollector, isScaledNormal, scaleValue ), src);
  38. if( isScaledNormal && !( dataCollector.IsTemplate && dataCollector.IsSRP ) )
  39. {
  40. dataCollector.AddToIncludes( UniqueId, Constants.UnityStandardUtilsLibFuncs );
  41. }
  42. int outputUsage = 0;
  43. for ( int i = 0; i < m_outputPorts.Count; i++ )
  44. {
  45. if ( m_outputPorts[ i ].IsConnected )
  46. outputUsage += 1;
  47. }
  48. if ( outputUsage > 1 )
  49. {
  50. string varName = "localUnpackNormal" + UniqueId;
  51. dataCollector.AddLocalVariable( UniqueId, "float3 " + varName + " = " + normalMapUnpackMode + ";" );
  52. return GetOutputVectorItem( 0, outputId, varName );
  53. }
  54. else
  55. {
  56. return GetOutputVectorItem( 0, outputId, normalMapUnpackMode );
  57. }
  58. }
  59. }
  60. }