This repository has been archived on 2023-10-09. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
blender-archive/source/blender/draw/engines/overlay/overlay_paint.c
Jacques Lucke 1a81d268a1 Materials: support changing materials during evaluation
This commit allows that the evaluated geometry of an object has
different materials from the original geometry. This is needed
for geometry nodes.

The main thing that changes for render engines and exporters
is that the number of material slots on an object and its geometry
might not match anymore. For original data, the slot counts are
still equal, but not for evaluated data.

Accessing material slots though rna stays the same. The behavior
adapts automatically depending on whether the object is evaluated.

When accessing materials of an object through `BKE_object_material_*`
one has to use a new api for evaluated objects:
`BKE_object_material_get_eval` and `BKE_object_material_count_eval`.
In the future, the different behavior might be hidden behind a more
general C api, but that would require quite a few more changes.

The ground truth for the number of materials is the number of materials
on the geometry now. This is important in the current design, because
Eevee needs to know the number of materials just based on the mesh in
`mesh_render_mat_len_get` and similar places.

In a few places I had to add a special case for mesh edit mode to get it
to work properly. This is unfortunate, but I don't see a way around that
for now.

Differential Revision: https://developer.blender.org/D11236
2021-05-19 10:23:09 +02:00

283 lines
10 KiB
C

/*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Copyright 2019, Blender Foundation.
*/
/** \file
* \ingroup draw_engine
*/
#include "DRW_render.h"
#include "BKE_image.h"
#include "DNA_mesh_types.h"
#include "DEG_depsgraph_query.h"
#include "overlay_private.h"
/* Check if the given object is rendered (partially) transparent */
static bool paint_object_is_rendered_transparent(View3D *v3d, Object *ob)
{
if (v3d->shading.type == OB_WIRE) {
return true;
}
if (v3d->shading.type == OB_SOLID) {
if (v3d->shading.flag & V3D_SHADING_XRAY) {
return true;
}
if (ob && v3d->shading.color_type == V3D_SHADING_OBJECT_COLOR) {
return ob->color[3] < 1.0f;
}
if (ob && ob->type == OB_MESH && ob->data &&
v3d->shading.color_type == V3D_SHADING_MATERIAL_COLOR) {
Mesh *me = ob->data;
for (int i = 0; i < me->totcol; i++) {
Material *mat = BKE_object_material_get_eval(ob, i + 1);
if (mat && mat->a < 1.0f) {
return true;
}
}
}
}
return false;
}
void OVERLAY_paint_init(OVERLAY_Data *vedata)
{
OVERLAY_StorageList *stl = vedata->stl;
OVERLAY_PrivateData *pd = stl->pd;
const DRWContextState *draw_ctx = DRW_context_state_get();
pd->painting.in_front = pd->use_in_front && draw_ctx->obact &&
(draw_ctx->obact->dtx & OB_DRAW_IN_FRONT);
pd->painting.alpha_blending = paint_object_is_rendered_transparent(draw_ctx->v3d,
draw_ctx->obact);
}
void OVERLAY_paint_cache_init(OVERLAY_Data *vedata)
{
const DRWContextState *draw_ctx = DRW_context_state_get();
OVERLAY_PassList *psl = vedata->psl;
OVERLAY_PrivateData *pd = vedata->stl->pd;
struct GPUShader *sh;
DRWShadingGroup *grp;
DRWState state;
const bool is_edit_mode = (pd->ctx_mode == CTX_MODE_EDIT_MESH);
const bool draw_contours = !is_edit_mode &&
(pd->overlay.wpaint_flag & V3D_OVERLAY_WPAINT_CONTOURS) != 0;
float opacity = 0.0f;
pd->paint_depth_grp = NULL;
psl->paint_depth_ps = NULL;
switch (pd->ctx_mode) {
case CTX_MODE_POSE:
case CTX_MODE_EDIT_MESH:
case CTX_MODE_PAINT_WEIGHT: {
opacity = is_edit_mode ? 1.0 : pd->overlay.weight_paint_mode_opacity;
if (opacity > 0.0f) {
state = DRW_STATE_WRITE_COLOR | DRW_STATE_DEPTH_EQUAL;
state |= pd->painting.alpha_blending ? DRW_STATE_BLEND_ALPHA : DRW_STATE_BLEND_MUL;
DRW_PASS_CREATE(psl->paint_color_ps, state | pd->clipping_state);
sh = OVERLAY_shader_paint_weight();
pd->paint_surf_grp = grp = DRW_shgroup_create(sh, psl->paint_color_ps);
DRW_shgroup_uniform_block(grp, "globalsBlock", G_draw.block_ubo);
DRW_shgroup_uniform_bool_copy(grp, "drawContours", draw_contours);
DRW_shgroup_uniform_bool_copy(grp, "useAlphaBlend", pd->painting.alpha_blending);
DRW_shgroup_uniform_float_copy(grp, "opacity", opacity);
DRW_shgroup_uniform_texture(grp, "colorramp", G_draw.weight_ramp);
if (pd->painting.alpha_blending) {
state = DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL;
DRW_PASS_CREATE(psl->paint_depth_ps, state | pd->clipping_state);
sh = OVERLAY_shader_depth_only();
pd->paint_depth_grp = DRW_shgroup_create(sh, psl->paint_depth_ps);
}
}
break;
}
case CTX_MODE_PAINT_VERTEX: {
opacity = pd->overlay.vertex_paint_mode_opacity;
if (opacity > 0.0f) {
state = DRW_STATE_WRITE_COLOR | DRW_STATE_DEPTH_EQUAL;
state |= pd->painting.alpha_blending ? DRW_STATE_BLEND_ALPHA : DRW_STATE_BLEND_MUL;
DRW_PASS_CREATE(psl->paint_color_ps, state | pd->clipping_state);
sh = OVERLAY_shader_paint_vertcol();
pd->paint_surf_grp = grp = DRW_shgroup_create(sh, psl->paint_color_ps);
DRW_shgroup_uniform_block(grp, "globalsBlock", G_draw.block_ubo);
DRW_shgroup_uniform_bool_copy(grp, "useAlphaBlend", pd->painting.alpha_blending);
DRW_shgroup_uniform_float_copy(grp, "opacity", opacity);
}
break;
}
case CTX_MODE_PAINT_TEXTURE: {
const ImagePaintSettings *imapaint = &draw_ctx->scene->toolsettings->imapaint;
const bool mask_enabled = imapaint->flag & IMAGEPAINT_PROJECT_LAYER_STENCIL &&
imapaint->stencil != NULL;
opacity = mask_enabled ? pd->overlay.texture_paint_mode_opacity : 0.0f;
if (opacity > 0.0f) {
state = DRW_STATE_WRITE_COLOR | DRW_STATE_DEPTH_EQUAL | DRW_STATE_BLEND_ALPHA;
DRW_PASS_CREATE(psl->paint_color_ps, state | pd->clipping_state);
GPUTexture *tex = BKE_image_get_gpu_texture(imapaint->stencil, NULL, NULL);
const bool mask_premult = (imapaint->stencil->alpha_mode == IMA_ALPHA_PREMUL);
const bool mask_inverted = (imapaint->flag & IMAGEPAINT_PROJECT_LAYER_STENCIL_INV) != 0;
sh = OVERLAY_shader_paint_texture();
pd->paint_surf_grp = grp = DRW_shgroup_create(sh, psl->paint_color_ps);
DRW_shgroup_uniform_block(grp, "globalsBlock", G_draw.block_ubo);
DRW_shgroup_uniform_float_copy(grp, "opacity", opacity);
DRW_shgroup_uniform_bool_copy(grp, "maskPremult", mask_premult);
DRW_shgroup_uniform_vec3_copy(grp, "maskColor", imapaint->stencil_col);
DRW_shgroup_uniform_bool_copy(grp, "maskInvertStencil", mask_inverted);
DRW_shgroup_uniform_texture(grp, "maskImage", tex);
}
break;
}
default:
BLI_assert(0);
break;
}
if (opacity <= 0.0f) {
psl->paint_color_ps = NULL;
pd->paint_surf_grp = NULL;
}
{
state = DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL;
DRW_PASS_CREATE(psl->paint_overlay_ps, state | pd->clipping_state);
sh = OVERLAY_shader_paint_face();
pd->paint_face_grp = grp = DRW_shgroup_create(sh, psl->paint_overlay_ps);
DRW_shgroup_uniform_vec4_copy(grp, "color", (float[4]){1.0f, 1.0f, 1.0f, 0.2f});
DRW_shgroup_state_enable(grp, DRW_STATE_BLEND_ALPHA);
sh = OVERLAY_shader_paint_wire();
pd->paint_wire_selected_grp = grp = DRW_shgroup_create(sh, psl->paint_overlay_ps);
DRW_shgroup_uniform_block(grp, "globalsBlock", G_draw.block_ubo);
DRW_shgroup_uniform_bool_copy(grp, "useSelect", true);
DRW_shgroup_state_enable(grp, DRW_STATE_BLEND_ALPHA);
pd->paint_wire_grp = grp = DRW_shgroup_create(sh, psl->paint_overlay_ps);
DRW_shgroup_uniform_block(grp, "globalsBlock", G_draw.block_ubo);
DRW_shgroup_uniform_bool_copy(grp, "useSelect", false);
DRW_shgroup_state_enable(grp, DRW_STATE_BLEND_ALPHA);
sh = OVERLAY_shader_paint_point();
pd->paint_point_grp = grp = DRW_shgroup_create(sh, psl->paint_overlay_ps);
DRW_shgroup_uniform_block(grp, "globalsBlock", G_draw.block_ubo);
}
}
void OVERLAY_paint_texture_cache_populate(OVERLAY_Data *vedata, Object *ob)
{
OVERLAY_PrivateData *pd = vedata->stl->pd;
struct GPUBatch *geom = NULL;
const Mesh *me_orig = DEG_get_original_object(ob)->data;
const bool use_face_sel = (me_orig->editflag & ME_EDIT_PAINT_FACE_SEL) != 0;
if (pd->paint_surf_grp) {
geom = DRW_cache_mesh_surface_texpaint_single_get(ob);
DRW_shgroup_call(pd->paint_surf_grp, geom, ob);
}
if (use_face_sel) {
geom = DRW_cache_mesh_surface_get(ob);
DRW_shgroup_call(pd->paint_face_grp, geom, ob);
}
}
void OVERLAY_paint_vertex_cache_populate(OVERLAY_Data *vedata, Object *ob)
{
OVERLAY_PrivateData *pd = vedata->stl->pd;
struct GPUBatch *geom = NULL;
const Mesh *me_orig = DEG_get_original_object(ob)->data;
const bool is_edit_mode = (pd->ctx_mode == CTX_MODE_EDIT_MESH);
const bool use_wire = !is_edit_mode && (pd->overlay.paint_flag & V3D_OVERLAY_PAINT_WIRE);
const bool use_face_sel = !is_edit_mode && (me_orig->editflag & ME_EDIT_PAINT_FACE_SEL);
const bool use_vert_sel = !is_edit_mode && (me_orig->editflag & ME_EDIT_PAINT_VERT_SEL);
if (ELEM(ob->mode, OB_MODE_WEIGHT_PAINT, OB_MODE_EDIT)) {
if (pd->paint_surf_grp) {
geom = DRW_cache_mesh_surface_weights_get(ob);
DRW_shgroup_call(pd->paint_surf_grp, geom, ob);
}
if (pd->paint_depth_grp) {
geom = DRW_cache_mesh_surface_weights_get(ob);
DRW_shgroup_call(pd->paint_depth_grp, geom, ob);
}
}
if (use_face_sel || use_wire) {
geom = DRW_cache_mesh_surface_edges_get(ob);
DRW_shgroup_call(use_face_sel ? pd->paint_wire_selected_grp : pd->paint_wire_grp, geom, ob);
}
if (use_face_sel) {
geom = DRW_cache_mesh_surface_get(ob);
DRW_shgroup_call(pd->paint_face_grp, geom, ob);
}
if (use_vert_sel) {
geom = DRW_cache_mesh_all_verts_get(ob);
DRW_shgroup_call(pd->paint_point_grp, geom, ob);
}
}
void OVERLAY_paint_weight_cache_populate(OVERLAY_Data *vedata, Object *ob)
{
OVERLAY_paint_vertex_cache_populate(vedata, ob);
}
void OVERLAY_paint_draw(OVERLAY_Data *vedata)
{
OVERLAY_StorageList *stl = vedata->stl;
OVERLAY_PrivateData *pd = stl->pd;
OVERLAY_PassList *psl = vedata->psl;
OVERLAY_FramebufferList *fbl = vedata->fbl;
DefaultFramebufferList *dfbl = DRW_viewport_framebuffer_list_get();
if (DRW_state_is_fbo()) {
if (pd->painting.alpha_blending) {
GPU_framebuffer_bind(pd->painting.in_front ? fbl->overlay_in_front_fb :
fbl->overlay_default_fb);
}
else {
/* Paint overlay needs final color because of multiply blend mode. */
GPU_framebuffer_bind(pd->painting.in_front ? dfbl->in_front_fb : dfbl->default_fb);
}
}
if (psl->paint_depth_ps) {
DRW_draw_pass(psl->paint_depth_ps);
}
if (psl->paint_color_ps) {
DRW_draw_pass(psl->paint_color_ps);
}
if (psl->paint_overlay_ps) {
DRW_draw_pass(psl->paint_overlay_ps);
}
}