Unity Shader 学习笔记(十四)使用屏幕特效实现亮度饱和度和对比度

Unity Shader 学习笔记(十四)使用屏幕特效实现亮度饱和度和对比度

1.创建一个场景
2.场景中随便添加一些GameObject
3,添加方向光源
4.创建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)
			{
				// Increase or decrease theese values to 
				//adjust r, g and b color channels seperately
				float AvgLumR = 0.5;
				float AvgLumG = 0.5;
				float AvgLumB = 0.5;
				
				//Luminance coefficients for getting lumoinance from the image
				float3 LuminanceCoeff = float3(0.2125, 0.7154, 0.0721);
				
				//Operation for brightness
				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;
			}

			fixed4 frag(v2f_img i) : COLOR
			{
				//Get the colors from the RenderTexture and the uv's
				//from the v2f_img struct
				fixed4 renderTex = tex2D(_MainTex, i.uv);
				
				//Apply the Brughtness, saturation, contrast operations
				renderTex.rgb = ContrastSaturationBrightness(renderTex.rgb, 
															_BrightnessAmount, 
															_satAmount, 
															_conAmount);
				
				return renderTex;
			}
	
			ENDCG
		}
	} 
	FallBack off
}


5.创建BSC_ImageEffect.cs脚本,并将该脚本绑定到摄像机
using UnityEngine;
using System.Collections;

[ExecuteInEditMode]
public class BSC_ImageEffect : MonoBehaviour 
{

	#region Variables
	public Shader curShader;
    //当前亮度
	public float brightnessAmount = 1.0f;
    //当前饱和度
	public float saturationAmount = 1.0f;
    //当前对比度
	public float contrastAmount = 1.0f;
	private Material curMaterial;
	#endregion
	
	#region Properties
	Material material
	{
		get
		{
			if(curMaterial == null)
			{
				curMaterial = new Material(curShader);
				curMaterial.hideFlags = HideFlags.HideAndDontSave;
			}
			return curMaterial;
		}
	}
	#endregion
	
	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);
	}
	
	void OnDisable()
	{
		if(curMaterial)
		{
			DestroyImmediate(curMaterial);
		}
	}
}


6,摄像机的Inspector面板中为 Cur Shader赋值为刚才创建的shader

7.分别调节 brightnessAmount , saturationAmount , contrastAmount 的值来修改亮度,饱和度和对比度


你可能感兴趣的:(Unity Shader 学习笔记(十四)使用屏幕特效实现亮度饱和度和对比度)