Làm thế nào để tải 3D trong Java

Làm thế nào để tải 3D trong Java

Aspose.3D FOSS cho Java cung cấp một API đơn giản để mở các tệp 3D mà không có bất kỳ phụ thuộc bản địa. Scene đối tượng bạn có thể đi bộ trong các hệ thống phân cấp nút và đọc dữ liệu địa chất nguyên cho mỗi mảnh trên sân khấu.

Hướng dẫn Step-by-Step

Bước 1: Thêm sự phụ thuộc Maven

Thêm sự phụ thuộc của Aspose.3D FOSS vào bạn pom.xml.Không cần thêm thư viện bản địa.

<dependency>
  <groupId>org.aspose</groupId>
  <artifactId>aspose-3d-foss</artifactId>
  <version>26.5.0</version>
</dependency>

Bước 2: nhập các lớp học cần thiết

Các Scene lớp là container cấp cao cho tất cả dữ liệu 3D. nhập khẩu cùng với các Node, Mesh, và bất kỳ loại lớp tùy chọn tải cụ thể nào bạn cần.

import com.aspose.threed.Scene;
import com.aspose.threed.Node;
import com.aspose.threed.Mesh;
import com.aspose.threed.Entity;
import com.aspose.threed.ObjLoadOptions;
import com.aspose.threed.GltfLoadOptions;
import com.aspose.threed.StlLoadOptions;

Tất cả các lớp học công cộng sống dưới sự com.aspose.threed gói .


Bước 3: Tải file

Sử dụng static Scene.fromFile() phương pháp để mở bất kỳ định dạng được hỗ trợ. Thư viện phát hiện định hình tự động từ phần mở rộng tệp.

// Automatic format detection from the file extension
Scene scene = Scene.fromFile("model.obj");

Thay vào đó, tạo ra một Scene trường hợp và gọi điện thoại open().Điều này hữu ích khi bạn muốn vượt qua các tùy chọn tải hoặc xử lý lỗi một cách rõ ràng:

Scene scene = new Scene();
scene.open("model.obj");

Cả hai phương pháp hỗ trợ OBJ, STL (binary và ASCII), glTF 2.0 / GLB, và FBX tệp.


Bước 4: Chuyển qua các nút cảnh

Một cảnh tải là một cây của Node Các vật thể được làm gốc trong scene.getRootNode().Sử dụng getChildNodes() để iterate tái diễn và truy cập tất cả các nút:

import com.aspose.threed.Scene;
import com.aspose.threed.Node;

public class SceneWalker {
    public static void main(String[] args) throws Exception {
        Scene scene = Scene.fromFile("model.obj");
        walkNode(scene.getRootNode(), 0);
    }

    static void walkNode(Node node, int depth) {
        String indent = "  ".repeat(depth);
        System.out.println(indent + "Node: " + node.getName());
        for (Node child : node.getChildNodes()) {
            walkNode(child, depth + 1);
        }
    }
}

Mỗi người Node Có thể mang lại 0 hoặc nhiều hơn Entity Các vật (mùa, máy ảnh, đèn) kiểm tra node.getEntities() để kiểm tra mỗi entity gắn liền với một nút, hoặc sử dụng node.getEntity() để lấy lại bản thân chính.


Bước 5: Truy cập Vertex và dữ liệu Polygon

Chọn một entity của node để Mesh và gọi getControlPoints() Đối với các vị trí vertex và getPolygons() Đối với danh sách Face Index:

import com.aspose.threed.Scene;
import com.aspose.threed.Node;
import com.aspose.threed.Entity;
import com.aspose.threed.Mesh;

Scene scene = Scene.fromFile("model.obj");

for (Node node : scene.getRootNode().getChildNodes()) {
    Entity entity = node.getEntity();
    if (entity instanceof Mesh) {
        Mesh mesh = (Mesh) entity;
        System.out.printf("Mesh '%s': %d vertices, %d polygons%n",
            node.getName(),
            mesh.getControlPoints().size(),
            mesh.getPolygonCount());

        // First vertex position (Vector4: x, y, z, w)
        if (!mesh.getControlPoints().isEmpty()) {
            var v = mesh.getControlPoints().get(0);
            System.out.printf("  First vertex: (%.4f, %.4f, %.4f)%n", v.x, v.y, v.z);
        }

        // First polygon: array of control-point indices
        if (!mesh.getPolygons().isEmpty()) {
            int[] poly = mesh.getPolygons().get(0);
            System.out.println("  First polygon indices: " + java.util.Arrays.toString(poly));
        }
    }
}

mesh.getControlPoints() Trở lại a List<Vector4> nơi đâu x, y, z - Chạy vị trí và w là sự phối hợp đồng tính (thường là 1.0).

mesh.getPolygons() Trở lại a List<int[]> nơi mỗi hàng là bộ chỉ số điểm kiểm soát được đặt ra cho một mặt.


Bước 6: áp dụng các tùy chọn tải định dạng cụ thể

Để kiểm soát kỹ lưỡng về cách một tệp được giải thích, hãy chuyển sang ví dụ tùy chọn tải lên để: Scene.fromFile() hoặc scene.open().

Các file - ObjLoadOptions:

import com.aspose.threed.Scene;
import com.aspose.threed.ObjLoadOptions;

ObjLoadOptions options = new ObjLoadOptions();
options.setFlipCoordinateSystem(true);  // Convert right-hand Y-up to Z-up
options.setScale(0.01);                  // Convert centimetres to metres
options.setEnableMaterials(true);        // Load the .mtl material file alongside
options.setNormalizeNormal(true);        // Normalize all normals to unit length

Scene scene = Scene.fromFile("model.obj", options);

Các tập tin GLTF / GLB - GltfLoadOptions:

import com.aspose.threed.Scene;
import com.aspose.threed.GltfLoadOptions;

GltfLoadOptions options = new GltfLoadOptions();
options.setFlipCoordinateSystem(true);  // Flip the coordinate system if needed

Scene scene = Scene.fromFile("model.glb", options);

file STL - StlLoadOptions:

import com.aspose.threed.Scene;
import com.aspose.threed.StlLoadOptions;

StlLoadOptions options = new StlLoadOptions();
options.setFlipCoordinateSystem(true);
options.setRecalculateNormal(true);  // Recompute normals from face geometry

Scene scene = Scene.fromFile("model.stl", options);

Các định dạng nhập khẩu được hỗ trợ

định dạngmở rộngGhi chú
OBJ.objWavefront OBJ; tùy chọn .mtl file; có thể với ObjLoadOptions.setEnableMaterials(true)
STL.stlASCII và chế độ nhị phân được tự phát hiện
GTTF.gltf, .glbglTF 2.0; biến thể nhị phân GLB và JSON được hỗ trợ cả hai
FBX.fbxPhân phối FBX hỗ trợ

Các vấn đề và Fixes chung

IOException trên tải - Kiểm tra đường tệp là chính xác và file tồn tại. Sử dụng các con đường tuyệt đối trong quá trình phát triển để loại bỏ sự không rõ ràng về hướng dẫn làm việc.

NullPointerException - Khả năng truy cập entity Không phải tất cả các nút đều mang theo địa chất. luôn giữ gìn với node.getEntity() instanceof Mesh trước khi casting, hoặc iterate node.getEntities() để xử lý các nút với nhiều đối tượng gắn kết.

Hệ thống phối hợp không phù hợp - Nếu mẫu tải xuất hiện nhấp hoặc xoay, sử dụng setFlipCoordinateSystem(true) Các tùy chọn tùy chỉnh (ObjLoadOptions, GltfLoadOptions,hoặc StlLoadOptions).

Phim tải nhưng getChildNodes() trống rỗng — Một số tệp lưu trữ địa phương dưới các cảnh phụ thay vì nút gốc. scene.getSubScenes() và kiểm tra các nút gốc của mỗi sub-scene.


Câu hỏi thường gặp (FAQ)

Tôi có thể tải các định dạng nào?

OBJ, STL (binary và ASCII), glTF 2.0 / GLB, và FBX. Phát hiện định dạng tự động dựa trên phần mở rộng tệp khi bạn gọi Scene.fromFile().

Tôi có thể tải từ một dòng không?

Có . scene.open(InputStream)Scene.fromStream(InputStream) Cả hai đều chấp nhận Java. InputStream.Bạn cũng có thể vượt qua một FileFormat Parameters khi dòng không mang theo một mở rộng.

Thư viện có an toàn không?

Mỗi người Scene trường hợp là độc lập và không chia sẻ trạng thái thay đổi với các trường đại khác.

Làm thế nào để đọc polygon count mà không iterating mỗi khuôn mặt?

gọi điện mesh.getPolygonCount() cho một số lượng toàn diện trực tiếp.


Xem thêm

 Tiếng Việt