uhura
uhura

Reputation: 2683

Select cloned mesh

Problem :

I Created a mesh and cloned it. When I try to select one, both is selected. Meshes have different ids but material and geometry ids are the same.

I created a simple jsfiddle example: jsfiddle

How I clone :

var cloned =  object.clone(); 

or

var cloned = new THREE.Mesh(object.geometry, object.material);

In both cases result is the same, on click one - selects both. I tried to change material id, doesn't help.

If I create new material, then everything is fine, I can select each object separately :

var cloned = new THREE.Mesh(object.geometry, new THREE.MeshLambertMaterial(....));

or

var cloned = new THREE.Mesh(object.geometry, object.material.clone());

My question. What is the difference in materials between object.clone().material and object.material.clone()?

The same behavior is when I create many meshes with same material. On mouse down on one mesh will select all 10 meshes. Example:

var geo = new THREE.CubeGeometry(....)
var material = new THREE.MeshLambertMaterial(....)
for(var i = 0; i < 10; i++){
    var mesh = new THREE.Mesh(geo, material);
}

Upvotes: 3

Views: 1339

Answers (1)

WestLangley
WestLangley

Reputation: 104763

When I try to select one, both is selected.

That is not true. Only one is being selected, but both are being highlighted.

Cloning a mesh creates a new mesh, but the new mesh shares the material (and geometry) with the original mesh.

So in your demo, you only have one instance of the material that both meshes share.

This is why, when you change the color of one mesh, the other changes, too.

Upvotes: 3

Related Questions