TypeScript에서 3D 모델을 저장하는 방법

TypeScript에서 3D 모델을 저장하는 방법

Aspose.3D FOSS for TypeScript는 단일 scene.save() call. 출력 형식은 파일 확장자를 통해 자동으로 감지됩니다. 이 가이드는 각 형식으로 저장하는 방법과 형식별 옵션 사용에 대해 다룹니다.

단계별 가이드

Step 1: @aspose/3d 설치

npm install @aspose/3d

단계 2: 씬 로드 또는 구성

저장하기 전에 기존 파일을 로드하거나 프로그래밍 방식으로 씬을 구축합니다.

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

// Load from file
const scene = new Scene();
scene.open('source.obj', new ObjLoadOptions());

// Or create a new empty scene
const emptyScene = new Scene();

단계 3: 자동 감지 형식으로 저장

scene.save(path) 파일 확장자를 통해 출력 형식을 감지합니다:

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

const scene = new Scene();
scene.open('input.obj', new ObjLoadOptions());

// Save as binary glTF
scene.save('output.glb');

// Save as JSON glTF
scene.save('output.gltf');

// Save as STL
scene.save('output.stl');

// Save as OBJ
scene.save('output.obj');

// Save as FBX
scene.save('output.fbx');

// Save as 3MF
scene.save('output.3mf');

// Save as COLLADA
scene.save('output.dae');

단계 4: 형식별 SaveOptions 사용

세밀한 제어를 위해 형식별 옵션 객체를 전달합니다:

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('model.obj', new ObjLoadOptions());

// Export to GLB with specific options
const saveOptions = new GltfSaveOptions();
saveOptions.binaryMode = true;   // produce GLB (binary glTF)

scene.save('output.glb', saveOptions);

단계 5: 버퍼(메모리)로 저장

사용 scene.saveToBuffer() 출력을 ~ 형태로 얻으려면 Buffer 디스크에 쓰지 않고:

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

const scene = new Scene();
scene.open('model.obj', new ObjLoadOptions());

const buffer = scene.saveToBuffer('output.glb');
console.log(`Buffer size: ${buffer.length} bytes`);

// Send via HTTP, upload to S3, etc.

단계 6: 출력 확인

저장 후 파일이 존재하고 크기가 0이 아닌지 확인합니다:

import * as fs from 'fs';

const stats = fs.statSync('output.glb');
console.log(`Saved output.glb: ${stats.size} bytes`);

형식 지원 매트릭스

확장자형식참고
.glbglTF 2.0 BinaryglTF에 권장; 모든 에셋이 단일 파일에 포함됨
.gltfglTF 2.0 JSON별도 .bin 및 텍스처 파일이 JSON과 함께 위치함
.objWavefront OBJ작성 .mtl 재질 파일이 함께 위치함 .obj 재질이 있는 경우
.stlSTL기본값: 바이너리 STL; 사용 StlSaveOptions.ascii = true 텍스트용
.fbxAutodesk FBXBinary FBX 형식
.3mf3D Manufacturing3D 프린팅 워크플로우에 적합
.daeCOLLADAXML 기반 교환 형식

일반적인 문제 및 해결 방법

Error: Unsupported format 저장할 때

확장자가 지원되는 형식과 일치하는지 확인하십시오. 라이브러리는 확장자를 사용해 형식을 감지합니다; 파일 이름이 output.xyz 실패합니다.

.obj 파일이 저장되었지만 재질이 누락되었습니다

OBJ를 저장할 때, 재질 라이브러리 (.mtl)가 자동으로 옆에 기록됩니다 .obj. 두 파일은 다시 열 때 동일한 디렉터리에 있어야 합니다. 기하학만 필요하면, 설정하십시오 ObjSaveOptions.enableMaterials = false.

Large .gltf 별도 텍스처와 함께

사용 .glb 대신 .gltf; 이진 데이터를 하나의 독립형 파일로 번들링합니다. 설정하십시오 GltfSaveOptions.binaryMode = true 전달할 때 GltfSaveOptions 인스턴스.


자주 묻는 질문

한 번 실행으로 여러 형식에 저장할 수 있나요?

예; 호출 scene.save() 다른 경로로 여러 번:

scene.save('output.glb');
scene.save('output.stl');
scene.save('output.obj');

저장하면 씬이 변경되나요?

아니오. scene.save() 씬 그래프에 대한 읽기 전용 작업입니다. 동일한 씬을 여러 형식으로 저장해도 부작용이 없습니다.

원본 파일을 덮어쓸 수 있나요?

예. 동일한 경로를 scene.save() 사용한 scene.open(). 라이브러리는 버퍼에 기록한 다음 디스크에 씁니다.


참고

 한국어