在 JavaFX 中绘制 3d 曲面 [英] Drawing 3d surfaces in JavaFX

查看:34
本文介绍了在 JavaFX 中绘制 3d 曲面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用数学方程式在 JavaFX 中绘制 3d 图形,基本上是2个变量函数,例如:z=2xy和其他3D图形?
有什么办法可以在 JavaFX 中做到这一点,或者我需要另一个 Java 库.

解决方案

正如@Roland 所指出的,JavaFX 3D API 不包括除基本元素之外的其他元素,例如

triangleMesh.getFaces().setAll(0,0,20,0,21,0,...);

您将拥有可以在场景中渲染的网格.

第三方库

您可以查看

对于其他 3D 形状,请查看库中其余的 3D 复杂形状.

您还可以查看 VRL Studio,其中包括出色的 3D 功能绘图仪,其中包括其他事情.

How to draw 3d graphs in JavaFX using a mathematical equation, basically 2 variable functions, for example: z=2xy and other 3D figures?
Is there any way to do it in JavaFX or do I need another Java library for that.

解决方案

As @Roland points out, JavaFX 3D API doesn't include other than the basic elements, like TriangleMesh, which you can use to create complex shapes, like 3D graphs.

In fact, plotting 2D functions f=f(x,y) is be a very good use case for understanding how TriangleMesh works.

Basically you will need:

A function

The function can be expressed using built-in Function functional interface:

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 range of coordinates

If you think of a rectangular grid and a given number of the divisions, you will have a way to get all the plotting (x,y) points, and with the function, you will have the third coordinate z to generate the 3D points required for the mesh.

TriangleMesh triangleMesh = new TriangleMesh();
triangleMesh.getPoints().setAll(x0,y0,z0, x1,y1,z1, ...);

You will need to provide the texture coordinates if you want to have an image or a density map as a texture, or just an empty set of coordinates for now:

triangleMesh.getTexCoords().setAll(0,0);

Finally, you need to provide the faces, which are triangles. You just need to get the indices of the vertices for every triangle in your grid, 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 a scene.

Third party libraries

You can have a look at FXyz library, where you will find SurfacePlotMesh, that will do exactly as described above, including texture coordinates. The FXyz Sampler is an application to visualize most of the possibilities on this library. This is a sample of a plotted function:

For other 3D shapes, have a look to the rest of the 3D complex shapes in the library.

And you can have a look to VRL Studio, which includes a great 3D function plotter, among other things.

这篇关于在 JavaFX 中绘制 3d 曲面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆