| 370 | model.gid = `${path}|${normalize}`; |
| 371 | |
| 372 | async function getMaterials(lines) { |
| 373 | const parsedMaterialPromises = []; |
| 374 | |
| 375 | for (let line of lines) { |
| 376 | const mtllibMatch = line.match(/^mtllib (.+)/); |
| 377 | |
| 378 | if (mtllibMatch) { |
| 379 | // Object has material |
| 380 | let mtlPath = ''; |
| 381 | const mtlFilename = mtllibMatch[1]; |
| 382 | const objPathParts = path.split('/'); |
| 383 | if (objPathParts.length > 1) { |
| 384 | objPathParts.pop(); |
| 385 | const objFolderPath = objPathParts.join('/'); |
| 386 | mtlPath = objFolderPath + '/' + mtlFilename; |
| 387 | } else { |
| 388 | mtlPath = mtlFilename; |
| 389 | } |
| 390 | |
| 391 | parsedMaterialPromises.push( |
| 392 | fileExists(mtlPath).then(exists => { |
| 393 | if (exists) { |
| 394 | return parseMtl(mtlPath); |
| 395 | } else { |
| 396 | console.warn(`MTL file not found or error in parsing; proceeding without materials: ${mtlPath}`); |
| 397 | return {}; |
| 398 | |
| 399 | } |
| 400 | }).catch(error => { |
| 401 | console.warn(`Error loading MTL file: ${mtlPath}`, error); |
| 402 | return {}; |
| 403 | }) |
| 404 | ); |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | try { |
| 409 | const parsedMaterials = await Promise.all(parsedMaterialPromises); |
| 410 | const materials = Object.assign({}, ...parsedMaterials); |
| 411 | return materials; |
| 412 | } catch (error) { |
| 413 | return {}; |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | try{ |
| 418 | if (fileType.match(/\.stl$/i)) { |