简体   繁体   English

在JavaFX中绘制3d表面

[英]Drawing 3d surfaces in JavaFX

How to draw 3d graphs in JavaFX using a mathematical equation, basically 2 variable functions, for example: z=2xy and other 3D figures? 如何使用数学方程在JavaFX中绘制三维图形,基本上是2个变量函数,例如: z=2xy和其他3D图形?
Is there any way to do it in JavaFX or do I need another Java library for that. 有没有办法在JavaFX中做到这一点,还是我需要另一个Java库。

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. 正如@Roland所指出的,JavaFX 3D API不包括除TriangleMesh之外的基本元素,您可以使用它来创建复杂的形状,如3D图形。

In fact, plotting 2D functions f=f(x,y) is be a very good use case for understanding how TriangleMesh works. 实际上,绘制2D函数f=f(x,y)是理解TriangleMesh如何工作的一个非常好的用例。

Basically you will need: 基本上你需要:

A function 一个功能

The function can be expressed using built-in Function functional interface: 该功能可以使用内置的Function功能界面表达:

Function<Point2D,Number> function2D;

so for any pair of coordinates (x,y) it will return a value: 所以对于任何一对坐标(x,y),它将返回一个值:

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. 如果你想到一个矩形网格和给定数量的分区,你将有办法获得所有绘图(x,y)点,并且通过该函数,你将得到第三个坐标z来生成所需的3D点数对于网格。

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: 您只需要获取网格中每个三角形的顶点索引,就像在此示例中一样 ,在这种情况下使用0作为纹理索引:

面孔

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. 您可以查看FXyz库,您可以在其中找到SurfacePlotMesh ,它将完全如上所述,包括纹理坐标。 The FXyz Sampler is an application to visualize most of the possibilities on this library. FXyz Sampler是一个可视化此库的大部分可能性的应用程序。 This is a sample of a plotted function: 这是绘制函数的示例:

FXyz

For other 3D shapes, have a look to the rest of the 3D complex shapes in the library. 对于其他3D形状,请查看库中其余的3D复杂形状。

And you can have a look to VRL Studio , which includes a great 3D function plotter, among other things. 您可以查看VRL Studio ,其中包括一个出色的3D功能绘图仪等。

I wrote a JavaFX demo application (embedded in swing) which can plot 3d points. 我写了一个JavaFX演示应用程序(嵌入在swing中),可以绘制3d点。 You can create/calculate some 3d points with your function and than just draw it. 您可以使用您的函数创建/计算一些3d点,而不仅仅是绘制它。 Check out the StarterFrame class, where the points are generated. 查看生成点的StarterFrame类。 More points means a more detailed plot. 更多点意味着更详细的情节。 Maybe this helps you to write something on your own. 也许这可以帮助你自己写点东西。 Otherwise i would recommend a library. 否则我会推荐一个图书馆。

https://github.com/adihubba/javafx-3d-surface-chart https://github.com/adihubba/javafx-3d-surface-chart

高斯正态分布

JavaFX doesn't have a built-in mechanism to draw 3d graphs. JavaFX没有用于绘制3d图形的内置机制。 You have to use a 3rd party library. 您必须使用第三方库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM