When I change one of the materials, the other same material is also changed

There are now 2 objects, they all reference the same material. When I change the material of one of the objects, the material of the other object is also changed. How should I avoid this situation? Or is there a way to copy materials through code? For example, “m_mateial.clone()”

Yes, the appearance of a material will be the same even when applied to multiple models or meshInstances. So if you decide to change your brass metal be more reflective, it will change reflectivity on all surfaces.

The simple thing to do is to duplicate the material and rename it. Just right-click in the Assets folder to bring up a context menu and choose Duplicate. The duplicated material will have the same name but with “copy” appended to the name. You can then apply this new material to another object or meshInstance and edit it independently.

When I first encountered this behavior it seemed like a bug to me since no other 3D program I’ve used works that way. But it appears to be an intentional part of the design and it can be powerful if you want to make changes to many things at once using a very small amount of code.

If you plan on making the change to a material through a script when some event occurs and you want to only affect it on one object or meshInstance and not all of them, you can change material settings on a single meshInstance, but not affect other meshInstances using the same material using .setParameter(). Here is a past thread with some examples.

1 Like

All Right.
In unity3d, I can Instantiate a reference to the material as a separate material that does not affect other objects. This can only be done, I will make an additional copy in the Asset panel.
Thank you…

There is: https://developer.playcanvas.com/en/api/pc.StandardMaterial.html#clone

can you show me how to use this api? very thank! I can`t understand this api.

var material1 = new pc.StandardMaterial();
var material2 = material1.clone();