glTF exporter: code refactoring - remove primitive splitting

This commit is contained in:
Julien Duroure 2019-10-22 06:53:06 +02:00
parent ee68a17611
commit 45c87c1ae3
1 changed files with 7 additions and 195 deletions

View File

@ -605,6 +605,7 @@ def extract_primitives(glTF, blender_mesh, blender_object, blender_vertex_groups
triangles = tessellate_polygon((polyline,))
for triangle in triangles:
for triangle_index in triangle:
loop_index_list.append(blender_polygon.loop_indices[triangle_index])
else:
@ -961,203 +962,14 @@ def extract_primitives(glTF, blender_mesh, blender_object, blender_vertex_groups
attributes[target_tangent_id].extend(target_tangents[morph_index])
#
# Add primitive plus split them if needed.
# Add non-empty primitives
#
result_primitives = []
for material_idx, primitive in material_idx_to_primitives.items():
export_color = True
#
indices = primitive[INDICES_ID]
if len(indices) == 0:
continue
position = primitive[ATTRIBUTES_ID][POSITION_ATTRIBUTE]
normal = primitive[ATTRIBUTES_ID][NORMAL_ATTRIBUTE]
if use_tangents:
tangent = primitive[ATTRIBUTES_ID][TANGENT_ATTRIBUTE]
tex_coords = []
for tex_coord_index in range(0, tex_coord_max):
tex_coords.append(primitive[ATTRIBUTES_ID][TEXCOORD_PREFIX + str(tex_coord_index)])
colors = []
if export_color:
for color_index in range(0, color_max):
colors.append(primitive[ATTRIBUTES_ID][COLOR_PREFIX + str(color_index)])
joints = []
weights = []
if export_settings[gltf2_blender_export_keys.SKINS]:
for bone_index in range(0, bone_max):
joints.append(primitive[ATTRIBUTES_ID][JOINTS_PREFIX + str(bone_index)])
weights.append(primitive[ATTRIBUTES_ID][WEIGHTS_PREFIX + str(bone_index)])
target_positions = []
target_normals = []
target_tangents = []
if export_settings[gltf2_blender_export_keys.MORPH]:
for morph_index in range(0, morph_max):
target_positions.append(primitive[ATTRIBUTES_ID][MORPH_POSITION_PREFIX + str(morph_index)])
target_normals.append(primitive[ATTRIBUTES_ID][MORPH_NORMAL_PREFIX + str(morph_index)])
if use_tangents:
target_tangents.append(primitive[ATTRIBUTES_ID][MORPH_TANGENT_PREFIX + str(morph_index)])
#
count = len(indices)
if count == 0:
continue
max_index = max(indices)
#
# NOTE: Values used by some graphics APIs as "primitive restart" values are disallowed.
# Specifically, the value 65535 (in UINT16) cannot be used as a vertex index.
# https://github.com/KhronosGroup/glTF/issues/1142
# https://github.com/KhronosGroup/glTF/pull/1476/files
range_indices = 65535
#
if max_index >= range_indices:
#
# Splitting result_primitives.
#
# At start, all indices are pending.
pending_attributes = {
POSITION_ATTRIBUTE: [],
NORMAL_ATTRIBUTE: []
}
if use_tangents:
pending_attributes[TANGENT_ATTRIBUTE] = []
pending_primitive = {
MATERIAL_ID: material_idx,
INDICES_ID: [],
ATTRIBUTES_ID: pending_attributes
}
pending_primitive[INDICES_ID].extend(indices)
pending_attributes[POSITION_ATTRIBUTE].extend(position)
pending_attributes[NORMAL_ATTRIBUTE].extend(normal)
if use_tangents:
pending_attributes[TANGENT_ATTRIBUTE].extend(tangent)
tex_coord_index = 0
for tex_coord in tex_coords:
pending_attributes[TEXCOORD_PREFIX + str(tex_coord_index)] = tex_coord
tex_coord_index += 1
if export_color:
color_index = 0
for color in colors:
pending_attributes[COLOR_PREFIX + str(color_index)] = color
color_index += 1
if export_settings[gltf2_blender_export_keys.SKINS]:
joint_index = 0
for joint in joints:
pending_attributes[JOINTS_PREFIX + str(joint_index)] = joint
joint_index += 1
weight_index = 0
for weight in weights:
pending_attributes[WEIGHTS_PREFIX + str(weight_index)] = weight
weight_index += 1
if export_settings[gltf2_blender_export_keys.MORPH]:
morph_index = 0
for target_position in target_positions:
pending_attributes[MORPH_POSITION_PREFIX + str(morph_index)] = target_position
morph_index += 1
morph_index = 0
for target_normal in target_normals:
pending_attributes[MORPH_NORMAL_PREFIX + str(morph_index)] = target_normal
morph_index += 1
if use_tangents:
morph_index = 0
for target_tangent in target_tangents:
pending_attributes[MORPH_TANGENT_PREFIX + str(morph_index)] = target_tangent
morph_index += 1
pending_indices = pending_primitive[INDICES_ID]
# Continue until all are processed.
while len(pending_indices) > 0:
process_indices = pending_primitive[INDICES_ID]
max_index = max(process_indices)
pending_indices = []
#
#
all_local_indices = []
for i in range(0, (max_index // range_indices) + 1):
all_local_indices.append([])
#
#
# For all faces ...
for face_index in range(0, len(process_indices), 3):
written = False
face_min_index = min(process_indices[face_index + 0], process_indices[face_index + 1],
process_indices[face_index + 2])
face_max_index = max(process_indices[face_index + 0], process_indices[face_index + 1],
process_indices[face_index + 2])
# ... check if it can be but in a range of maximum indices.
for i in range(0, (max_index // range_indices) + 1):
offset = i * range_indices
# Yes, so store the primitive with its indices.
if face_min_index >= offset and face_max_index < offset + range_indices:
all_local_indices[i].extend(
[process_indices[face_index + 0], process_indices[face_index + 1],
process_indices[face_index + 2]])
written = True
break
# If not written, the triangle face has indices from different ranges.
if not written:
pending_indices.extend([process_indices[face_index + 0], process_indices[face_index + 1],
process_indices[face_index + 2]])
# Only add result_primitives, which do have indices in it.
for local_indices in all_local_indices:
if len(local_indices) > 0:
current_primitive = extract_primitive_floor(pending_primitive, local_indices, use_tangents)
result_primitives.append(current_primitive)
print_console('DEBUG', 'Adding primitive with splitting. Indices: ' + str(
len(current_primitive[INDICES_ID])) + ' Vertices: ' + str(
len(current_primitive[ATTRIBUTES_ID][POSITION_ATTRIBUTE]) // 3))
# Process primitive faces having indices in several ranges.
if len(pending_indices) > 0:
pending_primitive = extract_primitive_pack(pending_primitive, pending_indices, use_tangents)
print_console('DEBUG', 'Creating temporary primitive for splitting')
else:
#
# No splitting needed.
#
result_primitives.append(primitive)
print_console('DEBUG', 'Adding primitive without splitting. Indices: ' + str(
len(primitive[INDICES_ID])) + ' Vertices: ' + str(
len(primitive[ATTRIBUTES_ID][POSITION_ATTRIBUTE]) // 3))
result_primitives = [
primitive
for primitive in material_idx_to_primitives.values()
if len(primitive[INDICES_ID]) != 0
]
print_console('INFO', 'Primitives created: ' + str(len(result_primitives)))