# 【调色】

类似 Photoshop 里的调整亮度、饱和度、对比度、色相的功能。

对应的 Shader 文件 ScreenAfterEffectShader2.shader

Shader "Hidden/ScreenAfterEffectShader2"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        _Brightness("亮度",Range(0,10)) = 1
        _Saturation("饱和度",Range(0,1)) = 1
        _Contrast("对比度",Range(0,10)) = 1
        _HueShift("色相",Range(-1,1)) = 0
    }
    SubShader
    {
        // No culling or depth
        Cull Off ZWrite Off ZTest Always

        Pass
        {
            CGPROGRAM
            #pragma vertex vert_img
            #pragma fragment frag

            #include "UnityCG.cginc"

            sampler2D _MainTex;
            float _Brightness;
            float _Saturation;
            float _Contrast;
            float _HueShift;

            float3 HSVToRGB( float3 c )
            {
                float4 K = float4( 1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0 );
                float3 p = abs( frac( c.xxx + K.xyz ) * 6.0 - K.www );
                return c.z * lerp( K.xxx, saturate( p - K.xxx ), c.y );
            }

            float3 RGBToHSV(float3 c)
            {
                float4 K = float4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);
                float4 p = lerp( float4( c.bg, K.wz ), float4( c.gb, K.xy ), step( c.b, c.g ) );
                float4 q = lerp( float4( p.xyw, c.r ), float4( c.r, p.yzx ), step( p.x, c.r ) );
                float d = q.x - min( q.w, q.y );
                float e = 1.0e-10;
                return float3( abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);
            }

            fixed4 frag (v2f_img i) : SV_Target
            {
                half4 col = tex2D(_MainTex, i.uv);
                half3 finalColor = col.rgb;

                // 色相
                half3 hsv = RGBToHSV(finalColor);
                hsv.r = hsv.r + _HueShift;
                finalColor = HSVToRGB(hsv);

                // 亮度
                finalColor = finalColor * _Brightness;

                // 饱和度
                float lumin = dot(finalColor,half3(0.22,0.707,0.071));
                finalColor = lerp(lumin,finalColor,_Saturation);
                // 对比度
                float middlepoint = half3(0.227,0.227,0.227);
                finalColor = lerp(middlepoint,finalColor,_Contrast);

                return fixed4(finalColor,1);
            }
            ENDCG
        }
    }
}