Problem creating custom mesh from txt files

Hey! I am having some trouble creating a custom mesh from txt files. In the playground linked at the bottom of this post you will see that I load in two txt files, one containing vertices of the mesh and the other containing the indices of the faces which each vertex belongs to. We are able to load the txt files into Blender, where it looks correct, and export it as an stl file, which I have also loaded into the playground. The stl file loads as expected and this is how we want the mesh to look when created using the txt files.

As you can see there are two obvious problems:

  • The first is that vertices are arranged in a different pattern than the stl file. They might be loading in in the wrong order, but I am not sure about that. This also might be causing the second issue.
  • The second is that the meshes are not complete. We believe it might be a problem with the material but are not too sure how to go about fixing this.

Also, we believe that the coordinates of the txt file vertices are correct, and it is possible that the stl file coordinates are flipped in some way. (ex: xzy instead of xyz).

Any help would be greatly appreciated, thanks so much!

Playground: https://playground.babylonjs.com/#EQ46H3#4

Edit: the playground now has two stl files. The first (which is uncommented) is an stl file created using numpy-stl · PyPI
The second (which is currently commented) was created by exporting from Blender.

This is what the mesh looks like in Blender:

1 Like

Hi,

I guess some of the faces have incorrect ordering of indices. I.e. You are actually showing the back of the faces. You need to change the order of the indices for these faces.

Take this PG as example. It has two triangles. One facing front, and the other facing back. You can rotate the camera to see the triangle that is back facing.
https://playground.babylonjs.com/#VKBJN#8

If you modified the order of indices (see line 16), you can see the back facing triangle is now also front facing.
https://playground.babylonjs.com/#VKBJN#1433

This is a screenshot I took from your PG. I moved the camera inside the brain and look out. The missing faces look solid when look inside out.

2 Likes

The winding is wrong indeed, you can fix it by inverting index 1 and index 2 for each triangles:

https://playground.babylonjs.com/#EQ46H3#6

2 Likes

Hey thanks for your answer. So we found a temporary fix, as seen in this playground: https://playground.babylonjs.com/#EQ46H3#16. By inverting all of the vertices the mesh appears to be correct, the only problem is that when we compare the coordinates to Blender (where we got the mesh from) the coordinates are inverted, so (x,y,z) is now (-x,-y,-z). When we use your answer the mesh is not messed up as it was before (the before can be seen here: https://playground.babylonjs.com/#EQ46H3#17), but is still flipped the wrong way (as seen here: https://playground.babylonjs.com/#EQ46H3#18). So when we use the negative method, it looks right, but the coordinates are still wrong. Could this be an issue caused by the interaction of potentially different coordinate spaces between Blender and Babylon? Or is there a way to flip the faces that will invert the mesh as we want while maintaining the same coordinates as in Blender? Thanks so much for your help!

In my PG, the coordinates are the same (I think?) than in Blender. If the position of the mesh is not ok, you can simply apply some rotations on it.

The coordinate systems in Blender and Babylon.js are not the same, so you can’t have the same display in both without doing some extra steps.