Multi material dynamic batching?

Documentation states:
https://developer.playcanvas.com/en/user-manual/optimization/batching/

“There are a variety of rules which the engine will apply to see if mesh instances are able to be combined. The primary rule is that all mesh instances must share the same material.”

example:
https://playcanvas.github.io/#graphics/batching-dynamic.html

            // randomly assign a material
            entity.model.material = Math.random() < 0.5 ? material1 : material2;

            // add the entity to the batchGroup - this instructs engine to try and render these meshes in a small number of draw calls
            // there will be at least 2 draw calls, one for each material
            entity.model.batchGroupId = batchGroup.id;

there will be at least 2 draw calls, one for each material

the fact that the two material types objects are being crammed into a single dynamic batch is really confusing me; shouldn’t they be be on their own batch object?

Hi @plasmalasgun,

You don’t really have to worry about that, the pc.Batcher will survey all models using the same batch group and automatically split them to batches.

Batches that respect those rules, so if you batch 100 entities that use the same model, and that model uses two materials, then that batch group will get rendered in two (2) draw calls.

Potentially, if you don’t care about static vs dynamic batching or the batch group AABB, you can just assign all of your models to the same batch group. The pc.Batcher will do its best to split everything to the proper batches to minimize the draw calls.

2 Likes

I find concern to worry when you are trying to mirror the “batching functionality” when using dynamic hardware instancing; whose groups or “hardware-instancing batches” consist of model+material+buffer, while regular batching material (the example batched box, cone, cylinder, sphere and capsule (5 meshes) and (2 materials) under a single batch id… Since i would need 10 “hardware-instancing batches”:
yellow box, cyan box
yellow cone, cyan cone
yellow cylinder, cyan cylinder
yellow sphere, cyan sphere
yellow capsule, cyan capsule

shouldn’t the batches be bundled this way too (doesn’t the batcher does this automatically then?)

Ah if you are using hardware instancing, instead of CPU based batching, then yes you will have to group everything yourself and enable HW instancing per mesh and per material.

There isn’t a system currently in the engine to that for HW instancing.

1 Like