'JavaFX 3D boxes overlapping each other

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)



Solution 1:[1]

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)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 mipa