JavaFx 3D文件导入

3d文件转换工具Jar下载地址 :jfx3dimporter

package com.tdmodel;

import java.io.IOException;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.PerspectiveCamera;
import javafx.scene.Scene;
import javafx.scene.SceneAntialiasing;
import javafx.scene.SubScene;
import javafx.scene.input.MouseEvent;
import javafx.scene.input.ScrollEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.transform.Rotate;
import javafx.scene.transform.Translate;
import javafx.stage.Stage;

/**
 *
 * @author zhuqing
 */
public class Import3dModel extends Application {

    private final Rotate cameraXRotate = new Rotate(-20, 0, 0, 0, Rotate.X_AXIS);
    private final Rotate cameraYRotate = new Rotate(-20, 0, 0, 0, Rotate.Y_AXIS);
    private final Translate cameraPosition = new Translate(0, 0, -20);

    private double scaleFactor = 1;

    private double dragStartX, dragStartY, dragStartRotateX, dragStartRotateY;

    @Override
    public void start(Stage primaryStage) throws IOException {

        BorderPane borderPane = new BorderPane();

        borderPane.setCenter(load3DModel());

        Scene scene = new Scene(borderPane, 800, 600);
        initMouseDrag(scene);

        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /**
     * 导入3d模型
     *
     * @return
     * @throws IOException
     */
    private Group load3DModel() throws IOException {

        /**
         * 创建摄像头
         */
        PerspectiveCamera camera = new PerspectiveCamera(true);
        camera.getTransforms().addAll(cameraXRotate, cameraYRotate, cameraPosition);

        /**
         * 导入3d模型
         */
        Group groupRoot = Importer3D.load("C:\\Users\\zhuqing.BJGOODWILL\\Documents\\NetBeansProjects\\3dModel\\src\\resources\\hands.3DS");
        groupRoot.getChildren().add(camera);

        /**
         * 创建SubScene
         */
        SubScene subScene = new SubScene(groupRoot, 800, 600, true, SceneAntialiasing.BALANCED);
        subScene.setCamera(camera);
        Group group = new Group();
        group.getChildren().add(subScene);

        return group;
    }

    private void initMouseDrag(Scene scene) {
        /**
         * 鼠标事件
         */
        scene.addEventFilter(MouseEvent.ANY, new EventHandler() {
            @Override
            public void handle(MouseEvent event) {

                if (event.getEventType() == MouseEvent.MOUSE_PRESSED) {
                    dragStartX = event.getSceneX();
                    dragStartY = event.getSceneY();
                    dragStartRotateX = cameraXRotate.getAngle();
                    dragStartRotateY = cameraYRotate.getAngle();
                } else if (event.getEventType() == MouseEvent.MOUSE_DRAGGED) {
                    double xDelta = event.getSceneX() - dragStartX;
                    double yDelta = event.getSceneY() - dragStartY;
                    cameraXRotate.setAngle(dragStartRotateX - (yDelta * 0.3));
                    cameraYRotate.setAngle(dragStartRotateY + (xDelta * 0.3));
                }

            }
        });

        /**
         * 滚轮滚动数据
         */
        scene.setOnScroll(new EventHandler() {
            @Override
            public void handle(ScrollEvent event) {
                double z = cameraPosition.getZ() - (event.getDeltaY() * 0.1 * scaleFactor);
                z = Math.min(z, 0);
                cameraPosition.setZ(z);
            }
        });

    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

}

package com.tdmodel;

import com.interactivemesh.jfx.importer.ModelImporter;
import com.interactivemesh.jfx.importer.stl.StlMeshImporter;
import com.interactivemesh.jfx.importer.tds.TdsModelImporter;
import com.interactivemesh.jfx.importer.x3d.X3dModelImporter;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.paint.Color;
import javafx.scene.paint.PhongMaterial;
import javafx.scene.shape.MeshView;
import javafx.scene.shape.TriangleMesh;

import java.io.IOException;

/**
 *3d模型导入工具
 */
public final class Importer3D {

    /**
     * 导入3d模型文件
     * @param fileUrl
     */
    public static Group load(final String fileUrl) throws IOException {

        final int dot = fileUrl.lastIndexOf('.');
        if (dot <= 0) {
            throw new IOException("Unknown 3D file format, url missing extension [" + fileUrl + "]");
        }
        final String extension = fileUrl.substring(dot + 1, fileUrl.length()).toLowerCase();

        switch (extension) {
            case "3ds":
                ModelImporter tdsImporter = new TdsModelImporter();
                tdsImporter.read(fileUrl);
                final Node[] tdsMesh = (Node[]) tdsImporter.getImport();
                tdsImporter.getNamedMaterials();
                return new Group(tdsMesh);
            case "stl":
                StlMeshImporter stlImporter = new StlMeshImporter();
                stlImporter.read(fileUrl);
                TriangleMesh cylinderHeadMesh = stlImporter.getImport();
                stlImporter.close();
                MeshView cylinderHeadMeshView = new MeshView();
                cylinderHeadMeshView.setMaterial(new PhongMaterial(Color.GRAY));
                cylinderHeadMeshView.setMesh(cylinderHeadMesh);
                stlImporter.close();
                return new Group(cylinderHeadMeshView);
            case "x3d":

                X3dModelImporter x3dModel = new X3dModelImporter();
                x3dModel.read(fileUrl);
                
                final Node[] x3dMesh = x3dModel.getImport();
                x3dModel.close();
                return new Group(x3dMesh);

            default:
                throw new IOException("Unsupported 3D file format [" + extension + "]");
        }
    }
}

你可能感兴趣的:(JavaFx 3D文件导入)