Skip to content

Commit

Permalink
Sequential to Parallel mtl file handling
Browse files Browse the repository at this point in the history
  • Loading branch information
diyaayay committed Feb 14, 2024
1 parent d7aa2f7 commit 25bbdc9
Showing 1 changed file with 37 additions and 39 deletions.
76 changes: 37 additions & 39 deletions src/webgl/loading.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,47 +160,46 @@ p5.prototype.loadModel = function(path,options) {

async function getMaterials(lines){
const parsedMaterialPromises=[];
const mtlPaths=[];
return new Promise(async (resolve,reject)=>{
for (let i = 0; i < lines.length; i++) {
const mtllibMatch = lines[i].match(/^mtllib (.+)/);
if (mtllibMatch) {
let mtlPath='';
const mtlFilename = mtllibMatch[1];
const objPathParts = path.split('/');
if(objPathParts.length > 1){
objPathParts.pop();
const objFolderPath = objPathParts.join('/');
mtlPath = objFolderPath + '/' + mtlFilename;
}else{
mtlPath = mtlFilename;
}
try {
if(await fileExists(mtlPath)){
const parsedMaterialsIndividual = await parseMtl(self, mtlPath);
mtlPaths.push(mtlPath);
parsedMaterialPromises.push(parsedMaterialsIndividual);
}else{
console.warn('MTL file not found or error in parsing; proceeding without materials.');
resolve({});
return;
}
} catch (error){
console.warn('Error loading MTL file:', error);
resolve({});
return;
}

for (let i = 0; i < lines.length; i++) {
const mtllibMatch = lines[i].match(/^mtllib (.+)/);
if (mtllibMatch) {
let mtlPath='';
const mtlFilename = mtllibMatch[1];
const objPathParts = path.split('/');
if(objPathParts.length > 1){
objPathParts.pop();
const objFolderPath = objPathParts.join('/');
mtlPath = objFolderPath + '/' + mtlFilename;
}else{
mtlPath = mtlFilename;
}
}try {
const parsedMaterials = await Promise.all(parsedMaterialPromises);
const materials=await Object.assign({}, ...parsedMaterials);
resolve (materials);
} catch (error) {
resolve({});
}
parsedMaterialPromises.push(
fileExists(mtlPath).then(exists => {
if (exists) {
return parseMtl(self, mtlPath);
} else {
console.warn(`MTL file not found or error in parsing; proceeding without materials: ${mtlPath}`);
return {};

});
}
}).catch(error => {
console.warn(`Error loading MTL file: ${mtlPath}`, error);
return {};
})
);
}
}
try {
const parsedMaterials = await Promise.all(parsedMaterialPromises);
const materials= Object.assign({}, ...parsedMaterials);
return materials ;
} catch (error) {
return {};
}
}


async function fileExists(url) {
try {
const response = await fetch(url, { method: 'HEAD' });
Expand Down Expand Up @@ -246,7 +245,6 @@ p5.prototype.loadModel = function(path,options) {
parseObj(model, lines, parsedMaterials);

}catch (error) {
console.error(error.message);
if (failureCallback) {
failureCallback(error);
} else {
Expand Down

0 comments on commit 25bbdc9

Please sign in to comment.