a quick old code i have that generates a single box and adds it to the subscene, good starting point for you
i basically use a managed component (you can use an unmanaged one with UnityObjectRef), register the material and mesh from the component i retrieve, and using batchid and materialid create a new mesh
if you have voxel terrain, you want to avoid creating mesh with rendermesharray and make it as burstable as possible which i am doing in newer version of my terrain gen
public void OnUpdate(ref SystemState state)
{
state.Enabled = false;
Entity managedPoolEntity = _entityQuery.GetSingletonEntity();
MaterialMeshPoolComponent matMeshPool = _entityQuery.GetSingleton<MaterialMeshPoolComponent>();
EntitiesGraphicsSystem egs = state.World.GetExistingSystemManaged<EntitiesGraphicsSystem>();
NativeList<BatchMeshID> meshIDs = new(matMeshPool.TileMeshes.Length, Allocator.Temp);
foreach (UnityObjectRef<Mesh> meshes in matMeshPool.TileMeshes) meshIDs.Add(egs.RegisterMesh(meshes));
NativeList< BatchMaterialID> matIDs = new(matMeshPool.TileMaterials.Length, Allocator.Temp);
foreach (UnityObjectRef<Material> materials in matMeshPool.TileMaterials) matIDs.Add(egs.RegisterMaterial(materials));
BlobBuilder builder = new BlobBuilder(Allocator.Temp);
ref MaterialMeshPoolBlob root = ref builder.ConstructRoot<MaterialMeshPoolBlob>();
builder.Construct(ref root.MeshIDs, meshIDs);
builder.Construct(ref root.MaterialIDs, matIDs);
BlobAssetReference<MaterialMeshPoolBlob> blobRef = builder.CreateBlobAssetReference<MaterialMeshPoolBlob>(Allocator.Persistent);
MaterialMeshPoolSingleton materialMeshPoolSingleton = new() { Blob = blobRef };
builder.Dispose();
meshIDs.Dispose();
matIDs.Dispose();
Entity singleton = state.EntityManager.CreateEntity();
state.EntityManager.SetName(singleton, "MaterialMeshPoolSingleton");
state.EntityManager.AddComponentData(singleton, materialMeshPoolSingleton);
SceneTag sceneTag = state.EntityManager.GetSharedComponent<SceneTag>(managedPoolEntity);
SceneSection sceneSect = state.EntityManager.GetSharedComponent<SceneSection>(managedPoolEntity);
Entity testEntity = state.EntityManager.CreateEntity();
state.EntityManager.SetName(testEntity, "TestEntity");
RenderMeshDescription rmd = new RenderMeshDescription
{
FilterSettings = RenderFilterSettings.Default
};
MaterialMeshInfo mmi = new MaterialMeshInfo(materialMeshPoolSingleton.Blob.Value.MaterialIDs[0],
materialMeshPoolSingleton.Blob.Value.MeshIDs[0]);
state.EntityManager.AddSharedComponent(testEntity, sceneTag);
state.EntityManager.AddSharedComponent(testEntity, sceneSect);
state.EntityManager.AddComponent<BlendProbeTag>(testEntity);
RenderMeshUtility.AddComponents(testEntity, state.EntityManager, rmd, mmi);
state.EntityManager.AddComponentData(testEntity, new LocalTransform {
Position = new float3(0f, 0f, 0f),
Rotation = quaternion.identity,
Scale = 1f
});
Debug.Log("done");
}