Blender  V3.3
overlay_paint.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2019 Blender Foundation. */
3 
8 #include "DRW_render.h"
9 
10 #include "BKE_image.h"
11 
12 #include "DNA_mesh_types.h"
13 
14 #include "DEG_depsgraph_query.h"
15 
16 #include "overlay_private.h"
17 
18 /* Check if the given object is rendered (partially) transparent */
20 {
21  if (v3d->shading.type == OB_WIRE) {
22  return true;
23  }
24  if (v3d->shading.type == OB_SOLID) {
25  if (v3d->shading.flag & V3D_SHADING_XRAY) {
26  return true;
27  }
28 
29  if (ob && v3d->shading.color_type == V3D_SHADING_OBJECT_COLOR) {
30  return ob->color[3] < 1.0f;
31  }
32  if (ob && ob->type == OB_MESH && ob->data &&
34  Mesh *me = ob->data;
35  for (int i = 0; i < me->totcol; i++) {
36  Material *mat = BKE_object_material_get_eval(ob, i + 1);
37  if (mat && mat->a < 1.0f) {
38  return true;
39  }
40  }
41  }
42  }
43 
44  /* Check object display types. */
45  if (ob && ELEM(ob->dt, OB_WIRE, OB_BOUNDBOX)) {
46  return true;
47  }
48 
49  return false;
50 }
51 
53 {
54  OVERLAY_StorageList *stl = vedata->stl;
55  OVERLAY_PrivateData *pd = stl->pd;
56  const DRWContextState *draw_ctx = DRW_context_state_get();
57 
58  pd->painting.in_front = pd->use_in_front && draw_ctx->obact &&
59  (draw_ctx->obact->dtx & OB_DRAW_IN_FRONT);
61  draw_ctx->obact);
62 }
63 
65 {
66  const DRWContextState *draw_ctx = DRW_context_state_get();
67  OVERLAY_PassList *psl = vedata->psl;
68  OVERLAY_PrivateData *pd = vedata->stl->pd;
69  struct GPUShader *sh;
70  DRWShadingGroup *grp;
72 
73  const bool is_edit_mode = (pd->ctx_mode == CTX_MODE_EDIT_MESH);
74  const bool draw_contours = !is_edit_mode &&
76  float opacity = 0.0f;
77  pd->paint_depth_grp = NULL;
78  psl->paint_depth_ps = NULL;
79 
80  switch (pd->ctx_mode) {
81  case CTX_MODE_POSE:
82  case CTX_MODE_EDIT_MESH:
83  case CTX_MODE_PAINT_WEIGHT: {
84  opacity = is_edit_mode ? 1.0 : pd->overlay.weight_paint_mode_opacity;
85  if (opacity > 0.0f) {
88 
89  const bool do_shading = draw_ctx->v3d->shading.type != OB_WIRE;
90 
91  sh = OVERLAY_shader_paint_weight(do_shading);
93  DRW_shgroup_uniform_block(grp, "globalsBlock", G_draw.block_ubo);
94  DRW_shgroup_uniform_bool_copy(grp, "drawContours", draw_contours);
95  DRW_shgroup_uniform_float_copy(grp, "opacity", opacity);
97 
98  /* Arbitrary light to give a hint of the geometry behind the weights. */
99  if (do_shading) {
100  float light_dir[3];
101  copy_v3_fl3(light_dir, 0.0f, 0.5f, 0.86602f);
102  normalize_v3(light_dir);
103  DRW_shgroup_uniform_vec3_copy(grp, "light_dir", light_dir);
104  }
105 
106  if (pd->painting.alpha_blending) {
111  }
112  }
113  break;
114  }
115  case CTX_MODE_PAINT_VERTEX: {
117  if (opacity > 0.0f) {
121 
124  DRW_shgroup_uniform_block(grp, "globalsBlock", G_draw.block_ubo);
125  DRW_shgroup_uniform_bool_copy(grp, "useAlphaBlend", pd->painting.alpha_blending);
126  DRW_shgroup_uniform_float_copy(grp, "opacity", opacity);
127  }
128  break;
129  }
130  case CTX_MODE_PAINT_TEXTURE: {
131  const ImagePaintSettings *imapaint = &draw_ctx->scene->toolsettings->imapaint;
132  const bool mask_enabled = imapaint->flag & IMAGEPAINT_PROJECT_LAYER_STENCIL &&
133  imapaint->stencil != NULL;
134 
135  opacity = mask_enabled ? pd->overlay.texture_paint_mode_opacity : 0.0f;
136  if (opacity > 0.0f) {
139 
141 
142  const bool mask_premult = (imapaint->stencil->alpha_mode == IMA_ALPHA_PREMUL);
143  const bool mask_inverted = (imapaint->flag & IMAGEPAINT_PROJECT_LAYER_STENCIL_INV) != 0;
146  DRW_shgroup_uniform_block(grp, "globalsBlock", G_draw.block_ubo);
147  DRW_shgroup_uniform_float_copy(grp, "opacity", opacity);
148  DRW_shgroup_uniform_bool_copy(grp, "maskPremult", mask_premult);
149  DRW_shgroup_uniform_vec3_copy(grp, "maskColor", imapaint->stencil_col);
150  DRW_shgroup_uniform_bool_copy(grp, "maskInvertStencil", mask_inverted);
151  DRW_shgroup_uniform_texture(grp, "maskImage", tex);
152  }
153  break;
154  }
155  default:
156  BLI_assert(0);
157  break;
158  }
159 
160  if (opacity <= 0.0f) {
161  psl->paint_color_ps = NULL;
162  pd->paint_surf_grp = NULL;
163  }
164 
165  {
170  DRW_shgroup_uniform_vec4_copy(grp, "color", (float[4]){1.0f, 1.0f, 1.0f, 0.2f});
172 
175  DRW_shgroup_uniform_block(grp, "globalsBlock", G_draw.block_ubo);
176  DRW_shgroup_uniform_bool_copy(grp, "useSelect", true);
178 
180  DRW_shgroup_uniform_block(grp, "globalsBlock", G_draw.block_ubo);
181  DRW_shgroup_uniform_bool_copy(grp, "useSelect", false);
183 
186  DRW_shgroup_uniform_block(grp, "globalsBlock", G_draw.block_ubo);
187  }
188 }
189 
191 {
192  OVERLAY_PrivateData *pd = vedata->stl->pd;
193  struct GPUBatch *geom = NULL;
194 
195  const Mesh *me_orig = DEG_get_original_object(ob)->data;
196  const bool use_face_sel = (me_orig->editflag & ME_EDIT_PAINT_FACE_SEL) != 0;
197 
198  if (pd->paint_surf_grp) {
200  DRW_shgroup_call(pd->paint_surf_grp, geom, ob);
201  }
202 
203  if (use_face_sel) {
204  geom = DRW_cache_mesh_surface_get(ob);
205  DRW_shgroup_call(pd->paint_face_grp, geom, ob);
206  }
207 }
208 
210 {
211  OVERLAY_PrivateData *pd = vedata->stl->pd;
212  struct GPUBatch *geom = NULL;
213 
214  const Mesh *me_orig = DEG_get_original_object(ob)->data;
215  const bool is_edit_mode = (pd->ctx_mode == CTX_MODE_EDIT_MESH);
216  const bool use_wire = !is_edit_mode && (pd->overlay.paint_flag & V3D_OVERLAY_PAINT_WIRE);
217  const bool use_face_sel = !is_edit_mode && (me_orig->editflag & ME_EDIT_PAINT_FACE_SEL);
218  const bool use_vert_sel = !is_edit_mode && (me_orig->editflag & ME_EDIT_PAINT_VERT_SEL);
219 
221  if (pd->paint_surf_grp) {
223  DRW_shgroup_call(pd->paint_surf_grp, geom, ob);
224  }
225  if (pd->paint_depth_grp) {
227  DRW_shgroup_call(pd->paint_depth_grp, geom, ob);
228  }
229  }
230 
231  if (use_face_sel || use_wire) {
233  DRW_shgroup_call(use_face_sel ? pd->paint_wire_selected_grp : pd->paint_wire_grp, geom, ob);
234  }
235 
236  if (use_face_sel) {
237  geom = DRW_cache_mesh_surface_get(ob);
238  DRW_shgroup_call(pd->paint_face_grp, geom, ob);
239  }
240 
241  if (use_vert_sel) {
242  geom = DRW_cache_mesh_all_verts_get(ob);
243  DRW_shgroup_call(pd->paint_point_grp, geom, ob);
244  }
245 }
246 
248 {
250 }
251 
253 {
254  OVERLAY_StorageList *stl = vedata->stl;
255  OVERLAY_PrivateData *pd = stl->pd;
256 
257  OVERLAY_PassList *psl = vedata->psl;
258  OVERLAY_FramebufferList *fbl = vedata->fbl;
259 
260  if (DRW_state_is_fbo()) {
262  fbl->overlay_default_fb);
263  }
264 
265  if (psl->paint_depth_ps) {
267  }
268  if (psl->paint_color_ps) {
270  }
271  if (psl->paint_overlay_ps) {
273  }
274 }
@ CTX_MODE_PAINT_TEXTURE
Definition: BKE_context.h:116
@ CTX_MODE_EDIT_MESH
Definition: BKE_context.h:104
@ CTX_MODE_PAINT_VERTEX
Definition: BKE_context.h:115
@ CTX_MODE_PAINT_WEIGHT
Definition: BKE_context.h:114
@ CTX_MODE_POSE
Definition: BKE_context.h:112
struct GPUTexture * BKE_image_get_gpu_texture(struct Image *image, struct ImageUser *iuser, struct ImBuf *ibuf)
Definition: image_gpu.cc:438
struct Material * BKE_object_material_get_eval(struct Object *ob, short act)
Definition: material.c:707
#define BLI_assert(a)
Definition: BLI_assert.h:46
MINLINE float normalize_v3(float r[3])
MINLINE void copy_v3_fl3(float v[3], float x, float y, float z)
#define ELEM(...)
struct Object * DEG_get_original_object(struct Object *object)
@ IMA_ALPHA_PREMUL
@ ME_EDIT_PAINT_VERT_SEL
@ ME_EDIT_PAINT_FACE_SEL
@ OB_WIRE
@ OB_BOUNDBOX
@ OB_SOLID
@ OB_MODE_EDIT
@ OB_MODE_WEIGHT_PAINT
@ OB_MESH
@ OB_DRAW_IN_FRONT
#define IMAGEPAINT_PROJECT_LAYER_STENCIL
#define IMAGEPAINT_PROJECT_LAYER_STENCIL_INV
@ V3D_SHADING_MATERIAL_COLOR
@ V3D_SHADING_OBJECT_COLOR
@ V3D_OVERLAY_PAINT_WIRE
@ V3D_SHADING_XRAY
@ V3D_OVERLAY_WPAINT_CONTOURS
DRWState
Definition: DRW_render.h:298
@ DRW_STATE_BLEND_ALPHA
Definition: DRW_render.h:328
@ DRW_STATE_DEPTH_EQUAL
Definition: DRW_render.h:312
@ DRW_STATE_WRITE_DEPTH
Definition: DRW_render.h:302
@ DRW_STATE_WRITE_COLOR
Definition: DRW_render.h:303
@ DRW_STATE_DEPTH_LESS_EQUAL
Definition: DRW_render.h:311
@ DRW_STATE_BLEND_MUL
Definition: DRW_render.h:333
#define DRW_PASS_CREATE(pass, state)
Definition: DRW_render.h:690
#define DRW_shgroup_uniform_block(shgroup, name, ubo)
Definition: DRW_render.h:651
#define DRW_shgroup_call(shgroup, geom, ob)
Definition: DRW_render.h:414
GPUBatch
Definition: GPU_batch.h:78
void GPU_framebuffer_bind(GPUFrameBuffer *fb)
struct GPUShader GPUShader
Definition: GPU_shader.h:20
struct GPUTexture GPUTexture
Definition: GPU_texture.h:17
char alpha_mode
GPUBatch * DRW_cache_mesh_all_verts_get(Object *ob)
Definition: draw_cache.c:2834
GPUBatch * DRW_cache_mesh_surface_texpaint_single_get(Object *ob)
Definition: draw_cache.c:2884
GPUBatch * DRW_cache_mesh_surface_weights_get(Object *ob)
Definition: draw_cache.c:2902
GPUBatch * DRW_cache_mesh_surface_get(Object *ob)
Definition: draw_cache.c:2858
GPUBatch * DRW_cache_mesh_surface_edges_get(Object *ob)
Definition: draw_cache.c:2864
struct DRW_Global G_draw
Definition: draw_common.c:32
bool DRW_state_is_fbo(void)
const DRWContextState * DRW_context_state_get(void)
void DRW_shgroup_uniform_float_copy(DRWShadingGroup *shgroup, const char *name, const float value)
void DRW_shgroup_uniform_texture(DRWShadingGroup *shgroup, const char *name, const GPUTexture *tex)
void DRW_shgroup_state_enable(DRWShadingGroup *shgroup, DRWState state)
void DRW_shgroup_uniform_vec3_copy(DRWShadingGroup *shgroup, const char *name, const float *value)
DRWShadingGroup * DRW_shgroup_create(struct GPUShader *shader, DRWPass *pass)
void DRW_shgroup_uniform_vec4_copy(DRWShadingGroup *shgroup, const char *name, const float *value)
void DRW_shgroup_uniform_bool_copy(DRWShadingGroup *shgroup, const char *name, const bool value)
void DRW_draw_pass(DRWPass *pass)
const int state
ccl_gpu_kernel_postfix ccl_global float int int int int sh
static const pxr::TfToken opacity("opacity", pxr::TfToken::Immortal)
void OVERLAY_paint_texture_cache_populate(OVERLAY_Data *vedata, Object *ob)
void OVERLAY_paint_init(OVERLAY_Data *vedata)
Definition: overlay_paint.c:52
static bool paint_object_is_rendered_transparent(View3D *v3d, Object *ob)
Definition: overlay_paint.c:19
void OVERLAY_paint_cache_init(OVERLAY_Data *vedata)
Definition: overlay_paint.c:64
void OVERLAY_paint_draw(OVERLAY_Data *vedata)
void OVERLAY_paint_weight_cache_populate(OVERLAY_Data *vedata, Object *ob)
void OVERLAY_paint_vertex_cache_populate(OVERLAY_Data *vedata, Object *ob)
GPUShader * OVERLAY_shader_paint_face(void)
GPUShader * OVERLAY_shader_paint_point(void)
GPUShader * OVERLAY_shader_paint_weight(bool shading)
GPUShader * OVERLAY_shader_depth_only(void)
GPUShader * OVERLAY_shader_paint_wire(void)
GPUShader * OVERLAY_shader_paint_vertcol(void)
GPUShader * OVERLAY_shader_paint_texture(void)
struct Object * obact
Definition: DRW_render.h:983
struct Scene * scene
Definition: DRW_render.h:979
struct View3D * v3d
Definition: DRW_render.h:976
struct GPUUniformBuf * block_ubo
Definition: draw_common.h:129
struct GPUTexture * weight_ramp
Definition: draw_common.h:132
struct Image * stencil
char editflag
short totcol
OVERLAY_PassList * psl
OVERLAY_StorageList * stl
OVERLAY_FramebufferList * fbl
struct GPUFrameBuffer * overlay_in_front_fb
struct GPUFrameBuffer * overlay_default_fb
DRWPass * paint_overlay_ps
DRWPass * paint_depth_ps
DRWPass * paint_color_ps
DRWShadingGroup * paint_surf_grp
View3DOverlay overlay
DRWShadingGroup * paint_depth_grp
DRWShadingGroup * paint_wire_selected_grp
DRWShadingGroup * paint_face_grp
DRWShadingGroup * paint_point_grp
struct OVERLAY_PrivateData::@257 painting
enum eContextObjectMode ctx_mode
DRWShadingGroup * paint_wire_grp
struct OVERLAY_PrivateData * pd
float color[4]
void * data
struct ToolSettings * toolsettings
struct ImagePaintSettings imapaint
float texture_paint_mode_opacity
float vertex_paint_mode_opacity
float weight_paint_mode_opacity
View3DShading shading