Material.clone()
The Material.clone()
method creates a new instance of the material with the same properties and parameters as the original material. This method is useful when you need to create materials that share attributes, but may have different colors or textures.
material.clone()
This method returns a new instance of the material with the same properties and parameters as the original material.
const material1 = new THREE.MeshBasicMaterial({ color: 0xff0000 });
const material2 = material1.clone();
material2.color.set(0x00ff00);
const mesh1 = new THREE.Mesh(geometry, material1);
const mesh2 = new THREE.Mesh(geometry, material2);
In this example, the MeshBasicMaterial
material1
is created with a red color. The clone()
method is then called to create a copy of the material, which is assigned to material2
. The color of material2
is then changed to green using the set()
method. A mesh mesh1
is created with material1
, and another mesh mesh2
is created with material2
. mesh1
will be red, while mesh2
will be green.
const material = new THREE.MeshBasicMaterial({ color: 0xff0000 });
const material2 = material.clone();
material2.color.set(0x00ff00);
const mesh1 = new THREE.Mesh(geometry, material);
const mesh2 = new THREE.Mesh(geometry, material);
scene.add(mesh1, mesh2);
In this example, the MeshBasicMaterial
material
is created with a red color. Two meshes, mesh1
and mesh2
, are created using material
. The clone()
method is then called to create a copy of material
, which is assigned to material2
. The color of material2
is then changed to green using the set()
method. Both meshes will be red, as they use the original material
, while material2
is unused.
Mesh
or other object that uses the material to see it in the scene.clone()
method is called, the changes will not be reflected in the other material.clone()
method may not work as expected. It is recommended to test your materials thoroughly after cloning, especially if they are meant for advanced use cases.