[Bf-blender-cvs] [5c1d2c5a9d3] master: Brush: Wrap mtex/mask_tex around functions.

Jeroen Bakker noreply at git.blender.org
Tue Oct 18 16:14:01 CEST 2022


Commit: 5c1d2c5a9d336b7b41617139e04ace23229e8d1f
Author: Jeroen Bakker
Date:   Tue Oct 18 13:50:04 2022 +0200
Branches: master
https://developer.blender.org/rB5c1d2c5a9d336b7b41617139e04ace23229e8d1f

Brush: Wrap mtex/mask_tex around functions.

`Brush` has two attributes for holding texture information (`MTex`).
One for color textures (`mtex`) and one for mask textures (`mask_mtex`).

Unfortunately sculpt mode due to reasons used `mtex` to store mask textures.
Changes like brush asset/paint mode require modes/tools to read the mask/color texture from
one place.

To start sanatizing this we isolate the attributes in functions.
`BKE_brush_color_texture_get` and `BKE_brush_mask_texture_get`. All object
paint modes should use these functions.

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

M	source/blender/blenkernel/BKE_brush.h
M	source/blender/blenkernel/intern/brush.cc
M	source/blender/editors/sculpt_paint/paint_image_2d.c
M	source/blender/editors/sculpt_paint/paint_image_proj.c
M	source/blender/editors/sculpt_paint/paint_vertex.cc
M	source/blender/editors/sculpt_paint/sculpt.c
M	source/blender/editors/sculpt_paint/sculpt_expand.c

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

diff --git a/source/blender/blenkernel/BKE_brush.h b/source/blender/blenkernel/BKE_brush.h
index 4d728002c87..8f069841fbc 100644
--- a/source/blender/blenkernel/BKE_brush.h
+++ b/source/blender/blenkernel/BKE_brush.h
@@ -18,7 +18,9 @@ extern "C" {
 struct Brush;
 struct ImBuf;
 struct ImagePool;
+struct Object;
 struct Main;
+struct MTex;
 struct Scene;
 struct ToolSettings;
 struct UnifiedPaintSettings;
@@ -109,6 +111,7 @@ float BKE_brush_curve_strength(const struct Brush *br, float p, float len);
  */
 float BKE_brush_sample_tex_3d(const struct Scene *scene,
                               const struct Brush *br,
+                              const struct MTex *mtex,
                               const float point[3],
                               float rgba[4],
                               int thread,
@@ -119,6 +122,24 @@ float BKE_brush_sample_masktex(const struct Scene *scene,
                                int thread,
                                struct ImagePool *pool);
 
+/**
+ * Get the mask texture for this given object mode.
+ *
+ * This is preferred above using mtex/mask_mtex attributes directly as due to legacy these
+ * attributes got switched in sculpt mode. 
+ */
+const struct MTex *BKE_brush_mask_texture_get(const struct Brush *brush,
+                                              const eObjectMode object_mode);
+
+/**
+ * Get the color texture for this given object mode.
+ *
+ * This is preferred above using mtex/mask_mtex attributes directly as due to legacy these
+ * attributes got switched in sculpt mode.
+ */
+const struct MTex *BKE_brush_color_texture_get(const struct Brush *brush,
+                                               const eObjectMode object_mode);
+
 /**
  * Radial control.
  */
diff --git a/source/blender/blenkernel/intern/brush.cc b/source/blender/blenkernel/intern/brush.cc
index 20d86ea5a2d..6f1435e90b8 100644
--- a/source/blender/blenkernel/intern/brush.cc
+++ b/source/blender/blenkernel/intern/brush.cc
@@ -1974,19 +1974,37 @@ void BKE_brush_curve_preset(Brush *b, eCurveMappingPreset preset)
   BKE_curvemapping_changed(cumap, false);
 }
 
+const struct MTex *BKE_brush_mask_texture_get(const struct Brush *brush,
+                                              const eObjectMode object_mode)
+{
+  if (object_mode == OB_MODE_SCULPT) {
+    return &brush->mtex;
+  }
+  return &brush->mask_mtex;
+}
+
+const struct MTex *BKE_brush_color_texture_get(const struct Brush *brush,
+                                               const eObjectMode object_mode)
+{
+  if (object_mode == OB_MODE_SCULPT) {
+    return &brush->mask_mtex;
+  }
+  return &brush->mtex;
+}
+
 float BKE_brush_sample_tex_3d(const Scene *scene,
                               const Brush *br,
+                              const MTex *mtex,
                               const float point[3],
                               float rgba[4],
                               const int thread,
                               struct ImagePool *pool)
 {
   UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
-  const MTex *mtex = &br->mtex;
   float intensity = 1.0;
   bool hasrgb = false;
 
-  if (!mtex->tex) {
+  if (mtex == nullptr || mtex->tex == nullptr) {
     intensity = 1;
   }
   else if (mtex->brush_map_mode == MTEX_MAP_MODE_3D) {
diff --git a/source/blender/editors/sculpt_paint/paint_image_2d.c b/source/blender/editors/sculpt_paint/paint_image_2d.c
index 79316361e53..b7ce4b2973c 100644
--- a/source/blender/editors/sculpt_paint/paint_image_2d.c
+++ b/source/blender/editors/sculpt_paint/paint_image_2d.c
@@ -402,7 +402,8 @@ static ImBuf *brush_painter_imbuf_new(
 
       if (is_texbrush) {
         brush_imbuf_tex_co(&tex_mapping, x, y, texco);
-        BKE_brush_sample_tex_3d(scene, brush, texco, rgba, thread, pool);
+        const MTex *mtex = &brush->mtex;
+        BKE_brush_sample_tex_3d(scene, brush, mtex, texco, rgba, thread, pool);
         /* TODO(sergey): Support texture paint color space. */
         if (!use_float) {
           IMB_colormanagement_scene_linear_to_display_v3(rgba, display);
@@ -446,6 +447,7 @@ static void brush_painter_imbuf_update(BrushPainter *painter,
 {
   Scene *scene = painter->scene;
   Brush *brush = painter->brush;
+  const MTex *mtex = &brush->mtex;
   BrushPainterCache *cache = &tile->cache;
 
   const char *display_device = scene->display_settings.display_device;
@@ -485,7 +487,7 @@ static void brush_painter_imbuf_update(BrushPainter *painter,
       if (!use_texture_old) {
         if (is_texbrush) {
           brush_imbuf_tex_co(&tex_mapping, x, y, texco);
-          BKE_brush_sample_tex_3d(scene, brush, texco, rgba, thread, pool);
+          BKE_brush_sample_tex_3d(scene, brush, mtex, texco, rgba, thread, pool);
           /* TODO(sergey): Support texture paint color space. */
           if (!use_float) {
             IMB_colormanagement_scene_linear_to_display_v3(rgba, display);
diff --git a/source/blender/editors/sculpt_paint/paint_image_proj.c b/source/blender/editors/sculpt_paint/paint_image_proj.c
index 41d5090b38c..d9aa11a2847 100644
--- a/source/blender/editors/sculpt_paint/paint_image_proj.c
+++ b/source/blender/editors/sculpt_paint/paint_image_proj.c
@@ -5370,7 +5370,7 @@ static void do_projectpaint_thread(TaskPool *__restrict UNUSED(pool), void *ph_v
 
             /* Color texture (alpha used as mask). */
             if (ps->is_texbrush) {
-              MTex *mtex = &brush->mtex;
+              const MTex *mtex = BKE_brush_color_texture_get(brush, OB_MODE_TEXTURE_PAINT);
               float samplecos[3];
               float texrgba[4];
 
@@ -5386,7 +5386,8 @@ static void do_projectpaint_thread(TaskPool *__restrict UNUSED(pool), void *ph_v
 
               /* NOTE: for clone and smear,
                * we only use the alpha, could be a special function */
-              BKE_brush_sample_tex_3d(ps->scene, brush, samplecos, texrgba, thread_index, pool);
+              BKE_brush_sample_tex_3d(
+                  ps->scene, brush, mtex, samplecos, texrgba, thread_index, pool);
 
               copy_v3_v3(texrgb, texrgba);
               custom_mask *= texrgba[3];
diff --git a/source/blender/editors/sculpt_paint/paint_vertex.cc b/source/blender/editors/sculpt_paint/paint_vertex.cc
index 6e3504332ed..acd8b1a6bb1 100644
--- a/source/blender/editors/sculpt_paint/paint_vertex.cc
+++ b/source/blender/editors/sculpt_paint/paint_vertex.cc
@@ -433,9 +433,10 @@ static void paint_and_tex_color_alpha_intern(VPaint *vp,
                                              float r_rgba[4])
 {
   const Brush *brush = BKE_paint_brush(&vp->paint);
-  BLI_assert(brush->mtex.tex != nullptr);
-  if (brush->mtex.brush_map_mode == MTEX_MAP_MODE_3D) {
-    BKE_brush_sample_tex_3d(vc->scene, brush, co, r_rgba, 0, nullptr);
+  const MTex *mtex = BKE_brush_mask_texture_get(brush, OB_MODE_SCULPT);
+  BLI_assert(mtex->tex != nullptr);
+  if (mtex->brush_map_mode == MTEX_MAP_MODE_3D) {
+    BKE_brush_sample_tex_3d(vc->scene, brush, mtex, co, r_rgba, 0, nullptr);
   }
   else {
     float co_ss[2]; /* screenspace */
@@ -445,7 +446,7 @@ static void paint_and_tex_color_alpha_intern(VPaint *vp,
             co_ss,
             (eV3DProjTest)(V3D_PROJ_TEST_CLIP_BB | V3D_PROJ_TEST_CLIP_NEAR)) == V3D_PROJ_RET_OK) {
       const float co_ss_3d[3] = {co_ss[0], co_ss[1], 0.0f}; /* we need a 3rd empty value */
-      BKE_brush_sample_tex_3d(vc->scene, brush, co_ss_3d, r_rgba, 0, nullptr);
+      BKE_brush_sample_tex_3d(vc->scene, brush, mtex, co_ss_3d, r_rgba, 0, nullptr);
     }
     else {
       zero_v4(r_rgba);
diff --git a/source/blender/editors/sculpt_paint/sculpt.c b/source/blender/editors/sculpt_paint/sculpt.c
index 0f71acd3698..95192114429 100644
--- a/source/blender/editors/sculpt_paint/sculpt.c
+++ b/source/blender/editors/sculpt_paint/sculpt.c
@@ -2458,7 +2458,7 @@ float SCULPT_brush_strength_factor(SculptSession *ss,
 {
   StrokeCache *cache = ss->cache;
   const Scene *scene = cache->vc->scene;
-  const MTex *mtex = &br->mtex;
+  const MTex *mtex = BKE_brush_mask_texture_get(br, OB_MODE_SCULPT);
   float avg = 1.0f;
   float rgba[4];
   float point[3];
@@ -2470,7 +2470,7 @@ float SCULPT_brush_strength_factor(SculptSession *ss,
   }
   else if (mtex->brush_map_mode == MTEX_MAP_MODE_3D) {
     /* Get strength by feeding the vertex location directly into a texture. */
-    avg = BKE_brush_sample_tex_3d(scene, br, point, rgba, 0, ss->tex_pool);
+    avg = BKE_brush_sample_tex_3d(scene, br, mtex, point, rgba, 0, ss->tex_pool);
   }
   else {
     float symm_point[3], point_2d[2];
@@ -2499,19 +2499,19 @@ float SCULPT_brush_strength_factor(SculptSession *ss,
       x = symm_point[0];
       y = symm_point[1];
 
-      x *= br->mtex.size[0];
-      y *= br->mtex.size[1];
+      x *= mtex->size[0];
+      y *= mtex->size[1];
 
-      x += br->mtex.ofs[0];
-      y += br->mtex.ofs[1];
+      x += mtex->ofs[0];
+      y += mtex->ofs[1];
 
-      avg = paint_get_tex_pixel(&br->mtex, x, y, ss->tex_pool, thread_id);
+      avg = paint_get_tex_pixel(mtex, x, y, ss->tex_pool, thread_id);
 
       avg += br->texture_sample_bias;
     }
     else {
       const float point_3d[3] = {point_2d[0], point_2d[1], 0.0f};
-      avg = BKE_brush_sample_tex_3d(scene, br, point_3d, rgba, 0, ss->tex_pool);
+      avg = BKE_brush_sample_tex_3d(scene, br, mtex, point_3d, rgba, 0, ss->tex_pool);
     }
   }
 
diff --git a/source/blender/editors/sculpt_paint/sculpt_expand.c b/source/blender/editors/sculpt_paint/sculpt_expand.c
index 655b03bbb36..3133bb2007e 100644
--- a/source/blender/editors/sculpt_paint/sculpt_expand.c
+++ b/source/blender/editors/sculpt_paint/sculpt_expand.c
@@ -167,15 +167,16 @@ static float sculpt_expand_falloff_value_vertex_get(SculptSession *ss,
   if (expand_cache->texture_distortion_strength == 0.0f) {
     return expand_cache->vert_falloff[v_i];
   }
-
-  if (!expand_cache->brush->mtex.tex) {
+  const Brush *brush = expand_cache->brush;
+  const MTex *mtex = BKE_brush_mask_texture_get(brush, OB_MODE_SCULPT);
+  if (!mtex->tex) {
     return expand_cache->vert_falloff[v_i];
   }
 
   float rgba[4];
   const float *vertex_co = SCULPT_vertex_co_get(ss, v);
   const float avg = BKE_brush_sample_tex_3d(
-      expand

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list