Blender V5.3
mesh_paint.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2026 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "mesh_paint.hh"
10
11#include "BKE_mesh.h"
12#include "BKE_mesh.hh"
13#include "BKE_object.hh"
14#include "BKE_object_types.hh"
15#include "BKE_paint.hh"
16#include "BKE_paint_types.hh"
17#include "BKE_scene.hh"
18
19#include "BLI_math_geom_c.hh"
20#include "BLI_math_matrix.hh"
22
23#include "DEG_depsgraph.hh"
24
25#include "DNA_mesh_types.h"
26#include "DNA_object_types.h"
27#include "DNA_scene_types.h"
28
29#include "ED_image.hh"
30#include "ED_paint.hh"
31
32#include "sculpt_intern.hh"
33
34#include "../paint_intern.hh"
35
37
38/* -------------------------------------------------------------------- */
41
42static void ensure_valid_pivot(const Object &ob, Paint &paint)
43{
44 bke::PaintRuntime &paint_runtime = *paint.runtime;
46
47 /* Account for the case where no objects are evaluated. */
48 if (!pbvh) {
49 return;
50 }
51
52 /* No valid pivot? Use bounding box center. */
53 if (paint_runtime.average_stroke_counter == 0 || !paint_runtime.last_stroke_valid) {
55 const float3 center = math::midpoint(bounds.min, bounds.max);
56 const float3 location = math::transform_point(ob.object_to_world(), center);
57
59 }
60}
61
62static void init_session(
63 Main &bmain, Depsgraph &depsgraph, Paint &paint, Object &ob, eObjectMode object_mode)
64{
65 BLI_assert(ob.runtime->sculpt_session == nullptr);
66 ob.runtime->sculpt_session = MEM_new<SculptSession>(__func__);
67 ob.runtime->sculpt_session->mode_type = object_mode;
68
70 BKE_scene_graph_evaluated_ensure(&depsgraph, &bmain);
71 BKE_sculptsession_update_for_edit(&depsgraph, &ob, true);
72
74}
75
77 Main &bmain, Depsgraph &depsgraph, Scene &scene, Object &ob, const eObjectMode mode_flag)
78{
79 ob.mode |= mode_flag;
80
82
83 Paint *paint = nullptr;
84 if (mode_flag == OB_MODE_VERTEX_PAINT) {
85 const PaintMode paint_mode = PaintMode::Vertex;
86
87 BKE_paint_init(&bmain, &scene, paint_mode);
88 paint = BKE_paint_get_active_from_paintmode(&scene, paint_mode);
90 }
91 else if (mode_flag == OB_MODE_WEIGHT_PAINT) {
92 const PaintMode paint_mode = PaintMode::Weight;
93
94 BKE_paint_init(&bmain, &scene, paint_mode);
95 paint = BKE_paint_get_active_from_paintmode(&scene, paint_mode);
97 }
98 else if (mode_flag == OB_MODE_SCULPT) {
99 const PaintMode paint_mode = PaintMode::Sculpt;
100
101 BKE_paint_init(&bmain, &scene, paint_mode);
102 paint = BKE_paint_get_active_from_paintmode(&scene, paint_mode);
104 }
105 else if (mode_flag == OB_MODE_TEXTURE_PAINT) {
106 const PaintMode paint_mode = PaintMode::Texture3D;
107
108 BKE_paint_init(&bmain, &scene, paint_mode);
109 paint = BKE_paint_get_active_from_paintmode(&scene, paint_mode);
111 }
112 else {
113 BLI_assert(0);
114 }
115
117
118 if (ob.runtime->sculpt_session) {
119 MEM_delete(ob.runtime->sculpt_session->cache);
120 ob.runtime->sculpt_session->cache = nullptr;
122 }
123
124 BLI_assert(paint != nullptr);
125 init_session(bmain, depsgraph, *paint, ob, mode_flag);
126}
127
128void mode_exit_generic(Scene &scene, Object &ob, const eObjectMode mode_flag)
129{
131 ob.mode &= ~mode_flag;
132
134 if (mesh->editflag & ME_EDIT_PAINT_FACE_SEL) {
136 }
137 else if (mesh->editflag & ME_EDIT_PAINT_VERT_SEL) {
139 }
140 }
141
142 /* If the cache is not released by a cancel or a done, free it now. */
143 if (ob.runtime->sculpt_session) {
144 MEM_delete(ob.runtime->sculpt_session->cache);
145 ob.runtime->sculpt_session->cache = nullptr;
146 }
147
149
150 Paint *paint = nullptr;
151 if (mode_flag == OB_MODE_VERTEX_PAINT) {
153 }
154 else if (mode_flag == OB_MODE_WEIGHT_PAINT) {
156 }
157 else if (mode_flag == OB_MODE_SCULPT) {
159 }
160 else if (mode_flag == OB_MODE_TEXTURE_PAINT) {
162 }
163 else {
164 BLI_assert(0);
165 }
166
167 if (paint) {
169 }
170
171 /* Never leave derived meshes behind. */
173
174 /* Flush object mode. */
176}
177
179/* -------------------------------------------------------------------- */
182
183static void do_radial_symmetry(const Depsgraph &depsgraph,
184 const Scene &scene,
185 const Paint & /*paint*/,
186 const Brush &brush,
187 Object &object,
188 const BrushActionFn action_fn,
189 PaintModeData *paint_mode_data,
190 const ePaintSymmetryFlags symm,
191 const int axis)
192
193{
194 SculptSession &ss = *object.runtime->sculpt_session;
195 const Mesh &mesh = *id_cast<Mesh *>(object.data);
196
197 for (int i = 1; i < mesh.radial_symmetry[axis - 'X']; i++) {
198 const float angle = 2.0f * M_PI * i / mesh.radial_symmetry[axis - 'X'];
200 cache_calc_brushdata_symm(*ss.cache, symm, axis, angle);
201 action_fn(depsgraph, scene, brush, object, paint_mode_data);
202 }
203}
204
205void do_symmetrical_brush_actions(const Depsgraph &depsgraph,
206 const Scene &scene,
207 const Paint &paint,
208 Object &object,
209 const BrushActionFn action_fn,
210 PaintModeData *paint_mode_data)
211{
213 SculptSession &ss = *object.runtime->sculpt_session;
214 StrokeCache &cache = *ss.cache;
215 const ePaintSymmetryFlags symm = mesh_symmetry_xyz_get(object);
216
217 cache.bstrength = cache.base_brush_strength;
218
219 /* `symm` is a bit combination of XYZ -
220 * 1 is mirror X; 2 is Y; 3 is XY; 4 is Z; 5 is XZ; 6 is YZ; 7 is XYZ */
221 for (int i = 0; i <= symm; i++) {
222 if (!is_symmetry_iteration_valid(i, symm)) {
223 continue;
224 }
225 const ePaintSymmetryFlags symm_pass = ePaintSymmetryFlags(i);
226 cache.mirror_symmetry_pass = symm_pass;
227 cache.radial_symmetry_pass = 0;
228
229 cache_calc_brushdata_symm(cache, symm_pass, 0, 0);
230 action_fn(depsgraph, scene, brush, object, paint_mode_data);
231
233 depsgraph, scene, paint, brush, object, action_fn, paint_mode_data, symm_pass, 'X');
235 depsgraph, scene, paint, brush, object, action_fn, paint_mode_data, symm_pass, 'Y');
237 depsgraph, scene, paint, brush, object, action_fn, paint_mode_data, symm_pass, 'Z');
238 }
239}
240
241static float calc_overlap(const StrokeCache &cache,
242 const ePaintSymmetryFlags symm,
243 const char axis,
244 const float angle)
245{
246 float3 mirror = symmetry_flip(cache.location, symm);
247
248 if (axis != 0) {
249 float mat[3][3];
251 mul_m3_v3(mat, mirror);
252 }
253
254 const float distsq = len_squared_v3v3(mirror, cache.location);
255
256 if (distsq <= 4.0f * (cache.radius_squared)) {
257 return (2.0f * (cache.radius) - sqrtf(distsq)) / (2.0f * (cache.radius));
258 }
259 return 0.0f;
260}
261
263 const StrokeCache &cache,
264 const ePaintSymmetryFlags symm,
265 const char axis)
266{
267 float overlap = 0.0f;
268
269 for (int i = 1; i < mesh.radial_symmetry[axis - 'X']; i++) {
270 const float angle = 2.0f * M_PI * i / mesh.radial_symmetry[axis - 'X'];
271 overlap += calc_overlap(cache, symm, axis, angle);
272 }
273
274 return overlap;
275}
276
278 const ePaintSymmetryFlags symm,
279 const Mesh &mesh,
280 const StrokeCache &cache)
281{
282 if (!(paint.symmetry_flags & PAINT_SYMMETRY_FEATHER)) {
283 return 1.0f;
284 }
285
286 float overlap = 0.0f;
287 for (int i = 0; i <= symm; i++) {
288 if (!is_symmetry_iteration_valid(i, symm)) {
289 continue;
290 }
291
292 overlap += calc_overlap(cache, ePaintSymmetryFlags(i), 0, 0);
293
294 overlap += calc_radial_symmetry_feather(mesh, cache, ePaintSymmetryFlags(i), 'X');
295 overlap += calc_radial_symmetry_feather(mesh, cache, ePaintSymmetryFlags(i), 'Y');
296 overlap += calc_radial_symmetry_feather(mesh, cache, ePaintSymmetryFlags(i), 'Z');
297 }
298 return 1.0f / overlap;
299}
300
301static void do_tiled(const Depsgraph &depsgraph,
302 const Scene &scene,
303 const Paint &paint,
304 const Brush &brush,
305 Object &object,
306 const BrushActionFn action_fn,
307 PaintModeData *paint_mode_data)
308{
309 SculptSession &ss = *object.runtime->sculpt_session;
310 StrokeCache *cache = ss.cache;
311 const float radius = cache->radius;
312 const Bounds<float3> bb = *BKE_object_boundbox_get(&object);
313 const float *bbMin = bb.min;
314 const float *bbMax = bb.max;
315 const float *step = paint.tile_offset;
316
317 /* These are integer locations, for real location: multiply with step and add orgLoc.
318 * So 0,0,0 is at orgLoc. */
319 int start[3];
320 int end[3];
321 int cur[3];
322
323 /* Position of the "prototype" stroke for tiling. */
324 float orgLoc[3];
325 float original_initial_location[3];
326 copy_v3_v3(orgLoc, cache->location_symm);
327 copy_v3_v3(original_initial_location, cache->initial_location_symm);
328
329 for (int dim = 0; dim < 3; dim++) {
330 if ((paint.symmetry_flags & (PAINT_TILE_X << dim)) && step[dim] > 0) {
331 start[dim] = (bbMin[dim] - orgLoc[dim] - radius) / step[dim];
332 end[dim] = (bbMax[dim] - orgLoc[dim] + radius) / step[dim];
333 }
334 else {
335 start[dim] = end[dim] = 0;
336 }
337 }
338
339 /* First do the "un-tiled" position to initialize the stroke for this location. */
340 cache->tile_pass = 0;
341 action_fn(depsgraph, scene, brush, object, paint_mode_data);
342
343 /* Now do it for all the tiles. */
344 copy_v3_v3_int(cur, start);
345 for (cur[0] = start[0]; cur[0] <= end[0]; cur[0]++) {
346 for (cur[1] = start[1]; cur[1] <= end[1]; cur[1]++) {
347 for (cur[2] = start[2]; cur[2] <= end[2]; cur[2]++) {
348 if (!cur[0] && !cur[1] && !cur[2]) {
349 /* Skip tile at orgLoc, this was already handled before all others. */
350 continue;
351 }
352
353 ++cache->tile_pass;
354
355 for (int dim = 0; dim < 3; dim++) {
356 cache->location_symm[dim] = cur[dim] * step[dim] + orgLoc[dim];
357 cache->plane_offset[dim] = cur[dim] * step[dim];
358 cache->initial_location_symm[dim] = cur[dim] * step[dim] +
359 original_initial_location[dim];
360 }
361 action_fn(depsgraph, scene, brush, object, paint_mode_data);
362 }
363 }
364 }
365}
366
367static void do_radial_symmetry_with_tiling(const Depsgraph &depsgraph,
368 const Scene &scene,
369 const Paint &paint,
370 const Brush &brush,
371 Object &object,
372 const BrushActionFn action_fn,
373 PaintModeData *paint_mode_data,
374 const ePaintSymmetryFlags symm,
375 const int axis)
376{
377 SculptSession &ss = *object.runtime->sculpt_session;
378 const Mesh &mesh = *id_cast<Mesh *>(object.data);
379
380 for (int i = 1; i < mesh.radial_symmetry[axis - 'X']; i++) {
381 const float angle = 2.0f * M_PI * i / mesh.radial_symmetry[axis - 'X'];
383 cache_calc_brushdata_symm(*ss.cache, symm, axis, angle);
384 do_tiled(depsgraph, scene, paint, brush, object, action_fn, paint_mode_data);
385 }
386}
387
389 const Scene &scene,
390 const Paint &paint,
391 Object &object,
392 const BrushActionFn action_fn,
393 PaintModeData *paint_mode_data)
394{
396 const Mesh &mesh = *id_cast<Mesh *>(object.data);
397 SculptSession &ss = *object.runtime->sculpt_session;
398 StrokeCache &cache = *ss.cache;
399 const ePaintSymmetryFlags symm = mesh_symmetry_xyz_get(object);
400
401 cache.feather = calc_symmetry_feather(paint, symm, mesh, *ss.cache);
402 cache.bstrength = cache.base_brush_strength * cache.feather;
403
404 /* `symm` is a bit combination of XYZ -
405 * 1 is mirror X; 2 is Y; 3 is XY; 4 is Z; 5 is XZ; 6 is YZ; 7 is XYZ */
406 for (int i = 0; i <= symm; i++) {
407 if (!is_symmetry_iteration_valid(i, symm)) {
408 continue;
409 }
410 const ePaintSymmetryFlags symm_pass = ePaintSymmetryFlags(i);
411 cache.mirror_symmetry_pass = symm_pass;
412 cache.radial_symmetry_pass = 0;
413
414 cache_calc_brushdata_symm(cache, symm_pass, 0, 0);
415 do_tiled(depsgraph, scene, paint, brush, object, action_fn, paint_mode_data);
416
418 depsgraph, scene, paint, brush, object, action_fn, paint_mode_data, symm_pass, 'X');
420 depsgraph, scene, paint, brush, object, action_fn, paint_mode_data, symm_pass, 'Y');
422 depsgraph, scene, paint, brush, object, action_fn, paint_mode_data, symm_pass, 'Z');
423 }
424}
425
427/* -------------------------------------------------------------------- */
430
432 ViewContext &vc, const Paint &paint, const Brush &brush, Object &object, const float2 mval)
433{
434 SculptSession &ss = *object.runtime->sculpt_session;
435 StrokeCache *cache = ss.cache;
436
437 cache->initial_mouse = mval;
438 cache->mouse = cache->initial_mouse;
439 cache->mouse_event = cache->initial_mouse;
440
441 /* Truly temporary data that isn't stored in properties. */
442 cache->vc = &vc;
443 cache->brush = &brush;
444 cache->paint = &paint;
445 cache->first_time = true;
446
447 ED_view3d_init_mats_rv3d(&object, cache->vc->rv3d);
448 /* Cache projection matrix. */
449 cache->projection_mat = ED_view3d_ob_project_mat_get(cache->vc->rv3d, &object);
450
451 const float3 z_axis(0.0f, 0.0f, 1.0f);
452 object.runtime->world_to_object = math::invert(object.object_to_world());
454 object.world_to_object() * float4x4(cache->vc->rv3d->viewinv), z_axis));
455
459 cache->initial_normal = ss.cursor_sampled_normal.value_or(ss.cursor_normal);
460}
461
463/* -------------------------------------------------------------------- */
466
468 const Brush &brush,
469 IndexMaskMemory &memory,
470 FunctionRef<bool(const bke::pbvh::Node &)> node_ignore_fn)
471{
474
475 /* Build a list of all nodes that are potentially within the brush's area of influence */
476 switch (brush.falloff_shape) {
478 return bke::pbvh::search_nodes(pbvh, memory, [&](const bke::pbvh::Node &node) {
479 if (node_ignore_fn(node)) {
480 return false;
481 }
483 });
487 return bke::pbvh::search_nodes(pbvh, memory, [&](const bke::pbvh::Node &node) {
488 if (node_ignore_fn(node)) {
489 return false;
490 }
491 return node_in_cylinder(ray_dist_precalc, node, ss.cache->radius_squared, true);
492 });
493 }
495 return {};
496}
497
499
500} // namespace blender::ed::sculpt_paint
General operations, lookup, etc. for blender objects.
#define M_PI
Object is a sort of wrapper for general info.
#define BLI_assert_unreachable()
#define ELEM(...)
SIMD_FORCE_INLINE btScalar angle(const btVector3 &v) const
Return the angle between this and another vector.
Definition btVector3.h:356
VecBase< float, D > step(VecOp< float, D >, VecOp< float, D >) RET
pbvh::Tree * pbvh_get(Object &object)
void stroke_set_location(Paint &paint, float3 location)
void cursor_reinitialize_textures(Paint &paint)
IndexMask search_nodes(const Tree &pbvh, IndexMaskMemory &memory, FunctionRef< bool(const Node &)> filter_fn)
Definition pbvh.cc:2556
Bounds< float3 > bounds_get(const Tree &pbvh)
Definition pbvh.cc:1590
void mesh_select_vert_flush(Mesh &mesh)
void mesh_select_face_flush(Mesh &mesh)
void mode_exit_generic(Scene &scene, Object &ob, const eObjectMode mode_flag)
IndexMask gather_brush_nodes(const Object &ob, const Brush &brush, IndexMaskMemory &memory, FunctionRef< bool(const bke::pbvh::Node &)> node_ignore_fn)
static float calc_symmetry_feather(const Paint &paint, const ePaintSymmetryFlags symm, const Mesh &mesh, const StrokeCache &cache)
static void ensure_valid_pivot(const Object &ob, Paint &paint)
Definition mesh_paint.cc:42
void mode_enter_generic(Main &bmain, Depsgraph &depsgraph, Scene &scene, Object &ob, const eObjectMode mode_flag)
Definition mesh_paint.cc:76
bool node_in_sphere(const bke::pbvh::Node &node, const float3 &location, const float radius_sq, const bool original)
Definition sculpt.cc:2511
bool node_in_cylinder(const DistRayAABB_Precalc &ray_dist_precalc, const bke::pbvh::Node &node, const float radius_sq, const bool original)
Definition sculpt.cc:2521
bool brush_cursor_poll(bContext *C)
Definition sculpt.cc:3855
float3 symmetry_flip(const float3 &src, const ePaintSymmetryFlags symm)
static void do_radial_symmetry(const Depsgraph &depsgraph, const Scene &scene, const Paint &, const Brush &brush, Object &object, const BrushActionFn action_fn, PaintModeData *paint_mode_data, const ePaintSymmetryFlags symm, const int axis)
void do_symmetrical_brush_actions(const Depsgraph &depsgraph, const Scene &scene, const Paint &paint, Object &object, const BrushActionFn action_fn, PaintModeData *paint_mode_data)
ePaintSymmetryFlags mesh_symmetry_xyz_get(const Object &object)
Definition sculpt.cc:192
static void init_session(Main &bmain, Depsgraph &depsgraph, Paint &paint, Object &ob, eObjectMode object_mode)
Definition mesh_paint.cc:62
void do_symmetrical_brush_actions_with_tiling_and_feathering(const Depsgraph &depsgraph, const Scene &scene, const Paint &paint, Object &object, const BrushActionFn action_fn, PaintModeData *paint_mode_data)
void stroke_cache_common_init(ViewContext &vc, const Paint &paint, const Brush &brush, Object &object, const float2 mval)
static void do_tiled(const Depsgraph &depsgraph, const Scene &scene, const Paint &paint, const Brush &brush, Object &object, const BrushActionFn action_fn, PaintModeData *paint_mode_data)
static float calc_radial_symmetry_feather(const Mesh &mesh, const StrokeCache &cache, const ePaintSymmetryFlags symm, const char axis)
FunctionRef< void(const Depsgraph &depsgraph, const Scene &scene, const Brush &brush, Object &ob, PaintModeData *mode_data)> BrushActionFn
Definition mesh_paint.hh:44
static void do_radial_symmetry_with_tiling(const Depsgraph &depsgraph, const Scene &scene, const Paint &paint, const Brush &brush, Object &object, const BrushActionFn action_fn, PaintModeData *paint_mode_data, const ePaintSymmetryFlags symm, const int axis)
static float calc_overlap(const StrokeCache &cache, const ePaintSymmetryFlags symm, const char axis, const float angle)
void cache_calc_brushdata_symm(StrokeCache &cache, const ePaintSymmetryFlags symm, const char axis, const float angle)
Definition sculpt.cc:3725
bool is_symmetry_iteration_valid(const char i, const char symm)
CartesianBasis invert(const CartesianBasis &basis)
T midpoint(const T &a, const T &b)
MatBase< T, NumCol, NumRow > normalize(const MatBase< T, NumCol, NumRow > &a)
VecBase< T, 3 > transform_direction(const MatBase< T, 3, 3 > &mat, const VecBase< T, 3 > &direction)
VecBase< T, 3 > transform_point(const CartesianBasis &basis, const VecBase< T, 3 > &v)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
MINLINE float len_squared_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
void mul_m3_v3(const float M[3][3], float r[3])
void BKE_sculptsession_free(Object *ob)
MINLINE void copy_v3_v3(float r[3], const float a[3])
bool weight_paint_poll(bContext *C)
float4x4 ED_view3d_ob_project_mat_get(const RegionView3D *rv3d, const Object *ob)
@ PAINT_FALLOFF_SHAPE_SPHERE
@ PAINT_FALLOFF_SHAPE_TUBE
MINLINE void copy_v3_v3_int(int r[3], const int a[3])
struct DistRayAABB_Precalc dist_squared_ray_to_aabb_v3_precalc(const float ray_origin[3], const float ray_direction[3])
Definition math_geom.cc:703
MatBase< float, 4, 4 > float4x4
void BKE_scene_graph_evaluated_ensure(Depsgraph *depsgraph, Main *bmain)
Definition scene.cc:2872
void ED_paint_cursor_start(Paint *paint, bool(*poll)(bContext *C))
VecBase< float, 2 > float2
@ ME_EDIT_PAINT_VERT_SEL
@ ME_EDIT_PAINT_FACE_SEL
void BKE_object_free_derived_caches(Object *ob)
Definition object.cc:1810
void DEG_id_tag_update(ID *id, unsigned int flags)
@ OB_MODE_TEXTURE_PAINT
BMesh const char void * data
Mesh * BKE_mesh_from_object(Object *ob)
void BKE_paint_init(Main *bmain, Scene *sce, PaintMode mode, bool ensure_brushes=true)
bool vertex_paint_poll(bContext *C)
void ED_view3d_init_mats_rv3d(const Object *ob, RegionView3D *rv3d)
const Brush * BKE_paint_brush_for_read(const Paint *paint)
Paint * BKE_paint_get_active_from_paintmode(Scene *sce, PaintMode mode)
@ ID_RECALC_SYNC_TO_EVAL
Definition DNA_ID.h:1118
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:1074
bool ED_image_tools_paint_poll(bContext *C)
void BKE_sculptsession_update_for_edit(Depsgraph *depsgraph, Object *ob_orig, bool is_paint_tool)
std::optional< Bounds< float3 > > BKE_object_boundbox_get(const Object *ob)
Definition object.cc:3726
VecBase< float, 3 > float3
void BKE_paint_brushes_validate(Main *bmain, Paint *paint)
@ PAINT_SYMMETRY_FEATHER
void axis_angle_to_mat3_single(float R[3][3], char axis, float angle)
#define sqrtf
bke::ObjectRuntime * runtime
ed::sculpt_paint::StrokeCache * cache
Definition BKE_paint.hh:409
std::optional< float3 > cursor_sampled_normal
Definition BKE_paint.hh:424
RegionView3D * rv3d
Definition ED_view3d.hh:86