Getting data about all sides of a mesh

Hello,

I want to get specific data of all the sides of a mesh, but I’m not sure how I would achieve what I want.

To elaborate, for each side of a mesh, I want to create an object called side.

Side should have following items:

  • the amount of points/vertices that make up the side (so for a box 4)
  • an array of all the vertices (as position vectors) that make up the side (preferably in clockwise-like order, so if the first vert in the array is the bottom-left one, the 2nd vert in the array shouldn’t be the top-right vert etc.)

In this case, for a box, the array would have 4 vectors.

The position of the verts should also take the mesh transformations into account (so rotation, scaling etc.)

I’m not really sure where to begin, as fetching the vertices data of a box mesh for example, returns an array of 72 vertices, and I’m not sure how to iterate through these vertices in order to get the data.

Tagging @Evgeni_Popov

What I would do:

  • create a transformation matrix to correspond to the side you want. The easiest way would probably be to create a camera, set the position/orientation as you want, and then call camera.getTransformationMatrix().
  • loop over all triangles of the mesh. For each triangle:
    • transform (Vector3.TransformCoordinates) the 3 vertices with the world matrix of the mesh
    • project (Vector3.Project) the 3 vertices with the transformation matrix
    • test if the triangle is visible (backface culling). If the triangle is visible, store the 3 indices of the 3 vertices of the face
  • at the end of the loop, loop over the indices of the faces you stored in the previous step and retrieve the corresponding vertices (make sure you don’t duplicate vertices!)
1 Like

Thank you :+1:
Will try

I’m at the point where I am able to create an array which stores all the sides and the corresponding data (by using facet data and grouping quad faces and getting the info of their vertices), which gives me info about indices, amount of vertices, and an array of vertices for each side.
Problem is that the vertices are not stored in (counter-)clock-wise order in the array. How would I be able to rearrange them in that manner?

PG: https://playground.babylonjs.com/#1RZ1LE#17

You could foreach triplet (triangle) in the array simply inverse the second and third element. that should do it.

1 Like