Version: 2022.3
Language : English
Creating a renderer with BatchRendererGroup
Registering meshes and materials

Initializing a BatchRendererGroup object

The first step to render using BRG is to create an instance of BatchRendererGroup and initialize it with an implementation of OnPerformCulling.

The OnPerformCulling callback is the main entry point of BRG and Unity calls it whenever it culls visible objects. For information on the parameters it receives, see OnPerformCulling. Typically, there are two tasks that the OnPerformCulling callback needs to perform:

  • Visibility culling to determine which of its instances are visible based on the BatchCullingContext parameter.
  • Output the actual draw commands to render those instances. To do this you write to the BatchCullingOutput parameter.

In simple implementations, you can do these tasks directly in the OnPerformCulling callback, but for high-performance implementations, it’s best practice to do most of this work in Burst jobs. The OnPerformCulling callback should return a JobHandle that completes after the jobs write the output into the BatchCullingOutput parameter. If your implementation doesn’t use jobs, you can return an empty JobHandle.

See the following code sample for an example of how to create a BatchRendererGroup object and initialize it with the most minimum OnPerformCulling callback that compiles.

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();
    }
}

Before you use OnPerformCulling to create draw commands, you need to provide your BatchRendererGroup object any meshes you want it to draw, and any materials you want it to use. For more information, see the next topic, Registering meshes and materials.

Creating a renderer with BatchRendererGroup
Registering meshes and materials