3D modeļu ielāde
@aspose/3d nodrošina divas ielādes metodes uz Scene klasi: open() faila ceļa ielādei un openFromBuffer() atmiņā esošo datu ielādei. Formāta noteikšana ir automātiska, balstoties uz bināriem maģiskajiem skaitļiem vai faila paplašinājumu, tāpēc jums reti jānorāda avota formāts manuāli.
Ielāde no faila ceļa
Pārsūtiet faila ceļa virkni uz scene.open(). Bibliotēka noteic formātu no faila paplašinājuma un, bināriem formātiem, no maģiskajiem baitiem:
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() aizstāj jebkuru esošu ainas saturu. Ja jums jāielādē vairāki faili, izveidojiet atsevišķus Scene instancus.
OBJ ielāde ar materiāliem
OBJ faili atsaucas uz materiāliem caur blakusfailu .mtl fails. Materiālu ielāde ir pēc noklusējuma ieslēgts (ObjLoadOptions.enableMaterials = true). Iziet ObjLoadOptions lai kontrolētu šo un citas ielādes laika opcijas:
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}`);
}Ielāde no bufera
Izmantot openFromBuffer() kad faila dati jau ir atmiņā, piemēram, no HTTP pieprasījuma, datubāzes BLOB vai zip arhīva izvilkšanas. Tas novērš rakstīšanu uz diska:
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`);Bināriem formātiem (GLB, STL binary, 3MF) bibliotēka nolasa maģiskos baitus no bufera, lai automātiski noteiktu formātu, tāpēc jūs varat nodot undefined kā opciju argumentu:
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
Ielāde no HTTP (Node.js 18+)
Iegūstiet attālināto resursu un ielādējiet to, nepieskaroties failu sistēmai:
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`);Ielādētās ainas pārbaude
Pēc ielādes pārlūkojiet ainas grafu, lai uzskaitītu mezglus un identificētu ģeometriju:
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);Resursa metadatu nolasīšana
Šis assetInfo īpašība atklāj faila līmeņa metadatus:
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}`);
}Ne visi formāti aizpilda assetInfo. FBX un COLLADA satur bagātīgu metadatu; OBJ un STL parasti to nepatur.
Formāta specifiskās ielādes opcijas atsauce
| Formāts | Opciju klase | Importēšanas ceļš | Galvenās opcijas |
|---|---|---|---|
| OBJ | ObjLoadOptions | @aspose/3d/formats/obj | enableMaterials, flipCoordinateSystem, scale, normalizeNormal |
| glTF / GLB | (nav vajadzīgs) | N/A | Automātiski noteikts; nav vajadzīga opciju klase |
| STL | StlLoadOptions | @aspose/3d/formats/stl | (nav konfigurējamu opciju pašreizējā versijā) |
| FBX | FbxLoadOptions | @aspose/3d/formats/fbx | (nav konfigurējamu opciju pašreizējā versijā) |
| COLLADA | ColladaLoadOptions | @aspose/3d/formats/collada | (nav konfigurējamu opciju pašreizējā versijā) |
| 3MF | (nav vajadzīgs) | N/A | Automātiski noteikts |
Biežāk sastopamās ielādes kļūdas
| Simptoms | Iemesls | Labojums |
|---|---|---|
Error: Cannot find module '@aspose/3d/formats/obj' | moduleResolution nav iestatīts uz node | Pievienot "moduleResolution": "node" uz tsconfig.json |
| Tukšs mezglu saraksts pēc OBJ ielādes | .mtl sidecar trūkst vai enableMaterials nav iestatīts | Iestatīt options.enableMaterials = true un pārliecinieties .mtl ir tajā pašā direktorijā |
Aina ielādējas, bet rootNode.childNodes ir tukša | Formāts izmanto vienu saknes režģi, nevis bērna mezglus | Piekļūt scene.rootNode.entity tieši |
openFromBuffer izsauc izņēmumu ar binary STL | Buferis tika nepareizi sadalīts (trūkst beigu baitu) | Izmantojiet pilno readFileSync() izvade bez šķēlēšanas |
| Animācijas klipu masīvs ir tukšs pēc FBX ielādes | FBX fails izmanto ieceptas transformācijas, nevis klipus | Animācija tiek iecepta katrā kadrā; klipu masīvs būs tukšs |
Skatīt arī
- 3D modeļu saglabāšana: eksporta iespējas un
saveToBuffer - Ainas grafiks: mezglu hierarhijas pārlūkošana un modificēšana
- API pārskats: visas klases un uzskaitījumi
- Formāta atbalsts: pilna lasīšanas/rakstīšanas matrica