Get picked face (not triangle)

Hi!

I am new to BabylonJS och 3D in general, so I have started reading to make sure I go in the right direction. So bear with me.

My situation is that I have data in the form of an array with vertices, and an array with faces. For example, imagine a house with a triangle-formed roof, I will have 10 vertices and 7 faces (4 walls, 2 for the roof and 1 for the floor). The data is not triangulated. Each face may have data associated to it, for example an id, name, and much more.

I have read up on MeshBuilder.CreatePolyhedron and I can create a custom polyhedron by passing my vertices and faces as custom data.

const data = {
   "name":"abc",
   "category":["b"],
   "vertex": myVertices,
    "face": myFaces
};
const r= BABYLON.MeshBuilder.CreatePolyhedron("h" + i, {custom: data, faceColors: faceColors});

It seems that CreatePolyhedron do the triangulation, since I can pass a face with four vertices (I have not tried any more vertices).

But then it comes to identifying what face a user has clicked on.I played with the examples at: Babylon.js docs and tried PickingInfo.faceId. It did however return the id of the triangle, which is not useful for me. I want to know which one of the 7 faces was picked, so I can present the information about the face.

Is there any way to know which face (not triangle) a user has clicked on? Or am I going in the wrong direction by creating a Polyhedron, and I should instead be creating surfaces?

The only way would be to store the info in the face as well and for this you could maybe rely on the uv channel so then you could request .getTextureCoordinates on pickInfo to read the data back ?

Thanks for the insight! I am not sure right now how to do it, but I will investigate this as a viable solution.

1 Like

You can do you your own triangulation, set the references, create a custom mesh and you are good to go!

Click on faces of the mesh a watch the console.

First column pickInfo.faceId, second one the id of the original face the triangle was created from.

1 Like

Wow! Thanks! I have so much to learn. This was very helpful.

3 Likes