Replaced materialsCommon with unlit

This commit is contained in:
Omar Shehata 2018-08-30 15:55:32 -04:00
parent 26526c0f51
commit e8fc8ab04e
4 changed files with 31 additions and 89 deletions

View File

@ -85,11 +85,6 @@ var argv = yargs
type : 'boolean',
default : defaults.specularGlossiness
},
materialsCommon : {
describe : 'The glTF will be saved with the KHR_materials_common extension.',
type : 'boolean',
default : defaults.materialsCommon
},
metallicRoughnessOcclusionTexture : {
describe : 'Path to the metallic-roughness-occlusion texture that should override textures in the .mtl file, where occlusion is stored in the red channel, roughness is stored in the green channel, and metallic is stored in the blue channel. The model will be saved with a pbrMetallicRoughness material. This is often convenient in workflows where the .mtl does not exist or is not set up to use PBR materials. Intended for models with a single material',
type : 'string',
@ -122,11 +117,16 @@ var argv = yargs
},
alphaTexture : {
describe : 'Path to the alpha texture that should override textures in the .mtl file.'
}
},
unlit : {
describe : 'The glTF will be saved with the KHR_materials_unlit extension.',
type : 'boolean',
default : defaults.unlit
},
}).parse(args);
if (argv.metallicRoughness + argv.specularGlossiness + argv.materialsCommon > 1) {
console.error('Only one material type may be set from [--metallicRoughness, --specularGlossiness, --materialsCommon].');
if (argv.metallicRoughness + argv.specularGlossiness > 1) {
console.error('Only one material type may be set from [--metallicRoughness, --specularGlossiness].');
process.exit(1);
}
@ -165,7 +165,7 @@ var options = {
packOcclusion : argv.packOcclusion,
metallicRoughness : argv.metallicRoughness,
specularGlossiness : argv.specularGlossiness,
materialsCommon : argv.materialsCommon,
unlit : argv.unlit,
overridingTextures : overridingTextures,
outputDirectory : outputDirectory
};

View File

@ -97,9 +97,11 @@ function createGltf(objData, options) {
if (options.specularGlossiness) {
gltf.extensionsUsed.push('KHR_materials_pbrSpecularGlossiness');
gltf.extensionsRequired.push('KHR_materials_pbrSpecularGlossiness');
} else if (options.materialsCommon) {
gltf.extensionsUsed.push('KHR_materials_common');
gltf.extensionsRequired.push('KHR_materials_common');
}
if (options.unlit) {
gltf.extensionsUsed.push('KHR_materials_unlit');
gltf.extensionsRequired.push('KHR_materials_unlit');
}
return gltf;
@ -209,9 +211,15 @@ function resolveTextures(gltf, material) {
}
}
function addMaterial(gltf, material) {
function addMaterial(gltf, material, options) {
resolveTextures(gltf, material);
var materialIndex = gltf.materials.length;
if (options.unlit) {
if (!defined(material.extensions)) {
material.extensions = {};
}
material.extensions.KHR_materials_unlit = {};
}
gltf.materials.push(material);
return materialIndex;
}
@ -247,7 +255,7 @@ function getMaterial(gltf, materials, materialName, options) {
}
if (!defined(materialIndex)) {
materialIndex = addMaterial(gltf, material);
materialIndex = addMaterial(gltf, material, options);
}
return materialIndex;

View File

@ -50,7 +50,7 @@ function loadMtl(mtlPath, options) {
var overridingAlphaTexture = overridingTextures.alphaTexture;
// Textures that are packed into PBR textures need to be decoded first
var decodeOptions = options.materialsCommon ? undefined : {
var decodeOptions = {
decode : true
};
@ -280,10 +280,7 @@ function convertMaterial(material, options) {
return createSpecularGlossinessMaterial(material, options);
} else if (options.metallicRoughness) {
return createMetallicRoughnessMaterial(material, options);
} else if (options.materialsCommon) {
return createMaterialsCommonMaterial(material, options);
}
}
// No material type specified, convert the material to metallic roughness
convertTraditionalToMetallicRoughness(material);
return createMetallicRoughnessMaterial(material, options);
@ -740,65 +737,4 @@ function convertTraditionalToMetallicRoughness(material) {
material.specularColor = [metallicFactor, metallicFactor, metallicFactor, 1.0];
material.specularShininess = roughnessFactor;
}
function createMaterialsCommonMaterial(material, options) {
var diffuseAlphaTexture = createDiffuseAlphaTexture(material.diffuseTexture, material.alphaTexture, options);
var ambient = defaultValue(material.ambientTexture, material.ambientColor);
var diffuse = defaultValue(diffuseAlphaTexture, material.diffuseColor);
var emission = defaultValue(material.emissiveTexture, material.emissiveColor);
var specular = defaultValue(material.specularTexture, material.specularColor);
var alpha = material.alpha;
var shininess = material.specularShininess;
var hasSpecular = (shininess > 0.0) && (defined(material.specularTexture) || (specular[0] > 0.0 || specular[1] > 0.0 || specular[2] > 0.0));
var transparent;
var transparency = 1.0;
if (defined(material.alphaTexture)) {
transparent = true;
} else if (defined(material.diffuseTexture)) {
transparency = alpha;
transparent = material.diffuseTexture.transparent || (transparency < 1.0);
} else {
diffuse[3] = alpha;
transparent = alpha < 1.0;
}
if (!defined(material.ambientTexture)) {
// If ambient color is [1, 1, 1] assume it is a multiplier and instead change to [0, 0, 0]
if (ambient[0] === 1.0 && ambient[1] === 1.0 && ambient[2] === 1.0) {
ambient = [0.0, 0.0, 0.0, 1.0];
}
}
var doubleSided = transparent;
var technique = hasSpecular ? 'PHONG' : 'LAMBERT';
if (!options.hasNormals) {
// Constant technique only factors in ambient and emission sources - set emission to diffuse
emission = diffuse;
technique = 'CONSTANT';
}
return {
name : material.name,
extensions : {
KHR_materials_common : {
technique : technique,
transparent : transparent,
doubleSided : doubleSided,
values : {
ambient : ambient,
diffuse : diffuse,
emission : emission,
specular : specular,
shininess : shininess,
transparency : transparency,
transparent : transparent,
doubleSided : doubleSided
}
}
}
};
}
}

View File

@ -25,7 +25,7 @@ module.exports = obj2gltf;
* @param {Boolean} [options.packOcclusion=false] Pack the occlusion texture in the red channel of the metallic-roughness texture.
* @param {Boolean} [options.metallicRoughness=false] The values in the mtl file are already metallic-roughness PBR values and no conversion step should be applied. Metallic is stored in the Ks and map_Ks slots and roughness is stored in the Ns and map_Ns slots.
* @param {Boolean} [options.specularGlossiness=false] The values in the mtl file are already specular-glossiness PBR values and no conversion step should be applied. Specular is stored in the Ks and map_Ks slots and glossiness is stored in the Ns and map_Ns slots. The glTF will be saved with the KHR_materials_pbrSpecularGlossiness extension.
* @param {Boolean} [options.materialsCommon=false] The glTF will be saved with the KHR_materials_common extension.
* @param {Boolean} [options.unlit=false] The glTF will be saved with the KHR_materials_unlit extension.
* @param {Object} [options.overridingTextures] An object containing texture paths that override textures defined in the .mtl file. This is often convenient in workflows where the .mtl does not exist or is not set up to use PBR materials. Intended for models with a single material.
* @param {String} [options.overridingTextures.metallicRoughnessOcclusionTexture] Path to the metallic-roughness-occlusion texture, where occlusion is stored in the red channel, roughness is stored in the green channel, and metallic is stored in the blue channel. The model will be saved with a pbrMetallicRoughness material.
* @param {String} [options.overridingTextures.specularGlossinessTexture] Path to the specular-glossiness texture, where specular color is stored in the red, green, and blue channels and specular glossiness is stored in the alpha channel. The model will be saved with a material using the KHR_materials_pbrSpecularGlossiness extension.
@ -50,7 +50,7 @@ function obj2gltf(objPath, options) {
options.packOcclusion = defaultValue(options.packOcclusion, defaults.packOcclusion);
options.metallicRoughness = defaultValue(options.metallicRoughness, defaults.metallicRoughness);
options.specularGlossiness = defaultValue(options.specularGlossiness, defaults.specularGlossiness);
options.materialsCommon = defaultValue(options.materialsCommon, defaults.materialsCommon);
options.unlit = defaultValue(options.unlit, defaults.unlit);
options.overridingTextures = defaultValue(options.overridingTextures, defaultValue.EMPTY_OBJECT);
options.logger = defaultValue(options.logger, getDefaultLogger());
options.writer = defaultValue(options.writer, getDefaultWriter(options.outputDirectory));
@ -63,8 +63,8 @@ function obj2gltf(objPath, options) {
throw new DeveloperError('Either options.writer or options.outputDirectory must be defined when writing separate resources.');
}
if (options.metallicRoughness + options.specularGlossiness + options.materialsCommon > 1) {
throw new DeveloperError('Only one material type may be set from [metallicRoughness, specularGlossiness, materialsCommon].');
if (options.metallicRoughness + options.specularGlossiness > 1) {
throw new DeveloperError('Only one material type may be set from [metallicRoughness, specularGlossiness].');
}
if (defined(options.overridingTextures.metallicRoughnessOcclusionTexture) && defined(options.overridingTextures.specularGlossinessTexture)) {
@ -74,14 +74,12 @@ function obj2gltf(objPath, options) {
if (defined(options.overridingTextures.metallicRoughnessOcclusionTexture)) {
options.metallicRoughness = true;
options.specularGlossiness = false;
options.materialsCommon = false;
options.packOcclusion = true;
}
if (defined(options.overridingTextures.specularGlossinessTexture)) {
options.metallicRoughness = false;
options.specularGlossiness = true;
options.materialsCommon = false;
}
return loadObj(objPath, options)
@ -162,11 +160,11 @@ obj2gltf.defaults = {
*/
specularGlossiness : false,
/**
* Gets or sets whether the glTF will be saved with the KHR_materials_common extension.
* Gets or sets whether the glTF will be saved with the KHR_materials_unlit extension.
* @type Boolean
* @default false
*/
materialsCommon : false
unlit : false
};
/**