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,需要添加额外程序集引用。

结果

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

相关推荐
鑫鑫哥adam8 天前
一个 1x1 贴图,是怎么炸掉整个热更包的?
unity3d·游戏开发
Behavior9 天前
Unity 手游动态更换 App 图标 — Android 与 iOS 双端技术方案
c#·unity3d·游戏开发
SmalBox9 天前
03-03-架构篇-Runtime加载系统架构
unity3d·游戏开发
SmalBox9 天前
03-02-架构篇-Editor打包系统架构
unity3d·游戏开发
Behavior9 天前
Unity 手游 iOS Deep Link 唤醒全流程:从 URL Scheme / Universal Links 到 C# 层参数投递
c#·unity3d·游戏开发
SmalBox9 天前
03-01-架构篇-整体架构总览
unity3d·游戏开发
SmalBox9 天前
02-08-原理篇-异步加载与性能优化
unity3d·游戏开发
字节暗面9 天前
Unity AssetBundle 热更新安全排查:从 CDN 清单到本地缓存
unity3d·图片资源
SmalBox15 天前
02-07-原理篇-文件系统与跨平台适配
unity3d·游戏开发
SmalBox16 天前
02-06-原理篇-热更新与版本管理
unity3d·游戏开发