目录
2.新建一个脚本(BrightnessSaturationAndContrast.cs)挂载到摄像机上
3.新建一个Shader(BrightnessSaturationAndContrast.cs)
4.在创建中放置一个 Quad,将准备好的图片拖到Quad上
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
-
- public class BrightnessSaturationAndContrast : PostEffectsBase
- {
- public Shader briSatConShader;
-
- private Material briSatConMaterial;
-
- public Material material
- {
- get
- {
- briSatConMaterial = CheckShaderAndCreateMaterial(briSatConShader, briSatConMaterial);
- return briSatConMaterial;
- }
- }
-
- // 调整亮度
- [Range(0.0f, 3.0f)] public float brightness = 1.0f;
-
- //调整饱和度
- [Range(0.0f, 3.0f)] public float saturation = 1.0f;
-
- //调整对比度
- [Range(0.0f, 3.0f)] public float contrast = 1.0f;
-
- void OnRenderImage(RenderTexture src, RenderTexture dest)
- {
- if (material!=null)
- {
- //把修改的材质的参数传递给材质
- material.SetFloat("_Brightness",brightness);
- material.SetFloat("_Saturation",saturation);
- material.SetFloat("_Contrast",contrast);
- //对材质进行处理
- Graphics.Blit(src,dest,material);
- }
- else
- {
- Graphics.Blit(src,dest);
- }
- }
- }
- Shader "Unlit/Chapter12-BrightnessSaturationAndContrast"
- {
- Properties
- {
- _MainTex("Base(RGB)",2D)="white"{}
- _Brightness("Brightness",Float)=1
- _Saturation("Saturation",Float)=1
- _Contrast("Contrast",Float)=1
- }
- SubShader
- {
- Tags { "RenderType"="Opaque" }
- LOD 100
-
- Pass
- {
- ZTest Always Cull Off ZWrite Off
-
- CGPROGRAM
- #pragma vertex vert
- #pragma fragment frag
- // make fog work
- #pragma multi_compile_fog
-
- #include "UnityCG.cginc"
-
-
- struct v2f
- {
- float2 uv : TEXCOORD0;
- float4 pos : SV_POSITION;
- };
-
- sampler2D _MainTex;
- half _Brightness;
- half _Saturation;
- half _Contrast;
-
- v2f vert (appdata_img v)
- {
- v2f o;
- o.pos = UnityObjectToClipPos(v.vertex);
- o.uv = v.texcoord;
- return o;
- }
-
- fixed4 frag (v2f i) : SV_Target
- {
- fixed4 renderTex=tex2D(_MainTex,i.uv);
- //_Brightness调整亮度
- fixed3 finalColor=renderTex.rgb*_Brightness;
- //计算亮度值
- fixed luminance=0.2125*renderTex.r+0.7154*renderTex.g+0.0721*renderTex.b;
- fixed3 luminanceColor=fixed3(luminance,luminance,luminance);
- //计算饱和度颜色
- finalColor=lerp(luminanceColor,finalColor,_Saturation);
- //对比度
- fixed3 avgColor=fixed3(0.5,0.5,0.5);
- finalColor=lerp(avgColor,finalColor,_Contrast);
- return fixed4(finalColor,renderTex.a);
- }
- ENDCG
- }
- }
- Fallback Off
- }
原图