3

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!

Community
  • 1
  • 1
yerpy
  • 1,366
  • 3
  • 17
  • 44

1 Answers1

3

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(); 
}

SurfacePlotMesh

And if you add a density map:

surface.setTextureModeVertices3D(1530, p -> p.magnitude());

you'll get this:

Textured SurfacePlotMesh

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:

SurfacePlotMesh 2

SurfacePlotMesh 3

SurfacePlotMesh 4

José Pereda
  • 44,311
  • 7
  • 104
  • 132
  • I included jar's to java project and it doesnt work, still have for exmaple on "SurfacePlotMesh" cannot resolve symbol. – yerpy May 14 '16 at 15:48
  • What version of Java are you using? What are your imports? Maybe you are missing the correct import? – Birdasaur May 14 '16 at 22:12
  • I solved that problem by importing the all source to the project, not only jar's – yerpy May 15 '16 at 09:37
  • You shouldn't have to do that, it is not the right way. What IDE are you using? What build system? (Ant, Maven, Gradle etc) – Birdasaur May 15 '16 at 13:57