3D 表面 JavaFX [英] 3D surface JavaFX
问题描述
我正在尝试在 JavaFX 中实现我自己的 3D 表面动画,但我不明白它应该工作的一切,有人可以帮助我了解应该去哪里吗?
已经知道使用class构建Mesh需要类对象
TraingleMesh
,然后必须使用方法mesh.getPoints.addAll(...);
但是..我的Function
在使用apply
方法后根本没有帮助我,因为第一个参数必须是数组浮点类型,而不是double
应用一些数据后的变量.- 我该如何解决这个问题?
我在这里找到了@Roland 创建的一些纹理和面孔的实现:
如果你添加一个密度图:
surface.setTextureModeVertices3D(1530, p -> p.magnitude());
你会得到这个:
现在如果你想要一个表面的动画,你只需要创建一个:
私有SurfacePlotMesh表面;私人长效;@覆盖公共无效开始(阶段primaryStage){PerspectiveCamera 相机 = new PerspectiveCamera(true);相机.setTranslateZ(-30);表面 = 新的 SurfacePlotMesh(p->Math.sin(p.magnitude() + 1e-10)/(p.magnitude() + 1e-10),20、20、100、100、4);surface.setCullFace(CullFace.NONE);surface.setTextureModeVertices3D(1530, p -> p.magnitude());surface.getTransforms().addAll(new Rotate(200, Rotate.X_AXIS), new Rotate(-20, Rotate.Y_AXIS));最终组组=新组(表面);Scene scene = new Scene(group, 600, 400, true, SceneAntialiasing.BALANCED);场景.setCamera(相机);primaryStage.setScene(场景);primaryStage.show();lastEffect = System.nanoTime();AtomicInteger count=new AtomicInteger();AnimationTimer timerEffect = new AnimationTimer() {@覆盖公共无效句柄(现在很长时间){如果(现在> lastEffect + 1_000_000_000l){双 t = (count.get() % 5 + 1);surface.setFunction2D(p -> Math.sin(t * p.magnitude() + 1e-10)/(t * p.magnitude() + 1e-10));count.getAndIncrement();lastEffect = 现在;}}};timerEffect.start();}
然后你会得到你的表面动画:
I am trying to implement my own 3D surface animation in JavaFX but i do not understand everything like it should works, could someone help me with understanding which should go where ?
Already know that to build Mesh by using class need class object
TraingleMesh
and then have to add points by using methodmesh.getPoints.addAll(...);
but.. myFunction<Double, Double>
after usingapply
method does not help me at all, cuz the first argument has to be array float type, notdouble
variable after applying some data.- How could i solve that problem ?
I found some implementations of texture and faces created by @Roland here :
- How textures and faces working ?
It's really important for me, thanks for help!
Have a look at the FXyz library. It is open source, and you can learn from the code.
For textures, have a look at this post.
FXyz has a SurfacePlotMesh
class that does exactly what you want: plot a 3D surface based on a function g = f(x,y)
, by using a Function<Point2D, Number> function
parameter.
It also includes texturing, so you can include a density map in terms of Function<Point3D, Number> density
. Each value is mapped to a color.
Check this test Function2DPlotTest
here.
With this code snippet you can plot a function:
@Override
public void start(Stage primaryStage) {
PerspectiveCamera camera = new PerspectiveCamera(true);
camera.setTranslateZ(-30);
SurfacePlotMesh surface = new SurfacePlotMesh(
p-> Math.sin(p.magnitude() + 1e-10) / (p.magnitude() + 1e-10),
20, 20, 100, 100, 4);
surface.setCullFace(CullFace.NONE);
surface.setTextureModeVertices3D(1530, p -> p.magnitude());
surface.getTransforms().addAll(new Rotate(200, Rotate.X_AXIS), new Rotate(-20, Rotate.Y_AXIS));
final Group group = new Group(surface);
Scene scene = new Scene(group, 600, 400, true, SceneAntialiasing.BALANCED);
scene.setCamera(camera);
primaryStage.setScene(scene);
primaryStage.show();
}
And if you add a density map:
surface.setTextureModeVertices3D(1530, p -> p.magnitude());
you'll get this:
Now if you want an animation of the surface, you just need to create one:
private SurfacePlotMesh surface;
private long lastEffect;
@Override
public void start(Stage primaryStage) {
PerspectiveCamera camera = new PerspectiveCamera(true);
camera.setTranslateZ(-30);
surface = new SurfacePlotMesh(
p-> Math.sin(p.magnitude() + 1e-10) / (p.magnitude() + 1e-10),
20, 20, 100, 100, 4);
surface.setCullFace(CullFace.NONE);
surface.setTextureModeVertices3D(1530, p -> p.magnitude());
surface.getTransforms().addAll(new Rotate(200, Rotate.X_AXIS), new Rotate(-20, Rotate.Y_AXIS));
final Group group = new Group(surface);
Scene scene = new Scene(group, 600, 400, true, SceneAntialiasing.BALANCED);
scene.setCamera(camera);
primaryStage.setScene(scene);
primaryStage.show();
lastEffect = System.nanoTime();
AtomicInteger count=new AtomicInteger();
AnimationTimer timerEffect = new AnimationTimer() {
@Override
public void handle(long now) {
if (now > lastEffect + 1_000_000_000l) {
double t = (count.get() % 5 + 1);
surface.setFunction2D(p -> Math.sin(t * p.magnitude() + 1e-10)/(t * p.magnitude() + 1e-10));
count.getAndIncrement();
lastEffect = now;
}
}
};
timerEffect.start();
}
And you'll get your surface animation:
这篇关于3D 表面 JavaFX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!