diff --git a/lib/loadMtl.js b/lib/loadMtl.js index bd76c81b..b39b5ebf 100644 --- a/lib/loadMtl.js +++ b/lib/loadMtl.js @@ -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.'); }); diff --git a/lib/loadObj.js b/lib/loadObj.js index 6aa504f9..2760c01a 100644 --- a/lib/loadObj.js +++ b/lib/loadObj.js @@ -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); }) diff --git a/specs/data/box-resources-in-root/box-resources-in-root.mtl b/specs/data/box-resources-in-root/box-resources-in-root.mtl new file mode 100644 index 00000000..7bc18460 --- /dev/null +++ b/specs/data/box-resources-in-root/box-resources-in-root.mtl @@ -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 diff --git a/specs/data/box-resources-in-root/box-resources-in-root.obj b/specs/data/box-resources-in-root/box-resources-in-root.obj new file mode 100644 index 00000000..1e9d9453 --- /dev/null +++ b/specs/data/box-resources-in-root/box-resources-in-root.obj @@ -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 diff --git a/specs/data/box-resources-in-root/cesium.png b/specs/data/box-resources-in-root/cesium.png new file mode 100644 index 00000000..3b8baee1 Binary files /dev/null and b/specs/data/box-resources-in-root/cesium.png differ diff --git a/specs/lib/loadMtlSpec.js b/specs/lib/loadMtlSpec.js index 059dd452..a57e68ef 100644 --- a/specs/lib/loadMtlSpec.js +++ b/specs/lib/loadMtlSpec.js @@ -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'; @@ -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) { diff --git a/specs/lib/loadObjSpec.js b/specs/lib/loadObjSpec.js index a2c3c10c..a5ffb4aa 100644 --- a/specs/lib/loadObjSpec.js +++ b/specs/lib/loadObjSpec.js @@ -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'; @@ -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) {