From 05e9788feab072c423874c11f993a3173a37b6a4 Mon Sep 17 00:00:00 2001 From: Sean Lilley Date: Wed, 31 Oct 2018 22:02:13 -0400 Subject: [PATCH] Port over #116 --- CHANGES.md | 1 + lib/loadObj.js | 21 +++++++- .../box-mtllib-spaces/box mtllib blue.mtl | 12 +++++ .../box-mtllib-spaces/box mtllib green.mtl | 12 +++++ .../data/box-mtllib-spaces/box mtllib red.mtl | 12 +++++ specs/data/box-mtllib-spaces/box mtllib.obj | 50 +++++++++++++++++++ specs/lib/loadObjSpec.js | 12 +++++ 7 files changed, 118 insertions(+), 2 deletions(-) create mode 100644 specs/data/box-mtllib-spaces/box mtllib blue.mtl create mode 100644 specs/data/box-mtllib-spaces/box mtllib green.mtl create mode 100644 specs/data/box-mtllib-spaces/box mtllib red.mtl create mode 100644 specs/data/box-mtllib-spaces/box mtllib.obj diff --git a/CHANGES.md b/CHANGES.md index 77c3ce7..74a16fa 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -8,6 +8,7 @@ Change Log * Fixed handling of unnormalized input normals. [#163](https://github.com/AnalyticalGraphicsInc/obj2gltf/pull/163) * Added ability to use the first material in the mtl file when the obj is missing `usemtl`. [#163](https://github.com/AnalyticalGraphicsInc/obj2gltf/pull/163) * Fixed loading faces that contain less than 3 vertices. [#163](https://github.com/AnalyticalGraphicsInc/obj2gltf/pull/163) +* Fixed loading mtllib paths that contain spaces. [#163](https://github.com/AnalyticalGraphicsInc/obj2gltf/pull/163) ### 1.3.4 2018-10-16 diff --git a/lib/loadObj.js b/lib/loadObj.js index ff1333a..441504c 100644 --- a/lib/loadObj.js +++ b/lib/loadObj.js @@ -346,8 +346,8 @@ function loadObj(objPath, options) { var materialName = line.substring(7).trim(); useMaterial(materialName); } else if (/^mtllib/i.test(line)) { - var paths = line.substring(7).trim().split(' '); - mtlPaths = mtlPaths.concat(paths); + var mtllibLine = line.substring(7).trim(); + mtlPaths = mtlPaths.concat(getMtlPaths(mtllibLine)); } else if ((result = vertexPattern.exec(line)) !== null) { var position = scratchCartesian; position.x = parseFloat(result[1]); @@ -419,6 +419,23 @@ function loadObj(objPath, options) { }); } +function getMtlPaths(mtllibLine) { + // Handle paths with spaces. E.g. mtllib my material file.mtl + var mtlPaths = []; + var splits = mtllibLine.split(' '); + var length = splits.length; + var startIndex = 0; + for (var i = 0; i < length; ++i) { + if (path.extname(splits[i]) !== '.mtl') { + continue; + } + var mtlPath = splits.slice(startIndex, i + 1).join(' '); + mtlPaths.push(mtlPath); + startIndex = i + 1; + } + return mtlPaths; +} + function finishLoading(nodes, mtlPaths, objPath, usesMaterials, options) { nodes = cleanNodes(nodes); if (nodes.length === 0) { diff --git a/specs/data/box-mtllib-spaces/box mtllib blue.mtl b/specs/data/box-mtllib-spaces/box mtllib blue.mtl new file mode 100644 index 0000000..d3fe863 --- /dev/null +++ b/specs/data/box-mtllib-spaces/box mtllib blue.mtl @@ -0,0 +1,12 @@ +# Blender MTL File: 'box-multiple-materials.blend' +# Material Count: 1 + +newmtl Blue +Ns 96.078431 +Ka 0.000000 0.000000 0.000000 +Kd 0.000000 0.000000 0.640000 +Ks 0.500000 0.500000 0.500000 +Ke 0.000000 0.000000 0.000000 +Ni 1.000000 +d 1.000000 +illum 2 diff --git a/specs/data/box-mtllib-spaces/box mtllib green.mtl b/specs/data/box-mtllib-spaces/box mtllib green.mtl new file mode 100644 index 0000000..89abba9 --- /dev/null +++ b/specs/data/box-mtllib-spaces/box mtllib green.mtl @@ -0,0 +1,12 @@ +# Blender MTL File: 'box-multiple-materials.blend' +# Material Count: 1 + +newmtl Green +Ns 96.078431 +Ka 0.000000 0.000000 0.000000 +Kd 0.000000 0.640000 0.000000 +Ks 0.500000 0.500000 0.500000 +Ke 0.000000 0.000000 0.000000 +Ni 1.000000 +d 1.000000 +illum 2 diff --git a/specs/data/box-mtllib-spaces/box mtllib red.mtl b/specs/data/box-mtllib-spaces/box mtllib red.mtl new file mode 100644 index 0000000..3721d86 --- /dev/null +++ b/specs/data/box-mtllib-spaces/box mtllib red.mtl @@ -0,0 +1,12 @@ +# Blender MTL File: 'box-multiple-materials.blend' +# Material Count: 1 + +newmtl Red +Ns 96.078431 +Ka 0.000000 0.000000 0.000000 +Kd 0.640000 0.000000 0.000000 +Ks 0.500000 0.500000 0.500000 +Ke 0.000000 0.000000 0.000000 +Ni 1.000000 +d 1.000000 +illum 2 diff --git a/specs/data/box-mtllib-spaces/box mtllib.obj b/specs/data/box-mtllib-spaces/box mtllib.obj new file mode 100644 index 0000000..fe03d89 --- /dev/null +++ b/specs/data/box-mtllib-spaces/box mtllib.obj @@ -0,0 +1,50 @@ +# Blender v2.78 (sub 0) OBJ File: 'box-multiple-materials.blend' +# www.blender.org +mtllib box mtllib red.mtl +mtllib box mtllib green.mtl box mtllib blue.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 Red +f 3/1/1 7/2/1 5/3/1 1/4/1 +usemtl Green +f 1/9/3 2/10/3 4/11/3 3/12/3 +usemtl Blue +f 3/1/5 4/6/5 8/17/5 7/18/5 +usemtl Red +f 8/5/2 4/6/2 2/7/2 6/8/2 +usemtl Green +f 7/13/4 8/14/4 6/15/4 5/16/4 +usemtl Blue +f 5/19/6 6/20/6 2/7/6 1/4/6 diff --git a/specs/lib/loadObjSpec.js b/specs/lib/loadObjSpec.js index b0b5a4b..2e26545 100644 --- a/specs/lib/loadObjSpec.js +++ b/specs/lib/loadObjSpec.js @@ -27,6 +27,7 @@ var objNoMaterialsUrl = 'specs/data/box-no-materials/box-no-materials.obj'; var objMultipleMaterialsUrl = 'specs/data/box-multiple-materials/box-multiple-materials.obj'; var objUncleanedUrl = 'specs/data/box-uncleaned/box-uncleaned.obj'; var objMtllibUrl = 'specs/data/box-mtllib/box-mtllib.obj'; +var objMtllibSpacesUrl = 'specs/data/box-mtllib-spaces/box mtllib.obj'; var objMissingMtllibUrl = 'specs/data/box-missing-mtllib/box-missing-mtllib.obj'; var objMissingUsemtlUrl = 'specs/data/box-missing-usemtl/box-missing-usemtl.obj'; var objExternalResourcesUrl = 'specs/data/box-external-resources/box-external-resources.obj'; @@ -313,6 +314,17 @@ describe('loadObj', function() { }), done).toResolve(); }); + it('loads obj with mtllib paths with spaces', function(done) { + expect(loadObj(objMtllibSpacesUrl, defaultOptions) + .then(function(data) { + var materials = data.materials; + expect(Object.keys(materials).length).toBe(3); + expect(materials['Blue'].diffuseColor).toEqual([0.0, 0.0, 0.64, 1.0]); + expect(materials['Green'].diffuseColor).toEqual([0.0, 0.64, 0.0, 1.0]); + expect(materials['Red'].diffuseColor).toEqual([0.64, 0.0, 0.0, 1.0]); + }), done).toResolve(); + }); + it('loads obj with missing mtllib', function(done) { expect(loadObj(objMissingMtllibUrl, defaultOptions) .then(function(data) {