diff --git a/bin/obj2gltf.js b/bin/obj2gltf.js index 40ffe76f..4fb68a11 100644 --- a/bin/obj2gltf.js +++ b/bin/obj2gltf.js @@ -134,6 +134,10 @@ const argv = yargs choices: ['X', 'Y', 'Z'], type: 'string', default: 'Y' + }, + materialOverrides : { + describe: 'A JSON string describing overriden textures for materials in the .mtl file. Allows specifying different overrides for each material in the .mtl file. Example: \'{\"material1Name\": \"diffuseTexture\": \"PATH_TO_TEXTURE\", \"alphaTexture\": \"PATH_TO_TEXTURE\"}, "material2Name": {...}}\'', + type: 'string' } }).parse(args); @@ -181,7 +185,8 @@ const options = { overridingTextures : overridingTextures, outputDirectory : outputDirectory, inputUpAxis : argv.inputUpAxis, - outputUpAxis : argv.outputUpAxis + outputUpAxis : argv.outputUpAxis, + materialOverrides: JSON.parse(argv.materialOverrides || '{}') }; console.time('Total'); diff --git a/lib/loadMtl.js b/lib/loadMtl.js index 7a3d00d6..5a9d8296 100644 --- a/lib/loadMtl.js +++ b/lib/loadMtl.js @@ -48,6 +48,7 @@ function loadMtl(mtlPath, options) { const overridingDiffuseTexture = overridingTextures.baseColorTexture; const overridingEmissiveTexture = overridingTextures.emissiveTexture; const overridingAlphaTexture = overridingTextures.alphaTexture; + const materialOverrides = defined(options.materialOverrides) ? options.materialOverrides : {}; // Textures that are packed into PBR textures need to be decoded first const decodeOptions = { @@ -68,16 +69,17 @@ function loadMtl(mtlPath, options) { }; function createMaterial(name) { + const thisMaterialOverrides = defined(materialOverrides[name]) ? materialOverrides[name] : {}; material = new Material(); material.name = name; material.specularShininess = options.metallicRoughness ? 1.0 : 0.0; - material.specularTexture = overridingSpecularTexture; - material.specularShininessTexture = overridingSpecularShininessTexture; - material.diffuseTexture = overridingDiffuseTexture; - material.ambientTexture = overridingAmbientTexture; - material.normalTexture = overridingNormalTexture; - material.emissiveTexture = overridingEmissiveTexture; - material.alphaTexture = overridingAlphaTexture; + material.specularTexture = thisMaterialOverrides.specularTexture || overridingSpecularTexture; + material.specularShininessTexture = thisMaterialOverrides.specularShininessTexture || overridingSpecularShininessTexture; + material.diffuseTexture = thisMaterialOverrides.diffuseTexture || overridingDiffuseTexture; + material.ambientTexture = thisMaterialOverrides.ambientTexture || overridingAmbientTexture; + material.normalTexture = thisMaterialOverrides.normalTexture || overridingNormalTexture; + material.emissiveTexture = thisMaterialOverrides.emissiveTexture || overridingEmissiveTexture; + material.alphaTexture = thisMaterialOverrides.alphaTexture || overridingAlphaTexture; materials.push(material); } @@ -139,31 +141,31 @@ function loadMtl(mtlPath, options) { value = line.substring(3).trim(); material.alpha = correctAlpha(1.0 - parseFloat(value)); } else if (/^map_Ka /i.test(line)) { - if (!defined(overridingAmbientTexture)) { + if (!defined(material.ambientTexture)) { material.ambientTexture = normalizeTexturePath(line.substring(7).trim(), mtlDirectory); } } else if (/^map_Ke /i.test(line)) { - if (!defined(overridingEmissiveTexture)) { + if (!defined(material.emissiveTexture)) { material.emissiveTexture = normalizeTexturePath(line.substring(7).trim(), mtlDirectory); } } else if (/^map_Kd /i.test(line)) { - if (!defined(overridingDiffuseTexture)) { + if (!defined(material.diffuseTexture)) { material.diffuseTexture = normalizeTexturePath(line.substring(7).trim(), mtlDirectory); } } else if (/^map_Ks /i.test(line)) { - if (!defined(overridingSpecularTexture)) { + if (!defined(material.specularTexture)) { material.specularTexture = normalizeTexturePath(line.substring(7).trim(), mtlDirectory); } } else if (/^map_Ns /i.test(line)) { - if (!defined(overridingSpecularShininessTexture)) { + if (!defined(material.specularShininessTexture)) { material.specularShininessTexture = normalizeTexturePath(line.substring(7).trim(), mtlDirectory); } } else if (/^map_Bump /i.test(line)) { - if (!defined(overridingNormalTexture)) { + if (!defined(material.normalTexture)) { material.normalTexture = normalizeTexturePath(line.substring(9).trim(), mtlDirectory); } } else if (/^map_d /i.test(line)) { - if (!defined(overridingAlphaTexture)) { + if (!defined(material.alphaTexture)) { material.alphaTexture = normalizeTexturePath(line.substring(6).trim(), mtlDirectory); } }