LinearToGammaNode.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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( "Linear To Gamma", "Image Effects", "Converts color from linear space to gamma space" )]
  10. public sealed class LinearToGammaNode : HelperParentNode
  11. {
  12. //[SerializeField]
  13. //private bool m_exact = false;
  14. //private readonly static GUIContent LGExactContent = new GUIContent( "Exact Conversion", "Uses a precise version of the conversion, it's more expensive and often not needed." );
  15. public readonly static string[] ModeListStr = { "Fast Linear to sRGB", "Exact Linear to sRGB" };
  16. public readonly static int[] ModeListInt = { 0, 1 };
  17. public readonly static string[] ModeListStrLW = { "Fast Linear to sRGB", "Exact Linear to sRGB", "Linear to Gamma 2.0", "Linear to Gamma 2.2" };
  18. public readonly static int[] ModeListIntLW = { 0, 1, 2, 3 };
  19. [SerializeField]
  20. public int m_selectedMode = 0;
  21. protected override void CommonInit( int uniqueId )
  22. {
  23. base.CommonInit( uniqueId );
  24. m_funcType = "LinearToGammaSpace";
  25. m_inputPorts[ 0 ].ChangeType( WirePortDataType.FLOAT3, false );
  26. m_inputPorts[ 0 ].Name = "RGB";
  27. m_outputPorts[ 0 ].ChangeType( WirePortDataType.FLOAT3, false );
  28. m_autoWrapProperties = true;
  29. m_previewShaderGUID = "9027c408b928c5c4d8b450712049d541";
  30. m_textLabelWidth = 120;
  31. }
  32. protected override void OnUniqueIDAssigned()
  33. {
  34. base.OnUniqueIDAssigned();
  35. m_localVarName = "linearToGamma" + OutputId;
  36. }
  37. public override void DrawProperties()
  38. {
  39. base.DrawProperties();
  40. if( ContainerGraph.IsSRP )
  41. {
  42. m_selectedMode = EditorGUILayoutIntPopup( "Mode", m_selectedMode, ModeListStrLW, ModeListIntLW );
  43. EditorGUILayout.HelpBox( "Fast Linear: fast approximation from Linear to sRGB\n\nExact Linear: a more expensive but exact calculation from Linear to sRGB.\n\nLinear 2.0: crude approximation from Linear to Gamma using a power of 1/2.0 gamma value\n\nLinear 2.2: an approximation from Linear to Gamma using a power of 1/2.2 gamma value", MessageType.None );
  44. }
  45. else
  46. {
  47. m_selectedMode = EditorGUILayoutIntPopup( "Mode", m_selectedMode, ModeListStr, ModeListInt );
  48. EditorGUILayout.HelpBox( "Fast Linear: fast approximation from Linear to sRGB\n\nExact Linear: a more expensive but exact calculation from Linear to sRGB.", MessageType.None );
  49. }
  50. }
  51. public override string GenerateShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar )
  52. {
  53. string result = m_inputPorts[ 0 ].GeneratePortInstructions( ref dataCollector );
  54. if( !dataCollector.IsSRP )
  55. {
  56. m_selectedMode = Mathf.Min( m_selectedMode, 1 );
  57. if( m_selectedMode == 1 )
  58. {
  59. dataCollector.AddLocalVariable( UniqueId, "half3 " + m_localVarName + " = " + result + ";" );
  60. dataCollector.AddLocalVariable( UniqueId, m_localVarName + " = half3( LinearToGammaSpaceExact(" + m_localVarName + ".r), LinearToGammaSpaceExact(" + m_localVarName + ".g), LinearToGammaSpaceExact(" + m_localVarName + ".b) );" );
  61. return m_localVarName;
  62. }
  63. return base.GenerateShaderForOutput( outputId, ref dataCollector, ignoreLocalvar );
  64. }
  65. else
  66. {
  67. dataCollector.AddToIncludes( UniqueId, TemplateHelperFunctions.CoreCommonLib );
  68. dataCollector.AddToIncludes( UniqueId, TemplateHelperFunctions.CoreColorLib );
  69. switch( m_selectedMode )
  70. {
  71. default:
  72. case 0:
  73. m_funcLWFormatOverride = "FastLinearToSRGB( {0} )";
  74. m_funcHDFormatOverride = "FastLinearToSRGB( {0} )";
  75. break;
  76. case 1:
  77. m_funcLWFormatOverride = "LinearToSRGB( {0} )";
  78. m_funcHDFormatOverride = "LinearToSRGB( {0} )";
  79. break;
  80. case 2:
  81. m_funcLWFormatOverride = "LinearToGamma20( {0} )";
  82. m_funcHDFormatOverride = "LinearToGamma20( {0} )";
  83. break;
  84. case 3:
  85. m_funcLWFormatOverride = "LinearToGamma22( {0} )";
  86. m_funcHDFormatOverride = "LinearToGamma22( {0} )";
  87. break;
  88. }
  89. return base.GenerateShaderForOutput( outputId, ref dataCollector, ignoreLocalvar );
  90. }
  91. }
  92. public override void WriteToString( ref string nodeInfo, ref string connectionsInfo )
  93. {
  94. base.WriteToString( ref nodeInfo, ref connectionsInfo );
  95. IOUtils.AddFieldValueToString( ref nodeInfo, m_selectedMode );
  96. }
  97. public override void ReadFromString( ref string[] nodeParams )
  98. {
  99. base.ReadFromString( ref nodeParams );
  100. if( UIUtils.CurrentShaderVersion() > 11003 && UIUtils.CurrentShaderVersion() <= 14503 )
  101. {
  102. bool fast = Convert.ToBoolean( GetCurrentParam( ref nodeParams ) );
  103. if( fast )
  104. m_selectedMode = 1;
  105. }
  106. if( UIUtils.CurrentShaderVersion() > 14503 )
  107. {
  108. m_selectedMode = Convert.ToInt32( GetCurrentParam( ref nodeParams ) );
  109. }
  110. }
  111. }
  112. }