Unity6 自定义渲染管线--渲染管线基础流程

www.bilibili.com/video/BV1Xz...

渲染管线基础流程

基础流程

c# 复制代码
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;

namespace LiteRP
{
    public class LiteRenderPipeline : RenderPipeline
    {
        // 老版本
        protected override void Render(ScriptableRenderContext context, Camera[] cameras)
        {
            // 不实现
        }

        // 新版本
        protected override void Render(ScriptableRenderContext context, List<Camera> cameras)
        {
            // 开始渲染上下文
            BeginContextRendering(context, cameras);

            // 渲染相机
            for(int i = 0; i < cameras.Count; ++i)
            {
                Camera camera = cameras[i];
                RenderCamera(context, camera);
            }

            // 结束渲染上下文
            EndContextRendering(context, cameras);
        }

        private void RenderCamera(ScriptableRenderContext context, Camera camera)
        {
            // 开始渲染相机
            BeginCameraRendering(context, camera);

            // 结束渲染相机
            EndCameraRendering(context, camera);
        }
    }
}

剔除

目前在 profiler 里看不到 culling 相关的内容

c# 复制代码
private void RenderCamera(ScriptableRenderContext context, Camera camera)
{
    // 开始渲染相机
    BeginCameraRendering(context, camera);
    // 获取相机剔除参数,并进行剔除
    ScriptableCullingParameters cullingParameters;
    if (!camera.TryGetCullingParameters(out cullingParameters))
        return;
    CullingResults cullingResults = context.Cull(ref cullingParameters);

    // 结束渲染相机
    EndCameraRendering(context, camera);
}

性能复杂度会随着场景复杂度提升而提升,在自定义渲染管线中,我们只能通过 CullingParameters 的设置来有限的干预这个过程,一旦 CullScriptable 的性能开销过高,只能通过降低场景复杂度的方式来进行优化。

相机渲染流程

c# 复制代码
private void RenderCamera(ScriptableRenderContext context, Camera camera)
{
    // 开始渲染相机
    BeginCameraRendering(context, camera);
    // 获取相机剔除参数,并进行剔除
    ScriptableCullingParameters cullingParameters;
    if (!camera.TryGetCullingParameters(out cullingParameters))
        return;
    CullingResults cullingResults = context.Cull(ref cullingParameters);

    // 为相机创建 CommandBuffer
    CommandBuffer cmd = CommandBufferPool.Get(camera.name);
    // 设置相机属性参数
    context.SetupCameraProperties(camera);
    // 清理渲染目标
    // 指定渲染排序设置 SortSettings
    // 指定渲染状态设置 DrawSettings
    // 指定渲染过滤设置 FilterSettings
    // 创建渲染列表
    // 绘制渲染列表
    // 提交命令缓冲区
    context.ExecuteCommandBuffer(cmd);
    // 释放命令缓冲区
    cmd.Clear();
    CommandBufferPool.Release(cmd);
    // 提交渲染上下文
    context.Submit();
    // 结束渲染相机
    EndCameraRendering(context, camera);
}

额外:

因为使用到了 CommandBufferPool,需要添加额外程序集引用。

结果

目前可以看到一个基础场景了

相关推荐
Behavior1 小时前
16天,他用Opus 5.5把《小王子》做成3D游戏,书里的星球能逛了
aigc·unity3d·游戏开发
SmalBox8 小时前
06-03-源码-下载系统(DownloadSystem)
unity3d·游戏开发
SmalBox1 天前
06-02-源码-内置文件系统实现
unity3d·游戏开发
SmalBox2 天前
06-01-源码-文件系统体系(IFileSystem)
unity3d·游戏开发
SmalBox3 天前
05-08-源码-缓存系统与内存管理
unity3d·游戏开发
雪庭4 天前
OCCT OCAF 实战
unity3d
SmalBox4 天前
05-07-源码-引用计数与资源生命周期
unity3d·游戏开发
雪庭4 天前
用 OCCT 造一只啤酒瓶:完整实录
unity3d
SmalBox5 天前
05-06-源码-异步操作体系(OperationSystem)
unity3d·游戏开发
侑虎科技5 天前
深度分析屏幕空间反射算法以及在URP中的实现(一)
unity3d