[Bf-blender-cvs] [c6c989c169d] greasepencil-object: GPencil: Add SVG vertex color export limitted support

Antonio Vazquez noreply at git.blender.org
Fri Jul 31 16:51:21 CEST 2020


Commit: c6c989c169da5ffaebcf5390ebe0bfb114432027
Author: Antonio Vazquez
Date:   Fri Jul 31 16:51:09 2020 +0200
Branches: greasepencil-object
https://developer.blender.org/rBc6c989c169da5ffaebcf5390ebe0bfb114432027

GPencil: Add SVG vertex color export limitted support

It's impossible export vertex color, so an average color is used.

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

M	source/blender/io/gpencil/intern/gpencil_io_base.cc
M	source/blender/io/gpencil/intern/gpencil_io_base.h
M	source/blender/io/gpencil/intern/gpencil_io_svg.cc

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

diff --git a/source/blender/io/gpencil/intern/gpencil_io_base.cc b/source/blender/io/gpencil/intern/gpencil_io_base.cc
index 2f1393b4c5a..2b46cc96b70 100644
--- a/source/blender/io/gpencil/intern/gpencil_io_base.cc
+++ b/source/blender/io/gpencil/intern/gpencil_io_base.cc
@@ -287,11 +287,32 @@ struct bGPDstroke *GpencilExporter::gps_current_get(void)
   return gps_cur;
 }
 
-void GpencilExporter::gps_current_set(struct Object *ob, struct bGPDstroke *gps)
+void GpencilExporter::gps_current_set(struct Object *ob,
+                                      struct bGPDstroke *gps,
+                                      const bool set_colors)
 {
   gps_cur = gps;
-  gp_style = BKE_gpencil_material_settings(ob, gps->mat_nr + 1);
-  gp_style_current_set(gp_style);
+  if (set_colors) {
+    gp_style = BKE_gpencil_material_settings(ob, gps->mat_nr + 1);
+    gp_style_current_set(gp_style);
+
+    /* Stroke color. */
+    copy_v4_v4(stroke_color, gp_style->stroke_rgba);
+    /* Get average vertex color and apply. */
+    float avg_color[4] = {0.0f, 0.0f, 0.0f, 0.0f};
+    for (int i = 0; i < gps->totpoints; i++) {
+      bGPDspoint *pt = &gps->points[i];
+      add_v4_v4(avg_color, pt->vert_color);
+    }
+
+    mul_v4_v4fl(avg_color, avg_color, 1.0f / (float)gps->totpoints);
+    interp_v3_v3v3(stroke_color, stroke_color, avg_color, avg_color[3]);
+
+    /* Fill color. */
+    copy_v4_v4(fill_color, gp_style->fill_rgba);
+    /* Apply vertex color for fill. */
+    interp_v3_v3v3(fill_color, fill_color, gps->vert_color_fill, gps->vert_color_fill[3]);
+  }
 }
 
 void GpencilExporter::gp_style_current_set(MaterialGPencilStyle *igp_style)
diff --git a/source/blender/io/gpencil/intern/gpencil_io_base.h b/source/blender/io/gpencil/intern/gpencil_io_base.h
index d05ff9b2062..7da6ce5c811 100644
--- a/source/blender/io/gpencil/intern/gpencil_io_base.h
+++ b/source/blender/io/gpencil/intern/gpencil_io_base.h
@@ -73,6 +73,8 @@ class GpencilExporter {
   struct RegionView3D *rv3d;
   int winx, winy;
 
+  float stroke_color[4], fill_color[4];
+
   struct bGPDlayer *gpl_current_get(void);
   struct bGPDframe *gpf_current_get(void);
   struct bGPDstroke *gps_current_get(void);
@@ -82,7 +84,7 @@ class GpencilExporter {
 
   void gpl_current_set(struct bGPDlayer *gpl);
   void gpf_current_set(struct bGPDframe *gpf);
-  void gps_current_set(struct Object *ob, struct bGPDstroke *gps);
+  void gps_current_set(struct Object *ob, struct bGPDstroke *gps, const bool set_colors);
   void gp_style_current_set(MaterialGPencilStyle *gp_style);
 
  private:
diff --git a/source/blender/io/gpencil/intern/gpencil_io_svg.cc b/source/blender/io/gpencil/intern/gpencil_io_svg.cc
index b358575ed68..f7ff22075e2 100644
--- a/source/blender/io/gpencil/intern/gpencil_io_svg.cc
+++ b/source/blender/io/gpencil/intern/gpencil_io_svg.cc
@@ -155,7 +155,7 @@ void GpencilExporterSVG::export_layers(void)
         /* Duplicate the stroke to apply any layer thickness change. */
         bGPDstroke *gps_duplicate = BKE_gpencil_stroke_duplicate(gps, true);
 
-        gps_current_set(ob, gps_duplicate);
+        gps_current_set(ob, gps_duplicate, true);
 
         /* Apply layer thickness change. */
         gps_duplicate->thickness += gpl->line_change;
@@ -186,7 +186,7 @@ void GpencilExporterSVG::export_layers(void)
               bGPDstroke *gps_perimeter = BKE_gpencil_stroke_perimeter_from_view(
                   rv3d, gpd, gpl, gps_duplicate, 3, diff_mat);
 
-              gps_current_set(ob, gps_perimeter);
+              gps_current_set(ob, gps_perimeter, false);
 
               /* Sample stroke. */
               BKE_gpencil_stroke_sample(gps_perimeter, 0.03f, false);
@@ -243,25 +243,24 @@ void GpencilExporterSVG::export_stroke_path(pugi::xml_node gpl_node, const bool
 {
   bGPDlayer *gpl = gpl_current_get();
   bGPDstroke *gps = gps_current_get();
-  MaterialGPencilStyle *gp_style = gp_style_current_get();
 
   pugi::xml_node gps_node = gpl_node.append_child("path");
 
   float col[3];
   std::string stroke_hex;
   if (is_fill) {
-    gps_node.append_attribute("stroke-opacity").set_value(gp_style->fill_rgba[3] * gpl->opacity);
-    gps_node.append_attribute("fill-opacity").set_value(gp_style->fill_rgba[3] * gpl->opacity);
+    gps_node.append_attribute("stroke-opacity").set_value(fill_color[3] * gpl->opacity);
+    gps_node.append_attribute("fill-opacity").set_value(fill_color[3] * gpl->opacity);
 
-    interp_v3_v3v3(col, gp_style->fill_rgba, gpl->tintcolor, gpl->tintcolor[3]);
+    interp_v3_v3v3(col, fill_color, gpl->tintcolor, gpl->tintcolor[3]);
     linearrgb_to_srgb_v3_v3(col, col);
     stroke_hex = rgb_to_hex(col);
   }
   else {
-    gps_node.append_attribute("stroke-opacity").set_value(gp_style->stroke_rgba[3] * gpl->opacity);
-    gps_node.append_attribute("fill-opacity").set_value(gp_style->stroke_rgba[3] * gpl->opacity);
+    gps_node.append_attribute("stroke-opacity").set_value(stroke_color[3] * gpl->opacity);
+    gps_node.append_attribute("fill-opacity").set_value(stroke_color[3] * gpl->opacity);
 
-    interp_v3_v3v3(col, gp_style->stroke_rgba, gpl->tintcolor, gpl->tintcolor[3]);
+    interp_v3_v3v3(col, stroke_color, gpl->tintcolor, gpl->tintcolor[3]);
     linearrgb_to_srgb_v3_v3(col, col);
     stroke_hex = rgb_to_hex(col);
   }
@@ -346,21 +345,20 @@ void GpencilExporterSVG::color_string_set(pugi::xml_node gps_node, const bool is
 {
   bGPDlayer *gpl = gpl_current_get();
   bGPDstroke *gps = gps_current_get();
-  MaterialGPencilStyle *gp_style = gp_style_current_get();
 
   const bool round_cap = (gps->caps[0] == GP_STROKE_CAP_ROUND ||
                           gps->caps[1] == GP_STROKE_CAP_ROUND);
 
   float col[3];
   if (is_fill) {
-    interp_v3_v3v3(col, gp_style->fill_rgba, gpl->tintcolor, gpl->tintcolor[3]);
+    interp_v3_v3v3(col, fill_color, gpl->tintcolor, gpl->tintcolor[3]);
     linearrgb_to_srgb_v3_v3(col, col);
     std::string stroke_hex = rgb_to_hex(col);
     gps_node.append_attribute("fill").set_value(stroke_hex.c_str());
     gps_node.append_attribute("stroke").set_value("none");
   }
   else {
-    interp_v3_v3v3(col, gp_style->stroke_rgba, gpl->tintcolor, gpl->tintcolor[3]);
+    interp_v3_v3v3(col, stroke_color, gpl->tintcolor, gpl->tintcolor[3]);
     linearrgb_to_srgb_v3_v3(col, col);
     std::string stroke_hex = rgb_to_hex(col);
     gps_node.append_attribute("stroke").set_value(stroke_hex.c_str());
@@ -373,8 +371,8 @@ void GpencilExporterSVG::color_string_set(pugi::xml_node gps_node, const bool is
       gps_node.append_attribute("fill").set_value(stroke_hex.c_str());
     }
   }
-  gps_node.append_attribute("stroke-opacity").set_value(gp_style->stroke_rgba[3] * gpl->opacity);
-  gps_node.append_attribute("fill-opacity").set_value(gp_style->fill_rgba[3] * gpl->opacity);
+  gps_node.append_attribute("stroke-opacity").set_value(stroke_color[3] * gpl->opacity);
+  gps_node.append_attribute("fill-opacity").set_value(fill_color[3] * gpl->opacity);
 }
 
 }  // namespace gpencil



More information about the Bf-blender-cvs mailing list