Laden von 3D-Modellen

@aspose/3d bietet zwei Lademethoden für die Scene Klasse: open() für das Laden von Dateipfaden und openFromBuffer() für In-Memory-Daten. Die Formatserkennung erfolgt automatisch anhand von binären Magic Numbers oder der Dateierweiterung, sodass Sie selten das Quellformat explizit angeben müssen.

Laden von einem Dateipfad

übergeben Sie den Dateipfad-String an scene.open(). Die Bibliothek erkennt das Format anhand der Dateierweiterung und, bei binären Formaten, anhand der Magic Bytes:

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

open() ersetzt jeglichen vorhandenen Szeneninhalt. Wenn Sie mehrere Dateien laden müssen, erstellen Sie separate Scene Instanzen.

Laden von OBJ mit Materialien

OBJ-Dateien referenzieren Materialien über eine sidecar .mtl Datei. Das Laden von Materialien ist standardmäßig aktiviert (ObjLoadOptions.enableMaterials = true). Übergeben ObjLoadOptions um dies und andere Ladezeit-Optionen zu steuern:

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 aus einem Puffer

Verwenden openFromBuffer() wenn die Dateidaten bereits im Speicher sind, zum Beispiel von einem HTTP‑Abruf, einem Datenbank‑BLOB oder einer zip‑Archiv‑Extraktion. Das vermeidet das Schreiben auf die Festplatte:

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ärformate (GLB, STL binary, 3MF) liest die Bibliothek die Magic‑Bytes aus dem Puffer, um das Format automatisch zu erkennen, sodass Sie undefined als Optionsargument:

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 über HTTP (Node.js 18+)

Rufen Sie ein entferntes Asset ab und laden Sie es, ohne das Dateisystem zu berühren:

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

Untersuchen der geladenen Szene

Nach dem Laden traversieren Sie den Szenengraph, um Knoten aufzulisten und Geometrie zu identifizieren:

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

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

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

Auslesen von Asset‑Metadaten

Das assetInfo property gibt Dateiebene-Metadaten frei:

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

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

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 füllen assetInfo. FBX und COLLADA besitzen umfangreiche Metadaten; OBJ und STL haben typischerweise keine.

Referenz zu formatbezogenen Ladeoptionen

FormatOptionenklasseImportpfadSchlüsseloptionen
OBJObjLoadOptions@aspose/3d/formats/objenableMaterials, flipCoordinateSystem, scale, normalizeNormal
glTF / GLB(keine erforderlich)N/AAutomatisch erkannt; keine Optionsklasse erforderlich
STLStlLoadOptions@aspose/3d/formats/stl(keine konfigurierbaren Optionen in der aktuellen Version)
FBXFbxLoadOptions@aspose/3d/formats/fbx(keine konfigurierbaren Optionen in der aktuellen Version)
COLLADAColladaLoadOptions@aspose/3d/formats/collada(keine konfigurierbaren Optionen in der aktuellen Version)
3MF(keine erforderlich)N/AAutomatisch erkannt

Häufige Ladefehler

SymptomUrsacheBehebung
Error: Cannot find module '@aspose/3d/formats/obj'moduleResolution nicht gesetzt auf nodeHinzufügen "moduleResolution": "node" zu tsconfig.json
Leere Knotenliste nach dem Laden von OBJ.mtl Sidecar fehlt oder enableMaterials nicht gesetztSetzen options.enableMaterials = true und sicherstellen .mtl befindet sich im selben Verzeichnis
Szene lädt, aber rootNode.childNodes ist leerFormat verwendet ein einzelnes Root-Mesh, keine KindknotenZugriff scene.rootNode.entity direkt
openFromBuffer wirft bei binärem STLPuffer wurde falsch geschnitten (nachfolgende Bytes fehlen)Verwende das vollständige readFileSync() Ausgabe ohne Schneiden
Animations-Clip-Array ist nach dem FBX-Laden leerFBX-Datei verwendet vorgebackene Transformationen, nicht ClipsAnimation wird pro Frame gebacken; das Clip-Array wird leer sein

Siehe auch

 Deutsch