Fix #129873: GPv3: Missing material name overlay

The option of showing material name overlay is present but the feature
was missing in GPv3. This fix adds material name overlay back for GPv3.

Pull Request: https://projects.blender.org/blender/blender/pulls/129936
This commit is contained in:
YimingWu
2024-11-08 05:05:32 +01:00
committed by YimingWu
parent 5c880a5291
commit 54d957a44e

View File

@@ -8,15 +8,20 @@
#include "DRW_render.hh"
#include "draw_manager_text.hh"
#include "ED_grease_pencil.hh"
#include "BKE_attribute.hh"
#include "BKE_curves.hh"
#include "BKE_grease_pencil.hh"
#include "DNA_grease_pencil_types.h"
#include "overlay_private.hh"
#include "DEG_depsgraph_query.hh"
static void is_selection_visible(bool &r_show_points, bool &r_show_lines)
{
using namespace blender;
@@ -58,6 +63,82 @@ static void is_selection_visible(bool &r_show_points, bool &r_show_lines)
r_show_lines = flag_show_lines;
}
static void overlay_grease_pencil_draw_stroke_color_name(Object &object,
const int mat_nr,
const blender::float3 position)
{
Material *ma = BKE_object_material_get_eval(&object, mat_nr + 1);
const DRWContextState *draw_ctx = DRW_context_state_get();
ViewLayer *view_layer = draw_ctx->view_layer;
int theme_id = DRW_object_wire_theme_get(&object, view_layer, nullptr);
uchar color[4];
UI_GetThemeColor4ubv(theme_id, color);
const float3 fpt = blender::math::transform_point(object.object_to_world(), position);
DRWTextStore *dt = DRW_text_cache_ensure();
DRW_text_cache_add(dt,
fpt,
ma->id.name + 2,
strlen(ma->id.name + 2),
10,
0,
DRW_TEXT_CACHE_GLOBALSPACE | DRW_TEXT_CACHE_STRING_PTR,
color);
}
static void OVERLAY_grease_pencil_material_names(Object *ob)
{
using namespace blender;
const DRWContextState *draw_ctx = DRW_context_state_get();
bool show_points = false;
bool show_lines = false;
is_selection_visible(show_points, show_lines);
GreasePencil &grease_pencil = *static_cast<GreasePencil *>(ob->data);
Vector<ed::greasepencil::DrawingInfo> drawings = ed::greasepencil::retrieve_visible_drawings(
*draw_ctx->scene, grease_pencil, false);
if (drawings.is_empty()) {
return;
}
for (const ed::greasepencil::DrawingInfo &info : drawings) {
const bke::greasepencil::Drawing &drawing = info.drawing;
const bke::CurvesGeometry strokes = drawing.strokes();
const OffsetIndices<int> points_by_curve = strokes.points_by_curve();
const bke::AttrDomain domain = show_points ? bke::AttrDomain::Point : bke::AttrDomain::Curve;
const VArray<bool> selections = *strokes.attributes().lookup_or_default<bool>(
".selection", domain, true);
const VArray<int> materials = *strokes.attributes().lookup_or_default<int>(
"material_index", bke::AttrDomain::Curve, 0);
const Span<float3> positions = strokes.positions();
auto show_stroke_name = [&](const int stroke_i) {
if (show_points) {
for (const int point_i : points_by_curve[stroke_i]) {
if (selections[point_i]) {
return true;
}
}
return false;
}
return selections[stroke_i];
};
for (const int stroke_i : strokes.curves_range()) {
const int point_i = points_by_curve[stroke_i].first();
if (show_stroke_name(stroke_i)) {
overlay_grease_pencil_draw_stroke_color_name(*ob, materials[stroke_i], positions[point_i]);
}
}
}
}
void OVERLAY_edit_grease_pencil_cache_init(OVERLAY_Data *vedata)
{
using namespace blender;
@@ -137,7 +218,7 @@ void OVERLAY_grease_pencil_cache_init(OVERLAY_Data *vedata)
const GreasePencil &grease_pencil = *static_cast<GreasePencil *>(ob->data);
if (ts->gp_sculpt.lock_axis != GP_LOCKAXIS_CURSOR && grease_pencil.has_active_layer()) {
const blender::bke::greasepencil::Layer &layer = *grease_pencil.get_active_layer();
const bke::greasepencil::Layer &layer = *grease_pencil.get_active_layer();
mat = layer.to_world_space(*ob);
}
const View3DCursor *cursor = &scene->cursor;
@@ -170,7 +251,7 @@ void OVERLAY_grease_pencil_cache_init(OVERLAY_Data *vedata)
mat.location() = cursor->location;
}
else if (grease_pencil.has_active_layer()) {
const blender::bke::greasepencil::Layer &layer = *grease_pencil.get_active_layer();
const bke::greasepencil::Layer &layer = *grease_pencil.get_active_layer();
mat.location() = layer.to_world_space(*ob).location();
}
/* Local transform of the grid from the overlay settings. */
@@ -222,6 +303,14 @@ void OVERLAY_edit_grease_pencil_cache_populate(OVERLAY_Data *vedata, Object *ob)
DRW_shgroup_call_no_cull(points_grp, geom_points, ob);
}
}
View3D *v3d = draw_ctx->v3d;
/* Don't show object extras in set's. */
if ((ob->base_flag & (BASE_FROM_SET | BASE_FROM_DUPLI)) == 0) {
if ((v3d->gp_flag & V3D_GP_SHOW_MATERIAL_NAME) && DRW_state_show_text()) {
OVERLAY_grease_pencil_material_names(ob);
}
}
}
void OVERLAY_sculpt_grease_pencil_cache_populate(OVERLAY_Data *vedata, Object *ob)