DecodeDepthNormalNode.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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( "Decode Depth Normal", "Miscellaneous", "Decodes both Depth and Normal from a previously encoded pixel value" )]
  8. public sealed class DecodeDepthNormalNode : ParentNode
  9. {
  10. // URP will only have support for depth normals texture over v.10 ... must revisit this node when it comes out
  11. private const string SRPErrorMessage = "This node is only currently supported on the Built-in pipeline";
  12. private const string DecodeDepthNormalFunc = "DecodeDepthNormal( {0}, {1}, {2} );";
  13. protected override void CommonInit( int uniqueId )
  14. {
  15. base.CommonInit( uniqueId );
  16. AddInputPort( WirePortDataType.FLOAT4, false, "Encoded" );
  17. AddOutputPort( WirePortDataType.FLOAT, "Depth" );
  18. AddOutputPort( WirePortDataType.FLOAT3, "Normal" );
  19. m_previewShaderGUID = "dbf37c4d3ce0f0b41822584d6c9ba203";
  20. }
  21. public override string GenerateShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar )
  22. {
  23. if( dataCollector.IsSRP )
  24. {
  25. UIUtils.ShowMessage( SRPErrorMessage, MessageSeverity.Error );
  26. return GenerateErrorValue( outputId );
  27. }
  28. if( m_outputPorts[ outputId ].IsLocalValue( dataCollector.PortCategory ) )
  29. return m_outputPorts[ outputId ].LocalValue( dataCollector.PortCategory );
  30. dataCollector.AddToIncludes( UniqueId, Constants.UnityCgLibFuncs );
  31. string encodedValue = m_inputPorts[ 0 ].GeneratePortInstructions( ref dataCollector );
  32. string depthDecodedVal = "depthDecodedVal" + OutputId;
  33. string normalDecodedVal = "normalDecodedVal" + OutputId;
  34. RegisterLocalVariable( 0, "0", ref dataCollector, depthDecodedVal );
  35. RegisterLocalVariable( 1, "float3(0,0,0)", ref dataCollector, normalDecodedVal );
  36. dataCollector.AddLocalVariable( UniqueId, string.Format( DecodeDepthNormalFunc, encodedValue , depthDecodedVal, normalDecodedVal) );
  37. return m_outputPorts[ outputId ].LocalValue( dataCollector.PortCategory );
  38. }
  39. }
  40. }