Java에서 3D 모델을 다운로드하는 방법

Java에서 3D 모델을 다운로드하는 방법

Aspose.3D FOSS for Java는 원주민 의존성 없이 3D 파일을 열기 위한 간단한 API를 제공합니다. Scene 객체는 노드 히어로를 걸어서 장면의 각 메시지에 대한 원료 지질 데이터를 읽을 수 있습니다.

단계별 가이드

단계 1 : Maven 의존성을 추가하십시오.

Aspose.3D FOSS 의존성을 귀하의 경우에 추가합니다. pom.xml.추가로 원주민 도서관이 필요하지 않습니다.

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

단계 2 : 필요한 수업을 가져오기

그들의 Scene 클래스는 모든 3D 데이터를 위한 최고 수준의 컨테이너입니다. Node, Mesh, 그리고 필요한 모든 형식 특정 로드 옵션 클래스.

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;

모든 공공 계급은 그 아래에서 살고 있다. com.aspose.threed 패키지 입니다.


단계 3 : 파일을 업로드합니다.

스테이크를 사용하십시오. Scene.fromFile() 도서관은 지원되는 형식을 열 수 있습니다.이 도서는 파일 확장에서 자동으로 형식을 감지합니다.

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

대체로, A를 만들기 위해서 Scene 과 전화 open().이것은 충전 옵션을 통과하거나 오류를 명시적으로 처리하려는 경우에 유용합니다 :

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

두 접근 방식 모두 OBJ, STL (비나리 및 ASCII), glTF 2.0 / GLB 및 FBX 파일을 지원합니다.


단계 4 : 무대 노드를 통과합니다.

짐이 있는 장면은 나무의 Node 뿌리 잡힌 물체들에 대하여 scene.getRootNode().사용하기 getChildNodes() 반복적으로 이테라이트하고 모든 노드를 방문하십시오 :

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);
        }
    }
}

각각의 Node 0 또는 더 많이 옮길 수 있습니다. Entity 물건 (대사, 카메라, 빛) 확인하기 node.getEntities() 각 단위에 연결된 노드 또는 사용을 검사하기 위해, node.getEntity() 원시체를 다시 얻는 것이다.


5단계: Vertex 및 Polygon 데이터에 액세스

노드의 본질을 삽입하여 Mesh 그리고 전화 getControlPoints() Vertex 위치 및 getPolygons() 얼굴 인덱스 목록에 대 한:

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() 돌아오는 A List<Vector4> 어디서 x, y, z 위치를 유지하고, 그리고 w 동일한 조율이 일반적으로 1.0입니다.

mesh.getPolygons() 돌아오는 A List<int[]> 각 링은 한 얼굴에 대한 컨트롤 포인트 지표의 주문 세트입니다.


단계 6: 형식 특정 충전 옵션을 적용합니다.

파일이 어떻게 해석되는지에 대한 얇은 통제를 위해, 로드 옵션 예제를 입력하여 Scene.fromFile() 또는 scene.open().

파일 - 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);

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);

스테이크 파일 - 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);

지원되는 수입 형식

형식확장노트
OBJ.objWavefront OBJ; 선택적 .mtl 파일을 사용하여; 가능성에 따라 ObjLoadOptions.setEnableMaterials(true)
STL.stlASCII 및 바이너리 모드는 자동으로 발견됩니다.
GSTF.gltf, .glbglTF 2.0; 바이너리 GLB 및 JSON 변형 모두 지원
FBX.fbx바이너리 FBX 지원

일반적인 문제와 해결책

IOException 로드에 대하여 - 파일 경로가 올바르고 파일이 존재하는지 확인하십시오. 개발 중 절대 경로는 작업-디렉터리 불확실성을 제거합니다.

NullPointerException 액세스 단체 - 모든 노드가 지질을 가지고 있지 않습니다. 항상 경비와 함께 node.getEntity() instanceof Mesh 을 때, 또는 이테라이트 node.getEntities() 여러 개의 묶여있는 물체를 가진 노드를 처리합니다.

시스템 조정 불합리성 - 충전된 모델이 거나 회전되는 경우, 사용하기 setFlipCoordinateSystem(true) 적절한 옵션 클래스에 대 한 (ObjLoadOptions, GltfLoadOptions,또는 StlLoadOptions).

장면이 흔들리지만 getChildNodes() 텅 비어있다 — 일부 파일은 뿌리 노드 대신 하위 스케줄 아래에서 지질을 저장합니다. scene.getSubScenes() 그리고 각 하위 장면의 뿌리 노드를 검사합니다.


자주 묻는 질문 (FAQ)

어떤 형식으로 충전할 수 있나요?

OBJ, STL (비나리 및 ASCII), glTF 2.0 / GLB, 그리고 FBX. 형식 탐지 자동으로 파일 확장에 따라 전화 할 때 Scene.fromFile().

흐름에서 충전할 수 있나요?

예요 ᄒᄒ. scene.open(InputStream) 그리고 Scene.fromStream(InputStream) 둘 다 Java를 사용합니다. InputStream.당신은 또한 통과 할 수 있습니다 A FileFormat 파라미터는 흐름이 확장을 수행하지 않는 경우입니다.

도서관이 안전한가요?

각각의 Scene 사례는 독립적이며 다른 사건과 변동 상태를 공유하지 않습니다.

각 얼굴을 이테라링하지 않고 폴리곤 계산을 어떻게 읽을 수 있습니까?

전화기 mesh.getPolygonCount() 직접적인 전체 계산을 위해서.


또한 보기

 한국어