3D surface JavaFX

て烟熏妆下的殇ゞ 提交于 2021-02-19 05:32:14

问题


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 method mesh.getPoints.addAll(...); but.. my Function<Double, Double> after using apply method does not help me at all, cuz the first argument has to be array float type, not double variable after applying some data.

    • How could i solve that problem ?
  • I found some implementations of texture and faces created by @Roland here :

3D surface - stack

  • How textures and faces working ?

It's really important for me, thanks for help!


回答1:


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:



来源:https://stackoverflow.com/questions/37225197/3d-surface-javafx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!