Attempt to load missing resources by looking in the same directory as the obj

This commit is contained in:
Sean Lilley 2017-11-17 15:07:52 -05:00
parent 42c4b33521
commit 2c21105d38
7 changed files with 91 additions and 0 deletions

View File

@ -222,6 +222,11 @@ function loadMaterialTexture(material, name, texturePath, textureOptions, mtlDir
texturePromise = Promise.resolve(); texturePromise = Promise.resolve();
} else { } else {
texturePromise = loadTexture(texturePath, textureOptions) 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() { .catch(function() {
options.logger('Could not read texture file at ' + texturePath + '. This texture will be ignored.'); options.logger('Could not read texture file at ' + texturePath + '. This texture will be ignored.');
}); });

View File

@ -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.'); 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;
} }
return loadMtl(mtlPath, options) 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) { .then(function(materialsInMtl) {
materials = materials.concat(materialsInMtl); materials = materials.concat(materialsInMtl);
}) })

View File

@ -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

View File

@ -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 not shown.

After

Width:  |  Height:  |  Size: 7.5 KiB

View File

@ -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 texturedWithOptionsMaterialPath = 'specs/data/box-texture-options/box-texture-options.mtl';
var multipleMaterialsPath = 'specs/data/box-multiple-materials/box-multiple-materials.mtl'; var multipleMaterialsPath = 'specs/data/box-multiple-materials/box-multiple-materials.mtl';
var externalMaterialPath = 'specs/data/box-external-resources/box-external-resources.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 transparentMaterialPath = 'specs/data/box-transparent/box-transparent.mtl';
var diffuseTexturePath = 'specs/data/box-textured/cesium.png'; var diffuseTexturePath = 'specs/data/box-textured/cesium.png';
@ -211,6 +212,16 @@ describe('loadMtl', function() {
}), done).toResolve(); }), 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) { it('alpha of 0.0 is treated as 1.0', function(done) {
expect(loadMtl(transparentMaterialPath, options) expect(loadMtl(transparentMaterialPath, options)
.then(function(materials) { .then(function(materials) {

View File

@ -23,6 +23,7 @@ var objUncleanedPath = 'specs/data/box-uncleaned/box-uncleaned.obj';
var objMtllibPath = 'specs/data/box-mtllib/box-mtllib.obj'; var objMtllibPath = 'specs/data/box-mtllib/box-mtllib.obj';
var objMissingMtllibPath = 'specs/data/box-missing-mtllib/box-missing-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 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 objTexturedPath = 'specs/data/box-textured/box-textured.obj';
var objMissingTexturePath = 'specs/data/box-missing-texture/box-missing-texture.obj'; var objMissingTexturePath = 'specs/data/box-missing-texture/box-missing-texture.obj';
var objSubdirectoriesPath = 'specs/data/box-subdirectories/box-textured.obj'; var objSubdirectoriesPath = 'specs/data/box-subdirectories/box-textured.obj';
@ -327,6 +328,15 @@ describe('loadObj', function() {
}), done).toResolve(); }), 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) { it('loads obj with texture', function(done) {
expect(loadObj(objTexturedPath, options) expect(loadObj(objTexturedPath, options)
.then(function(data) { .then(function(data) {