# InstancedMesh Position

**URL:** https://discourse.threejs.org/t/instancedmesh-position/42357
**Category:** Questions
**Tags:** materials, geometry
**Created:** [September 8, 2022, 11:38am UTC](https://discourse.threejs.org/t/instancedmesh-position/42357 "2022-09-08T11:38:22Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Teymoor](https://avatars.discourse-cdn.com/v4/letter/t/ad7895/32.png) [@Teymoor](https://discourse.threejs.org/u/Teymoor)
#### Post date: [September 8, 2022, 11:38am UTC](https://discourse.threejs.org/t/instancedmesh-position/42357/1 "2022-09-08T11:38:22Z")

</div>

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

```javascript
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

---

<div class="post-metadata">

### Author: ![anidivr](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/anidivr/32/32924_2.png) [@anidivr](https://discourse.threejs.org/u/anidivr)
#### Post date: [September 8, 2022, 1:42pm UTC](https://discourse.threejs.org/t/instancedmesh-position/42357/2 "2022-09-08T13:42:19Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Teymoor](https://avatars.discourse-cdn.com/v4/letter/t/ad7895/32.png) [@Teymoor](https://discourse.threejs.org/u/Teymoor)
#### Post date: [September 8, 2022, 1:43pm UTC](https://discourse.threejs.org/t/instancedmesh-position/42357/3 "2022-09-08T13:43:14Z")

</div>

thank you anidivr

---

<div class="post-metadata">

### Author: ![Fyrestar](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/fyrestar/32/52793_2.png) [@Fyrestar](https://discourse.threejs.org/u/Fyrestar)
#### Post date: [September 8, 2022, 3:30pm UTC](https://discourse.threejs.org/t/instancedmesh-position/42357/4 "2022-09-08T15:30:35Z")

</div>

> [@Teymoor](#):
>
> ```javascript
> mesh1.setMatrixAt(
> i,
> new THREE.Matrix4()
> .makeTranslation(2, 0 ,1)
> .multiply(new THREE.Matrix4().makeScale(0, 0.1, 0))
> );
> 
> ```

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`

---

<div class="post-metadata">

### Author: ![Teymoor](https://avatars.discourse-cdn.com/v4/letter/t/ad7895/32.png) [@Teymoor](https://discourse.threejs.org/u/Teymoor)
#### Post date: [September 8, 2022, 9:10pm UTC](https://discourse.threejs.org/t/instancedmesh-position/42357/5 "2022-09-08T21:10:08Z")

</div>

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