我正在尝试在 JavaFX 中实现我自己的 3D 表面动画,但我不理解它应该工作的一切,有人可以帮助我理解哪个应该放在哪里吗?
已经知道使用类构建Mesh需要类对象
TraingleMesh
然后必须使用方法mesh.getPoints.addAll(...);
添加点数但是..我的Function<Double, Double>
使用后apply
方法对我一点帮助都没有,因为第一个参数必须是数组 float 类型,而不是double
应用一些数据后的变量。- 我该如何解决这个问题?
我在这里找到了@Roland 创建的纹理和面的一些实现:
- 纹理和面孔如何运作?
这对我来说真的很重要,谢谢你的帮助!
最佳答案
看看 FXyz library .它是开源的,您可以从代码中学习。
对于纹理,看看这个 post .
FXyz 有一个 SurfacePlotMesh
完全符合您要求的类:根据函数绘制 3D 表面 g = f(x,y)
, 通过使用 Function<Point2D, Number> function
参数。
它还包括纹理,因此您可以根据 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();
}
然后你会得到你的表面动画:
关于java - 3D 表面 JavaFX,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37225197/