Skip to content

Commit

Permalink
Manage specular with new principled
Browse files Browse the repository at this point in the history
  • Loading branch information
julienduroure committed Oct 18, 2023
1 parent 2e4906c commit fd45e67
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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):
Expand All @@ -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):
Expand All @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Binary file modified tests/scenes/17_ior.blend
Binary file not shown.
Binary file modified tests/scenes/20_specular.blend
Binary file not shown.
39 changes: 13 additions & 26 deletions tests/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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));

});

Expand Down Expand Up @@ -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]);

});
Expand Down Expand Up @@ -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]);
Expand Down

0 comments on commit fd45e67

Please sign in to comment.