When exporting meshes with per-vertex coloring to X3D, storing per-vertex colors (as opposed to per-face) when appropriate

This commit is contained in:
Seva Alekseyev (NIH) 2016-03-01 12:43:39 -05:00
parent 0f2f0e4951
commit d33b3e15ca
1 changed files with 29 additions and 6 deletions

View File

@ -592,6 +592,24 @@ def export(file,
# same as face_groups.items() but sorted so we can get predictable output.
face_groups_items = list(face_groups.items())
face_groups_items.sort(key=lambda m: (m[0][0], getattr(m[0][1], 'name', '')))
is_col = (mesh.tessface_vertex_colors.active and (material is None or material.use_vertex_color_paint))
mesh_faces_col = mesh.tessface_vertex_colors.active.data if is_col else None
# Check if vertex colors can be exported in per-vertex mode.
# Do we have just one color per vertex in every face that uses the vertex?
if is_col:
is_col_per_vertex = True
vert_color = dict()
for i, face in enumerate(mesh_faces):
fcol = mesh_faces_col[i]
face_colors = (fcol.color1, fcol.color2, fcol.color3, fcol.color4)
for j, vert_index in enumerate(face.vertices):
if vert_index not in vert_color:
vert_color[vert_index] = face_colors[j]
elif vert_color[vert_index] != face_colors[j]:
is_col_per_vertex = False
break
for (material_index, image), face_group in face_groups_items: # face_groups.items()
if face_group:
@ -601,7 +619,6 @@ def export(file,
ident += '\t'
is_smooth = False
is_col = (mesh.tessface_vertex_colors.active and (material is None or material.use_vertex_color_paint))
# kludge but as good as it gets!
for i in face_group:
@ -678,7 +695,6 @@ def export(file,
ident = ident[:-1]
fw('%s</Appearance>\n' % ident)
mesh_faces_col = mesh.tessface_vertex_colors.active.data if is_col else None
mesh_faces_uv = mesh.tessface_uv_textures.active.data if is_uv else None
#-- IndexedFaceSet or IndexedLineSet
@ -841,7 +857,7 @@ def export(file,
fw(ident_step + 'normalPerVertex="true"\n')
# IndexedTriangleSet assumes true
if is_col:
if is_col and not is_col_per_vertex:
fw(ident_step + 'colorPerVertex="false"\n')
# for IndexedTriangleSet we use a uv per vertex so this isnt needed.
@ -912,10 +928,17 @@ def export(file,
fw('" />\n')
if is_col:
# Need better logic here, dynamic determination
# which of the X3D coloring models fits better this mesh - per face
# or per vertex. Probably with an explicit fallback mode parameter.
fw('%s<Color color="' % ident)
# XXX, 1 color per face, only
for i in face_group:
fw('%.3f %.3f %.3f ' % mesh_faces_col[i].color1[:])
if is_col_per_vertex:
for i in range(len(mesh.vertices)):
fw('%.3f %.3f %.3f ' % vert_color[i][:])
else: # Export as colors per face.
# TODO: average them rather than using the first one!
for i in face_group:
fw('%.3f %.3f %.3f ' % mesh_faces_col[i].color1[:])
fw('" />\n')
#--- output vertexColors