| import { |
| LoadingManager, |
| MeshPhongMaterial, |
| Mesh, |
| Color, |
| Object3D, |
| Group, |
| BoxGeometry, |
| } from "three"; |
| import { STLLoader } from "three/examples/jsm/loaders/STLLoader.js"; |
| import { GLTFLoader } from "three/examples/jsm/loaders/GLTFLoader.js"; |
| import { ColladaLoader } from "three/examples/jsm/loaders/ColladaLoader.js"; |
| import { OBJLoader } from "three/examples/jsm/loaders/OBJLoader.js"; |
|
|
| |
| |
| |
| |
| |
| |
| export const loadMeshFile = ( |
| path: string, |
| manager: LoadingManager, |
| done: (result: Object3D | Group | Mesh | null, err?: Error) => void |
| ) => { |
| |
| let ext = path.split(/\./g).pop()?.toLowerCase(); |
|
|
| |
| if (path.startsWith("blob:") && path.includes("#.")) { |
| const fragmentExt = path.split("#.").pop(); |
| if (fragmentExt) { |
| ext = fragmentExt.toLowerCase(); |
| } |
| } |
|
|
| |
| if (!ext) { |
| console.error(`Could not determine file extension for: ${path}`); |
| done(null, new Error(`Unsupported file format: ${path}`)); |
| return; |
| } |
|
|
| switch (ext) { |
| case "gltf": |
| case "glb": |
| new GLTFLoader(manager).load( |
| path, |
| (result) => done(result.scene), |
| null, |
| (err) => done(null, err as Error) |
| ); |
| break; |
| case "obj": |
| new OBJLoader(manager).load( |
| path, |
| (result) => done(result), |
| null, |
| (err) => done(null, err as Error) |
| ); |
| break; |
| case "dae": |
| new ColladaLoader(manager).load( |
| path, |
| (result) => done(result.scene), |
| null, |
| (err) => done(null, err as Error) |
| ); |
| break; |
| case "stl": |
| console.log(`🔧 Loading STL file: ${path}`); |
| new STLLoader(manager).load( |
| path, |
| (result) => { |
| console.log(`✅ STL loaded successfully: ${path}`); |
| const material = new MeshPhongMaterial(); |
| const mesh = new Mesh(result, material); |
| done(mesh); |
| }, |
| (progress) => { |
| console.log(`📊 STL loading progress: ${path}`, progress); |
| }, |
| (err) => { |
| console.error(`❌ STL loading failed: ${path}`, err); |
|
|
| |
| console.log(`🔄 Creating fallback geometry for: ${path}`); |
| const fallbackGeometry = new BoxGeometry(0.05, 0.05, 0.05); |
| const fallbackMaterial = new MeshPhongMaterial({ |
| color: 0xff6b35, |
| transparent: true, |
| opacity: 0.7, |
| }); |
| const fallbackMesh = new Mesh(fallbackGeometry, fallbackMaterial); |
| done(fallbackMesh); |
| } |
| ); |
| break; |
| default: |
| done(null, new Error(`Unsupported file format: ${ext}`)); |
| } |
| }; |
|
|
| |
| |
| |
| |
| |
| export const createColor = (color: string): Color => { |
| return new Color(color); |
| }; |
|
|