Laden 3D Modelle

@aspose/3d Es gibt zwei Ladenmethoden auf Scene Klasse : open() für die Datei-Path-Ladung und openFromBuffer() Format-Detektion ist automatisch aus binären magischen Zahlen oder Datei-Erweiterung, so dass Sie selten müssen, das Quellformat ausdrücklich angeben.

Laden von einem Dateiweg

Pass die Datei-Path-Straße auf scene.open().Die Bibliothek erkennt das Format aus der Dateierweiterung und, für binäre Formate, aus den magischen Byten:

import { Scene } from '@aspose/3d';

const scene = new Scene();
scene.open('model.fbx');

console.log(`Root children: ${scene.rootNode.childNodes.length}`);
console.log(`Animation clips: ${scene.animationClips.length}`);

FBX Beschränkung: FBX-Support ist auf ASCII FBX-Import nur beschränkt. Binary FBX wird noch nicht unterstützt. FBX wird auch nicht in die automatische Format-Detektion angetrieben, so dass die Ladung ausdrücklich erforderlich sein kann FbxLoadOptions.Für zuverlässige Modellladung verwenden Sie OBJ, STL oder glTF-Formate.

open() ersetzt vorhandene Sceneinhalte. Wenn Sie mehrere Dateien laden müssen, erstellen Sie separate Scene und Beispiele.

OBJ mit Materialien laden

OBJ Dateien Referenzmaterialien durch eine Seitenkarte .mtl Datei. Material Laden ist Erlaubt durch default (ObjLoadOptions.enableMaterials = truePass Pass ObjLoadOptions Um diese und andere Ladezeiten-Optionen zu kontrollieren:

import { Scene } from '@aspose/3d';
import { ObjLoadOptions } from '@aspose/3d/formats/obj';

const scene = new Scene();
const options = new ObjLoadOptions();
options.enableMaterials = true;       // load the .mtl sidecar
options.flipCoordinateSystem = false; // keep original Y-up orientation
options.normalizeNormal = true;       // fix degenerate normals
options.scale = 1.0;                  // unit scale multiplier

scene.open('character.obj', options);

// Inspect materials on the first node
const first = scene.rootNode.childNodes[0];
if (first) {
    console.log(`Materials on ${first.name}: ${first.materials.length}`);
}

Laden von einem Buffer

Gebrauch openFromBuffer() wenn die Datei-Daten bereits in der Speicher sind, zum Beispiel aus einem HTTP-Fech, einer Datenbank BLOB oder einer Zip-Archiv-Extraktion.:

import { Scene } from '@aspose/3d';
import { ObjLoadOptions } from '@aspose/3d/formats/obj';
import * as fs from 'fs';

const buffer: Buffer = fs.readFileSync('model.obj');
const scene = new Scene();
const options = new ObjLoadOptions();
options.enableMaterials = true;

scene.openFromBuffer(buffer, options);
console.log(`Loaded ${scene.rootNode.childNodes.length} root nodes`);

Für binäre Formate (GLB, STL binäre, 3MF) läßt die Bibliothek die magischen Byte vom Buffer lesen, um das Format automatisch zu erkennen, so dass Sie durchlaufen können undefined Als Optionen Argument:

import { Scene } from '@aspose/3d';
import * as fs from 'fs';

const glbBuffer: Buffer = fs.readFileSync('model.glb');
const scene = new Scene();
scene.openFromBuffer(glbBuffer); // format auto-detected from GLB magic bytes

Laden von HTTP (Node.js 18+)

Fetch ein Remote-Activ und laden Sie es ohne das Dateisystem anzuwenden:

import { Scene } from '@aspose/3d';

async function loadFromUrl(url: string): Promise<Scene> {
    const response = await fetch(url);
    if (!response.ok) throw new Error(`HTTP ${response.status}`);
    const arrayBuffer = await response.arrayBuffer();
    const buffer = Buffer.from(arrayBuffer);

    const scene = new Scene();
    scene.openFromBuffer(buffer);
    return scene;
}

const scene = await loadFromUrl('https://example.com/model.glb');
console.log(`Loaded scene: ${scene.rootNode.childNodes.length} root nodes`);

Überprüfen der Laden-Szenen

Nach der Ladung gehen Sie durch den Scenograf, um Noten zu erheben und die Geometrie zu identifizieren:

import { Scene, Mesh, Node } from '@aspose/3d';

const scene = new Scene();
scene.open('model.fbx'); // ASCII FBX only; see FBX limitation note above

function inspect(node: Node, depth: number = 0): void {
    const indent = '  '.repeat(depth);
    const entityType = node.entity ? node.entity.constructor.name : '(none)';
    console.log(`${indent}${node.name} [${entityType}]`);

    if (node.entity instanceof Mesh) {
        const mesh = node.entity as Mesh;
        console.log(`${indent}  vertices: ${mesh.controlPoints.length}, polygons: ${mesh.polygonCount}`);
    }

    for (const child of node.childNodes) {
        inspect(child, depth + 1);
    }
}

inspect(scene.rootNode);

Lesen Sie Vermögensmetadata

Die assetInfo Eigentum zeigt Datei-Metadaten auf:

import { Scene } from '@aspose/3d';

const scene = new Scene();
scene.open('model.fbx'); // ASCII FBX only

const info = scene.assetInfo;
if (info) {
    console.log(`Creator: ${info.creator}`);
    console.log(`Unit name: ${info.unitName}`);
    console.log(`Unit scale: ${info.unitScaleFactor}`);
}

Nicht alle Formate bewohnen assetInfo.FBX und COLLADA haben reiche Metadaten; OBJ und STL tun normalerweise nicht.

Format-spezifische Load Options Referenz

FormatOptionsklasseEinfuhrwegSchlüsseloptionen
OBJObjLoadOptions@aspose/3d/formats/objenableMaterials, flipCoordinateSystem, scale, normalizeNormal
Gltf(nicht erforderlich ist)N/AAuto-detektion; umfasst sowohl GlTF JSON als auch GLB binäre
STLStlLoadOptions@aspose/3d/formats/stl(Keine konfigurierbaren Optionen in der aktuellen Version)
FBXFbxLoadOptions@aspose/3d/formats/fbxASCII FBX nur; binäre FBX nicht unterstützt. nicht automatisch erkannt — pass FbxLoadOptions Ausdrücklich zu.
COLLADAColladaLoadOptions@aspose/3d/formats/collada(Keine konfigurierbaren Optionen in der aktuellen Version)
3MF(nicht erforderlich ist)N/ASelbstdetektion

Alle Formate unterstützen die automatische Entdeckung von Datei-Erweiterung; Pass Format-spezifische Optionen nur, wenn Sie nicht-Defaktionsverhalten benötigen.

Häufige Ladefehler

SymptomeUrsacheFix
Error: Cannot find module '@aspose/3d/formats/obj'moduleResolution nicht aufgestellt nodeAdd zu "moduleResolution": "node" zu tsconfig.json
Leere Node-Liste nach der Ladung von OBJ.mtl Vermisst oder enableMaterials nicht setSet options.enableMaterials = true und sicherstellen .mtl in demselben Direktorat
Die Bühne laden, aber rootNode.childNodes ist leerFormat verwendet eine einzige Wurzelmesser, nicht KindernotenZugang scene.rootNode.entity Direkt
openFromBuffer mit binären STLBuffer wurde falsch geschnitten (Trailing byte fehlen)Verwenden Sie die Fülle readFileSync() Produktion ohne Schleim
Animationsclips ist nach FBX-Laden leerFBX-Datei verwendet verpackt Transformationen, nicht ClipsAnimation wird per Rahmen gepackt; die Klip-Array wird leer sein
FBX-Datei laden nicht oder produziert leere SzenenBinary FBX wird nicht unterstütztKonvertieren Sie auf ASCII FBX oder verwenden Sie glTF/OBJ/STL stattdessen

Siehe auch

 Deutsch