UnpackScaleNormalNode.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Amplify Shader Editor - Visual Shader Editing Tool
  2. // Copyright (c) Amplify Creations, Lda <info@amplify.pt>
  3. using System;
  4. using UnityEngine;
  5. namespace AmplifyShaderEditor
  6. {
  7. public enum UnpackInputMode
  8. {
  9. Tangent,
  10. Object
  11. }
  12. [NodeAttributes( "Unpack Scale Normal", "Textures", "Applies UnpackNormal/UnpackScaleNormal function" )]
  13. [Serializable]
  14. public class UnpackScaleNormalNode : ParentNode
  15. {
  16. [SerializeField]
  17. private UnpackInputMode m_inputMode = UnpackInputMode.Tangent;
  18. protected override void CommonInit( int uniqueId )
  19. {
  20. base.CommonInit( uniqueId );
  21. AddInputPort( WirePortDataType.FLOAT4, false, "Value" );
  22. AddInputPort( WirePortDataType.FLOAT, false, "Scale" );
  23. m_inputPorts[ 1 ].FloatInternalData = 1;
  24. AddOutputVectorPorts( WirePortDataType.FLOAT3, "XYZ" );
  25. m_useInternalPortData = true;
  26. m_autoWrapProperties = true;
  27. m_previewShaderGUID = "8b0ae05e25d280c45af81ded56f8012e";
  28. }
  29. public override void DrawProperties()
  30. {
  31. base.DrawProperties();
  32. m_inputMode = (UnpackInputMode)EditorGUILayoutEnumPopup( "Type" , m_inputMode );
  33. }
  34. public override string GenerateShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar )
  35. {
  36. string src = m_inputPorts[ 0 ].GeneratePortInstructions( ref dataCollector );
  37. bool isScaledNormal = false;
  38. if ( m_inputPorts[ 1 ].IsConnected )
  39. {
  40. isScaledNormal = true;
  41. }
  42. else
  43. {
  44. if ( m_inputPorts[ 1 ].FloatInternalData != 1 )
  45. {
  46. isScaledNormal = true;
  47. }
  48. }
  49. string normalMapUnpackMode = string.Empty;
  50. string scaleValue = isScaledNormal?m_inputPorts[ 1 ].GeneratePortInstructions( ref dataCollector ):"1.0";
  51. normalMapUnpackMode = GeneratorUtils.GenerateUnpackNormalStr( ref dataCollector, CurrentPrecisionType, UniqueId, OutputId, src, isScaledNormal, scaleValue , m_inputMode );
  52. if( isScaledNormal && !( dataCollector.IsTemplate && dataCollector.IsSRP ) )
  53. {
  54. dataCollector.AddToIncludes( UniqueId, Constants.UnityStandardUtilsLibFuncs );
  55. }
  56. int outputUsage = 0;
  57. for ( int i = 0; i < m_outputPorts.Count; i++ )
  58. {
  59. if ( m_outputPorts[ i ].IsConnected )
  60. outputUsage += 1;
  61. }
  62. if ( outputUsage > 1 && !dataCollector.IsSRP )
  63. {
  64. string varName = "localUnpackNormal" + OutputId;
  65. dataCollector.AddLocalVariable( UniqueId, "float3 " + varName + " = " + normalMapUnpackMode + ";" );
  66. return GetOutputVectorItem( 0, outputId, varName );
  67. }
  68. else
  69. {
  70. return GetOutputVectorItem( 0, outputId, normalMapUnpackMode );
  71. }
  72. }
  73. public override void WriteToString( ref string nodeInfo , ref string connectionsInfo )
  74. {
  75. base.WriteToString( ref nodeInfo , ref connectionsInfo );
  76. IOUtils.AddFieldValueToString( ref nodeInfo , m_inputMode );
  77. }
  78. public override void ReadFromString( ref string[] nodeParams )
  79. {
  80. base.ReadFromString( ref nodeParams );
  81. if( UIUtils.CurrentShaderVersion() > 18912 )
  82. {
  83. m_inputMode = (UnpackInputMode)Enum.Parse( typeof( UnpackInputMode ) , GetCurrentParam( ref nodeParams ) );
  84. }
  85. }
  86. }
  87. }