BufferGeometry
Object3D
Raycaster
Camera
CubeCamera
PerspectiveCamera
OrthographicCamera
StereoCamera
Clock
Curve
CurvePath
Path
Shape
ShapePath
ArrowHelper
AxesHelper
BoxHelper
Box3Helper
CameraHelper
DirectionalLightHelper
GridHelper
PolarGridHelper
HemisphereLightHelper
PlaneHelper
PointLightHelper
SkeletonHelper
SpotLightHelper
Light
PointLight
RectAreaLight
SpotLight
DirectionalLight
HemisphereLight
LightShadow
PointLightShadow
AnimationLoader
AudioLoader
BufferGeometryLoader
CompressedTextureLoader
CubeTextureLoader
DataTextureLoader
FileLoader
ImageBitmapLoader
ImageLoader
Loader
LoaderUtils
MaterialLoader
ObjectLoader
TextureLoader
LoadingManager
Material
Box2
Box3
Color
Cylindrical
Euler
Frustum
Interpolant
Line3
MathUtils
Matrix3
Matrix4
Plane
Quaternion
AnimationAction
AnimationClip
AnimationMixer
AnimationObjectGroup
AnimationUtils
keyframeTrack
PropertyBinding
PropertyMixer
BooleanKeyframeTrack
QuaternionKeyframeTrack
StringKeyframeTrack
Audio
AudioAnalyser
AudioContext
AudioListener
PositionalAudio

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.

Syntax

material.clone()

Return Value

This method returns a new instance of the material with the same properties and parameters as the original material.

Examples

Example 1

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.

Example 2

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.

Remarks

  • The new instance of the material will not be added to the scene automatically. You need to create a Mesh or other object that uses the material to see it in the scene.
  • If you modify the properties or parameters of the original or cloned material after the clone() method is called, the changes will not be reflected in the other material.
  • If the original material has textures, shaders, or other complex properties, the 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.