[SOLVED] mesh.setParameter( 'material_diffuse', [1,0,0] ) not working

Hello, I’m trying to use without success the setParameter( ‘material_diffuse’,…) method to change the color of a model. Here is my code :

var Clone = pc.createScript('clone');

Clone.attributes.add('source', {type:'entity'});

// initialize code called once per entity
Clone.prototype.initialize = function() {
    let clone = this.source.clone();
    let mesh = this.source.model.meshInstances[0];   
    mesh.setParameter( 'material_diffuse', [1,0,0] );
    mesh.setParameter( 'material_emissive', [1,0,0] );

    mesh = clone.model.meshInstances[0];
    mesh.setParameter( 'material_diffuse', [0,1,0] );
    //mesh.setParameter( 'material_emissive', [0,1,0] );

here is my test project : https://playcanvas.com/project/806696/overview/change-mesh-color

here is the result :

It looks like the ‘material_emissive’ parameter works… but not the ‘material_diffuse’…

Am I missing something ? Thanks for your help !

ps : I would like to avoid cloning materials to achieve this…

Hi @Jerome_Binachon ,

You don’t need to clone materials to do that, your code seems right. I imagine the issue here is your cubes are using the placeholder material the engine provides, since you haven’t assigned one in editor.

Try creating a single material, change slightly the diffuse color so it’s not the default color there (that will force the shader to include the diffuse shader chunk) and assign it to your cube. Then I think your code will work.

Hi @Leonidas, I tried what you suggested : it worked like a charm ! Thank you :slight_smile:

1 Like


1 Like