Dynamically generated Batch is invalid

Hello everyone,

I recently started out with Playcanvas and was trying to implement some batching for my 3D grid objects.
I can’t use the default batching playcanvas does on startup, as it would take too long to batch the whole grid. I also won’t be able to hide them directly on start.
That is why I started looking up the BatchManager for more control.
What I did then was relatively simple: generate some meshInstances, then prepare and batch them with the BatchManager.

And here comes the problem: nothing is shown at all!

So I tried out to find the source of the problem, first by checking if the created meshInstances are correct, then by verifying the batches… and there I could notice that none of the batches had a vertexBuffer for their mesh, and neither is a material associated to their meshInstance (nothing happened even after manually setting the material).

As I couldn’t find any clue on how to fix this problem even after half a day of debugging, I want to ask if someone knows a solution to this problem.

I already created an empty project showcasing the problem and with a commented script if it helps:
https://playcanvas.com/editor/scene/615245

Thank you for your time

I am not sure what is wrong with your code exactly, seems correct.

But I suspect that the entities you create can’t properly reference a mesh in their model component. Seems the way you initialise them isn’t working for some reason.

Check this, it makes the cube asset appear:

SpawnBatchCubes.prototype.spawnMeshInstance = function(meshInstance) {
    var cube = new pc.Entity("cube");
    var model = cube.addComponent('model', {
        castShadows: false,
        castShadowsLightmap: false,
        receiveShadows: false,
        isStatic: true,
        asset: 12681256
    });
    // var m = new pc.Model();
    // m.graph = new pc.GraphNode();
    // m.meshInstances = [ meshInstance ];
    // model.model = m;

    this.entity.addChild(cube);
};

By the way you can set batch groups per entity directly in code. No need to mess with their model/mesh instances. You can call after the this.app.batcher.generate(groupId) method with a group id, whenever you feel like and it will generate the batches only for that group.

Hope it helps.

Thank you @Leonidas for your fast reply!

Adding the “asset: 12681256” makes it spawn a simple cube, and not the received mesh from the batch instance, so that solution won’t work for me.

As for the spawnMeshInstance function, it binds in my example the meshInstance of either a 1x1x1 cube or the generated batch to the model component of a new entity.
The simple cube is already working, the one with the batch isn’t.

The batcher.generate however function might be a good alternativ to what I need, so I will test it out later. Though I would still like to know where it goes wrong…

Thank you again for your help!