Material or color not changing


#1

Project link: https://playcanvas.com/editor/project/601087
In the “materials.js” I have a loop script that should change attached material of the desktop. I see in the console log that event is fired, however the material itself does not change.
What am I doing wrong?


#2

The project is private, can you make it public or create a public project please?


#3

Sorry. Forgot to hit “Save” :slight_smile:


#4

There is a setInterval on the update call which means that it creates one every frame. I don’t think that is the problem but it’s definitely not helping.


#5

I added setInterval just to see if material changes at all, but visually it doesn’t. Neither on any action or the loop


#6

Looks like the main reason was because it was in a batch group. I’ve set the group to None here. Use 1 and 2 on the keyboard to change material: https://playcanvas.com/editor/scene/710814

I’m not too clued up on the batching but at a guess there would be some batch update call to make here? @Leonidas should be able to help here.


#7

Indeed the Batch Manager creates a new grouped meshInstance, so updating the Material on the individual mesh won’t have any effect.

You need to update that batch group using:

// get a batch group and regenerate it
var batchGroup = this.app.batcher.getGroupByName('Batch Group Name');
this.app.batcher.generate([ batchGroup.id ]);

// or regenerate all batch groups (scene wide) with a single call, careful this potentially can be slow
this.app.batcher.generate();

#8

You can also get the batchGroupId directly from a model Component:

var batchGroupId = this.myEntity.model.batchGroupId;
this.app.batcher.generate([ batchGroupId ]);

#9

So something like:

var batchGroup = this.entity.model.batchGroupId;
batchGroupId.meshInstances.forEach(function (mesh) {
    mesh.material = this.materials[i];
});

should theoretically work?


#10

Νο, batchGroup would be just a number, not an object.

This would work, but check my comment if you are thinking about performance:

    var meshes = this.entity.model.meshInstances;
    for (var i = 0; i < meshes.length; i++) {
        var mesh = meshes[i];
        mesh.material = material.resource;
    }

    // Note: if you are doing this per frame, then you shouldn't use batching for that entity.
    // The cost of regenerating the batched groups is high. 
    // Use the profiler, to see how much time is required for batching generation.
    var batchGroupId = this.entity.model.batchGroupId;
    this.app.batcher.generate([ batchGroupId ]);

#11

Thank you for the replies. I guess, grouping is not as beneficial as I thought it would be for my purposes.


#12

Batching is only really helpful if you have a lot of meshes that are sharing the same material. E.g Having lots of these desks shown in an open office view where all the desks are sharing the same material and in the same batch group would reduce the number of draw calls made.

In your current example where there is only one model, it doesn’t really give you a performance benefit.