From 400dc3227b5df8371b095b36557730ce29a85509 Mon Sep 17 00:00:00 2001 From: Sean Lilley Date: Thu, 28 Dec 2017 16:16:26 -0500 Subject: [PATCH] Better usemtl handling --- CHANGES.md | 4 ++++ lib/loadObj.js | 13 +++++++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index a4b64e7..b62fd97 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,6 +1,10 @@ Change Log ========== +### 2.2.0 ??? + +* Fixed handling of `usemtl` when appearing before an `o` or `g` token. [#121](https://github.com/AnalyticalGraphicsInc/obj2gltf/pull/121) + ### 2.1.0 2017-12-28 * Fixed loading faces that contain less than 3 vertices. [#120](https://github.com/AnalyticalGraphicsInc/obj2gltf/pull/120) diff --git a/lib/loadObj.js b/lib/loadObj.js index 70f8380..1ba9842 100644 --- a/lib/loadObj.js +++ b/lib/loadObj.js @@ -71,6 +71,7 @@ function loadObj(objPath, options) { var node; var mesh; var primitive; + var activeMaterial; // All nodes seen in the obj var nodes = []; @@ -120,12 +121,15 @@ function loadObj(objPath, options) { function addPrimitive() { primitive = new Primitive(); + primitive.material = activeMaterial; mesh.primitives.push(primitive); } function useMaterial(name) { - // Look to see if this material has already been used by a primitive in the mesh var material = getName(name); + activeMaterial = material; + + // Look to see if this material has already been used by a primitive in the mesh var primitives = mesh.primitives; var primitivesLength = primitives.length; for (var i = 0; i < primitivesLength; ++i) { @@ -136,7 +140,6 @@ function loadObj(objPath, options) { } // Add a new primitive with this material addPrimitive(); - primitive.material = getName(name); } function getOffset(a, attributeData, components) { @@ -533,6 +536,12 @@ function finishLoading(nodes, mtlPaths, objPath, options) { function loadMtls(mtlPaths, objPath, options) { var objDirectory = path.dirname(objPath); var materials = []; + + // Remove duplicates + mtlPaths = mtlPaths.filter(function(value, index, self) { + return self.indexOf(value) === index; + }); + return Promise.map(mtlPaths, function(mtlPath) { mtlPath = path.resolve(objDirectory, mtlPath); var shallowPath = path.resolve(path.join(objDirectory, path.basename(mtlPath)));