diff --git a/lib/loadMtl.js b/lib/loadMtl.js index b39b5eb..021d9ec 100644 --- a/lib/loadMtl.js +++ b/lib/loadMtl.js @@ -217,14 +217,17 @@ function loadMaterialTexture(material, name, texturePath, textureOptions, mtlDir var texturePromise = texturePromiseMap[texturePath]; if (!defined(texturePromise)) { + var shallowPath = path.resolve(path.join(mtlDirectory, path.basename(texturePath))); if (options.secure && outsideDirectory(texturePath, mtlDirectory)) { - options.logger('Could not read texture file at ' + texturePath + ' because it is outside of the mtl directory and the secure flag is true. This texture will be ignored.'); - texturePromise = Promise.resolve(); + // Try looking for the texture in the same directory as the obj + texturePromise = loadTexture(shallowPath, textureOptions) + .catch(function() { + options.logger('Could not read texture file at ' + texturePath + ' because it is outside of the mtl directory and the secure flag is true. This texture will be ignored.'); + }); } 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() { diff --git a/lib/loadObj.js b/lib/loadObj.js index 2760c01..1bdd79a 100644 --- a/lib/loadObj.js +++ b/lib/loadObj.js @@ -516,15 +516,21 @@ function loadMtls(mtlPaths, objPath, options) { var materials = []; return Promise.map(mtlPaths, function(mtlPath) { mtlPath = path.resolve(objDirectory, mtlPath); + var shallowPath = path.resolve(path.join(objDirectory, path.basename(mtlPath))); if (options.secure && outsideDirectory(mtlPath, objDirectory)) { - 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; + // Try looking for the .mtl in the same directory as the obj + return loadMtl(shallowPath, options) + .then(function(materialsInMtl) { + materials = materials.concat(materialsInMtl); + }) + .catch(function() { + 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 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) { diff --git a/specs/data/box-external-resources-in-root/box-external-resources-in-root.mtl b/specs/data/box-external-resources-in-root/box-external-resources-in-root.mtl new file mode 100644 index 0000000..2e2d3a5 --- /dev/null +++ b/specs/data/box-external-resources-in-root/box-external-resources-in-root.mtl @@ -0,0 +1,13 @@ +# Blender MTL File: 'box.blend' +# Material Count: 1 + +newmtl MaterialTextured +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 ../box-textured/cesium.png diff --git a/specs/data/box-external-resources-in-root/box-external-resources-in-root.obj b/specs/data/box-external-resources-in-root/box-external-resources-in-root.obj new file mode 100644 index 0000000..6b17fc5 --- /dev/null +++ b/specs/data/box-external-resources-in-root/box-external-resources-in-root.obj @@ -0,0 +1,46 @@ +# Blender v2.78 (sub 0) OBJ File: 'box-multiple-materials.blend' +# www.blender.org +mtllib box-external-resources-in-root.mtl +mtllib ../box/box.mtl +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 1.0000 +vt 0.0000 1.0000 +vt 0.0000 0.0000 +vt 1.0000 0.0000 +vn 0.0000 -1.0000 0.0000 +vn 0.0000 1.0000 0.0000 +vn -1.0000 0.0000 0.0000 +vn 1.0000 0.0000 0.0000 +vn 0.0000 0.0000 -1.0000 +vn 0.0000 0.0000 1.0000 +usemtl MaterialTextured +f 3/1/1 7/2/1 5/3/1 1/4/1 +f 1/9/3 2/10/3 4/11/3 3/12/3 +f 3/1/5 4/6/5 8/17/5 7/18/5 +usemtl Material +f 8/5/2 4/6/2 2/7/2 6/8/2 +f 7/13/4 8/14/4 6/15/4 5/16/4 +f 5/19/6 6/20/6 2/7/6 1/4/6 diff --git a/specs/data/box-external-resources-in-root/box.mtl b/specs/data/box-external-resources-in-root/box.mtl new file mode 100644 index 0000000..4f8d129 --- /dev/null +++ b/specs/data/box-external-resources-in-root/box.mtl @@ -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 diff --git a/specs/data/box-external-resources-in-root/cesium.png b/specs/data/box-external-resources-in-root/cesium.png new file mode 100644 index 0000000..3b8baee Binary files /dev/null and b/specs/data/box-external-resources-in-root/cesium.png differ diff --git a/specs/lib/loadMtlSpec.js b/specs/lib/loadMtlSpec.js index a57e68e..eb6ebb6 100644 --- a/specs/lib/loadMtlSpec.js +++ b/specs/lib/loadMtlSpec.js @@ -14,7 +14,8 @@ 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 resourcesInRootMaterialPath = 'specs/data/box-resources-in-root/box-resources-in-root.mtl'; +var externalInRootMaterialPath = 'specs/data/box-external-resources-in-root/box-external-resources-in-root.mtl'; var transparentMaterialPath = 'specs/data/box-transparent/box-transparent.mtl'; var diffuseTexturePath = 'specs/data/box-textured/cesium.png'; @@ -213,7 +214,19 @@ describe('loadMtl', function() { }); it('loads textures from root directory when the texture paths do not exist', function(done) { - expect(loadMtl(resourcesInRootPath, options) + expect(loadMtl(resourcesInRootMaterialPath, 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('loads textures from root directory when texture is outside of the mtl directory and secure is true', function(done) { + options.secure = true; + + expect(loadMtl(externalInRootMaterialPath, options) .then(function(materials) { var material = materials[0]; var baseColorTexture = material.pbrMetallicRoughness.baseColorTexture; diff --git a/specs/lib/loadObjSpec.js b/specs/lib/loadObjSpec.js index a5ffb4a..5abb287 100644 --- a/specs/lib/loadObjSpec.js +++ b/specs/lib/loadObjSpec.js @@ -24,6 +24,7 @@ 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 objExternalResourcesInRootPath = 'specs/data/box-external-resources-in-root/box-external-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'; @@ -337,6 +338,22 @@ describe('loadObj', function() { }), done).toResolve(); }); + it('loads .mtl from root directory when the .mtl path is outside of the obj directory and secure is true', function(done) { + options.secure = true; + + expect(loadObj(objExternalResourcesInRootPath, options) + .then(function(data) { + var materials = data.materials; + expect(materials.length).toBe(2); + + // .mtl files are loaded in an arbitrary order, so find the "MaterialTextured" material + var materialTextured = materials[0].name === 'MaterialTextured' ? materials[0] : materials[1]; + var baseColorTexture = materialTextured.pbrMetallicRoughness.baseColorTexture; + expect(baseColorTexture.source).toBeDefined(); + expect(baseColorTexture.name).toEqual('cesium'); + }), done).toResolve(); + }); + it('loads obj with texture', function(done) { expect(loadObj(objTexturedPath, options) .then(function(data) {