Changing material has no effect on my model

I want to change the material of some specific mesh. But it is not working when I tried this:

  Object.keys(materials).forEach(function (prop) {
    let MaterialName = materials[prop].name;
    if (MaterialName.includes("Glass") || MaterialName.includes("GLASS")) {
      // console.log(materials[prop])
      materials[prop]=new THREE.MeshPhysicalMaterial({
        metalness: 1,
        roughness: 0,
        transparent: true,
        opacity: 0.5,
        envMapIntensity: 1,
        side: THREE.FrontSide,
      });
    }
  });

It is still as it was. No change at all. How can I solve this? Iā€™m using react three fiber. Help me please. Thanks

@Tanjil_Hossain

Hard to say without a live example and the full code.

Maybe materials object do not hold reference to the original material.

I think if you a creating a new material you will need to assigned to the mesh.

mesh.material = myNewMaterial

Material is changing but is has no effect on model.But standard material is working fine if I change any property

Thats the reason, because the material is completely new and of other kind you should change the material to the mesh.

mesh.material = myNewMaterial

You should traverse the scene

const myNewMaterial = new THREE.MeshPhysicalMaterial({
        metalness: 1,
        roughness: 0,
        transparent: true,
        opacity: 0.5,
        envMapIntensity: 1,
        side: THREE.FrontSide,  // to test change this to `THREE.DoubleSide` just in case.
        color: 0x000000 // do not forget to set the right color to the new material.
      });

scene.traverse((obj) => {
   if(obj.isMesh && (obj.material.name.includes('Glass') || obj.material.name.includes('GLASS'))) {
      obj.material = myNewMaterial;
   }
})

Thanks a lot, man. It worked!

1 Like