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 ゼロまたはそれ以上を運ぶことができます。 Entity 物件(メッセージ、カメラ、ライト)チェック node.getEntities() ノードに付属するすべてのエンティティを検証したり、使用したりすること。 node.getEntity() 主な団体を引き継ぐこと。.


ステップ5: Vertex と Polygon のデータへのアクセス

ノードのエネルギーを押し付ける Mesh 呼びかけ getControlPoints() ベースポジションと、 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[]> それぞれのアレージが1つの顔のコントロールポイント指標の注文セットである。.


ステップ6:フォーマット特定の充電オプションを適用する

ファイルがどのように解釈されるかを精密にコントロールするには、ロードオプションの例を参照してください。 Scene.fromFile() あるいは scene.open().

OBJファイル - 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);

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

サポートされた輸入形式

フォーマット拡張ノート
OBJ.objワイフロントOBJ;オプション .mtl ファイルの作成:可能なものとともに、 ObjLoadOptions.setEnableMaterials(true)
STL.stlASCIIとバイナリーモードが自動的に検出される。
グラフ.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() 直接総数を計算する。.


見ることも

 日本語