GammaToLinearNode.cs 4.5 KB

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