Version: 6000.3
语言: 中文
在 URP 中使用 BatchRendererGroup API 创建渲染器
在URP中使用BatchRendererGroup API注册网格体和材质

在 URP 中初始化 BatchRendererGroup 对象

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

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

  • Visibility剔除,以根据BatchCullingContext参数确定其哪些实例可见。
  • 输出实际的绘制命令以渲染这些实例。为此,您需要写入 BatchCullingOutput 参数。

在简单的实现中,您可以直接在 OnPerformCulling 回调中执行这些任务,但对于高性能实现,最佳做法是在 Burst 作业中完成大部分工作。OnPerformCulling回调应返回一个JobHandle,该Handle在作业将输出写入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注册网格体和材质