Blender V5.3
draw_sculpt.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "draw_sculpt.hh"
10
11#include "DNA_mesh_types.h"
12#include "DNA_scene_types.h"
13#include "draw_attributes.hh"
15#include "draw_view.hh"
16
17#include "BKE_attribute.hh"
19#include "BKE_customdata.hh"
20#include "BKE_material.hh"
21#include "BKE_object.hh"
22#include "BKE_object_types.hh"
23#include "BKE_paint.hh"
24
25#include "BLI_math_matrix.hh"
26
27#include "bmesh_class.hh"
28
29#include "DRW_pbvh.hh"
30#include "DRW_render.hh"
31
32namespace blender::draw {
33
35{
36 static float3 colors[9] = {
37 {1.0f, 0.2f, 0.2f},
38 {0.2f, 1.0f, 0.2f},
39 {0.2f, 0.2f, 1.0f},
40 {1.0f, 1.0f, 0.2f},
41 {0.2f, 1.0f, 1.0f},
42 {1.0f, 0.2f, 1.0f},
43 {1.0f, 0.7f, 0.2f},
44 {0.2f, 1.0f, 0.7f},
45 {0.7f, 0.2f, 1.0f},
46 };
47
48 return colors[debug_index % 9];
49}
50
52 const bool use_wire,
54{
55 /* pbvh::Tree should always exist for non-empty meshes, created by depsgraph eval. */
57 const_cast<bke::pbvh::Tree *>(bke::object::pbvh_get(*ob)) :
58 nullptr;
59 if (!pbvh) {
60 return {};
61 }
62
63 /* TODO(Miguel Pozo): Don't use global context. */
64 const DRWContext *drwctx = DRW_context_get();
65 RegionView3D *rv3d = drwctx->rv3d;
66 const bool navigating = rv3d && (rv3d->rflag & RV3D_NAVIGATING);
67
68 Paint *paint = nullptr;
69 if (drwctx->evil_C != nullptr) {
71 }
72
73 /* TODO: take into account partial redraw for clipping planes. */
74 /* Frustum planes to show only visible pbvh::Tree nodes. */
75 std::array<float4, 6> draw_frustum_planes = View::default_get().frustum_planes_get();
76 /* Transform clipping planes to object space. Transforming a plane with a
77 * 4x4 matrix is done by multiplying with the transpose inverse.
78 * The inverse cancels out here since we transform by inverse(obmat). */
79 float4x4 tmat = math::transpose(ob->object_to_world());
80 for (int i : IndexRange(draw_frustum_planes.size())) {
81 draw_frustum_planes[i] = tmat * draw_frustum_planes[i];
82 }
83
84 /* Fast mode to show low poly multires while navigating. */
85 bool fast_mode = false;
86 if (paint && (paint->flags & PAINT_FAST_NAVIGATE)) {
87 fast_mode = navigating;
88 }
89
90 /* Update draw buffers only for visible nodes while painting.
91 * But do update them otherwise so navigating stays smooth. */
92 bool update_only_visible = rv3d && !(rv3d->rflag & RV3D_PAINTING);
93 if (paint && (paint->flags & PAINT_SCULPT_DELAY_UPDATES)) {
94 update_only_visible = true;
95 }
96
98
99 pbvh::DrawCache &draw_data = pbvh::ensure_draw_data(pbvh->draw_data);
100
101 IndexMaskMemory memory;
102 const IndexMask visible_nodes = bke::pbvh::search_nodes(
103 *pbvh, memory, [&](const bke::pbvh::Node &node) {
105 bke::pbvh::node_frustum_contain_aabb(node, draw_frustum_planes);
106 });
107
108 const IndexMask nodes_to_update = update_only_visible ? visible_nodes :
110
111 Span<gpu::Batch *> batches;
112 if (use_wire) {
113 batches = draw_data.ensure_lines_batches(*ob, {{}, fast_mode}, nodes_to_update);
114 }
115 else {
116 batches = draw_data.ensure_tris_batches(*ob, {attrs, fast_mode}, nodes_to_update);
117 }
118
119 const Span<int> material_indices = draw_data.ensure_material_indices(*ob);
120
121 const int max_material = std::max(0, BKE_object_material_count_eval(ob) - 1);
122 Vector<SculptBatch> result_batches(visible_nodes.size());
123 visible_nodes.foreach_index([&](const int i, const int pos) {
124 result_batches[pos] = {};
125 result_batches[pos].batch = batches[i];
126 result_batches[pos].material_slot = material_indices.is_empty() ?
127 0 :
128 std::clamp(material_indices[i], 0, max_material);
129 result_batches[pos].debug_index = pos;
130 });
131
132 return result_batches;
133}
134
136{
138
141 if (features & SCULPT_BATCH_MASK) {
143 }
144 if (features & SCULPT_BATCH_FACE_SET) {
146 }
147
149 if (features & SCULPT_BATCH_VERTEX_COLOR) {
150 if (const char *name = mesh->active_color_attribute) {
151 attrs.append(pbvh::GenericRequest(name));
152 }
153 }
154
155 if (features & SCULPT_BATCH_UV) {
156 const StringRef uv_name = mesh->active_uv_map_name();
157 if (!uv_name.is_empty()) {
158 attrs.append(pbvh::GenericRequest(uv_name));
159 }
160 }
161
162 return sculpt_batches_get_ex(ob, features & SCULPT_BATCH_WIREFRAME, attrs);
163}
164
167{
168 BLI_assert(ob->type == OB_MESH);
170
171 VectorSet<std::string> draw_attrs;
172 DRW_MeshCDMask cd_needed;
173 DRW_mesh_get_attributes(*ob, mesh, materials, &draw_attrs, &cd_needed);
174
176
179
180 for (const StringRef name : draw_attrs) {
181 attrs.append(pbvh::GenericRequest(name));
182 }
183
184 for (const StringRef name : cd_needed.uv) {
185 attrs.append(pbvh::GenericRequest(name));
186 }
187
188 return sculpt_batches_get_ex(ob, false, attrs);
189}
190
191} // namespace blender::draw
CustomData interface, see also DNA_customdata_types.h.
General operations, lookup, etc. for materials.
General operations, lookup, etc. for blender objects.
constexpr IndexRange()=default
constexpr bool is_empty() const
Definition BLI_span.hh:264
void append(const T &value)
constexpr bool is_empty() const
static View & default_get()
Definition draw_view.cc:317
std::array< float4, 6 > frustum_planes_get(int view_id=0) const
Definition draw_view.cc:327
virtual Span< int > ensure_material_indices(const Object &object)=0
virtual Span< gpu::Batch * > ensure_lines_batches(const Object &object, const ViewportRequest &request, const IndexMask &nodes_to_update)=0
virtual Span< gpu::Batch * > ensure_tris_batches(const Object &object, const ViewportRequest &request, const IndexMask &nodes_to_update)=0
void foreach_index(Fn &&fn, Mode mode=exec_mode::serial) const
Utilities for rendering attributes.
pbvh::Tree * pbvh_get(Object &object)
IndexMask search_nodes(const Tree &pbvh, IndexMaskMemory &memory, FunctionRef< bool(const Node &)> filter_fn)
Definition pbvh.cc:2556
IndexMask all_leaf_nodes(const Tree &pbvh, IndexMaskMemory &memory)
Definition pbvh.cc:2522
void update_normals_from_eval(Object &object_eval, Tree &pbvh)
Definition pbvh.cc:1186
bool node_frustum_contain_aabb(const Node &node, Span< float4 > frustum_planes)
Definition pbvh.cc:2416
DrawCache & ensure_draw_data(std::unique_ptr< bke::pbvh::DrawCache > &ptr)
Definition draw_pbvh.cc:254
std::string GenericRequest
Definition DRW_pbvh.hh:40
Vector< SculptBatch > sculpt_batches_get(const Object *ob, SculptBatchFeature features)
static Vector< SculptBatch > sculpt_batches_get_ex(const Object *ob, const bool use_wire, const Span< pbvh::AttributeRequest > attrs)
@ SCULPT_BATCH_VERTEX_COLOR
void DRW_mesh_get_attributes(const Object &object, const Mesh &mesh, const Span< const GPUMaterial * > materials, VectorSet< std::string > *r_attrs, DRW_MeshCDMask *r_cd_needed)
Vector< SculptBatch > sculpt_batches_per_material_get(const Object *ob, Span< const GPUMaterial * > materials)
MatBase< T, NumCol, NumRow > transpose(const MatBase< T, NumRow, NumCol > &mat)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
int BKE_object_material_count_eval(const Object *ob)
@ PAINT_SCULPT_DELAY_UPDATES
MatBase< float, 4, 4 > float4x4
T & DRW_object_get_data_for_drawing(const Object &object)
const DRWContext * DRW_context_get()
Paint * BKE_paint_get_active_from_context(const bContext *C)
bool BKE_pbvh_node_fully_hidden_get(const bke::pbvh::Node &node)
Definition pbvh.cc:1625
Mesh * BKE_object_get_original_mesh(const Object *object)
Definition object.cc:4461
VecBase< float, 3 > float3
RegionView3D * rv3d
const bContext * evil_C
bke::ObjectRuntime * runtime
eRegionView3D_Flag rflag
VectorSet< std::string > uv