[Bf-blender-cvs] [54f5c174a8c] master: Asset: Dropping Material assets on material slot under mouse cursor.

Jeroen Bakker noreply at git.blender.org
Wed Sep 8 08:48:22 CEST 2021


Commit: 54f5c174a8cf480d934f3be8ecc85c76537ad148
Author: Jeroen Bakker
Date:   Wed Sep 8 08:39:03 2021 +0200
Branches: master
https://developer.blender.org/rB54f5c174a8cf480d934f3be8ecc85c76537ad148

Asset: Dropping Material assets on material slot under mouse cursor.

This patch allows dropping material assets from material slot under the mouse
cursor. Before this change the material slot had to be hand-picked from the
properties panel.

For consistency it is chosen to do this in any shading mode as the tooltip shows
what is exactly going to happen during release.

The feature also works for other object types than Meshes as it uses the drawn surface on the
GPU to detect the material slots. Performance of this patch has been tested with AMD GCN3.0
cards and are very responsive.

Reviewed By: fclem, Severin

Differential Revision: https://developer.blender.org/D12190

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

M	source/blender/blenkernel/BKE_object.h
M	source/blender/blenkernel/intern/object.c
M	source/blender/draw/DRW_engine.h
M	source/blender/draw/engines/basic/basic_engine.c
M	source/blender/draw/intern/DRW_render.h
M	source/blender/draw/intern/draw_manager.c
M	source/blender/draw/intern/draw_manager.h
M	source/blender/editors/include/ED_view3d.h
M	source/blender/editors/object/object_relations.c
M	source/blender/editors/space_view3d/view3d_select.c
M	source/blender/editors/space_view3d/view3d_view.c

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

diff --git a/source/blender/blenkernel/BKE_object.h b/source/blender/blenkernel/BKE_object.h
index 31b3cd66cbb..0e153c5a82a 100644
--- a/source/blender/blenkernel/BKE_object.h
+++ b/source/blender/blenkernel/BKE_object.h
@@ -463,6 +463,8 @@ void BKE_object_replace_data_on_shallow_copy(struct Object *ob, struct ID *new_d
 struct PartEff;
 struct PartEff *BKE_object_do_version_give_parteff_245(struct Object *ob);
 
+bool BKE_object_supports_material_slots(struct Object *ob);
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/source/blender/blenkernel/intern/object.c b/source/blender/blenkernel/intern/object.c
index 394245b3a2c..d0d1db9b4f8 100644
--- a/source/blender/blenkernel/intern/object.c
+++ b/source/blender/blenkernel/intern/object.c
@@ -5754,3 +5754,9 @@ void BKE_object_replace_data_on_shallow_copy(Object *ob, ID *new_data)
   }
   ob->id.py_instance = NULL;
 }
+
+bool BKE_object_supports_material_slots(struct Object *ob)
+{
+  return ELEM(
+      ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT, OB_MBALL, OB_HAIR, OB_POINTCLOUD, OB_VOLUME);
+}
diff --git a/source/blender/draw/DRW_engine.h b/source/blender/draw/DRW_engine.h
index 8a35ab2aeb9..a125a13eaf9 100644
--- a/source/blender/draw/DRW_engine.h
+++ b/source/blender/draw/DRW_engine.h
@@ -110,6 +110,7 @@ void DRW_draw_select_loop(struct Depsgraph *depsgraph,
                           bool use_obedit_skip,
                           bool draw_surface,
                           bool use_nearest,
+                          const bool do_material_sub_selection,
                           const struct rcti *rect,
                           DRW_SelectPassFn select_pass_fn,
                           void *select_pass_user_data,
diff --git a/source/blender/draw/engines/basic/basic_engine.c b/source/blender/draw/engines/basic/basic_engine.c
index c120df7e897..87f5c6f5857 100644
--- a/source/blender/draw/engines/basic/basic_engine.c
+++ b/source/blender/draw/engines/basic/basic_engine.c
@@ -25,9 +25,12 @@
 
 #include "DRW_render.h"
 
+#include "BKE_object.h"
 #include "BKE_paint.h"
 #include "BKE_particle.h"
 
+#include "BLI_alloca.h"
+
 #include "DNA_particle_types.h"
 
 #include "GPU_shader.h"
@@ -80,6 +83,7 @@ typedef struct BASIC_PrivateData {
   DRWShadingGroup *depth_shgrp[2];
   DRWShadingGroup *depth_shgrp_cull[2];
   DRWShadingGroup *depth_hair_shgrp[2];
+  bool use_material_slot_selection;
 } BASIC_PrivateData; /* Transient data */
 
 /* Functions */
@@ -131,6 +135,8 @@ static void basic_cache_init(void *vedata)
     stl->g_data = MEM_callocN(sizeof(*stl->g_data), __func__);
   }
 
+  stl->g_data->use_material_slot_selection = DRW_state_is_material_select();
+
   /* Twice for normal and in front objects. */
   for (int i = 0; i < 2; i++) {
     DRWState clip_state = (draw_ctx->sh_cfg == GPU_SHADER_CFG_CLIPPED) ? DRW_STATE_CLIP_PLANES : 0;
@@ -155,6 +161,38 @@ static void basic_cache_init(void *vedata)
   }
 }
 
+/* TODO(fclem): DRW_cache_object_surface_material_get needs a refactor to allow passing NULL
+ * instead of gpumat_array. Avoiding all this boilerplate code. */
+static struct GPUBatch **basic_object_surface_material_get(Object *ob)
+{
+  const int materials_len = DRW_cache_object_material_count_get(ob);
+  struct GPUMaterial **gpumat_array = BLI_array_alloca(gpumat_array, materials_len);
+  memset(gpumat_array, 0, sizeof(*gpumat_array) * materials_len);
+
+  return DRW_cache_object_surface_material_get(ob, gpumat_array, materials_len);
+}
+
+static void basic_cache_populate_particles(void *vedata, Object *ob)
+{
+  const bool do_in_front = (ob->dtx & OB_DRAW_IN_FRONT) != 0;
+  BASIC_StorageList *stl = ((BASIC_Data *)vedata)->stl;
+  for (ParticleSystem *psys = ob->particlesystem.first; psys != NULL; psys = psys->next) {
+    if (!DRW_object_is_visible_psys_in_active_context(ob, psys)) {
+      continue;
+    }
+    ParticleSettings *part = psys->part;
+    const int draw_as = (part->draw_as == PART_DRAW_REND) ? part->ren_as : part->draw_as;
+    if (draw_as == PART_DRAW_PATH) {
+      struct GPUBatch *hairs = DRW_cache_particles_get_hair(ob, psys, NULL);
+      if (stl->g_data->use_material_slot_selection) {
+        const short material_slot = part->omat;
+        DRW_select_load_id(ob->runtime.select_id | (material_slot << 16));
+      }
+      DRW_shgroup_call(stl->g_data->depth_hair_shgrp[do_in_front], hairs, NULL);
+    }
+  }
+}
+
 static void basic_cache_populate(void *vedata, Object *ob)
 {
   BASIC_StorageList *stl = ((BASIC_Data *)vedata)->stl;
@@ -165,24 +203,13 @@ static void basic_cache_populate(void *vedata, Object *ob)
     return;
   }
 
-  bool do_in_front = (ob->dtx & OB_DRAW_IN_FRONT) != 0;
-
   const DRWContextState *draw_ctx = DRW_context_state_get();
   if (ob != draw_ctx->object_edit) {
-    for (ParticleSystem *psys = ob->particlesystem.first; psys != NULL; psys = psys->next) {
-      if (!DRW_object_is_visible_psys_in_active_context(ob, psys)) {
-        continue;
-      }
-      ParticleSettings *part = psys->part;
-      const int draw_as = (part->draw_as == PART_DRAW_REND) ? part->ren_as : part->draw_as;
-      if (draw_as == PART_DRAW_PATH) {
-        struct GPUBatch *hairs = DRW_cache_particles_get_hair(ob, psys, NULL);
-        DRW_shgroup_call(stl->g_data->depth_hair_shgrp[do_in_front], hairs, NULL);
-      }
-    }
+    basic_cache_populate_particles(vedata, ob);
   }
 
   /* Make flat object selectable in ortho view if wireframe is enabled. */
+  const bool do_in_front = (ob->dtx & OB_DRAW_IN_FRONT) != 0;
   if ((draw_ctx->v3d->overlay.flag & V3D_OVERLAY_WIREFRAMES) ||
       (draw_ctx->v3d->shading.type == OB_WIRE) || (ob->dtx & OB_DRAWWIRE) || (ob->dt == OB_WIRE)) {
     int flat_axis = 0;
@@ -211,9 +238,25 @@ static void basic_cache_populate(void *vedata, Object *ob)
     DRW_shgroup_call_sculpt(shgrp, ob, false, false);
   }
   else {
-    struct GPUBatch *geom = DRW_cache_object_surface_get(ob);
-    if (geom) {
-      DRW_shgroup_call(shgrp, geom, ob);
+    if (stl->g_data->use_material_slot_selection && BKE_object_supports_material_slots(ob)) {
+      struct GPUBatch **geoms = basic_object_surface_material_get(ob);
+      if (geoms) {
+        const int materials_len = DRW_cache_object_material_count_get(ob);
+        for (int i = 0; i < materials_len; i++) {
+          if (geoms[i] == NULL) {
+            continue;
+          }
+          const short material_slot_select_id = i + 1;
+          DRW_select_load_id(ob->runtime.select_id | (material_slot_select_id << 16));
+          DRW_shgroup_call(shgrp, geoms[i], ob);
+        }
+      }
+    }
+    else {
+      struct GPUBatch *geom = DRW_cache_object_surface_get(ob);
+      if (geom) {
+        DRW_shgroup_call(shgrp, geom, ob);
+      }
     }
   }
 }
diff --git a/source/blender/draw/intern/DRW_render.h b/source/blender/draw/intern/DRW_render.h
index 6639a100af9..660a4adaf51 100644
--- a/source/blender/draw/intern/DRW_render.h
+++ b/source/blender/draw/intern/DRW_render.h
@@ -731,6 +731,7 @@ void DRW_select_load_id(uint id);
 /* Draw State */
 bool DRW_state_is_fbo(void);
 bool DRW_state_is_select(void);
+bool DRW_state_is_material_select(void);
 bool DRW_state_is_depth(void);
 bool DRW_state_is_image_render(void);
 bool DRW_state_is_scene_render(void);
diff --git a/source/blender/draw/intern/draw_manager.c b/source/blender/draw/intern/draw_manager.c
index 9590a4aa7ee..aca645acc09 100644
--- a/source/blender/draw/intern/draw_manager.c
+++ b/source/blender/draw/intern/draw_manager.c
@@ -2232,6 +2232,7 @@ void DRW_draw_select_loop(struct Depsgraph *depsgraph,
                           bool use_obedit_skip,
                           bool draw_surface,
                           bool UNUSED(use_nearest),
+                          const bool do_material_sub_selection,
                           const rcti *rect,
                           DRW_SelectPassFn select_pass_fn,
                           void *select_pass_user_data,
@@ -2299,6 +2300,7 @@ void DRW_draw_select_loop(struct Depsgraph *depsgraph,
 
   DST.viewport = viewport;
   DST.options.is_select = true;
+  DST.options.is_material_select = do_material_sub_selection;
   drw_task_graph_init();
   /* Get list of enabled engines */
   if (use_obedit) {
@@ -2776,6 +2778,11 @@ bool DRW_state_is_select(void)
   return DST.options.is_select;
 }
 
+bool DRW_state_is_material_select(void)
+{
+  return DST.options.is_material_select;
+}
+
 bool DRW_state_is_depth(void)
 {
   return DST.options.is_depth;
diff --git a/source/blender/draw/intern/draw_manager.h b/source/blender/draw/intern/draw_manager.h
index 1747ca752c7..c09126c98ef 100644
--- a/source/blender/draw/intern/draw_manager.h
+++ b/source/blender/draw/intern/draw_manager.h
@@ -553,6 +553,7 @@ typedef struct DRWManager {
 
   struct {
     uint is_select : 1;
+    uint is_material_select : 1;
     uint is_depth : 1;
     uint is_image_render : 1;
     uint is_scene_render : 1;
diff --git a/source/blender/editors/include/ED_view3d.h b/source/blender/editors/include/ED_view3d.h
index 2c958d282f9..cf8dcbd7995 100644
--- a/source/blender/editors/include/ED_view3d.h
+++ b/source/blender/editors/include/ED_view3d.h
@@ -566,6 +566,13 @@ eV3DSelectObjectFilter ED_view3d_select_filter_from_mode(const struct Scene *sce
 void view3d_opengl_select_cache_begin(void);
 void view3d_opengl_select_cache_end(void);
 
+int view3d_opengl_select_ex(struct ViewContext *vc,
+                            unsigned int *buffer,
+                            unsigned int bufsize,
+                            const struct rcti *input,
+                            eV3DSelectMode select_mode,
+                            eV3DSelectObjectFilter select_filter,
+                            const bool do_material_slot_selection);
 int view3d_opengl_select(struct ViewContext *vc,
                          unsigned int *buffer,
                          unsigned int bufsize,
@@ -638,6 +645,9 @@ void ED_view3d_draw_setup_view(const struct wmWindowManager *wm,
 
 struct Base *ED_view3d_give_base_under_cursor(struct bContext *C, const int mval[2]);
 struct Object *ED_view3d_give_object_under_cursor(struct bContext *C, const int mval[2]);
+struct Object *ED_view3d_give_material_slot_under_cursor(struct bContext *C,
+       

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list