How to draw a Mesh if I only know vertex location?

Hello,
I have a list of vertices location in a STL file (mesh.vertices), but I don`t have indices order (mesh.triangles), how can I draw this mesh in Unity?

Thank you

I don’t think that’s enough data to rebuild your mesh accurately… What you have there is a point cloud, and without triangle indices to define where the surfaces are, the mesh faces could theoretically be anywhere.

You could try running the vertex collection through surfacing algorithms, but at best you’d be approximating the original geometry. Details might get jumbled up or lost, depending on how your mesh is set up.

More information about what this mesh is supposed to be would be helpful. If you have some idea of what your mesh is supposed to look like, you’d have better chances of finding an algorithm that will suit your needs.

Cheers

1 Like

Hello Harvester,
for each triangle I have the position of the 3 vertices and the normal verctor.
I think that normal verctor can help me to define where surfaces are, but I don’t know how to use it to create mesh in unity.

it seems you have non-indexed mesh
just set indices to 0, 1, 2, 3, 4, 5, 6…

The normal vector alone is not enough to know how a mesh is built. Is there a reason why you want to draw the .stl file manually? You could just import the mesh into Unity after exporting it into another format first (using Blender for instance).

@mholub 's suggestion is correct. In an STL file, the index data is assumed as part of the list of vertices itself.

For each facet, you’ll load its three vertices into vertices[0,1,2], and set triangles[0] to 0, triangles[1] to 1, and triangles[2] to 2. Then, you’ll want to set normals[0,1,2] to that facet’s normal. Then add 3 to all the numbers involved and repeat.

After you’ve done this, you should know that your triangles will all be separate pieces and the Unity renderer will be unable to smooth it - it’ll look very polygonal. If you want to fix this, you’ll have to run an algorithm to combine the indices down to other vertices that match.

1 Like