From fba3cdf4e99d255efad6dd303edcb747d69a0a17 Mon Sep 17 00:00:00 2001 From: Chetan Date: Mon, 2 Oct 2023 16:45:36 +0800 Subject: [PATCH 1/4] Add new optional command line parameter --doubleSidedMaterial to force materials to be rendered on both sides --- README.md | 1 + bin/obj2gltf.js | 6 ++++++ lib/loadMtl.js | 14 ++++++++++++-- lib/obj2gltf.js | 11 +++++++++++ 4 files changed, 30 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 3403b4d..574c61a 100644 --- a/README.md +++ b/README.md @@ -114,6 +114,7 @@ As a convenience the PBR textures may be supplied directly to the command line. | `--input-up-axis` | Up axis of the obj. | No | | `--output-up-axis` | Up axis of the converted glTF. | No | | `--triangle-winding-order-sanitization` | Apply triangle winding order sanitization. | No | +| `--doubleSidedMaterial` | Allow the material properties to be double-sided | No, default `false` | ## Build Instructions diff --git a/bin/obj2gltf.js b/bin/obj2gltf.js index e1d1abe..6e3da08 100755 --- a/bin/obj2gltf.js +++ b/bin/obj2gltf.js @@ -160,6 +160,11 @@ const argv = yargs type: "boolean", default: defaults.triangleWindingOrderSanitization, }, + doubleSidedMaterial: { + describe: "Allow the material properties to be double-sided", + type: "boolean", + default: defaults.doubleSidedMaterial, + }, }) .parse(args); @@ -216,6 +221,7 @@ const options = { inputUpAxis: argv.inputUpAxis, outputUpAxis: argv.outputUpAxis, triangleWindingOrderSanitization: argv.triangleWindingOrderSanitization, + doubleSidedMaterial: argv.doubleSidedMaterial, }; console.time("Total"); diff --git a/lib/loadMtl.js b/lib/loadMtl.js index da80de7..a9953bf 100644 --- a/lib/loadMtl.js +++ b/lib/loadMtl.js @@ -846,7 +846,12 @@ function createSpecularGlossinessMaterial(material, options) { } else { const alpha = material.alpha; diffuseFactor[3] = alpha; - transparent = alpha < 1.0; + + if (options.doubleSidedMaterial) { + transparent = true; + } else { + transparent = alpha < 1.0; + } } if (defined(diffuseTexture)) { @@ -927,7 +932,12 @@ function createMetallicRoughnessMaterial(material, options) { } else { const alpha = material.alpha; baseColorFactor[3] = alpha; - transparent = alpha < 1.0; + + if (options.doubleSidedMaterial) { + transparent = true; + } else { + transparent = alpha < 1.0; + } } if (defined(baseColorTexture)) { diff --git a/lib/obj2gltf.js b/lib/obj2gltf.js index 0916df9..811ea71 100644 --- a/lib/obj2gltf.js +++ b/lib/obj2gltf.js @@ -40,6 +40,7 @@ module.exports = obj2gltf; * @param {Logger} [options.logger] A callback function for handling logged messages. Defaults to console.log. * @param {Writer} [options.writer] A callback function that writes files that are saved as separate resources. * @param {String} [options.outputDirectory] Output directory for writing separate resources when options.writer is not defined. + * @param {Boolean} [options.doubleSidedMaterial=false] Allows materials to be double sided. * @return {Promise} A promise that resolves to the glTF JSON or glb buffer. */ function obj2gltf(objPath, options) { @@ -54,6 +55,10 @@ function obj2gltf(objPath, options) { options.checkTransparency, defaults.checkTransparency ); + options.doubleSidedMaterial = defaultValue( + options.doubleSidedMaterial, + defaults.doubleSidedMaterial + ); options.secure = defaultValue(options.secure, defaults.secure); options.packOcclusion = defaultValue( options.packOcclusion, @@ -179,6 +184,12 @@ obj2gltf.defaults = { * @default false */ checkTransparency: false, + /** + * Gets and sets whether a material will be doubleSided or not + * @type Boolean + * @default false + */ + doubleSidedMaterial: false, /** * Gets or sets whether the source model can reference paths outside of its directory. * @type Boolean From 44e76de7be8d1e0503ffce2c868c4d4b7bedd162 Mon Sep 17 00:00:00 2001 From: Sean Lilley Date: Tue, 3 Oct 2023 09:27:12 -0400 Subject: [PATCH 2/4] Deconflate transparent and doubleSideMaterial --- lib/loadMtl.js | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/lib/loadMtl.js b/lib/loadMtl.js index a9953bf..3eba5aa 100644 --- a/lib/loadMtl.js +++ b/lib/loadMtl.js @@ -846,19 +846,14 @@ function createSpecularGlossinessMaterial(material, options) { } else { const alpha = material.alpha; diffuseFactor[3] = alpha; - - if (options.doubleSidedMaterial) { - transparent = true; - } else { - transparent = alpha < 1.0; - } + transparent = alpha < 1.0; } if (defined(diffuseTexture)) { transparent = transparent || diffuseTexture.transparent; } - const doubleSided = transparent; + const doubleSided = transparent || options.doubleSidedMaterial; const alphaMode = transparent ? "BLEND" : "OPAQUE"; return { @@ -932,19 +927,14 @@ function createMetallicRoughnessMaterial(material, options) { } else { const alpha = material.alpha; baseColorFactor[3] = alpha; - - if (options.doubleSidedMaterial) { - transparent = true; - } else { - transparent = alpha < 1.0; - } + transparent = alpha < 1.0; } if (defined(baseColorTexture)) { transparent = transparent || baseColorTexture.transparent; } - const doubleSided = transparent; + const doubleSided = transparent || options.doubleSidedMaterial; const alphaMode = transparent ? "BLEND" : "OPAQUE"; return { From 11263509ee886d5a0e3983dd042476b4f36cf3af Mon Sep 17 00:00:00 2001 From: Sean Lilley Date: Tue, 3 Oct 2023 09:27:22 -0400 Subject: [PATCH 3/4] Add tests --- specs/lib/loadMtlSpec.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/specs/lib/loadMtlSpec.js b/specs/lib/loadMtlSpec.js index 4e845d8..9fc0290 100644 --- a/specs/lib/loadMtlSpec.js +++ b/specs/lib/loadMtlSpec.js @@ -406,6 +406,14 @@ describe("loadMtl", () => { expect(material.alphaMode).toBe("BLEND"); expect(material.doubleSided).toBe(true); }); + + it("uses doubleSidedMaterial option", () => { + options.metallicRoughness = true; + options.doubleSidedMaterial = true; + + const material = loadMtl._createMaterial(undefined, options); + expect(material.doubleSided).toBe(true); + }); }); describe("specularGlossiness", () => { @@ -530,5 +538,13 @@ describe("loadMtl", () => { expect(material.alphaMode).toBe("BLEND"); expect(material.doubleSided).toBe(true); }); + + it("uses doubleSidedMaterial option", () => { + options.specularGlossiness = true; + options.doubleSidedMaterial = true; + + const material = loadMtl._createMaterial(undefined, options); + expect(material.doubleSided).toBe(true); + }); }); }); From 0d4facf48a1032b6a553b0fa2bc8bc434997a4d7 Mon Sep 17 00:00:00 2001 From: Sean Lilley Date: Tue, 3 Oct 2023 09:27:28 -0400 Subject: [PATCH 4/4] Update CHANGES.md --- CHANGES.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index cc8a968..7d3862f 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,5 +1,9 @@ # Change Log +### 3.2.0 - 2023-??-?? + +- Added `doubleSidedMaterial` option to force materials to be rendered double sided. [#294](https://github.com/CesiumGS/obj2gltf/pull/294) + ### 3.1.6 - 2023-02-10 - Update npm module dependencies.