当您想要混合 2D 和 3D(和相机)时,您必须为 3D 内容使用 SubScene container:
SubScene 提供场景不同部分的分离,每个部分都可以使用不同的相机、深度缓冲区或场景抗锯齿进行渲染。子场景嵌入到主场景或另一个子场景中。
如果你有一个 BorderPane 容器,你可以完美地将 subScene 添加到它的中心。
对于类似的用例,您可以查看here 中的Qubit3D 类,该类主要是一个包含球体和圆柱体的子场景的组(均来自FXyz 3D library)。
您可以轻松地将这个组添加到您的 2D 场景中:
private final Rotate rotate = new Rotate(0, 0, 0, 0, javafx.geometry.Point3D.ZERO.add(1, 1, 1));
@Override
public void start(Stage primaryStage) throws Exception {
final Timeline timeline = new Timeline(
new KeyFrame(Duration.seconds(10),
new KeyValue(rotate.angleProperty(), 360)));
final Qubit3D qubit = new Qubit3D();
final BorderPane root = new BorderPane(qubit);
final Button buttonAnimate = new Button("Animate");
buttonAnimate.setOnAction(e -> {
rotate.setAngle(0);
timeline.playFromStart();
});
root.setLeft(new StackPane(buttonAnimate));
final Button buttonStop = new Button("Stop");
buttonStop.setOnAction(e -> timeline.stop());
root.setRight(new StackPane(buttonStop));
Scene scene = new Scene(root, 600, 400, true, SceneAntialiasing.BALANCED);
scene.setFill(Color.BISQUE);
primaryStage.setScene(scene);
primaryStage.setTitle("Qubit3D Sample");
primaryStage.show();
qubit.rotateRod(rotate);
}
我添加到 Qubit3D 的唯一修改是:
public void rotateRod(Rotate rotate) {
rodSphere.getTransforms().setAll(rotate);
}
如果你运行它:
请注意,您可以与球体交互(通过鼠标拖动事件),同时您还可以开始/停止球体和杆的完整旋转。