Skip to content

Commit

Permalink
Better usemtl handling
Browse files Browse the repository at this point in the history
  • Loading branch information
lilleyse committed Dec 28, 2017
1 parent 1bebb59 commit 400dc32
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
13 changes: 11 additions & 2 deletions lib/loadObj.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ function loadObj(objPath, options) {
var node;
var mesh;
var primitive;
var activeMaterial;

// All nodes seen in the obj
var nodes = [];
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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)));
Expand Down

0 comments on commit 400dc32

Please sign in to comment.