Add vertex colors to blender + fix unpack_color()

This commit is contained in:
LeovanGit 2023-10-27 23:05:05 +03:00 committed by itdominator
parent a62f56d461
commit 13a6511f23
2 changed files with 13 additions and 8 deletions

View File

@ -144,6 +144,7 @@ def model_to_mesh(model: Model, scene: Scene, materials_map : Dict[str, bpy.type
vertex_positions = []
vertex_uvs = []
vertex_normals = []
vertex_colors = []
# Keeps track of which vertices each group of weights affects
# i.e. maps offset of vertices -> weights that affect them
@ -180,6 +181,11 @@ def model_to_mesh(model: Model, scene: Scene, materials_map : Dict[str, bpy.type
if segment.normals:
vertex_normals += [tuple(convert_vector_space(n)) for n in segment.normals]
if segment.colors:
vertex_colors.extend(segment.colors)
else:
[vertex_colors.extend([0.0, 0.0, 0.0, 1.0]) for _ in range(len(segment.positions))]
if segment.weights:
vertex_weights_offsets[polygon_index_offset] = segment.weights
@ -215,7 +221,6 @@ def model_to_mesh(model: Model, scene: Scene, materials_map : Dict[str, bpy.type
blender_mesh.vertices.add(len(vertex_positions))
blender_mesh.vertices.foreach_set("co", [component for vertex_position in vertex_positions for component in vertex_position])
# LOOPS
flat_indices = [index for polygon in polygons for index in polygon]
@ -233,6 +238,9 @@ def model_to_mesh(model: Model, scene: Scene, materials_map : Dict[str, bpy.type
blender_mesh.uv_layers.new(do_init=False)
blender_mesh.uv_layers[0].data.foreach_set("uv", [component for i in flat_indices for component in vertex_uvs[i]])
# Colors
blender_mesh.color_attributes.new("COLOR0", "FLOAT_COLOR", "POINT")
blender_mesh.color_attributes[0].data.foreach_set("color", vertex_colors)
# POLYGONS/FACES

View File

@ -39,12 +39,9 @@ def pack_color(color) -> int:
return packed
def unpack_color(color: int) -> List[float]:
mask = int(0x000000ff)
r = (color & (mask << 16)) / 255.0
g = (color & (mask << 8)) / 255.0
b = (color & mask) / 255.0
a = (color & (mask << 24)) / 255.0
r = (color >> 16 & 0xFF) / 255.0
g = (color >> 8 & 0xFF) / 255.0
b = (color >> 0 & 0xFF) / 255.0
a = (color >> 24 & 0xFF) / 255.0
return [r,g,b,a]