[Bf-extensions-cvs] [d33b3e1] master: When exporting meshes with per-vertex coloring to X3D, storing per-vertex colors (as opposed to per-face) when appropriate

Seva Alekseyev noreply at git.blender.org
Tue Mar 1 18:43:50 CET 2016


Commit: d33b3e15ca6672490062774d18965af3d4968cf0
Author: Seva Alekseyev
Date:   Tue Mar 1 12:43:39 2016 -0500
Branches: master
https://developer.blender.org/rBAd33b3e15ca6672490062774d18965af3d4968cf0

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

===================================================================

M	io_scene_x3d/export_x3d.py

===================================================================

diff --git a/io_scene_x3d/export_x3d.py b/io_scene_x3d/export_x3d.py
index 24bfbe1..4f9fecf 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</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



More information about the Bf-extensions-cvs mailing list