From c18f8c49eb83def983c0f102d8578597177a0a0a Mon Sep 17 00:00:00 2001 From: Tim Knip Date: Fri, 29 Sep 2017 19:26:51 +0200 Subject: [PATCH] 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. --- lib/loadMtl.js | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/lib/loadMtl.js b/lib/loadMtl.js index e5ba4ab..d57b8be 100644 --- a/lib/loadMtl.js +++ b/lib/loadMtl.js @@ -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)) {