# When i build a mesh with triangles is it possible to select each triangle separately?

**URL:** <https://forum.babylonjs.com/t/when-i-build-a-mesh-with-triangles-is-it-possible-to-select-each-triangle-separately/13847>\
**Category:** Questions\
**Tags:** material\
**Created:** [September 2, 2020, 8:31am UTC](https://forum.babylonjs.com/t/when-i-build-a-mesh-with-triangles-is-it-possible-to-select-each-triangle-separately/13847 "2020-09-02T08:31:23Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Arash\_Bagheri](https://avatars.discourse-cdn.com/v4/letter/a/dfb087/32.png) [@Arash\_Bagheri](https://forum.babylonjs.com/u/Arash_Bagheri)\
**Post date:** [September 2, 2020, 8:31am UTC](https://forum.babylonjs.com/t/when-i-build-a-mesh-with-triangles-is-it-possible-to-select-each-triangle-separately/13847/1 "2020-09-02T08:31:23Z")

</div>

hello everyone

I have build a mesh with the help of triangles, I want to know is it possible to select each triangle separately?  
this is my PG for 6 triangles :  
[https://www.babylonjs-playground.com/#VKBJN#874](https://www.babylonjs-playground.com/#VKBJN#874)

and below screenshot is my final model with about 100,000 triangles.

 ![Screen Shot 1399-06-12 at 11.55.14](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/2X/0/0e45d29b82bc0dd11ea9662921762294aca3ac79.jpeg)

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [September 2, 2020, 1:53pm UTC](https://forum.babylonjs.com/t/when-i-build-a-mesh-with-triangles-is-it-possible-to-select-each-triangle-separately/13847/2 "2020-09-02T13:53:04Z")

</div>

You should be able to access all the required info from the ray cast functionnalities:

[https://doc.babylonjs.com/babylon101/picking\_collisions#advanced-picking-features](https://doc.babylonjs.com/babylon101/picking_collisions#advanced-picking-features)

and [Raycasts - Babylon.js Documentation](https://doc.babylonjs.com/babylon101/raycasts)

---

<div class="post-metadata">

**Author:** ![Arash\_Bagheri](https://avatars.discourse-cdn.com/v4/letter/a/dfb087/32.png) [@Arash\_Bagheri](https://forum.babylonjs.com/u/Arash_Bagheri)\
**Post date:** [September 5, 2020, 6:00am UTC](https://forum.babylonjs.com/t/when-i-build-a-mesh-with-triangles-is-it-possible-to-select-each-triangle-separately/13847/3 "2020-09-05T06:00:34Z")

</div>

Thank you so much 🙏🏻 🙏🏻

---

<div class="post-metadata">

**Author:** ![Arash\_Bagheri](https://avatars.discourse-cdn.com/v4/letter/a/dfb087/32.png) [@Arash\_Bagheri](https://forum.babylonjs.com/u/Arash_Bagheri)\
**Post date:** [September 5, 2020, 6:30am UTC](https://forum.babylonjs.com/t/when-i-build-a-mesh-with-triangles-is-it-possible-to-select-each-triangle-separately/13847/4 "2020-09-05T06:30:34Z")

</div>

Is it possible to select each triangles separately when I use triangle to build mesh?  
this is my PG but when I wanted to selected each triangle the problem is whole of the triangles is selected.  
[https://www.babylonjs-playground.com/#VKBJN#876](https://www.babylonjs-playground.com/#VKBJN#876)

---

<div class="post-metadata">

**Author:** ![JohnK](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/johnk/32/26764_2.png) [@JohnK](https://forum.babylonjs.com/u/JohnK)\
**Post date:** [September 6, 2020, 8:12am UTC](https://forum.babylonjs.com/t/when-i-build-a-mesh-with-triangles-is-it-possible-to-select-each-triangle-separately/13847/5 "2020-09-06T08:12:15Z")

</div>

> [@Arash\_Bagheri](#):
>
> com/#VKBJN#876

pickedMesh is not the only property returned see [https://doc.babylonjs.com/api/classes/babylon.pickinginfo](https://doc.babylonjs.com/api/classes/babylon.pickinginfo) you can also get faceId.

---

<div class="post-metadata">

**Author:** ![Arash\_Bagheri](https://avatars.discourse-cdn.com/v4/letter/a/dfb087/32.png) [@Arash\_Bagheri](https://forum.babylonjs.com/u/Arash_Bagheri)\
**Post date:** [September 6, 2020, 9:42am UTC](https://forum.babylonjs.com/t/when-i-build-a-mesh-with-triangles-is-it-possible-to-select-each-triangle-separately/13847/6 "2020-09-06T09:42:36Z")

</div>

> [@JohnK](#):
>
> PickingInfo - Babylon.js Documentation

Thank you so much.  
I used faceId but how can I use bv and bu to get the coordinate?  
i use faceId in my PG in this way : [https://www.babylonjs-playground.com/#VKBJN#878](https://www.babylonjs-playground.com/#VKBJN#878)

---

<div class="post-metadata">

**Author:** ![JohnK](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/johnk/32/26764_2.png) [@JohnK](https://forum.babylonjs.com/u/JohnK)\
**Post date:** [September 6, 2020, 10:16am UTC](https://forum.babylonjs.com/t/when-i-build-a-mesh-with-triangles-is-it-possible-to-select-each-triangle-separately/13847/7 "2020-09-06T10:16:13Z")

</div>

Each triangle requires three indices not just one see [Create Custom Meshes - Babylon.js Documentation](https://doc.babylonjs.com/how_to/custom)

One triangle (sometimes called a facet or face) has a faceId which links to three positions.

---

<div class="post-metadata">

**Author:** ![Arash\_Bagheri](https://avatars.discourse-cdn.com/v4/letter/a/dfb087/32.png) [@Arash\_Bagheri](https://forum.babylonjs.com/u/Arash_Bagheri)\
**Post date:** [September 6, 2020, 10:23am UTC](https://forum.babylonjs.com/t/when-i-build-a-mesh-with-triangles-is-it-possible-to-select-each-triangle-separately/13847/8 "2020-09-06T10:23:57Z")

</div>

> [@JohnK](#):
>
> Create Custom Meshes - Babylon.js Documentation

thanks, so if wanted to show three indices of each triangles what should I do?

---

<div class="post-metadata">

**Author:** ![JohnK](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/johnk/32/26764_2.png) [@JohnK](https://forum.babylonjs.com/u/JohnK)\
**Post date:** [September 6, 2020, 11:07am UTC](https://forum.babylonjs.com/t/when-i-build-a-mesh-with-triangles-is-it-possible-to-select-each-triangle-separately/13847/9 "2020-09-06T11:07:21Z")

</div>

[https://www.babylonjs-playground.com/#VKBJN#879](https://www.babylonjs-playground.com/#VKBJN#879)

from the three indices you can find the three positions for each of the triangle vertices.

x = myPositions(3 \* index);  
y = myPositions(3 \* index + 1);  
z = myPositions(3 \* index + 2);

From the positions you can find the min and max values for x, y, and z, create two vector3s minVec and maxVec and use this to create a new bounding box [BoundingBox - Babylon.js Documentation](https://doc.babylonjs.com/api/classes/babylon.boundingbox#constructor)

---

<div class="post-metadata">

**Author:** ![Arash\_Bagheri](https://avatars.discourse-cdn.com/v4/letter/a/dfb087/32.png) [@Arash\_Bagheri](https://forum.babylonjs.com/u/Arash_Bagheri)\
**Post date:** [September 6, 2020, 11:21am UTC](https://forum.babylonjs.com/t/when-i-build-a-mesh-with-triangles-is-it-possible-to-select-each-triangle-separately/13847/10 "2020-09-06T11:21:04Z")

</div>

thank you so much , its great. and is it possible to change the color of 3 indices when I selected them ?

---

<div class="post-metadata">

**Author:** ![JohnK](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/johnk/32/26764_2.png) [@JohnK](https://forum.babylonjs.com/u/JohnK)\
**Post date:** [September 6, 2020, 11:24am UTC](https://forum.babylonjs.com/t/when-i-build-a-mesh-with-triangles-is-it-possible-to-select-each-triangle-separately/13847/11 "2020-09-06T11:24:07Z")

</div>

[https://doc.babylonjs.com/how\_to/updating\_vertices#adding-to-the-data](https://doc.babylonjs.com/how_to/updating_vertices#adding-to-the-data)

It is always worth checking Further Reading on any docs page you are using to learn.

---

<div class="post-metadata">

**Author:** ![Arash\_Bagheri](https://avatars.discourse-cdn.com/v4/letter/a/dfb087/32.png) [@Arash\_Bagheri](https://forum.babylonjs.com/u/Arash_Bagheri)\
**Post date:** [September 6, 2020, 11:32am UTC](https://forum.babylonjs.com/t/when-i-build-a-mesh-with-triangles-is-it-possible-to-select-each-triangle-separately/13847/12 "2020-09-06T11:32:50Z")

</div>

sorry for my questions, But I didn’t understand how can I access to position with faceId. should I use bu and bv?

---

<div class="post-metadata">

**Author:** ![JohnK](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/johnk/32/26764_2.png) [@JohnK](https://forum.babylonjs.com/u/JohnK)\
**Post date:** [September 6, 2020, 11:36am UTC](https://forum.babylonjs.com/t/when-i-build-a-mesh-with-triangles-is-it-possible-to-select-each-triangle-separately/13847/13 "2020-09-06T11:36:26Z")

</div>

As I have shown use faceId to access the three indices for the triangle. From each of these indices access the x, y and z values from the vertex.positions array which you put equal to myPositions.

---

<div class="post-metadata">

**Author:** ![Arash\_Bagheri](https://avatars.discourse-cdn.com/v4/letter/a/dfb087/32.png) [@Arash\_Bagheri](https://forum.babylonjs.com/u/Arash_Bagheri)\
**Post date:** [September 6, 2020, 11:56am UTC](https://forum.babylonjs.com/t/when-i-build-a-mesh-with-triangles-is-it-possible-to-select-each-triangle-separately/13847/14 "2020-09-06T11:56:08Z")

</div>

thank you so much. should i use vertexData.positions[index0] ?

---

<div class="post-metadata">

**Author:** ![JohnK](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/johnk/32/26764_2.png) [@JohnK](https://forum.babylonjs.com/u/JohnK)\
**Post date:** [September 6, 2020, 12:00pm UTC](https://forum.babylonjs.com/t/when-i-build-a-mesh-with-triangles-is-it-possible-to-select-each-triangle-separately/13847/15 "2020-09-06T12:00:27Z")

</div>

No!

> [@JohnK](#):
>
> x = myPositions(3 \* index);  
> y = myPositions(3 \* index + 1);  
> z = myPositions(3 \* index + 2);

replace x, y, z in turn by x0, y0, z0, x1, y1, z1, x2, y2, z2 and index in turn by index0, index1, index2 to get pos0, pos1, pos2

---

<div class="post-metadata">

**Author:** ![Arash\_Bagheri](https://avatars.discourse-cdn.com/v4/letter/a/dfb087/32.png) [@Arash\_Bagheri](https://forum.babylonjs.com/u/Arash_Bagheri)\
**Post date:** [September 7, 2020, 7:54am UTC](https://forum.babylonjs.com/t/when-i-build-a-mesh-with-triangles-is-it-possible-to-select-each-triangle-separately/13847/16 "2020-09-07T07:54:08Z")

</div>

I try to get the position in this way but when I use myPositions(3\*index) I got this error:

 ![Screen Shot 1399-06-17 at 12.22.27](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/2X/0/0ec07a87845dd6b477e2aeac2a8a3204f9768d61.png)

---

<div class="post-metadata">

**Author:** ![JohnK](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/johnk/32/26764_2.png) [@JohnK](https://forum.babylonjs.com/u/JohnK)\
**Post date:** [September 7, 2020, 7:55am UTC](https://forum.babylonjs.com/t/when-i-build-a-mesh-with-triangles-is-it-possible-to-select-each-triangle-separately/13847/17 "2020-09-07T07:55:44Z")

</div>

Sorry my mistake - myPositions is an array

x = myPositions[3 \* index];  
y = myPositions[3 \* index + 1];  
z = myPositions[3 \* index + 2];

---

<div class="post-metadata">

**Author:** ![Arash\_Bagheri](https://avatars.discourse-cdn.com/v4/letter/a/dfb087/32.png) [@Arash\_Bagheri](https://forum.babylonjs.com/u/Arash_Bagheri)\
**Post date:** [September 7, 2020, 7:59am UTC](https://forum.babylonjs.com/t/when-i-build-a-mesh-with-triangles-is-it-possible-to-select-each-triangle-separately/13847/18 "2020-09-07T07:59:28Z")

</div>

thank you so much. 🙏🏻 🙏🏻

---

<div class="post-metadata">

**Author:** ![Arash\_Bagheri](https://avatars.discourse-cdn.com/v4/letter/a/dfb087/32.png) [@Arash\_Bagheri](https://forum.babylonjs.com/u/Arash_Bagheri)\
**Post date:** [September 7, 2020, 8:23am UTC](https://forum.babylonjs.com/t/when-i-build-a-mesh-with-triangles-is-it-possible-to-select-each-triangle-separately/13847/19 "2020-09-07T08:23:22Z")

</div>

sorry to be bothering you again, but is it possible to help me know how can I define BoundingBox correctly?

```
    x0 = myPositions[3 * index0];
    y0 = myPositions[3 * index0 + 1];
    z0 = myPositions[3 * index0 + 2];

    x1 = myPositions[3 * index1];
    y1 = myPositions[3 * index1 + 1];
    z1 = myPositions[3 * index1 + 2];

    x2 = myPositions[3 * index2];
    y2 = myPositions[3 * index2 + 1];
    z2 = myPositions[3 * index2 + 2];

    var minX = Math.min(x0, x1, x2);
    var minY = Math.min(y0, y1, y2);
    var minZ = Math.min(z0, z1, z2);

    var maxX = Math.max(x0, x1, x2);
    var maxY = Math.max(y0, y1, y2);
    var maxZ = Math.max(z0, z1, z2);

    var minVec = new BABYLON.Vector3(minX, minY, minZ);
    var maxVec = new BABYLON.Vector3(maxX, maxY, maxZ);

    var BoundingBox = new BoundingBox(minVec, maxVec);
```

---

<div class="post-metadata">

**Author:** ![JohnK](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/johnk/32/26764_2.png) [@JohnK](https://forum.babylonjs.com/u/JohnK)\
**Post date:** [September 7, 2020, 8:35am UTC](https://forum.babylonjs.com/t/when-i-build-a-mesh-with-triangles-is-it-possible-to-select-each-triangle-separately/13847/20 "2020-09-07T08:35:44Z")

</div>

BoundingBox, like Vector3, is in the BABYLON name space so new BABYLON.BoundingBox(minVec, maxVec);

[Next page](https://forum.babylonjs.com/t/when-i-build-a-mesh-with-triangles-is-it-possible-to-select-each-triangle-separately/13847.md?page=2)
