From b913b0fa5ff8584d6c94433542d3f8a1fd3d17a8 Mon Sep 17 00:00:00 2001 From: Tom Fili Date: Fri, 19 Jul 2019 13:07:34 -0400 Subject: [PATCH 1/4] Added a check to see if diffuse and alpha texture is the same. --- lib/loadMtl.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/loadMtl.js b/lib/loadMtl.js index dc42545..1e55990 100644 --- a/lib/loadMtl.js +++ b/lib/loadMtl.js @@ -387,6 +387,10 @@ function createDiffuseAlphaTexture(diffuseTexture, alphaTexture, options) { return diffuseTexture; } + if (diffuseTexture.path === alphaTexture.path) { + return diffuseTexture; + } + if (!defined(diffuseTexture.pixels) || !defined(alphaTexture.pixels)) { options.logger('Could not get decoded texture data for ' + diffuseTexture.path + ' or ' + alphaTexture.path + '. The material will be created without an alpha texture.'); return diffuseTexture; From b0de7d1bc1373855b2b7197588d427b1e89f8383 Mon Sep 17 00:00:00 2001 From: Tom Fili Date: Fri, 19 Jul 2019 13:15:38 -0400 Subject: [PATCH 2/4] Added test --- specs/lib/loadMtlSpec.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/specs/lib/loadMtlSpec.js b/specs/lib/loadMtlSpec.js index 2af7f0a..72cedcd 100644 --- a/specs/lib/loadMtlSpec.js +++ b/specs/lib/loadMtlSpec.js @@ -321,6 +321,18 @@ describe('loadMtl', () => { expect(material.alphaMode).toBe('BLEND'); expect(material.doubleSided).toBe(true); }); + + it('uses diffuse texture if diffuse and alpha are the same', () => { + options.metallicRoughness = true; + + const material = loadMtl._createMaterial({ + diffuseTexture : diffuseTexture, + alphaTexture : diffuseTexture + }, options); + + const pbr = material.pbrMetallicRoughness; + expect(pbr.baseColorTexture).toBe(diffuseTexture); + }); }); describe('specularGlossiness', () => { From 0fa1ec528d8e87fe7da4a2578c317699d1a8b37d Mon Sep 17 00:00:00 2001 From: Tom Fili Date: Fri, 19 Jul 2019 14:52:12 -0400 Subject: [PATCH 3/4] Made alpha images work if same as diffuse --- lib/loadMtl.js | 13 +++++++------ specs/lib/loadMtlSpec.js | 26 +++++++++++++++++++++++--- 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/lib/loadMtl.js b/lib/loadMtl.js index 1e55990..d8ea67a 100644 --- a/lib/loadMtl.js +++ b/lib/loadMtl.js @@ -388,6 +388,7 @@ function createDiffuseAlphaTexture(diffuseTexture, alphaTexture, options) { } if (diffuseTexture.path === alphaTexture.path) { + diffuseTexture.transparent = true; return diffuseTexture; } @@ -590,7 +591,7 @@ function createSpecularGlossinessMaterial(material, options) { emissiveFactor = [1.0, 1.0, 1.0]; } - if (defined(diffuseTexture)) { + if (defined(diffuseAlphaTexture)) { diffuseFactor = [1.0, 1.0, 1.0, 1.0]; } @@ -611,8 +612,8 @@ function createSpecularGlossinessMaterial(material, options) { transparent = alpha < 1.0; } - if (defined(diffuseTexture)) { - transparent = transparent || diffuseTexture.transparent; + if (defined(diffuseAlphaTexture)) { + transparent = transparent || diffuseAlphaTexture.transparent; } const doubleSided = transparent; @@ -662,7 +663,7 @@ function createMetallicRoughnessMaterial(material, options) { emissiveFactor = [1.0, 1.0, 1.0]; } - if (defined(baseColorTexture)) { + if (defined(diffuseAlphaTexture)) { baseColorFactor = [1.0, 1.0, 1.0, 1.0]; } @@ -683,8 +684,8 @@ function createMetallicRoughnessMaterial(material, options) { transparent = alpha < 1.0; } - if (defined(baseColorTexture)) { - transparent = transparent || baseColorTexture.transparent; + if (defined(diffuseAlphaTexture)) { + transparent = transparent || diffuseAlphaTexture.transparent; } const doubleSided = transparent; diff --git a/specs/lib/loadMtlSpec.js b/specs/lib/loadMtlSpec.js index 72cedcd..6d7eb7e 100644 --- a/specs/lib/loadMtlSpec.js +++ b/specs/lib/loadMtlSpec.js @@ -322,16 +322,20 @@ describe('loadMtl', () => { expect(material.doubleSided).toBe(true); }); - it('uses diffuse texture if diffuse and alpha are the same', () => { + it('uses diffuse texture if diffuse and alpha are the same', async () => { options.metallicRoughness = true; + // The transparent property will be modified so make a copy + const diffuseTextureCopy = await loadTexture(diffuseTexturePath, decodeOptions); const material = loadMtl._createMaterial({ - diffuseTexture : diffuseTexture, + diffuseTexture : diffuseTextureCopy, alphaTexture : diffuseTexture }, options); const pbr = material.pbrMetallicRoughness; - expect(pbr.baseColorTexture).toBe(diffuseTexture); + expect(pbr.baseColorTexture).toBe(diffuseTextureCopy); + expect(material.alphaMode).toBe('BLEND'); + expect(material.doubleSided).toBe(true); }); }); @@ -428,5 +432,21 @@ describe('loadMtl', () => { expect(material.alphaMode).toBe('BLEND'); expect(material.doubleSided).toBe(true); }); + + it('uses diffuse texture if diffuse and alpha are the same', async () => { + options.specularGlossiness = true; + + // The transparent property will be modified so make a copy + const diffuseTextureCopy = await loadTexture(diffuseTexturePath, decodeOptions); + const material = loadMtl._createMaterial({ + diffuseTexture : diffuseTextureCopy, + alphaTexture : diffuseTexture + }, options); + + const pbr = material.extensions.KHR_materials_pbrSpecularGlossiness; + expect(pbr.diffuseTexture).toEqual(diffuseTextureCopy); + expect(material.alphaMode).toBe('BLEND'); + expect(material.doubleSided).toBe(true); + }); }); }); From 3ba174dd3347026a9b4cc4febcd22c1b256f83f9 Mon Sep 17 00:00:00 2001 From: Tom Fili Date: Fri, 19 Jul 2019 21:03:33 -0400 Subject: [PATCH 4/4] Removed unneeded code --- lib/loadMtl.js | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/loadMtl.js b/lib/loadMtl.js index d8ea67a..17a31e7 100644 --- a/lib/loadMtl.js +++ b/lib/loadMtl.js @@ -388,7 +388,6 @@ function createDiffuseAlphaTexture(diffuseTexture, alphaTexture, options) { } if (diffuseTexture.path === alphaTexture.path) { - diffuseTexture.transparent = true; return diffuseTexture; }