LinearDepthNode.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Amplify Shader Editor - Visual Shader Editing Tool
  2. // Copyright (c) Amplify Creations, Lda <info@amplify.pt>
  3. using System;
  4. using UnityEngine;
  5. using UnityEditor;
  6. namespace AmplifyShaderEditor
  7. {
  8. [Serializable]
  9. [NodeAttributes( "Linear Depth", "Miscellaneous", "Converts depth values given on logarithmic space to linear" )]
  10. public sealed class LinearDepthNode : ParentNode
  11. {
  12. private readonly string[] LinearModeLabels = { "Eye Space", "0-1 Space" };
  13. private const string LinearEyeFuncFormat = "LinearEyeDepth({0})";
  14. private const string Linear01FuncFormat = "Linear01Depth({0})";
  15. private const string LinearEyeFuncSRPFormat = "LinearEyeDepth({0},_ZBufferParams)";
  16. private const string Linear01FuncSRPFormat = "Linear01Depth({0},_ZBufferParams)";
  17. private const string LinerValName = "depthToLinear";
  18. private const string ViewSpaceLabel = "View Space";
  19. private UpperLeftWidgetHelper m_upperLeftWidget = new UpperLeftWidgetHelper();
  20. [SerializeField]
  21. private int m_currentOption = 0;
  22. protected override void CommonInit( int uniqueId )
  23. {
  24. base.CommonInit( uniqueId );
  25. AddInputPort( WirePortDataType.FLOAT, false, Constants.EmptyPortValue );
  26. AddOutputPort( WirePortDataType.FLOAT, Constants.EmptyPortValue );
  27. m_autoWrapProperties = true;
  28. m_hasLeftDropdown = true;
  29. m_previewShaderGUID = "2b0785cc8b854974ab4e45419072705a";
  30. UpdateFromOption();
  31. }
  32. public override void Destroy()
  33. {
  34. base.Destroy();
  35. m_upperLeftWidget = null;
  36. }
  37. void UpdateFromOption()
  38. {
  39. m_previewMaterialPassId = m_currentOption;
  40. SetAdditonalTitleText( string.Format( Constants.SubTitleSpaceFormatStr, LinearModeLabels[ m_currentOption ] ) );
  41. }
  42. public override void Draw( DrawInfo drawInfo )
  43. {
  44. base.Draw( drawInfo );
  45. EditorGUI.BeginChangeCheck();
  46. m_currentOption = m_upperLeftWidget.DrawWidget( this, m_currentOption, LinearModeLabels );
  47. if( EditorGUI.EndChangeCheck() )
  48. {
  49. UpdateFromOption();
  50. }
  51. }
  52. public override void DrawProperties()
  53. {
  54. base.DrawProperties();
  55. EditorGUI.BeginChangeCheck();
  56. m_currentOption = EditorGUILayoutPopup( ViewSpaceLabel, m_currentOption, LinearModeLabels );
  57. if( EditorGUI.EndChangeCheck() )
  58. {
  59. SetAdditonalTitleText( string.Format( Constants.SubTitleSpaceFormatStr, LinearModeLabels[ m_currentOption ] ) );
  60. }
  61. }
  62. public override string GenerateShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar )
  63. {
  64. if( m_outputPorts[ 0 ].IsLocalValue( dataCollector.PortCategory ) )
  65. return m_outputPorts[ 0 ].LocalValue( dataCollector.PortCategory );
  66. base.GenerateShaderForOutput( outputId, ref dataCollector, ignoreLocalvar );
  67. string value = m_inputPorts[ 0 ].GeneratePortInstructions( ref dataCollector );
  68. if( dataCollector.IsSRP )
  69. {
  70. value = string.Format( ( ( m_currentOption == 0 ) ? LinearEyeFuncSRPFormat : Linear01FuncSRPFormat ), value );
  71. }
  72. else
  73. {
  74. value = string.Format( ( ( m_currentOption == 0 ) ? LinearEyeFuncFormat : Linear01FuncFormat ), value );
  75. }
  76. RegisterLocalVariable( 0, value, ref dataCollector, LinerValName + OutputId );
  77. return m_outputPorts[ 0 ].LocalValue( dataCollector.PortCategory );
  78. }
  79. public override void WriteToString( ref string nodeInfo, ref string connectionsInfo )
  80. {
  81. base.WriteToString( ref nodeInfo, ref connectionsInfo );
  82. IOUtils.AddFieldValueToString( ref nodeInfo, m_currentOption );
  83. }
  84. public override void ReadFromString( ref string[] nodeParams )
  85. {
  86. base.ReadFromString( ref nodeParams );
  87. int.TryParse( GetCurrentParam( ref nodeParams ), out m_currentOption );
  88. UpdateFromOption();
  89. }
  90. }
  91. }