使用屏幕特效实现亮度、饱和度及对比度

C#脚本:

using UnityEngine;
using System.Collections;

[ExecuteInEditMode]
public class BSC_ImageEffect : MonoBehaviour
{
    public Shader curShader;
    private Material curMaterial;

    Material material
    {
        get
        { 
            if (curMaterial == null)
            {
                curMaterial = new Material(curShader);
                curMaterial.hideFlags = HideFlags.HideAndDontSave;
            }
            return curMaterial;
        }
    }

    public float brightnessAmount = 1.0f;
    public float saturationAmount = 1.0f;
    public float contrastAmount = 1.0f;

    void Start()
    {
        if (!SystemInfo.supportsImageEffects)
        {
            enabled = false;
            return;
        }
        if (!curShader && !curShader.isSupported)
        { 
            enabled = false;
        } 
    }

    void OnRenderImage(RenderTexture sourceTexture, RenderTexture destTexture)
    {
        if (curShader != null)
        {
            material.SetFloat("_BrightnessAmount", brightnessAmount);
            material.SetFloat("_SatAmount", saturationAmount);
            material.SetFloat("_ConAmount", contrastAmount);

            Graphics.Blit(sourceTexture, destTexture, material);
        }
        else
        {
            Graphics.Blit(sourceTexture, destTexture);
        }
    }

    void Update()
    {
        brightnessAmount = Mathf.Clamp(brightnessAmount, 0.0f, 2.0f);
        saturationAmount = Mathf.Clamp(saturationAmount, 0.0f, 2.0f);
        contrastAmount = Mathf.Clamp(contrastAmount, 0.0f, 3.0f);
//        Camera.main.depthTextureMode = DepthTextureMode.Depth;
    }
}

shader脚本:

Shader "Custom/BSC_Effect" {
    Properties {
        _MainTex ("Albedo (RGB)", 2D) = "white" {}
        _BrightnessAmount("Brightness Amount",Range(0.0,1)) = 1.0
        _SatAmount("Saturation Amount",Range(0,1)) = 1
        _ConAmount("Contrast Amount",Range(0,1)) = 1
    }
    SubShader{
        Pass{
            CGPROGRAM
            #pragma vertex vert_img
            #pragma fragment frag
            #pragma fragmentoption ARB_precision_hint_fastest
            #include "UnityCG.cginc"

            uniform sampler2D _MainTex;
            fixed _BrightnessAmount;
            fixed _SatAmount;
            fixed _ConAmount;

            float3 ContrastSaturationBrightness(float3 color,float brt,float sat,float con)
            {
                float AvgLumR = 0.5;
                float AvgLumG = 0.5;
                float AvgLumB = 0.5;

                float3 LuminanceCoeff = float3(0.2125,0.7154,0.0721);

                float3 AvgLumin = float3(AvgLumR,AvgLumG,AvgLumB);
                float3 brtColor = color * brt;
                float intensityf = dot(brtColor,LuminanceCoeff);
                float3 intensity = float3(intensityf,intensityf,intensityf);

                float3 satColor = lerp(intensity,brtColor,sat);

                float3 conColor = lerp(AvgLumin,satColor,con);
                return conColor;
            }

            fixed4 frag(v2f_img i):COLOR{
                fixed4 renderTex = tex2D(_MainTex,i.uv);
                renderTex.rgb = ContrastSaturationBrightness(renderTex.rgb,
                                _BrightnessAmount,
                                _SatAmount,
                                _ConAmount
                );
                return renderTex;
            }
            ENDCG
        }
    }
    FallBack "Diffuse"
}

 

posted @ 2016-07-14 14:20  JohnRey  阅读(379)  评论(0编辑  收藏  举报