我正在尝试在JavaFX中实现自己的3D表面动画,但我不了解它应该起作用的一切,有人可以帮助我理解应该去哪里吗?
已经知道使用类构建网格需要类对象TraingleMesh
,然后必须使用方法mesh.getPoints.addAll(...);
添加点,但是..使用Function<Double, Double>
方法后我的apply
根本对我没有帮助,因为第一个应用某些数据后,参数必须为数组浮点类型,而不是double
变量。
我该如何解决这个问题?
我在这里找到了@Roland创建的纹理和面的一些实现:
3D surface - stack
纹理和面孔如何工作?
这对我来说真的很重要,谢谢您的帮助!
最佳答案
看看FXyz library。它是开源的,您可以从代码中学习。
对于纹理,请查看此post。
FXyz有一个SurfacePlotMesh
类,它可以完全满足您的需要:使用g = f(x,y)
参数基于功能Function<Point2D, Number> function
绘制3D曲面。
它还包括纹理,因此您可以根据Function<Point3D, Number> density
包括密度贴图。每个值都映射到一种颜色。
检查此测试Function2DPlotTest
here。
使用此代码段,您可以绘制函数:
@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();
}
如果添加密度图:
surface.setTextureModeVertices3D(1530, p -> p.magnitude());
你会得到这个:
现在,如果要制作表面动画,则只需创建一个:
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();
}
然后您将获得表面动画: