B4J Tutorial 3D MeshView

This is a tiny tutorial on how to load a .obj file into a 3d MeshView and display it in B4J.

I have loaded .obj files that I downloaded from the web, but they tend to not stick to the same format that I wrote this for (to display 3d scans from my phone).

The program has 4 main parts
1, Read the TriangleMesh data from the .obj file (its basically just a text file)
2, Convert the lists into arrays.
3, Create the MeshView
4, Display the MeshView (its basically a node so can be added as normal)

The program also allows for rotating the object.
It tries to scale the model so it is 75% of the width/height of the node it is in ( a tall model uses the 75% height, a fat (not pc) wide model will use 75% of the width)

I have commented the code - but to understand the .obj format it's worth a google.

Unfortunately I cannot upload any of my scans as they are quite big (smallest is 1MB+).
So I have uploaded a screenshot of the output from loading the sony demo model.

I have tried it with big files - 10M+ faces and the conversion takes around 500ms, but to display, takes a few seconds as javafx loads the MeshView (this is still faster than some other mesh viewers I tried, which took 19 seconds to render the mesh)
 

Attachments

  • Sony_XZ1_mesh_viewer.zip
    4.7 KB · Views: 510
  • headscan.png
    headscan.png
    229.9 KB · Views: 629
Last edited:

Daestrum

Expert
Licensed User
Longtime User
Found a way to speed up the mesh building (in the java code in main class)
B4X:
...
} else {
faceT.setVertexFormat(VertexFormat.POINT_TEXCOORD);
}
// the following replace the existing lines
faceT.getPoints().setAll(vertsx,0,vertsx.length);

if (normsx.length != 0){
   faceT.getNormals().setAll(normsx,0,normsx.length);
}

   faceT.getTexCoords().setAll(texCoordsx,0,texCoordsx.length);

   faceT.getFaces().setAll(facesx,0,facesx.length);

// new lines end here
   MeshView mesh = new MeshView(faceT);

This minor change halved the mesh building time (180 ms down to 81 ms)
 
Top