Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion bin/obj2gltf.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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');
Expand Down
30 changes: 16 additions & 14 deletions lib/loadMtl.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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);
}

Expand Down Expand Up @@ -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);
}
}
Expand Down