【问题标题】:How to set up and transform a Camera in a SubScene?如何在子场景中设置和转换相机?
【发布时间】:2016-10-09 17:31:49
【问题描述】:

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

这是我没有相机的情况:

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));)。我做错了什么?

另外,我如何告诉子场景占用任何可用空间?我不想指定具体的尺寸,因为程序可以在各种屏幕上运行。

【问题讨论】:

    标签: java javafx javafx-8 perspectivecamera


    【解决方案1】:

    您的相机位于与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);
    

    另外,我如何告诉子场景占用任何可用空间?

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

    private class MyView extends Pane {
    
        MyView() {
            ...
            setPrefSize(300, 300);
            scene.widthProperty().bind(widthProperty());
            scene.heightProperty().bind(heightProperty());
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-05-03
      • 2014-11-01
      • 2014-07-29
      • 1970-01-01
      • 2023-03-23
      • 1970-01-01
      • 2019-02-14
      相关资源
      最近更新 更多