diff --git a/addons/io_scene_gltf2/blender/exp/material/extensions/gltf2_blender_gather_materials_specular.py b/addons/io_scene_gltf2/blender/exp/material/extensions/gltf2_blender_gather_materials_specular.py index 2a78884bf..6a0f032cc 100644 --- a/addons/io_scene_gltf2/blender/exp/material/extensions/gltf2_blender_gather_materials_specular.py +++ b/addons/io_scene_gltf2/blender/exp/material/extensions/gltf2_blender_gather_materials_specular.py @@ -19,6 +19,7 @@ def export_specular(blender_material, export_settings): specular_extension = {} + extensions_needed = False specular_socket = gltf2_blender_get.get_socket(blender_material, 'Specular IOR Level') speculartint_socket = gltf2_blender_get.get_socket(blender_material, 'Specular Tint') @@ -33,18 +34,27 @@ def export_specular(blender_material, export_settings): if specular_non_linked is True: fac = specular_socket.default_value - if fac != 1.0: + fac = fac * 2.0 + if fac < 1.0: specular_extension['specularFactor'] = fac - if fac == 0.0: - return None, {} + extensions_needed = True + elif fac > 1.0: + # glTF specularFactor should be <= 1.0, so we will multiply ColorFactory by specularFactor, and set SpecularFactor to 1.0 (default value) + extensions_needed = True + else: + pass # If fac == 1.0, no need to export specularFactor, the default value is 1.0 + else: # Factor fac = gltf2_blender_get.get_factor_from_socket(specular_socket, kind='VALUE') if fac is not None and fac != 1.0: - specular_extension['specularFactor'] = fac - - if fac == 0.0: - return None, {} + fac = fac * 2.0 if fac is not None else None + if fac is not None and fac < 1.0: + specular_extension['specularFactor'] = fac + extensions_needed = True + elif fac is not None and fac > 1.0: + # glTF specularFactor should be <= 1.0, so we will multiply ColorFactory by specularFactor, and set SpecularFactor to 1.0 (default value) + extensions_needed = True # Texture if gltf2_blender_get.has_image_node_from_socket(specular_socket): @@ -56,16 +66,26 @@ def export_specular(blender_material, export_settings): ) specular_extension['specularTexture'] = original_specular_texture uvmap_infos.update({'specularTexture': uvmap_info}) + extensions_needed = True if specularcolor_non_linked is True: color = speculartint_socket.default_value[:3] + if fac is not None and fac > 1.0: + color = (color[0] * fac, color[1] * fac, color[2] * fac) + specular_extension['specularColorFactor'] = color if color != (1.0, 1.0, 1.0) else None if color != (1.0, 1.0, 1.0): - specular_extension['specularColorFactor'] = color + extensions_needed = True + else: # Factor - fac = gltf2_blender_get.get_factor_from_socket(speculartint_socket, kind='RGB') - if fac is not None and fac != (1.0, 1.0, 1.0): - specular_extension['specularColorFactor'] = fac + fac_color = gltf2_blender_get.get_factor_from_socket(speculartint_socket, kind='RGB') + if fac_color is not None and fac is not None and fac > 1.0: + fac_color = (fac_color[0] * fac, fac_color[1] * fac, fac_color[2] * fac) + elif fac_color is None and fac is not None and fac > 1.0: + fac_color = (fac, fac, fac) + specular_extension['specularColorFactor'] = fac_color if fac_color != (1.0, 1.0, 1.0) else None + if fac_color != (1.0, 1.0, 1.0): + extensions_needed = True # Texture if gltf2_blender_get.has_image_node_from_socket(speculartint_socket): @@ -77,5 +97,9 @@ def export_specular(blender_material, export_settings): ) specular_extension['specularColorTexture'] = original_specularcolor_texture uvmap_infos.update({'specularColorTexture': uvmap_info}) + extensions_needed = True + + if extensions_needed is False: + return None, {} return Extension('KHR_materials_specular', specular_extension, False), uvmap_infos diff --git a/addons/io_scene_gltf2/blender/imp/gltf2_blender_KHR_materials_specular.py b/addons/io_scene_gltf2/blender/imp/gltf2_blender_KHR_materials_specular.py index 9cecff594..53ccfe6c5 100644 --- a/addons/io_scene_gltf2/blender/imp/gltf2_blender_KHR_materials_specular.py +++ b/addons/io_scene_gltf2/blender/imp/gltf2_blender_KHR_materials_specular.py @@ -49,7 +49,7 @@ def specular(mh, location_specular, x_specularcolor, y_specularcolor = location_specular_tint if tex_specular_info is None: - specular_socket.default_value = specular_factor + specular_socket.default_value = specular_factor / 2.0 else: # Mix specular factor if specular_factor != 1.0: @@ -61,7 +61,7 @@ def specular(mh, location_specular, mh.node_tree.links.new(specular_socket, node.outputs[0]) # Inputs specular_socket = node.inputs[0] - node.inputs[1].default_value = specular_factor + node.inputs[1].default_value = specular_factor / 2.0 x_specular -= 200 texture( diff --git a/addons/io_scene_gltf2/blender/imp/gltf2_blender_pbrMetallicRoughness.py b/addons/io_scene_gltf2/blender/imp/gltf2_blender_pbrMetallicRoughness.py index 967b9ea3b..d9d637d7d 100644 --- a/addons/io_scene_gltf2/blender/imp/gltf2_blender_pbrMetallicRoughness.py +++ b/addons/io_scene_gltf2/blender/imp/gltf2_blender_pbrMetallicRoughness.py @@ -60,9 +60,6 @@ def pbr_metallic_roughness(mh: MaterialHelper): # This value may be overridden later if IOR extension is set on file pbr_node.inputs['IOR'].default_value = GLTF_IOR - pbr_node.inputs['Specular IOR Level'].default_value = 0.0 # Will be overridden by KHR_materials_specular if set - pbr_node.inputs['Specular Tint'].default_value = [0.0]*3 + [1.0] # Will be overridden by KHR_materials_specular if set - if mh.pymat.occlusion_texture is not None: if mh.settings_node is None: mh.settings_node = make_settings_node(mh) diff --git a/tests/scenes/17_ior.blend b/tests/scenes/17_ior.blend index 33de992b3..1ab8d4a2c 100644 Binary files a/tests/scenes/17_ior.blend and b/tests/scenes/17_ior.blend differ diff --git a/tests/scenes/20_specular.blend b/tests/scenes/20_specular.blend index bcdcc18f5..2c62da5bb 100644 Binary files a/tests/scenes/20_specular.blend and b/tests/scenes/20_specular.blend differ diff --git a/tests/test/test.js b/tests/test/test.js index bdcffbdd0..bf5e198fc 100644 --- a/tests/test/test.js +++ b/tests/test/test.js @@ -802,7 +802,7 @@ describe('Exporter', function() { const mat_transmission = asset.materials.find(mat => mat.name === 'Transmission'); assert.ok('KHR_materials_transmission' in mat_transmission.extensions); - assert.ok(!('KHR_materials_transmission' in mat_no_transmission.extensions)); + assert.strictEqual(mat_no_transmission.extensions, undefined); const transmission = mat_transmission.extensions.KHR_materials_transmission; assert.equalEpsilon(transmission.transmissionFactor, 0.2); @@ -914,32 +914,30 @@ describe('Exporter', function() { assert.ok(!("specularTexture" in mat_NoTextSpec.extensions['KHR_materials_specular'])); assert.ok(!("specularColorTexture" in mat_NoTextSpec.extensions['KHR_materials_specular'])); - assert.equalEpsilonArray(mat_NoTextSpec.extensions['KHR_materials_specular']["specularColorFactor"], [0.9, 0.8, 0.7]); - assert.equalEpsilon(mat_NoTextSpec.extensions['KHR_materials_specular']["specularFactor"], [0.7]); + assert.equalEpsilonArray(mat_NoTextSpec.extensions['KHR_materials_specular']["specularColorFactor"], [1.26, 1.12, 0.98]); + assert.ok(!("specularFactor" in mat_NoTextSpec.extensions['KHR_materials_specular'])); assert.ok(!("specularTexture" in mat_NoTextTint.extensions['KHR_materials_specular'])); assert.ok(!("specularColorTexture" in mat_NoTextTint.extensions['KHR_materials_specular'])); assert.equalEpsilonArray(mat_NoTextTint.extensions['KHR_materials_specular']["specularColorFactor"], [0.96, 0.96, 0.96]); - assert.equalEpsilon(mat_NoTextTint.extensions['KHR_materials_specular']["specularFactor"], [0.5]); + assert.ok(!("specularFactor" in mat_NoTextTint.extensions['KHR_materials_specular'])); assert.ok(!("specularTexture" in mat_NoTextAll.extensions['KHR_materials_specular'])); assert.ok(!("specularColorTexture" in mat_NoTextAll.extensions['KHR_materials_specular'])); - assert.equalEpsilonArray(mat_NoTextAll.extensions['KHR_materials_specular']["specularColorFactor"], [0.82, 0.94, 0.91]); - assert.equalEpsilon(mat_NoTextAll.extensions['KHR_materials_specular']["specularFactor"], [0.8]); + assert.equalEpsilonArray(mat_NoTextAll.extensions['KHR_materials_specular']["specularColorFactor"], [1.312, 1.504, 1.456]); + assert.ok(!("specularFactor" in mat_NoTextAll.extensions['KHR_materials_specular'])); assert.ok(!("specularTexture" in mat_NoTextAllIOR.extensions['KHR_materials_specular'])); assert.ok(!("specularColorTexture" in mat_NoTextAllIOR.extensions['KHR_materials_specular'])); - assert.equalEpsilonArray(mat_NoTextAllIOR.extensions['KHR_materials_specular']["specularColorFactor"], [0.82, 0.94, 0.912]); - assert.equalEpsilon(mat_NoTextAllIOR.extensions['KHR_materials_specular']["specularFactor"], [0.8]); + assert.equalEpsilonArray(mat_NoTextAllIOR.extensions['KHR_materials_specular']["specularColorFactor"], [1.312, 1.504, 1.4592]); + assert.ok(!("specularFactor" in mat_NoTextAllIOR.extensions['KHR_materials_specular'])); assert.ok(!("specularTexture" in mat_BaseColorTex_Factor.extensions['KHR_materials_specular'])); assert.ok(!("specularColorTexture" in mat_BaseColorTex_Factor.extensions['KHR_materials_specular'])); - assert.equalEpsilonArray(mat_BaseColorTex_Factor.extensions['KHR_materials_specular']["specularColorFactor"], [0.82, 0.94, 0.91]); - assert.equalEpsilon(mat_BaseColorTex_Factor.extensions['KHR_materials_specular']["specularFactor"], [0.8]); + assert.equalEpsilonArray(mat_BaseColorTex_Factor.extensions['KHR_materials_specular']["specularColorFactor"], [1.312, 1.504, 1.456]); + assert.ok(!("specularFactor" in mat_BaseColorTex_Factor.extensions['KHR_materials_specular'])); - assert.ok(!("specularTexture" in mat_BaseColorText_NoFactor.extensions['KHR_materials_specular'])); - assert.ok(!("specularColorTexture" in mat_BaseColorText_NoFactor.extensions['KHR_materials_specular'])); - assert.ok(!("specularColorFactor" in mat_BaseColorText_NoFactor.extensions['KHR_materials_specular'])); + assert.ok(!("extensions" in mat_BaseColorText_NoFactor)); }); @@ -1206,7 +1204,7 @@ describe('Exporter', function() { assert.equalEpsilon(pbr.metallicFactor, 0.2); assert.equalEpsilon(pbr.roughnessFactor, 0.3); assert.equalEpsilon(mat.extensions['KHR_materials_volume']["thicknessFactor"], 0.9); - assert.equalEpsilon(mat.extensions['KHR_materials_specular']["specularFactor"], 0.25); + assert.equalEpsilon(mat.extensions['KHR_materials_specular']["specularFactor"], 0.5); assert.equalEpsilonArray(mat.extensions['KHR_materials_specular']["specularColorFactor"], [0.7, 0.6, 0.5]); }); @@ -2937,18 +2935,7 @@ describe('Importer / Exporter (Roundtrip)', function() { const mat_SpecColorTex = asset.materials.find(mat => mat.name === "SpecColorTex"); const mat_SpecColorTexFac = asset.materials.find(mat => mat.name === "SpecColorTexFac"); - if ('specularFactor' in mat_SpecDefault.extensions['KHR_materials_specular']) { - assert.equalEpsilon(mat_SpecDefault.extensions['KHR_materials_specular']['specularFactor'], 1.0); - } else { - assert.ok(!("specularFactor" in mat_SpecDefault.extensions['KHR_materials_specular'])); - } - assert.ok(!("specularTexture" in mat_SpecDefault.extensions['KHR_materials_specular'])); - if ('specularColorFactor' in mat_SpecDefault.extensions['KHR_materials_specular']) { - assert.equalEpsilonArray(mat_SpecDefault.extensions['KHR_materials_specular']['specularColorFactor'], [1.0, 1.0, 1.0]); - } else { - assert.ok(!("specularColorFactor" in mat_SpecDefault.extensions['KHR_materials_specular'])); - } - assert.ok(!("specularColorTexture" in mat_SpecDefault.extensions['KHR_materials_specular'])); + assert.strictEqual(mat_SpecDefault.extensions, undefined); if ('specularColorFactor' in mat_Factor.extensions['KHR_materials_specular']) { assert.equalEpsilonArray(mat_Factor.extensions['KHR_materials_specular']['specularColorFactor'], [1.0, 1.0, 1.0]);