From 1b00d422461a917623b751b3eb82ad7b522fc273 Mon Sep 17 00:00:00 2001 From: Bastien Montagne <montagne29@wanadoo.fr> Date: Sat, 11 May 2019 15:43:25 +0200 Subject: [PATCH] Tentative fix for T59850: FBX: wrong alpha in some material imported by Unity. Looks like FBX also likes to make a mess with materials' behaviors... Many thanks to Thomas Chollet (@thomasch) for his help understanding how Unity handles materials' alpha when importing from FBX. Hopefully this won't break any other importer... ;) --- io_scene_fbx/__init__.py | 2 +- io_scene_fbx/export_fbx_bin.py | 13 ++++++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/io_scene_fbx/__init__.py b/io_scene_fbx/__init__.py index ac76f4502..693cfe7d8 100644 --- a/io_scene_fbx/__init__.py +++ b/io_scene_fbx/__init__.py @@ -21,7 +21,7 @@ bl_info = { "name": "FBX format", "author": "Campbell Barton, Bastien Montagne, Jens Restemeier", - "version": (4, 14, 6), + "version": (4, 14, 7), "blender": (2, 80, 0), "location": "File > Import-Export", "description": "FBX IO meshes, UV's, vertex colors, materials, textures, cameras, lamps and actions", diff --git a/io_scene_fbx/export_fbx_bin.py b/io_scene_fbx/export_fbx_bin.py index 055aa3e17..59fe34025 100644 --- a/io_scene_fbx/export_fbx_bin.py +++ b/io_scene_fbx/export_fbx_bin.py @@ -1257,7 +1257,18 @@ def fbx_data_material_elements(root, ma, scene_data): # 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) - elem_props_template_set(tmpl, props, "p_color", b"TransparentColor", ma_wrap.base_color) + # Sweetness... Looks like we are not the only ones to not know exactly how FBX is supposed to work (see T59850). + # According to one of its developers, Unity uses that formula to extract alpha value: + # + # alpha = 1 - TransparencyFactor + # if (alpha == 1 or alpha == 0): + # alpha = 1 - TransparentColor.r + # + # Until further info, let's assume this is correct way to do, hence the following code for TransparentColor. + if ma_wrap.transmission < 1.0e-5 or ma_wrap.transmission > (1.0 - 1.0e-5): + elem_props_template_set(tmpl, props, "p_color", b"TransparentColor", (ma_wrap.transmission,) * 3) + else: + elem_props_template_set(tmpl, props, "p_color", b"TransparentColor", ma_wrap.base_color) elem_props_template_set(tmpl, props, "p_number", b"TransparencyFactor", ma_wrap.transmission) elem_props_template_set(tmpl, props, "p_number", b"Opacity", 1.0 - ma_wrap.transmission) elem_props_template_set(tmpl, props, "p_vector_3d", b"NormalMap", (0.0, 0.0, 0.0)) -- GitLab