How to get 2D coordinates on window for 3D object in javafx相似,但是我无法获得解决方案。

我想为3D形状或更像其投影绘制2D边框。我编写了以下示例代码以供参考:

import javafx.application.Application;
import javafx.geometry.Point3D;
import javafx.scene.Group;
import javafx.scene.PerspectiveCamera;
import javafx.scene.Scene;
import javafx.scene.SceneAntialiasing;
import javafx.scene.SubScene;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.paint.PhongMaterial;
import javafx.scene.shape.Box;
import javafx.scene.shape.Rectangle;
import javafx.scene.transform.Rotate;
import javafx.stage.Stage;

public class CoordinateConversion extends Application {

    @Override
    public void start(Stage stage) {
        var box = new Box(20, 20, 20);
        box.setMaterial(new PhongMaterial(Color.TEAL));
        box.getTransforms().add(new Rotate(45, new Point3D(1, 1, 1)));
        var rootGroup = new Group(box);

        var camera = new PerspectiveCamera(true);
        camera.setFarClip(500);
        camera.setTranslateZ(-100);
        var aaScene = new SubScene(rootGroup, 0, 0, true, SceneAntialiasing.BALANCED);
        aaScene.setCamera(camera);

        var pane3d = new Pane(aaScene);
        pane3d.setBackground(new Background(new BackgroundFill(Color.BEIGE, null, null)));
        aaScene.widthProperty().bind(pane3d.widthProperty());
        aaScene.heightProperty().bind(pane3d.heightProperty());

        var overlay = new AnchorPane();

        var stack = new StackPane(pane3d, overlay);
        var filler = new Rectangle(0, 0, 100, 50);
        filler.setFill(Color.rgb(0, 255, 0, 0.3));
        var borderPane = new BorderPane();
        borderPane.setCenter(stack);
        borderPane.setTop(filler);
        var scene = new Scene(borderPane);
        stage.setScene(scene);
        stage.setWidth(800);
        stage.setHeight(400);
        stage.show();

        var sceneBounds = box.localToScene(box.getBoundsInLocal(), false);
        var overlayBounds = overlay.sceneToLocal(sceneBounds);
        var rect = new Rectangle(overlayBounds.getMinX(), overlayBounds.getMinY(), overlayBounds.getWidth(), overlayBounds.getHeight());
        rect.setFill(null);
        rect.setStroke(Color.RED);
        overlay.getChildren().add(rect);
    }

    public static void main(String[] args) {
        launch(args);
    }
}


它在角落绘制了一个红色矩形,而我想要的是我添加的“ photoshoped”橙色矩形。

java - 将坐标从3D场景转换为2D覆盖-LMLPHP

基本思想是将边界从一个父对象转换为另一个父对象,因此按照How to translate a node in a parent's coordinate system?中的说明,我从一个父对象转换为场景,然后从场景转换为下一个父对象。我对转换不了解的一些事情


如果使用box.localToScene(box.getBoundsInLocal(), true),即使该框位于子场景中,我也将获得NaN边界,并且我希望场景中的坐标。
即使覆盖开始的位置比场景低50个像素,sceneBoundsoverlayBounds也是相同的(由于填充物)。如果没有转换,我希望叠加层和场景之间的每次转换在y上都是+ -50。


我也尝试用sceneBounds获取box.getParent().localToScene(box.getBoundsInParent()),但ti相同。我想这是有道理的,因为我使用父级来转换父级中的边界,而不是使用节点来转换本地中的边界。

使用Javafx 12

最佳答案

您遇到了一个“时间”问题:将subScene设置为0x0尺寸,然后添加绑定,并在显示阶段后立即对sceneBounds进行计算,并返回NaN

如果将侦听器添加到pane3d.widthProperty(),您将看到在宽度更改之前已解析sceneBounds,因此,您正在使用0x0 subScene。所以您称呼为时过早。

可能的解决方案:


删除绑定并为subScene尺寸设置一个值:


var aaScene = new SubScene(rootGroup, 800, 400, true, SceneAntialiasing.BALANCED);
stage.show();
var sceneBounds = box.localToScene(box.getBoundsInLocal(), true);
...



保留绑定,但向subScene的height属性(在width之后设置)添加一个侦听器:


aaScene.heightProperty().addListener((obs, ov, nv) -> {
     var sceneBounds = box.localToScene(box.getBoundsInLocal(), true);
...
});


无论哪种情况,请注意,必须使用localToScene(..., true)来获取场景坐标:


  如果Node没有任何SubScene或rootScene设置为true,则结果点位于getScene()返回的Node的场景坐标中。


java - 将坐标从3D场景转换为2D覆盖-LMLPHP

08-04 02:20
查看更多