fix texture paths and parse texture map options

When the mtl has statements like `map_Bump -bm 0.2 ./foo.jpg` then
the options end up in the texture path.
eg: `/bar/-bm 0.2/foo.jpg`.

This commit fixes the path and parses the options.
This commit is contained in:
Tim Knip 2017-09-29 19:26:51 +02:00
parent 8f3f0d3862
commit c18f8c49eb
1 changed files with 42 additions and 0 deletions

View File

@ -195,10 +195,52 @@ loadMtl._createMaterial = function(materialOptions, options) {
return convertMaterial(combine(materialOptions, new Material()), options);
};
/**
* Parses texture map options like -o, -s, -bm which end up in the texturePath
*
* @param {String} texturePath The original texture path
* @param {Object} textureOptions This object will be filled with the options
*
* @return {String} The fixed texturePath or undefined when there's no texture options
*/
function parseMapOptions (texturePath, textureOptions) {
if (!/-(bm|t|s|o|blendu|blendv|boost|mm|texres|clamp|imfchan|type)\s+/.test(texturePath)) {
return;
}
var pathParts = texturePath.split(/[\\\/]/);
if (pathParts.length && pathParts.length > 2) {
var mapOptions = pathParts[pathParts.length - 2].split(/\s+/);
var currPart = null;
mapOptions.reduce(function (p, part) {
if (/-/.test(part)) {
currPart = part;
p[part] = [];
} else if (currPart) {
p[currPart].push(part);
}
return p;
}, textureOptions);
pathParts.splice(pathParts.length - 2, 1);
return path.join.apply(null, pathParts);
}
}
function loadMaterialTexture(material, name, texturePath, textureOptions, mtlDirectory, texturePromiseMap, texturePromises, options) {
if (!defined(texturePath)) {
return;
}
var mapOptions = {};
var newTexturePath = parseMapOptions(texturePath, mapOptions);
// TODO: handle texture options
// NOTE: this might not be a good place to do this
texturePath = newTexturePath ? newTexturePath : texturePath;
var texturePromise = texturePromiseMap[texturePath];
if (!defined(texturePromise)) {
if (options.secure && outsideDirectory(texturePath, mtlDirectory)) {