Merge pull request #115 from AnalyticalGraphicsInc/texture-load-order

Switched texture loading order so diffuse is always first
This commit is contained in:
likangning93 2017-11-29 13:09:07 -05:00 committed by GitHub
commit 4b6421273b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -34,7 +34,6 @@ function loadMtl(mtlPath, options) {
var material; var material;
var values; var values;
var value; var value;
var texturePath;
var mtlDirectory = path.dirname(mtlPath); var mtlDirectory = path.dirname(mtlPath);
var materials = []; var materials = [];
@ -58,9 +57,9 @@ function loadMtl(mtlPath, options) {
checkTransparency : options.checkTransparency checkTransparency : options.checkTransparency
}; };
var ambientTextureOptions = options.packOcclusion ? decodeOptions : undefined; var ambientTextureOptions = defined(overridingAmbientTexture) ? undefined : (options.packOcclusion ? decodeOptions : undefined);
var specularTextureOptions = decodeOptions; var specularTextureOptions = defined(overridingSpecularTexture) ? undefined : decodeOptions;
var specularShinessTextureOptions = decodeOptions; var specularShinessTextureOptions = defined(overridingSpecularShininessTexture) ? undefined : decodeOptions;
var emissiveTextureOptions; var emissiveTextureOptions;
var normalTextureOptions; var normalTextureOptions;
@ -68,12 +67,12 @@ function loadMtl(mtlPath, options) {
material = new Material(); material = new Material();
material.name = name; material.name = name;
material.specularShininess = options.metallicRoughness ? 1.0 : 0.0; material.specularShininess = options.metallicRoughness ? 1.0 : 0.0;
loadMaterialTexture(material, 'specularTexture', overridingSpecularTexture, undefined, mtlDirectory, texturePromiseMap, texturePromises, options); material.specularTexture = overridingSpecularTexture;
loadMaterialTexture(material, 'specularShininessTexture', overridingSpecularShininessTexture, undefined, mtlDirectory, texturePromiseMap, texturePromises, options); material.specularShininessTexture = overridingSpecularShininessTexture;
loadMaterialTexture(material, 'ambientTexture', overridingAmbientTexture, undefined, mtlDirectory, texturePromiseMap, texturePromises, options); material.diffuseTexture = overridingDiffuseTexture;
loadMaterialTexture(material, 'normalTexture', overridingNormalTexture, undefined, mtlDirectory, texturePromiseMap, texturePromises, options); material.ambientTexture = overridingAmbientTexture;
loadMaterialTexture(material, 'diffuseTexture', overridingDiffuseTexture, diffuseTextureOptions, mtlDirectory, texturePromiseMap, texturePromises, options); material.normalTexture = overridingNormalTexture;
loadMaterialTexture(material, 'emissiveTexture', overridingEmissiveTexture, undefined, mtlDirectory, texturePromiseMap, texturePromises, options); material.emissiveTexture = overridingEmissiveTexture;
materials.push(material); materials.push(material);
} }
@ -140,39 +139,46 @@ function loadMtl(mtlPath, options) {
material.alpha = correctAlpha(1.0 - parseFloat(value)); material.alpha = correctAlpha(1.0 - parseFloat(value));
} else if (/^map_Ka /i.test(line)) { } else if (/^map_Ka /i.test(line)) {
if (!defined(overridingAmbientTexture)) { if (!defined(overridingAmbientTexture)) {
texturePath = path.resolve(mtlDirectory, cleanTextureName(line.substring(7).trim())); material.ambientTexture = path.resolve(mtlDirectory, cleanTextureName(line.substring(7).trim()));
loadMaterialTexture(material, 'ambientTexture', texturePath, ambientTextureOptions, mtlDirectory, texturePromiseMap, texturePromises, options);
} }
} else if (/^map_Ke /i.test(line)) { } else if (/^map_Ke /i.test(line)) {
if (!defined(overridingEmissiveTexture)) { if (!defined(overridingEmissiveTexture)) {
texturePath = path.resolve(mtlDirectory, cleanTextureName(line.substring(7).trim())); material.emissiveTexture = path.resolve(mtlDirectory, cleanTextureName(line.substring(7).trim()));
loadMaterialTexture(material, 'emissiveTexture', texturePath, emissiveTextureOptions, mtlDirectory, texturePromiseMap, texturePromises, options);
} }
} else if (/^map_Kd /i.test(line)) { } else if (/^map_Kd /i.test(line)) {
if (!defined(overridingDiffuseTexture)) { if (!defined(overridingDiffuseTexture)) {
texturePath = path.resolve(mtlDirectory, cleanTextureName(line.substring(7).trim())); material.diffuseTexture = path.resolve(mtlDirectory, cleanTextureName(line.substring(7).trim()));
loadMaterialTexture(material, 'diffuseTexture', texturePath, diffuseTextureOptions, mtlDirectory, texturePromiseMap, texturePromises, options);
} }
} else if (/^map_Ks /i.test(line)) { } else if (/^map_Ks /i.test(line)) {
if (!defined(overridingSpecularTexture)) { if (!defined(overridingSpecularTexture)) {
texturePath = path.resolve(mtlDirectory, cleanTextureName(line.substring(7).trim())); material.specularTexture = path.resolve(mtlDirectory, cleanTextureName(line.substring(7).trim()));
loadMaterialTexture(material, 'specularTexture', texturePath, specularTextureOptions, mtlDirectory, texturePromiseMap, texturePromises, options);
} }
} else if (/^map_Ns /i.test(line)) { } else if (/^map_Ns /i.test(line)) {
if (!defined(overridingSpecularShininessTexture)) { if (!defined(overridingSpecularShininessTexture)) {
texturePath = path.resolve(mtlDirectory, cleanTextureName(line.substring(7).trim())); material.specularShininessTexture = path.resolve(mtlDirectory, cleanTextureName(line.substring(7).trim()));
loadMaterialTexture(material, 'specularShininessTexture', texturePath, specularShinessTextureOptions, mtlDirectory, texturePromiseMap, texturePromises, options);
} }
} else if (/^map_Bump /i.test(line)) { } else if (/^map_Bump /i.test(line)) {
if (!defined(overridingNormalTexture)) { if (!defined(overridingNormalTexture)) {
texturePath = path.resolve(mtlDirectory, cleanTextureName(line.substring(9).trim())); material.normalTexture = path.resolve(mtlDirectory, cleanTextureName(line.substring(9).trim()));
loadMaterialTexture(material, 'normalTexture', texturePath, normalTextureOptions, mtlDirectory, texturePromiseMap, texturePromises, options);
} }
} }
} }
function loadMaterialTextures(material) {
loadMaterialTexture(material, 'diffuseTexture', diffuseTextureOptions, mtlDirectory, texturePromiseMap, texturePromises, options);
loadMaterialTexture(material, 'ambientTexture', ambientTextureOptions, mtlDirectory, texturePromiseMap, texturePromises, options);
loadMaterialTexture(material, 'emissiveTexture', emissiveTextureOptions, mtlDirectory, texturePromiseMap, texturePromises, options);
loadMaterialTexture(material, 'specularTexture', specularTextureOptions, mtlDirectory, texturePromiseMap, texturePromises, options);
loadMaterialTexture(material, 'specularShininessTexture', specularShinessTextureOptions, mtlDirectory, texturePromiseMap, texturePromises, options);
loadMaterialTexture(material, 'normalTexture', normalTextureOptions, mtlDirectory, texturePromiseMap, texturePromises, options);
}
return readLines(mtlPath, parseLine) return readLines(mtlPath, parseLine)
.then(function() { .then(function() {
var length = materials.length;
for (var i = 0; i < length; ++i) {
loadMaterialTextures(materials[i]);
}
return Promise.all(texturePromises); return Promise.all(texturePromises);
}) })
.then(function() { .then(function() {
@ -210,7 +216,8 @@ loadMtl._createMaterial = function(materialOptions, options) {
return convertMaterial(combine(materialOptions, new Material()), options); return convertMaterial(combine(materialOptions, new Material()), options);
}; };
function loadMaterialTexture(material, name, texturePath, textureOptions, mtlDirectory, texturePromiseMap, texturePromises, options) { function loadMaterialTexture(material, name, textureOptions, mtlDirectory, texturePromiseMap, texturePromises, options) {
var texturePath = material[name];
if (!defined(texturePath)) { if (!defined(texturePath)) {
return; return;
} }