如何在SubScene中设置和转换相机?

时间:2016-10-09 17:31:49

标签: java javafx javafx-8 perspectivecamera

我正在尝试获取网格的等轴视图。网格只是主场景的一部分,所以我创建了一个子场景,我想为它添加一个摄像头。我希望能够放大和缩小相机,并在整个操作过程中平移它以保持视角。

以下是没有相机的情况:

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);
    }
}

enter image description here

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

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

并且网格消失。

enter image description here

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

另外,如何判断子场景占用的空间是多少?我不想指定特定的大小,因为程序可以在各种屏幕上运行。

1 个答案:

答案 0 :(得分:0)

您的相机位于与z相同的Group坐标。但是,您必须确保它在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);
  

另外,如何判断子场景是否占用了可用的空间?

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

private class MyView extends Pane {

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