效果
c#代码
using UnityEngine; public class MyBrightnessSaturationAndContrast : MonoBehaviour { public Shader m_Shader; public Material m_Material; [Range(0.0f, 3.0f)] public float m_brightness = 1.0f; [Range(0.0f, 3.0f)] public float m_saturation = 1.0f; [Range(0.0f, 3.0f)] public float m_contrast = 1.0f; void Start() { if (false == SystemInfo.supportsImageEffects) { Debug.LogWarning("This platform does not support image effects or render textures."); this.enabled = false; return; } if (null == m_Material) { if (null != m_Shader && m_Shader.isSupported) { m_Material = new Material(m_Shader); m_Material.hideFlags = HideFlags.DontSave; } } else if (m_Material.shader != m_Shader) { if (m_Shader.isSupported) //优先shader { m_Material = new Material(m_Shader); m_Material.hideFlags = HideFlags.DontSave; } } } void OnRenderImage(RenderTexture src, RenderTexture dest) { if (null != m_Material) { m_Material.SetFloat("_Brightness", m_brightness); m_Material.SetFloat("_Saturation", m_saturation); m_Material.SetFloat("_Contrast", m_contrast); Graphics.Blit(src, dest, m_Material); } else { Graphics.Blit(src, dest); } } }
shader
Shader "My/PostEff/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 #include "UnityCG.cginc" sampler2D _MainTex; float4 _MainTex_ST; half _Brightness; half _Saturation; half _Contrast; struct appdata { float4 vertex : POSITION; float2 uv : TEXCOORD0; }; struct v2f { float2 uv : TEXCOORD0; float4 vertex : SV_POSITION; }; v2f vert(appdata v) { v2f o; o.vertex = UnityObjectToClipPos(v.vertex); o.uv = TRANSFORM_TEX(v.uv, _MainTex); return o; } fixed4 frag(v2f i) : SV_Target { fixed4 renderTex = tex2D(_MainTex, i.uv); //亮度 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); //饱和度为0的颜色值 finalColor = lerp(luminanceColor, finalColor, _Saturation); //饱和度为0~某个饱和度 //对比度 fixed3 avgColor = fixed3(0.5, 0.5, 0.5); //对比度为0的颜色值 finalColor = lerp(avgColor, finalColor, _Contrast); return fixed4(finalColor, renderTex.a); } ENDCG } } Fallback Off }
标签:Material,Shader,finalColor,饱和度,对比度,renderTex,后处理,public From: https://www.cnblogs.com/sailJs/p/18098458