From ec4ad081e564781230e3a9b31ef48f1f8fb71899 Mon Sep 17 00:00:00 2001 From: Brecht Van Lommel <brecht@blender.org> Date: Thu, 17 Sep 2020 15:26:52 +0200 Subject: [PATCH] Shaders: update OBJ and FBX for for Principled BSDF emission strength --- io_scene_fbx/export_fbx_bin.py | 4 ++-- io_scene_fbx/import_fbx.py | 10 ++++++---- io_scene_obj/export_obj.py | 6 ++++-- io_scene_obj/import_obj.py | 2 ++ 4 files changed, 14 insertions(+), 8 deletions(-) diff --git a/io_scene_fbx/export_fbx_bin.py b/io_scene_fbx/export_fbx_bin.py index 1c43180db..65a5b8f14 100644 --- a/io_scene_fbx/export_fbx_bin.py +++ b/io_scene_fbx/export_fbx_bin.py @@ -1309,7 +1309,7 @@ def fbx_data_material_elements(root, ma, scene_data): elem_props_template_set(tmpl, props, "p_number", b"DiffuseFactor", 1.0) # Principled BSDF only has an emissive color, so we assume factor to be always 1.0. elem_props_template_set(tmpl, props, "p_color", b"EmissiveColor", ma_wrap.emission_color) - elem_props_template_set(tmpl, props, "p_number", b"EmissiveFactor", 1.0) + elem_props_template_set(tmpl, props, "p_number", b"EmissiveFactor", ma_wrap.emission_strength) # Not in Principled BSDF, so assuming always 0 elem_props_template_set(tmpl, props, "p_color", b"AmbientColor", ambient_color) elem_props_template_set(tmpl, props, "p_number", b"AmbientFactor", 0.0) @@ -1808,7 +1808,7 @@ PRINCIPLED_TEXTURE_SOCKETS_TO_FBX = ( ("base_color_texture", b"DiffuseColor"), ("alpha_texture", b"TransparencyFactor"), # Will be inverted in fact, not much we can do really... # ("base_color_texture", b"TransparentColor"), # Uses diffuse color in Blender! - # ("emit", "emit", b"EmissiveFactor"), + ("emission_strength_texture", b"EmissiveFactor"), ("emission_color_texture", b"EmissiveColor"), # ("ambient", "ambient", b"AmbientFactor"), # ("", "", b"AmbientColor"), # World stuff in Blender, for now ignore... diff --git a/io_scene_fbx/import_fbx.py b/io_scene_fbx/import_fbx.py index 3e0b2bfdc..319c49728 100644 --- a/io_scene_fbx/import_fbx.py +++ b/io_scene_fbx/import_fbx.py @@ -1457,10 +1457,9 @@ def blen_read_material(fbx_tmpl, fbx_obj, settings): # We have no metallic (a.k.a. reflection) color... # elem_props_get_color_rgb(fbx_props, b'ReflectionColor', const_color_white) ma_wrap.normalmap_strength = elem_props_get_number(fbx_props, b'BumpFactor', 1.0) - # For emission color we can take into account the factor, but only for default values, not in case of texture. - emission_factor = elem_props_get_number(fbx_props, b'EmissiveFactor', 1.0) - ma_wrap.emission_color = [c * emission_factor - for c in elem_props_get_color_rgb(fbx_props, b'EmissiveColor', const_color_black)] + # Emission strength and color + ma_wrap.emission_strength = elem_props_get_number(fbx_props, b'EmissiveFactor', 1.0) + ma_wrap.emission_color = elem_props_get_color_rgb(fbx_props, b'EmissiveColor', const_color_black) nodal_material_wrap_map[ma] = ma_wrap @@ -3151,6 +3150,9 @@ def load(operator, context, filepath="", elif lnk_type in {b'EmissiveColor'}: ma_wrap.emission_color_texture.image = image texture_mapping_set(fbx_lnk, ma_wrap.emission_color_texture) + elif lnk_type in {b'EmissiveFactor'}: + ma_wrap.emission_strength_texture.image = image + texture_mapping_set(fbx_lnk, ma_wrap.emission_strength_texture) else: print("WARNING: material link %r ignored" % lnk_type) diff --git a/io_scene_obj/export_obj.py b/io_scene_obj/export_obj.py index dbd966a86..50cec8360 100644 --- a/io_scene_obj/export_obj.py +++ b/io_scene_obj/export_obj.py @@ -88,7 +88,9 @@ def write_mtl(scene, filepath, path_mode, copy_set, mtl_dict): # XXX TODO Find a way to handle tint and diffuse color, in a consistent way with import... fw('Ks %.6f %.6f %.6f\n' % (mat_wrap.specular, mat_wrap.specular, mat_wrap.specular)) # Specular # Emission, not in original MTL standard but seems pretty common, see T45766. - fw('Ke %.6f %.6f %.6f\n' % mat_wrap.emission_color[:3]) + emission_strength = mat_wrap.emission_strength + emission = [emission_strength * c for c in mat_wrap.emission_color[:3]] + fw('Ke %.6f %.6f %.6f\n' % tuple(emission)) fw('Ni %.6f\n' % mat_wrap.ior) # Refraction index fw('d %.6f\n' % mat_wrap.alpha) # Alpha (obj uses 'd' for dissolve) @@ -117,7 +119,7 @@ def write_mtl(scene, filepath, path_mode, copy_set, mtl_dict): "map_Bump": "normalmap_texture", "disp": None, # displacement... "refl": "metallic_texture", - "map_Ke": "emission_color_texture", + "map_Ke": "emission_color_texture" if emission_strength != 0.0 else None, } for key, mat_wrap_key in sorted(image_map.items()): diff --git a/io_scene_obj/import_obj.py b/io_scene_obj/import_obj.py index db4efb9a0..85db6aec5 100644 --- a/io_scene_obj/import_obj.py +++ b/io_scene_obj/import_obj.py @@ -195,6 +195,7 @@ def create_materials(filepath, relpath, elif type == 'Ke': _generic_tex_set(mat_wrap.emission_color_texture, image, 'UV', map_offset, map_scale) + mat_wrap.emission_strength = 1.0 elif type == 'Bump': bump_mult = map_options.get(b'-bm') @@ -357,6 +358,7 @@ def create_materials(filepath, relpath, # We cannot set context_material.emit right now, we need final diffuse color as well for this. # XXX Unsupported currently context_mat_wrap.emission_color = _get_colors(line_split) + context_mat_wrap.emission_strength = 1.0 elif line_id == b'ns': # XXX Totally empirical conversion, trying to adapt it # (from 0.0 - 900.0 OBJ specular exponent range to 1.0 - 0.0 Principled BSDF range)... -- GitLab