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 完整代码
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
#include "UnityCG.cginc"
sampler2D _MainTex;
half _Brightness;
half _Saturation;
half4 _MainTex_TexelSize;
half _Weight;
float _BlurSize;
struct v2f {
float4 pos : SV_POSITION;
half2 uv[9]: TEXCOORD0;
};
v2f vert(appdata_img v) {
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
half2 uv = v.texcoord;
o.uv[0] = uv;
o.uv[1] = uv + float2(0.0, _MainTex_TexelSize.y * 1.0) * _BlurSize;
o.uv[2] = uv - float2(0.0, _MainTex_TexelSize.y * 1.0) * _BlurSize;
return o;
}
fixed4 frag(v2f i) : SV_Target {
fixed3 c = tex2D(_MainTex, i.uv[0]).rgb * _Weight;
half w=(1-_Weight)/2;
for (int index = 1; index < 3; index++) {
c += tex2D(_MainTex, i.uv[index]).rgb * w;
}
return fixed4(c, 1);
}
ENDCG
}
}
FallBack "Diffuse"
}
计算uv时乘以_BlurSize是为了提升效果,如果只是取最临近的像素,可能由于图像梯度不那么明显导致效果不明显,所以可以调节_BlurSize值取次临近的像素进行处理。
2.3.2 高斯处理
高斯处理就是取赋予像素特定的权重进行处理。像素选取时左右上下各取四个像素值进行计算,由于对称原因,所以像素权重只有三个值:0.4026, 0.2442, 0.0545。计算代码如下:
Shader "LL/Blur" {
Properties {
_MainTex ("Base (RGB)", 2D) = "white" {}
_BlurSize ("Blur Size", Float) = 1.0
}
SubShader {
Pass {
ZTest Always Cull Off ZWrite Off
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
sampler2D _MainTex;
half _Brightness;
half _Saturation;
half4 _MainTex_TexelSize;
half _Contrast;
float _BlurSize;
struct v2f {
float4 pos : SV_POSITION;
half2 uv[9]: TEXCOORD0;
};
v2f vert(appdata_img v) {
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
half2 uv = v.texcoord;
o.uv[0] = uv;
o.uv[1] = uv + float2(0.0, _MainTex_TexelSize.y * 1.0) * _BlurSize;
o.uv[2] = uv - float2(0.0, _MainTex_TexelSize.y * 1.0) * _BlurSize;
o.uv[3] = uv + float2(0.0, _MainTex_TexelSize.y * 2.0) * _BlurSize;
o.uv[4] = uv - float2(0.0, _MainTex_TexelSize.y * 2.0) * _BlurSize;
o.uv[5] = uv + float2(_MainTex_TexelSize.x * 1.0, 0.0) * _BlurSize;
o.uv[6] = uv - float2(_MainTex_TexelSize.x * 1.0, 0.0) * _BlurSize;
o.uv[7] = uv + float2(_MainTex_TexelSize.x * 2.0, 0.0) * _BlurSize;
o.uv[8] = uv - float2(_MainTex_TexelSize.x * 2.0, 0.0) * _BlurSize;
return o;
}
fixed4 frag(v2f i) : SV_Target {
float weight[3] = {0.4026, 0.2442, 0.0545};
fixed3 c = tex2D(_MainTex, i.uv[0]).rgb * weight[0];
for (int index = 1; index < 3; index++) {
c