Unity Shader入门精要之 screen post-processing effect

本篇记录了学习Unity Shader入门精要的屏幕后处理的一些知识点。

OnRenderImage(RenderTexture src, RenderTexture dest)

以上函数是Unity提供的接口,当前渲染得到图像存储在 src 中,在函数中通过一些操作后将处理后里的图像存储在 dest 中显示到屏幕中。

Graphics.Blit(Texture source, RenderTexture dest, Material mat, int pass)

该函数 source 传给 mat 使用的Shader中名为 _MainTex 的属性, mat 使用的 shader 会对屏幕做各种处理,然后传给 dest, pass 默认为 -1 ,若为 - 1 会依次调用shader中的所有Pass,否则只会调用指定的Pass。

判断当前设备是否支持屏幕特效:

1 UnityEngine.SystemInfo.supportsImageEffects

判断当前设备是否支持某种格式的渲染纹理:

1 UnityEngine.SystemInfo.SupportsRenderTextureFormat(RenderTextureFormat.ARGB32)

使用一个Shader之前需要判断当前设备的显卡是否支持该Shader,判断方式:

shader.isSupported 

1、调整屏幕的亮度(Brightness)、饱和度(Saturation)、对比度(Contrast)

调整亮度 即 将原颜色rgb分量的乘以亮度系数(0-1)

调整饱和度 即 在饱和度为0的颜色值与原颜色之间插值(0-1)

调整对比度 即 在对比度为0的颜色值与原颜色之间插值(0-1)

以下是本人对案例一些片元着色器代码的个人理解,纯属学习之用

扫描二维码关注公众号,回复: 5346179 查看本文章
 1 fixed4 frag(v2f i) : SV_Target 
 2 {
 3     fixed4 renderTex = tex2D(_MainTex, i.uv); //获取对原屏幕图像的采样结果
 4                   
 5     // Apply brightness
 6     //调整亮度 即 将原颜色乘以亮度系数(0-1)即可
 7     fixed3 finalColor = renderTex.rgb * _Brightness;
 8                 
 9     // Apply saturation
10     //亮度值 即 每个颜色分量乘以对应的特定的亮度系数再相加,注意亮度值是一个特定的概念0
11     //r- 0.2125   g-0.7154   b-    0.0721
12     fixed luminance = 0.2125 * renderTex.r + 0.7154 * renderTex.g + 0.0721 * renderTex.b;
13     //创建饱和度为0的颜色值,即rgb三个颜色分量值都为亮度值 
14     fixed3 luminanceColor = fixed3(luminance, luminance, luminance);
15     //在饱和度为0的颜色值与实际的颜色之间插值(0-1),即可得到希望的饱和度颜色
16     finalColor = lerp(luminanceColor, finalColor, _Saturation);
17                 
18     // Apply contrast
19     //创建对比度为0的颜色值,即rgb三个颜色分量的值相等
20     fixed3 avgColor = fixed3(0.5, 0.5, 0.5);
21     //在对比度为0的颜色值与实际的颜色之间插值(0-1),即可得到希望的对比度颜色
22     finalColor = lerp(avgColor, finalColor, _Contrast);
23                 
24     return fixed4(finalColor, renderTex.a);  
25 }

猜你喜欢

转载自www.cnblogs.com/luguoshuai/p/10447049.html