InstancedMesh Position

can i have two instancedMesh with 50 object on each with same location and size?
Is it correct to create two Instancedmesh with just one “for” loop?

const mesh1 = instancedmesh(geometry,material,50)
const mesh2 = instancedmesh(geometry,material,50)
for (var i = 1; i < 50; ++i) {
mesh1.setMatrixAt(i,…
mesh2.setMatrixAt(i,…

I want these objects to be placed in exactly the same place, given that the location of the first object is random
My problem is that I want to use the x and z position of mesh1 for mesh2 but not works

mesh1.setMatrixAt(
  i,
  new THREE.Matrix4()
    .makeTranslation(2, 0 ,1)
    .multiply(new THREE.Matrix4().makeScale(0, 0.1, 0))
);

//-----------------
mesh2.setMatrixAt(
i,
new THREE.Matrix4()
.makeTranslation(mesh1.position.x, 0, mesh1.position.z)
.multiply(new THREE.Matrix4().makeScale(0, 0.05, 0))
);

//------
mesh1.position.x and z dont work in mesh2 traslation

I keep position, scale and rotation in a separate data structure, then loop over this to set the instances as needed. Perhaps this solution works for you.

Note that when two objects overlap (even perfectly) there may be flickering if the colors are different.

thank you anidivr

You should not create matrices all over again, create those 2 you use outside the loop and reuse them.

If you want a second Instanced Mesh with the exact same matrices you could also just replace the butter attribute with the first one in the second.

instancedMesh2.instanceMatrix = instancedMesh1.instanceMatrix

1 Like

thank you fyrestar
when im using for example const x = math.random() for x matrix ,I can’t use outside the loop