After loading an OBJ file, I created a clone of it in an array. However, when I attempt to change the vertex color of one clone, all the clones are affected. How can I create independent clones with unique attributes?
var oload = new OBJLoader();
oload.load("objects/tunnel1.obj", function(data)
{
tunnels[0] = data;
tunnels[0].traverse(function(titem)
{
if(titem.isMesh)
{
if(titem.name.toUpperCase().indexOf("INNER")>-1)
{
titem.material = new THREE.MeshBasicMaterial(map: wnewtex, vertexColors: true);
}
}
});
});
for(var x = 0; x < width; x++)
{
for(var y = 0; y < height; y++)
{
if(leveldata[x][y] == 15)
{
tunnels.push(tunnels[0].clone());
tunnels[tunnels.length-1].position.set(-x*2,newh,(height-y-1)*2);
scene.add(tunnels[tunnels.length-1]);
}
else if(leveldata[x][y] == 16)
{
tunnels.push(tunnels[0].clone());
tunnels[tunnels.length-1].position.set(-x*2,newh,(height-y-1)*2);
tunnels[tunnels.length-1].rotation.set(0,Math.PI/2,0);
scene.add(tunnels[tunnels.length-1]);
}
else if(leveldata[x][y] == 23)
{
tunnels.push(tunnels[0].clone());
tunnels[tunnels.length-1].position.set(-x*2,newh,(height-y-1)*2);
tunnels[tunnels.length-1].rotation.set(0,Math.PI/2,0);
tunnels[tunnels.length-1].traverse(function(titem)
{
if(titem.isMesh)
{
if(titem.name.toUpperCase().indexOf("INNER")>-1)
{
titem.geometry.setAttribute("color",new THREE.BufferAttribute(new Float32Array(item.geometry.attributes.position.count*3), 3 ));
var colort = titem.geometry.attributes.color.array;
for(var v2 = 0; v2 < titem.geometry.attributes.position.count*3; v2+=3)
{ // Make Red
colort[v2+0] = 1;
colort[v2+1] = 0;
colort[v2+2] = 0;
}
titem.geometry.attributes.color.needsUpdate = true;
}
}
});
scene.add(tunnels[tunnels.length-1]);
}
else if(leveldata[x][y] == 26)
{
tunnels.push(tunnels[0].clone());
tunnels[tunnels.length-1].position.set(-x*2,newh,(height-y-1)*2);
tunnels[tunnels.length-1].rotation.set(0,Math.PI/2,0);
tunnels[tunnels.length-1].traverse(function(titem)
{
if(titem.isMesh)
{
if(titem.name.toUpperCase().indexOf("INNER")>-1)
{
titem.geometry.setAttribute("color",new THREE.BufferAttribute(new Float32Array(item.geometry.attributes.position.count*3), 3 ));
var colort = titem.geometry.attributes.color.array;
for(var v2 = 0; v2 < titem.geometry.attributes.position.count*3; v2+=3)
{ // Make Green
colort[v2+0] = 0;
colort[v2+1] = 1;
colort[v2+2] = 0;
}
titem.geometry.attributes.color.needsUpdate = true;
} // If 26 was the last tunnel adjusted
} // then even 23 turns green
});
scene.add(tunnels[tunnels.length-1]);
}
}
}
It appears that using .clone()
does not create independent copies of the attributes for the new object. Does anyone know of an alternative method to clone objects without linking their attributes?