Skip to content

Commit

Permalink
Prepare vertex color info extraction
Browse files Browse the repository at this point in the history
  • Loading branch information
julienduroure committed Sep 15, 2023
1 parent b80389d commit 5d5f1ae
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ def gather_primitives(

# We already call this function, in order to retrieve uvmap info, if any
# So here, only the cache will be used
base_material, uvmap_info = get_base_material(internal_primitive['material'], materials, export_settings)
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, uvmap_info, export_settings)
material = get_final_material(blender_mesh, blender_mat, 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 @@ -260,7 +260,7 @@ def __gather_extensions(blender_mesh,
variants.append(variant_extension)
if len(variants) > 0:
if i.material:
base_material, uvmap_info = gather_material(
base_material, material_info = gather_material(
i.material,
export_settings
)
Expand All @@ -270,7 +270,7 @@ def __gather_extensions(blender_mesh,

if base_material is not None:
# Now, we can retrieve the real material, by checking attributes and active maps
mat = get_final_material(blender_mesh, i.material, attr_indices, base_material, uvmap_info, export_settings)
mat = get_final_material(blender_mesh, i.material, attr_indices, base_material, material_info["uv_info"], export_settings)
else:
mat = None

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -405,8 +405,8 @@ def material_uvmap_attribute_add(self):
# So we need to get, for each material, what are these custom attribute
# No choice : We need to retrieve materials here. Anyway, this will be baked, and next call will be quick
for material_idx in self.prim_indices.keys():
_, uvmap_info = get_base_material(material_idx, self.materials, self.export_settings)
self.uvmap_attribute_list = list(set([i['value'] for i in uvmap_info.values() if 'type' in i.keys() and i['type'] == "Attribute" ]))
_, material_info = get_base_material(material_idx, self.materials, self.export_settings)
self.uvmap_attribute_list = list(set([i['value'] for i in material_info["uv_info"].values() if 'type' in i.keys() and i['type'] == "Attribute" ]))

additional_fields = []
for attr in self.uvmap_attribute_list:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ def gather_material(blender_material, export_settings):
if not __filter_material(blender_material, export_settings):
return None, {}

mat_unlit, uvmap_info = __export_unlit(blender_material, export_settings)
mat_unlit, uvmap_info, vc_info = __export_unlit(blender_material, export_settings)
if mat_unlit is not None:
export_user_extensions('gather_material_hook', export_settings, mat_unlit, blender_material)
return mat_unlit, uvmap_info
return mat_unlit, {"uv_info": uvmap_info, "vc_info": vc_info}

orm_texture, default_sockets = __gather_orm_texture(blender_material, export_settings)

Expand All @@ -67,7 +67,7 @@ def gather_material(blender_material, export_settings):
extensions, uvmap_info_extensions = __gather_extensions(blender_material, emissive_factor, export_settings)
normal_texture, uvmap_info_normal = __gather_normal_texture(blender_material, export_settings)
occlusion_texture, uvmap_info_occlusion = __gather_occlusion_texture(blender_material, orm_texture, default_sockets, export_settings)
pbr_metallic_roughness, uvmap_info_pbr_metallic_roughness = __gather_pbr_metallic_roughness(blender_material, orm_texture, export_settings)
pbr_metallic_roughness, uvmap_info_pbr_metallic_roughness, vc_info = __gather_pbr_metallic_roughness(blender_material, orm_texture, export_settings)

if any([i>1.0 for i in emissive_factor or []]) is True:
# Strength is set on extension
Expand Down Expand Up @@ -105,7 +105,7 @@ def gather_material(blender_material, export_settings):

export_user_extensions('gather_material_hook', export_settings, material, blender_material)

return material, uvmap_infos
return material, {"uv_info": uvmap_infos, "vc_info": vc_info}


def __get_new_material_texture_shared(base, node):
Expand Down Expand Up @@ -311,9 +311,9 @@ def __export_unlit(blender_material, export_settings):

info = gltf2_unlit.detect_shadeless_material(blender_material, export_settings)
if info is None:
return None, {}
return None, {}, {"color": None, "alpha": None}

base_color_texture, uvmap_info = gltf2_unlit.gather_base_color_texture(info, export_settings)
base_color_texture, uvmap_info, vc_info = gltf2_unlit.gather_base_color_texture(info, export_settings)

material = gltf2_io.Material(
alpha_cutoff=__gather_alpha_cutoff(blender_material, export_settings),
Expand All @@ -340,7 +340,7 @@ def __export_unlit(blender_material, export_settings):

export_user_extensions('gather_material_unlit_hook', export_settings, material, blender_material)

return material, uvmap_info
return material, uvmap_info, vc_info

def get_active_uvmap_index(blender_mesh):
# retrieve active render UVMap
Expand Down Expand Up @@ -459,15 +459,15 @@ def get_material_from_idx(material_idx, materials, export_settings):
def get_base_material(material_idx, materials, export_settings):

material = None
uvmap_info = {}
material_info = {"uv_info": {}, "vc_info": {}}

mat = get_material_from_idx(material_idx, materials, export_settings)
if mat is not None:
material, uvmap_info = gather_material(
material, material_info = gather_material(
mat,
export_settings
)
return material, uvmap_info
return material, material_info

def get_all_textures():
# Make sure to have all texture here, always in same order
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from ...exp import gltf2_blender_get
from ..gltf2_blender_gather_cache import cached
from ..gltf2_blender_get import image_tex_is_valid_from_socket
from .gltf2_blender_search_node_tree import get_vertex_color_info
from .gltf2_blender_gather_texture_info import gather_texture_info

@cached
Expand All @@ -28,7 +29,7 @@ def gather_material_pbr_metallic_roughness(blender_material, orm_texture, export

uvmap_infos = {}

base_color_texture, uvmap_info, _ = __gather_base_color_texture(blender_material, export_settings)
base_color_texture, uvmap_info, vc_info, _ = __gather_base_color_texture(blender_material, export_settings)
uvmap_infos.update(uvmap_info)
metallic_roughness_texture, uvmap_info, _ = __gather_metallic_roughness_texture(blender_material, orm_texture, export_settings)
uvmap_infos.update(uvmap_info)
Expand All @@ -45,7 +46,7 @@ def gather_material_pbr_metallic_roughness(blender_material, orm_texture, export

export_user_extensions('gather_material_pbr_metallic_roughness_hook', export_settings, material, blender_material, orm_texture)

return material, uvmap_infos
return material, uvmap_infos, vc_info


def __filter_pbr_material(blender_material, export_settings):
Expand Down Expand Up @@ -103,10 +104,11 @@ def __gather_base_color_texture(blender_material, export_settings):
if socket is not None and image_tex_is_valid_from_socket(socket)
)
if not inputs:
return None, {}, None
return None, {}, {"uv_info": {}, "vc_info": {}}, None

tex, uvmap_info, factor = gather_texture_info(inputs[0], inputs, (), export_settings)
return tex, {'baseColorTexture': uvmap_info}, factor
vc_info = get_vertex_color_info(inputs[0], inputs, export_settings)
return tex, {'baseColorTexture': uvmap_info}, vc_info, factor


def __gather_extensions(blender_material, export_settings):
Expand Down Expand Up @@ -184,3 +186,5 @@ def get_default_pbr_for_emissive_node():
metallic_roughness_texture=None,
roughness_factor=None
)


Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from ....io.com.gltf2_io_extensions import Extension
from ...exp import gltf2_blender_get
from . import gltf2_blender_gather_texture_info
from .gltf2_blender_search_node_tree import get_vertex_color_info

def detect_shadeless_material(blender_material, export_settings):
"""Detect if this material is "shadeless" ie. should be exported
Expand Down Expand Up @@ -144,5 +145,8 @@ def gather_base_color_texture(info, export_settings):
(),
export_settings,
)
return unlit_texture, {'baseColorTexture': uvmap_info}
return None, {}

vc_info = get_vertex_color_info(sockets[0], sockets, export_settings)

return unlit_texture, {'baseColorTexture': uvmap_info}, vc_info
return None, {}, {"color": None, "alpha": None}
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,6 @@ def __search_from_socket(start_socket: bpy.types.NodeSocket,
return []

return __search_from_socket(start_socket, shader_node_filter, [])

def get_vertex_color_info(primary_socket, sockets, export_settings):
return {"color": None, "alpha": None} #TODO, placeholder for now

0 comments on commit 5d5f1ae

Please sign in to comment.