Drawing 3d surfaces in JavaFX
As @Roland points out, the JavaFX 3D API does not include other than basic elements, for example TriangleMesh
, which you can use to create complex shapes such as 3D graphics.
In fact, plotting 2D functions f=f(x,y)
is a very good option for understanding how it works TriangleMesh
.
Basically you will need:
Function
A function can be expressed using the built-in functional interface Function
:
Function<Point2D,Number> function2D;
so for any pair of coordinates (x, y) it will return a value:
double value = function2D.apply(new Point2D(x,y)).doubleValue();
Grid or coordinate range
If you think about a rectangular grid and a given number of divisions, you will be able to get all the construction points (x,y)
, and using the function you will have a third coordinate z
to create the 3D points needed for the grid.
TriangleMesh triangleMesh = new TriangleMesh();
triangleMesh.getPoints().setAll(x0,y0,z0, x1,y1,z1, ...);
You will need to provide texture coordinates if you want to have an image or density map as a texture, or just an empty set of coordinates:
triangleMesh.getTexCoords().setAll(0,0);
Finally, you need to provide faces that are triangles. You just need to get the vertex indices for each triangle in your mesh, like in this sample , using 0 for the texture indices in this case:
triangleMesh.getFaces().setAll(0,0,20,0,21,0,...);
And you will have your mesh ready to be rendered in the scene.
Third party libraries
You can look at the FXyz library where you can find SurfacePlotMesh
one that will work exactly as described above, including texture coordinates. FXyz Sampler is an application for visualizing most of the features in this library. This is a sample of the constructed function:
For other 3D shapes, take a look at the rest of the 3D complex shapes in the library.
And you can take a look at VRL Studio , which includes, among other things, an excellent 3D plotter.
source to share
I wrote a JavaFX demo application (built in swing) that can display 3D points. You can create / calculate some 3D points with your function, not just draw it. Check out the StarterFrame class where the points are generated. More points means a more detailed plot. Perhaps this will help you write something on your own. Otherwise, I would recommend the library.
https://github.com/adihubba/javafx-3d-surface-chart
source to share