【Unity Shader入门练习】调整屏幕的亮度、饱和度和对比度

时间:2021-11-30 04:37:05
  • 0x00 序言
    本文是《Unity Shader入门精要》读书笔记,最近接触到项目中用到了模糊处理,经过查阅资料,发觉模糊处理是屏幕后处理技术中的一种,就从屏幕后处理技术开始学习。[使用的Unity版本:5.6.1f1]

  • 0x01效果图
    【Unity Shader入门练习】调整屏幕的亮度、饱和度和对比度

  • 0x02 屏幕后处理的定义
    屏幕后处理,通常指的是在渲染完整个场景得到屏幕图像后,再对这个图像一系列操作,实现各种屏幕特效。
    想要实现屏幕后处理的基础在于得到渲染后的屏幕图像,即抓取屏幕,而Unity为我们提供了这样一个方便的接口——OnRenderImage函数:

MonoBehaviour.OnRenderImage(RenderTexture src, RenderTexture dest)
  • 0x03 后处理过程
    1.需要在摄像机中添加一个用于屏幕后处理的脚本。在这个脚本中,我们会实现OnRenderImage函数来获取当前屏幕的渲染纹理;
    2.调用Graphics.Blit函数使用特定的Unity Shader来对当前图像进行处理;
    3.把返回的渲染纹理显示到屏幕上。

  • 0x04 后处理脚本代码

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

[ExecuteInEditMode] //编辑器状态下可以直接看到运行结果
[RequireComponent (typeof(Camera))]

public class BrightnessSaturationAndContrast : MonoBehaviour {

protected void CheckResources()
{
bool isSupported = CheckSupport();

if (isSupported == false)
{
NotSupported();
}
}

protected bool CheckSupport()
{
if (SystemInfo.supportsImageEffects == false)
{
Debug.LogWarning("This platform does not support image effects or render textures.");
return false;
}
return true;
}

protected void NotSupported()
{
enabled = false;
}

void Start () {
CheckResources();
}

//第一个参数指定了特效需要使用的Shader,第二个参数则是用于后期处理的材质。
protected Material CheckShaderAndCreateMaterial(Shader shader, Material material)
{
if(shader == null)
{
return null;
}

if (shader.isSupported && material && material.shader == shader)
{
return material;
}

if (!shader.isSupported)
{
return null;
}
else
{
material = new Material(shader);
material.hideFlags = HideFlags.DontSave;
if (material)
return material;
else
return null;
}
}

public Shader briSatConShader;
private Material briSatConMaterial;
public Material material
{
get
{
briSatConMaterial = CheckShaderAndCreateMaterial(briSatConShader, briSatConMaterial);
return briSatConMaterial;
}
}

[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);
}
}
}
  • 0x05 Shader代码
// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'

Shader "Brightness Saturation And Contrast"{
//声明本例使用的各个属性
Properties{
_MainTex("Base (RGB)", 2D) = "white" {}
_Brightness("Brightness", Float) = 1
_Saturation("Saturation", Float) = 1
_Contrast("Contrast", Float) = 1
}

//定义用于屏幕后处理的pass
SubShader{
Pass{
//关闭深度写入
//为了防止它“挡住”在其后面被渲染的物体。
ZTest Always Cull Off ZWrite Off

CGPROGRAM
#pragma vertex vert
#pragma fragment frag

#include "UnityCG.cginc"

//在CG代码块中声明对应的变量
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 renderTex = tex2D(_MainTex, i.uv);
//Apply brightness
fixed3 finalColor = renderTex.rgb * _Brightness;

//Apply saturation
//['luːmɪn(ə)ns]亮度
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);
}

ENDCG
}
}

Fallback Off
}