["Tips for the Development of Unity Shaders and Screen Effects"] Learning and Organizing: About Screen Effects [1]

Screen special effects are also called post-production special effects. By capturing the entire screen and rendering it, you can achieve effects such as bloom effects, motion blur, HDR effects, depth of field effects, etc.
Since we need to render the Unity camera screen, we need to hang a special script on the camera when making screen effects, and pass the rendering texture to the shader through this script.

Common script

First record the basic structure of the script.

[ExecuteInEditMode] //添加此特性后,这个脚本就可以在编辑状态下运行
public class MyTestRenderImageBase : MonoBehaviour
{
    //当前的着色器
    public Shader curShader = null;
    //当前的材质球
    private Material curMaterial = null;
    private Material CurMaterial
    {
        get
        {
            if(curMaterial == null)
            {
                //如果没有材质球,则创建一个使用curShader的材质球
                curMaterial = new Material(curShader);
                curMaterial.hideFlags = HideFlags.HideAndDontSave;
            }
            return curMaterial;
        }
    }

    private void Start()
    {
        //容错处理
        if(!SystemInfo.supportsImageEffects)
        {
            enabled = false;
            return;
        }
        if(!curShader && !curShader.isSupported)
        {
            enabled = false;
        }
    }

    /// <summary>
    /// Unity提供的屏幕特效处理接口
    /// </summary>
    /// <param name="source">源纹理,通常就是当前屏幕的渲染纹理,或者上一步处理后得到的渲染纹理</param>
    /// <param name="destination">目标渲染纹理</param>
    private void OnRenderImage(RenderTexture source, RenderTexture destination)
    {
        if(curShader)
        {
        //如果着色器存在,就是用着色器渲染当前纹理
            Graphics.Blit(source, destination, CurMaterial);
        }
        else
        {
            //着色器不存在时,就直接将当前纹理覆盖传递给目标纹理
            Graphics.Blit(source, destination);
        }
    }

    private void OnDisable()
    {
        if(curMaterial)
        {
            //由于使用了HideFlags.HideAndDontSave标签,所以必须手动销毁此对象
            DestroyImmediate(curMaterial);
        }
    }
}

As can be seen from the above code, the general structure is to obtain a Shade→get the shader→use this shader to render the current screen.
Start()The code in it is basically fault-tolerant.
For example, use it SystemInfo.supportsImageEffectsto detect whether the system supports screen special effects processing.
Use it !curShader && !curShader.isSupportedto determine whether the current Shader is available.

Graphics.BlitThis function is responsible for using a specific Shader to render the current image.
The following types may be used:

public static void Blit(Texture source, RenderTexture dest);
public static void Blit(Texture source, RenderTexture dest, Material mat);
public static void Blit(Texture source, Material mat, int pass = -1);

The passdefault value is -1, which means that all Passes in the Shader will be called in sequence, otherwise only the Pass at the specified index will be called.

It should be noted that for some complex screen effects, we may need to call Graphics.Blitthe function multiple times to process the output result of the previous step.

Other screen special effects processing scripts are basically modified based on this script.

Guess you like

Origin blog.csdn.net/EverNess010/article/details/79833626