ScreenPosInputsNode.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 Position", "Camera And Screen", "Gives access to the screen coordinates of the mesh vertex or fragment, where the X and Y values represent the horizontal and vertical positions. Use the Mode dropdown to choose the desired output mode." )]
  10. public sealed class ScreenPosInputsNode : SurfaceShaderINParentNode
  11. {
  12. enum Mode
  13. {
  14. Normalized = 0,
  15. Raw,
  16. Center,
  17. Tiled,
  18. Pixel
  19. };
  20. private readonly string[] m_outputTypeStr = { "Normalized", "Raw", "Center", "Tiled", "Pixel" };
  21. [SerializeField]
  22. private int m_outputTypeInt = ( int )Mode.Normalized;
  23. [SerializeField]
  24. private bool m_scaleAndOffset = false;
  25. private UpperLeftWidgetHelper m_upperLeftWidget = new UpperLeftWidgetHelper();
  26. protected override void CommonInit( int uniqueId )
  27. {
  28. base.CommonInit( uniqueId );
  29. m_currentInput = SurfaceInputs.SCREEN_POS;
  30. InitialSetup();
  31. m_textLabelWidth = 65;
  32. m_autoWrapProperties = true;
  33. m_hasLeftDropdown = true;
  34. m_previewShaderGUID = "a5e7295278a404175b732f1516fb68a6";
  35. if( UIUtils.CurrentWindow != null && UIUtils.CurrentWindow.CurrentGraph != null && UIUtils.CurrentShaderVersion() <= 2400 )
  36. {
  37. m_outputTypeInt = ( int )Mode.Raw;
  38. m_previewMaterialPassId = ( int )m_outputTypeInt;
  39. }
  40. ConfigureHeader();
  41. }
  42. public override void Draw( DrawInfo drawInfo )
  43. {
  44. base.Draw( drawInfo );
  45. EditorGUI.BeginChangeCheck();
  46. m_outputTypeInt = m_upperLeftWidget.DrawWidget( this, m_outputTypeInt, m_outputTypeStr );
  47. if( EditorGUI.EndChangeCheck() )
  48. {
  49. ConfigureHeader();
  50. }
  51. }
  52. public override void DrawProperties()
  53. {
  54. //base.DrawProperties();
  55. EditorGUI.BeginChangeCheck();
  56. m_outputTypeInt = EditorGUILayoutPopup( "Mode", m_outputTypeInt, m_outputTypeStr );
  57. if( EditorGUI.EndChangeCheck() )
  58. {
  59. ConfigureHeader();
  60. }
  61. }
  62. void ConfigureHeader()
  63. {
  64. SetAdditonalTitleText( string.Format( Constants.SubTitleModeFormatStr, m_outputTypeStr[ m_outputTypeInt ] ) );
  65. m_previewMaterialPassId = m_outputTypeInt;
  66. }
  67. public override void Reset()
  68. {
  69. base.Reset();
  70. }
  71. public override void Destroy()
  72. {
  73. base.Destroy();
  74. m_upperLeftWidget = null;
  75. }
  76. public override string GenerateShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalVar )
  77. {
  78. if ( m_outputPorts[ 0 ].IsLocalValue( dataCollector.PortCategory ) )
  79. {
  80. return GetOutputVectorItem( 0, outputId, m_outputPorts[ 0 ].LocalValue( dataCollector.PortCategory ) );
  81. }
  82. m_currentPrecisionType = PrecisionType.Float;
  83. // TODO: these kinds of calls need a serious cleanup, too much redundancy
  84. string screenPos = string.Empty;
  85. if ( dataCollector.TesselationActive && dataCollector.IsFragmentCategory || dataCollector.IsTemplate )
  86. {
  87. switch ( ( Mode )m_outputTypeInt )
  88. {
  89. case Mode.Normalized: screenPos = GeneratorUtils.GenerateScreenPositionNormalizedOnFrag( ref dataCollector, UniqueId, CurrentPrecisionType ); break;
  90. case Mode.Raw: screenPos = GeneratorUtils.GenerateScreenPositionRawOnFrag( ref dataCollector, UniqueId, CurrentPrecisionType ); break;
  91. case Mode.Center: screenPos = GeneratorUtils.GenerateScreenPositionCenterOnFrag( ref dataCollector, UniqueId, CurrentPrecisionType ); break;
  92. case Mode.Tiled: screenPos = GeneratorUtils.GenerateScreenPositionTiledOnFrag( ref dataCollector, UniqueId, CurrentPrecisionType ); break;
  93. case Mode.Pixel: screenPos = GeneratorUtils.GenerateScreenPositionPixelOnFrag( ref dataCollector, UniqueId, CurrentPrecisionType ); break;
  94. }
  95. }
  96. else
  97. {
  98. switch ( ( Mode )m_outputTypeInt )
  99. {
  100. case Mode.Normalized: screenPos = GeneratorUtils.GenerateScreenPositionNormalized( ref dataCollector, UniqueId, CurrentPrecisionType ); break;
  101. case Mode.Raw: screenPos = GeneratorUtils.GenerateScreenPositionRaw( ref dataCollector, UniqueId, CurrentPrecisionType ); break;
  102. case Mode.Center: screenPos = GeneratorUtils.GenerateScreenPositionCenter( ref dataCollector, UniqueId, CurrentPrecisionType ); break;
  103. case Mode.Tiled: screenPos = GeneratorUtils.GenerateScreenPositionTiled( ref dataCollector, UniqueId, CurrentPrecisionType ); break;
  104. case Mode.Pixel: screenPos = GeneratorUtils.GenerateScreenPositionPixel( ref dataCollector, UniqueId, CurrentPrecisionType ); break;
  105. }
  106. }
  107. m_outputPorts[ 0 ].SetLocalValue( screenPos, dataCollector.PortCategory );
  108. return GetOutputVectorItem( 0, outputId, screenPos );
  109. }
  110. public override void ReadFromString( ref string[] nodeParams )
  111. {
  112. base.ReadFromString( ref nodeParams );
  113. if( UIUtils.CurrentShaderVersion() > 2400 )
  114. {
  115. if( UIUtils.CurrentShaderVersion() < 6102 )
  116. {
  117. bool project = Convert.ToBoolean( GetCurrentParam( ref nodeParams ) );
  118. m_outputTypeInt = project ? ( int )Mode.Normalized : ( int )Mode.Raw;
  119. }
  120. else
  121. {
  122. m_outputTypeInt = Convert.ToInt32( GetCurrentParam( ref nodeParams ) );
  123. }
  124. }
  125. if( UIUtils.CurrentShaderVersion() > 3107 )
  126. {
  127. m_scaleAndOffset = Convert.ToBoolean( GetCurrentParam( ref nodeParams ) );
  128. m_scaleAndOffset = false;
  129. }
  130. ConfigureHeader();
  131. }
  132. public override void WriteToString( ref string nodeInfo, ref string connectionsInfo )
  133. {
  134. base.WriteToString( ref nodeInfo, ref connectionsInfo );
  135. IOUtils.AddFieldValueToString( ref nodeInfo, m_outputTypeInt );
  136. IOUtils.AddFieldValueToString( ref nodeInfo, m_scaleAndOffset );
  137. }
  138. }
  139. }