KhronosGroup / glTF-Blender-IO

Blender glTF 2.0 importer and exporter
https://docs.blender.org/manual/en/latest/addons/import_export/scene_gltf2.html
Apache License 2.0
1.48k stars 316 forks source link

Export: shadeless materials from "Import Images as Planes" not recognized as shadeless #1335

Open scurest opened 3 years ago

scurest commented 3 years ago

The "Import Images as Planes" addon (ships with Blender, but disabled by default) has a "Shadeless" option. It creates a node graph like this

shadeless

The IAP_SHADELESS group does some stuff with the lightpath, like the lightpath trick the glTF importer creates but probably more comprehensive.

The exporter cannot currently recognize this node setup, see Import image to plane not exported in GLTF.

It would be nice to enhance the unlit detection code so it plays nice with another default addon.

I think this patch will do it if you want to do it.

Patch ````diff diff --git a/addons/io_scene_gltf2/blender/exp/gltf2_blender_gather_materials_unlit.py b/addons/io_scene_gltf2/blender/exp/gltf2_blender_gather_materials_unlit.py index 625b23dd..3dbd9048 100644 --- a/addons/io_scene_gltf2/blender/exp/gltf2_blender_gather_materials_unlit.py +++ b/addons/io_scene_gltf2/blender/exp/gltf2_blender_gather_materials_unlit.py @@ -44,7 +44,7 @@ def detect_shadeless_material(blender_material, export_settings): return None # Be careful not to misidentify a lightpath trick as mix-alpha. - result = __detect_lightpath_trick(socket) + result = __detect_shadeless_hint(socket) if result is not None: socket = result['next_socket'] else: @@ -53,7 +53,7 @@ def detect_shadeless_material(blender_material, export_settings): socket = result['next_socket'] info['alpha_socket'] = result['alpha_socket'] - result = __detect_lightpath_trick(socket) + result = __detect_shadeless_hint(socket) if result is not None: socket = result['next_socket'] @@ -87,6 +87,30 @@ def __detect_mix_alpha(socket): } +def __detect_shadeless_hint(socket): + # Detect something that hints this is a "shadeless" material. + return __detect_iap_shadeless(socket) or __detect_lightpath_trick(socket) + + +def __detect_iap_shadeless(socket): + # Detects the group node "Import Images as Planes" uses for "Shadeless" + # materials. + # + # next_socket => [ IAP_SHADELESS ] => socket + # + # Returns None if not detected. Otherwise, a dict containing next_socket. + prev = gltf2_blender_get.previous_node(socket) + is_iap_shadeless = ( + prev and + prev.type == 'GROUP' and + prev.node_tree and + prev.node_tree.name == 'IAP_SHADELESS' and + 'Color' in prev.inputs + ) + if not is_iap_shadeless: return None + return {'next_socket': prev.inputs['Color']} + + def __detect_lightpath_trick(socket): # Detects this (used to prevent casting light on other objects) See ex. # https://blender.stackexchange.com/a/21535/88681 ````

scurest commented 3 years ago

The importer could also use the IAP_SHADELESS group instead of the lightpath trick...

emackey commented 3 years ago

I'm in favor of the exporter doing this, with the patch shown above. I'm not so sure that the importer should piggyback IAP_SHADELESS though, I'd rather keep the lightpath as the import strategy.