Unity Shader 学习笔记(十四)使用屏幕特效实现亮度饱和度和对比度
2016-02-16 15:37
183 查看
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 的值来修改亮度,饱和度和对比度
相关文章推荐
- 【Unity3D】 Unity3D中自带事件函数的执行顺序
- 【Unity】unity 枚举分隔符
- Unity3D 封装自己的Debug类库dll
- 开始写博客啦!!
- unity3d 导出NGUI图集中的图片
- DrawCall的简单介绍
- 【Unity插件】NGUI核心组件之UICamera
- 【Unity插件】NGUI核心组件之UIAtlas
- 【Unity插件】NGUI核心组件之UIAnchor
- 【Unity插件】NGUI核心组件之UIPanel
- Unity3D在后台打包的方法
- Unity中HideInInspector和SerializeField
- Unity-Animator深入系列---fullPathHash和shortNameHash
- Unity3d UGUI序列帧动画
- Unity3D 播放MP4 的几种状态
- Unity编辑器扩展
- Unity3d中加载资源prefab的方法
- Unity Shader 学习笔记(十三)表面着色器的顶点动画
- Unity3d 怪物死亡燃烧掉效果
- Unity按钮事件