UnityShader 屏幕特效入门前

1.
OnRenderImage(RenderTexture src ,RenderTexture dest):
在所有渲染完成后得到屏幕图像基础上对图像进行后期处理。
它允许您通过使用基于着色器的过滤器进行处理来修改最终图像。传入的图像是source渲染纹理
即是场景图片,destination是目标渲染纹理。我们可以对source纹理通过shader相应处理输出destination,这个函数就是下面。

2.
public static void Blit(Texture source, RenderTexture dest);
public static void Blit(Texture source, RenderTexture dest, Material mat, int pass = -1);
public static void Blit(Texture source, Material mat, int pass = -1);
参数src对应了源纹理,在屏幕后期处理中,这个参数代表上一次的渲染纹理,或者是当前屏幕纹理。
dest 代表目标渲染纹理,如果为空的话,会直接将结果渲染在屏幕,参数,mat到变要进行后期处理
的材质。该材质的_MainTexture接收参数source的赋值。最后Pass,默认是-1,代表shader依次调
用Shader里面的Pass.否则按给定的索引进行调用。

3.
—-下面是屏幕常见的基础类:
注释如代码里面

using UnityEngine;
using System.Collections;

//非运行模式也触发该脚本。
[ExecuteInEditMode]
//屏幕后处理特效一般都需要绑定在摄像机上  
[RequireComponent (typeof(Camera))]
public class PostEffectsBase : MonoBehaviour {

    // Called when start
    //用来检测是否支持屏幕特效,不支持disbale这个被继承的子类。
    protected void CheckResources() {
        bool isSupported = CheckSupport();

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

    // Called in CheckResources to check support on this platform
    protected bool CheckSupport() {
        if (SystemInfo.supportsImageEffects == false || SystemInfo.supportsRenderTextures == false) {
            Debug.LogWarning("This platform does not support image effects or render textures.");
            return false;
        }

        return true;
    }

    // Called when the platform doesn't support this effect
    //if it Can't Be Supported ,this script will be disable . 
    protected void NotSupported() {
        enabled = false;
    }

    protected void Start() {
        CheckResources();
    }
    //用来检测材质和支持shader.
    // Called when need to create the material used by this effect
    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;
        }
    }
}

下一篇简单介绍屏幕模糊特效,以及上面类如何使用。

你可能感兴趣的:(Unity3DShader,Unity,Shader)