BRG を使用して描画するための最初のステップは、BatchRendererGroup のインスタンスを作成し、それを OnPerformCulling の実装で初期化することです。
OnPerformCulling コールバックは BRG のメインのエントリーポイントで、Unity は可視オブジェクトをカリングするたびにこれを呼び出します。このコールバックが受け取るパラメーターの詳細については、OnPerformCulling を参照してください。通常、OnPerformCulling コールバックが実行する必要があるタスクは、以下の 2 つです。
単純な実装では OnPerformCulling コールバックで直接これらのタスクを実行できますが、高性能な実装ではこの作業のほとんどを Burst ジョブで実行するのが効率的です。OnPerformCulling コールバックは、ジョブが BatchCullingOutput パラメーターに出力を書き込んだ後に完了する JobHandle を返します。ジョブを使用しない実装の場合は、空の 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 オブジェクトに提供する必要があります。詳細は、次のトピックのメッシュとマテリアルの登録を参照してください。