Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.mtl files support for .obj files to render color in 3D custom geometry #6710

Merged
merged 39 commits into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
a47fbbc
fixed unit test
diyaayay Jan 7, 2024
2143dba
removed console logs
diyaayay Jan 7, 2024
3498ad0
changed calculation of diffuse to vertex colors
diyaayay Jan 9, 2024
5c7c07e
Merge branch 'processing:main' into Mtl-support-to-obj-files
diyaayay Jan 9, 2024
b46af55
removed console logs, changed default vertexColors to 1
diyaayay Jan 9, 2024
0b7a1fe
Merge branch 'Mtl-support-to-obj-files' of https://github.com/diyaaya…
diyaayay Jan 9, 2024
77637ba
Fixes vertexColor and mtlpath assignment
diyaayay Jan 10, 2024
1169993
remove console logs
diyaayay Jan 10, 2024
06c5e28
added unit test for loading obj mtl and assigned vertex colors
diyaayay Jan 10, 2024
2b46a55
unit test for missing mtl file
diyaayay Jan 10, 2024
0790146
unit tests for later when vertexColors are properly assigned
diyaayay Jan 13, 2024
1fa08a2
plant.obj to smaller file for testing
diyaayay Jan 13, 2024
be5cf6c
added multiple mtl file handling, still need to fix vertexColors
diyaayay Jan 13, 2024
cd6039d
handles other failing unit tests
diyaayay Jan 13, 2024
32f94f3
Merge branch 'processing:main' into Mtl-support-to-obj-files
diyaayay Jan 14, 2024
16fad39
vertex color assign and duplicate vertices
diyaayay Jan 19, 2024
c3538b7
Merge branch 'Mtl-support-to-obj-files' of https://github.com/diyaaya…
diyaayay Jan 19, 2024
3cb1a13
Merge branch 'main' into Mtl-support-to-obj-files
diyaayay Jan 19, 2024
90a3a53
commit before merging
diyaayay Jan 19, 2024
2fffa53
linting erros , conflicts
diyaayay Jan 19, 2024
48b07a3
had merge conflicts
diyaayay Jan 19, 2024
341b131
Merge remote-tracking branch 'upstream/main' into Mtl-support-to-obj-…
diyaayay Jan 25, 2024
60a3b06
fixes vertexColors to flat array
diyaayay Jan 25, 2024
9538edc
changes duplication logic
diyaayay Jan 25, 2024
56bc7f2
checks for existence of current material
diyaayay Jan 25, 2024
97950c7
fixes unit test for vertexColors being a flat array
diyaayay Jan 25, 2024
4e46332
added visual test for diffuse colors of obj files
diyaayay Jan 26, 2024
104463a
Fixed visual test for diffuse colors
diyaayay Jan 27, 2024
a4047af
screenshot for visual test
diyaayay Feb 1, 2024
7236340
removes console logs
diyaayay Feb 1, 2024
b32c935
normalize model for visual test
diyaayay Feb 2, 2024
19e4655
Merge remote-tracking branch 'upstream/main' into Mtl-support-to-obj-…
diyaayay Feb 2, 2024
14a2420
visual tests work for fill in .obj model and mtl color png fix
diyaayay Feb 2, 2024
687e010
adds check for either a fully colored model or colorless model
diyaayay Feb 4, 2024
2aa937f
Merge remote-tracking branch 'upstream/main' into Mtl-support-to-obj-…
diyaayay Feb 8, 2024
31bdeb9
colored model and unit test for color-no color
diyaayay Feb 9, 2024
ac68110
mtl file not found, obj displayed without materials
diyaayay Feb 12, 2024
d7aa2f7
unit test for missing mtl displayes obj file without color
diyaayay Feb 12, 2024
25bbdc9
Sequential to Parallel mtl file handling
diyaayay Feb 14, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
166 changes: 145 additions & 21 deletions src/webgl/loading.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,42 @@ p5.prototype.loadModel = function(path,options) {
model.gid = `${path}|${normalize}`;
const self = this;

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 {
const parsedMaterialsIndividual = await parseMtl(self, mtlPath);
mtlPaths.push(mtlPath);
parsedMaterialPromises.push(parsedMaterialsIndividual);
}catch (error) {
reject(error);
return;
}
}
}try {
const parsedMaterials = await Promise.all(parsedMaterialPromises);
const materials=await Object.assign({}, ...parsedMaterials);
resolve (materials);
} catch (error) {
reject(error);
}

});
}
if (fileType.match(/\.stl$/i)) {
this.httpDo(
path,
Expand Down Expand Up @@ -188,31 +224,41 @@ p5.prototype.loadModel = function(path,options) {
} else if (fileType.match(/\.obj$/i)) {
this.loadStrings(
path,
strings => {
parseObj(model, strings);

if (normalize) {
model.normalize();
}
async lines => {
try{
const parsedMaterials=await getMaterials(lines);

if (flipU) {
model.flipU();
}

if (flipV) {
model.flipV();
if(parsedMaterials && typeof parsedMaterials==='object'){
parseObj(model, lines, parsedMaterials);
}
}catch (error) {
if (failureCallback) {
failureCallback(error);
} else {
p5._friendlyError('Error during parsing: ' + error.message);
}
}
finally{
if (normalize) {
model.normalize();
}
if (flipU) {
model.flipU();
}
if (flipV) {
model.flipV();
}

self._decrementPreload();
if (typeof successCallback === 'function') {
successCallback(model);
self._decrementPreload();
if (typeof successCallback === 'function') {
successCallback(model);
}
}
},
failureCallback
);
} else {
p5._friendlyFileLoadError(3, path);

if (failureCallback) {
failureCallback();
} else {
Expand All @@ -224,6 +270,52 @@ p5.prototype.loadModel = function(path,options) {
return model;
};

function parseMtl(p5,mtlPath){
return new Promise((resolve, reject)=>{
let currentMaterial = null;
let materials= {};
p5.loadStrings(
mtlPath,
lines => {
for (let line = 0; line < lines.length; ++line){
const tokens = lines[line].trim().split(/\s+/);
if(tokens[0] === 'newmtl') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work on this parser, looks great!

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];
}
}
resolve(materials);
},reject
);
});
}

/**
* Parse OBJ lines into model. For reference, this is what a simple model of a
* square might look like:
Expand All @@ -235,7 +327,7 @@ p5.prototype.loadModel = function(path,options) {
*
* f 4 3 2 1
*/
function parseObj(model, lines) {
function parseObj(model, lines, materials= {}) {
// OBJ allows a face to specify an index for a vertex (in the above example),
// but it also allows you to specify a custom combination of vertex, UV
// coordinate, and vertex normal. So, "3/4/3" would mean, "use vertex 3 with
Expand All @@ -250,16 +342,21 @@ function parseObj(model, lines) {
vt: [],
vn: []
};
const indexedVerts = {};

const indexedVerts = {};
const usedVerts = {}; // Track colored vertices
let currentMaterial = null;
for (let line = 0; line < lines.length; ++line) {
// Each line is a separate object (vertex, face, vertex normal, etc)
// For each line, split it into tokens on whitespace. The first token
// describes the type.
const tokens = lines[line].trim().split(/\b\s+/);

if (tokens.length > 0) {
if (tokens[0] === 'v' || tokens[0] === 'vn') {
if (tokens[0] === 'usemtl') {
// Switch to a new material
currentMaterial = tokens[1];
}else if (tokens[0] === 'v' || tokens[0] === 'vn') {
// Check if this line describes a vertex or vertex normal.
// It will have three numeric parameters.
const vertex = new p5.Vector(
Expand Down Expand Up @@ -287,6 +384,7 @@ function parseObj(model, lines) {
// Now, convert the given token into an index
const vertString = tokens[vertexTokens[tokenInd]];
let vertIndex = 0;
let vertParts;

// TODO: Faces can technically use negative numbers to refer to the
// previous nth vertex. I haven't seen this used in practice, but
Expand All @@ -295,7 +393,7 @@ function parseObj(model, lines) {
if (indexedVerts[vertString] !== undefined) {
vertIndex = indexedVerts[vertString];
} else {
const vertParts = vertString.split('/');
vertParts = vertString.split('/');
for (let i = 0; i < vertParts.length; i++) {
vertParts[i] = parseInt(vertParts[i]) - 1;
}
Expand All @@ -312,8 +410,24 @@ function parseObj(model, lines) {
model.vertexNormals.push(loadedVerts.vn[vertParts[2]].copy());
}
}
if (usedVerts[vertIndex] && usedVerts[vertIndex]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right now I think we just store the last used material for each vertex. I think this means that if we reuse the same vertex first with material1, then with material2, then a third time with material1 again, we'll end up with three duplicates of the vertex instead of just two, since we make a new duplicate if the current material is not the same as the last used material.

Maybe instead of storing a map of source index → material, we can store a map from source index → another map of material → destination index. In that world, this if statement would check if usedVerts[vertIndex][currentMaterial] is defined yet, and create it if not. (This would maybe have to also absorb the else branch right above it maybe.) Then, below, we would add usedVerts[vertIndex][currentMaterial] to the current face.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, I missed out on how I could have optimized the logic here and thus have lesser duplication like in the example you mentioned above. Will work on it now. Thanks for pointing this out. @davepagurek

!== currentMaterial) {
// Duplicate vertex, UV, and normal,faces to refer to new indices
vertParts = vertString.split('/');
for (let i = 0; i < vertParts.length; i++) {
vertParts[i] = parseInt(vertParts[i]) - 1;
}
const duplicatedVertIndex = model.vertices.length;
model.vertices.push(loadedVerts.v[vertParts[0]].copy());
model.uvs.push(loadedVerts.vt[vertParts[1]] ?
loadedVerts.vt[vertParts[1]].slice() : [0, 0]);
model.vertexNormals.push(loadedVerts.vn[vertParts[2]] ?
loadedVerts.vn[vertParts[2]].copy() : new p5.Vector());
vertIndex = duplicatedVertIndex;
}

face.push(vertIndex);
usedVerts[vertIndex] = currentMaterial;
}

if (
Expand All @@ -322,6 +436,16 @@ function parseObj(model, lines) {
face[1] !== face[2]
) {
model.faces.push(face);
//same material for all vertices in a particular face
const materialDiffuseColor =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to add a check here if this exists, so that for objs without materials, we can leave the vertexColors array empty?

materials[currentMaterial].diffuseColor;
for (let i=0 ;i<face.length;i++) {
model.vertexColors.push([
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think elsewhere we treat vertexColors as a flat array:

this.immediateMode.geometry.vertexColors.push(
vertexColor[0],
vertexColor[1],
vertexColor[2],
vertexColor[3]
);

Maybe we can take out the [...] here to be consistent?

materialDiffuseColor[0],
materialDiffuseColor[1],
materialDiffuseColor[2]
]);
}
}
}
}
Expand All @@ -331,7 +455,7 @@ function parseObj(model, lines) {
if (model.vertexNormals.length === 0) {
model.computeNormals();
}

console.log(model.vertexColors);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume this is for debugging, although we should probably take this out before merging.

return model;
}

Expand Down
72 changes: 72 additions & 0 deletions test/unit/assets/octa-color.mtl
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
newmtl m000001
Ns 100
Kd 0 0 0.5
Ks 1 1 1
Ka 0 0 0
Ni 1
d 1
illum 2

newmtl m003627
Ns 100
Kd 0 0 0.942654
Ks 1 1 1
Ka 0 0 0
Ni 1
d 1
illum 2

newmtl m010778
Ns 100
Kd 0 0.815632 1
Ks 1 1 1
Ka 0 0 0
Ni 1
d 1
illum 2

newmtl m012003
Ns 100
Kd 0 0.965177 1
Ks 1 1 1
Ka 0 0 0
Ni 1
d 1
illum 2

newmtl m019240
Ns 100
Kd 0.848654 1 0.151346
Ks 1 1 1
Ka 0 0 0
Ni 1
d 1
illum 2

newmtl m021392
Ns 100
Kd 1 0.888635 0
Ks 1 1 1
Ka 0 0 0
Ni 1
d 1
illum 2

newmtl m022299
Ns 100
Kd 1 0.77791 0
Ks 1 1 1
Ka 0 0 0
Ni 1
d 1
illum 2

newmtl m032767
Ns 100
Kd 0.5 0 0
Ks 1 1 1
Ka 0 0 0
Ni 1
d 1
illum 2

23 changes: 23 additions & 0 deletions test/unit/assets/octa-color.obj
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
mtllib octa-color.mtl
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The plant obj is 9mb, which might slow down tests. Maybe for the missing material test, we can make a duplicate of this colored obj, but change the path from octa-color.mtl to something that doesn't exist?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, that's what I had thought of too, made the same changes already for plant.obj and will push them asap.

v 1 0 0
v 0 1 0
v 0 0 1
v -1 0 0
v 0 -1 0
v 0 0 -1
usemtl m000001
f 1 5 6
usemtl m003627
f 2 1 6
usemtl m010778
f 1 2 3
usemtl m012003
f 4 2 6
usemtl m019240
f 4 5 3
usemtl m021392
f 5 4 6
usemtl m022299
f 2 4 3
usemtl m032767
f 5 1 3
23 changes: 23 additions & 0 deletions test/unit/assets/plant.obj
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
mtllib plant.mtl
v 1 0 0
v 0 1 0
v 0 0 1
v -1 0 0
v 0 -1 0
v 0 0 -1
usemtl m000001
f 1 5 6
usemtl m003627
f 2 1 6
usemtl m010778
f 1 2 3
usemtl m012003
f 4 2 6
usemtl m019240
f 4 5 3
usemtl m021392
f 5 4 6
usemtl m022299
f 2 4 3
usemtl m032767
f 5 1 3
Loading
Loading