Blender V5.3
blenkernel/intern/paint.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2009 by Nicholas Bishop. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9/* ALlow using deprecated color for sync legacy. */
10#define DNA_DEPRECATED_ALLOW
11
12#include <algorithm>
13#include <cstdlib>
14#include <cstring>
15#include <optional>
16
17#include "MEM_guardedalloc.h"
18
19#include "DNA_asset_types.h"
20#include "DNA_brush_types.h"
21#include "DNA_key_types.h"
22#include "DNA_mesh_types.h"
23#include "DNA_meshdata_types.h"
24#include "DNA_modifier_types.h"
25#include "DNA_object_enums.h"
26#include "DNA_object_types.h"
27#include "DNA_scene_types.h"
28#include "DNA_space_types.h"
29#include "DNA_userdef_types.h"
30#include "DNA_view3d_types.h"
31#include "DNA_workspace_types.h"
32
33#include "BLI_hash_c.hh"
34#include "BLI_listbase.hh"
35#include "BLI_math_color_c.hh"
36#include "BLI_math_matrix.hh"
37#include "BLI_math_vector_c.hh"
38#include "BLI_noise.hh"
39#include "BLI_string.hh"
40#include "BLI_utildefines.hh"
41#include "BLI_vector.hh"
42
43#include "BLT_translation.hh"
44
45#include "BKE_asset.hh"
46#include "BKE_asset_edit.hh"
47#include "BKE_attribute.h"
48#include "BKE_attribute.hh"
49#include "BKE_brush.hh"
50#include "BKE_ccg.hh"
51#include "BKE_colortools.hh"
52#include "BKE_context.hh"
53#include "BKE_crazyspace.hh"
54#include "BKE_deform.hh"
55#include "BKE_idtype.hh"
56#include "BKE_image.hh"
57#include "BKE_key.hh"
58#include "BKE_layer.hh"
59#include "BKE_lib_id.hh"
60#include "BKE_main.hh"
61#include "BKE_material.hh"
62#include "BKE_mesh.hh"
63#include "BKE_mesh_runtime.hh"
64#include "BKE_modifier.hh"
65#include "BKE_multires.hh"
66#include "BKE_object.hh"
67#include "BKE_paint.hh"
68#include "BKE_paint_bvh.hh"
69#include "BKE_paint_types.hh"
70#include "BKE_scene.hh"
71#include "BKE_subdiv_ccg.hh"
72
73#include "DEG_depsgraph.hh"
75
76#include "GPU_texture.hh"
77
78#include "PRF_profile.hh"
79
80#include "RNA_enum_types.hh"
81
82#include "BLO_read_write.hh"
83
85
86#include "WM_api.hh"
87
88#include "bmesh.hh"
89
90namespace blender {
91
92using bke::AttrDomain;
93
94static void palette_init_data(ID *id)
95{
96 Palette *palette = id_cast<Palette *>(id);
97
98 INIT_DEFAULT_STRUCT_AFTER(palette, id);
99
100 /* Enable fake user by default. */
101 id_fake_user_set(&palette->id);
102}
103
104static void palette_copy_data(Main * /*bmain*/,
105 std::optional<Library *> /*owner_library*/,
106 ID *id_dst,
107 const ID *id_src,
108 const int /*flag*/)
109{
110 Palette *palette_dst = id_cast<Palette *>(id_dst);
111 const Palette *palette_src = id_cast<const Palette *>(id_src);
112
113 BLI_duplicatelist(&palette_dst->colors, &palette_src->colors);
114}
115
116static void palette_free_data(ID *id)
117{
118 Palette *palette = id_cast<Palette *>(id);
119
120 palette->colors.free_no_destruct();
121}
122
125{
126 Palette *palette = id_cast<Palette *>(id);
127
128 for (PaletteColor &color : palette->colors) {
129 fn.single(color.color);
131 }
132}
133
134static void palette_blend_write(BlendWriter *writer, ID *id, const void *id_address)
135{
136 Palette *palette = id_cast<Palette *>(id);
137
138 writer->write_id_struct(id_address, palette);
139 BKE_id_blend_write(writer, &palette->id);
140
141 writer->write_struct_list(&palette->colors);
142}
143
145{
146 Palette *palette = id_cast<Palette *>(id);
147 BLO_read_struct_list(reader, PaletteColor, &palette->colors);
148}
149
150static void palette_undo_preserve(BlendLibReader * /*reader*/, ID *id_new, ID *id_old)
151{
152 /* Whole Palette is preserved across undo-steps, and it has no extra pointer, simple. */
153 /* NOTE: We do not care about potential internal references to self here, Palette has none. */
154 /* NOTE: We do not swap IDProperties, as dealing with potential ID pointers in those would be
155 * fairly delicate. */
156 BKE_lib_id_swap(nullptr, id_new, id_old, false, 0);
157 std::swap(id_new->properties, id_old->properties);
158 std::swap(id_new->system_properties, id_old->system_properties);
159}
160
162 .id_code = Palette::id_type,
163 .id_filter = FILTER_ID_PAL,
164 .dependencies_id_types = 0,
165 .main_listbase_index = INDEX_ID_PAL,
166 .struct_size = sizeof(Palette),
167 .name = "Palette",
168 .name_plural = N_("palettes"),
169 .translation_context = BLT_I18NCONTEXT_ID_PALETTE,
171 .asset_type_info = nullptr,
172
173 .init_data = palette_init_data,
174 .copy_data = palette_copy_data,
175 .free_data = palette_free_data,
176 .make_local = nullptr,
177 .foreach_id = nullptr,
178 .foreach_cache = nullptr,
179 .foreach_path = nullptr,
180 .foreach_working_space_color = palette_foreach_working_space_color,
181 .foreach_asset_weak_reference = nullptr,
182 .owner_pointer_get = nullptr,
183
184 .blend_write = palette_blend_write,
185 .blend_read_data = palette_blend_read_data,
186 .blend_read_after_liblink = nullptr,
187
188 .blend_read_undo_preserve = palette_undo_preserve,
189
190 .lib_override_apply_post = nullptr,
191};
192
193static void paint_curve_copy_data(Main * /*bmain*/,
194 std::optional<Library *> /*owner_library*/,
195 ID *id_dst,
196 const ID *id_src,
197 const int /*flag*/)
198{
199 PaintCurve *paint_curve_dst = id_cast<PaintCurve *>(id_dst);
200 const PaintCurve *paint_curve_src = id_cast<const PaintCurve *>(id_src);
201
202 if (paint_curve_src->tot_points != 0) {
203 paint_curve_dst->points = static_cast<PaintCurvePoint *>(
204 MEM_dupalloc(paint_curve_src->points));
205 }
206}
207
208static void paint_curve_free_data(ID *id)
209{
210 PaintCurve *paint_curve = id_cast<PaintCurve *>(id);
211
212 MEM_SAFE_DELETE(paint_curve->points);
213 paint_curve->tot_points = 0;
214}
215
216static void paint_curve_blend_write(BlendWriter *writer, ID *id, const void *id_address)
217{
218 PaintCurve *pc = id_cast<PaintCurve *>(id);
219
220 writer->write_id_struct(id_address, pc);
221 BKE_id_blend_write(writer, &pc->id);
222
223 writer->write_struct_array(pc->tot_points, pc->points);
224}
225
227{
228 PaintCurve *pc = id_cast<PaintCurve *>(id);
230}
231
233 .id_code = PaintCurve::id_type,
234 .id_filter = FILTER_ID_PC,
235 .dependencies_id_types = 0,
236 .main_listbase_index = INDEX_ID_PC,
237 .struct_size = sizeof(PaintCurve),
238 .name = "PaintCurve",
239 .name_plural = N_("paint_curves"),
240 .translation_context = BLT_I18NCONTEXT_ID_PAINTCURVE,
242 .asset_type_info = nullptr,
243
244 .init_data = nullptr,
245 .copy_data = paint_curve_copy_data,
246 .free_data = paint_curve_free_data,
247 .make_local = nullptr,
248 .foreach_id = nullptr,
249 .foreach_cache = nullptr,
250 .foreach_path = nullptr,
251 .foreach_working_space_color = nullptr,
252 .foreach_asset_weak_reference = nullptr,
253 .owner_pointer_get = nullptr,
254
255 .blend_write = paint_curve_blend_write,
256 .blend_read_data = paint_curve_blend_read_data,
257 .blend_read_after_liblink = nullptr,
258
259 .blend_read_undo_preserve = nullptr,
260
261 .lib_override_apply_post = nullptr,
262};
263
264namespace bke::paint {
265
266void invalidate_overlay_tex(Scene &scene, const Tex *tex)
267{
269 const Brush *br = BKE_paint_brush_for_read(&paint);
270 if (!br) {
271 return;
272 }
273
274 if (br->mtex.tex == tex) {
275 paint.runtime->overlay_flags |= eOverlayControlFlags::InvalidTexturePrimary;
276 }
277 if (br->mask_mtex.tex == tex) {
278 paint.runtime->overlay_flags |= eOverlayControlFlags::InvalidTextureSecondary;
279 }
280 });
281}
282
284{
286 const Brush *br = BKE_paint_brush_for_read(&paint);
287 if (br && br->curve_distance_falloff == curve) {
288 paint.runtime->overlay_flags |= eOverlayControlFlags::InvalidCurve;
289 }
290 });
291}
292
294{
296 if (!br) {
297 return;
298 }
299 paint.runtime->overlay_flags |= eOverlayControlFlags::InvalidMask;
300}
301
303{
305 [&](Paint &paint) { invalidate_overlay_all(paint); });
306}
307
309{
310 return paint.runtime->overlay_flags;
311}
312
314{
315 if (flags & BRUSH_OVERLAY_OVERRIDE_MASK) {
317 paint.runtime->overlay_flags |= eOverlayControlFlags::OverrideCursor;
318 }
320 paint.runtime->overlay_flags |= eOverlayControlFlags::OverridePrimary;
321 }
323 paint.runtime->overlay_flags |= eOverlayControlFlags::OverrideSecondary;
324 }
325 }
326 else {
327 paint.runtime->overlay_flags &= ~eOverlayControlFlags::OverrideMask;
328 }
329}
330
332{
333 paint.runtime->overlay_flags &= ~(flag);
334}
335
337{
338 if (this->overlay_texture) {
340 }
341}
342
349
351{
352 paint.runtime->primary_snap = std::make_unique<TexSnapshot>();
353 paint.runtime->secondary_snap = std::make_unique<TexSnapshot>();
354 paint.runtime->cursor_snap = std::make_unique<CursorSnapshot>();
355
357}
358
359} // namespace bke::paint
360
362{
363 ToolSettings *ts = sce->toolsettings;
364 Paint **paint_ptr = nullptr;
365 /* Some paint modes don't store paint settings as pointer, for these this can be set and
366 * referenced by paint_ptr. */
367 Paint *paint_tmp = nullptr;
368
369 switch (mode) {
371 paint_ptr = reinterpret_cast<Paint **>(&ts->sculpt);
372 break;
374 paint_ptr = reinterpret_cast<Paint **>(&ts->vpaint);
375 break;
377 paint_ptr = reinterpret_cast<Paint **>(&ts->wpaint);
378 break;
381 paint_tmp = reinterpret_cast<Paint *>(&ts->imapaint);
382 paint_ptr = &paint_tmp;
383 break;
385 paint_ptr = reinterpret_cast<Paint **>(&ts->gp_paint);
386 break;
388 paint_ptr = reinterpret_cast<Paint **>(&ts->gp_vertexpaint);
389 break;
391 paint_ptr = reinterpret_cast<Paint **>(&ts->gp_sculptpaint);
392 break;
394 paint_ptr = reinterpret_cast<Paint **>(&ts->gp_weightpaint);
395 break;
397 paint_ptr = reinterpret_cast<Paint **>(&ts->curves_sculpt);
398 break;
400 break;
401 }
402 if (paint_ptr) {
403 BKE_paint_ensure(ts, paint_ptr);
404 return true;
405 }
406 return false;
407}
408
410{
411 if (sce) {
412 ToolSettings *ts = sce->toolsettings;
413
414 switch (mode) {
416 return &ts->sculpt->paint;
418 return &ts->vpaint->paint;
420 return &ts->wpaint->paint;
423 return &ts->imapaint.paint;
425 return &ts->gp_paint->paint;
427 return &ts->gp_vertexpaint->paint;
429 return &ts->gp_sculptpaint->paint;
431 return &ts->gp_weightpaint->paint;
433 return &ts->curves_sculpt->paint;
435 return nullptr;
436 default:
437 return &ts->imapaint.paint;
438 }
439 }
440
441 return nullptr;
442}
443
471
472Paint *BKE_paint_get_active(const Main &bmain, Scene *sce, ViewLayer *view_layer)
473{
474 if (sce && view_layer) {
475 ToolSettings *ts = sce->toolsettings;
476 BKE_view_layer_synced_ensure(bmain, sce, view_layer);
477 Object *actob = BKE_view_layer_active_object_get(view_layer);
478
479 if (actob) {
480 switch (actob->mode) {
481 case OB_MODE_SCULPT:
482 return &ts->sculpt->paint;
484 return &ts->vpaint->paint;
486 return &ts->wpaint->paint;
488 return &ts->imapaint.paint;
490 return &ts->gp_paint->paint;
492 return &ts->gp_vertexpaint->paint;
494 return &ts->gp_sculptpaint->paint;
496 return &ts->gp_weightpaint->paint;
498 return &ts->curves_sculpt->paint;
499 default:
500 break;
501 }
502 }
503
504 /* default to image paint */
505 return &ts->imapaint.paint;
506 }
507
508 return nullptr;
509}
510
512{
513 const Main *bmain = CTX_data_main(C);
514 Scene *sce = CTX_data_scene(C);
515 ViewLayer *view_layer = CTX_data_view_layer(C);
516
517 if (sce && view_layer) {
518 ToolSettings *ts = sce->toolsettings;
519 BKE_view_layer_synced_ensure(*bmain, sce, view_layer);
520 Object *obact = BKE_view_layer_active_object_get(view_layer);
521
523 if (sima != nullptr) {
524 if (obact && obact->mode == OB_MODE_EDIT) {
525 if (sima->mode == SI_MODE_PAINT) {
526 return &ts->imapaint.paint;
527 }
528 }
529 else {
530 return &ts->imapaint.paint;
531 }
532 }
533 else {
534 return BKE_paint_get_active(*bmain, sce, view_layer);
535 }
536 }
537
538 return nullptr;
539}
540
542{
543 const Main *bmain = CTX_data_main(C);
544 Scene *sce = CTX_data_scene(C);
545 ViewLayer *view_layer = CTX_data_view_layer(C);
546
547 if (sce && view_layer) {
548 BKE_view_layer_synced_ensure(*bmain, sce, view_layer);
549 Object *obact = BKE_view_layer_active_object_get(view_layer);
550
552 if (sima != nullptr) {
553 if (obact && obact->mode == OB_MODE_EDIT) {
554 if (sima->mode == SI_MODE_PAINT) {
556 }
557 }
558 else {
560 }
561 }
562 else if (obact) {
563 switch (obact->mode) {
564 case OB_MODE_SCULPT:
565 return PaintMode::Sculpt;
567 if (obact->type == OB_GREASE_PENCIL) {
569 }
570 return PaintMode::Invalid;
572 return PaintMode::GPencil;
578 return PaintMode::Vertex;
580 return PaintMode::Weight;
585 default:
587 }
588 }
589 else {
590 /* default to image paint */
592 }
593 }
594
595 return PaintMode::Invalid;
596}
597
637
643{
644 /* Don't resolve this during file read, it will be done after. */
645 if (bmain->is_locked_for_linking) {
646 return false;
647 }
648 /* Attempt to restore a valid active brush from brush asset information. */
649 if (paint->brush != nullptr) {
650 return false;
651 }
652 if (paint->brush_asset_reference == nullptr) {
653 return false;
654 }
655
656 Brush *brush = reinterpret_cast<Brush *>(
657 bke::asset_edit_id_from_weak_reference(*bmain, ID_BR, *paint->brush_asset_reference));
659
660 /* Ensure we have a brush with appropriate mode to assign.
661 * Could happen if contents of asset blend was manually changed. */
662 if (brush == nullptr || (paint->runtime->ob_mode & brush->ob_mode) == 0) {
663 MEM_delete(paint->brush_asset_reference);
664 paint->brush_asset_reference = nullptr;
665 return false;
666 }
667
668 paint->brush = brush;
669 return true;
670}
671
673{
674 return paint ? paint->brush : nullptr;
675}
676
678{
679 return paint ? paint->brush : nullptr;
680}
681
683{
684 if (paint == nullptr) {
685 return false;
686 }
687 return !brush || (paint->runtime->ob_mode & brush->ob_mode) != 0;
688}
689
691{
692 if (std::optional<AssetWeakReference> weak_ref = bke::asset_edit_weak_reference_from_id(
693 brush->id))
694 {
695 return MEM_new<AssetWeakReference>(__func__, *weak_ref);
696 }
697
698 return nullptr;
699}
700
702 Paint *paint,
703 const AssetWeakReference &brush_asset_reference)
704{
705 /* Don't resolve this during file read, it will be done after. */
706 if (bmain->is_locked_for_linking) {
707 return false;
708 }
709
710 Brush *brush = reinterpret_cast<Brush *>(
711 bke::asset_edit_id_from_weak_reference(*bmain, ID_BR, brush_asset_reference));
712 BLI_assert(brush == nullptr || !ID_IS_LINKED(brush) ||
714
715 /* Ensure we have a brush with appropriate mode to assign.
716 * Could happen if contents of asset blend were manually changed. */
717 if (brush == nullptr || !BKE_paint_can_use_brush(paint, brush)) {
718 return false;
719 }
720
721 /* Update the brush itself. */
722 paint->brush = brush;
723 /* Update the brush asset reference. */
724 {
725 MEM_delete(paint->brush_asset_reference);
726 paint->brush_asset_reference = nullptr;
727 if (brush != nullptr) {
728 BLI_assert(bke::asset_edit_weak_reference_from_id(brush->id) == brush_asset_reference);
729 paint->brush_asset_reference = MEM_new<AssetWeakReference>(__func__, brush_asset_reference);
731 }
732 }
733
734 return true;
735}
736
738{
740 return false;
741 }
742
743 paint->brush = brush;
744
745 MEM_delete(paint->brush_asset_reference);
746 paint->brush_asset_reference = nullptr;
747 if (brush != nullptr) {
748 paint->brush_asset_reference = asset_reference_create_from_brush(brush);
749 }
750
752
753 return true;
754}
755
757 const bke::paint::AssetCategory asset_category)
758{
759 switch (asset_category) {
761 return "essentials_brushes-mesh_sculpt.blend";
763 return "essentials_brushes-mesh_vertex.blend";
765 return "essentials_brushes-mesh_weight.blend";
767 return "essentials_brushes-mesh_texture.blend";
769 return "essentials_brushes-gp_draw.blend";
771 return "essentials_brushes-gp_sculpt.blend";
773 return "essentials_brushes-gp_weight.blend";
775 return "essentials_brushes-gp_vertex.blend";
777 return "essentials_brushes-curve_sculpt.blend";
778 default:
779 return nullptr;
780 }
781}
782
784 const char *name, const bke::paint::AssetCategory asset_category)
785{
786 const char *essentials_file_name = paint_brush_essentials_asset_file_name_from_asset_category(
787 asset_category);
788 if (!essentials_file_name) {
789 return nullptr;
790 }
791
792 AssetWeakReference *weak_ref = MEM_new<AssetWeakReference>(__func__);
794 weak_ref->asset_library_identifier = nullptr;
796 "brushes/%s/Brush/%s", essentials_file_name, name);
797 return weak_ref;
798}
799
800static std::optional<AssetWeakReference> paint_brush_asset_reference_from_essentials(
801 const char *name, const bke::paint::AssetCategory asset_category)
802{
803 const char *essentials_file_name = paint_brush_essentials_asset_file_name_from_asset_category(
804 asset_category);
805 if (!essentials_file_name) {
806 return {};
807 }
808
809 AssetWeakReference weak_ref;
811 weak_ref.asset_library_identifier = nullptr;
813 "brushes/%s/Brush/%s", essentials_file_name, name);
814 return weak_ref;
815}
816
818 const bke::paint::AssetCategory asset_category,
819 const char *name)
820{
821 std::optional<AssetWeakReference> weak_ref = paint_brush_asset_reference_from_essentials(
822 name, asset_category);
823 if (!weak_ref) {
824 return nullptr;
825 }
826
827 return reinterpret_cast<Brush *>(
829}
830
831static void paint_brush_set_essentials_reference(Paint *paint, const char *name)
832{
833 /* Set brush asset reference to a named brush in the essentials asset library. */
834 MEM_delete(paint->brush_asset_reference);
835
836 BLI_assert(paint->runtime->initialized);
838 name, paint->runtime->asset_category);
839 paint->brush = nullptr;
840}
841
843 std::optional<int> brush_type,
844 StringRefNull *r_name)
845{
846 const char *name = "";
847
848 switch (asset_category) {
850 name = "Draw";
851 if (brush_type) {
852 switch (eBrushSculptType(*brush_type)) {
854 name = "Mask";
855 break;
857 name = "Face Set Paint";
858 break;
860 name = "Paint Hard";
861 break;
863 name = "Density";
864 break;
866 name = "Erase Multires Displacement";
867 break;
869 name = "Smear Multires Displacement";
870 break;
871 default:
872 break;
873 }
874 }
875 break;
877 name = "Paint Hard";
878 if (brush_type) {
879 switch (eBrushVertexPaintType(*brush_type)) {
881 name = "Blur";
882 break;
884 name = "Average";
885 break;
887 name = "Smear";
888 break;
890 /* Use default, don't override. */
891 break;
892 }
893 }
894 break;
896 name = "Add Weight";
897 if (brush_type) {
898 switch (eBrushWeightPaintType(*brush_type)) {
900 name = "Blur";
901 break;
903 name = "Average";
904 break;
906 name = "Smear";
907 break;
909 /* Use default, don't override. */
910 break;
911 }
912 }
913 break;
915 name = "Paint Hard";
916 if (brush_type) {
917 switch (eBrushImagePaintType(*brush_type)) {
919 name = "Blur";
920 break;
922 name = "Smear";
923 break;
925 name = "Fill";
926 break;
928 name = "Mask";
929 break;
931 name = "Clone";
932 break;
934 break;
935 }
936 }
937 break;
939 name = "Comb";
940 if (brush_type) {
941 switch (eBrushCurvesSculptType(*brush_type)) {
943 name = "Add";
944 break;
946 name = "Delete";
947 break;
949 name = "Density";
950 break;
952 name = "Select";
953 break;
954 default:
955 break;
956 }
957 }
958 break;
960 name = "Pencil";
961 /* Different default brush for some brush types. */
962 if (brush_type) {
963 switch (eBrushGPaintType(*brush_type)) {
965 name = "Eraser Hard";
966 break;
968 name = "Fill";
969 break;
972 /* Use default, don't override. */
973 break;
974 }
975 }
976 break;
978 name = "Paint";
979 if (brush_type) {
980 switch (eBrushGPVertexType(*brush_type)) {
982 name = "Blur";
983 break;
985 name = "Average";
986 break;
988 name = "Smear";
989 break;
991 name = "Replace";
992 break;
994 /* Use default, don't override. */
995 break;
997 /* Unused brush type. */
999 break;
1000 }
1001 }
1002 break;
1004 name = "Smooth";
1005 if (brush_type) {
1006 switch (eBrushGPSculptType(*brush_type)) {
1008 name = "Clone";
1009 break;
1010 default:
1011 break;
1012 }
1013 }
1014 break;
1016 name = "Paint";
1017 if (brush_type) {
1018 switch (eBrushGPWeightType(*brush_type)) {
1020 name = "Blur";
1021 break;
1023 name = "Average";
1024 break;
1026 name = "Smear";
1027 break;
1029 /* Use default, don't override. */
1030 break;
1031 }
1032 }
1033 break;
1034 default:
1036 break;
1037 }
1038
1039 *r_name = name;
1040}
1041
1042std::optional<AssetWeakReference> BKE_paint_brush_type_default_reference(
1043 const bke::paint::AssetCategory asset_category, std::optional<int> brush_type)
1044{
1045 StringRefNull name;
1046
1047 paint_brush_default_essentials_name_get(asset_category, brush_type, &name);
1048 if (name.is_empty()) {
1049 return {};
1050 }
1051
1052 return paint_brush_asset_reference_from_essentials(name.c_str(), asset_category);
1053}
1054
1055static void paint_brush_set_default_reference(Paint *paint, const bool do_regular = true)
1056{
1057 if (!paint->runtime || !paint->runtime->initialized) {
1058 /* Can happen when loading old file where toolsettings are created in versioning, without
1059 * calling #paint_runtime_init(). Will be done later when necessary. */
1060 return;
1061 }
1062
1063 StringRefNull name;
1064
1065 paint_brush_default_essentials_name_get(paint->runtime->asset_category, std::nullopt, &name);
1066
1067 if (do_regular && !name.is_empty()) {
1069 }
1070}
1071
1100
1106
1112
1114 AssetWeakReference &&asset_weak_reference)
1115{
1116 if (!paint->runtime->previous_active_brush_reference) {
1117 paint->runtime->previous_active_brush_reference = MEM_new<AssetWeakReference>(__func__);
1118 }
1119 *paint->runtime->previous_active_brush_reference = asset_weak_reference;
1120}
1121
1123{
1124 MEM_SAFE_DELETE(paint->runtime->previous_active_brush_reference);
1125}
1126
1128 FunctionRef<void(AssetWeakReference &weak_ref)> fn)
1129{
1130 const auto foreach_reference = [&](AssetWeakReference *weak_ref) {
1131 if (weak_ref) {
1132 fn(*weak_ref);
1133 }
1134 };
1135
1136 foreach_reference(paint.brush_asset_reference);
1137 if (paint.runtime) {
1138 foreach_reference(paint.runtime->previous_active_brush_reference);
1139 }
1140
1141 foreach_reference(paint.tool_brush_bindings.main_brush_asset_reference);
1142 for (NamedBrushAssetReference &brush_binding :
1143 paint.tool_brush_bindings.active_brush_per_brush_type)
1144 {
1145 foreach_reference(brush_binding.brush_asset_reference);
1146 }
1147}
1148
1150{
1151 /* Clear brush with invalid mode. Unclear if this can still happen,
1152 * but kept from old paint tool-slots code. */
1154 if (brush && (paint->runtime->ob_mode & brush->ob_mode) == 0) {
1155 BKE_paint_brush_set(paint, nullptr);
1157 }
1158}
1159
1161{
1162 if (!paint->runtime) {
1163 paint->runtime = MEM_new<bke::PaintRuntime>(__func__);
1164 }
1165
1166 if (paint == &ts->imapaint.paint) {
1167 paint->runtime->ob_mode = OB_MODE_TEXTURE_PAINT;
1168 paint->runtime->asset_category = bke::paint::AssetCategory::MeshTexture;
1169 }
1170 else if (ts->sculpt && paint == &ts->sculpt->paint) {
1171 paint->runtime->ob_mode = OB_MODE_SCULPT;
1172 paint->runtime->asset_category = bke::paint::AssetCategory::MeshSculpt;
1173 }
1174 else if (ts->vpaint && paint == &ts->vpaint->paint) {
1175 paint->runtime->ob_mode = OB_MODE_VERTEX_PAINT;
1176 paint->runtime->asset_category = bke::paint::AssetCategory::MeshVertex;
1177 }
1178 else if (ts->wpaint && paint == &ts->wpaint->paint) {
1179 paint->runtime->ob_mode = OB_MODE_WEIGHT_PAINT;
1180 paint->runtime->asset_category = bke::paint::AssetCategory::MeshWeight;
1181 }
1182 else if (ts->gp_paint && paint == &ts->gp_paint->paint) {
1183 paint->runtime->ob_mode = OB_MODE_PAINT_GREASE_PENCIL;
1184 paint->runtime->asset_category = bke::paint::AssetCategory::GPencilDraw;
1185 }
1186 else if (ts->gp_vertexpaint && paint == &ts->gp_vertexpaint->paint) {
1187 paint->runtime->ob_mode = OB_MODE_VERTEX_GREASE_PENCIL;
1188 paint->runtime->asset_category = bke::paint::AssetCategory::GPencilVertex;
1189 }
1190 else if (ts->gp_sculptpaint && paint == &ts->gp_sculptpaint->paint) {
1191 paint->runtime->ob_mode = OB_MODE_SCULPT_GREASE_PENCIL;
1192 paint->runtime->asset_category = bke::paint::AssetCategory::GPencilSculpt;
1193 }
1194 else if (ts->gp_weightpaint && paint == &ts->gp_weightpaint->paint) {
1195 paint->runtime->ob_mode = OB_MODE_WEIGHT_GREASE_PENCIL;
1196 paint->runtime->asset_category = bke::paint::AssetCategory::GPencilWeight;
1197 }
1198 else if (ts->curves_sculpt && paint == &ts->curves_sculpt->paint) {
1199 paint->runtime->ob_mode = OB_MODE_SCULPT_CURVES;
1200 paint->runtime->asset_category = bke::paint::AssetCategory::CurvesSculpt;
1201 }
1202 else {
1204 }
1205
1206 paint->runtime->primary_snap = std::make_unique<bke::paint::TexSnapshot>();
1207 paint->runtime->secondary_snap = std::make_unique<bke::paint::TexSnapshot>();
1208 paint->runtime->cursor_snap = std::make_unique<bke::paint::CursorSnapshot>();
1209 paint->runtime->initialized = true;
1210}
1211
1213 const eObjectMode ob_mode)
1214{
1215 switch (ob_mode) {
1217 case OB_MODE_EDIT:
1218 return brush->image_brush_type;
1219 case OB_MODE_SCULPT:
1220 return brush->sculpt_brush_type;
1222 return brush->vertex_brush_type;
1224 return brush->weight_brush_type;
1226 return brush->gpencil_brush_type;
1228 return brush->gpencil_vertex_brush_type;
1230 return brush->gpencil_sculpt_brush_type;
1232 return brush->gpencil_weight_brush_type;
1234 return brush->curves_sculpt_brush_type;
1235 default:
1236 return {};
1237 }
1238}
1239
1241 const PaintMode mode)
1242{
1243 switch (mode) {
1246 return brush->image_brush_type;
1247 case PaintMode::Sculpt:
1248 return brush->sculpt_brush_type;
1249 case PaintMode::Vertex:
1250 return brush->vertex_brush_type;
1251 case PaintMode::Weight:
1252 return brush->weight_brush_type;
1253 case PaintMode::GPencil:
1254 return brush->gpencil_brush_type;
1256 return brush->gpencil_vertex_brush_type;
1258 return brush->gpencil_sculpt_brush_type;
1260 return brush->gpencil_weight_brush_type;
1262 return brush->curves_sculpt_brush_type;
1263 case PaintMode::Invalid:
1264 default:
1265 return {};
1266 }
1267}
1268
1269PaintCurve *BKE_paint_curve_add(Main *bmain, const char *name)
1270{
1271 PaintCurve *pc = BKE_id_new<PaintCurve>(bmain, name);
1272 return pc;
1273}
1274
1276{
1277 return paint ? paint->palette : nullptr;
1278}
1279
1281{
1282 if (paint) {
1283 id_us_min(id_cast<ID *>(paint->palette));
1284 paint->palette = palette;
1285 id_us_plus(id_cast<ID *>(paint->palette));
1286 }
1287}
1288
1290{
1291 pc->add_index = (add_index || pc->tot_points == 1) ? (add_index + 1) : 0;
1292}
1293
1295{
1296 if (BLI_listbase_count_at_most(&palette->colors, palette->active_color) == palette->active_color)
1297 {
1298 palette->active_color--;
1299 }
1300
1301 BLI_remlink(&palette->colors, color);
1302
1303 if (palette->active_color < 0 && !palette->colors.is_empty()) {
1304 palette->active_color = 0;
1305 }
1306
1307 MEM_delete(color);
1308}
1309
1311{
1312 palette->colors.free_no_destruct();
1313 palette->active_color = 0;
1314}
1315
1316void BKE_palette_color_set(PaletteColor *color, const float rgb[3])
1317{
1318 copy_v3_v3(color->color, rgb);
1320}
1321
1326
1327Palette *BKE_palette_add(Main *bmain, const char *name)
1328{
1329 Palette *palette = BKE_id_new<Palette>(bmain, name);
1330 return palette;
1331}
1332
1334{
1335 PaletteColor *color = MEM_new<PaletteColor>(__func__);
1336 BLI_addtail(&palette->colors, color);
1337 return color;
1338}
1339
1341{
1342 return ((ob != nullptr) && (ob->type == OB_MESH) && (ob->data != nullptr) &&
1343 ((id_cast<Mesh *>(ob->data))->editflag & ME_EDIT_PAINT_FACE_SEL) &&
1345}
1346
1348{
1349 return ((ob != nullptr) && (ob->type == OB_MESH) && (ob->data != nullptr) &&
1350 ((id_cast<Mesh *>(ob->data))->editflag & ME_EDIT_PAINT_VERT_SEL) &&
1352}
1353
1355{
1356 if (ob == nullptr || ob->data == nullptr) {
1357 return false;
1358 }
1359 if (ob->type == OB_GREASE_PENCIL) {
1362 }
1363 return false;
1364}
1365
1371
1373{
1374 return ((ob != nullptr) && (ob->type == OB_MESH) && (ob->data != nullptr) &&
1376}
1377
1379{
1380 CurveMapping *cumap = nullptr;
1381 CurveMap *cuma = nullptr;
1382
1383 if (!paint->cavity_curve) {
1384 paint->cavity_curve = BKE_curvemapping_add(1, 0, 0, 1, 1);
1385 }
1386 cumap = paint->cavity_curve;
1388 cumap->preset = eCurveMappingPreset(preset);
1389
1390 cuma = cumap->cm;
1392 BKE_curvemapping_changed(cumap, false);
1393}
1394
1396{
1397 switch (mode) {
1398 case PaintMode::Sculpt:
1399 return OB_MODE_SCULPT;
1400 case PaintMode::Vertex:
1401 return OB_MODE_VERTEX_PAINT;
1402 case PaintMode::Weight:
1403 return OB_MODE_WEIGHT_PAINT;
1406 return OB_MODE_TEXTURE_PAINT;
1408 return OB_MODE_SCULPT_CURVES;
1409 case PaintMode::GPencil:
1411 case PaintMode::Invalid:
1412 default:
1413 return OB_MODE_OBJECT;
1414 }
1415}
1416
1418{
1419 const UnifiedPaintSettings &default_ups = UnifiedPaintSettings();
1420 paint.unified_paint_settings.size = default_ups.size;
1421 paint.unified_paint_settings.input_samples = default_ups.input_samples;
1422 paint.unified_paint_settings.unprojected_size = default_ups.unprojected_size;
1423 paint.unified_paint_settings.alpha = default_ups.alpha;
1424 paint.unified_paint_settings.weight = default_ups.weight;
1425 paint.unified_paint_settings.flag = default_ups.flag;
1426 if (!paint.unified_paint_settings.curve_rand_hue) {
1427 paint.unified_paint_settings.curve_rand_hue = BKE_paint_default_curve();
1428 }
1429 if (!paint.unified_paint_settings.curve_rand_saturation) {
1430 paint.unified_paint_settings.curve_rand_saturation = BKE_paint_default_curve();
1431 }
1432 if (!paint.unified_paint_settings.curve_rand_value) {
1433 paint.unified_paint_settings.curve_rand_value = BKE_paint_default_curve();
1434 }
1435 copy_v3_v3(paint.unified_paint_settings.color, default_ups.color);
1436 copy_v3_v3(paint.unified_paint_settings.secondary_color, default_ups.secondary_color);
1437}
1438
1440{
1441 Paint *paint = nullptr;
1442 if (*r_paint) {
1443 if (!(*r_paint)->runtime) {
1444 (*r_paint)->runtime = MEM_new<bke::PaintRuntime>(__func__);
1445 }
1446 if (!(*r_paint)->runtime->initialized && *r_paint == reinterpret_cast<Paint *>(&ts->imapaint))
1447 {
1448 paint_runtime_init(ts, *r_paint);
1449 }
1450 else {
1451 BLI_assert(ELEM(*r_paint,
1452 /* Cast is annoying, but prevent nullptr-pointer access. */
1453 (Paint *)ts->gp_paint,
1454 (Paint *)ts->gp_vertexpaint,
1455 (Paint *)ts->gp_sculptpaint,
1456 (Paint *)ts->gp_weightpaint,
1457 (Paint *)ts->sculpt,
1458 (Paint *)ts->vpaint,
1459 (Paint *)ts->wpaint,
1460 (Paint *)ts->curves_sculpt,
1461 (Paint *)&ts->imapaint));
1462#ifndef NDEBUG
1463 Paint paint_test = dna::shallow_copy(**r_paint);
1464 paint_runtime_init(ts, *r_paint);
1465 /* Swap so debug doesn't hide errors when release fails. */
1466 dna::shallow_swap(**r_paint, paint_test);
1467 BLI_assert(paint_test.runtime->ob_mode == (*r_paint)->runtime->ob_mode);
1468#endif
1469 }
1470 return true;
1471 }
1472
1473 if ((reinterpret_cast<VPaint **>(r_paint) == &ts->vpaint) ||
1474 (reinterpret_cast<VPaint **>(r_paint) == &ts->wpaint))
1475 {
1476 VPaint *data = MEM_new<VPaint>(__func__);
1477 paint = &data->paint;
1480 }
1481 else if (reinterpret_cast<Sculpt **>(r_paint) == &ts->sculpt) {
1482 Sculpt *data = MEM_new<Sculpt>(__func__);
1483
1484 paint = &data->paint;
1487 }
1488 else if (reinterpret_cast<GpPaint **>(r_paint) == &ts->gp_paint) {
1489 GpPaint *data = MEM_new<GpPaint>(__func__);
1490 paint = &data->paint;
1492 }
1493 else if (reinterpret_cast<GpVertexPaint **>(r_paint) == &ts->gp_vertexpaint) {
1494 GpVertexPaint *data = MEM_new<GpVertexPaint>(__func__);
1495 paint = &data->paint;
1497 }
1498 else if (reinterpret_cast<GpSculptPaint **>(r_paint) == &ts->gp_sculptpaint) {
1499 GpSculptPaint *data = MEM_new<GpSculptPaint>(__func__);
1500 paint = &data->paint;
1502 }
1503 else if (reinterpret_cast<GpWeightPaint **>(r_paint) == &ts->gp_weightpaint) {
1504 GpWeightPaint *data = MEM_new<GpWeightPaint>(__func__);
1505 paint = &data->paint;
1507 }
1508 else if (reinterpret_cast<CurvesSculpt **>(r_paint) == &ts->curves_sculpt) {
1509 CurvesSculpt *data = MEM_new<CurvesSculpt>(__func__);
1510 paint = &data->paint;
1512 }
1513 else if (*r_paint == &ts->imapaint.paint) {
1514 paint = &ts->imapaint.paint;
1516 }
1517
1518 paint->flags |= PAINT_SHOW_BRUSH;
1519
1520 *r_paint = paint;
1521
1523
1524 return false;
1525}
1526
1528{
1529 if (paint->brush_asset_reference) {
1531 }
1532
1533 if (!paint->brush) {
1535 }
1536}
1537
1539{
1540 if (!paint.mesh_automasking_settings) {
1541 paint.mesh_automasking_settings = MEM_new<MeshAutomaskingSettings>(__func__);
1542 paint.mesh_automasking_settings->cavity_curve = BKE_sculpt_default_cavity_curve();
1543 paint.mesh_automasking_settings->cavity_curve_op = BKE_sculpt_default_cavity_curve();
1544 }
1545}
1546
1547void BKE_paint_init(Main *bmain, Scene *sce, PaintMode mode, const bool ensure_brushes)
1548{
1551
1552 if (ensure_brushes) {
1554 }
1555
1556 if (!paint->cavity_curve) {
1558 }
1559
1562 }
1563}
1564
1566{
1567 BKE_curvemapping_free(paint->cavity_curve);
1568 MEM_delete(paint->brush_asset_reference);
1569 MEM_delete(paint->tool_brush_bindings.main_brush_asset_reference);
1570
1571 for (NamedBrushAssetReference &brush_ref :
1572 paint->tool_brush_bindings.active_brush_per_brush_type.items_mutable())
1573 {
1574 MEM_delete(brush_ref.name);
1575 MEM_delete(brush_ref.brush_asset_reference);
1576 MEM_delete(&brush_ref);
1577 }
1578
1579 BKE_curvemapping_free(paint->unified_paint_settings.curve_rand_hue);
1580 BKE_curvemapping_free(paint->unified_paint_settings.curve_rand_saturation);
1581 BKE_curvemapping_free(paint->unified_paint_settings.curve_rand_value);
1582
1583 if (paint->mesh_automasking_settings) {
1584 BKE_curvemapping_free(paint->mesh_automasking_settings->cavity_curve);
1585 BKE_curvemapping_free(paint->mesh_automasking_settings->cavity_curve_op);
1586 MEM_delete(paint->mesh_automasking_settings);
1587 }
1588
1589 MEM_SAFE_DELETE(paint->runtime);
1590}
1591
1592void BKE_paint_copy(const Paint *src, Paint *dst, const int flag)
1593{
1594 dst->brush = src->brush;
1596
1597 if (src->brush_asset_reference) {
1598 dst->brush_asset_reference = MEM_new<AssetWeakReference>(__func__,
1599 *src->brush_asset_reference);
1600 }
1602 dst->tool_brush_bindings.main_brush_asset_reference = MEM_new<AssetWeakReference>(
1604 }
1608 {
1609 brush_ref.name = BLI_strdup(brush_ref.name);
1610 brush_ref.brush_asset_reference = MEM_new<AssetWeakReference>(
1611 __func__, *brush_ref.brush_asset_reference);
1612 }
1613
1620
1621 if ((flag & LIB_ID_CREATE_NO_USER_REFCOUNT) == 0) {
1622 id_us_plus(id_cast<ID *>(dst->palette));
1623 }
1624
1625 if (src->mesh_automasking_settings) {
1626 dst->mesh_automasking_settings = MEM_new<MeshAutomaskingSettings>(
1632 }
1633
1634 dst->runtime = MEM_new<bke::PaintRuntime>(__func__);
1635 if (src->runtime) {
1637 dst->runtime->ob_mode = src->runtime->ob_mode;
1638 dst->runtime->primary_snap = std::make_unique<bke::paint::TexSnapshot>();
1639 dst->runtime->secondary_snap = std::make_unique<bke::paint::TexSnapshot>();
1640 dst->runtime->cursor_snap = std::make_unique<bke::paint::CursorSnapshot>();
1641 dst->runtime->initialized = true;
1642 }
1643}
1644
1646{
1647 if (ts->vpaint) {
1648 fn(*reinterpret_cast<Paint *>(ts->vpaint));
1649 }
1650 if (ts->wpaint) {
1651 fn(*reinterpret_cast<Paint *>(ts->wpaint));
1652 }
1653 if (ts->sculpt) {
1654 fn(*reinterpret_cast<Paint *>(ts->sculpt));
1655 }
1656 if (ts->gp_paint) {
1657 fn(*reinterpret_cast<Paint *>(ts->gp_paint));
1658 }
1659 if (ts->gp_vertexpaint) {
1660 fn(*reinterpret_cast<Paint *>(ts->gp_vertexpaint));
1661 }
1662 if (ts->gp_sculptpaint) {
1663 fn(*reinterpret_cast<Paint *>(ts->gp_sculptpaint));
1664 }
1665 if (ts->gp_weightpaint) {
1666 fn(*reinterpret_cast<Paint *>(ts->gp_weightpaint));
1667 }
1668 if (ts->curves_sculpt) {
1669 fn(*reinterpret_cast<Paint *>(ts->curves_sculpt));
1670 }
1671 fn(*reinterpret_cast<Paint *>(&ts->imapaint));
1672}
1673
1674namespace bke::paint {
1676{
1677 const PaintRuntime &paint_runtime = *paint->runtime;
1678 if (paint_runtime.last_stroke_valid && paint_runtime.average_stroke_counter > 0) {
1679 float fac = 1.0f / paint_runtime.average_stroke_counter;
1680 return paint_runtime.average_stroke_accum * fac;
1681 }
1682
1683 return ob->object_to_world().location();
1684}
1686{
1687 PaintRuntime &paint_runtime = *paint.runtime;
1688 paint_runtime.average_stroke_accum += location;
1689 paint_runtime.average_stroke_counter++;
1690 paint_runtime.last_stroke_valid = true;
1691}
1693{
1694 PaintRuntime &paint_runtime = *paint.runtime;
1695 paint_runtime.average_stroke_accum = location;
1696 paint_runtime.average_stroke_counter = 1;
1697 paint_runtime.last_stroke_valid = true;
1698}
1699} // namespace bke::paint
1700
1702 const float3 &initial_hsv_jitter,
1703 const float distance,
1704 const float pressure,
1705 const float3 &color)
1706{
1707 constexpr float noise_scale = 1 / 20.0f;
1708
1709 const float random_hue = (color_jitter.flag & BRUSH_COLOR_JITTER_USE_HUE_AT_STROKE) ?
1710 initial_hsv_jitter[0] :
1712 float2(distance * noise_scale, initial_hsv_jitter[0] * 100));
1713
1714 const float random_sat = (color_jitter.flag & BRUSH_COLOR_JITTER_USE_SAT_AT_STROKE) ?
1715 initial_hsv_jitter[1] :
1717 float2(distance * noise_scale, initial_hsv_jitter[1] * 100));
1718
1719 const float random_val = (color_jitter.flag & BRUSH_COLOR_JITTER_USE_VAL_AT_STROKE) ?
1720 initial_hsv_jitter[2] :
1722 float2(distance * noise_scale, initial_hsv_jitter[2] * 100));
1723
1724 float hue_jitter_scale = color_jitter.hue;
1725 if (color_jitter.flag & BRUSH_COLOR_JITTER_USE_HUE_RAND_PRESS) {
1726 hue_jitter_scale *= BKE_curvemapping_evaluateF(color_jitter.curve_hue_jitter, 0, pressure);
1727 }
1728 float sat_jitter_scale = color_jitter.saturation;
1729 if (color_jitter.flag & BRUSH_COLOR_JITTER_USE_SAT_RAND_PRESS) {
1730 sat_jitter_scale *= BKE_curvemapping_evaluateF(color_jitter.curve_sat_jitter, 0, pressure);
1731 }
1732 float val_jitter_scale = color_jitter.value;
1733 if (color_jitter.flag & BRUSH_COLOR_JITTER_USE_VAL_RAND_PRESS) {
1734 val_jitter_scale *= BKE_curvemapping_evaluateF(color_jitter.curve_val_jitter, 0, pressure);
1735 }
1736
1737 float3 hsv;
1738 rgb_to_hsv_v(color, hsv);
1739
1740 hsv[0] += math::interpolate(0.5f, random_hue, hue_jitter_scale) - 0.5f;
1741 /* Wrap hue. */
1742 if (hsv[0] > 1.0f) {
1743 hsv[0] -= 1.0f;
1744 }
1745 else if (hsv[0] < 0.0f) {
1746 hsv[0] += 1.0f;
1747 }
1748
1749 hsv[1] *= math::interpolate(1.0f, random_sat * 2.0f, sat_jitter_scale);
1750 hsv[2] *= math::interpolate(1.0f, random_val * 2.0f, val_jitter_scale);
1751
1752 float3 random_color;
1753 hsv_to_rgb_v(hsv, random_color);
1754 return random_color;
1755}
1756
1758{
1759 if (paint->cavity_curve) {
1760 BKE_curvemapping_blend_write(writer, paint->cavity_curve);
1761 }
1762 if (paint->brush_asset_reference) {
1763 BKE_asset_weak_reference_write(writer, paint->brush_asset_reference);
1764 }
1765
1766 {
1767 /* Write tool system bindings. */
1768 ToolSystemBrushBindings &tool_brush_bindings = paint->tool_brush_bindings;
1769
1770 if (tool_brush_bindings.main_brush_asset_reference) {
1772 }
1773 writer->write_struct_list(&tool_brush_bindings.active_brush_per_brush_type);
1774 for (NamedBrushAssetReference &brush_ref : tool_brush_bindings.active_brush_per_brush_type) {
1775 writer->write_string(brush_ref.name);
1776 if (brush_ref.brush_asset_reference) {
1778 }
1779 }
1780 }
1781
1782 if (paint->unified_paint_settings.curve_rand_hue) {
1783 BKE_curvemapping_blend_write(writer, paint->unified_paint_settings.curve_rand_hue);
1784 }
1785
1786 if (paint->unified_paint_settings.curve_rand_saturation) {
1787 BKE_curvemapping_blend_write(writer, paint->unified_paint_settings.curve_rand_saturation);
1788 }
1789
1790 if (paint->unified_paint_settings.curve_rand_value) {
1791 BKE_curvemapping_blend_write(writer, paint->unified_paint_settings.curve_rand_value);
1792 }
1793
1794 if (paint->mesh_automasking_settings) {
1795 writer->write_struct(paint->mesh_automasking_settings);
1796 MeshAutomaskingSettings &automasking_settings = *paint->mesh_automasking_settings;
1797 if (automasking_settings.cavity_curve) {
1798 BKE_curvemapping_blend_write(writer, automasking_settings.cavity_curve);
1799 }
1800
1801 if (automasking_settings.cavity_curve_op) {
1802 BKE_curvemapping_blend_write(writer, automasking_settings.cavity_curve_op);
1803 }
1804 }
1805}
1806
1808{
1809 BLO_read_struct(reader, CurveMapping, &paint->cavity_curve);
1810 if (paint->cavity_curve) {
1811 BKE_curvemapping_blend_read(reader, paint->cavity_curve);
1812 }
1813 else {
1815 }
1816
1817 BLO_read_struct(reader, AssetWeakReference, &paint->brush_asset_reference);
1818 if (paint->brush_asset_reference) {
1819 BKE_asset_weak_reference_read(reader, paint->brush_asset_reference);
1820 }
1821
1822 {
1823 /* Read tool system bindings. */
1824 ToolSystemBrushBindings &tool_brush_bindings = paint->tool_brush_bindings;
1825
1826 BLO_read_struct(reader, AssetWeakReference, &tool_brush_bindings.main_brush_asset_reference);
1827 if (tool_brush_bindings.main_brush_asset_reference) {
1828 BKE_asset_weak_reference_read(reader, tool_brush_bindings.main_brush_asset_reference);
1829 }
1830
1832 reader, NamedBrushAssetReference, &tool_brush_bindings.active_brush_per_brush_type);
1833 for (NamedBrushAssetReference &brush_ref : tool_brush_bindings.active_brush_per_brush_type) {
1834 BLO_read_string(reader, &brush_ref.name);
1835
1837 if (brush_ref.brush_asset_reference) {
1839 }
1840 }
1841 }
1842 UnifiedPaintSettings *ups = &paint->unified_paint_settings;
1844 if (ups->curve_rand_hue) {
1847 }
1848
1850 if (ups->curve_rand_saturation) {
1853 }
1854
1856 if (ups->curve_rand_value) {
1859 }
1860
1861 BLO_read_struct(reader, MeshAutomaskingSettings, &paint->mesh_automasking_settings);
1862 if (paint->mesh_automasking_settings) {
1863 MeshAutomaskingSettings &automasking_settings = *paint->mesh_automasking_settings;
1864
1865 BLO_read_struct(reader, CurveMapping, &automasking_settings.cavity_curve);
1866 if (automasking_settings.cavity_curve) {
1867 BKE_curvemapping_blend_read(reader, automasking_settings.cavity_curve);
1868 BKE_curvemapping_init(automasking_settings.cavity_curve);
1869 }
1870
1871 BLO_read_struct(reader, CurveMapping, &automasking_settings.cavity_curve_op);
1872 if (automasking_settings.cavity_curve_op) {
1873 BKE_curvemapping_blend_read(reader, automasking_settings.cavity_curve_op);
1874 BKE_curvemapping_init(automasking_settings.cavity_curve_op);
1875 }
1876 }
1877
1878 paint->runtime = MEM_new<bke::PaintRuntime>(__func__);
1879
1881}
1882
1884 const int gridsize,
1885 const int x,
1886 const int y)
1887{
1888 return grid_hidden[CCG_grid_xy_to_index(gridsize, x, y)] ||
1889 grid_hidden[CCG_grid_xy_to_index(gridsize, x + 1, y)] ||
1890 grid_hidden[CCG_grid_xy_to_index(gridsize, x + 1, y + 1)] ||
1891 grid_hidden[CCG_grid_xy_to_index(gridsize, x, y + 1)];
1892}
1893
1895{
1896 BMLoop *l_iter;
1897 BMLoop *l_first;
1898
1899 l_iter = l_first = BM_FACE_FIRST_LOOP(f);
1900 do {
1901 if (BM_elem_flag_test(l_iter->v, BM_ELEM_HIDDEN)) {
1902 return true;
1903 }
1904 } while ((l_iter = l_iter->next) != l_first);
1905
1906 return false;
1907}
1908
1909namespace bke::paint {
1910bool supports_scene_size(const PaintMode paint_mode, const Brush &brush)
1911{
1912 switch (paint_mode) {
1913 case PaintMode::Sculpt:
1914 return true;
1915 case PaintMode::Vertex:
1916 case PaintMode::Weight:
1917 return false;
1919 if (!USER_EXPERIMENTAL_TEST(&U, use_3d_texture_paint)) {
1920 return false;
1921 }
1923 case PaintMode::GPencil:
1927 return true;
1929 return false;
1931 return false;
1932 case PaintMode::Invalid:
1934 return false;
1935 }
1937 return false;
1938}
1939bool supports_symmetry_tiling(const PaintMode paint_mode, const Brush &brush)
1940{
1941 switch (paint_mode) {
1942 case PaintMode::Sculpt:
1943 return true;
1944 case PaintMode::Vertex:
1945 case PaintMode::Weight:
1946 return false;
1948 if (!USER_EXPERIMENTAL_TEST(&U, use_3d_texture_paint)) {
1949 return false;
1950 }
1952 case PaintMode::GPencil:
1956 return false;
1958 return false;
1960 return false;
1961 case PaintMode::Invalid:
1963 return false;
1964 }
1966 return false;
1967}
1968} // namespace bke::paint
1969
1971{
1972 int factor = CCG_grid_factor(level, gpm->level);
1973 int gridsize = CCG_grid_size(gpm->level);
1974
1975 return gpm->data[(y * factor) * gridsize + (x * factor)];
1976}
1977
1978/* Threshold to move before updating the brush rotation, reduces jitter. */
1979static float paint_rake_rotation_spacing(const Paint & /*paint*/,
1980 const Brush &brush,
1981 bool in_stroke,
1982 bool is_first_dab)
1983{
1984 const float rotation_spacing = brush.sculpt_brush_type == SCULPT_BRUSH_TYPE_CLAY_STRIPS ? 1.0f :
1985 20.0f;
1986 if (!in_stroke) {
1987 /* Increase spacing when not in stroke to avoid cursor jitter. */
1988 return std::max(rotation_spacing, 20.0f);
1989 }
1990 else if (is_first_dab) {
1991 /* Use a smaller limit if the stroke hasn't started to prevent excessive pre-roll. */
1992 return std::min(rotation_spacing, 4.0f);
1993 }
1994 else {
1995 return rotation_spacing;
1996 }
1997}
1998
2000{
2001 bke::PaintRuntime &paint_runtime = *paint.runtime;
2002 paint_runtime.brush_rotation = rotation;
2003
2004 if (brush.mask_mtex.brush_angle_mode & MTEX_ANGLE_RAKE) {
2005 paint_runtime.brush_rotation_sec = rotation;
2006 }
2007 else {
2008 paint_runtime.brush_rotation_sec = 0.0f;
2009 }
2010}
2011
2012static bool paint_rake_rotation_active(const MTex &mtex)
2013{
2014 return mtex.tex && mtex.brush_angle_mode & MTEX_ANGLE_RAKE;
2015}
2016
2017static bool paint_rake_rotation_active(const Brush &brush, PaintMode paint_mode)
2018{
2020 BKE_brush_has_cube_tip(&brush, paint_mode);
2021}
2022
2024 const Brush &brush,
2025 const float mouse_pos[2],
2026 const PaintMode paint_mode,
2027 bool in_stroke,
2028 bool is_first_dab)
2029{
2030 bke::PaintRuntime &paint_runtime = *paint.runtime;
2031
2032 bool ok = false;
2033 if (paint_rake_rotation_active(brush, paint_mode)) {
2034 const float r = paint_rake_rotation_spacing(paint, brush, in_stroke, is_first_dab);
2035 float rotation;
2036
2037 float dpos[2];
2038 sub_v2_v2v2(dpos, mouse_pos, paint_runtime.last_rake);
2039
2040 /* Limit how often we update the angle to prevent jitter. */
2041 if (len_squared_v2(dpos) >= r * r) {
2042 rotation = atan2f(dpos[1], dpos[0]) + float(0.5f * M_PI);
2043
2044 copy_v2_v2(paint_runtime.last_rake, mouse_pos);
2045
2046 paint_runtime.last_rake_angle = rotation;
2047
2049 ok = true;
2050 }
2051 /* Make sure we reset here to the last rotation to avoid accumulating
2052 * values in case a random rotation is also added. */
2053 else {
2055 ok = false;
2056 }
2057 }
2058 else {
2059 paint_runtime.brush_rotation = paint_runtime.brush_rotation_sec = 0.0f;
2060 ok = true;
2061 }
2062 return ok;
2063}
2064
2066{
2067 ss->deform_cos = {};
2068 ss->deform_imats = {};
2069 ss->vert_normals_deform = {};
2070 ss->face_normals_deform = {};
2071}
2072
2077{
2079
2080 if (ss.bm) {
2081 if (ob->data) {
2082 BMeshToMeshParams params{};
2083 params.calc_object_remap = false;
2084 BM_mesh_bm_to_me(nullptr, ss.bm, id_cast<Mesh *>(ob->data), &params);
2085 }
2086 }
2087}
2088
2090{
2091 if (ob && ob->runtime->sculpt_session) {
2093 }
2094}
2095
2097{
2098 SculptSession *ss = object.runtime->sculpt_session;
2099 if (!ss) {
2100 return;
2101 }
2102
2103 ss->pbvh.reset();
2104 ss->edge_to_face_offsets = {};
2105 ss->edge_to_face_indices = {};
2106 ss->edge_to_face_map = {};
2107 ss->vert_to_edge_offsets = {};
2108 ss->vert_to_edge_indices = {};
2109 ss->vert_to_edge_map = {};
2110
2111 ss->preview_verts = {};
2112
2113 ss->boundary_info_cache.reset();
2115 ss->topology_island_cache.reset();
2116
2117 ss->clear_active_elements(false);
2118}
2119
2121{
2122 if (ob && ob->runtime->sculpt_session) {
2124
2125 if (ss->bm) {
2128 BM_mesh_free(ss->bm);
2129 }
2130
2132
2133 MEM_delete(ss);
2134
2135 ob->runtime->sculpt_session = nullptr;
2136 }
2137}
2138
2139SculptSession::SculptSession() = default;
2140
2142{
2143 if (this->bm_log) {
2144 BM_log_free(this->bm_log);
2145 }
2146
2147 if (this->tex_pool) {
2149 }
2150}
2151
2153{
2154 return active_vert_;
2155}
2156
2158{
2159 return last_active_vert_;
2160}
2161
2163{
2164 if (std::holds_alternative<int>(active_vert_)) {
2165 return std::get<int>(active_vert_);
2166 }
2167 if (std::holds_alternative<BMVert *>(active_vert_)) {
2168 BMVert *bm_vert = std::get<BMVert *>(active_vert_);
2169 return BM_elem_index_get(bm_vert);
2170 }
2171
2172 return -1;
2173}
2174
2176{
2177 if (std::holds_alternative<int>(last_active_vert_)) {
2178 return std::get<int>(last_active_vert_);
2179 }
2180 if (std::holds_alternative<BMVert *>(last_active_vert_)) {
2181 BMVert *bm_vert = std::get<BMVert *>(last_active_vert_);
2182 return BM_elem_index_get(bm_vert);
2183 }
2184
2185 return -1;
2186}
2187
2188float3 SculptSession::active_vert_position(const Depsgraph &depsgraph, const Object &object) const
2189{
2190 if (std::holds_alternative<int>(active_vert_)) {
2191 if (this->subdiv_ccg) {
2192 return this->subdiv_ccg->positions[std::get<int>(active_vert_)];
2193 }
2194 const Span<float3> positions = bke::pbvh::vert_positions_eval(depsgraph, object);
2195 return positions[std::get<int>(active_vert_)];
2196 }
2197 if (std::holds_alternative<BMVert *>(active_vert_)) {
2198 BMVert *bm_vert = std::get<BMVert *>(active_vert_);
2199 return bm_vert->co;
2200 }
2201
2203 return float3(std::numeric_limits<float>::infinity());
2204}
2205
2206void SculptSession::clear_active_elements(bool persist_last_active)
2207{
2208 if (persist_last_active) {
2209 if (!std::holds_alternative<std::monostate>(active_vert_)) {
2210 last_active_vert_ = active_vert_;
2211 }
2212 }
2213 else {
2214 last_active_vert_ = {};
2215 }
2216 active_vert_ = {};
2217 active_grid_index.reset();
2218 active_face_index.reset();
2219}
2220
2222{
2223 active_vert_ = vert;
2224}
2225
2226std::optional<PersistentMultiresData> SculptSession::persistent_multires_data()
2227{
2229 if (persistent.grids_num == -1 || persistent.grid_size == -1) {
2230 return std::nullopt;
2231 }
2232
2233 if (this->subdiv_ccg->grids_num != persistent.grids_num ||
2234 this->subdiv_ccg->grid_size != persistent.grid_size)
2235 {
2236 return std::nullopt;
2237 }
2238
2239 return PersistentMultiresData{persistent.sculpt_persistent_co,
2240 persistent.sculpt_persistent_no,
2241 persistent.sculpt_persistent_disp};
2242}
2243
2245 Object *ob,
2246 const bool auto_create_mdisps)
2247{
2248 Mesh &mesh = *id_cast<Mesh *>(ob->data);
2249
2250 if (ob->runtime->sculpt_session && ob->runtime->sculpt_session->bm) {
2251 /* Can't combine multires and dynamic topology. */
2252 return nullptr;
2253 }
2254
2255 bool need_mdisps = false;
2256
2257 if (!CustomData_get_layer(&mesh.corner_data, CD_MDISPS)) {
2258 if (!auto_create_mdisps) {
2259 /* Multires can't work without displacement layer. */
2260 return nullptr;
2261 }
2262 need_mdisps = true;
2263 }
2264
2265 /* Weight paint operates on original vertices, and needs to treat multires as regular modifier
2266 * to make it so that pbvh::Tree vertices are at the multires surface. */
2267 if ((ob->mode & OB_MODE_SCULPT) == 0) {
2268 return nullptr;
2269 }
2270
2271 VirtualModifierData virtual_modifier_data;
2272 for (ModifierData *md = BKE_modifiers_get_virtual_modifierlist(ob, &virtual_modifier_data); md;
2273 md = md->next)
2274 {
2275 if (md->type == eModifierType_Multires) {
2276 MultiresModifierData *mmd = reinterpret_cast<MultiresModifierData *>(md);
2277
2279 continue;
2280 }
2281
2282 if (mmd->sculptlvl > 0 && !(mmd->flags & eMultiresModifierFlag_UseSculptBaseMesh)) {
2283 if (need_mdisps) {
2284 CustomData_add_layer(&mesh.corner_data, CD_MDISPS, CD_SET_DEFAULT, mesh.corners_num);
2285 }
2286
2287 return mmd;
2288 }
2289
2290 return nullptr;
2291 }
2292 }
2293
2294 return nullptr;
2295}
2296
2298{
2299 return sculpt_multires_modifier_get(scene, ob, false);
2300}
2301
2303{
2304 const SculptSession &ss = *object.runtime->sculpt_session;
2307 return ss.subdiv_ccg->grids_num * key.grid_area;
2308}
2309
2311{
2312 const SculptSession &ss = *object.runtime->sculpt_session;
2315 return ss.subdiv_ccg->grids_num * square_i(key.grid_size - 1);
2316}
2317
2318/* Checks if there are any supported deformation modifiers active */
2319static bool sculpt_modifiers_active(const Scene *scene, const Sculpt *sd, Object *ob)
2320{
2321 const Mesh &mesh = *id_cast<Mesh *>(ob->data);
2322
2323 if (ob->runtime->sculpt_session->bm || BKE_sculpt_multires_active(scene, ob)) {
2324 return false;
2325 }
2326
2327 /* Non-locked shape keys could be handled in the same way as deformed mesh. */
2328 if ((ob->shapeflag & OB_SHAPE_LOCK) == 0 && mesh.key && ob->shapenr) {
2329 return true;
2330 }
2331
2332 VirtualModifierData virtual_modifier_data;
2333 for (ModifierData *md = BKE_modifiers_get_virtual_modifierlist(ob, &virtual_modifier_data); md;
2334 md = md->next)
2335 {
2336 const ModifierTypeInfo *mti = BKE_modifier_get_info(md->type);
2338 continue;
2339 }
2340 if (md->type == eModifierType_Multires && (ob->mode & OB_MODE_SCULPT)) {
2341 MultiresModifierData *mmd = reinterpret_cast<MultiresModifierData *>(md);
2343 continue;
2344 }
2345 }
2346 /* Exception for shape keys because we can edit those. */
2347 if (md->type == eModifierType_ShapeKey) {
2348 continue;
2349 }
2350
2351 if (mti->type == ModifierTypeType::OnlyDeform) {
2352 return true;
2353 }
2354 if (sd == nullptr || (sd->flags & SCULPT_ONLY_DEFORM) == 0) {
2355 return true;
2356 }
2357 }
2358
2359 return false;
2360}
2361
2362static void sculptsession_update(Depsgraph *depsgraph,
2363 Object *ob,
2364 Object *ob_eval,
2365 bool is_paint_tool)
2366{
2367 using namespace blender::bke;
2368 Scene *scene = DEG_get_input_scene(depsgraph);
2369 Sculpt *sd = scene->toolsettings->sculpt;
2371 Mesh *mesh_orig = BKE_object_get_original_mesh(ob);
2372 /* Use the "unchecked" function, because this code also runs as part of the depsgraph node that
2373 * evaluates the object's geometry. So from perspective of the depsgraph, the mesh is not fully
2374 * evaluated yet. */
2375 Mesh *mesh_eval = BKE_object_get_evaluated_mesh_unchecked(ob_eval);
2376 MultiresModifierData *mmd = sculpt_multires_modifier_get(scene, ob, true);
2377
2378 BLI_assert(mesh_eval != nullptr);
2379
2380 /* This is for handling a newly opened file with no object visible,
2381 * causing `mesh_eval == nullptr`. */
2382 if (mesh_eval == nullptr) {
2383 return;
2384 }
2385
2387
2388 ss.shapekey_active = (mmd == nullptr && ss.bm == nullptr) ? BKE_keyblock_from_object(ob) :
2389 nullptr;
2390
2391 ss.multires_modifier = mmd;
2392
2393 ss.subdiv_ccg = mesh_eval->runtime->subdiv_ccg.get();
2394
2395 pbvh::Tree &pbvh = object::pbvh_ensure(*depsgraph, *ob);
2396
2397 if (ss.deform_modifiers_active) {
2398 /* Painting doesn't need crazyspace, use already evaluated mesh coordinates if possible. */
2399 bool used_me_eval = false;
2400
2402 const Mesh *me_eval_deform = BKE_object_get_mesh_deform_eval(ob_eval);
2403 BLI_assert(me_eval_deform != nullptr);
2404 /* If the fully evaluated mesh has the same topology as the deform-only version, use it.
2405 * This matters because crazyspace evaluation is very restrictive and excludes even modifiers
2406 * that simply recompute vertex weights (which can even include Geometry Nodes). */
2407 if (me_eval_deform->faces_num == mesh_eval->faces_num &&
2408 me_eval_deform->corners_num == mesh_eval->corners_num &&
2409 me_eval_deform->verts_num == mesh_eval->verts_num)
2410 {
2412
2413 BLI_assert(me_eval_deform->verts_num == mesh_orig->verts_num);
2414
2415 ss.deform_cos = mesh_eval->vert_positions();
2417
2418 used_me_eval = true;
2419 }
2420 }
2421
2422 /* We depend on the deform coordinates not being updated in the middle of a stroke. This array
2423 * eventually gets cleared inside BKE_sculpt_update_object_before_eval.
2424 * See #126713 for more information. */
2425 if (ss.deform_cos.is_empty() && !used_me_eval) {
2427
2428 BKE_crazyspace_build_sculpt(depsgraph, scene, ob, ss.deform_imats, ss.deform_cos);
2430
2431 for (float3x3 &matrix : ss.deform_imats) {
2432 matrix = math::invert(matrix);
2433 }
2434 }
2435 }
2436 else {
2438 }
2439
2440 if (ss.shapekey_active != nullptr && ss.deform_cos.is_empty()) {
2441 ss.deform_cos = Span(static_cast<const float3 *>(ss.shapekey_active->data),
2442 mesh_orig->verts_num);
2443 if (!ss.deform_cos.is_empty()) {
2445 }
2446 }
2447
2448 if (is_paint_tool) {
2449 /* We should rebuild the PBVH_pixels when painting canvas changes.
2450 *
2451 * The relevant changes are stored/encoded in the paint canvas key.
2452 * These include the active uv map, and resolutions. */
2453 if (USER_EXPERIMENTAL_TEST(&U, use_3d_texture_paint)) {
2454 std::string paint_canvas_key = BKE_paint_canvas_key_get(scene->toolsettings->imapaint, ob);
2455 if (!ss.last_paint_canvas_key || paint_canvas_key != ss.last_paint_canvas_key) {
2456 ss.last_paint_canvas_key = paint_canvas_key;
2458 }
2459 }
2460
2461 /* Ensure attributes are correctly updated in the UI. */
2462 if (ob->mode & OB_MODE_SCULPT) {
2464 }
2465 }
2466
2467 /* This solves a crash when running a sculpt brush in background mode, because there is no redraw
2468 * after entering sculpt mode to make sure normals are allocated. Recalculating normals with
2469 * every brush step is too expensive currently. */
2470 bke::pbvh::update_normals(*depsgraph, *ob, pbvh);
2471}
2472
2474{
2475 /* Update before mesh evaluation in the dependency graph. */
2476 Object *ob_orig = DEG_get_original(ob_eval);
2477 SculptSession *ss = ob_orig->runtime->sculpt_session;
2478 if (!ss) {
2479 return;
2480 }
2481
2483
2484 if (!ss->cache && !ss->filter_cache && !ss->expand_cache) {
2485 /* Avoid performing the following normal update for Multires, as it causes race conditions
2486 * and other intermittent crashes with shared meshes.
2487 * See !125268 and #125157 for more information. */
2488 if (pbvh && pbvh->type() != bke::pbvh::Type::Grids) {
2489 /* pbvh::Tree nodes may contain dirty normal tags. To avoid losing that information when
2490 * the pbvh::Tree is deleted, make sure all tagged geometry normals are up to date.
2491 * See #122947 for more information. */
2493 }
2494 /* We free pbvh on changes, except in the middle of drawing a stroke
2495 * since it can't deal with changing PVBH node organization, we hope
2496 * topology does not change in the meantime .. weak. */
2498
2500 }
2501 else if (pbvh) {
2502 IndexMaskMemory memory;
2503 const IndexMask node_mask = bke::pbvh::all_leaf_nodes(*pbvh, memory);
2504 pbvh->tag_positions_changed(node_mask);
2506 }
2507}
2508
2509void BKE_sculptsession_update_after_eval(Depsgraph *depsgraph, Object *ob_eval)
2510{
2511 /* Update after mesh evaluation in the dependency graph, to rebuild pbvh::Tree or
2512 * other data when modifiers change the mesh. */
2513 Object *ob_orig = DEG_get_original(ob_eval);
2514
2515 sculptsession_update(depsgraph, ob_orig, ob_eval, false);
2516}
2517
2519{
2520 using namespace blender::bke;
2521 Mesh *orig_me = BKE_object_get_original_mesh(object);
2522
2523 if (BKE_id_attributes_color_find(&orig_me->id, orig_me->active_color_attribute)) {
2524 return;
2525 }
2526
2527 AttributeOwner owner = AttributeOwner::from_id(&orig_me->id);
2528 const std::string unique_name = BKE_attribute_calc_unique_name(owner, "Color");
2529 if (!orig_me->attributes_for_write().add(
2531 {
2532 return;
2533 }
2534
2535 BKE_id_attributes_active_color_set(&orig_me->id, unique_name);
2536 BKE_id_attributes_default_color_set(&orig_me->id, unique_name);
2538 BKE_mesh_tessface_clear(orig_me);
2539}
2540
2541void BKE_sculptsession_update_for_edit(Depsgraph *depsgraph, Object *ob_orig, bool is_paint_tool)
2542{
2544 BLI_assert(ob_orig == DEG_get_original(ob_orig));
2545
2546 Object *ob_eval = DEG_get_evaluated(depsgraph, ob_orig);
2547
2548 sculptsession_update(depsgraph, ob_orig, ob_eval, is_paint_tool);
2549}
2550
2551void BKE_sculpt_mask_layers_ensure(Depsgraph *depsgraph,
2552 Main *bmain,
2553 Object *ob,
2555{
2556 using namespace blender::bke;
2557 Mesh *mesh = id_cast<Mesh *>(ob->data);
2558 const OffsetIndices faces = mesh->faces();
2559 const Span<int> corner_verts = mesh->corner_verts();
2560 MutableAttributeAccessor attributes = mesh->attributes_for_write();
2561
2562 /* if multires is active, create a grid paint mask layer if there
2563 * isn't one already */
2564 if (mmd && !CustomData_has_layer(&mesh->corner_data, CD_GRID_PAINT_MASK)) {
2565 int level = max_ii(1, mmd->sculptlvl);
2566 int gridsize = CCG_grid_size(level);
2567 int gridarea = gridsize * gridsize;
2568
2569 GridPaintMask *gmask = static_cast<GridPaintMask *>(CustomData_add_layer(
2570 &mesh->corner_data, CD_GRID_PAINT_MASK, CD_SET_DEFAULT, mesh->corners_num));
2571
2572 for (int i = 0; i < mesh->corners_num; i++) {
2573 GridPaintMask *gpm = &gmask[i];
2574
2575 gpm->level = level;
2576 gpm->data = MEM_new_array_zeroed<float>(gridarea, "GridPaintMask.data");
2577 }
2578
2579 /* If vertices already have mask, copy into multires data. */
2580 if (const VArray<float> mask = *attributes.lookup<float>(".sculpt_mask", AttrDomain::Point)) {
2581 const VArraySpan<float> mask_span(mask);
2582 for (const int i : faces.index_range()) {
2583 const IndexRange face = faces[i];
2584
2585 /* Mask center. */
2586 float avg = 0.0f;
2587 for (const int vert : corner_verts.slice(face)) {
2588 avg += mask_span[vert];
2589 }
2590 avg /= float(face.size());
2591
2592 /* Fill in multires mask corner. */
2593 for (const int corner : face) {
2594 GridPaintMask *gpm = &gmask[corner];
2595 const int vert = corner_verts[corner];
2596 const int prev = corner_verts[mesh::face_corner_prev(face, corner)];
2597 const int next = corner_verts[mesh::face_corner_next(face, corner)];
2598
2599 gpm->data[0] = avg;
2600 gpm->data[1] = (mask_span[vert] + mask_span[next]) * 0.5f;
2601 gpm->data[2] = (mask_span[vert] + mask_span[prev]) * 0.5f;
2602 gpm->data[3] = mask_span[vert];
2603 }
2604 }
2605 }
2606 /* The evaluated multires CCG must be updated to contain the new data. */
2608 if (depsgraph) {
2609 BKE_scene_graph_evaluated_ensure(depsgraph, bmain);
2610 }
2611 }
2612 else {
2613 attributes.add<float>(".sculpt_mask", AttrDomain::Point, AttributeInitDefaultValue());
2614 }
2615}
2616
2617static bool check_sculpt_object_deformed(Object *object, const bool for_construction)
2618{
2619 bool deformed = false;
2620
2621 /* Active modifiers means extra deformation, which can't be handled correct
2622 * on birth of pbvh::Tree and sculpt "layer" levels, so use pbvh::Tree only for internal brush
2623 * stuff and show final evaluated mesh so user would see actual object shape. */
2624 deformed |= object->runtime->sculpt_session->deform_modifiers_active;
2625
2626 if (for_construction) {
2627 deformed |= object->runtime->sculpt_session->shapekey_active != nullptr;
2628 }
2629 else {
2630 /* As in case with modifiers, we can't synchronize deformation made against
2631 * pbvh::Tree and non-locked keyblock, so also use pbvh::Tree only for brushes and
2632 * final DM to give final result to user. */
2633 deformed |= object->runtime->sculpt_session->shapekey_active &&
2634 (object->shapeflag & OB_SHAPE_LOCK) == 0;
2635 }
2636
2637 return deformed;
2638}
2639
2641{
2642 using namespace blender::bke;
2643
2644 const AttributeAccessor attributes = mesh.attributes();
2645 const VArray<bool> hide_poly = *attributes.lookup_or_default<bool>(
2646 ".hide_poly", AttrDomain::Face, false);
2647 if (hide_poly.is_single() && !hide_poly.get_internal_single()) {
2649 return;
2650 }
2651
2652 const OffsetIndices<int> faces = mesh.faces();
2653
2654 const VArraySpan<bool> hide_poly_span(hide_poly);
2655 BitGroupVector<> &grid_hidden = BKE_subdiv_ccg_grid_hidden_ensure(subdiv_ccg);
2656 threading::parallel_for(faces.index_range(), 1024, [&](const IndexRange range) {
2657 for (const int i : range) {
2658 const bool face_hidden = hide_poly_span[i];
2659 for (const int corner : faces[i]) {
2660 grid_hidden[corner].set_all(face_hidden);
2661 }
2662 }
2663 });
2664}
2665
2666namespace bke {
2667
2668static std::unique_ptr<pbvh::Tree> build_pbvh_for_dynamic_topology(Object *ob)
2669{
2670 BMesh &bm = *ob->runtime->sculpt_session->bm;
2671 BM_data_layer_ensure_named(&bm, &bm.vdata, CD_PROP_INT32, ".sculpt_dyntopo_node_id_vertex");
2672 BM_data_layer_ensure_named(&bm, &bm.pdata, CD_PROP_INT32, ".sculpt_dyntopo_node_id_face");
2673
2674 return std::make_unique<pbvh::Tree>(pbvh::Tree::from_bmesh(bm));
2675}
2676
2677static std::unique_ptr<pbvh::Tree> build_pbvh_from_regular_mesh(Object *ob,
2678 const Mesh *me_eval_deform)
2679{
2681 std::unique_ptr<pbvh::Tree> pbvh = std::make_unique<pbvh::Tree>(pbvh::Tree::from_mesh(mesh));
2682
2683 const bool is_deformed = check_sculpt_object_deformed(ob, true);
2684 if (is_deformed && me_eval_deform != nullptr) {
2685 BKE_pbvh_vert_coords_apply(*pbvh, me_eval_deform->vert_positions());
2686 }
2687
2688 return pbvh;
2689}
2690
2691static std::unique_ptr<pbvh::Tree> build_pbvh_from_ccg(Object *ob, SubdivCCG &subdiv_ccg)
2692{
2693 const Mesh &base_mesh = *BKE_mesh_from_object(ob);
2694 BKE_sculpt_sync_face_visibility_to_grids(base_mesh, subdiv_ccg);
2695
2696 return std::make_unique<pbvh::Tree>(pbvh::Tree::from_grids(base_mesh, subdiv_ccg));
2697}
2698
2699} // namespace bke
2700
2701namespace bke::object {
2702
2703pbvh::Tree &pbvh_ensure(Depsgraph &depsgraph, Object &object)
2704{
2705 if (pbvh::Tree *pbvh = pbvh_get(object)) {
2706 return *pbvh;
2707 }
2708 BLI_assert(object.runtime->sculpt_session != nullptr);
2709 SculptSession &ss = *object.runtime->sculpt_session;
2710
2711 if (ss.bm != nullptr) {
2712 /* Sculpting on a BMesh (dynamic-topology) gets a special pbvh::Tree. */
2714 }
2715 else {
2716 Object *object_eval = DEG_get_evaluated(&depsgraph, &object);
2717 Mesh *mesh_eval = id_cast<Mesh *>(object_eval->data);
2718 if (mesh_eval->runtime->subdiv_ccg != nullptr) {
2719 ss.pbvh = build_pbvh_from_ccg(&object, *mesh_eval->runtime->subdiv_ccg);
2720 }
2721 else {
2722 const Mesh *me_eval_deform = BKE_object_get_mesh_deform_eval(object_eval);
2723 ss.pbvh = build_pbvh_from_regular_mesh(&object, me_eval_deform);
2724 }
2725 }
2726
2727 return *object::pbvh_get(object);
2728}
2729
2730const pbvh::Tree *pbvh_get(const Object &object)
2731{
2732 if (!object.runtime->sculpt_session) {
2733 return nullptr;
2734 }
2735 return object.runtime->sculpt_session->pbvh.get();
2736}
2737
2739{
2740 BLI_assert(object.type == OB_MESH);
2741 if (!object.runtime->sculpt_session) {
2742 return nullptr;
2743 }
2744 return object.runtime->sculpt_session->pbvh.get();
2745}
2746
2747} // namespace bke::object
2748
2750{
2751 return object->runtime->sculpt_session && object->runtime->sculpt_session->bm;
2752}
2753
2755{
2757 if (ss == nullptr || ss->mode_type != OB_MODE_SCULPT) {
2758 return false;
2759 }
2761 if (!pbvh) {
2762 return false;
2763 }
2764
2765 /* External render engines like Cycles do not have access to the pbvh::Tree
2766 * like Eevee does, and need evaluated mesh geometry to render from. */
2767 const bool external_engine = rv3d && rv3d->view_render != nullptr;
2768
2769 if (pbvh->type() == bke::pbvh::Type::Mesh) {
2770 /* Regular mesh only draws from pbvh::Tree without modifiers and shape keys,
2771 * and without external render engine. */
2772 return !(ss->shapekey_active || ss->deform_modifiers_active || external_engine);
2773 }
2774
2775 if (pbvh->type() == bke::pbvh::Type::BMesh) {
2776 /* Dyntopo draws from pbvh::Tree, except for external render engines. */
2777 return !external_engine;
2778 }
2779
2780 /* Multires always draws directly from the pbvh::Tree. */
2781 return true;
2782}
2783
2784/* Returns the face set random color for rendering in the overlay given its ID and a color seed. */
2785#define GOLDEN_RATIO_CONJUGATE 0.618033988749895f
2786void BKE_paint_face_set_overlay_color_get(const int face_set, const int seed, uchar r_color[4])
2787{
2788 float rgba[4];
2789 float random_mod_hue = GOLDEN_RATIO_CONJUGATE * (face_set + (seed % 10));
2790 random_mod_hue = random_mod_hue - floorf(random_mod_hue);
2791 const float random_mod_sat = BLI_hash_int_01(face_set + seed + 1);
2792 const float random_mod_val = BLI_hash_int_01(face_set + seed + 2);
2793 hsv_to_rgb(random_mod_hue,
2794 0.6f + (random_mod_sat * 0.25f),
2795 1.0f - (random_mod_val * 0.35f),
2796 &rgba[0],
2797 &rgba[1],
2798 &rgba[2]);
2799 rgba_float_to_uchar(r_color, rgba);
2800}
2801
2802} // namespace blender
Generic geometry attributes built on CustomData.
support for deformation groups and hooks.
General operations, lookup, etc. for materials.
General operations, lookup, etc. for blender objects.
A BVH for high poly meshes.
#define M_PI
#define BLO_read_struct_list(reader, struct_name, list)
#define BLO_read_struct(reader, struct_name, ptr_p)
#define BLT_I18NCONTEXT_ID_PALETTE
#define BLT_I18NCONTEXT_ID_PAINTCURVE
#define ID_IS_LINKED(_id)
Definition DNA_ID.h:697
#define FILTER_ID_PC
Definition DNA_ID.h:1218
#define FILTER_ID_PAL
Definition DNA_ID.h:1217
#define BRUSH_OVERLAY_OVERRIDE_MASK
Object is a sort of wrapper for general info.
#define USER_EXPERIMENTAL_TEST(userdef, member)
Read Blender memory allocation and freeing.
#define PRF_scope(category)
#define C
Definition RandGen.cpp:29
#define NC_BRUSH
Definition WM_types.hh:398
#define NA_SELECTED
Definition WM_types.hh:639
#define GOLDEN_RATIO_CONJUGATE
#define BLI_assert_unreachable()
#define INIT_DEFAULT_STRUCT_AFTER(struct_dst, member)
#define ELEM(...)
#define BM_FACE_FIRST_LOOP(p)
#define BM_elem_index_get(ele)
#define BM_elem_flag_test(ele, hflag)
constexpr Span()=default
static AttributeOwner from_id(ID *id)
constexpr int64_t size() const
constexpr Span slice(int64_t start, int64_t size) const
Definition BLI_span.hh:141
constexpr const char * c_str() const
GAttributeReader lookup_or_default(StringRef name, AttrDomain domain, AttrType data_type, const void *default_value=nullptr) const
GAttributeReader lookup(const StringRef name) const
bool add(const StringRef name, const AttrDomain domain, const AttrType data_type, const AttributeInit &initializer)
static Tree from_bmesh(BMesh &bm)
static Tree from_grids(const Mesh &base_mesh, const SubdivCCG &subdiv_ccg)
Definition pbvh.cc:465
static Tree from_mesh(const Mesh &mesh)
Definition pbvh.cc:304
constexpr bool is_empty() const
float distance(float, float) RET
bool implements_3d_texture_paint(const Brush &brush)
Definition brush.cc:1850
int face_corner_prev(const IndexRange face, const int corner)
Definition BKE_mesh.hh:306
int face_corner_next(const IndexRange face, const int corner)
Definition BKE_mesh.hh:315
pbvh::Tree & pbvh_ensure(Depsgraph &depsgraph, Object &object)
pbvh::Tree * pbvh_get(Object &object)
void stroke_track_location(Paint &paint, float3 location)
bool supports_symmetry_tiling(PaintMode paint_mode, const Brush &brush)
void set_overlay_brush_override(Paint &paint, eOverlayFlags flag)
void stroke_set_location(Paint &paint, float3 location)
void invalidate_overlay_all(Scene &scene)
float3 stroke_get_average(const Paint *paint, const Object *ob)
void reset_overlay_flag(Paint &paint, eOverlayControlFlags flag)
bool supports_scene_size(PaintMode paint_mode, const Brush &brush)
void invalidate_overlay_tex(Scene &scene, const Tex *tex)
void invalidate_cursor_overlay(Scene &scene, CurveMapping *curve)
eOverlayControlFlags get_overlay_flags(const Paint &paint)
void cursor_reinitialize_textures(Paint &paint)
void update_normals(const Depsgraph &depsgraph, Object &object_orig, Tree &pbvh)
Definition pbvh.cc:1179
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
Span< float3 > vert_positions_eval(const Depsgraph &depsgraph, const Object &object_orig)
Definition pbvh.cc:933
ID * asset_edit_id_from_weak_reference(Main &global_main, ID_Type id_type, const AssetWeakReference &weak_ref)
static std::unique_ptr< pbvh::Tree > build_pbvh_from_regular_mesh(Object *ob, const Mesh *me_eval_deform)
std::optional< AssetWeakReference > asset_edit_weak_reference_from_id(const ID &id)
static std::unique_ptr< pbvh::Tree > build_pbvh_from_ccg(Object *ob, SubdivCCG &subdiv_ccg)
static std::unique_ptr< pbvh::Tree > build_pbvh_for_dynamic_topology(Object *ob)
bool asset_edit_id_is_editable(const ID &id)
void shallow_swap(T &a, T &b)
Definition DNA_defs.h:164
internal::ShallowDataConstRef< T > shallow_copy(const T &other)
Definition DNA_defs.h:147
CartesianBasis invert(const CartesianBasis &basis)
T interpolate(const T &a, const T &b, const FactorT &t)
float perlin(float position)
Definition noise.cc:664
void parallel_for(const IndexRange range, const int64_t grain_size, const Function &function, const TaskSizeHints &size_hints=detail::TaskSizeHints_Static(1))
Definition BLI_task.hh:69
BMesh Iterators.
void WM_main_add_notifier(uint type, void *reference)
CurveMapping * BKE_curvemapping_add(int tot, float minx, float miny, float maxx, float maxy)
Definition colortools.cc:99
UserDef U
Definition blender.cc:58
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void BKE_sculptsession_free(Object *ob)
static void paint_curve_blend_write(BlendWriter *writer, ID *id, const void *id_address)
static bool paint_brush_update_from_asset_reference(Main *bmain, Paint *paint)
static void palette_foreach_working_space_color(ID *id, const IDTypeForeachColorFunctionCallback &fn)
void BKE_palette_color_set(PaletteColor *color, const float rgb[3])
CurveMapping * BKE_curvemapping_copy(const CurveMapping *cumap)
const EnumPropertyItem rna_enum_brush_gpencil_weight_types_items[]
Definition rna_brush.cc:302
eObjectMode BKE_paint_object_mode_from_paintmode(PaintMode mode)
Object * BKE_view_layer_active_object_get(const ViewLayer *view_layer)
void linearrgb_to_srgb_v3_v3(float srgb[3], const float linear[3])
void BKE_id_attributes_default_color_set(struct ID *id, std::optional< StringRef > name)
void BKE_pbvh_mark_rebuild_pixels(bke::pbvh::Tree &pbvh)
Definition pbvh.cc:1606
void BM_mesh_free(BMesh *bm)
BMesh Free Mesh.
@ GPWEIGHT_BRUSH_TYPE_SMEAR
@ GPWEIGHT_BRUSH_TYPE_DRAW
@ GPWEIGHT_BRUSH_TYPE_BLUR
@ GPWEIGHT_BRUSH_TYPE_AVERAGE
bool BKE_object_sculpt_use_dyntopo(const Object *object)
std::variant< std::monostate, int, BMVert * > ActiveVert
Definition BKE_paint.hh:357
MultiresModifierData * BKE_sculpt_multires_active(const Scene *scene, Object *ob)
MINLINE void copy_v3_v3(float r[3], const float a[3])
unsigned int uint
Definition BKE_text.h:55
void BKE_paint_brushes_set_default_references(ToolSettings *ts)
static AssetWeakReference * paint_brush_asset_reference_ptr_from_essentials(const char *name, const bke::paint::AssetCategory asset_category)
static void palette_undo_preserve(BlendLibReader *, ID *id_new, ID *id_old)
bool BKE_paint_brush_set_default(Main *bmain, Paint *paint)
void hsv_to_rgb(float h, float s, float v, float *r_r, float *r_g, float *r_b)
Definition math_color.cc:25
static std::optional< AssetWeakReference > paint_brush_asset_reference_from_essentials(const char *name, const bke::paint::AssetCategory asset_category)
std::optional< AssetWeakReference > BKE_paint_brush_type_default_reference(bke::paint::AssetCategory asset_category, std::optional< int > brush_type)
void BKE_sculpt_sync_face_visibility_to_grids(const Mesh &mesh, SubdivCCG &subdiv_ccg)
bool BKE_paint_calculate_rake_rotation(Paint &paint, const Brush &brush, const float mouse_pos[2], PaintMode paint_mode, bool in_stroke, bool is_first_dab)
static bool sculpt_modifiers_active(const Scene *scene, const Sculpt *sd, Object *ob)
void id_us_plus(ID *id)
Definition lib_id.cc:433
MINLINE int square_i(int a)
void BKE_paint_settings_foreach_mode(ToolSettings *ts, FunctionRef< void(Paint &paint)> fn)
bool CustomData_has_layer(const CustomData *data, eCustomDataType type)
void void void void void void BLI_duplicatelist(ListBase *dst, const ListBase *src) ATTR_NONNULL(1
Definition listbase.cc:834
static void paint_curve_copy_data(Main *, std::optional< Library * >, ID *id_dst, const ID *id_src, const int)
MINLINE void copy_v2_v2(float r[2], const float a[2])
void BKE_paint_previous_asset_reference_clear(Paint *paint)
IDTypeInfo IDType_ID_PAL
@ BRUSH_COLOR_JITTER_USE_SAT_RAND_PRESS
@ BRUSH_COLOR_JITTER_USE_HUE_AT_STROKE
@ BRUSH_COLOR_JITTER_USE_VAL_AT_STROKE
@ BRUSH_COLOR_JITTER_USE_HUE_RAND_PRESS
@ BRUSH_COLOR_JITTER_USE_SAT_AT_STROKE
@ BRUSH_COLOR_JITTER_USE_VAL_RAND_PRESS
KeyBlock * BKE_keyblock_from_object(Object *ob)
Definition key.cc:1407
void BKE_sculptsession_update_before_eval(Object *ob_eval)
void rgb_to_hsv_v(const float rgb[3], float r_hsv[3])
static void paint_brush_set_default_reference(Paint *paint, const bool do_regular=true)
@ WPAINT_BRUSH_TYPE_AVERAGE
@ WPAINT_BRUSH_TYPE_SMEAR
@ WPAINT_BRUSH_TYPE_DRAW
@ WPAINT_BRUSH_TYPE_BLUR
char * BLI_sprintfN(const char *__restrict format,...) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1) ATTR_MALLOC ATTR_PRINTF_FORMAT(1
void BKE_curvemapping_blend_write(BlendWriter *writer, const CurveMapping *cumap)
void BKE_pbvh_vert_coords_apply(bke::pbvh::Tree &pbvh, Span< float3 > vert_positions)
Definition pbvh.cc:2428
const EnumPropertyItem rna_enum_brush_gpencil_types_items[]
Definition rna_brush.cc:221
IDTypeInfo IDType_ID_PC
MINLINE void sub_v2_v2v2(float r[2], const float a[2], const float b[2])
static void palette_blend_write(BlendWriter *writer, ID *id, const void *id_address)
int CCG_grid_factor(int low_level, int high_level)
Definition BKE_ccg.hh:112
MINLINE int max_ii(int a, int b)
void BM_log_free(BMLog *log)
Definition bmesh_log.cc:501
int BKE_sculpt_get_grid_num_faces(const Object &object)
void BKE_mesh_tessface_clear(Mesh *mesh)
void BKE_sculptsession_bm_to_me(Object *ob)
void BKE_paint_copy(const Paint *src, Paint *dst, int flag)
std::string BKE_attribute_calc_unique_name(const AttributeOwner &owner, StringRef name)
void BKE_paint_mesh_automasking_settings_ensure(Paint &paint)
void BKE_curvemapping_free(CurveMapping *cumap)
void id_fake_user_set(ID *id)
Definition lib_id.cc:471
void BKE_id_blend_write(BlendWriter *writer, ID *id)
Definition lib_id.cc:2890
@ ASSET_LIBRARY_ESSENTIALS
void BKE_sculptsession_free_pbvh(Object &object)
bool BKE_paint_is_grid_face_hidden(BoundedBitSpan grid_hidden, int gridsize, int x, int y)
@ CTX_MODE_VERTEX_GREASE_PENCIL
@ CTX_MODE_PAINT_VERTEX
@ CTX_MODE_VERTEX_GPENCIL_LEGACY
@ CTX_MODE_WEIGHT_GPENCIL_LEGACY
@ CTX_MODE_SCULPT_CURVES
@ CTX_MODE_SCULPT
@ CTX_MODE_PAINT_GREASE_PENCIL
@ CTX_MODE_PAINT_GPENCIL_LEGACY
@ CTX_MODE_SCULPT_GREASE_PENCIL
@ CTX_MODE_PAINT_TEXTURE
@ CTX_MODE_SCULPT_GPENCIL_LEGACY
@ CTX_MODE_WEIGHT_GREASE_PENCIL
@ CTX_MODE_PAINT_WEIGHT
@ LIB_ID_CREATE_NO_USER_REFCOUNT
BLI_INLINE float BLI_hash_int_01(unsigned int k)
Definition BLI_hash_c.hh:94
ViewLayer * CTX_data_view_layer(const bContext *C)
BitGroupVector & BKE_subdiv_ccg_grid_hidden_ensure(SubdivCCG &subdiv_ccg)
void BKE_sculpt_mask_layers_ensure(Depsgraph *depsgraph, Main *bmain, Object *ob, MultiresModifierData *mmd)
void BKE_scene_graph_evaluated_ensure(Depsgraph *depsgraph, Main *bmain)
Definition scene.cc:2872
static void palette_free_data(ID *id)
static void paint_curve_blend_read_data(BlendDataReader *reader, ID *id)
CCGKey BKE_subdiv_ccg_key_top_level(const SubdivCCG &subdiv_ccg)
void BKE_subdiv_ccg_grid_hidden_free(SubdivCCG &subdiv_ccg)
@ SCULPT_BRUSH_TYPE_SIMPLIFY
@ SCULPT_BRUSH_TYPE_CLAY_STRIPS
@ SCULPT_BRUSH_TYPE_DRAW_FACE_SETS
@ SCULPT_BRUSH_TYPE_PAINT
@ SCULPT_BRUSH_TYPE_DISPLACEMENT_ERASER
@ SCULPT_BRUSH_TYPE_MASK
@ SCULPT_BRUSH_TYPE_DISPLACEMENT_SMEAR
@ GPSCULPT_BRUSH_TYPE_CLONE
static void paint_brush_default_essentials_name_get(const bke::paint::AssetCategory asset_category, std::optional< int > brush_type, StringRefNull *r_name)
std::optional< int > BKE_paint_get_brush_type_from_paintmode(const Brush *brush, PaintMode mode)
const EnumPropertyItem rna_enum_brush_image_brush_type_items[]
Definition rna_brush.cc:211
void BKE_curvemapping_init(CurveMapping *cumap)
PaintCurve * BKE_paint_curve_add(Main *bmain, const char *name)
const EnumPropertyItem rna_enum_brush_gpencil_vertex_types_items[]
Definition rna_brush.cc:241
@ GPAINT_BRUSH_TYPE_ERASE
@ GPAINT_BRUSH_TYPE_FILL
@ GPAINT_BRUSH_TYPE_TINT
@ GPAINT_BRUSH_TYPE_DRAW
void BKE_curvemapping_blend_read(BlendDataReader *reader, CurveMapping *cumap)
void BKE_palette_color_sync_legacy(PaletteColor *color)
static void paint_runtime_init(const ToolSettings *ts, Paint *paint)
Brush * BKE_paint_brush_from_essentials(Main *bmain, bke::paint::AssetCategory asset_category, const char *name)
VecBase< float, 2 > float2
unsigned char uchar
Definition BKE_text.h:58
@ ME_EDIT_PAINT_VERT_SEL
@ ME_EDIT_PAINT_FACE_SEL
void BLI_remlink(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:138
void DEG_id_tag_update(ID *id, unsigned int flags)
void hsv_to_rgb_v(const float hsv[3], float r_rgb[3])
Definition math_color.cc:61
@ OB_MODE_VERTEX_GREASE_PENCIL
@ OB_MODE_TEXTURE_PAINT
@ OB_MODE_SCULPT_GREASE_PENCIL
@ OB_MODE_SCULPT_CURVES
@ OB_MODE_WEIGHT_GREASE_PENCIL
@ OB_MODE_PAINT_GREASE_PENCIL
BMesh const char void * data
void BKE_sculptsession_update_after_eval(Depsgraph *depsgraph, Object *ob_eval)
Mesh * BKE_mesh_from_object(Object *ob)
T * DEG_get_evaluated(const Depsgraph *depsgraph, T *id)
bool BKE_paint_select_elem_test(const Object *ob)
void BKE_curvemap_reset(CurveMap *cuma, const rctf *clipr, int preset, CurveMapSlopeType slope)
void BKE_paint_blend_read_data(BlendDataReader *reader, const Scene *scene, Paint *paint)
void BKE_paint_brushes_ensure(Main *bmain, Paint *paint)
Palette * BKE_paint_palette(Paint *paint)
void BKE_paint_init(Main *bmain, Scene *sce, PaintMode mode, bool ensure_brushes=true)
ModifierData * BKE_modifiers_get_virtual_modifierlist(const Object *ob, VirtualModifierData *data)
void BKE_curvemapping_changed(CurveMapping *cumap, bool rem_doubles)
int BKE_sculpt_get_grid_num_verts(const Object &object)
PaintMode BKE_paintmode_get_from_tool(const bToolRef *tref)
const EnumPropertyItem rna_enum_brush_sculpt_brush_type_items[]
Definition rna_brush.cc:148
@ IMAGE_PAINT_BRUSH_TYPE_SOFTEN
@ IMAGE_PAINT_BRUSH_TYPE_SMEAR
@ IMAGE_PAINT_BRUSH_TYPE_DRAW
@ IMAGE_PAINT_BRUSH_TYPE_FILL
@ IMAGE_PAINT_BRUSH_TYPE_MASK
@ IMAGE_PAINT_BRUSH_TYPE_CLONE
static AssetWeakReference * asset_reference_create_from_brush(Brush *brush)
CurveMapping * BKE_sculpt_default_cavity_curve()
Definition scene.cc:142
bool BKE_view_layer_synced_ensure(const Main &bmain, const Scene *scene, ViewLayer *view_layer)
const EnumPropertyItem rna_enum_brush_vertex_brush_type_items[]
Definition rna_brush.cc:195
void BKE_paint_foreach_asset_weak_reference(Paint &paint, FunctionRef< void(AssetWeakReference &weak_ref)> fn)
bool BKE_paint_always_hide_test(const Object *ob)
static const char * paint_brush_essentials_asset_file_name_from_asset_category(const bke::paint::AssetCategory asset_category)
void BKE_asset_weak_reference_write(BlendWriter *writer, const AssetWeakReference *weak_ref)
static bool paint_rake_rotation_active(const MTex &mtex)
@ CURVES_SCULPT_BRUSH_TYPE_ADD
@ CURVES_SCULPT_BRUSH_TYPE_DENSITY
@ CURVES_SCULPT_BRUSH_TYPE_DELETE
@ CURVES_SCULPT_BRUSH_TYPE_SELECTION_PAINT
bool BKE_paint_brush_set(Paint *paint, Brush *brush)
void BKE_paint_cavity_curve_preset(Paint *paint, int preset)
void BKE_paint_update_brush_rake_rotation(Paint &paint, const Brush &brush, float rotation)
const void * CustomData_get_layer(const CustomData *data, eCustomDataType type)
bool BKE_paint_select_face_test(const Object *ob)
void BKE_paint_previous_asset_reference_set(Paint *paint, AssetWeakReference &&asset_weak_reference)
void BKE_image_pool_free(ImagePool *pool)
MatBase< float, 3, 3 > float3x3
Mesh * BKE_object_get_evaluated_mesh_unchecked(const Object *object)
Definition object.cc:4427
int BLI_listbase_count_at_most(const ListBase *listbase, int count_max) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:518
float BKE_paint_grid_paint_mask(const GridPaintMask *gpm, uint level, uint x, uint y)
void BKE_sculpt_color_layer_create_if_needed(Object *object)
static MultiresModifierData * sculpt_multires_modifier_get(const Scene *scene, Object *ob, const bool auto_create_mdisps)
void BKE_palette_color_remove(Palette *palette, PaletteColor *color)
void BKE_paint_curve_clamp_endpoint_add_index(PaintCurve *pc, int add_index)
int CCG_grid_xy_to_index(const int grid_size, const int x, const int y)
Definition BKE_ccg.hh:75
void BLO_read_array_and_validate_size(BlendDataReader *reader, T **ptr_p, SizeT *array_size, const int elems=1)
PaletteColor * BKE_palette_color_add(Palette *palette)
void BKE_sculptsession_free_deformMats(SculptSession *ss)
const Brush * BKE_paint_brush_for_read(const Paint *paint)
SpaceImage * CTX_wm_space_image(const bContext *C)
const EnumPropertyItem * BKE_paint_get_tool_enum_from_paintmode(PaintMode mode)
MINLINE float len_squared_v2(const float v[2]) ATTR_WARN_UNUSED_RESULT
void BKE_lib_id_swap(Main *bmain, ID *id_a, ID *id_b, const bool do_self_remap, const int self_remap_flags)
Definition lib_id.cc:1130
@ GPVERTEX_BRUSH_TYPE_REPLACE
@ GPVERTEX_BRUSH_TYPE_BLUR
@ GPVERTEX_BRUSH_TYPE_AVERAGE
@ GPVERTEX_BRUSH_TYPE_TINT
@ GPVERTEX_BRUSH_TYPE_SMEAR
@ GPVERTEX_BRUSH_TYPE_DRAW
void BM_data_layer_ensure_named(BMesh *bm, CustomData *data, int type, const StringRef name)
static void palette_init_data(ID *id)
T * DEG_get_original(T *id)
bool BKE_sculptsession_use_pbvh_draw(const Object *ob, const RegionView3D *rv3d)
bool BKE_paint_can_use_brush(const Paint *paint, const Brush *brush)
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118
const EnumPropertyItem rna_enum_brush_weight_brush_type_items[]
Definition rna_brush.cc:203
Main * CTX_data_main(const bContext *C)
static void paint_init_data(Paint &paint)
char * BLI_strdup(const char *str) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1) ATTR_MALLOC
void BKE_paint_free(Paint *paint)
@ INDEX_ID_PC
Definition DNA_ID.h:1347
@ INDEX_ID_PAL
Definition DNA_ID.h:1346
static void palette_blend_read_data(BlendDataReader *reader, ID *id)
Paint * BKE_paint_get_active_from_context(const bContext *C)
void BKE_paint_face_set_overlay_color_get(int face_set, int seed, uchar r_color[4])
bool BKE_brush_has_cube_tip(const Brush *brush, PaintMode paint_mode)
Definition brush.cc:1816
void GPU_texture_free(gpu::Texture *texture)
Paint * BKE_paint_get_active_from_paintmode(Scene *sce, PaintMode mode)
void BKE_asset_weak_reference_read(BlendDataReader *reader, AssetWeakReference *weak_ref)
void BKE_id_attributes_active_color_set(struct ID *id, std::optional< StringRef > name)
bool BKE_paint_is_bmesh_face_hidden(const BMFace *f)
@ ID_RECALC_GEOMETRY_ALL_MODES
Definition DNA_ID.h:1183
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:1074
Scene * DEG_get_input_scene(const Depsgraph *graph)
@ eMultiresModifierFlag_UseSculptBaseMesh
int CCG_grid_size(const int level)
Definition BKE_ccg.hh:106
std::optional< int > BKE_paint_get_brush_type_from_obmode(const Brush *brush, eObjectMode ob_mode)
void BKE_sculptsession_update_for_edit(Depsgraph *depsgraph, Object *ob_orig, bool is_paint_tool)
CurveMapping * BKE_paint_default_curve()
Definition scene.cc:157
Brush * BKE_paint_brush(Paint *paint)
void BKE_texpaint_slots_refresh_object(Scene *scene, Object *ob)
Scene * CTX_data_scene(const bContext *C)
bool BKE_paint_select_grease_pencil_test(const Object *ob)
Mesh * BKE_object_get_original_mesh(const Object *object)
Definition object.cc:4461
void BKE_palette_clear(Palette *palette)
void BM_mesh_bm_to_me(Main *bmain, BMesh *bm, Mesh *mesh, const BMeshToMeshParams *params)
bool BKE_paint_ensure_from_paintmode(Scene *sce, PaintMode mode)
VecBase< float, 3 > float3
static void paint_brush_set_essentials_reference(Paint *paint, const char *name)
bool BKE_paint_select_vert_test(const Object *ob)
@ BRUSH_OVERLAY_CURSOR_OVERRIDE_ON_STROKE
@ BRUSH_OVERLAY_SECONDARY_OVERRIDE_ON_STROKE
@ BRUSH_OVERLAY_PRIMARY_OVERRIDE_ON_STROKE
bool BKE_paint_ensure(ToolSettings *ts, Paint **r_paint)
bool BKE_paint_brush_set_essentials(Main *bmain, Paint *paint, const char *name)
void BKE_paint_brushes_validate(Main *bmain, Paint *paint)
bool BKE_modifier_is_enabled(const Scene *scene, ModifierData *md, int required_mode)
static void sculptsession_bm_to_me_update_data_only(Object *ob)
float3 BKE_paint_randomize_color(const BrushColorJitterSettings &color_jitter, const float3 &initial_hsv_jitter, const float distance, const float pressure, const float3 &color)
std::string BKE_paint_canvas_key_get(ImagePaintSettings &settings, Object *ob)
static void paint_curve_free_data(ID *id)
void id_us_min(ID *id)
Definition lib_id.cc:441
const ModifierTypeInfo * BKE_modifier_get_info(ModifierType type)
PaintMode BKE_paintmode_get_active_from_context(const bContext *C)
static void sculptsession_update(Depsgraph *depsgraph, Object *ob, Object *ob_eval, bool is_paint_tool)
float BKE_curvemapping_evaluateF(const CurveMapping *cumap, int cur, float value)
void * BKE_id_new(Main *bmain, short type, const char *name)
Definition lib_id.cc:1612
void BKE_paint_palette_set(Paint *paint, Palette *palette)
static float paint_rake_rotation_spacing(const Paint &, const Brush &brush, bool in_stroke, bool is_first_dab)
void BKE_crazyspace_build_sculpt(Depsgraph *depsgraph, Scene *scene, Object *ob, Array< float3x3, 0 > &deformmats, Array< float3, 0 > &deformcos)
Paint * BKE_paint_get_active(const Main &bmain, Scene *sce, ViewLayer *view_layer)
const EnumPropertyItem rna_enum_brush_gpencil_sculpt_types_items[]
Definition rna_brush.cc:266
static bool check_sculpt_object_deformed(Object *object, const bool for_construction)
void BLO_read_string(BlendDataReader *reader, char **ptr_p)
Definition readfile.cc:6653
static void palette_copy_data(Main *, std::optional< Library * >, ID *id_dst, const ID *id_src, const int)
MINLINE void rgba_float_to_uchar(unsigned char r_col[4], const float col_f[4])
const Mesh * BKE_object_get_mesh_deform_eval(const Object *object)
Definition object.cc:4519
void BKE_paint_blend_write(BlendWriter *writer, Paint *paint)
void * MEM_new_array_zeroed(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
void * CustomData_add_layer(CustomData *data, eCustomDataType type, eCDAllocType alloctype, int totelem)
Palette * BKE_palette_add(Main *bmain, const char *name)
@ CUMA_EXTEND_EXTRAPOLATE
bool BKE_id_attributes_color_find(const struct ID *id, StringRef name)
@ VPAINT_BRUSH_TYPE_DRAW
@ VPAINT_BRUSH_TYPE_SMEAR
@ VPAINT_BRUSH_TYPE_AVERAGE
@ VPAINT_BRUSH_TYPE_BLUR
const EnumPropertyItem rna_enum_brush_curves_sculpt_brush_type_items[]
Definition rna_brush.cc:314
@ IDTYPE_FLAGS_NO_ANIMDATA
Definition BKE_idtype.hh:52
#define floorf
#define atan2f
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
eAssetLibraryType asset_library_type
struct BMLoop * next
struct BMVert * v
CustomData pdata
CustomData vdata
void write_string(const char *data)
void write_id_struct(const void *id_address, const T *id, BlendStructWriterTypedFn< T > fn=nullptr)
void write_struct_list(const ListBaseT< T > *list, BlendStructWriterTypedFn< T > fn=nullptr)
void write_struct(const T *data, BlendStructWriterTypedFn< T > fn=nullptr)
void write_struct_array(const int64_t array_size, const T *data, BlendStructWriterTypedFn< T > fn=nullptr)
eCurveMappingFlags flag
eCurveMappingPreset preset
IDProperty * system_properties
Definition DNA_ID.h:490
IDProperty * properties
Definition DNA_ID.h:481
eMTex_BrushAngleMode brush_angle_mode
bool is_locked_for_linking
Definition BKE_main.hh:313
struct CurveMapping * cavity_curve_op
bke::MeshRuntime * runtime
char * active_color_attribute
struct ModifierData * next
struct AssetWeakReference * brush_asset_reference
bke::ObjectRuntime * runtime
eObject_ShapeFlag shapeflag
PaintCurvePoint * points
struct Brush * brush
struct CurveMapping * cavity_curve
struct UnifiedPaintSettings unified_paint_settings
bke::PaintRuntime * runtime
ToolSystemBrushBindings tool_brush_bindings
struct AssetWeakReference * brush_asset_reference
struct Palette * palette
struct MeshAutomaskingSettings * mesh_automasking_settings
ListBaseT< PaletteColor > colors
struct ViewRender * view_render
struct ToolSettings * toolsettings
std::unique_ptr< SculptBoundaryInfoCache > boundary_info_cache
Definition BKE_paint.hh:458
ed::sculpt_paint::StrokeCache * cache
Definition BKE_paint.hh:409
std::optional< std::string > last_paint_canvas_key
Definition BKE_paint.hh:494
std::optional< int > active_face_index
Definition BKE_paint.hh:414
Array< int > vert_to_edge_offsets
Definition BKE_paint.hh:376
Array< int > vert_to_edge_indices
Definition BKE_paint.hh:377
KeyBlock * shapekey_active
Definition BKE_paint.hh:368
void clear_active_elements(bool persist_last_active)
Array< float3, 0 > deform_cos
Definition BKE_paint.hh:395
GroupedSpan< int > vert_to_edge_map
Definition BKE_paint.hh:378
Array< int > edge_to_face_offsets
Definition BKE_paint.hh:371
std::unique_ptr< SculptTopologyIslandCache > topology_island_cache
Definition BKE_paint.hh:497
void set_active_vert(ActiveVert vert)
SculptFakeNeighbors fake_neighbors
Definition BKE_paint.hh:459
float3 active_vert_position(const Depsgraph &depsgraph, const Object &object) const
std::optional< int > active_grid_index
Definition BKE_paint.hh:415
std::optional< PersistentMultiresData > persistent_multires_data()
ed::sculpt_paint::filter::Cache * filter_cache
Definition BKE_paint.hh:410
Array< float3x3, 0 > deform_imats
Definition BKE_paint.hh:397
Array< int > edge_to_face_indices
Definition BKE_paint.hh:372
MultiresModifierData * multires_modifier
Definition BKE_paint.hh:385
SharedCache< Vector< float3 > > vert_normals_deform
Definition BKE_paint.hh:403
struct blender::SculptSession::@037313230252170341305206315164127110104334247060 persistent
GroupedSpan< int > edge_to_face_map
Definition BKE_paint.hh:373
ed::sculpt_paint::expand::Cache * expand_cache
Definition BKE_paint.hh:411
Array< int > preview_verts
Definition BKE_paint.hh:432
std::unique_ptr< bke::pbvh::Tree > pbvh
Definition BKE_paint.hh:390
SharedCache< Vector< float3 > > face_normals_deform
Definition BKE_paint.hh:404
eSpaceImage_Mode mode
CurvesSculpt * curves_sculpt
struct ImagePaintSettings imapaint
GpSculptPaint * gp_sculptpaint
GpVertexPaint * gp_vertexpaint
GpWeightPaint * gp_weightpaint
struct AssetWeakReference * main_brush_asset_reference
ListBaseT< NamedBrushAssetReference > active_brush_per_brush_type
eUnifiedPaintSettingsFlags flag
struct CurveMapping * curve_rand_saturation
struct CurveMapping * curve_rand_value
struct CurveMapping * curve_rand_hue
std::unique_ptr< SubdivCCG > subdiv_ccg
paint::AssetCategory asset_category
std::unique_ptr< paint::TexSnapshot > primary_snap
std::unique_ptr< paint::TexSnapshot > secondary_snap
std::unique_ptr< paint::CursorSnapshot > cursor_snap
#define N_(msgid)