Skip to content

Commit 659b7c4

Browse files
committed
update to latest gltf2obj
1 parent 6c4dd98 commit 659b7c4

3 files changed

Lines changed: 58 additions & 16 deletions

File tree

lib/loadMtl.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -224,13 +224,26 @@ function loadMaterialTexture(material, name, textureOptions, mtlDirectory, textu
224224

225225
var texturePromise = texturePromiseMap[texturePath];
226226
if (!defined(texturePromise)) {
227+
var shallowPath = path.resolve(path.join(mtlDirectory, path.basename(texturePath)));
227228
if (options.secure && outsideDirectory(texturePath, mtlDirectory)) {
228-
options.logger('Could not read texture file at ' + texturePath + ' because it is outside of the mtl directory and the secure flag is true. This texture will be ignored.');
229-
texturePromise = Promise.resolve();
229+
// Try looking for the texture in the same directory as the obj
230+
options.logger('Texture file is outside of the mtl directory and the secure flag is true. Attempting to read the texture file from within the obj directory instead.');
231+
texturePromise = loadTexture(shallowPath, textureOptions)
232+
.catch(function(error) {
233+
options.logger(error.message);
234+
options.logger('Could not read texture file at ' + shallowPath + '. This texture will be ignored');
235+
});
230236
} else {
231237
texturePromise = loadTexture(texturePath, textureOptions)
232-
.catch(function() {
233-
options.logger('Could not read texture file at ' + texturePath + '. This texture will be ignored.');
238+
.catch(function(error) {
239+
// Try looking for the texture in the same directory as the obj
240+
options.logger(error.message);
241+
options.logger('Could not read texture file at ' + texturePath + '. Attempting to read the texture file from within the obj directory instead.');
242+
return loadTexture(shallowPath, textureOptions);
243+
})
244+
.catch(function(error) {
245+
options.logger(error.message);
246+
options.logger('Could not read texture file at ' + shallowPath + '. This texture will be ignored.');
234247
});
235248
}
236249
texturePromiseMap[texturePath] = texturePromise;

lib/loadObj.js

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ function loadObj(objPath, options) {
7171
var node;
7272
var mesh;
7373
var primitive;
74+
var activeMaterial;
7475

7576
// All nodes seen in the obj
7677
var nodes = [];
@@ -120,12 +121,15 @@ function loadObj(objPath, options) {
120121

121122
function addPrimitive() {
122123
primitive = new Primitive();
124+
primitive.material = activeMaterial;
123125
mesh.primitives.push(primitive);
124126
}
125127

126128
function useMaterial(name) {
127-
// Look to see if this material has already been used by a primitive in the mesh
128129
var material = getName(name);
130+
activeMaterial = material;
131+
132+
// Look to see if this material has already been used by a primitive in the mesh
129133
var primitives = mesh.primitives;
130134
var primitivesLength = primitives.length;
131135
for (var i = 0; i < primitivesLength; ++i) {
@@ -136,7 +140,6 @@ function loadObj(objPath, options) {
136140
}
137141
// Add a new primitive with this material
138142
addPrimitive();
139-
primitive.material = getName(name);
140143
}
141144

142145
function getOffset(a, attributeData, components) {
@@ -329,7 +332,7 @@ function loadObj(objPath, options) {
329332
function isConvex(positions2D) {
330333
var turnDirection = getTurnDirection(positions2D[0], positions2D[1], positions2D[2]);
331334
for (var i=1; i < positions2D.length-2; ++i) {
332-
var currentTurnDirection = getTurnDirection(positions2D[i], positions2D[i+1], positions2D[i+2]);
335+
var currentTurnDirection = getTurnDirection(positions2D[i], positions2D[i+1], positions2D[i+2]);
333336
if (turnDirection * currentTurnDirection < 0) {
334337
return false;
335338
}
@@ -465,7 +468,9 @@ function loadObj(objPath, options) {
465468
faceUvs.push(result[2]);
466469
faceNormals.push(result[3]);
467470
}
468-
addFace(faceVertices, facePositions, faceUvs, faceNormals);
471+
if (faceVertices.length > 2) {
472+
addFace(faceVertices, facePositions, faceUvs, faceNormals);
473+
}
469474

470475
faceVertices.length = 0;
471476
facePositions.length = 0;
@@ -531,18 +536,41 @@ function finishLoading(nodes, mtlPaths, objPath, options) {
531536
function loadMtls(mtlPaths, objPath, options) {
532537
var objDirectory = path.dirname(objPath);
533538
var materials = [];
539+
540+
// Remove duplicates
541+
mtlPaths = mtlPaths.filter(function(value, index, self) {
542+
return self.indexOf(value) === index;
543+
});
544+
534545
return Promise.map(mtlPaths, function(mtlPath) {
535546
mtlPath = path.resolve(objDirectory, mtlPath);
547+
var shallowPath = path.resolve(path.join(objDirectory, path.basename(mtlPath)));
536548
if (options.secure && outsideDirectory(mtlPath, objDirectory)) {
537-
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.');
538-
return;
549+
// Try looking for the .mtl in the same directory as the obj
550+
options.logger('The material file is outside of the obj directory and the secure flag is true. Attempting to read the material file from within the obj directory instead.');
551+
return loadMtl(shallowPath, options)
552+
.then(function(materialsInMtl) {
553+
materials = materials.concat(materialsInMtl);
554+
})
555+
.catch(function(error) {
556+
options.logger(error.message);
557+
options.logger('Could not read material file at ' + shallowPath + '. Using default material instead.');
558+
});
539559
}
560+
540561
return loadMtl(mtlPath, options)
562+
.catch(function(error) {
563+
// Try looking for the .mtl in the same directory as the obj
564+
options.logger(error.message);
565+
options.logger('Could not read material file at ' + mtlPath + '. Attempting to read the material file from within the obj directory instead.');
566+
return loadMtl(shallowPath, options);
567+
})
541568
.then(function(materialsInMtl) {
542569
materials = materials.concat(materialsInMtl);
543570
})
544-
.catch(function() {
545-
options.logger('Could not read mtl file at ' + mtlPath + '. Using default material instead.');
571+
.catch(function(error) {
572+
options.logger(error.message);
573+
options.logger('Could not read material file at ' + shallowPath + '. Using default material instead.');
546574
});
547575
}, {concurrency : 10})
548576
.then(function() {
@@ -615,13 +643,12 @@ function setDefaultNames(items, defaultName, usedNames) {
615643
}
616644

617645
function setDefaults(nodes) {
618-
var usedNodeNames = {};
619-
var usedMeshNames = {};
620-
setDefaultNames(nodes, 'Node', usedNodeNames);
646+
var usedNames = {};
647+
setDefaultNames(nodes, 'Node', usedNames);
621648
var nodesLength = nodes.length;
622649
for (var i = 0; i < nodesLength; ++i) {
623650
var node = nodes[i];
624-
setDefaultNames(node.meshes, node.name, usedMeshNames);
651+
setDefaultNames(node.meshes, node.name + '-Mesh', usedNames);
625652
}
626653
}
627654

lib/outsideDirectory.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ module.exports = outsideDirectory;
99
* @param {String} file Path to the file.
1010
* @param {String} directory Path to the directory.
1111
* @returns {Boolean} Whether the file is outside of the directory.
12+
*
13+
* @private
1214
*/
1315
function outsideDirectory(file, directory) {
1416
return (path.relative(directory, file).indexOf('..') === 0);

0 commit comments

Comments
 (0)