Skip to content

Commit

Permalink
Merge pull request #2037 from KhronosGroup/udim_basecolor
Browse files Browse the repository at this point in the history
Udim basecolor
  • Loading branch information
julienduroure authored Nov 19, 2023
2 parents a5da400 + 5ecf0b3 commit e741868
Show file tree
Hide file tree
Showing 35 changed files with 495 additions and 169 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,22 @@ def gather_primitives(
"""
primitives = []

blender_primitives = __gather_cache_primitives(materials, blender_mesh, uuid_for_skined_data,
blender_primitives, addional_materials_udim = __gather_cache_primitives(materials, blender_mesh, uuid_for_skined_data,
vertex_groups, modifiers, export_settings)

for internal_primitive in blender_primitives:

# We already call this function, in order to retrieve uvmap info, if any
# So here, only the cache will be used
base_material, material_info = get_base_material(internal_primitive['material'], materials, export_settings)
for internal_primitive, udim_material in zip(blender_primitives, addional_materials_udim):

# Now, we can retrieve the real material, by checking attributes and active maps
blender_mat = get_material_from_idx(internal_primitive['material'], materials, export_settings)
material = get_final_material(blender_mesh, blender_mat, internal_primitive['uvmap_attributes_index'], base_material, material_info["uv_info"], export_settings)
if udim_material is None : # classic case, not an udim material
# We already call this function, in order to retrieve uvmap info, if any
# So here, only the cache will be used
base_material, material_info = get_base_material(internal_primitive['material'], materials, export_settings)
# Now, we can retrieve the real material, by checking attributes and active maps
blender_mat = get_material_from_idx(internal_primitive['material'], materials, export_settings)
material = get_final_material(blender_mesh, blender_mat, internal_primitive['uvmap_attributes_index'], base_material, material_info["uv_info"], export_settings)
else:
# UDIM case
base_material, material_info, unique_material_id = udim_material
material = get_final_material(blender_mesh, unique_material_id, internal_primitive['uvmap_attributes_index'], base_material, material_info["uv_info"], export_settings)

primitive = gltf2_io.MeshPrimitive(
attributes=internal_primitive['attributes'],
Expand Down Expand Up @@ -127,7 +131,7 @@ def __gather_cache_primitives(
"""
primitives = []

blender_primitives = gltf2_blender_gather_primitives_extract.extract_primitives(
blender_primitives, additional_materials_udim = gltf2_blender_gather_primitives_extract.extract_primitives(
materials, blender_mesh, uuid_for_skined_data, vertex_groups, modifiers, export_settings)

for internal_primitive in blender_primitives:
Expand All @@ -141,7 +145,7 @@ def __gather_cache_primitives(
}
primitives.append(primitive)

return primitives
return primitives, additional_materials_udim

def __gather_indices(blender_primitive, blender_mesh, modifiers, export_settings):
indices = blender_primitive.get('indices')
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@ def export_anisotropy(blender_material, export_settings):

anisotropy_extension = {}
uvmap_infos = {}
udim_infos = {}

anisotropy_socket = get_socket(blender_material, 'Anisotropic')
anisotropic_rotation_socket = get_socket(blender_material, 'Anisotropic Rotation')
anisotropy_tangent_socket = get_socket(blender_material, 'Tangent')

if anisotropy_socket.socket is None or anisotropic_rotation_socket.socket is None or anisotropy_tangent_socket.socket is None:
return None, {}
return None, {}, {}

if anisotropy_socket.socket.is_linked is False and anisotropic_rotation_socket.socket.is_linked is False:
# We don't need the complex node setup, just export the value
Expand All @@ -42,9 +43,9 @@ def export_anisotropy(blender_material, export_settings):
anisotropy_extension['anisotropyRotation'] = anisotropyRotation

if len(anisotropy_extension) == 0:
return None, {}
return None, {}, {}

return Extension('KHR_materials_anisotropy', anisotropy_extension, False), uvmap_infos
return Extension('KHR_materials_anisotropy', anisotropy_extension, False), uvmap_infos, udim_infos

# Get complex node setup

Expand All @@ -59,7 +60,7 @@ def export_anisotropy(blender_material, export_settings):
# Trying to export from grayscale textures
anisotropy_texture, uvmap_info = export_anisotropy_from_grayscale_textures(blender_material, export_settings)
if anisotropy_texture is None:
return None, {}
return None, {}, {}

fac = get_factor_from_socket(anisotropy_socket, kind='VALUE')
if fac is None and anisotropy_texture is not None:
Expand All @@ -76,7 +77,7 @@ def export_anisotropy(blender_material, export_settings):
anisotropy_extension['anisotropyTexture'] = anisotropy_texture
uvmap_infos.update({'anisotropyTexture': uvmap_info})

return Extension('KHR_materials_anisotropy', anisotropy_extension, False), uvmap_infos
return Extension('KHR_materials_anisotropy', anisotropy_extension, False), uvmap_infos, udim_infos


# Export from complex node setup
Expand All @@ -88,17 +89,18 @@ def export_anisotropy(blender_material, export_settings):

# Get texture data
# No need to check here that we have a texture, this check is already done insode detect_anisotropy_nodes
anisotropy_texture, uvmap_info , _ = gltf2_blender_gather_texture_info.gather_texture_info(
anisotropy_texture, uvmap_info , udim_info, _ = gltf2_blender_gather_texture_info.gather_texture_info(
anisotropy_data['tex_socket'],
(anisotropy_data['tex_socket'],),
(),
export_settings,
)
anisotropy_extension['anisotropyTexture'] = anisotropy_texture
uvmap_infos.update({'anisotropyTexture': uvmap_info})
udim_infos.update({'anisotropyTexture' : udim_info} if len(udim_info.keys()) > 0 else {})


return Extension('KHR_materials_anisotropy', anisotropy_extension, False), uvmap_infos
return Extension('KHR_materials_anisotropy', anisotropy_extension, False), uvmap_infos, udim_infos

def export_anisotropy_from_grayscale_textures(blender_material, export_settings):
# There will be a texture, with a complex calculation (no direct channel mapping)
Expand All @@ -114,7 +116,7 @@ def export_anisotropy_from_grayscale_textures(blender_material, export_settings)
if not has_image_node_from_socket(primary_socket, export_settings):
primary_socket = anisotropic_rotation_socket

anisotropyTexture, uvmap_info, _ = gltf2_blender_gather_texture_info.gather_texture_info(
anisotropyTexture, uvmap_info, _, _ = gltf2_blender_gather_texture_info.gather_texture_info(
primary_socket,
sockets,
(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def export_clearcoat(blender_material, export_settings):
clearcoat_enabled = True

if not clearcoat_enabled:
return None, {}
return None, {}, {}

if isinstance(clearcoat_roughness_socket.socket, bpy.types.NodeSocket) and not clearcoat_roughness_socket.socket.is_linked:
clearcoat_extension['clearcoatRoughnessFactor'] = clearcoat_roughness_socket.socket.default_value
Expand All @@ -59,35 +59,39 @@ def export_clearcoat(blender_material, export_settings):
clearcoat_roughness_slots = (clearcoat_roughness_socket,)

uvmap_infos = {}
udim_infos = {}

if len(clearcoat_roughness_slots) > 0:
if has_clearcoat_texture:
clearcoat_texture, uvmap_info, _ = gltf2_blender_gather_texture_info.gather_texture_info(
clearcoat_texture, uvmap_info, udim_info, _ = gltf2_blender_gather_texture_info.gather_texture_info(
clearcoat_socket,
clearcoat_roughness_slots,
(),
export_settings,
)
clearcoat_extension['clearcoatTexture'] = clearcoat_texture
uvmap_infos.update({'clearcoatTexture' : uvmap_info})
udim_infos.update({'clearcoatTexture' : udim_info} if len(udim_info.keys()) > 0 else {})

if has_clearcoat_roughness_texture:
clearcoat_roughness_texture, uvmap_info, _ = gltf2_blender_gather_texture_info.gather_texture_info(
clearcoat_roughness_texture, uvmap_info, udim_info, _ = gltf2_blender_gather_texture_info.gather_texture_info(
clearcoat_roughness_socket,
clearcoat_roughness_slots,
(),
export_settings,
)
clearcoat_extension['clearcoatRoughnessTexture'] = clearcoat_roughness_texture
uvmap_infos.update({'clearcoatRoughnessTexture': uvmap_info})
udim_infos.update({'clearcoatRoughnessTexture': udim_info} if len(udim_info.keys()) > 0 else {})

if has_image_node_from_socket(clearcoat_normal_socket, export_settings):
clearcoat_normal_texture, uvmap_info, _ = gltf2_blender_gather_texture_info.gather_material_normal_texture_info_class(
clearcoat_normal_texture, uvmap_info, udim_info, _ = gltf2_blender_gather_texture_info.gather_material_normal_texture_info_class(
clearcoat_normal_socket,
(clearcoat_normal_socket,),
export_settings
)
clearcoat_extension['clearcoatNormalTexture'] = clearcoat_normal_texture
uvmap_infos.update({'clearcoatNormalTexture': uvmap_info})
udim_infos.update({'clearcoatNormalTexture': udim_info} if len(udim_info.keys()) > 0 else {})

return Extension('KHR_materials_clearcoat', clearcoat_extension, False), uvmap_infos
return Extension('KHR_materials_clearcoat', clearcoat_extension, False), uvmap_infos, udim_infos
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ def export_emission_texture(blender_material, export_settings):
emissive = get_socket(blender_material, "Emissive")
if emissive.socket is None:
emissive = get_socket_from_gltf_material_node(blender_material, "Emissive")
emissive_texture, uvmap_info, _ = gltf2_blender_gather_texture_info.gather_texture_info(emissive, (emissive,), (), export_settings)
return emissive_texture, {'emissiveTexture': uvmap_info}
emissive_texture, uvmap_info, udim_info, _ = gltf2_blender_gather_texture_info.gather_texture_info(emissive, (emissive,), (), export_settings)
return emissive_texture, {'emissiveTexture': uvmap_info}, {'emissiveTexture': udim_info} if len(udim_info.keys()) > 0 else {}

def export_emission_strength_extension(emissive_factor, export_settings):
emissive_strength_extension = {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@ def export_sheen(blender_material, export_settings):
sheen_socket = get_socket(blender_material, "Sheen Weight")

if sheenTint_socket.socket is None or sheenRoughness_socket.socket is None or sheen_socket.socket is None:
return None, {}
return None, {}, {}

if sheen_socket.socket.is_linked is False and sheen_socket.socket.default_value == 0.0:
return None, {}
return None, {}, {}

uvmap_infos = {}
udim_infos = {}

#TODOExt : What to do if sheen_socket is linked? or is not between 0 and 1?

Expand All @@ -56,14 +57,15 @@ def export_sheen(blender_material, export_settings):

# Texture
if has_image_node_from_socket(sheenTint_socket, export_settings):
original_sheenColor_texture, uvmap_info, _ = gltf2_blender_gather_texture_info.gather_texture_info(
original_sheenColor_texture, uvmap_info, udim_info, _ = gltf2_blender_gather_texture_info.gather_texture_info(
sheenTint_socket,
(sheenTint_socket,),
(),
export_settings,
)
sheen_extension['sheenColorTexture'] = original_sheenColor_texture
uvmap_infos.update({'sheenColorTexture': uvmap_info})
udim_infos.update({'sheenColorTexture': udim_info} if len(udim_info) > 0 else {})

if sheenRoughness_non_linked is True:
fac = sheenRoughness_socket.socket.default_value
Expand All @@ -79,13 +81,14 @@ def export_sheen(blender_material, export_settings):

# Texture
if has_image_node_from_socket(sheenRoughness_socket, export_settings):
original_sheenRoughness_texture, uvmap_info , _ = gltf2_blender_gather_texture_info.gather_texture_info(
original_sheenRoughness_texture, uvmap_info , udim_info, _ = gltf2_blender_gather_texture_info.gather_texture_info(
sheenRoughness_socket,
(sheenRoughness_socket,),
(),
export_settings,
)
sheen_extension['sheenRoughnessTexture'] = original_sheenRoughness_texture
uvmap_infos.update({'sheenRoughnessTexture': uvmap_info})
udim_infos.update({'sheenRoughnessTexture': udim_info} if len(udim_info) > 0 else {})

return Extension('KHR_materials_sheen', sheen_extension, False), uvmap_infos
return Extension('KHR_materials_sheen', sheen_extension, False), uvmap_infos, udim_infos
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ def export_specular(blender_material, export_settings):
speculartint_socket = get_socket(blender_material, 'Specular Tint')

if specular_socket.socket is None or speculartint_socket.socket is None:
return None, {}
return None, {}, {}

uvmap_infos = {}
udim_infos = {}

specular_non_linked = isinstance(specular_socket.socket, bpy.types.NodeSocket) and not specular_socket.socket.is_linked
specularcolor_non_linked = isinstance(speculartint_socket.socket, bpy.types.NodeSocket) and not speculartint_socket.socket.is_linked
Expand Down Expand Up @@ -62,14 +63,15 @@ def export_specular(blender_material, export_settings):

# Texture
if has_image_node_from_socket(specular_socket, export_settings):
specular_texture, uvmap_info, _ = gather_texture_info(
specular_texture, uvmap_info, udim_info, _ = gather_texture_info(
specular_socket,
(specular_socket,),
(),
export_settings,
)
specular_extension['specularTexture'] = specular_texture
uvmap_infos.update({'specularTexture': uvmap_info})
udim_infos.update({'specularTexture': udim_info} if len(udim_info) > 0 else {})
extensions_needed = True

if specularcolor_non_linked is True:
Expand All @@ -93,17 +95,18 @@ def export_specular(blender_material, export_settings):

# Texture
if has_image_node_from_socket(speculartint_socket, export_settings):
specularcolor_texture, uvmap_info, _ = gather_texture_info(
specularcolor_texture, uvmap_info, udim_info, _ = gather_texture_info(
speculartint_socket,
(speculartint_socket,),
(),
export_settings,
)
specular_extension['specularColorTexture'] = specularcolor_texture
uvmap_infos.update({'specularColorTexture': uvmap_info})
udim_infos.update({'specularColorTexture': udim_info} if len(udim_info) > 0 else {})
extensions_needed = True

if extensions_needed is False:
return None, {}
return None, {}, {}

return Extension('KHR_materials_specular', specular_extension, False), uvmap_infos
return Extension('KHR_materials_specular', specular_extension, False), uvmap_infos, udim_infos
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,17 @@ def export_transmission(blender_material, export_settings):
transmission_enabled = True

if not transmission_enabled:
return None, {}
return None, {}, {}

uvmap_info = {}
udim_info = {}

# Pack transmission channel (R).
if has_transmission_texture:
transmission_slots = (transmission_socket,)

if len(transmission_slots) > 0:
combined_texture, uvmap_info, _ = gltf2_blender_gather_texture_info.gather_texture_info(
combined_texture, uvmap_info, udim_info, _ = gltf2_blender_gather_texture_info.gather_texture_info(
transmission_socket,
transmission_slots,
(),
Expand All @@ -57,4 +58,4 @@ def export_transmission(blender_material, export_settings):
if has_transmission_texture:
transmission_extension['transmissionTexture'] = combined_texture

return Extension('KHR_materials_transmission', transmission_extension, False), {'transmissionTexture': uvmap_info}
return Extension('KHR_materials_transmission', transmission_extension, False), {'transmissionTexture': uvmap_info}, {'transmissionTexture': udim_info} if len(udim_info) > 0 else {}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def export_volume(blender_material, export_settings):
transmission_enabled = True

if transmission_enabled is False:
return None, {}
return None, {}, {}

volume_extension = {}
has_thickness_texture = False
Expand All @@ -45,7 +45,7 @@ def export_volume(blender_material, export_settings):
thickness_socket = get_socket_from_gltf_material_node(blender_material, 'Thickness')
if thickness_socket.socket is None:
# If no thickness (here because there is no glTF Material Output node), no volume extension export
return None, {}
return None, {}, {}

density_socket = get_socket(blender_material, 'Density', volume=True)
attenuation_color_socket = get_socket(blender_material, 'Color', volume=True)
Expand All @@ -64,7 +64,7 @@ def export_volume(blender_material, export_settings):
val = thickness_socket.socket.default_value
if val == 0.0:
# If no thickness, no volume extension export
return None, {}
return None, {}, {}
volume_extension['thicknessFactor'] = val
elif has_image_node_from_socket(thickness_socket, export_settings):
fac = get_factor_from_socket(thickness_socket, kind='VALUE')
Expand All @@ -76,8 +76,9 @@ def export_volume(blender_material, export_settings):
if has_thickness_texture:
thickness_slots = (thickness_socket,)

udim_info = {}
if len(thickness_slots) > 0:
combined_texture, uvmap_info, _ = gltf2_blender_gather_texture_info.gather_texture_info(
combined_texture, uvmap_info, udim_info, _ = gltf2_blender_gather_texture_info.gather_texture_info(
thickness_socket,
thickness_slots,
(),
Expand All @@ -86,4 +87,4 @@ def export_volume(blender_material, export_settings):
if has_thickness_texture:
volume_extension['thicknessTexture'] = combined_texture

return Extension('KHR_materials_volume', volume_extension, False), {'thicknessTexture': uvmap_info}
return Extension('KHR_materials_volume', volume_extension, False), {'thicknessTexture': uvmap_info}, {'thicknessTexture': udim_info} if len(udim_info.keys()) > 0 else {}
Loading

0 comments on commit e741868

Please sign in to comment.