屏幕特效——实现亮度、饱和度、对比度

作者:追风剑情 发布于:2016-7-17 11:08 分类:Shader

色调:    指画面上表现情感的设色及其浓淡。如:暖色调、冷色调。
饱和度: 颜色纯度,越纯越鲜艳,纯的颜色属于高度饱和,灰色属于完全不饱和。
亮度:    光线强弱决定。
对比度: 画面上的明暗差

BSC_Effect.shader


  1. Shader "Custom/BSC_Effect" {
  2. Properties {
  3. _MainTex ("Base (RGB)", 2D) = "white" {}
  4. //亮度值
  5. _BrightnessAmount ("Brightness Amount", Range(0.0, 1)) = 1.0
  6. //饱和度
  7. _satAmount ("Saturation Amount", Range(0.0, 1)) = 1.0
  8. //对比度
  9. _conAmount ("Contrast Amount" , Range(0.0, 1)) = 1.0
  10. }
  11. SubShader {
  12. Pass
  13. {
  14. CGPROGRAM
  15. #pragma vertex vert_img
  16. #pragma fragment frag
  17. #pragma fragmentoption ARB_precision_hint_fastest
  18. #include "UnityCG.cginc"
  19. uniform sampler2D _MainTex;
  20. fixed _BrightnessAmount;
  21. fixed _satAmount;
  22. fixed _conAmount;
  23. float3 ContrastSaturationBrightness(float3 color, float brt, float sat, float con)
  24. {
  25. float AvgLumR = 0.5;
  26. float AvgLumG = 0.5;
  27. float AvgLumB = 0.5;
  28. //当前图像的全局亮度值,这个系数是由CIE颜色匹配函数得来的,并成为了行业标准。
  29. float3 LuminanceCoeff = float3(0.2125, 0.7154, 0.0721);
  30. //把当前颜色值和这些亮度系数进行点积运算得到图像的全局亮度。
  31. float3 AvgLumin = float3(AvgLumR, AvgLumG, AvgLumB);
  32. float3 brtColor = color * brt;
  33. float intensityf = dot(brtColor, LuminanceCoeff);
  34. float3 intensity = float3(intensityf, intensityf, intensityf);
  35. //得到亮度值以后,再用一组lerp函数从亮度值的操作开始混合运算。
  36. //然后用原始图像乘上函数传递过来的亮度值。
  37. float3 satColor = lerp(intensity, brtColor, sat);
  38. float3 conColor = lerp(AvgLumin, satColor, con);
  39. return conColor;
  40. }
  41. fixed4 frag(v2f_img i) : COLOR
  42. {
  43. fixed4 renderTex = tex2D(_MainTex, i.uv);
  44. renderTex.rgb = ContrastSaturationBrightness(renderTex.rgb,
  45. _BrightnessAmount,
  46. _satAmount,
  47. _conAmount);
  48. return renderTex;
  49. }
  50. ENDCG
  51. }
  52. }
  53. FallBack "Diffuse"
  54. }


TestRenderImage.cs


  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. [ExecuteInEditMode]
  5. public class TestRenderImage : MonoBehaviour {
  6.  
  7. public Shader curShader;
  8. private Material curMaterial;
  9. public float brightnessAmount = 1.0f;
  10. public float saturationAmount = 1.0f;
  11. public float contrastAmount = 1.0f;
  12.  
  13. Material material
  14. {
  15. get
  16. {
  17. if(curMaterial == null)
  18. {
  19. curMaterial = new Material(curShader);
  20. curMaterial.hideFlags = HideFlags.HideAndDontSave;
  21. }
  22. return curMaterial;
  23. }
  24. }
  25.  
  26. void Start () {
  27. if(!SystemInfo.supportsImageEffects)
  28. {
  29. enabled = false;
  30. return;
  31. }
  32.  
  33. if(null != curShader && !curShader.isSupported)
  34. {
  35. enabled = false;
  36. }
  37. }
  38.  
  39. //对屏幕后期处理
  40. void OnRenderImage(RenderTexture sourceTexture, RenderTexture destTexture)
  41. {
  42. if (curShader != null)
  43. {
  44. material.SetFloat("_BrightnessAmount", brightnessAmount);
  45. material.SetFloat("_satAmount", saturationAmount);
  46. material.SetFloat("_conAmount", contrastAmount);
  47.  
  48. Graphics.Blit (sourceTexture, destTexture, material);
  49. } else {
  50. Graphics.Blit (sourceTexture, destTexture);
  51. }
  52. }
  53.  
  54. void Update () {
  55. brightnessAmount = Mathf.Clamp (brightnessAmount, 0.0f, 2.0f);
  56. saturationAmount = Mathf.Clamp (saturationAmount, 0.0f, 2.0f);
  57. contrastAmount = Mathf.Clamp (contrastAmount, 0.0f, 3.0f);
  58. }
  59.  
  60. void OnDisable()
  61. {
  62. if (curMaterial)
  63. DestroyImmediate (curMaterial);
  64. }
  65. }


运行测试

原效果

1111.jpg

Brichtness=0.5  Saturation=2.0  Contrast=1.75

05-20-175.jpg

Brichtness=1.64  Saturation=0.0  Contrast=0.57

164-00-057.jpg

标签: Shader

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号