Loading files
This file is pinned to the selected release. Download the original to open it in your design tool.
import * as THREE from 'three';
import type { GLTF } from 'three/examples/jsm/loaders/GLTFLoader.js';
import type { CollectedMesh } from '../types/mesh';
import { alignToGround } from './meshCollector';
import { generateOBJString, generateMTLString, createOBJBlob, downloadOBJBlobs } from './obj/objExport';
/**
* THREE.js Object3D를 OBJ Blob으로 변환 (MTL 메테리얼 포함)
* @param object3D - Export할 3D 객체
* @param filename - 파일명 (확장자 제외)
* @param additionalRotation - 추가 회전값 (라디안, X축)
* @returns OBJ 파일 Blob과 MTL 파일 Blob
*/
export function exportToOBJBlob(object3D: THREE.Object3D, filename: string = 'model', additionalRotation: number = 0): { objBlob: Blob; mtlBlob: Blob } {
try {
// 모든 메시 수집 및 변환
const meshes: Array<{ geometry: THREE.BufferGeometry; material: THREE.Material | THREE.Material[] }> = [];
object3D.traverse((child) => {
if (child instanceof THREE.Mesh && child.geometry) {
const geo = child.geometry.clone();
// World matrix 적용 (현재 위치/회전/스케일 모두 포함)
child.updateMatrixWorld(true);
geo.applyMatrix4(child.matrixWorld);
// 추가 회전 적용 (QR 판만 눕히기)
if (additionalRotation !== 0) {
const rotationMatrix = new THREE.Matrix4().makeRotationX(additionalRotation);
geo.applyMatrix4(rotationMatrix);
}
meshes.push({ geometry: geo, material: child.material });
}
});
if (meshes.length === 0) {
throw new Error('No meshes found in object');
}
// 바운딩 박스 계산하여 바닥면 정렬
const alignedMeshes = alignToGround(
meshes.map((m) => ({ ...m, partName: 'default' }))
);
// 공통 모듈 사용하여 OBJ/MTL 생성
const objString = generateOBJString(alignedMeshes, filename);
const mtlString = generateMTLString(alignedMeshes, filename);
return createOBJBlob(objString, mtlString);
} catch (error) {
console.error('OBJ export failed:', error);
throw error;
}
}
/**
* THREE.js Object3D를 OBJ 파일로 변환하여 다운로드
* @param object3D - Export할 3D 객체
* @param filename - 저장할 파일명 (확장자 제외)
*/
export function exportToOBJ(object3D: THREE.Object3D, filename: string = 'model') {
try {
const { objBlob, mtlBlob } = exportToOBJBlob(object3D, filename);
downloadOBJBlobs(objBlob, mtlBlob, filename);
} catch (error) {
console.error('OBJ export failed:', error);
throw error;
}
}
/**
* GLB 파츠들을 OBJ로 변환 (디버깅용, 옆으로 눕혀서 바닥 접지)
* front.glb와 back.glb만 70도 기울임
* @param backGltf - back.glb
* @param brigeGltf - brige.glb
* @param frontGltf - front.glb
* @param pinGltf - pin.glb
* @param filename - 저장할 파일명 (확장자 제외)
*/
export function exportGLBPartsToOBJ(
backGltf: GLTF,
brigeGltf: GLTF,
frontGltf: GLTF,
pinGltf: GLTF,
filename: string = 'glb_parts_debug'
) {
try {
// 파츠별 회전값 정의 (GLB 파일이 이미 Three.js 좌표계로 변환되어 있음)
const parts = [
{ name: 'back', gltf: backGltf, rotationX: 0 },
{ name: 'brige', gltf: brigeGltf, rotationX: 0 },
{ name: 'front', gltf: frontGltf, rotationX: 0 },
{ name: 'pin', gltf: pinGltf, rotationX: 0 },
];
// 모든 메시 수집 (파츠별 회전 적용)
const allMeshes: Array<{ geometry: THREE.BufferGeometry; material: THREE.Material | THREE.Material[] }> = [];
parts.forEach(({ gltf, rotationX }) => {
gltf.scene.traverse((child) => {
if (child instanceof THREE.Mesh && child.geometry) {
const geo = child.geometry.clone();
// World matrix 적용
child.updateMatrixWorld(true);
geo.applyMatrix4(child.matrixWorld);
// 파츠별 회전 적용 (front, back만 70도)
if (rotationX !== 0) {
const rotationMatrix = new THREE.Matrix4().makeRotationX(rotationX);
geo.applyMatrix4(rotationMatrix);
}
allMeshes.push({ geometry: geo, material: child.material });
}
});
});
if (allMeshes.length === 0) {
throw new Error('No meshes found in GLB parts');
}
// 바운딩 박스 계산하여 바닥면 정렬
const alignedMeshes = alignToGround(
allMeshes.map((m) => ({ ...m, partName: 'default' }))
);
// 공통 모듈 사용하여 OBJ/MTL 생성
const objString = generateOBJString(alignedMeshes, filename, '# Generated by 3D QR Platform (GLB Parts Debug)');
const mtlString = generateMTLString(alignedMeshes, filename);
const { objBlob, mtlBlob } = createOBJBlob(objString, mtlString);
// 다운로드
downloadOBJBlobs(objBlob, mtlBlob, filename);
} catch (error) {
console.error('GLB Parts OBJ export failed:', error);
throw error;
}
}
/**
* CollectedMesh 배열을 OBJ로 export (Preview 씬의 상태 그대로)
* @param meshes - meshCollector로 수집된 메시들
* @param filename - 저장할 파일명 (확장자 제외)
*/
export function exportCollectedMeshesToOBJ(
meshes: CollectedMesh[],
filename: string = 'obj_export'
) {
try {
if (meshes.length === 0) {
throw new Error('No meshes to export');
}
// 경고: 지원되지 않는 메테리얼 타입 체크
meshes.forEach(({ material, partName }) => {
const mat = Array.isArray(material) ? material[0] : material;
if (!(mat instanceof THREE.MeshStandardMaterial || mat instanceof THREE.MeshBasicMaterial)) {
console.warn(`[OBJ Export] ${partName} has unsupported material type:`, mat?.type || 'undefined');
}
});
// 공통 모듈 사용하여 OBJ/MTL 생성
const objString = generateOBJString(meshes, filename, '# Generated by 3D QR Platform (Preview Export)');
const mtlString = generateMTLString(meshes, filename);
const { objBlob, mtlBlob } = createOBJBlob(objString, mtlString);
// 다운로드
downloadOBJBlobs(objBlob, mtlBlob, filename);
} catch (error) {
console.error('Preview meshes OBJ export failed:', error);
throw error;
}
}
3b9ddfd73This file is pinned to the selected release. Download the original to open it in your design tool.