Better usemtl handling

This commit is contained in:
Sean Lilley 2017-12-28 16:16:26 -05:00
parent 1bebb590ef
commit 400dc3227b
2 changed files with 15 additions and 2 deletions

View File

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

View File

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