TypeScript에서 3D 모델 최적화 방법
Aspose.3D FOSS for TypeScript는 출력 파일 크기를 줄이고 처리량을 향상시키는 여러 전략을 제공합니다. 이 가이드는 포맷 선택, 바이너리 임베딩, 인메모리 파이프라인, 그리고 Node.js 수준 최적화를 다룹니다.
단계별 가이드
1단계: 올바른 출력 포맷 선택
GLB(바이너리 glTF)는 도구 지원이 좋으며 가장 컴팩트한 출력을 생성합니다. OBJ는 텍스트 기반이라 크기가 더 큽니다. STL은 기하학 전용 워크플로에 대해 컴팩트합니다.
| 형식 | 크기 | 재료 포함 | 애니메이션 포함 | 최적 활용 |
|---|---|---|---|---|
| GLB | 작음 | 예 (내장) | 예 | 웹, 게임, 일반 교환 |
| glTF | 중간 | 예 (별도) | 예 | 개발, 검사 |
| STL | 작음 | 아니오 | 아니오 | 3D printing, geometry-only |
| OBJ | 대형 | 분리 .mtl | 아니오 | 레거시 도구, 광범위한 호환성 |
| FBX | 중간 | 아니오* | 아니오* | 가져오기/내보내기 기능은 존재하지만 자동 감지에 연결되어 있지 않음 |
| 3MF | 작음 | 예 | 아니오 | 최신 3D 프린팅 |
2단계: 바이너리 GLB로 내보내기
GLB로 저장할 때, 설정 GltfSaveOptions.binaryMode = true 단일 자체 포함 바이너리 파일을 생성하도록 합니다. 이렇게 하면 별도의 .bin 사이드카를 피할 수 있으며 많은 3D 뷰어에 필요합니다:
import { Scene } from '@aspose/3d';
import { ObjLoadOptions } from '@aspose/3d/formats/obj';
import { GltfSaveOptions } from '@aspose/3d/formats/gltf';
const scene = new Scene();
scene.open('complex-model.obj', new ObjLoadOptions());
const opts = new GltfSaveOptions();
opts.binaryMode = true;
scene.save('optimized.glb', opts);
console.log('Saved compact binary GLB');3단계: 인메모리 파이프라인에 Buffer I/O 사용
웹 서비스에서 파일을 처리할 때, 사용 openFromBuffer 와 saveToBuffer 파일 시스템에 쓰는 것을 피하기 위해:
import { Scene } from '@aspose/3d';
import { ObjLoadOptions } from '@aspose/3d/formats/obj';
function convertInMemory(inputBuffer: Buffer): Buffer {
const scene = new Scene();
scene.openFromBuffer(inputBuffer, new ObjLoadOptions());
return scene.saveToBuffer('glb');
}4단계: 워커 스레드를 사용해 파일을 배치 처리
대규모 변환 작업의 경우 Node.js 워커 스레드에 작업을 분산시켜 여러 CPU 코어를 활용합니다:
// worker.ts
import { workerData, parentPort } from 'worker_threads';
import { Scene } from '@aspose/3d';
import { ObjLoadOptions } from '@aspose/3d/formats/obj';
const { inputPath, outputPath } = workerData;
const scene = new Scene();
scene.open(inputPath, new ObjLoadOptions());
scene.save(outputPath);
parentPort?.postMessage({ done: true, output: outputPath });// main.ts: dispatch files to workers
import { Worker } from 'worker_threads';
import * as fs from 'fs';
import * as path from 'path';
const files = fs.readdirSync('./input').filter(f => f.endsWith('.obj'));
for (const file of files) {
const inputPath = path.join('./input', file);
const outputPath = path.join('./output', file.replace('.obj', '.glb'));
const worker = new Worker('./dist/worker.js', {
workerData: { inputPath, outputPath }
});
worker.on('message', msg => console.log(`Converted: ${msg.output}`));
worker.on('error', err => console.error(`Error: ${err}`));
}5단계: 대형 모델의 메모리 모니터링
파일 크기가 50 MB를 초과하면 힙 사용량을 모니터링하고 메모리가 제한된 경우 파일을 순차적으로 처리합니다:
function logMemory(label: string) {
const used = process.memoryUsage();
console.log(`[${label}] heapUsed: ${Math.round(used.heapUsed / 1024 / 1024)} MB`);
}
logMemory('before load');
const scene = new Scene();
scene.open('large-model.obj');
logMemory('after load');
scene.save('output.glb');
logMemory('after save');매우 큰 모델의 경우 Node.js 힙을 늘립니다:
node --max-old-space-size=8192 convert.js자주 묻는 질문
가장 컴팩트한 출력 포맷은 무엇인가요?
GLB(바이너리 glTF)는 임베디드 에셋을 포함하여 재질과 텍스처가 있는 씬에 대해 가장 컴팩트한 단일 파일 출력을 제공합니다. STL은 기하학 전용 콘텐츠에 대해 더 컴팩트합니다.
@aspose/3d가 메쉬 단순화 또는 LOD를 적용하나요?
아니요. 이 라이브러리는 메쉬 토폴로지를 변경하지 않고 원본 기하학을 읽고 씁니다. 메쉬 단순화(정점 감소, LOD 생성)는 지원되지 않습니다.
재질을 제거해서 파일 크기를 줄일 수 있나요?
설정 ObjSaveOptions.enableMaterials = false OBJ로 저장할 때. glTF의 경우 모든 재질 데이터가 항상 포함됩니다; 기하학 전용 출력에는 STL을 사용하세요.