How to reduce draw call count in RealityKit

I'm trying to render a large number of entities, it looks like each ModelEntity causes a draw call, even if you share the ModelComponent so each Entity shares the mesh and materials.

I tried to use the MeshInstanceCollection inside MeshResource to generate a large number of objects in the scene, the code works and draws many objects but the draw count is still one call per instance, this seems strange I would assume it should only be one draw call for the single entity since I have specified to use instancing in the resource.

Has anybody else successfully used instancing in RealityKit to draw a large number go Entities (maybe around 10,000) or drawn this amount of items successfully with 60fps any other way?

Here is some sample code that draws 100 cubes using instancing but still causes 100 draw calls.

  func instanceTest(scene: RealityKit.Scene) {
    var resource = MeshResource.generateBox(size: 0.2)

    var contents = MeshResource.Contents()
    contents.models = resource.contents.models

    var arr: [MeshResource.Instance] = []
    var matrix = matrix_identity_float4x4
    matrix[3, 0] = 0.5

    for i in 0..<100 {
      let inst = MeshResource.Instance(id: "\(i)", model: "MeshModel", at: matrix)
      arr.append(inst)
    }
    contents.instances = MeshInstanceCollection(arr)
    let updatedResource = try? MeshResource.generate(from: contents)

    let unlitMaterial = UnlitMaterial(color: .red)
    let modelEntity = ModelEntity(
      mesh: updatedResource!,
      materials: [unlitMaterial]
    )

    let anchor = AnchorEntity()
    anchor.addChild(modelEntity)
    scene.addAnchor(anchor)
  }

It doesn't seem like you can reduce the draw calls using multiple entities, but instead I used the new LowLevelMesh to render lots of geometry available in iOS18.

I tried the code awhile back but couldn't get .showStatistics to work in RealtyKit without crashing. Do you know possibe reason for that? I've only tackled draw calls in SceneKit and just now converting one of those projects to RealityKit. I wanted to compare your code with using Clone. Even though it looks like there shouldn't be a difference.

How to reduce draw call count in RealityKit
 
 
Q