I would like to create a 3D demo application with JavaFX to visualize movements of points in 3D space and first I need to set up a coordinate grid for visual reference. Unfortunately, I was not able to find a sample code for a grid like in this picture:
Does anyone know what is the most practical way to create something like it?
There are a few solutions out there already.
FXyz3D library has a
CubeWorld
class, that gives you precisely a reference grid.It is quite easy to use. Just import the
'org.fxyz3d:fxyz3d:0.3.0'
dependency from JCenter and use it:As you can see, the solution is based on using 2D rectangles for each face, and the grid lines are created with 3D cylinders. It has very nice features (like self lightning or frontal faces according to camera don't show grid), but it is quite intensive in nodes (sample above has 168 nodes).
There are other solutions that use a lower number of nodes. For instance, for this sample, that also happens to be related to Leap Motion, I used a
TriangleMesh
.This is an easy solution, and with just two meshes. However, you see the triangles, instead of squares.
So let's try to get rid of the triangles. For that I'll use a
PolygonMesh
, as in this other question, based on the 3DViewer project that is available at the OpenJFX repository, contains already a PolygonalMesh implementation, that allows any number of points per face, so any polygon can be a face.This will give you a plane grid based in square faces:
So you can use 2 or 3 of them to create a coordinate system like this:
Note that I've duplicated the plane to mock a wider stroke every 5 lines.
Finally adding axes:
Now you have:
The total amount of nodes of this sample is 14.
Of course, it can be improved to add labels and many other features.