ScreenDepthNode.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. // Amplify Shader Editor - Visual Shader Editing Tool
  2. // Copyright (c) Amplify Creations, Lda <info@amplify.pt>
  3. using UnityEngine;
  4. using UnityEditor;
  5. using System;
  6. namespace AmplifyShaderEditor
  7. {
  8. [Serializable]
  9. [NodeAttributes( "Screen Depth", "Camera And Screen", "Given a screen position returns the depth of the scene to the object as seen by the camera" )]
  10. public sealed class ScreenDepthNode : ParentNode
  11. {
  12. [SerializeField]
  13. private int m_viewSpaceInt = ( int )DepthMode.DepthEye;
  14. private DepthMode m_depthMode { get { return ( DepthMode )m_viewSpaceInt; } }
  15. private UpperLeftWidgetHelper m_upperLeftWidget = new UpperLeftWidgetHelper();
  16. protected override void CommonInit( int uniqueId )
  17. {
  18. base.CommonInit( uniqueId );
  19. AddInputPort( WirePortDataType.FLOAT4, false, "Pos" );
  20. AddOutputPort( WirePortDataType.FLOAT, "Depth" );
  21. m_autoWrapProperties = true;
  22. m_hasLeftDropdown = true;
  23. UpdateAdditonalTitleText();
  24. }
  25. public override void AfterCommonInit()
  26. {
  27. base.AfterCommonInit();
  28. if( PaddingTitleLeft == 0 )
  29. {
  30. PaddingTitleLeft = Constants.PropertyPickerWidth + Constants.IconsLeftRightMargin;
  31. if( PaddingTitleRight == 0 )
  32. PaddingTitleRight = Constants.PropertyPickerWidth + Constants.IconsLeftRightMargin;
  33. }
  34. }
  35. public override void Destroy()
  36. {
  37. base.Destroy();
  38. m_upperLeftWidget = null;
  39. }
  40. private void UpdateAdditonalTitleText()
  41. {
  42. SetAdditonalTitleText( string.Format( Constants.SubTitleModeFormatStr, GeneratorUtils.DepthModeStr[ m_viewSpaceInt ] ) );
  43. }
  44. public override void Draw( DrawInfo drawInfo )
  45. {
  46. base.Draw( drawInfo );
  47. EditorGUI.BeginChangeCheck();
  48. m_viewSpaceInt = m_upperLeftWidget.DrawWidget( this, m_viewSpaceInt, GeneratorUtils.DepthModeStr );
  49. if( EditorGUI.EndChangeCheck() )
  50. {
  51. UpdateAdditonalTitleText();
  52. }
  53. }
  54. public override void DrawProperties()
  55. {
  56. base.DrawProperties();
  57. EditorGUI.BeginChangeCheck();
  58. m_viewSpaceInt = EditorGUILayoutPopup( "Depth Mode", m_viewSpaceInt, GeneratorUtils.DepthModeStr );
  59. if ( EditorGUI.EndChangeCheck() )
  60. {
  61. UpdateAdditonalTitleText();
  62. }
  63. }
  64. public override string GenerateShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar )
  65. {
  66. if ( dataCollector.PortCategory == MasterNodePortCategory.Tessellation )
  67. {
  68. UIUtils.ShowNoVertexModeNodeMessage( this );
  69. return "0";
  70. }
  71. if ( m_outputPorts[ 0 ].IsLocalValue( dataCollector.PortCategory ) )
  72. return GetOutputColorItem( 0, outputId, m_outputPorts[ 0 ].LocalValue( dataCollector.PortCategory ) );
  73. if ( !( dataCollector.IsTemplate && dataCollector.IsSRP ) )
  74. dataCollector.AddToIncludes( UniqueId, Constants.UnityCgLibFuncs );
  75. if ( !dataCollector.IsTemplate || dataCollector.TemplateDataCollectorInstance.CurrentSRPType != TemplateSRPType.HDRP )
  76. {
  77. if ( dataCollector.IsTemplate && dataCollector.CurrentSRPType == TemplateSRPType.URP )
  78. {
  79. dataCollector.AddToDirectives( Constants.CameraDepthTextureLWEnabler, -1, AdditionalLineType.Define );
  80. }
  81. else
  82. {
  83. dataCollector.AddToUniforms( UniqueId, Constants.CameraDepthTextureValue );
  84. dataCollector.AddToUniforms( UniqueId, Constants.CameraDepthTextureTexelSize );
  85. }
  86. }
  87. string screenPosNorm = string.Empty;
  88. if ( m_inputPorts[ 0 ].IsConnected )
  89. {
  90. screenPosNorm = m_inputPorts[ 0 ].GeneratePortInstructions( ref dataCollector );
  91. }
  92. else
  93. {
  94. if ( dataCollector.IsTemplate )
  95. {
  96. if ( !dataCollector.TemplateDataCollectorInstance.GetCustomInterpolatedData( TemplateInfoOnSematics.SCREEN_POSITION_NORMALIZED, WirePortDataType.FLOAT4, PrecisionType.Float, ref screenPosNorm, true, MasterNodePortCategory.Fragment ) )
  97. {
  98. screenPosNorm = GeneratorUtils.GenerateScreenPositionNormalized( ref dataCollector, UniqueId, CurrentPrecisionType, !dataCollector.UsingCustomScreenPos );
  99. }
  100. }
  101. else
  102. {
  103. screenPosNorm = GeneratorUtils.GenerateScreenPositionNormalized( ref dataCollector, UniqueId, CurrentPrecisionType, !dataCollector.UsingCustomScreenPos );
  104. }
  105. }
  106. string screenDepthInstruction = TemplateHelperFunctions.CreateDepthFetch( dataCollector, screenPosNorm );
  107. if ( m_depthMode == DepthMode.DepthLinearEye || m_depthMode == DepthMode.DepthLinear01 )
  108. {
  109. string viewSpace = ( m_depthMode == DepthMode.DepthLinearEye ) ? "LinearEyeDepth" : "Linear01Depth";
  110. if ( dataCollector.IsTemplate && dataCollector.IsSRP )
  111. {
  112. screenDepthInstruction = string.Format( "{0}( {1}, _ZBufferParams )", viewSpace, screenDepthInstruction );
  113. }
  114. else
  115. {
  116. screenDepthInstruction = string.Format( "{0}( {1} )", viewSpace, screenDepthInstruction );
  117. }
  118. }
  119. else if ( m_depthMode == DepthMode.DepthEye )
  120. {
  121. screenDepthInstruction = string.Format( "{0} * ( _ProjectionParams.z - _ProjectionParams.y )", screenDepthInstruction );
  122. }
  123. dataCollector.AddLocalVariable( UniqueId, CurrentPrecisionType, WirePortDataType.FLOAT, GeneratorUtils.DepthModeVarNameStr[ m_viewSpaceInt ] + OutputId, screenDepthInstruction );
  124. m_outputPorts[ 0 ].SetLocalValue( GeneratorUtils.DepthModeVarNameStr[ m_viewSpaceInt ] + OutputId, dataCollector.PortCategory );
  125. return GetOutputColorItem( 0, outputId, GeneratorUtils.DepthModeVarNameStr[ m_viewSpaceInt ] + OutputId );
  126. }
  127. public override void ReadFromString( ref string[] nodeParams )
  128. {
  129. base.ReadFromString( ref nodeParams );
  130. m_viewSpaceInt = Convert.ToInt32( GetCurrentParam( ref nodeParams ) );
  131. if( UIUtils.CurrentShaderVersion() >= 13901 && UIUtils.CurrentShaderVersion() < 19702 )
  132. {
  133. bool convertToLinear = Convert.ToBoolean( GetCurrentParam( ref nodeParams ) );
  134. if ( !convertToLinear )
  135. {
  136. if ( m_viewSpaceInt == ( int )DepthMode.DepthLinearEye )
  137. {
  138. m_viewSpaceInt = ( int )DepthMode.DepthEye;
  139. }
  140. else if ( m_viewSpaceInt == ( int )DepthMode.DepthLinear01 )
  141. {
  142. m_viewSpaceInt = ( int )DepthMode.Depth01;
  143. }
  144. }
  145. }
  146. UpdateAdditonalTitleText();
  147. }
  148. public override void WriteToString( ref string nodeInfo, ref string connectionsInfo )
  149. {
  150. base.WriteToString( ref nodeInfo, ref connectionsInfo );
  151. IOUtils.AddFieldValueToString( ref nodeInfo, m_viewSpaceInt );
  152. }
  153. }
  154. }