(八)图像处理

1.前言

本篇浅谈一下图像处理包括调节亮度、灰度处理以及模糊处理。图像处理一般用于平面模型特殊要求处理,但主要用于屏幕后处理,屏幕后处理一般采用OnRenderImage方法进行。主要图像处理参考文献为六种灰度计算方法文章

2.图像处理

2.1 亮度

亮度就是让图像看起来更亮,处理比较简单,即颜色值添加一个亮度系数,调节亮度系数越大,图像越亮,最终结果为屏幕全为白色。

                //Brightness
				fixed3 finalColor = color.rgb * _Brightness;

2.2 灰度

灰度计算方法有很多种,可以将颜色的rgb值平均化处理,也可以对rgb分量添加不同的权重进行计算,甚至可以采用单一色道,如下所示:

				//Grey
				//fixed luminance=(color.r+color.g+color.b)/3;
				//fixed luminance=color.r*0.2+color.g*0.78+color.b*0.02;
				//fixed luminance=color.r*0.3+color.g*0.59+color.b*0.11;

				//单一色道g	
				fixed luminance=color.r;
				fixed3 finalColor = fixed3(luminance,luminance,luminance);

可以通过Lerp控制灰度的程度:

				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);

2.2.1 完整代码

// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'

Shader "LL/ImageEffects" {
   
	Properties {
   
		_MainTex ("Base (RGB)", 2D) = "white" {
   }
		_Brightness ("Brightness", Float) = 1
		_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"  
			  
			sampler2D _MainTex;  
			half _Brightness;
			half _Saturation;
			half _Contrast;
			  
			struct v2f {
   
				float4 pos : SV_POSITION;
				half2 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 color = tex2D(_MainTex, i.uv);  

//fixed3 rgb=color.rgb*_Brightness;
				 

                //Brightness
				//fixed3 finalColor = color.rgb * _Brightness;

				//Grey
				//fixed luminance=(color.r+color.g+color.b)/3;
				//fixed luminance=color.r*0.2+color.g*0.78+color.b*0.02;
				//fixed luminance=color.r*0.3+color.g*0.59+color.b*0.11;

				//单一色道g	
				fixed luminance=color.r;
				fixed3 finalColor = fixed3(luminance,luminance,luminance);


				/*
				// Apply saturation
				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);
				
				// Apply contrast
				fixed3 avgColor = fixed3(0.5, 0.5, 0.5);
				finalColor = lerp(avgColor, finalColor, _Contrast);
				
				return fixed4(finalColor, renderTex.a);  */
				return fixed4(finalColor, color.a);
			}  
			  
			ENDCG
		}  
	}
	
	Fallback Off
}

2.3 模糊处理

图像模糊处理也比较简单,只要取临近的几个像素进行平均化即可,即可以完全相加平均,也可以通过权重计算,赋予不同位置的像素不同的权重。

2.3.1 简单处理

由于与某个像素相邻的像素有8个,但是由于是模糊处理,没必要全部用到。一般只取上下左右四个像素进行模糊处理。此处值采用上下两个像素进行模糊处理,采用权重_Weight控制每个像素权重。当其值为0.33时为绝对平均化处理,当其值为1时不做模糊处理。代码如下:

Shader "LL/Blur" {
   
	Properties {
   
		_MainTex ("Base (RGB)", 2D) = "white" {
   }
		_BlurSize ("Blur Size", Float) = 1.0
		_Weight("Weight",Float)=1
	}
	SubShader {
   
		Pass {
     
			ZTest Always Cull Off ZWrite Off
			
			CGPROGRAM  
			#pragma vertex vert  
			#pragma fragment frag  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值