Skip to content

Custom Renderer Features

zilch edited this page Jan 24, 2021 · 1 revision

Custom Renderer Features

在URP里,可以通过扩展RendererFeatures来实现自定义的Pass。

编写自定义RendererFeatures

  • 新建PostProcessingPass,继承ScriptableRenderPass

        public class PostProcessingPass : ScriptableRenderPass{
            private const string CommandBufferTag = "CustomPostProcessing";
    
            public PostProcessingPass(){
                //通过设置renderPassEvent,来控制该pass在何时被渲染。
                this.renderPassEvent = RenderPassEvent.AfterRenderingTransparents;
            }
            
            ///在Execute里可以构造Pass渲染指令
            public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
            {
                var cmd = CommandBufferPool.Get(CommandBufferTag);
                try{
                    // 调用渲染函数
                    Render(cmd, ref renderingData,context);
                    // 执行命令缓冲区
                    context.ExecuteCommandBuffer(cmd);
                }finally{
                    // 释放命令缓存
                    CommandBufferPool.Release(cmd);
                }
            }
        }
  • 新建PostProcessingFeature,继承自ScriptableRendererFeature

    public class PostProcessingFeature : ScriptableRendererFeature
    {
        private PostProcessingPass _pass;
        //在此处初始化
        public override void Create(){
            _pass = new PostProcessingPass();
        }
    
        //在此处将自定义的Pass加入到渲染管线中
        public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData){
            //设置渲染对象
            _pass.ConfigureTarget(renderer.cameraColorTarget);
            //将Pass加入到渲染管线
            renderer.EnqueuePass(_pass);
        }
    
    
    }

    继承自ScriptableRendererFeature后需要实现两个函数,其中

    • OnCreate 用于初始化
    • AddRenderPasses 则用于将自定义的Pass加入到渲染管线中。
  • PostProcessingFeature加入到渲染管线中

    • 选中Settings/ForwardRenderer
    • 在右侧面板点击Add Renderer Features即可添加
Clone this wiki locally