メッシュ と マテリアル は Unity のマネージ C# オブジェクトなので、Burst C# コードからは使用できません。したがって、これらを BRG の描画コマンドで使用するには、BRG に事前登録する必要があります。
メッシュオブジェクトの登録には BatchRendererGroup.RegisterMesh を使用し、マテリアルオブジェクトの登録には BatchRendererGroup.RegisterMaterial を使用してください。前者の関数は BatchMeshID を返し、後者の関数は BatchMaterialIDを を返します。これらは Burst 互換ハンドルを含むプレーンなデータ構造体で、間違ったハンドルタイプの使用によるエラーを防ぐために、強く型付けされています。
メッシュオブジェクトとマテリアルオブジェクトの登録は、ランタイムを含め、いつでも行えます。ただし、以下の条件だけは満たしている必要があります。
不要になったメッシュオブジェクトやマテリアルオブジェクトは登録解除することもできます。これはメッシュオブジェクトやマテリアルオブジェクトをアンロードする場合には行う必要があります。BatchRendererGroup.Dispose は、すべてのアセットの登録を自動的に解除します。
ノート: BatchMeshID や BatchMaterialID はシリアル化できません。これらは、その登録された BatchRendererGroup でのみ有効で、登録が解除されたり、その BatchRendererGroup が存在しなくなると無効になります。また BatchMeshID と BatchMaterialID は、メッシュあるいはマテリアルオブジェクトが強制的にアンロードされた場合にも無効になります (例: Unity がメッシュあるいはマテリアルオブジェクトを含むシーンをアンロードした場合)。
同じメッシュあるいはマテリアルのオブジェクトを複数回登録することも可能です。これは、メッシュあるいはマテリアルの登録を、どのメッシュあるいはマテリアルが既に登録されているか把握せずに行いたい場合に役立ちます。この場合、BatchRenderer は以下のように内部的に登録回数をカウントします。
ノート: BRG は、フレーム内の最初の OnPerformCulling コールバックメソッドの後に、メッシュあるいはマテリアルオブジェクトに加えられた変更を検査します。つまり Unity は、それ以前に発生したすべての変更を考慮します。これには最初のコールバック自体で行った変更は含まれますが、そのコールバックがスケジュールしたジョブで発生した変更は含まれません。それ以降にメッシュあるいはマテリアルのオブジェクトに変更を加えると、未定義の動作が発生します。
BatchRendererGroup オブジェクトにメッシュやマテリアルを登録する方法については、以下のコードサンプルを参照してください。このコードサンプルは、BatchRendererGroup オブジェクトの初期化 に掲載のものをベースに構築されています。
using System;
using Unity.Collections;
using Unity.Collections.LowLevel.Unsafe;
using Unity.Jobs;
using UnityEngine;
using UnityEngine.Rendering;
public class SimpleBRGExample : MonoBehaviour
{
public Mesh mesh;
public Material material;
private BatchRendererGroup m_BRG;
private BatchMeshID m_MeshID;
private BatchMaterialID m_MaterialID;
private void Start()
{
m_BRG = new BatchRendererGroup(this.OnPerformCulling, IntPtr.Zero);
m_MeshID = m_BRG.RegisterMesh(mesh);
m_MaterialID = m_BRG.RegisterMaterial(material);
}
private void OnDisable()
{
m_BRG.Dispose();
}
public unsafe JobHandle OnPerformCulling(
BatchRendererGroup rendererGroup,
BatchCullingContext cullingContext,
BatchCullingOutput cullingOutput,
IntPtr userContext)
{
// この単純な例はジョブを使用しないので、空の JobHandle を返します。
// パフォーマンス負荷の高いアプリケーションの場合は、Burst ジョブを使用して
// カリングと描画コマンドの出力を実装することが推奨されます。その場合、この関数は
// Burst ジョブの終了時に完了するハンドルを返します。
return new JobHandle();
}
}
登録されたメッシュとマテリアルを使用する描画コマンドを作成するには、その描画コマンドのインスタンスに使用するデータ (Transform 行列など) を前もって提供する必要があります。BatchRendererGroup は、バッチと呼ばれる概念を使用して、各インスタンスに使用するデータを提供します。詳細は次のトピック バッチの作成 を参照してください。
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.