# 【屏幕后处理】

摄像机在把画面渲染成图片后,显示在显示器之前可以进行图片后处理效果,可以使用自定义shader处理,十分灵活。

1.在摄像机添加自定义脚本ScreenAfterEffect.cs

using UnityEngine;

[ExecuteInEditMode()]
public class ScreenAfterEffect : MonoBehaviour
{
    public Material material;
    void Start()
    {
        if(material == null || material.shader == null || material.shader.isSupported == false)
        {
            enabled = false;
            return;
        }
    }

    private void OnRenderImage(RenderTexture src, RenderTexture dest) {
        Graphics.Blit(src, dest,material,0);
    }
}

2.添加 Material 对应的 Shader 文件 ScreenAfterEffectShader.shader

Shader "Hidden/ScreenAfterEffectShader"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
    }
    SubShader
    {
        // No culling or depth
        Cull Off ZWrite Off ZTest Always

        Pass
        {
            CGPROGRAM
            #pragma vertex vert_img
            #pragma fragment frag

            #include "UnityCG.cginc"

            sampler2D _MainTex;

            fixed4 frag (v2f_img i) : SV_Target
            {
                fixed4 col = tex2D(_MainTex, i.uv);
                // just invert the colors
                col.rgb = 1 - col.rgb;
                return col;
            }
            ENDCG
        }
    }
}