diff options
Diffstat (limited to 'Assets/AmplifyShaderEditor/Plugins/Editor/Nodes/Textures/UnpackScaleNormalNode.cs')
-rw-r--r-- | Assets/AmplifyShaderEditor/Plugins/Editor/Nodes/Textures/UnpackScaleNormalNode.cs | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/Assets/AmplifyShaderEditor/Plugins/Editor/Nodes/Textures/UnpackScaleNormalNode.cs b/Assets/AmplifyShaderEditor/Plugins/Editor/Nodes/Textures/UnpackScaleNormalNode.cs new file mode 100644 index 00000000..364cfcca --- /dev/null +++ b/Assets/AmplifyShaderEditor/Plugins/Editor/Nodes/Textures/UnpackScaleNormalNode.cs @@ -0,0 +1,68 @@ +// Amplify Shader Editor - Visual Shader Editing Tool +// Copyright (c) Amplify Creations, Lda <info@amplify.pt> + +using System; +namespace AmplifyShaderEditor +{ + [NodeAttributes( "Unpack Scale Normal", "Textures", "Applies UnpackNormal/UnpackScaleNormal function" )] + [Serializable] + public class UnpackScaleNormalNode : ParentNode + { + protected override void CommonInit( int uniqueId ) + { + base.CommonInit( uniqueId ); + AddInputPort( WirePortDataType.FLOAT4, false, "Value" ); + AddInputPort( WirePortDataType.FLOAT, false, "Scale" ); + m_inputPorts[ 1 ].FloatInternalData = 1; + AddOutputVectorPorts( WirePortDataType.FLOAT3, "XYZ" ); + m_useInternalPortData = true; + m_previewShaderGUID = "8b0ae05e25d280c45af81ded56f8012e"; + } + + public override string GenerateShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar ) + { + string src = m_inputPorts[ 0 ].GeneratePortInstructions( ref dataCollector ); + + + bool isScaledNormal = false; + if ( m_inputPorts[ 1 ].IsConnected ) + { + isScaledNormal = true; + } + else + { + if ( m_inputPorts[ 1 ].FloatInternalData != 1 ) + { + isScaledNormal = true; + } + } + + string normalMapUnpackMode = string.Empty; + string scaleValue = isScaledNormal?m_inputPorts[ 1 ].GeneratePortInstructions( ref dataCollector ):"1.0"; + normalMapUnpackMode = string.Format( TemplateHelperFunctions.CreateUnpackNormalStr( dataCollector, isScaledNormal, scaleValue ), src); + if( isScaledNormal && !( dataCollector.IsTemplate && dataCollector.IsSRP ) ) + { + dataCollector.AddToIncludes( UniqueId, Constants.UnityStandardUtilsLibFuncs ); + } + + int outputUsage = 0; + for ( int i = 0; i < m_outputPorts.Count; i++ ) + { + if ( m_outputPorts[ i ].IsConnected ) + outputUsage += 1; + } + + + if ( outputUsage > 1 ) + { + string varName = "localUnpackNormal" + OutputId; + dataCollector.AddLocalVariable( UniqueId, "float3 " + varName + " = " + normalMapUnpackMode + ";" ); + return GetOutputVectorItem( 0, outputId, varName ); + } + else + { + return GetOutputVectorItem( 0, outputId, normalMapUnpackMode ); + } + } + } +} |