กำลังโหลดโมเดล 3D

@aspose/3d ให้สองวิธีการโหลดบน Scene คลาส: open() สำหรับการโหลดโดยใช้เส้นทางไฟล์และ openFromBuffer() สำหรับข้อมูลในหน่วยความจำ การตรวจจับรูปแบบทำโดยอัตโนมัติจากหมายเลขมาจิกไบนารีหรือส่วนขยายไฟล์ ดังนั้นคุณจึงแทบไม่จำเป็นต้องระบุรูปแบบแหล่งข้อมูลอย่างชัดเจน.

การโหลดจากเส้นทางไฟล์

ส่งสตริงเส้นทางไฟล์ไปยัง scene.open(). ไลบรารีตรวจจับรูปแบบจากส่วนขยายไฟล์และสำหรับรูปแบบไบนารีจากไบต์มาจิก:

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() แทนที่เนื้อหาในซีนที่มีอยู่ทั้งหมด หากคุณต้องการโหลดหลายไฟล์ ให้สร้าง Scene อินสแตนซ์.

การโหลด OBJ พร้อมวัสดุ

ไฟล์ OBJ อ้างอิงวัสดุผ่านไฟล์ sidecar .mtl ไฟล์ การโหลดวัสดุเป็น เปิดใช้งานโดยค่าเริ่มต้น (ObjLoadOptions.enableMaterials = true). ส่ง ObjLoadOptions เพื่อควบคุมสิ่งนี้และตัวเลือกการโหลดอื่น ๆ:

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

การโหลดจากบัฟเฟอร์

ใช้ openFromBuffer() เมื่อข้อมูลไฟล์อยู่ในหน่วยความจำแล้ว เช่น จากการดึงข้อมูล HTTP, BLOB ของฐานข้อมูล, หรือการสกัดไฟล์จาก zip archive วิธีนี้ช่วยหลีกเลี่ยงการเขียนลงดิสก์:

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

สำหรับรูปแบบไบนารี (GLB, STL binary, 3MF) ไลบรารีจะอ่านไบต์มาจิกจากบัฟเฟอร์เพื่อทำการตรวจจับรูปแบบโดยอัตโนมัติ ดังนั้นคุณสามารถส่ง undefined เป็นอาร์กิวเมนต์ options:

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

การโหลดจาก HTTP (Node.js 18+)

ดึงแอสเซทจากระยะไกลและโหลดโดยไม่ต้องสัมผัสระบบไฟล์:

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

การตรวจสอบ Scene ที่โหลดแล้ว

หลังจากโหลดแล้ว ให้เดินผ่านกราฟของ scene เพื่อแสดงรายการโหนดและระบุเรขาคณิต:

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

การอ่านเมตาดาต้าแอสเซท

The assetInfo property เปิดเผยเมตาดาต้าระดับไฟล์:

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

ไม่ใช่ทุกฟอร์แมตที่เติมข้อมูล assetInfo. FBX และ COLLADA มีเมตาดาต้าครบถ้วน; OBJ และ STL โดยทั่วไปไม่มี.

อ้างอิงตัวเลือกการโหลดตามรูปแบบ

รูปแบบคลาส Optionsพาธนำเข้าตัวเลือกสำคัญ
OBJObjLoadOptions@aspose/3d/formats/objenableMaterials, flipCoordinateSystem, scale, normalizeNormal
glTF / GLB(ไม่จำเป็นต้องมี)N/Aตรวจจับอัตโนมัติ; ไม่ต้องใช้คลาส Options
STLStlLoadOptions@aspose/3d/formats/stl(ไม่มีตัวเลือกที่กำหนดค่าได้ในเวอร์ชันปัจจุบัน)
FBXFbxLoadOptions@aspose/3d/formats/fbx(ไม่มีตัวเลือกที่กำหนดค่าได้ในเวอร์ชันปัจจุบัน)
COLLADAColladaLoadOptions@aspose/3d/formats/collada(ไม่มีตัวเลือกที่กำหนดค่าได้ในเวอร์ชันปัจจุบัน)
3MF(ไม่จำเป็นต้องมี)N/Aตรวจจับอัตโนมัติ

ข้อผิดพลาดทั่วไปในการโหลด

อาการสาเหตุแก้ไข
Error: Cannot find module '@aspose/3d/formats/obj'moduleResolution ไม่ได้ตั้งค่าเป็น nodeเพิ่ม "moduleResolution": "node" ไปยัง tsconfig.json
รายการโหนดว่างหลังจากโหลด OBJ.mtl ไฟล์ sidecar หายหรือ enableMaterials ไม่ได้ตั้งค่าตั้งค่า options.enableMaterials = true และตรวจสอบให้แน่ใจว่า .mtl อยู่ในไดเรกทอรีเดียวกัน
ฉากโหลดได้แต่ rootNode.childNodes ว่างเปล่ารูปแบบใช้ mesh รากเดียว ไม่ใช่โหนดลูกการเข้าถึง scene.rootNode.entity โดยตรง
openFromBuffer โยนข้อผิดพลาดพร้อม STL แบบไบนารีบัฟเฟอร์ถูกตัดอย่างไม่ถูกต้อง (ไบต์ส่วนท้ายหายไป)ใช้เต็มรูปแบบ readFileSync() ผลลัพธ์โดยไม่ตัดส่วน
อาร์เรย์คลิปแอนิเมชันว่างเปล่าหลังจากโหลด FBXไฟล์ FBX ใช้การแปลงที่บันทึกไว้ (baked transforms) ไม่ใช่คลิปแอนิเมชันถูกบันทึกไว้ต่อเฟรม; อาร์เรย์คลิปจะว่างเปล่า

ดูเพิ่มเติม

 ภาษาไทย