# How to position instances of a mesh in to any format

**URL:** https://discourse.threejs.org/t/how-to-position-instances-of-a-mesh-in-to-any-format/48895
**Category:** Questions
**Created:** [March 9, 2023, 7:39am UTC](https://discourse.threejs.org/t/how-to-position-instances-of-a-mesh-in-to-any-format/48895 "2023-03-09T07:39:57Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![deancarter](https://avatars.discourse-cdn.com/v4/letter/d/f17d59/32.png) [@deancarter](https://discourse.threejs.org/u/deancarter)
#### Post date: [March 9, 2023, 7:39am UTC](https://discourse.threejs.org/t/how-to-position-instances-of-a-mesh-in-to-any-format/48895/1 "2023-03-09T07:39:57Z")

</div>

how to position instances of a mesh in to any format such as having the instances on a plane and shape the instances in a hexagon

---

<div class="post-metadata">

### Author: ![PavelBoytchev](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/pavelboytchev/32/38639_2.png) [@PavelBoytchev](https://discourse.threejs.org/u/PavelBoytchev)
#### Post date: [March 11, 2023, 2:41pm UTC](https://discourse.threejs.org/t/how-to-position-instances-of-a-mesh-in-to-any-format/48895/2 "2023-03-11T14:41:06Z")

</div>

Your question is somewhat not clear to me. One of the possible interpretations is: how to position individual instances of a mesh, so that each instance has its own position. If this is your question, look at this example, which positions 8 instances in an octagonal formation:

[https://codepen.io/boytchev/full/yLxpGBX](https://codepen.io/boytchev/full/yLxpGBX)

[![image](https://canada1.discourse-cdn.com/flex035/uploads/threejs/original/3X/a/f/afac9244a36f4ff0c154581681e6ba280ac0ad3c.jpeg)](https://codepen.io/boytchev/full/yLxpGBX)

---

<div class="post-metadata">

### Author: ![deancarter](https://avatars.discourse-cdn.com/v4/letter/d/f17d59/32.png) [@deancarter](https://discourse.threejs.org/u/deancarter)
#### Post date: [March 11, 2023, 5:55pm UTC](https://discourse.threejs.org/t/how-to-position-instances-of-a-mesh-in-to-any-format/48895/3 "2023-03-11T17:55:08Z")

</div>

what i meant was how would i be able to instance grass mesh or shader with accurate positioning on a plane, similar to if i imported a house glb file and wanted grass instances around it,

this post was close by using vertex painting in blender but was not very clear on how to achieve it exactly.

> [@Instancing within bounds of mesh](https://discourse.threejs.org/t/instancing-within-bounds-of-mesh/24899):
>
> Hello community, I have made a sphere with “land masses” on it, I am trying to find the best way of instancing geometries on only the land masses, having them face the right way up, without going outside the “lands” bounds, I’ve been looking into using the position attribute of the land geometry but the vertices are not even and very sporadic (as its low poly and an uneven distribution of the topology) does anyone know a lightweight best approach to achieving this or some resources to point in …

---

<div class="post-metadata">

### Author: ![PavelBoytchev](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/pavelboytchev/32/38639_2.png) [@PavelBoytchev](https://discourse.threejs.org/u/PavelBoytchev)
#### Post date: [March 11, 2023, 6:26pm UTC](https://discourse.threejs.org/t/how-to-position-instances-of-a-mesh-in-to-any-format/48895/4 "2023-03-11T18:26:55Z")

</div>

If you plan to use `MeshSurfaceSampler` and you want to exclude the area where the house is, you should do this:

- pick an attribute that keeps weights of every vertex in the mesh
- let the sampler know what is the name of this attribute (`setWeightAttribute`)
- set the weight 0 for each vertex under the house , and 1 to each vertex around the house
- build an internal “map” of allowed and forbidden areas (`build`)
- and then you can pick random positions in the allowed areas (`sample`)

* * *

I’m afraid that I do not understand your question again. Maybe someone could be more helpful

---

<div class="post-metadata">

### Author: ![Lawrence3DPK](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/lawrence3dpk/32/29184_2.png) [@Lawrence3DPK](https://discourse.threejs.org/u/Lawrence3DPK)
#### Post date: [March 11, 2023, 6:48pm UTC](https://discourse.threejs.org/t/how-to-position-instances-of-a-mesh-in-to-any-format/48895/5 "2023-03-11T18:48:05Z")

</div>

As @PavelBoytchev has mentioned you need to use the surface sampler, here’s some example code to put you in the correct direction…

```javascript
sampler = new MeshSurfaceSampler( groundobj )
.setWeightAttribute( 'color' )
.build();
for ( let i = 0; i < instanceCount; i ++ ) {
  sampler.sample( position ); 
  dummy.position.copy(position) 
  InstanceMesh.setMatrixAt( i, dummy.matrix)
  InstanceMesh.instanceMatrix.needsUpdate = true;
} 

```

---

<div class="post-metadata">

### Author: ![deancarter](https://avatars.discourse-cdn.com/v4/letter/d/f17d59/32.png) [@deancarter](https://discourse.threejs.org/u/deancarter)
#### Post date: [March 11, 2023, 7:08pm UTC](https://discourse.threejs.org/t/how-to-position-instances-of-a-mesh-in-to-any-format/48895/6 "2023-03-11T19:08:06Z")

</div>

thank you!
