DepthFade.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. // Amplify Shader Editor - Visual Shader Editing Tool
  2. // Copyright (c) Amplify Creations, Lda <info@amplify.pt>
  3. using UnityEngine;
  4. using System;
  5. namespace AmplifyShaderEditor
  6. {
  7. [Serializable]
  8. [NodeAttributes( "Depth Fade", "Surface Data", "Outputs a linear gradient representing the distance between the surface of this object and geometry behind" )]
  9. public sealed class DepthFade : ParentNode
  10. {
  11. private const string ConvertToLinearStr = "Convert To Linear";
  12. private const string SaturateStr = "Saturate";
  13. private const string MirrorStr = "Mirror";
  14. [SerializeField]
  15. private bool m_convertToLinear = true;
  16. [SerializeField]
  17. private bool m_saturate = false;
  18. [SerializeField]
  19. private bool m_mirror = true;
  20. protected override void CommonInit( int uniqueId )
  21. {
  22. base.CommonInit( uniqueId );
  23. AddInputPort( WirePortDataType.FLOAT3, false, "Vertex Position", -1, MasterNodePortCategory.Fragment, 1 );
  24. AddInputPort( WirePortDataType.FLOAT, false, "Distance",-1,MasterNodePortCategory.Fragment,0 );
  25. GetInputPortByUniqueId(0).FloatInternalData = 1;
  26. AddOutputPort( WirePortDataType.FLOAT, "Out" );
  27. m_useInternalPortData = true;
  28. m_autoWrapProperties = true;
  29. }
  30. public override string GenerateShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar )
  31. {
  32. if( dataCollector.PortCategory == MasterNodePortCategory.Tessellation )
  33. {
  34. UIUtils.ShowNoVertexModeNodeMessage( this );
  35. return "0";
  36. }
  37. if( m_outputPorts[ 0 ].IsLocalValue( dataCollector.PortCategory ) )
  38. return GetOutputColorItem( 0, outputId, m_outputPorts[ 0 ].LocalValue( dataCollector.PortCategory ) );
  39. if( !( dataCollector.IsTemplate && dataCollector.IsSRP ) )
  40. dataCollector.AddToIncludes( UniqueId, Constants.UnityCgLibFuncs );
  41. if( !dataCollector.IsTemplate || dataCollector.TemplateDataCollectorInstance.CurrentSRPType != TemplateSRPType.HDRP )
  42. {
  43. if( dataCollector.IsTemplate && dataCollector.CurrentSRPType == TemplateSRPType.URP )
  44. {
  45. dataCollector.AddToDirectives( Constants.CameraDepthTextureLWEnabler, -1, AdditionalLineType.Define );
  46. }
  47. else
  48. {
  49. dataCollector.AddToUniforms( UniqueId, Constants.CameraDepthTextureValue );
  50. dataCollector.AddToUniforms( UniqueId, Constants.CameraDepthTextureTexelSize );
  51. }
  52. }
  53. string screenPosNorm;
  54. InputPort vertexPosPort = GetInputPortByUniqueId( 1 );
  55. if( vertexPosPort.IsConnected )
  56. {
  57. string vertexPosVar = "vertexPos" + OutputId;
  58. if ( dataCollector.IsTemplate || !dataCollector.TesselationActive )
  59. {
  60. GenerateInputInVertex( ref dataCollector, 1, vertexPosVar, false );
  61. }
  62. else
  63. {
  64. // @diogo: surface shader + tessellation? can't add interpolators, so generate everything in fragment
  65. vertexPosVar = vertexPosPort.GeneratePortInstructions( ref dataCollector );
  66. }
  67. screenPosNorm = GeneratorUtils.GenerateScreenPositionNormalizedForValue( vertexPosVar, OutputId, ref dataCollector, UniqueId, CurrentPrecisionType, !dataCollector.UsingCustomScreenPos );
  68. }
  69. else
  70. {
  71. if( dataCollector.IsTemplate )
  72. {
  73. string ppsScreenPos = string.Empty;
  74. if( !dataCollector.TemplateDataCollectorInstance.GetCustomInterpolatedData( TemplateInfoOnSematics.SCREEN_POSITION_NORMALIZED, WirePortDataType.FLOAT4, PrecisionType.Float, ref ppsScreenPos, true, MasterNodePortCategory.Fragment ) )
  75. {
  76. screenPosNorm = GeneratorUtils.GenerateScreenPositionNormalized( ref dataCollector, UniqueId, CurrentPrecisionType, !dataCollector.UsingCustomScreenPos );
  77. }
  78. else
  79. {
  80. screenPosNorm = ppsScreenPos;
  81. }
  82. }
  83. else
  84. {
  85. screenPosNorm = GeneratorUtils.GenerateScreenPositionNormalized( ref dataCollector, UniqueId, CurrentPrecisionType, !dataCollector.UsingCustomScreenPos );
  86. }
  87. }
  88. string screenDepth = TemplateHelperFunctions.CreateDepthFetch( dataCollector, screenPosNorm );
  89. if( m_convertToLinear )
  90. {
  91. if( dataCollector.IsTemplate && dataCollector.IsSRP )
  92. screenDepth = string.Format( "LinearEyeDepth({0},_ZBufferParams)", screenDepth );
  93. else
  94. screenDepth = string.Format( "LinearEyeDepth({0})", screenDepth );
  95. }
  96. else
  97. {
  98. screenDepth = string.Format( "({0}*( _ProjectionParams.z - _ProjectionParams.y ))", screenDepth );
  99. }
  100. string distance = GetInputPortByUniqueId( 0 ).GeneratePortInstructions( ref dataCollector );
  101. dataCollector.AddLocalVariable( UniqueId, "float screenDepth" + OutputId + " = " + screenDepth + ";" );
  102. string finalVarName = "distanceDepth" + OutputId;
  103. string finalVarValue = string.Empty;
  104. if( dataCollector.IsTemplate && dataCollector.IsSRP )
  105. finalVarValue = "( screenDepth" + OutputId + " - LinearEyeDepth( " + screenPosNorm + ".z,_ZBufferParams ) ) / ( " + distance + " )";
  106. else
  107. finalVarValue = "( screenDepth" + OutputId + " - LinearEyeDepth( " + screenPosNorm + ".z ) ) / ( " + distance + " )";
  108. if( m_mirror )
  109. {
  110. finalVarValue = string.Format( "abs( {0} )", finalVarValue );
  111. }
  112. if( m_saturate )
  113. {
  114. finalVarValue = string.Format( "saturate( {0} )", finalVarValue );
  115. }
  116. dataCollector.AddLocalVariable( UniqueId, CurrentPrecisionType, WirePortDataType.FLOAT, finalVarName, finalVarValue );
  117. m_outputPorts[ 0 ].SetLocalValue( finalVarName, dataCollector.PortCategory );
  118. return GetOutputColorItem( 0, outputId, finalVarName );
  119. }
  120. public override void DrawProperties()
  121. {
  122. base.DrawProperties();
  123. m_convertToLinear = EditorGUILayoutToggle( ConvertToLinearStr, m_convertToLinear );
  124. m_mirror = EditorGUILayoutToggle( MirrorStr, m_mirror );
  125. m_saturate = EditorGUILayoutToggle( SaturateStr, m_saturate );
  126. }
  127. public override void ReadFromString( ref string[] nodeParams )
  128. {
  129. base.ReadFromString( ref nodeParams );
  130. if( UIUtils.CurrentShaderVersion() >= 13901 )
  131. {
  132. m_convertToLinear = Convert.ToBoolean( GetCurrentParam( ref nodeParams ) );
  133. }
  134. if( UIUtils.CurrentShaderVersion() > 15607 )
  135. {
  136. m_saturate = Convert.ToBoolean( GetCurrentParam( ref nodeParams ) );
  137. }
  138. if( UIUtils.CurrentShaderVersion() > 15700 )
  139. {
  140. m_mirror = Convert.ToBoolean( GetCurrentParam( ref nodeParams ) );
  141. }
  142. }
  143. public override void WriteToString( ref string nodeInfo, ref string connectionsInfo )
  144. {
  145. base.WriteToString( ref nodeInfo, ref connectionsInfo );
  146. IOUtils.AddFieldValueToString( ref nodeInfo, m_convertToLinear );
  147. IOUtils.AddFieldValueToString( ref nodeInfo, m_saturate );
  148. IOUtils.AddFieldValueToString( ref nodeInfo, m_mirror );
  149. }
  150. }
  151. }