shader颜色调整:色相,明度,饱和度,对比度

Shader "Unlit/Colorful"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        _BrightnessAmount("Brightness Amount", Range(0.0, 2.0)) = 1.0
        _SaturationAmount("Saturation Amount", Range(0.0, 2.0)) = 1.0
        _ContrastAmount("Contrast Amount", Range(0.0, 2.0)) = 1.0
    }

    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 100

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            // make fog work
            #pragma multi_compile_fog
            
            #include "UnityCG.cginc"


            sampler2D _MainTex;
            float4 _MainTex_ST;
            fixed _BrightnessAmount;
            fixed _SaturationAmount;
            fixed _ContrastAmount;

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float4 pos : SV_POSITION;
                float2 uv : TEXCOORD0;
            };

            float3 ContrastSaturationBrightness(float3 color, float brt, float sat, float con) 
            {
                // Increase or decrease these values to  
                // adjust r, g and b color channels separately  
                float avgLumR = 0.5;
                float avgLumG = 0.5;
                float avgLumB = 0.5;

                // Luminance coefficients for getting luminance from the image  
                float3 LuminanceCoeff = float3 (0.2125, 0.7154, 0.0721);

                // Operation for brightmess  
                float3 avgLumin = float3 (avgLumR, avgLumG, avgLumB);
                float3 brtColor = color * brt;
                float intensityf = dot(brtColor, LuminanceCoeff);
                float3 intensity = float3 (intensityf, intensityf, intensityf);

                // Operation for saturation  
                float3 satColor = lerp(intensity, brtColor, sat);

                // Operation for contrast  
                float3 conColor = lerp(avgLumin, satColor, con);

                return conColor;
            }

            v2f vert (appdata v)
            {
                v2f o;
                o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
                o.uv = TRANSFORM_TEX(v.uv, _MainTex);
                return o;
            }
            
            fixed4 frag (v2f i) : SV_Target
            {
                fixed4 col = tex2D(_MainTex, i.uv);
                col.rgb = ContrastSaturationBrightness(col, _BrightnessAmount, _SaturationAmount, _ContrastAmount);
                return col;
            }
            ENDCG
        }
    }
}

 

posted on 2016-11-08 18:41  sskyline  阅读(964)  评论(0编辑  收藏  举报