Can I use custom shader chunks with batched models?

My goal is to alter diffuse color of pixels according to position of the pixel in the model.

My current solution is as follows:

  1. I’ve replaced “transformVS” chunk with my own code. My code is almost the same as the original one (transform.vert). The only difference is I’ve added varying vPosition and stored vertex_position in it.
  2. I’ve replaced “diffuseTexPS” chunk with my own code which calculates dAlbedo according to value of vPosition.

This works fine with non batched models but it doesn’t work with batched ones.

It looks like batched models completely ignore my custom vertex shader code (“transformVS” chunk) and they use the original code. Also it looks like I can’t use any custom uniforms in my fragment shader code in “diffuseTexPS” chunk - my code in “diffuseTexPS” chunk still runs but my uniforms doesn’t have correct values.

Does anyone know how to solve this? Thanks a lot.

Hi @NokFrt,

You can definitely use custom shaders or override shder chunks on batched models. The batcher will group the mesh instances based on their material/shader and same uniform values.

I am not sure why your shader changes aren’t respected after the batcher runs, most likely a race condition or something. You can easily get a reference to the material used by any batch group and further override its shader after the batcher ran. That will be automatically applied to all of the mesh instances included in that batch group.

var batches = this.app.batcher._batchList;
batches.forEach( function(batch){

   var material = batch.meshInstance.material;
   // this is the material used by this batch instance
}); 
1 Like

Thanks a lot Leonidas. The problem is solved :slight_smile:

The BatchGroup clones original material but it looks like it loose my custom parameters for uniforms and I have to set them again for the new material.

1 Like