You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

97 lines
3.2 KiB

  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 LinerValName = "depthToLinear";
  16. private const string ViewSpaceLabel = "View Space";
  17. private UpperLeftWidgetHelper m_upperLeftWidget = new UpperLeftWidgetHelper();
  18. [SerializeField]
  19. private int m_currentOption = 0;
  20. protected override void CommonInit( int uniqueId )
  21. {
  22. base.CommonInit( uniqueId );
  23. AddInputPort( WirePortDataType.FLOAT, false, Constants.EmptyPortValue );
  24. AddOutputPort( WirePortDataType.FLOAT, Constants.EmptyPortValue );
  25. m_autoWrapProperties = true;
  26. m_hasLeftDropdown = true;
  27. m_previewShaderGUID = "2b0785cc8b854974ab4e45419072705a";
  28. UpdateFromOption();
  29. }
  30. public override void Destroy()
  31. {
  32. base.Destroy();
  33. m_upperLeftWidget = null;
  34. }
  35. void UpdateFromOption()
  36. {
  37. m_previewMaterialPassId = m_currentOption;
  38. SetAdditonalTitleText( string.Format( Constants.SubTitleSpaceFormatStr, LinearModeLabels[ m_currentOption ] ) );
  39. }
  40. public override void Draw( DrawInfo drawInfo )
  41. {
  42. base.Draw( drawInfo );
  43. EditorGUI.BeginChangeCheck();
  44. m_currentOption = m_upperLeftWidget.DrawWidget( this, m_currentOption, LinearModeLabels );
  45. if( EditorGUI.EndChangeCheck() )
  46. {
  47. UpdateFromOption();
  48. }
  49. }
  50. public override void DrawProperties()
  51. {
  52. base.DrawProperties();
  53. EditorGUI.BeginChangeCheck();
  54. m_currentOption = EditorGUILayoutPopup( ViewSpaceLabel, m_currentOption, LinearModeLabels );
  55. if( EditorGUI.EndChangeCheck() )
  56. {
  57. SetAdditonalTitleText( string.Format( Constants.SubTitleSpaceFormatStr, LinearModeLabels[ m_currentOption ] ) );
  58. }
  59. }
  60. public override string GenerateShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar )
  61. {
  62. if( m_outputPorts[ 0 ].IsLocalValue( dataCollector.PortCategory ) )
  63. return m_outputPorts[ 0 ].LocalValue( dataCollector.PortCategory );
  64. base.GenerateShaderForOutput( outputId, ref dataCollector, ignoreLocalvar );
  65. string value = m_inputPorts[ 0 ].GeneratePortInstructions( ref dataCollector );
  66. value = string.Format( (( m_currentOption == 0 ) ? LinearEyeFuncFormat : Linear01FuncFormat), value );
  67. RegisterLocalVariable( 0, value, ref dataCollector, LinerValName + OutputId );
  68. return m_outputPorts[ 0 ].LocalValue( dataCollector.PortCategory );
  69. }
  70. public override void WriteToString( ref string nodeInfo, ref string connectionsInfo )
  71. {
  72. base.WriteToString( ref nodeInfo, ref connectionsInfo );
  73. IOUtils.AddFieldValueToString( ref nodeInfo, m_currentOption );
  74. }
  75. public override void ReadFromString( ref string[] nodeParams )
  76. {
  77. base.ReadFromString( ref nodeParams );
  78. int.TryParse( GetCurrentParam( ref nodeParams ), out m_currentOption );
  79. UpdateFromOption();
  80. }
  81. }
  82. }