SurfaceDepthNode.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. using System;
  2. using UnityEditor;
  3. using UnityEngine;
  4. namespace AmplifyShaderEditor
  5. {
  6. [Serializable]
  7. [NodeAttributes( "Surface Depth", "Surface Data", "Returns the surface view depth" )]
  8. public sealed class SurfaceDepthNode : ParentNode
  9. {
  10. [SerializeField]
  11. private int m_viewSpaceInt = 0;
  12. private DepthMode m_depthMode { get { return ( DepthMode )m_viewSpaceInt; } }
  13. private UpperLeftWidgetHelper m_upperLeftWidget = new UpperLeftWidgetHelper();
  14. private void UpdateAdditonalTitleText()
  15. {
  16. SetAdditonalTitleText( string.Format( Constants.SubTitleModeFormatStr, GeneratorUtils.DepthModeStr[ m_viewSpaceInt ] ) );
  17. }
  18. protected override void CommonInit( int uniqueId )
  19. {
  20. base.CommonInit( uniqueId );
  21. AddInputPort( WirePortDataType.FLOAT3, false, "Vertex Position" );
  22. AddOutputPort( WirePortDataType.FLOAT, "Depth" );
  23. m_autoWrapProperties = true;
  24. m_hasLeftDropdown = true;
  25. UpdateAdditonalTitleText();
  26. }
  27. public override void Destroy()
  28. {
  29. base.Destroy();
  30. m_upperLeftWidget = null;
  31. }
  32. public override void AfterCommonInit()
  33. {
  34. base.AfterCommonInit();
  35. if( PaddingTitleLeft == 0 )
  36. {
  37. PaddingTitleLeft = Constants.PropertyPickerWidth + Constants.IconsLeftRightMargin;
  38. if( PaddingTitleRight == 0 )
  39. PaddingTitleRight = Constants.PropertyPickerWidth + Constants.IconsLeftRightMargin;
  40. }
  41. }
  42. public override void Draw( DrawInfo drawInfo )
  43. {
  44. base.Draw( drawInfo );
  45. EditorGUI.BeginChangeCheck();
  46. m_viewSpaceInt = m_upperLeftWidget.DrawWidget( this, m_viewSpaceInt, GeneratorUtils.DepthModeStr );
  47. if( EditorGUI.EndChangeCheck() )
  48. {
  49. UpdateAdditonalTitleText();
  50. }
  51. }
  52. public override void DrawProperties()
  53. {
  54. base.DrawProperties();
  55. EditorGUI.BeginChangeCheck();
  56. m_viewSpaceInt = EditorGUILayoutPopup( "Depth Mode", m_viewSpaceInt, GeneratorUtils.DepthModeStr );
  57. if( EditorGUI.EndChangeCheck() )
  58. {
  59. UpdateAdditonalTitleText();
  60. }
  61. }
  62. private string ApplyLinearDepthModifier( ref MasterNodeDataCollector dataCollector, string instruction )
  63. {
  64. switch ( m_depthMode )
  65. {
  66. case DepthMode.DepthLinearEye: instruction = GeneratorUtils.ApplyLinearDepthModifier( ref dataCollector, instruction, m_depthMode ); break;
  67. case DepthMode.DepthLinear01: instruction = GeneratorUtils.ApplyLinearDepthModifier( ref dataCollector, instruction, m_depthMode ); break;
  68. case DepthMode.DepthEye: instruction = string.Format( "( {0} ) * ( _ProjectionParams.z - _ProjectionParams.y )", instruction ); break;
  69. case DepthMode.Depth01:
  70. default: break;
  71. }
  72. return instruction;
  73. }
  74. public override string GenerateShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar )
  75. {
  76. if ( dataCollector.PortCategory == MasterNodePortCategory.Tessellation )
  77. {
  78. UIUtils.ShowNoVertexModeNodeMessage( this );
  79. return "0";
  80. }
  81. if ( dataCollector.IsTemplate )
  82. {
  83. if( m_inputPorts[ 0 ].IsConnected )
  84. {
  85. string vertexPos = "vertexPos" + OutputId;
  86. GenerateInputInVertex( ref dataCollector, 0, vertexPos, false );
  87. string clipPos = dataCollector.TemplateDataCollectorInstance.GetClipPosForValue( vertexPos, OutputId );
  88. string varName = GeneratorUtils.DepthModeVarNameStr[ m_viewSpaceInt ] + OutputId;
  89. string instruction = string.Format( "{0}.z / {0}.w", clipPos );
  90. instruction = ApplyLinearDepthModifier( ref dataCollector, instruction );
  91. dataCollector.AddLocalVariable( UniqueId, CurrentPrecisionType, WirePortDataType.FLOAT, varName, instruction );
  92. return varName;
  93. }
  94. else
  95. {
  96. return dataCollector.TemplateDataCollectorInstance.GetSurfaceDepth( m_depthMode, CurrentPrecisionType );
  97. }
  98. }
  99. dataCollector.AddToIncludes( UniqueId, Constants.UnityShaderVariables );
  100. if ( dataCollector.PortCategory == MasterNodePortCategory.Vertex )
  101. {
  102. string vertexPos;
  103. string varName;
  104. if ( m_inputPorts[ 0 ].IsConnected )
  105. {
  106. vertexPos = m_inputPorts[ 0 ].GeneratePortInstructions( ref dataCollector );
  107. varName = GeneratorUtils.DepthModeVarNameStr[ m_viewSpaceInt ] + OutputId;
  108. }
  109. else
  110. {
  111. vertexPos = Constants.VertexShaderInputStr + ".vertex.xyz";
  112. varName = GeneratorUtils.DepthModeVarNameStr[ m_viewSpaceInt ];
  113. }
  114. string instruction = string.Format( "UnityObjectToClipPos( {0} ).zw", vertexPos );
  115. string clipDepth = "clipDepth" + OutputId;
  116. dataCollector.AddLocalVariable( UniqueId, CurrentPrecisionType, WirePortDataType.FLOAT2, clipDepth, instruction );
  117. instruction = string.Format( "{0}.x / {0}.y", clipDepth );
  118. instruction = ApplyLinearDepthModifier( ref dataCollector, instruction );
  119. dataCollector.AddLocalVariable( UniqueId, CurrentPrecisionType, WirePortDataType.FLOAT3, varName, instruction );
  120. return varName;
  121. }
  122. if ( m_inputPorts[ 0 ].IsConnected )
  123. {
  124. if ( dataCollector.TesselationActive )
  125. {
  126. if ( m_outputPorts[ 0 ].IsLocalValue( dataCollector.PortCategory ) )
  127. return m_outputPorts[ 0 ].LocalValue( dataCollector.PortCategory );
  128. string vertexPos = m_inputPorts[ 0 ].GeneratePortInstructions( ref dataCollector );
  129. string instruction = string.Format( "UnityObjectToClipPos( {0} ).zw", vertexPos );
  130. string clipDepth = "clipDepth" + OutputId;
  131. string varName = GeneratorUtils.DepthModeVarNameStr[ m_viewSpaceInt ] + OutputId;
  132. dataCollector.AddLocalVariable( UniqueId, CurrentPrecisionType, WirePortDataType.FLOAT2, clipDepth, instruction );
  133. instruction = string.Format( "{0}.x / {0}.y", clipDepth );
  134. instruction = ApplyLinearDepthModifier( ref dataCollector, instruction );
  135. RegisterLocalVariable( 0, instruction, ref dataCollector, varName );
  136. return m_outputPorts[ 0 ].LocalValue( dataCollector.PortCategory );
  137. }
  138. else
  139. {
  140. var savedPortCategory = dataCollector.PortCategory;
  141. dataCollector.PortCategory = MasterNodePortCategory.Vertex;
  142. string vertexPos = m_inputPorts[ 0 ].GeneratePortInstructions( ref dataCollector );
  143. string varName = GeneratorUtils.DepthModeVarNameStr[ m_viewSpaceInt ] + OutputId;
  144. dataCollector.PortCategory = savedPortCategory;
  145. string clipDepth = "clipDepth" + OutputId;
  146. dataCollector.AddToInput( UniqueId, clipDepth, WirePortDataType.FLOAT2 );
  147. string clipDepthOutput = string.Format( "{0}.{1}", Constants.VertexShaderOutputStr, clipDepth );
  148. string clipDepthInput = string.Format( "{0}.{1}", Constants.InputVarStr, clipDepth );
  149. string vertexInstruction = string.Format( "UnityObjectToClipPos( {0} ).zw", vertexPos );
  150. dataCollector.AddToVertexLocalVariables( UniqueId, CurrentPrecisionType, WirePortDataType.FLOAT2, clipDepth, vertexInstruction );
  151. dataCollector.AddToVertexLocalVariables( UniqueId, clipDepthOutput, clipDepth );
  152. string fragmentInstruction = string.Format( "{0}.x / {0}.y", clipDepthInput );
  153. fragmentInstruction = ApplyLinearDepthModifier( ref dataCollector, fragmentInstruction );
  154. dataCollector.AddLocalVariable( UniqueId, CurrentPrecisionType, WirePortDataType.FLOAT, varName, fragmentInstruction );
  155. return varName;
  156. }
  157. }
  158. else
  159. {
  160. return GeneratorUtils.GenerateSurfaceDepth( ref dataCollector, UniqueId, CurrentPrecisionType, m_depthMode );
  161. }
  162. }
  163. public override void ReadFromString( ref string[] nodeParams )
  164. {
  165. base.ReadFromString( ref nodeParams );
  166. m_viewSpaceInt = Convert.ToInt32( GetCurrentParam( ref nodeParams ) );
  167. UpdateAdditonalTitleText();
  168. }
  169. public override void WriteToString( ref string nodeInfo, ref string connectionsInfo )
  170. {
  171. base.WriteToString( ref nodeInfo, ref connectionsInfo );
  172. IOUtils.AddFieldValueToString( ref nodeInfo, m_viewSpaceInt );
  173. }
  174. }
  175. }