Static 2D text over 3D scene in javafx java

1.2k views Asked by At

My goal is to overlay 2D text over a 3d scene in javafx as seen in this image

Using a subscene is not a valid choice as I want the 3d model to be able to take up the entire space on the screen.

I tried adding a label to the scene and turning depth buffering off but once the model gets rotated (the actual camera changes position) the correct positioning breaks. (Used code to control the camera )

Can I somehow overlay a static 2D GUI over my 3D scene maybe by using anchor panes and having a 2D scene with transparent background?

On stack overflow I only found these questions:
Question No.1
Question No.2
which don't correspond to my exact needs.

1

There are 1 answers

0
Kilian On BEST ANSWER

I misunderstood the concept of subscenes as they all showed entirely separated controls. Overlaying 3D Text is possible using the following structure...

  • Root Container (e.g. an Anchor Pane)
    • 2D Content (Label)
    • SubScene
      • perspective camera
      • root 3D
        • 3D content

Code example:

//Add 2D content here
AnchorPane globalRoot = new AnchorPane();
globalRoot.getChildren().add(new Label("Hello World"));
Scene scene = new Scene(globalRoot, 1024, 768, true);

SubScene sub = new 
SubScene(root3D,1024,768,false,SceneAntialiasing.BALANCED);
sub.setCamera(camera);
globalRoot.getChildren().add(sub);

//Add all 3D content to the root3D node    

primaryStage.setScene(scene);
primaryStage.show();