From 3c5f0551826e3b86b2834c253bfe95246f711955 Mon Sep 17 00:00:00 2001 From: Sean Lilley Date: Thu, 21 Sep 2017 10:37:50 -0400 Subject: [PATCH 1/2] Constant lighting added back to materialsCommon --- lib/loadMtl.js | 12 +++++++++--- lib/loadObj.js | 3 +++ specs/lib/loadMtlSpec.js | 16 ++++++++++++++++ 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/lib/loadMtl.js b/lib/loadMtl.js index 4d53699..e5ba4ab 100644 --- a/lib/loadMtl.js +++ b/lib/loadMtl.js @@ -25,6 +25,7 @@ module.exports = loadMtl; * * @param {String} mtlPath Path to the .mtl file. * @param {Object} options The options object passed along from lib/obj2gltf.js + * @param {Boolean} options.hasNormals Whether the model has normals. * @returns {Promise} A promise resolving to an array of glTF materials with Texture objects stored in the texture slots. * * @private @@ -224,7 +225,7 @@ function convertMaterial(material, options) { } else if (options.metallicRoughness) { return createMetallicRoughnessMaterial(material, options); } else if (options.materialsCommon) { - return createMaterialsCommonMaterial(material); + return createMaterialsCommonMaterial(material, options); } // No material type specified, convert the material to metallic roughness @@ -609,7 +610,7 @@ function convertTraditionalToMetallicRoughness(material) { material.specularShininess = roughnessFactor; } -function createMaterialsCommonMaterial(material) { +function createMaterialsCommonMaterial(material, options) { var ambient = defaultValue(material.ambientTexture, material.ambientColor); var diffuse = defaultValue(material.diffuseTexture, material.diffuseColor); var emission = defaultValue(material.emissiveTexture, material.emissiveColor); @@ -637,9 +638,14 @@ function createMaterialsCommonMaterial(material) { } 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 : { diff --git a/lib/loadObj.js b/lib/loadObj.js index 2e809bf..6aa504f 100644 --- a/lib/loadObj.js +++ b/lib/loadObj.js @@ -482,6 +482,9 @@ function loadObj(objPath, options) { // Parse the obj file return readLines(objPath, parseLine) .then(function() { + // Add hasNormals to options object for loadMtl + options.hasNormals = normals.length > 0; + // Unload resources positions = undefined; normals = undefined; diff --git a/specs/lib/loadMtlSpec.js b/specs/lib/loadMtlSpec.js index fa766d6..0a54bf5 100644 --- a/specs/lib/loadMtlSpec.js +++ b/specs/lib/loadMtlSpec.js @@ -84,6 +84,7 @@ describe('loadMtl', function() { options = clone(obj2gltf.defaults); options.overridingTextures = {}; options.logger = function() {}; + options.hasNormals = true; }); it('loads mtl', function(done) { @@ -460,6 +461,21 @@ describe('loadMtl', function() { expect(values.shininess).toEqual(0.1); }); + it('sets CONSTANT technique when there are no normals', function() { + options.materialsCommon = true; + options.hasNormals = false; + + var material = loadMtl._createMaterial({ + diffuseColor : [1.0, 1.0, 1.0, 1.0] + }, options); + + var extension = material.extensions.KHR_materials_common; + var values = extension.values; + + expect(extension.technique).toBe('CONSTANT'); + expect(values.emission).toEqual(values.diffuse); + }); + it('ambient of [1, 1, 1] is treated as [0, 0, 0]', function() { options.materialsCommon = true; From 723a059e3e0e3b333ecca52088114ec200e5038a Mon Sep 17 00:00:00 2001 From: Sean Lilley Date: Thu, 21 Sep 2017 10:45:56 -0400 Subject: [PATCH 2/2] Update CHANGES.md --- CHANGES.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGES.md b/CHANGES.md index 65456fe..7f43c03 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -3,6 +3,7 @@ Change Log ### 2.1.0 ??? +* Added back support for the `CONSTANT` technique when a model uses the `KHR_materials_common` extension and has no normals. [#108](https://github.com/AnalyticalGraphicsInc/obj2gltf/pull/108) * Improved handling of materials with alpha. If the alpha value is 0.0 it is now treated as 1.0. [#107](https://github.com/AnalyticalGraphicsInc/obj2gltf/pull/107) ### 2.0.0 2017-08-11