GrabScreenPosition.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // Amplify Shader Editor - Visual Shader Editing Tool
  2. // Copyright (c) Amplify Creations, Lda <info@amplify.pt>
  3. using System;
  4. using UnityEditor;
  5. using UnityEngine;
  6. namespace AmplifyShaderEditor
  7. {
  8. [Serializable]
  9. [NodeAttributes( "Grab Screen Position", "Camera And Screen", "Screen position correctly transformed to be used with Grab Screen Color" )]
  10. public sealed class GrabScreenPosition : ParentNode
  11. {
  12. private readonly string[] m_outputTypeStr = { "Normalized", "Screen" };
  13. [SerializeField]
  14. private int m_outputTypeInt = 0;
  15. private UpperLeftWidgetHelper m_upperLeftWidget = new UpperLeftWidgetHelper();
  16. protected override void CommonInit( int uniqueId )
  17. {
  18. base.CommonInit( uniqueId );
  19. AddOutputVectorPorts( WirePortDataType.FLOAT4, "XYZW" );
  20. m_autoWrapProperties = true;
  21. m_hasLeftDropdown = true;
  22. m_textLabelWidth = 65;
  23. ConfigureHeader();
  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. public override void Draw( DrawInfo drawInfo )
  41. {
  42. base.Draw( drawInfo );
  43. EditorGUI.BeginChangeCheck();
  44. m_outputTypeInt = m_upperLeftWidget.DrawWidget( this, m_outputTypeInt, m_outputTypeStr );
  45. if( EditorGUI.EndChangeCheck() )
  46. {
  47. ConfigureHeader();
  48. }
  49. }
  50. public override void DrawProperties()
  51. {
  52. base.DrawProperties();
  53. EditorGUI.BeginChangeCheck();
  54. m_outputTypeInt = EditorGUILayoutPopup( "Type", m_outputTypeInt, m_outputTypeStr );
  55. if( EditorGUI.EndChangeCheck() )
  56. {
  57. ConfigureHeader();
  58. }
  59. }
  60. void ConfigureHeader()
  61. {
  62. SetAdditonalTitleText( string.Format( Constants.SubTitleTypeFormatStr, m_outputTypeStr[ m_outputTypeInt ] ) );
  63. }
  64. public override string GenerateShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalVar )
  65. {
  66. if( m_outputPorts[ 0 ].IsLocalValue( dataCollector.PortCategory ) )
  67. return GetOutputColorItem( 0, outputId, m_outputPorts[ 0 ].LocalValue( dataCollector.PortCategory ) );
  68. string localVarName = string.Empty;
  69. if( m_outputTypeInt == 0 )
  70. localVarName = GeneratorUtils.GenerateGrabScreenPositionNormalized( ref dataCollector, UniqueId, CurrentPrecisionType, !dataCollector.UsingCustomScreenPos );
  71. else
  72. localVarName = GeneratorUtils.GenerateGrabScreenPosition( ref dataCollector, UniqueId, CurrentPrecisionType, !dataCollector.UsingCustomScreenPos );
  73. m_outputPorts[ 0 ].SetLocalValue( localVarName, dataCollector.PortCategory );
  74. return GetOutputColorItem( 0, outputId, localVarName );
  75. }
  76. public override void ReadFromString( ref string[] nodeParams )
  77. {
  78. base.ReadFromString( ref nodeParams );
  79. if( UIUtils.CurrentShaderVersion() > 3108 )
  80. {
  81. if( UIUtils.CurrentShaderVersion() < 6102 )
  82. {
  83. bool project = Convert.ToBoolean( GetCurrentParam( ref nodeParams ) );
  84. m_outputTypeInt = project ? 0 : 1;
  85. }
  86. else
  87. {
  88. m_outputTypeInt = Convert.ToInt32( GetCurrentParam( ref nodeParams ) );
  89. }
  90. }
  91. ConfigureHeader();
  92. }
  93. public override void WriteToString( ref string nodeInfo, ref string connectionsInfo )
  94. {
  95. base.WriteToString( ref nodeInfo, ref connectionsInfo );
  96. IOUtils.AddFieldValueToString( ref nodeInfo, m_outputTypeInt );
  97. }
  98. }
  99. }