glTF exporter: fix UVMap export when ORM are using different maps

This commit is contained in:
Julien Duroure 2020-09-17 19:00:16 +02:00
parent ec4ad081e5
commit 9db2451888
4 changed files with 74 additions and 54 deletions

View File

@ -15,7 +15,7 @@
bl_info = {
'name': 'glTF 2.0 format',
'author': 'Julien Duroure, Scurest, Norbert Nopper, Urs Hanselmann, Moritz Becher, Benjamin Schmithüsen, Jim Eckerlein, and many external contributors',
"version": (1, 4, 28),
"version": (1, 4, 29),
'blender': (2, 90, 0),
'location': 'File > Import-Export',
'description': 'Import-Export as glTF 2.0',

View File

@ -146,7 +146,7 @@ def __gather_emissive_texture(blender_material, export_settings):
emissive = gltf2_blender_get.get_socket(blender_material, "Emissive")
if emissive is None:
emissive = gltf2_blender_get.get_socket_old(blender_material, "Emissive")
return gltf2_blender_gather_texture_info.gather_texture_info((emissive,), export_settings)
return gltf2_blender_gather_texture_info.gather_texture_info(emissive, (emissive,), export_settings)
def __gather_extensions(blender_material, export_settings):
@ -187,6 +187,7 @@ def __gather_normal_texture(blender_material, export_settings):
if normal is None:
normal = gltf2_blender_get.get_socket_old(blender_material, "Normal")
return gltf2_blender_gather_texture_info.gather_material_normal_texture_info_class(
normal,
(normal,),
export_settings)
@ -226,22 +227,19 @@ def __gather_orm_texture(blender_material, export_settings):
return None
# Double-check this will past the filter in texture_info
info = gltf2_blender_gather_texture_info.gather_texture_info(result, export_settings)
info = gltf2_blender_gather_texture_info.gather_texture_info(result[0], result, export_settings)
if info is None:
return None
return result
def __gather_occlusion_texture(blender_material, orm_texture, export_settings):
if orm_texture is not None:
return gltf2_blender_gather_texture_info.gather_material_occlusion_texture_info_class(
orm_texture,
export_settings)
occlusion = gltf2_blender_get.get_socket(blender_material, "Occlusion")
if occlusion is None:
occlusion = gltf2_blender_get.get_socket_old(blender_material, "Occlusion")
return gltf2_blender_gather_texture_info.gather_material_occlusion_texture_info_class(
(occlusion,),
occlusion,
orm_texture or (occlusion,),
export_settings)
@ -297,14 +295,22 @@ def __gather_clearcoat_extension(blender_material, export_settings):
clearcoat_roughness_slots = (clearcoat_roughness_socket,)
if len(clearcoat_roughness_slots) > 0:
combined_texture = gltf2_blender_gather_texture_info.gather_texture_info(clearcoat_roughness_slots, export_settings)
if has_clearcoat_texture:
clearcoat_extension['clearcoatTexture'] = combined_texture
clearcoat_extension['clearcoatTexture'] = gltf2_blender_gather_texture_info.gather_texture_info(
clearcoat_socket,
clearcoat_roughness_slots,
export_settings,
)
if has_clearcoat_roughness_texture:
clearcoat_extension['clearcoatRoughnessTexture'] = combined_texture
clearcoat_extension['clearcoatRoughnessTexture'] = gltf2_blender_gather_texture_info.gather_texture_info(
clearcoat_roughness_socket,
clearcoat_roughness_slots,
export_settings,
)
if __has_image_node_from_socket(clearcoat_normal_socket):
clearcoat_extension['clearcoatNormalTexture'] = gltf2_blender_gather_texture_info.gather_material_normal_texture_info_class(
clearcoat_normal_socket,
(clearcoat_normal_socket,),
export_settings
)
@ -336,7 +342,11 @@ def __gather_transmission_extension(blender_material, export_settings):
transmission_slots = (transmission_socket,)
if len(transmission_slots) > 0:
combined_texture = gltf2_blender_gather_texture_info.gather_texture_info(transmission_slots, export_settings)
combined_texture = gltf2_blender_gather_texture_info.gather_texture_info(
transmission_socket,
transmission_slots,
export_settings,
)
if has_transmission_texture:
transmission_extension['transmissionTexture'] = combined_texture

View File

@ -90,7 +90,7 @@ def __gather_base_color_texture(blender_material, export_settings):
else:
inputs = (base_color_socket,)
return gltf2_blender_gather_texture_info.gather_texture_info(inputs, export_settings)
return gltf2_blender_gather_texture_info.gather_texture_info(base_color_socket, inputs, export_settings)
def __gather_extensions(blender_material, export_settings):
@ -115,28 +115,29 @@ def __gather_metallic_factor(blender_material, export_settings):
def __gather_metallic_roughness_texture(blender_material, orm_texture, export_settings):
if orm_texture is not None:
texture_input = orm_texture
metallic_socket = gltf2_blender_get.get_socket(blender_material, "Metallic")
roughness_socket = gltf2_blender_get.get_socket(blender_material, "Roughness")
hasMetal = metallic_socket is not None and __has_image_node_from_socket(metallic_socket)
hasRough = roughness_socket is not None and __has_image_node_from_socket(roughness_socket)
if not hasMetal and not hasRough:
metallic_roughness = gltf2_blender_get.get_socket_old(blender_material, "MetallicRoughness")
if metallic_roughness is None or not __has_image_node_from_socket(metallic_roughness):
return None
texture_input = (metallic_roughness,)
elif not hasMetal:
texture_input = (roughness_socket,)
elif not hasRough:
texture_input = (metallic_socket,)
else:
metallic_socket = gltf2_blender_get.get_socket(blender_material, "Metallic")
roughness_socket = gltf2_blender_get.get_socket(blender_material, "Roughness")
texture_input = (metallic_socket, roughness_socket)
hasMetal = metallic_socket is not None and __has_image_node_from_socket(metallic_socket)
hasRough = roughness_socket is not None and __has_image_node_from_socket(roughness_socket)
if not hasMetal and not hasRough:
metallic_roughness = gltf2_blender_get.get_socket_old(blender_material, "MetallicRoughness")
if metallic_roughness is None or not __has_image_node_from_socket(metallic_roughness):
return None
texture_input = (metallic_roughness,)
elif not hasMetal:
texture_input = (roughness_socket,)
elif not hasRough:
texture_input = (metallic_socket,)
else:
texture_input = (metallic_socket, roughness_socket)
return gltf2_blender_gather_texture_info.gather_texture_info(texture_input, export_settings)
return gltf2_blender_gather_texture_info.gather_texture_info(
texture_input[0],
orm_texture or texture_input,
export_settings,
)
def __gather_roughness_factor(blender_material, export_settings):

View File

@ -23,40 +23,46 @@ from io_scene_gltf2.io.com.gltf2_io_extensions import Extension
from io_scene_gltf2.io.exp.gltf2_io_user_extensions import export_user_extensions
def gather_texture_info(blender_shader_sockets, export_settings):
return __gather_texture_info_helper(blender_shader_sockets, 'DEFAULT', export_settings)
# blender_shader_sockets determine the texture and primary_socket determines
# the textranform and UVMap. Ex: when combining an ORM texture, for
# occlusion the primary_socket would be the occlusion socket, and
# blender_shader_sockets would be the (O,R,M) sockets.
def gather_material_normal_texture_info_class(blender_shader_sockets, export_settings):
return __gather_texture_info_helper(blender_shader_sockets, 'NORMAL', export_settings)
def gather_texture_info(primary_socket, blender_shader_sockets, export_settings):
return __gather_texture_info_helper(primary_socket, blender_shader_sockets, 'DEFAULT', export_settings)
def gather_material_occlusion_texture_info_class(blender_shader_sockets, export_settings):
return __gather_texture_info_helper(blender_shader_sockets, 'OCCLUSION', export_settings)
def gather_material_normal_texture_info_class(primary_socket, blender_shader_sockets, export_settings):
return __gather_texture_info_helper(primary_socket, blender_shader_sockets, 'NORMAL', export_settings)
def gather_material_occlusion_texture_info_class(primary_socket, blender_shader_sockets, export_settings):
return __gather_texture_info_helper(primary_socket, blender_shader_sockets, 'OCCLUSION', export_settings)
@cached
def __gather_texture_info_helper(
primary_socket: bpy.types.NodeSocket,
blender_shader_sockets: typing.Tuple[bpy.types.NodeSocket],
kind: str,
export_settings):
if not __filter_texture_info(blender_shader_sockets, export_settings):
if not __filter_texture_info(primary_socket, blender_shader_sockets, export_settings):
return None
fields = {
'extensions': __gather_extensions(blender_shader_sockets, export_settings),
'extensions': __gather_extensions(primary_socket, export_settings),
'extras': __gather_extras(blender_shader_sockets, export_settings),
'index': __gather_index(blender_shader_sockets, export_settings),
'tex_coord': __gather_tex_coord(blender_shader_sockets, export_settings),
'tex_coord': __gather_tex_coord(primary_socket, export_settings),
}
if kind == 'DEFAULT':
texture_info = gltf2_io.TextureInfo(**fields)
elif kind == 'NORMAL':
fields['scale'] = __gather_normal_scale(blender_shader_sockets, export_settings)
fields['scale'] = __gather_normal_scale(primary_socket, export_settings)
texture_info = gltf2_io.MaterialNormalTextureInfoClass(**fields)
elif kind == 'OCCLUSION':
fields['strength'] = __gather_occlusion_strength(blender_shader_sockets, export_settings)
fields['strength'] = __gather_occlusion_strength(primary_socket, export_settings)
texture_info = gltf2_io.MaterialOcclusionTextureInfoClass(**fields)
if texture_info.index is None:
@ -67,7 +73,11 @@ def __gather_texture_info_helper(
return texture_info
def __filter_texture_info(blender_shader_sockets, export_settings):
def __filter_texture_info(primary_socket, blender_shader_sockets, export_settings):
if primary_socket is None:
return False
if __get_tex_from_socket(primary_socket) is None:
return False
if not blender_shader_sockets:
return False
if not all([elem is not None for elem in blender_shader_sockets]):
@ -79,12 +89,11 @@ def __filter_texture_info(blender_shader_sockets, export_settings):
return True
def __gather_extensions(blender_shader_sockets, export_settings):
if not hasattr(blender_shader_sockets[0], 'links'):
def __gather_extensions(primary_socket, export_settings):
if not hasattr(primary_socket, 'links'):
return None
tex_nodes = [__get_tex_from_socket(socket).shader_node for socket in blender_shader_sockets]
texture_node = tex_nodes[0] if (tex_nodes is not None and len(tex_nodes) > 0) else None
texture_node = __get_tex_from_socket(primary_socket).shader_node
if texture_node is None:
return None
texture_transform = gltf2_blender_get.get_texture_transform_from_texture_node(texture_node)
@ -100,9 +109,9 @@ def __gather_extras(blender_shader_sockets, export_settings):
# MaterialNormalTextureInfo only
def __gather_normal_scale(blender_shader_sockets, export_settings):
def __gather_normal_scale(primary_socket, export_settings):
result = gltf2_blender_search_node_tree.from_socket(
blender_shader_sockets[0],
primary_socket,
gltf2_blender_search_node_tree.FilterByType(bpy.types.ShaderNodeNormalMap))
if not result:
return None
@ -113,7 +122,7 @@ def __gather_normal_scale(blender_shader_sockets, export_settings):
# MaterialOcclusionTextureInfo only
def __gather_occlusion_strength(blender_shader_sockets, export_settings):
def __gather_occlusion_strength(primary_socket, export_settings):
return None
@ -122,8 +131,8 @@ def __gather_index(blender_shader_sockets, export_settings):
return gltf2_blender_gather_texture.gather_texture(blender_shader_sockets, export_settings)
def __gather_tex_coord(blender_shader_sockets, export_settings):
blender_shader_node = __get_tex_from_socket(blender_shader_sockets[0]).shader_node
def __gather_tex_coord(primary_socket, export_settings):
blender_shader_node = __get_tex_from_socket(primary_socket).shader_node
if len(blender_shader_node.inputs['Vector'].links) == 0:
return 0