From d33b3e15ca6672490062774d18965af3d4968cf0 Mon Sep 17 00:00:00 2001 From: Seva Alekseyev Date: Tue, 1 Mar 2016 12:43:39 -0500 Subject: When exporting meshes with per-vertex coloring to X3D, storing per-vertex colors (as opposed to per-face) when appropriate --- io_scene_x3d/export_x3d.py | 35 +++++++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 6 deletions(-) (limited to 'io_scene_x3d') diff --git a/io_scene_x3d/export_x3d.py b/io_scene_x3d/export_x3d.py index 24bfbe1e..4f9fecf8 100644 --- a/io_scene_x3d/export_x3d.py +++ b/io_scene_x3d/export_x3d.py @@ -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\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\n') #--- output vertexColors -- cgit v1.2.3