Skip to content

Commit

Permalink
fixed unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
diyaayay committed Jan 7, 2024
1 parent a3fdfbe commit a47fbbc
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 6 deletions.
49 changes: 49 additions & 0 deletions src/webgl/loading.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,55 @@ p5.prototype.loadModel = function(path) {
return model;
};

function parseMtl(mtlPath){ //accepts mtlPath to load file
return new Promise((resolve,reject)=>{
// console.log('parser is called');
let currentMaterial = null;
p5.prototype.loadStrings(
mtlPath,
lines => {
for (let line = 0; line < lines.length; ++line){
const tokens = lines[line].trim().split(/\s+/);
if(tokens[0] === 'newmtl') {
const materialName = tokens[1];
currentMaterial = materialName;
materials[currentMaterial] = {};
}else if (tokens[0] === 'Kd'){
//Diffuse color
materials[currentMaterial].diffuseColor = [
parseFloat(tokens[1]),
parseFloat(tokens[2]),
parseFloat(tokens[3])
];
} else if (tokens[0] === 'Ka'){
//Ambient Color
materials[currentMaterial].ambientColor = [
parseFloat(tokens[1]),
parseFloat(tokens[2]),
parseFloat(tokens[3])
];
}else if (tokens[0] === 'Ks'){
//Specular color
materials[currentMaterial].specularColor = [
parseFloat(tokens[1]),
parseFloat(tokens[2]),
parseFloat(tokens[3])
];

}else if (tokens[0] === 'map_Kd') {
//Texture path
materials[currentMaterial].texturePath = tokens[1];
}
}
// console.log(materials);
resolve(materials);
},
reject
);
});

}

/**
* Parse OBJ lines into model. For reference, this is what a simple model of a
* square might look like:
Expand Down
10 changes: 4 additions & 6 deletions test/unit/io/loadModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,27 +75,25 @@ suite('loadModel', function() {

testSketchWithPromise('success callback is called', function(
sketch,
resolve,
reject
done
) {
var hasBeenCalled = false;
sketch.preload = function() {
sketch.loadModel(
validFile,
function() {
hasBeenCalled = true;
done();
},
function(err) {
reject(new Error('Error callback was entered: ' + err));
done(new Error('Error callback was entered: ' + err));
}
);
};

sketch.setup = function() {
if (!hasBeenCalled) {
reject(new Error('Setup called prior to success callback'));
} else {
setTimeout(resolve, 50);
done(new Error('Setup called prior to success callback'));
}
};
});
Expand Down

0 comments on commit a47fbbc

Please sign in to comment.