Skip to content

Commit

Permalink
Merge pull request #108 from AnalyticalGraphicsInc/constant-lighting
Browse files Browse the repository at this point in the history
Constant lighting added back to materialsCommon
  • Loading branch information
shehzan10 committed Sep 21, 2017
2 parents 3934450 + 723a059 commit 8f3f0d3
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 9 additions & 3 deletions lib/loadMtl.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 : {
Expand Down
3 changes: 3 additions & 0 deletions lib/loadObj.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
16 changes: 16 additions & 0 deletions specs/lib/loadMtlSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ describe('loadMtl', function() {
options = clone(obj2gltf.defaults);
options.overridingTextures = {};
options.logger = function() {};
options.hasNormals = true;
});

it('loads mtl', function(done) {
Expand Down Expand Up @@ -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;

Expand Down

0 comments on commit 8f3f0d3

Please sign in to comment.