Modifying material on a single object

I’ve just spent many hours trying to get a simple one off glow tween working using a material’s emissive channel. When I press a button, an item glows. It all works great, but when I take it out of the test environment I can see when when I run the glow, ALL objects in the scene with this material are affected.

How do I temporarily modify the material of just one object?

Thanks

Hi @Grimmy,

How are you updating the material emissive? If you’re changing the material property directly then all objects sharing the material will get updated.

To update just a single model, you need to either clone and use a separate material or set the parameter directly on the mesh instance:

// for example changing the emissive intensity
this.entity.render.meshInstances.forEach(meshInstance => {
   meshInstance.setParameter("emissiveIntensity", 0.5);
});

There are several forum posts on this method, try searching on how setParameter works.

Thanks. I had a looks at setParameter on some other forum topics and in the api but I tried it and it does nothing.
-Does setParameter change a material only on one instance of an object? Nothing seems to actually mention if it does.

Either way, I dont see any effect when using this code…

this.entity.render.meshInstances.forEach(meshInstance => {     
  meshInstance.setParameter("emissiveIntensity", data.intensity);
  console.log("Intensity >> "+data.intensity  ); //this prints fine!! :)
  this.entity.render.material.update();//I also tried material.update()...doesnt work
});

No need to call material.update(), most likely you need to change the default value on the material for emissive intensity. Check this discussion here:

If you continue having trouble, feel free to share a sample project so we can help you out in context.

Thanks. I have set the initial parameters at the start like this but it still does nothing…

 this.entity.render.meshInstances.forEach(meshInstance => {
  meshInstance.setParameter("material_emissive", [new pc.Color(1, 1, 1, 1)]);
  meshInstance.setParameter("emissiveIntensity", 0);
});

However, I have no idea if the parameter names are correct. There are no errors if I type in something random and there is no list of parameter names anywhere.

EDIT: I downloaded the json for the shader and found that i should be using the following. However, it still doesnt work…

this.entity.render.meshInstances.forEach(meshInstance => {
    meshInstance.setParameter("emissive", [1,1,1]);
    meshInstance.setParameter("emissiveIntensity", 0);
    
});

All the debug prints out fine values…its as if the setParameter is just not doing anything at all. ?!?

Your parameter name for emissive color is material_emissive and requires an array [1.0, 1.0, 1.0] e.g. for white.

For intensity it’s material_emissiveIntensity and you set a single numeric value from 0.0 to 1.0.

Have you checked the following example? It works quite fine and uses setParameter to animate opacity (the code is very well documented).

https://developer.playcanvas.com/en/tutorials/fading-objects-in-and-out/

If you keep having trouble please share a sample project.

Okay Thanks. It was just the names of the parameters were wrong. How exactly can I find out the parameter names?
It all works now though! Thanks

They are part of a private for the moment API. Private mainly because the internal shaders using those parameters are in active development.

You can find them by looking in the shader chunks repo, for example here is the emissive chunk: