欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

屏幕后处理-饱和度,亮度,对比度处理

程序员文章站 2022-07-14 16:51:54
...

效果

亮度:

屏幕后处理-饱和度,亮度,对比度处理
屏幕后处理-饱和度,亮度,对比度处理

饱和度

屏幕后处理-饱和度,亮度,对比度处理

屏幕后处理-饱和度,亮度,对比度处理

对比度

屏幕后处理-饱和度,亮度,对比度处理
屏幕后处理-饱和度,亮度,对比度处理

思路

通过后处理技术,在图片渲染完成后,c#获取得到相机所渲染的区域的图像,通过参数在shader中对其rgb值进一步进行显式处理,将处理的结果再生成目标图形,最后显示出来。

实现 shader

Shader "Custom/BritnessSiturationConstract" {
	Properties {
        _MainTex("_MainTex",2D) = "white" {}
        _Brightness("_Brightness", Float) = 1.0
        _Saturation("_Saturation",Float) = 1
        _Contrast("_Contrast", Float)  = 1
	}
	SubShader {
		Pass{
		    ZTest Always Cull Off ZWrite Off

		    CGPROGRAM
		    
		    #pragma vertex vert
		    #pragma fragment frag
		    #include "UnityCG.cginc"
		    #include "Lighting.cginc"
		    
		    
		    sampler2D _MainTex;
		    float4 _MainTex_ST;
		    half _Brightness;
		    half _Saturation;
		    half _Contrast;
		    
		    
		    struct v2f {
		        float4 pos : SV_POSITION;
		        float2 uv : TEXCOORD0;
		    };
		    
		    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);
		        
		        //亮度计算, 通过rgb乘以一个相同的系数,达到亮度的统一变化
		        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);
		        
		        //计算对比度,  通过 0.5的插值变化
		        fixed3 avgColor = fixed3 (0.5,0.5,0.5);
		        finalColor = lerp(avgColor,finalColor,_Contrast);
		        
		        return fixed4(finalColor,renderTex.a);
		        
		    };
		    
		    
		    
		    
		     
		    
		    
		    
		    ENDCG
		
		}
	}
	FallBack Off
}


实现 C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SocialPlatforms;

public class BritnessSiturationConstract : PostEffectBaseTest
{

	public Shader shader;
	protected Material constMaterial;

	public Material material
	{
		get
		{
			constMaterial = CheckShaderAndCreateMaterial(shader, constMaterial);
			return constMaterial;
		}
	}
	[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