Skip to content

Commit

Permalink
Attempt to load missing resources by looking in the same directory as…
Browse files Browse the repository at this point in the history
… the obj
  • Loading branch information
lilleyse committed Nov 17, 2017
1 parent 42c4b33 commit 2c21105
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/loadMtl.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,11 @@ function loadMaterialTexture(material, name, texturePath, textureOptions, mtlDir
texturePromise = Promise.resolve();
} else {
texturePromise = loadTexture(texturePath, textureOptions)
.catch(function() {
// Try looking for the texture in the same directory as the obj
var shallowPath = path.resolve(path.join(mtlDirectory, path.basename(texturePath)));
return loadTexture(shallowPath, textureOptions);
})
.catch(function() {
options.logger('Could not read texture file at ' + texturePath + '. This texture will be ignored.');
});
Expand Down
6 changes: 6 additions & 0 deletions lib/loadObj.js
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,13 @@ function loadMtls(mtlPaths, objPath, options) {
options.logger('Could not read mtl file at ' + mtlPath + ' because it is outside of the obj directory and the secure flag is true. Using default material instead.');
return;
}

return loadMtl(mtlPath, options)
.catch(function() {
// Try looking for the .mtl in the same directory as the obj
var shallowPath = path.resolve(path.join(objDirectory, path.basename(mtlPath)));
return loadMtl(shallowPath, options);
})
.then(function(materialsInMtl) {
materials = materials.concat(materialsInMtl);
})
Expand Down
13 changes: 13 additions & 0 deletions specs/data/box-resources-in-root/box-resources-in-root.mtl
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Blender MTL File: 'box.blend'
# Material Count: 1

newmtl Material
Ns 96.078431
Ka 0.000000 0.000000 0.000000
Kd 0.640000 0.640000 0.640000
Ks 0.500000 0.500000 0.500000
Ke 0.000000 0.000000 0.000000
Ni 1.000000
d 1.000000
illum 2
map_Kd resources/textures/cesium.png
46 changes: 46 additions & 0 deletions specs/data/box-resources-in-root/box-resources-in-root.obj
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Blender v2.78 (sub 0) OBJ File: 'box.blend'
# www.blender.org
mtllib resources/box-resources-in-root.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
usemtl Material
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
Binary file added specs/data/box-resources-in-root/cesium.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions specs/lib/loadMtlSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ var texturedMaterialPath = 'specs/data/box-complex-material/box-complex-material
var texturedWithOptionsMaterialPath = 'specs/data/box-texture-options/box-texture-options.mtl';
var multipleMaterialsPath = 'specs/data/box-multiple-materials/box-multiple-materials.mtl';
var externalMaterialPath = 'specs/data/box-external-resources/box-external-resources.mtl';
var resourcesInRootPath = 'specs/data/box-resources-in-root/box-resources-in-root.mtl';
var transparentMaterialPath = 'specs/data/box-transparent/box-transparent.mtl';

var diffuseTexturePath = 'specs/data/box-textured/cesium.png';
Expand Down Expand Up @@ -211,6 +212,16 @@ describe('loadMtl', function() {
}), done).toResolve();
});

it('loads textures from root directory when the texture paths do not exist', function(done) {
expect(loadMtl(resourcesInRootPath, options)
.then(function(materials) {
var material = materials[0];
var baseColorTexture = material.pbrMetallicRoughness.baseColorTexture;
expect(baseColorTexture.source).toBeDefined();
expect(baseColorTexture.name).toBe('cesium');
}), done).toResolve();
});

it('alpha of 0.0 is treated as 1.0', function(done) {
expect(loadMtl(transparentMaterialPath, options)
.then(function(materials) {
Expand Down
10 changes: 10 additions & 0 deletions specs/lib/loadObjSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ var objUncleanedPath = 'specs/data/box-uncleaned/box-uncleaned.obj';
var objMtllibPath = 'specs/data/box-mtllib/box-mtllib.obj';
var objMissingMtllibPath = 'specs/data/box-missing-mtllib/box-missing-mtllib.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 objTexturedPath = 'specs/data/box-textured/box-textured.obj';
var objMissingTexturePath = 'specs/data/box-missing-texture/box-missing-texture.obj';
var objSubdirectoriesPath = 'specs/data/box-subdirectories/box-textured.obj';
Expand Down Expand Up @@ -327,6 +328,15 @@ describe('loadObj', function() {
}), done).toResolve();
});

it('loads .mtl from root directory when the .mtl path does not exist', function(done) {
expect(loadObj(objResourcesInRootPath, options)
.then(function(data) {
var baseColorTexture = data.materials[0].pbrMetallicRoughness.baseColorTexture;
expect(baseColorTexture.name).toBe('cesium');
expect(baseColorTexture.source).toBeDefined();
}), done).toResolve();
});

it('loads obj with texture', function(done) {
expect(loadObj(objTexturedPath, options)
.then(function(data) {
Expand Down

0 comments on commit 2c21105

Please sign in to comment.