色调: 指画面上表现情感的设色及其浓淡。如:暖色调、冷色调。
饱和度: 颜色纯度,越纯越鲜艳,纯的颜色属于高度饱和,灰色属于完全不饱和。
亮度: 光线强弱决定。
对比度: 画面上的明暗差
BSC_Effect.shader
Shader "Custom/BSC_Effect" {
Properties {
_MainTex ("Base (RGB)", 2D) = "white" {}
//亮度值
_BrightnessAmount ("Brightness Amount", Range(0.0, 1)) = 1.0
//饱和度
_satAmount ("Saturation Amount", Range(0.0, 1)) = 1.0
//对比度
_conAmount ("Contrast Amount" , Range(0.0, 1)) = 1.0
}
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;
//当前图像的全局亮度值,这个系数是由CIE颜色匹配函数得来的,并成为了行业标准。
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);
//得到亮度值以后,再用一组lerp函数从亮度值的操作开始混合运算。
//然后用原始图像乘上函数传递过来的亮度值。
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"
}
TestRenderImage.cs
using UnityEngine;
using System.Collections;
[ExecuteInEditMode]
public class TestRenderImage : MonoBehaviour {
public Shader curShader;
private Material curMaterial;
public float brightnessAmount = 1.0f;
public float saturationAmount = 1.0f;
public float contrastAmount = 1.0f;
Material material
{
get
{
if(curMaterial == null)
{
curMaterial = new Material(curShader);
curMaterial.hideFlags = HideFlags.HideAndDontSave;
}
return curMaterial;
}
}
void Start () {
if(!SystemInfo.supportsImageEffects)
{
enabled = false;
return;
}
if(null != 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);
}
void OnDisable()
{
if (curMaterial)
DestroyImmediate (curMaterial);
}
}
运行测试
原效果
Brichtness=0.5 Saturation=2.0 Contrast=1.75
Brichtness=1.64 Saturation=0.0 Contrast=0.57