大家好,我是阿赵。
  之前介绍过使用PostProcessing来做屏幕后处理效果。我们不一定要用PostProcessing来做后处理效果。
  PostProcessing功能强大,比如不同的layer控制不同的屏幕效果,比如可以使用PostProcessVolume的非全局效果达到某个范围内有过渡性的后处理效果。但如果我们并没有用到这些效果,只是单纯的想在自己需要的时候,添加一个指定摄像机的屏幕效果,那么其实选择还有很多,比如Unity本身提供了Graphics图形接口,还有CommandBuffer相关的方法,也是可以直接制作屏幕效果的。
  从PostProcessing的实现原理来说,他其实也是使用了CommandBuffer来实现,只是对它进行了封装。
  下面来介绍一下CommandBuffer。

一、Graphics和CommandBuffer

  在PostProcessing出现之前,我们同样可以制作屏幕后处理效果,一般的做法是:
  在OnRenderImage声明周期方法里面,通过Graphics.Blit(source, destination, material)方法,对摄像机传入的屏幕渲染结果,通过自己指定的材质球里面的shader处理,得到一张新的屏幕渲染效果,然后显示在屏幕上面。
  这里出现了Graphics。
  Graphics是Unity提供的图形绘制接口,除了Blit方法,还有很多其他的绘制方法,比如CopyTexture、DrawMesh、DrawTexture等。
  然后看CommandBuffer,它也提供了CommandBuffer.Blit方法,也提供了DrawMesh之类的方法。其实很多人到了这一步就开始有点困惑了。Graphics和CommandBuffer有什么区别呢?为什么都提供差不多的方法呢?什么时候该用Graphics,什么时候该用CommandBuffer呢?
  虽然两者很相似,但他们两个代表的含义是不一样的。

1、Graphics

  当我们调用Graphics方法时,它都是直接生效的,或者下一帧就生效的。比如我们调用Graphics.Blit方法,它会立刻就把原图通过shader处理,输出一张新图。比如我们DrawMesh,它会在下一帧就把我们需要的网格模型渲染出来。如果用DrawMeshNow,它就会在同一帧立刻把网格模型渲染出来。它是一个实际执行的命令。

2、CommandBuffer

  CommandBuffer是一个对象,其实是一个命令的列表。我们可以创建一个CommandBuffer对象,然后把需要它做的事情添加到这个对象的命令列表里面。至于什么时候执行,是可以控制的。比如我们可以把这个列表命令添加到摄像机渲染的某一个过程中间,或者灯光渲染的某个过程中间执行。也可以调用Graphics.ExecuteCommandBuffer方法,立刻执行这些命令。

二、CommandBuffer的执行过程

1、正常的渲染过程

  如果我们打开Unity自带的FrameDebug工具,可以看到整个渲染的过程
比如在Deferred延迟渲染里面,过程如下图:
unity的CommandBuffer介绍-LMLPHP

如果是Forward前向渲染,过程会如下图
unity的CommandBuffer介绍-LMLPHP
unity的CommandBuffer介绍-LMLPHP
unity的CommandBuffer介绍-LMLPHP

  如果选中其中的步骤,可以看到该步骤是渲染了什么画面,一步一步的看这个过程,对我们理解渲染是很有帮助的。

2、插入CommandBuffer的过程

  通过添加CommandBuffer命令,我们可以做到的是,在上面的渲染过程的其中一个步骤,插入自己想要的渲染处理。
  比如我这里截取一段代码:

        cmd1 = new CommandBuffer();
        cmd1.name = "AzhaoDrawLightObj";

        cmd1.SetRenderTarget(rt1);
        cmd1.ClearRenderTarget(true, true, Color.clear);

        for (int i = 0; i < renders.Length; i++)
        {
            Renderer item = renders[i];
            if (item.gameObject.activeInHierarchy == false || item.enabled == false)
            {
                continue;
            }
            if(item.gameObject.layer == 8)
            {
                cmd1.DrawRenderer(item, whiteMat);
            }
            else
            {
                cmd1.DrawRenderer(item, blackMat);
            }
            
        }

        cmd1.Blit(rt1, rt2);
        for (int i = 0; i < iterations; i++)
        {
            blurMat.SetFloat("_BlurSize", 1.0f + i * blurSpread);

            cmd1.Blit(rt2, rt3, blurMat, 0); 

            cmd1.Blit(rt3, rt2, blurMat, 1); 

        }
        cmd1.Blit(rt2, rt1);
        comMat.SetTexture("_AddTex", rt1);
        cam.AddCommandBuffer(CameraEvent.BeforeImageEffects, cmd1);

  这里我创建了一个名字叫做AzhaoDrawLightObj的CommandBuffer命令列表,然后我打算在刚才2个球的中间,渲染一个会发光的Cube立方体,所以给这个叫做AzhaoDrawLightObj的CommandBuffer添加了很多命令。具体这些命令是干什么用的,现在不需要去深究,之后我会单独写文章说明。
这里只需要关注两段:
第一段:

cmd1 = new CommandBuffer();
cmd1.name = "AzhaoDrawLightObj";
cmd1.SetRenderTarget(rt1);
cmd1.ClearRenderTarget(true, true, Color.clear);

  这一段是创建了CommandBuffer,给它命名,然后设置目标渲染的RenderTexture,并且清理这张RenderTexture
第二段:

cam.AddCommandBuffer(CameraEvent.BeforeImageEffects, cmd1);

  这一句代码,cam是一个Camera摄像机,通过AddCommandBuffer方法,我把刚刚创建的CommandBuffer添加到摄像机上面。CameraEvent.BeforeImageEffects是指定这个CommandBuffer生效的时机。
再回头看FrameDebug的渲染过程:
unity的CommandBuffer介绍-LMLPHP

  会发现在渲染过程中,插入了BeforeImageEffects这个过程,里面就看到我刚刚添加的AzhaoDrawLightObj的过程了。
unity的CommandBuffer介绍-LMLPHP

  现在在两个球中间,就单独出现了一个会放过的立方体了。使用CommandBuffer是比较自由的,可以控制渲染单个物体,可以控制在某个时机处理特定的渲染,这些自由度,是使用固定的PostProcessing很难做得到的。
unity的CommandBuffer介绍-LMLPHP

  这时候可以在摄像机上面,看到有哪些CommandBuffer添加了。
  值得注意的是,给摄像机添加CommandBuffer,只需要在OnEnable生命周期添加一次,就能一直生效,不要在Update添加,不然会发现重复添加了非常多。
这是在Update里面调用添加的后果:
unity的CommandBuffer介绍-LMLPHP

既然是在OnEnable里面添加的,也要记得在OnDisable里面删除。

void OnDisable()
{
    if (cmd1!=null)
    {
        cmd1.Dispose();
        cmd1 = null;
    }
}

三、添加CommandBuffer的事件说明

  这里说明的是CameraEvent事件,在不同的渲染模式下,使用的CameraEvent事件是不一样的,千万不要用错了。除了CameraEvent还有LightEvent
  下面是Unity的官方文档里面对CameraEvent和LightEvent执行顺序的说明

1、Deferred rendering path

CameraEvent.BeforeGBuffer
Unity renders opaque geometry
CameraEvent.AfterGBuffer
Unity resolves depth.
CameraEvent.BeforeReflections
Unity renders default reflections, and Reflection Probe reflections.
CameraEvent.AfterReflections
Unity copies reflections to the Emissive channel of the G-buffer.
CameraEvent.BeforeLighting
Unity renders shadows. See LightEvent order of execution.
CameraEvent.AfterLighting
CameraEvent.BeforeFinalPass
Unity processes the final pass.
CameraEvent.AfterFinalPass
CameraEvent.BeforeForwardOpaque (only called if there is opaque geometry that cannot be rendered using deferred)
Unity renders opaque geometry that cannot be rendered with deferred rendering.
CameraEvent.AfterForwardOpaque (only called if there is opaque geometry that cannot be rendered using deferred)
CameraEvent.BeforeSkybox
Unity renders the skybox
CameraEvent.AfterSkybox
Unity renders halos.
CameraEvent.BeforeImageEffectsOpaque
Unity applies opaque-only post-processing effects.
CameraEvent.AfterImageEffectsOpaque
CameraEvent.BeforeForwardAlpha
Unity renders transparent geometry, and UI Canvases with a Rendering Mode of Screen Space - Camera
.
CameraEvent.AfterForwardAlpha
CameraEvent.BeforeHaloAndLensFlares
Unity renders lens flares.
CameraEvent.AfterHaloAndLensFlares
CameraEvent.BeforeImageEffects
Unity applies post-processing effects.
CameraEvent.AfterImageEffects
CameraEvent.AfterEverything
Unity renders UI Canvases with a Rendering Mode that is not Screen Space - Camera.

2、Forward rendering path

CameraEvent.BeforeDepthTexture
Unity renders depth for opaque geometry.
CameraEvent.AfterDepthTexture
CameraEvent.BeforeDepthNormalsTexture
Unity renders depth normals for opaque geometry.
CameraEvent.AfterDepthNormalsTexture
Unity renders shadows. See LightEvent order of execution.
CameraEvent.BeforeForwardOpaque
Unity renders opaque geometry.
CameraEvent.AfterForwardOpaque
CameraEvent.BeforeSkybox
Unity renders the skybox.
CameraEvent.AfterSkybox
Unity renders halos.
CameraEvent.BeforeImageEffectsOpaque
Unity applies opaque-only post-processing effects.
CameraEvent.AfterImageEffectsOpaque
CameraEvent.BeforeForwardAlpha
Unity renders transparent geometry, and UI Canvases with a Rendering Mode of Screen Space - Camera.
CameraEvent.AfterForwardAlpha
CameraEvent.BeforeHaloAndLensFlares
Unity renders lens flares.
CameraEvent.AfterHaloAndLensFlares
CameraEvent.BeforeImageEffects
Unity applies post-processing effects.
CameraEvent.AfterImageEffects
CameraEvent.AfterEverything
Unity renders UI Canvases with a Rendering Mode that is not Screen Space - Camera.

3、LightEvent

LightEvent.BeforeShadowMap
LightEvent.BeforeShadowMapPass
Unity renders all shadow casters for the current Pass
LightEvent.AfterShadowMapPass
Unity repeats the last three steps, for each Pass
LightEvent.AfterShadowMap
LightEvent.BeforeScreenSpaceMask
Unity gathers the shadow map into a screen space buffer and performs filtering *AfterScreenSpaceMask

07-13 10:18