-1

I am having a problem with JavaFX 3D, the problem is as follows:

When I turn my perspective camera around, the last added box (blue box) overlaps the first added box (red box), here is a screenshot:

enter image description here

can anyone tell me why is this happening? And is there a way to fix it? (the boxes are literally 2 box classes with a width, height, depth, position and color)

Minimal reproducible example since somebody asked for it:

Box box1 = new Box();
Box box2 = new Box();

box1.setWidth(300);
box2.setWidth(300);

box1.setHeight(300);
box2.setHeight(300);

box1.setDepth(300);
box2.setDepth(300);

box1.setTranslateX(300);
box2.setTranslateX(300);

box1.setTranslateY(300);
box2.setTranslateX(300);

Group root = new Group();

root.getChildren().addAll(box, box2);

PerspectiveCamera cam = new PerspectiveCamera();

Scene scene = new Scene(root);
scene.setCamera(camera);

stage.setScene(scene);
stage.show();

where stage is the stage inside public void start(Stage stage), JavaFX's default run method (any class that extends Application should implement it)

Nimantha
  • 6,405
  • 6
  • 28
  • 69
IkeVoodoo
  • 77
  • 1
  • 3
  • 8

2 Answers2

0

You probably have to add a subscene with the depth buffer enabled. See: https://openjfx.io/javadoc/15/javafx.graphics/javafx/scene/SubScene.html#%3Cinit%3E(javafx.scene.Parent,double,double,boolean,javafx.scene.SceneAntialiasing)

mipa
  • 10,369
  • 2
  • 16
  • 35
0

Solution:

I used the following constructor:

new Scene(root, WIDTH, HEIGHT, true, SceneAntialiasing.BALANCED);

instead of:

new Scene(root);

IkeVoodoo
  • 77
  • 1
  • 3
  • 8