Set default material if usemtl is missing

This commit is contained in:
Sean Lilley 2018-03-06 19:22:06 -05:00
parent bbdecdece2
commit 5730d2371f
4 changed files with 87 additions and 0 deletions

View File

@ -525,6 +525,7 @@ function finishLoading(nodes, mtlPaths, objPath, options) {
var name = path.basename(objPath, path.extname(objPath));
return loadMtls(mtlPaths, objPath, options)
.then(function(materials) {
assignDefaultMaterial(nodes, materials);
return {
nodes : nodes,
materials : materials,
@ -578,6 +579,26 @@ function loadMtls(mtlPaths, objPath, options) {
});
}
function assignDefaultMaterial(nodes, materials) {
if (materials.length === 0) {
return;
}
var defaultMaterial = materials[0].name;
var nodesLength = nodes.length;
for (var i = 0; i < nodesLength; ++i) {
var meshes = nodes[i].meshes;
var meshesLength = meshes.length;
for (var j = 0; j < meshesLength; ++j) {
var primitives = meshes[j].primitives;
var primitivesLength = primitives.length;
for (var k = 0; k < primitivesLength; ++k) {
var primitive = primitives[k];
primitive.material = defaultValue(primitive.material, defaultMaterial);
}
}
}
}
function removeEmptyMeshes(meshes) {
return meshes.filter(function(mesh) {
// Remove empty primitives

View File

@ -0,0 +1,12 @@
# Blender MTL File: 'None'
# Material Count: 1
newmtl Material
Ns 96.078431
Ka 0.100000 0.000000 0.000000
Kd 0.640000 0.640000 0.640000
Ks 0.500000 0.500000 0.500000
Ke 0.000000 0.000000 0.100000
Ni 1.000000
d 1.000000
illum 2

View File

@ -0,0 +1,45 @@
# Blender v2.78 (sub 0) OBJ File: ''
# www.blender.org
mtllib box-missing-usemtl.mtl
o Cube
v -1.000000 -1.000000 1.000000
v -1.000000 1.000000 1.000000
v -1.000000 -1.000000 -1.000000
v -1.000000 1.000000 -1.000000
v 1.000000 -1.000000 1.000000
v 1.000000 1.000000 1.000000
v 1.000000 -1.000000 -1.000000
v 1.000000 1.000000 -1.000000
vt 0.0000 0.0000
vt 1.0000 0.0000
vt 1.0000 1.0000
vt 0.0000 1.0000
vt 0.0000 0.0000
vt 1.0000 0.0000
vt 1.0000 1.0000
vt 0.0000 1.0000
vt 0.0000 0.0000
vt 1.0000 0.0000
vt 1.0000 1.0000
vt 0.0000 1.0000
vt 0.0000 0.0000
vt 1.0000 0.0000
vt 1.0000 1.0000
vt 0.0000 1.0000
vt 1.0000 0.0000
vt 1.0000 1.0000
vt 0.0000 0.0000
vt 0.0000 1.0000
vn -1.0000 0.0000 0.0000
vn 0.0000 0.0000 -1.0000
vn 1.0000 0.0000 0.0000
vn 0.0000 0.0000 1.0000
vn 0.0000 -1.0000 0.0000
vn 0.0000 1.0000 0.0000
s off
f 1/1/1 2/2/1 4/3/1 3/4/1
f 3/5/2 4/6/2 8/7/2 7/8/2
f 7/9/3 8/10/3 6/11/3 5/12/3
f 5/13/4 6/14/4 2/15/4 1/16/4
f 3/5/5 7/17/5 5/18/5 1/16/5
f 8/19/6 4/6/6 2/15/6 6/20/6

View File

@ -25,6 +25,7 @@ var objUncleanedPath = 'specs/data/box-uncleaned/box-uncleaned.obj';
var objMtllibPath = 'specs/data/box-mtllib/box-mtllib.obj';
var objMtllibSpacesPath = 'specs/data/box-mtllib-spaces/box mtllib.obj';
var objMissingMtllibPath = 'specs/data/box-missing-mtllib/box-missing-mtllib.obj';
var objMissingUsemtlPath = 'specs/data/box-missing-usemtl/box-missing-usemtl.obj';
var objExternalResourcesPath = 'specs/data/box-external-resources/box-external-resources.obj';
var objResourcesInRootPath = 'specs/data/box-resources-in-root/box-resources-in-root.obj';
var objExternalResourcesInRootPath = 'specs/data/box-external-resources-in-root/box-external-resources-in-root.obj';
@ -363,6 +364,14 @@ describe('loadObj', function() {
}), done).toResolve();
});
it('loads obj with missing usemtl', function(done) {
expect(loadObj(objMissingUsemtlPath, options)
.then(function(data) {
expect(data.materials.length).toBe(1);
expect(data.nodes[0].meshes[0].primitives[0].material).toBe('Material');
}), done).toResolve();
});
it('loads .mtl outside of the obj directory', function(done) {
expect(loadObj(objExternalResourcesPath, options)
.then(function(data) {