Lots of materials, changing texture changes on all of them?

Hi guys, I have a lot of planes with the same material, when I change texture on material on one of the planes it changes on all of them. In unity it’s solved using material/sharedMaterial how to do the same thing in playcanvas?

I am using simple plane and and I am using material as it’s instructed in the docs. Texture changes but on all materials it’s the same but it’s supposed to be different.

What I am doing wrong?

for (let i = 0; i < this.materials.length; i++) {

        let count = this.assetDatabase.length;
        let randomTexture = Math.floor(Math.random() * count);
        let texture = this.assetDatabase[randomTexture];
        this.plane[i].render.material.diffuseMap = texture.resource;
        this.plane[i].render.material.opacityMap = texture.resource;
        this.plane[i].render.material.update();
    }

Hi @mirkoni,

So, the official way would be to clone the material, change the textures and apply it to that specific model.

// sample code
this.plane[i].render.material = material.clone();
this.plane[i].render.material.diffuseMap = texture.resource;
this.plane[i].render.material.opacityMap = texture.resource;
this.plane[i].render.material.update();

Another way is to use setParameter on each mesh instance to change a uniform (material parameter) for a certain model only. This API is private and subject to change, and it also means that you need to get familiar with the right uniform names each time.

// sample code
this.plane[i].render.meshInstances[0].setParameter('texture_diffuseMap', texture.resource);
this.plane[i].render.meshInstances[0].setParameter('texture_opacityMap', texture.resource);

(material.update() isn’t required, hence this solution is more performant too)

2 Likes

thank you! it works great!

1 Like