在java(或JavaFx)中从2D点或路径创建自定义形状3D

问题描述 投票:0回答:1

由于我缺乏知识,是关于如何在java中从2d点(坐标/路径)创建3d模型并在javafx上显示? 在基础知识中,我知道如何使用相机和灯光系统,这不是问题,但我有石头图片(原始图像),用户用鼠标输入选定的点,代码:

(画布将其放在具有原始图像的 ImageView 上。画布 w , h 绑定到 imageView 的父级,而 imageview 绑定到父级 w,h (因此 imageview 和画布是相同的 w,h))

        canvas.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent mouseEvent) {
                if (isStarted()) {
                    App.logger.debug("Added the Point into: -> X: " + mouseEvent.getX() + " And Y: " + mouseEvent.getY());
                    points.add(new Point2D(mouseEvent.getX(), mouseEvent.getY()));
                    reDraw();
                    restorePoints = new ArrayList<Point2D>();
                } else {
                    App.logger.debug("The Start stat is :" + isStarted() + " So we can't Draw Shapes.");
                }
            }
        });

点和恢复点是:

ArrayList<Point2D> points = new ArrayList<Point2D>()
并且我有正确的图像点,显示了选择为3d的区域,但我找不到如何将其从3dpoints更改为3d模型(对于z,用户选择z我设置了所有 z 点)

 ArrayList<Point3D> points3D = new ArrayList<Point3D>();
        for (Point2D point2D : points) {
            points3D.add(new Point3D(point2D.getX(), point2D.getY(), 24));
        }

在额外信息中,我使用以下代码将点转换为路径:

    private void connectJoint() {
        Point2D startPoint = null;
        Point2D startingPoint = null, endingPoint = null;
        Collection<PathElement> pathElements = new ArrayList<PathElement>();
        for (Point2D point : points) {
            if (startPoint == null) {
                startingPoint = point;
                startPoint = point;
                pathElements.add(new MoveTo(point.getX(), point.getY()));
                continue;
            }
            pathElements.add(new LineTo(point.getX(), point.getY()));
            graphicsContext.strokeLine(startPoint.getX(), startPoint.getY(), point.getX(), point.getY());
            pathElements.add(new LineTo(startingPoint.getX(), startingPoint.getY()));
            startPoint = point;
        }
        endingPoint = startPoint;
        if (startingPoint != null && endingPoint != null && points.size() > 2) {
            shadowLastLine(startingPoint, endingPoint);

        }
        pathOfSelectedAreaWithEndJoined = new Path(pathElements);
    }

那么,回到我的问题:如何从 java 中的点或路径创建 3dModel 以在 javafx 中显示?

java javafx java-3d
1个回答
0
投票

此方法将使用

MeshView
给出的 xy 父坐标( z 始终为 0 )对
PickResult
对象进行网格划分。存在一些关于拓扑和工件的问题。 由于三角形有 9 个坐标(3 个顶点),因此条件将从第三次单击事件开始。而且,每次新的点击都需要在第一个三角形之后出现一个新的面。这就是为什么需要使用
mesh.getFaces().clear()
方法清除 faces 数组。

所有 3d 顶点都指向 u =0 & v=0 uv 坐标,并且光栅图像不能用作纹理。检查这个答案

应用程序.java

public class App extends Application {

    private final TriangleMesh mesh = new TriangleMesh();

    @Override
    public void start(Stage stage) {

        mesh.getTexCoords().addAll(0, 0);
        MeshView meshView = new MeshView(mesh);
        meshView.setMaterial(new PhongMaterial(Color.CYAN));

        AnchorPane anchorPane = new AnchorPane(meshView);

        anchorPane.setOnMouseClicked(e -> {
            float x = (float) e.getX();
            float y = (float) e.getY();

            mesh.getPoints().addAll(x, y, 0);

            if (mesh.getPoints().size() >= 9) {

                makeFaces();
            }

        });

        var scene = new Scene(anchorPane, 640, 480);
        stage.setScene(scene);
        stage.show();
    }

    private void makeFaces() {
        mesh.getFaces().clear();
        int vertices = mesh.getPoints().size();
        int faces = 1 + ((vertices - 9) / 3);

        for (int i = 0; i < faces; i++) {

            mesh.getFaces().addAll(i, 0, i + 1, 0, i + 2, 0);

        }

    }

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

}
© www.soinside.com 2019 - 2024. All rights reserved.