Version: Unity 6.0 (6000.0)
语言 : 中文
在 URP 中使用 BatchRendererGroup API 创建渲染器
在 URP 中使用 BatchRendererGroup API 注册网格和材质

在 URP 中初始化 BatchRendererGroup 对象

使用 BRG 进行渲染的第一步是创建 BatchRendererGroup 实例,并通过 OnPerformCulling 实现对其进行初始化。

OnPerformCulling 回调是 BRG 的主要入口点,Unity 每次剔除可见对象时都会调用它。有关其收到的参数的信息,请参阅 OnPerformCulling。通常,OnPerformCulling 回调需要执行两个任务:

  • 可见性剔除,用于根据 BatchCullingContext 参数确定哪些实例可见。
  • 输出实际绘制命令,用于渲染这些实例。为此,应写入 BatchCullingOutput 参数。

在简单实现中,可以直接在 OnPerformCulling 回调中执行这些任务,但在高性能实现中,最佳做法是在 Burst 作业中执行大部分工作。OnPerformCulling 回调会返回一个 JobHandle,其会在作业将输出写入 BatchCullingOutput 参数后完成。如果您的实现未使用作业,则可以返回空 JobHandle。

请参阅以下代码示例,了解如何创建 BatchRendererGroup 对象并使用可进行编译的最小 OnPerformCulling 回调对其进行初始化。

using System;
using Unity.Collections;
using Unity.Collections.LowLevel.Unsafe;
using Unity.Jobs;
using UnityEngine;
using UnityEngine.Rendering;

public class SimpleBRGExample : MonoBehaviour
{
    private BatchRendererGroup m_BRG;

    private void Start()
    {
        m_BRG = new BatchRendererGroup(this.OnPerformCulling, IntPtr.Zero);
    }

    private void OnDisable()
    {
        m_BRG.Dispose();
    }

    public unsafe JobHandle OnPerformCulling(
        BatchRendererGroup rendererGroup,
        BatchCullingContext cullingContext,
        BatchCullingOutput cullingOutput,
        IntPtr userContext)
    {
        // This example doesn't use jobs, so it can return an empty JobHandle.
        // Performance-sensitive applications should use Burst jobs to implement
        // culling and draw command output. In this case, this function would return a
        // handle here that completes when the Burst jobs finish.
        return new JobHandle();
    }
}

在使用 OnPerformCulling 创建绘制命令之前,您需要向 BatchRendererGroup 对象提供您希望其绘制的任何网格以及您希望其使用的任何材质。有关更多信息,请参阅下一个主题:注册网格和材质

在 URP 中使用 BatchRendererGroup API 创建渲染器
在 URP 中使用 BatchRendererGroup API 注册网格和材质