[Bf-blender-cvs] [42c5fd505e1] temp-T96709-painting-target: Change shading coloring in workbench based on active node.

Jeroen Bakker noreply at git.blender.org
Mon Mar 28 11:46:53 CEST 2022


Commit: 42c5fd505e1baf05864ea8e7844a3ec7fdab91bd
Author: Jeroen Bakker
Date:   Mon Mar 28 11:46:16 2022 +0200
Branches: temp-T96709-painting-target
https://developer.blender.org/rB42c5fd505e1baf05864ea8e7844a3ec7fdab91bd

Change shading coloring in workbench based on active node.

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

M	source/blender/draw/engines/workbench/workbench_engine.c
M	source/blender/editors/include/ED_paint.h
M	source/blender/editors/sculpt_paint/paint_canvas_material.cc
M	source/blender/makesrna/intern/rna_object.c

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

diff --git a/source/blender/draw/engines/workbench/workbench_engine.c b/source/blender/draw/engines/workbench/workbench_engine.c
index f97db2fcda9..3490e9198e4 100644
--- a/source/blender/draw/engines/workbench/workbench_engine.c
+++ b/source/blender/draw/engines/workbench/workbench_engine.c
@@ -26,6 +26,8 @@
 #include "DNA_modifier_types.h"
 #include "DNA_node_types.h"
 
+#include "ED_paint.h"
+
 #include "workbench_engine.h"
 #include "workbench_private.h"
 
@@ -93,7 +95,7 @@ static void workbench_cache_sculpt_populate(WORKBENCH_PrivateData *wpd,
                                             eV3DShadingColorType color_type)
 {
   const bool use_single_drawcall = !ELEM(color_type, V3D_SHADING_MATERIAL_COLOR);
-  BLI_assert(color_type != V3D_SHADING_TEXTURE_COLOR);
+  //  BLI_assert(color_type != V3D_SHADING_TEXTURE_COLOR);
 
   if (use_single_drawcall) {
     DRWShadingGroup *grp = workbench_material_setup(wpd, ob, 0, color_type, NULL);
@@ -315,6 +317,11 @@ static eV3DShadingColorType workbench_color_type_get(WORKBENCH_PrivateData *wpd,
     color_type = V3D_SHADING_MATERIAL_COLOR;
   }
 
+  if (is_sculpt_pbvh) {
+    color_type = ED_paint_draw_color_override(
+        &wpd->scene->toolsettings->paint_mode, ob, color_type);
+  }
+
   if (r_draw_shadow) {
     *r_draw_shadow = (ob->dtx & OB_DRAW_NO_SHADOW_CAST) == 0 && SHADOW_ENABLED(wpd);
     /* Currently unsupported in sculpt mode. We could revert to the slow
diff --git a/source/blender/editors/include/ED_paint.h b/source/blender/editors/include/ED_paint.h
index bf3ada80188..23f4b8e2c25 100644
--- a/source/blender/editors/include/ED_paint.h
+++ b/source/blender/editors/include/ED_paint.h
@@ -6,6 +6,8 @@
 
 #pragma once
 
+#include "DNA_view3d_enums.h"
+
 #ifdef __cplusplus
 extern "C" {
 #endif
@@ -116,6 +118,11 @@ void ED_paint_canvas_material_itemf(struct Object *ob,
                                     struct EnumPropertyItem **r_items,
                                     int *r_totitem);
 
+/** Color type of an object can be overridden in sculpt/paint mode. */
+eV3DShadingColorType ED_paint_draw_color_override(const struct PaintModeSettings *settings,
+                                                  struct Object *ob,
+                                                  eV3DShadingColorType orig_color_type);
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/source/blender/editors/sculpt_paint/paint_canvas_material.cc b/source/blender/editors/sculpt_paint/paint_canvas_material.cc
index 63befbbb112..d5c37b39767 100644
--- a/source/blender/editors/sculpt_paint/paint_canvas_material.cc
+++ b/source/blender/editors/sculpt_paint/paint_canvas_material.cc
@@ -123,7 +123,22 @@ struct MaterialCanvas {
         }
         break;
       }
+
+      default:
+        BLI_assert_unreachable();
+    }
+  }
+
+  eV3DShadingColorType shading_color_override() const
+  {
+    switch (node->type) {
+      case SH_NODE_TEX_IMAGE:
+        return V3D_SHADING_TEXTURE_COLOR;
+      case SH_NODE_ATTRIBUTE:
+        return V3D_SHADING_VERTEX_COLOR;
     }
+    BLI_assert_unreachable();
+    return V3D_SHADING_MATERIAL_COLOR;
   }
 
   static bool supports(const Object *ob, bNode *node)
@@ -218,6 +233,27 @@ struct MaterialWrapper {
     return result;
   }
 
+  std::optional<MaterialCanvas> active_canvas(const Object *ob) const
+  {
+    if (!ma->use_nodes) {
+      return std::nullopt;
+    }
+
+    uint16_t resource_index = 0;
+    LISTBASE_FOREACH (bNode *, node, &ma->nodetree->nodes) {
+      if (!MaterialCanvas::supports(ob, node)) {
+        continue;
+      }
+      if ((node->flag & NODE_ACTIVE) == 0) {
+        resource_index += 1;
+        continue;
+      }
+      return MaterialCanvas(material_slot, resource_index, node);
+    }
+
+    return std::nullopt;
+  }
+
   void append_rna_itemf(struct EnumPropertyItem **r_items, int *r_totitem)
   {
     EnumPropertyItem item;
@@ -347,4 +383,46 @@ void ED_paint_canvas_material_itemf(Object *ob, struct EnumPropertyItem **r_item
     }
   }
 }
+
+eV3DShadingColorType ED_paint_draw_color_override(const PaintModeSettings *settings,
+                                                  Object *ob,
+                                                  eV3DShadingColorType orig_color_type)
+{
+  if (ob->sculpt == nullptr) {
+    return orig_color_type;
+  }
+
+  eV3DShadingColorType override = orig_color_type;
+  switch (settings->canvas_source) {
+    case PAINT_CANVAS_COLOR_ATTRIBUTE:
+      override = V3D_SHADING_VERTEX_COLOR;
+      break;
+    case PAINT_CANVAS_IMAGE:
+      override = V3D_SHADING_TEXTURE_COLOR;
+      break;
+    case PAINT_CANVAS_MATERIAL: {
+      std::optional<MaterialWrapper> material = get_active_material(ob);
+      if (!material.has_value()) {
+        break;
+      }
+      std::optional<MaterialCanvas> canvas = material->active_canvas(ob);
+      if (!canvas.has_value()) {
+        break;
+      }
+
+      override = canvas->shading_color_override();
+      break;
+    }
+  }
+
+  /* Reset to original color based on enabled experimental features */
+  if (!U.experimental.use_sculpt_vertex_colors && override == V3D_SHADING_VERTEX_COLOR) {
+    return orig_color_type;
+  }
+  if (!U.experimental.use_sculpt_texture_paint && override == V3D_SHADING_TEXTURE_COLOR) {
+    return orig_color_type;
+  }
+
+  return override;
+}
 }
\ No newline at end of file
diff --git a/source/blender/makesrna/intern/rna_object.c b/source/blender/makesrna/intern/rna_object.c
index d2882680214..0324acf03f0 100644
--- a/source/blender/makesrna/intern/rna_object.c
+++ b/source/blender/makesrna/intern/rna_object.c
@@ -1523,6 +1523,18 @@ static void rna_Object_paint_canvas_set(PointerRNA *ptr, int value)
   ED_paint_canvas_material_set(ob, value);
 }
 
+static void rna_Object_paint_canvas_update(Main *UNUSED(main),
+                                           Scene *UNUSED(scene),
+                                           PointerRNA *ptr)
+{
+  Object *ob = ptr->data;
+  // PBVH should be recalced. It could still point to an incorrect vertex color layer.
+  if (ob->id.us > 0) {
+    DEG_id_tag_update(&ob->id, 0);
+    WM_main_add_notifier(NC_GEOM | ND_DATA, &ob->id);
+  }
+}
+
 static const EnumPropertyItem *rna_Object_paint_canvas_itemf(bContext *UNUSED(C),
                                                              PointerRNA *ptr,
                                                              PropertyRNA *UNUSED(prop),
@@ -3233,7 +3245,7 @@ static void rna_def_object(BlenderRNA *brna)
                               "rna_Object_paint_canvas_get",
                               "rna_Object_paint_canvas_set",
                               "rna_Object_paint_canvas_itemf");
-  RNA_def_property_ui_text(prop, "Canvas", "Canvas used when painting");
+  RNA_def_property_update(prop, 0, "rna_Object_paint_canvas_update");
 
   /* transform */
   prop = RNA_def_property(srna, "location", PROP_FLOAT, PROP_TRANSLATION);



More information about the Bf-blender-cvs mailing list