如何在子场景中设置和转换相机?

How to set up and transform a Camera in a SubScene?

我正在尝试获取网格的等距视图。网格只是主场景的一部分,所以我创建了一个子场景,我想给它添加一个相机。我希望能够放大和缩小相机并在所有这些操作中保持视角。

这是我没有相机的情况:

public class MyApp extends Application {

    @Override
    public void start(Stage stage) throws Exception {

        Button actionButton = new Button("Placeholder\t\t\t\t\t\t\t\t\t\t\t\t");
        HBox hbox = new HBox(actionButton);

        BorderPane mainPane = new BorderPane(new MyView(), null, null, hbox, null);

        Scene scene = new Scene(mainPane);
        stage.setScene(scene);
        stage.show();
    }

    private class MyView extends Group {

        MyView() {

            super();

            GridPane grid = new GridPane();
            for (int i = 0; i < 64; i++) {
                Rectangle tile = new Rectangle(30, 30, Color.GREEN);
                BorderPane pane = new BorderPane(tile);
                pane.setBorder(new Border(new BorderStroke(null, BorderStrokeStyle.SOLID,
                                                           null, null, null)));
                grid.add(pane, i / 8, i % 8);
            }

            Group root = new Group();
            root.getChildren().add(grid);

            SubScene scene = new SubScene(root, 300, 300, true, SceneAntialiasing.BALANCED);
            scene.setFill(Color.DARKCYAN); // just to see the area

            getChildren().add(scene);
        }
    }

    public static void main(String[] args) throws Exception {

        launch(args);
    }
}

现在我在 MyView 构造函数中这样添加相机:

Camera camera = new PerspectiveCamera(true);
scene.setCamera(camera);

网格消失。

我什至还没有做任何转换(我会做 camera.getTransforms().addAll(new Rotate(-15, Rotate.Y_AXIS));)。我做错了什么?

另外,我怎样才能让子场景使用任何 space 可用的东西?我不想指定具体大小,因为该程序可以 运行 在各种屏幕上。

您的相机位于与 Group 相同的 z 坐标处。但是你必须确保它在 farClipnearClip:

之间的距离
PerspectiveCamera camera = new PerspectiveCamera(true);
camera.setTranslateZ(-100);
camera.setFieldOfView(120);

此外,对于等距视图透视相机使用不当Camera。使用 ParallelCamera 代替:

Camera camera = new ParallelCamera();
//camera.setRotationAxis(new Point3D(1, 1, 0));
//camera.setRotate(30);
scene.setCamera(camera);

also, how can I tell the subscene to take whatever space is available?

MyView 扩展的类型更改为可调整大小的类型,并将 SubScene 的大小绑定到 MyView 的大小:

private class MyView extends Pane {

    MyView() {
        ...
        setPrefSize(300, 300);
        scene.widthProperty().bind(widthProperty());
        scene.heightProperty().bind(heightProperty());
    }