Blender V5.3
sculpt_paint_color.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2020 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "DNA_brush_types.h"
10#include "DNA_meshdata_types.h"
11
12#include "BLI_color.hh"
14#include "BLI_hash_c.hh"
16#include "BLI_math_matrix.hh"
17#include "BLI_math_vector.hh"
18#include "BLI_vector.hh"
19
20#include "BKE_attribute.hh"
21#include "BKE_brush.hh"
22#include "BKE_colorband.hh"
23#include "BKE_colortools.hh"
24#include "BKE_customdata.hh"
25#include "BKE_mesh.hh"
26#include "BKE_object_types.hh"
27#include "BKE_paint.hh"
28#include "BKE_paint_bvh.hh"
29#include "BKE_paint_types.hh"
30
32
33#include "mesh_brush_common.hh"
34#include "sculpt_automask.hh"
35#include "sculpt_color.hh"
36#include "sculpt_intern.hh"
37#include "sculpt_smooth.hh"
38
39#include "IMB_imbuf.hh"
40
41#include <cmath>
42
44
45template<typename Func> inline void to_static_color_type(const CPPType &type, const Func &func)
46{
47 if (type.is<ColorGeometry4f>()) {
48 func(MPropCol());
49 }
50 else if (type.is<ColorGeometry4b>()) {
51 func(MLoopCol());
52 }
53}
54
55template<typename T> float4 to_float(const T &src);
56
57template<> float4 to_float(const MLoopCol &src)
58{
59 float4 dst;
60 rgba_uchar_to_float(dst, reinterpret_cast<const uchar *>(&src));
62 return dst;
63}
64template<> float4 to_float(const MPropCol &src)
65{
66 return src.color;
67}
68
69template<typename T> void from_float(const float4 &src, T &dst);
70
71template<> void from_float(const float4 &src, MLoopCol &dst)
72{
73 float4 temp;
75 temp[3] = src[3];
76 rgba_float_to_uchar(reinterpret_cast<uchar *>(&dst), temp);
77}
78template<> void from_float(const float4 &src, MPropCol &dst)
79{
80 copy_v4_v4(dst.color, src);
81}
82
83template<typename T>
85 const Span<int> corner_verts,
86 const GroupedSpan<int> vert_to_face_map,
87 const GSpan color_attribute,
88 const bke::AttrDomain color_domain,
89 const int vert)
90{
91 const T *colors_typed = static_cast<const T *>(color_attribute.data());
92 if (color_domain == bke::AttrDomain::Corner) {
93 float4 r_color(0.0f);
94 for (const int face : vert_to_face_map[vert]) {
95 const int corner = bke::mesh::face_find_corner_from_vert(faces[face], corner_verts, vert);
96 r_color += to_float(colors_typed[corner]);
97 }
98 return r_color / float(vert_to_face_map[vert].size());
99 }
100 return to_float(colors_typed[vert]);
101}
102
103template<typename T>
104static void color_vert_set(const OffsetIndices<int> faces,
105 const Span<int> corner_verts,
106 const GroupedSpan<int> vert_to_face_map,
107 const GMutableSpan color_attribute,
108 const bke::AttrDomain color_domain,
109 const int vert,
110 const float4 &color)
111{
112 if (color_domain == bke::AttrDomain::Corner) {
113 for (const int i_face : vert_to_face_map[vert]) {
114 const IndexRange face = faces[i_face];
115 MutableSpan<T> colors{static_cast<T *>(color_attribute.data()) + face.start(), face.size()};
116 Span<int> face_verts = corner_verts.slice(face);
117
118 for (const int i : IndexRange(face.size())) {
119 if (face_verts[i] == vert) {
120 from_float(color, colors[i]);
121 }
122 }
123 }
124 }
125 else {
126 from_float(color, static_cast<T *>(color_attribute.data())[vert]);
127 }
128}
129
131 const Span<int> corner_verts,
132 const GroupedSpan<int> vert_to_face_map,
133 const GSpan color_attribute,
134 const bke::AttrDomain color_domain,
135 const int vert)
136{
138 to_static_color_type(color_attribute.type(), [&](auto dummy) {
139 using T = decltype(dummy);
140 color = color_vert_get<T>(
141 faces, corner_verts, vert_to_face_map, color_attribute, color_domain, vert);
142 });
143 return color;
144}
145
146float4 color_corner_get(const GSpan color_attribute, int corner_index)
147{
148 float4 r_color;
149 to_static_color_type(color_attribute.type(), [&](auto dummy) {
150 using T = decltype(dummy);
151 const T *colors_typed = static_cast<const T *>(color_attribute.data());
152 r_color = to_float(colors_typed[corner_index]);
153 });
154 return r_color;
155}
156
158 const Span<int> corner_verts,
159 const GroupedSpan<int> vert_to_face_map,
160 const bke::AttrDomain color_domain,
161 const int vert,
162 const float4 &color,
163 const GMutableSpan color_attribute)
164{
165 to_static_color_type(color_attribute.type(), [&](auto dummy) {
166 using T = decltype(dummy);
167 color_vert_set<T>(
168 faces, corner_verts, vert_to_face_map, color_attribute, color_domain, vert, color);
169 });
170}
171
173 GMutableSpan color_attribute,
174 MutableSpan<float4> r_colors)
175{
177 to_static_color_type(color_attribute.type(), [&](auto dummy) {
178 using T = decltype(dummy);
179 T *colors_typed = static_cast<T *>(color_attribute.data());
180 for (const int i : indices.index_range()) {
181 T temp = colors_typed[indices[i]];
182 from_float(r_colors[i], colors_typed[indices[i]]);
183 r_colors[i] = to_float(temp);
184 }
185 });
186}
187
188void gather_colors(const GSpan color_attribute,
189 const Span<int> indices,
190 MutableSpan<float4> r_colors)
191{
192 to_static_color_type(color_attribute.type(), [&](auto dummy) {
193 using T = decltype(dummy);
194 const T *colors_typed = static_cast<const T *>(color_attribute.data());
195 for (const int i : indices.index_range()) {
196 r_colors[i] = to_float(colors_typed[indices[i]]);
197 }
198 });
199}
200
202 const Span<int> corner_verts,
203 const GroupedSpan<int> vert_to_face_map,
204 const GSpan color_attribute,
205 const bke::AttrDomain color_domain,
206 const Span<int> verts,
207 const MutableSpan<float4> r_colors)
208{
210 if (color_domain == bke::AttrDomain::Point) {
211 gather_colors(color_attribute, verts, r_colors);
212 }
213 else {
214 to_static_color_type(color_attribute.type(), [&](auto dummy) {
215 using T = decltype(dummy);
216 for (const int i : verts.index_range()) {
217 r_colors[i] = color_vert_get<T>(
218 faces, corner_verts, vert_to_face_map, color_attribute, color_domain, verts[i]);
219 }
220 });
221 }
222}
223
225{
226 const bke::AttributeAccessor attributes = mesh.attributes();
227 const StringRef name = mesh.active_color_attribute;
228 const bke::GAttributeReader colors = attributes.lookup(name);
229 if (!colors) {
230 return {};
231 }
232 const bke::AttrType data_type = bke::cpp_type_to_attribute_type(colors.varray.type());
233 if (!bke::mesh::is_color_attribute({colors.domain, data_type})) {
234 return {};
235 }
236 return colors;
237}
238
240{
241 bke::MutableAttributeAccessor attributes = mesh.attributes_for_write();
242 const StringRef name = mesh.active_color_attribute;
243 bke::GSpanAttributeWriter colors = attributes.lookup_for_write_span(name);
244 if (!colors) {
245 return {};
246 }
247 const bke::AttrType data_type = bke::cpp_type_to_attribute_type(colors.span.type());
248 if (!bke::mesh::is_color_attribute({colors.domain, data_type})) {
249 colors.finish();
250 return {};
251 }
252 return colors;
253}
254
266
267static void do_color_smooth_task(const Depsgraph &depsgraph,
268 const Object &object,
269 const Span<float3> vert_positions,
270 const Span<float3> vert_normals,
271 const OffsetIndices<int> faces,
272 const Span<int> corner_verts,
273 const GroupedSpan<int> vert_to_face_map,
274 const MeshAttributeData &attribute_data,
275 const Brush &brush,
278 bke::GSpanAttributeWriter &color_attribute)
279{
280 const SculptSession &ss = *object.runtime->sculpt_session;
281 const StrokeCache &cache = *ss.cache;
282
283 const Span<int> verts = node.verts();
284
285 tls.factors.resize(verts.size());
286 const MutableSpan<float> factors = tls.factors;
287 fill_factor_from_hide_and_mask(attribute_data.hide_vert, attribute_data.mask, verts, factors);
288 filter_region_clip_factors(ss, vert_positions, verts, factors);
289 if (brush.flag & BRUSH_FRONTFACE) {
290 calc_front_face(cache.view_normal_symm, vert_normals, verts, factors);
291 }
292
293 tls.distances.resize(verts.size());
294 const MutableSpan<float> distances = tls.distances;
296 ss, vert_positions, verts, eBrushFalloffShape(brush.falloff_shape), distances);
297 filter_distances_with_radius(cache.radius, distances, factors);
298 apply_hardness_to_distances(cache, distances);
299 calc_brush_strength_factors(cache, brush, distances, factors);
300
301 auto_mask::calc_vert_factors(depsgraph, object, cache.automasking.get(), node, verts, factors);
302
303 calc_brush_texture_factors(PaintMode::Sculpt, ss, brush, vert_positions, verts, factors);
304 scale_factors(factors, cache.bstrength);
305
306 tls.colors.resize(verts.size());
307 MutableSpan<float4> colors = tls.colors;
308 for (const int i : verts.index_range()) {
309 colors[i] = color_vert_get(faces,
310 corner_verts,
311 vert_to_face_map,
312 color_attribute.span,
313 color_attribute.domain,
314 verts[i]);
315 }
316
317 const GroupedSpan<int> neighbors = calc_vert_neighbors(faces,
318 corner_verts,
319 vert_to_face_map,
320 attribute_data.hide_poly,
321 verts,
323 tls.neighbor_data);
324
325 tls.new_colors.resize(verts.size());
326 MutableSpan<float4> new_colors = tls.new_colors;
328 corner_verts,
329 vert_to_face_map,
330 color_attribute.span,
331 color_attribute.domain,
332 neighbors,
333 new_colors);
334
335 for (const int i : colors.index_range()) {
336 blend_color_interpolate_float(new_colors[i], colors[i], new_colors[i], factors[i]);
337 }
338
339 for (const int i : verts.index_range()) {
340 color_vert_set(faces,
341 corner_verts,
342 vert_to_face_map,
343 color_attribute.domain,
344 verts[i],
345 new_colors[i],
346 color_attribute.span);
347 }
348}
349
350static void do_paint_brush_task(const Depsgraph &depsgraph,
351 Object &object,
352 const Span<float3> vert_positions,
353 const Span<float3> vert_normals,
354 const OffsetIndices<int> faces,
355 const Span<int> corner_verts,
356 const GroupedSpan<int> vert_to_face_map,
357 const MeshAttributeData &attribute_data,
358 const Paint &paint,
359 const Brush &brush,
360 const float4x4 &mat,
361 const float4 wet_mix_sampled_color,
364 const MutableSpan<float4> mix_colors,
365 bke::GSpanAttributeWriter &color_attribute)
366{
367 const SculptSession &ss = *object.runtime->sculpt_session;
368 const StrokeCache &cache = *ss.cache;
369
370 const float bstrength = fabsf(ss.cache->bstrength);
371 const float alpha = BKE_brush_alpha_get(&paint, &brush);
372
373 const Span<int> verts = node.verts();
374
375 tls.factors.resize(verts.size());
376 const MutableSpan<float> factors = tls.factors;
377 fill_factor_from_hide_and_mask(attribute_data.hide_vert, attribute_data.mask, verts, factors);
378 filter_region_clip_factors(ss, vert_positions, verts, factors);
379 if (brush.flag & BRUSH_FRONTFACE) {
380 calc_front_face(cache.view_normal_symm, vert_normals, verts, factors);
381 }
382
383 float radius;
384
385 tls.distances.resize(verts.size());
386 const MutableSpan<float> distances = tls.distances;
388 tls.positions.resize(verts.size());
389 calc_local_positions(vert_positions,
390 verts,
391 mat,
392 cache.location_symm,
393 cache.view_normal_symm,
394 eBrushFalloffShape(brush.falloff_shape),
395 tls.positions);
397 radius = 1.0f;
398 }
399 else {
401 ss, vert_positions, verts, eBrushFalloffShape(brush.falloff_shape), distances);
402 radius = cache.radius;
403 }
404 filter_distances_with_radius(radius, distances, factors);
405 apply_hardness_to_distances(radius, cache.hardness, distances);
406 BKE_brush_calc_curve_factors(eBrushCurvePreset(brush.curve_distance_falloff_preset),
407 brush.curve_distance_falloff,
408 distances,
409 radius,
410 factors);
411
413 if (cache.automasking) {
414 tls.auto_mask.resize(verts.size());
415 auto_mask = tls.auto_mask;
416 auto_mask.fill(1.0f);
417 auto_mask::calc_vert_factors(depsgraph, object, *cache.automasking, node, verts, auto_mask);
418 scale_factors(factors, auto_mask);
419 }
420
421 calc_brush_texture_factors(PaintMode::Sculpt, ss, brush, vert_positions, verts, factors);
422 scale_factors(factors, bstrength);
423
424 const float density = ss.cache->paint_brush.density;
425 if (density < 1.0f) {
426 BLI_assert(ss.cache->paint_brush.density_seed);
427 const float seed = ss.cache->paint_brush.density_seed.value_or(0.0f);
428 for (const int i : verts.index_range()) {
429 const float hash_noise = BLI_hash_int_01(seed * 1000 * verts[i]);
430 if (hash_noise > density) {
431 const float noise = density * hash_noise;
432 factors[i] *= noise;
433 }
434 }
435 }
436
437 float3 brush_color_rgb = ss.cache->toggle_settings.invert ?
440
441 const std::optional<BrushColorJitterSettings> color_jitter_settings =
443 if (color_jitter_settings) {
444 brush_color_rgb = BKE_paint_randomize_color(*color_jitter_settings,
447 ss.cache->pressure,
448 brush_color_rgb);
449 }
450
451 float4 brush_color(brush_color_rgb, 1.0f);
452
453 const Span<float4> orig_colors = orig_color_data_get_mesh(object, node);
454
456 gather_data_mesh(mix_colors.as_span(), verts, color_buffer.as_mutable_span());
457
458 if (brush.flag & BRUSH_USE_GRADIENT) {
459 switch (brush.gradient_stroke_mode) {
461 BKE_colorband_evaluate(brush.gradient, ss.cache->pressure, brush_color);
462 break;
464 float coord = fmod(ss.cache->stroke_distance / brush.gradient_spacing, 1.0);
465 BKE_colorband_evaluate(brush.gradient, coord, brush_color);
466 break;
467 }
470 brush.gradient, ss.cache->stroke_distance / brush.gradient_spacing, brush_color);
471 break;
472 }
473 }
474 }
475
476 tls.new_colors.resize(verts.size());
477 MutableSpan<float4> new_colors = tls.new_colors;
478 for (const int i : verts.index_range()) {
479 new_colors[i] = color_vert_get(faces,
480 corner_verts,
481 vert_to_face_map,
482 color_attribute.span,
483 color_attribute.domain,
484 verts[i]);
485 }
486
487 for (const int i : verts.index_range()) {
488 /* Brush paint color, brush test falloff and flow. */
489 float4 paint_color = brush_color * factors[i] * ss.cache->paint_brush.flow;
490 float4 wet_mix_color = wet_mix_sampled_color * factors[i] * ss.cache->paint_brush.flow;
491
492 /* Interpolate with the wet_mix color for wet paint mixing. */
494 paint_color, paint_color, wet_mix_color, ss.cache->paint_brush.wet_mix);
495 blend_color_mix_float(color_buffer[i], color_buffer[i], paint_color);
496
497 /* Final mix over the original color using brush alpha. We apply auto-making again
498 * at this point to avoid washing out non-binary masking modes like cavity masking. */
499 float automasking = auto_mask.is_empty() ? 1.0f : auto_mask[i];
500 const float4 buffer_color = float4(color_buffer[i]) * alpha * automasking;
501
502 IMB_blend_color_float(new_colors[i], orig_colors[i], buffer_color, IMB_BlendMode(brush.blend));
503 new_colors[i] = math::clamp(new_colors[i], 0.0f, 1.0f);
504 }
505
506 scatter_data_mesh(color_buffer.as_span(), verts, mix_colors);
507
508 for (const int i : verts.index_range()) {
509 color_vert_set(faces,
510 corner_verts,
511 vert_to_face_map,
512 color_attribute.domain,
513 verts[i],
514 new_colors[i],
515 color_attribute.span);
516 }
517}
518
523
524static void do_sample_wet_paint_task(const Depsgraph &depsgraph,
525 const Object &object,
526 const Span<float3> vert_positions,
527 const Span<float3> vert_normals,
528 const OffsetIndices<int> faces,
529 const Span<int> corner_verts,
530 const GroupedSpan<int> vert_to_face_map,
531 const MeshAttributeData &attribute_data,
532 const GSpan color_attribute,
533 const bke::AttrDomain color_domain,
534 const Brush &brush,
537 SampleWetPaintData &swptd)
538{
540 const SculptSession &ss = *object.runtime->sculpt_session;
541 const StrokeCache &cache = *ss.cache;
542 const float radius = cache.radius * brush.wet_paint_radius_factor;
543
544 const Span<int> verts = node.verts();
545
546 tls.factors.resize(verts.size());
547 const MutableSpan<float> factors = tls.factors;
548 fill_factor_from_hide_and_mask(attribute_data.hide_vert, attribute_data.mask, verts, factors);
549 if (brush.flag & BRUSH_FRONTFACE) {
550 calc_front_face(cache.view_normal_symm, vert_normals, verts, factors);
551 }
552 auto_mask::calc_vert_factors(depsgraph, object, cache.automasking.get(), node, verts, factors);
553
554 tls.distances.resize(verts.size());
555 const MutableSpan<float> distances = tls.distances;
557 ss, vert_positions, verts, eBrushFalloffShape(brush.falloff_shape), distances);
558 filter_distances_with_radius(radius, distances, factors);
559
560 for (const int i : verts.index_range()) {
561 if (factors[i] > 0.0f) {
562 swptd.color +=
564 faces, corner_verts, vert_to_face_map, color_attribute, color_domain, verts[i]) *
565 factors[i];
566 swptd.tot_samples++;
567 }
568 }
569}
570
571void do_paint_brush(const Depsgraph &depsgraph,
572 const Sculpt &sd,
573 Object &ob,
574 const IndexMask &node_mask)
575{
577
582
583 if (!ss.cache->paint_brush.density_seed) {
584 ss.cache->paint_brush.density_seed = BLI_hash_int_01(ss.cache->location_symm[0] * 1000);
585 }
586
587 float4x4 mat;
588
589 /* If the brush is round the tip does not need to be aligned to the surface, so this saves a
590 * whole iteration over the affected nodes. */
592 mat = cube_tip_init(ob, brush);
593
594 if (math::is_zero(mat)) {
595 return;
596 }
597 }
598
599 Mesh &mesh = *id_cast<Mesh *>(ob.data);
600 const Span<float3> vert_positions = bke::pbvh::vert_positions_eval(depsgraph, ob);
601 const Span<float3> vert_normals = bke::pbvh::vert_normals_eval(depsgraph, ob);
602 const OffsetIndices<int> faces = mesh.faces();
603 const Span<int> corner_verts = mesh.corner_verts();
604 const GroupedSpan<int> vert_to_face_map = mesh.vert_to_face_map();
605 const MeshAttributeData attribute_data(mesh);
607 if (!color_attribute) {
608 return;
609 }
610
611 /* Regular Paint mode. */
612
613 /* Wet paint color sampling. */
614 float4 wet_color(0);
615 if (ss.cache->paint_brush.wet_mix > 0.0f) {
618 node_mask.index_range(),
619 1,
621 [&](const IndexRange range, SampleWetPaintData swptd) {
622 ColorPaintLocalData &tls = all_tls.local();
623 node_mask.slice(range).foreach_index([&](const int i) {
624 do_sample_wet_paint_task(depsgraph,
625 ob,
626 vert_positions,
627 vert_normals,
628 faces,
629 corner_verts,
630 vert_to_face_map,
631 attribute_data,
632 color_attribute.span,
633 color_attribute.domain,
634 brush,
635 nodes[i],
636 tls,
637 swptd);
638 });
639 return swptd;
640 },
641 [](const SampleWetPaintData &a, const SampleWetPaintData &b) {
642 SampleWetPaintData joined{};
643 joined.color = a.color + b.color;
644 joined.tot_samples = a.tot_samples + b.tot_samples;
645 return joined;
646 });
647
648 if (swptd.tot_samples > 0 && is_finite_v4(swptd.color)) {
649 wet_color = math::clamp(swptd.color / float(swptd.tot_samples), 0.0f, 1.0f);
650
651 if (ss.cache->first_time) {
652 ss.cache->paint_brush.wet_mix_prev_color = wet_color;
653 }
655 wet_color,
656 ss.cache->paint_brush.wet_mix_prev_color,
657 ss.cache->paint_brush.wet_persistence);
658 ss.cache->paint_brush.wet_mix_prev_color = math::clamp(wet_color, 0.0f, 1.0f);
659 }
660 }
661
662 if (ss.cache->paint_brush.mix_colors.is_empty()) {
663 ss.cache->paint_brush.mix_colors = Array<float4>(mesh.verts_num, float4(0));
664 }
665
667 node_mask.foreach_index(
668 [&](const int i) {
669 ColorPaintLocalData &tls = all_tls.local();
670 do_paint_brush_task(depsgraph,
671 ob,
672 vert_positions,
673 vert_normals,
674 faces,
675 corner_verts,
676 vert_to_face_map,
677 attribute_data,
678 sd.paint,
679 brush,
680 mat,
681 wet_color,
682 nodes[i],
683 tls,
684 ss.cache->paint_brush.mix_colors,
685 color_attribute);
686 },
688 pbvh.tag_attribute_changed(node_mask, mesh.active_color_attribute);
689 color_attribute.finish();
690}
691
692static void do_smear_brush_task(const Depsgraph &depsgraph,
693 Object &object,
694 const Span<float3> vert_positions,
695 const Span<float3> vert_normals,
696 const OffsetIndices<int> faces,
697 const Span<int> corner_verts,
698 const GroupedSpan<int> vert_to_face_map,
699 const MeshAttributeData &attribute_data,
700 const Brush &brush,
703 bke::GSpanAttributeWriter &color_attribute)
704{
705 const SculptSession &ss = *object.runtime->sculpt_session;
706 const StrokeCache &cache = *ss.cache;
707 const float strength = ss.cache->bstrength;
708
709 const Span<int> verts = node.verts();
710
711 tls.factors.resize(verts.size());
712 const MutableSpan<float> factors = tls.factors;
713 fill_factor_from_hide_and_mask(attribute_data.hide_vert, attribute_data.mask, verts, factors);
714 filter_region_clip_factors(ss, vert_positions, verts, factors);
715 if (brush.flag & BRUSH_FRONTFACE) {
716 calc_front_face(cache.view_normal_symm, vert_normals, verts, factors);
717 }
718
719 tls.distances.resize(verts.size());
720 const MutableSpan<float> distances = tls.distances;
722 ss, vert_positions, verts, eBrushFalloffShape(brush.falloff_shape), distances);
723 filter_distances_with_radius(cache.radius, distances, factors);
724 apply_hardness_to_distances(cache, distances);
725 calc_brush_strength_factors(cache, brush, distances, factors);
726
727 auto_mask::calc_vert_factors(depsgraph, object, cache.automasking.get(), node, verts, factors);
728
729 calc_brush_texture_factors(PaintMode::Sculpt, ss, brush, vert_positions, verts, factors);
730 scale_factors(factors, strength);
731
732 float3 brush_delta;
733 if (brush.stroke_method == BRUSH_STROKE_ANCHORED) {
734 brush_delta = ss.cache->grab_delta_symm;
735 }
736 else {
737 brush_delta = ss.cache->location_symm - ss.cache->last_location_symm;
738 }
739
740 Vector<int> neighbors;
741 Vector<int> neighbor_neighbors;
742
743 for (const int i : verts.index_range()) {
744 if (factors[i] == 0.0f) {
745 continue;
746 }
747 const int vert = verts[i];
748 const float3 &no = vert_normals[vert];
749
750 float3 current_disp;
751 switch (brush.smear_deform_type) {
753 current_disp = brush_delta;
754 break;
756 current_disp = ss.cache->location_symm - vert_positions[vert];
757 break;
759 current_disp = vert_positions[vert] - ss.cache->location_symm;
760 break;
761 }
762
763 /* Project into vertex plane. */
764 current_disp += no * -math::dot(current_disp, no);
765
766 const float3 current_disp_norm = math::normalize(current_disp);
767
768 current_disp = current_disp_norm * strength;
769
770 float4 accum(0);
771 float totw = 0.0f;
772
773 /*
774 * NOTE: we have to do a nested iteration here to avoid
775 * blocky artifacts on quad topologies. The runtime cost
776 * is not as bad as it seems due to neighbor iteration
777 * in the sculpt code being cache bound; once the data is in
778 * the cache iterating over it a few more times is not terribly
779 * costly.
780 */
781
782 for (const int neighbor : vert_neighbors_get_mesh(
783 faces, corner_verts, vert_to_face_map, attribute_data.hide_poly, vert, neighbors))
784 {
785 const float3 &nco = vert_positions[neighbor];
786 for (const int neighbor_neighbor : vert_neighbors_get_mesh(faces,
787 corner_verts,
788 vert_to_face_map,
789 attribute_data.hide_poly,
790 neighbor,
791 neighbor_neighbors))
792 {
793 if (neighbor_neighbor == vert) {
794 continue;
795 }
796
797 float3 vert_disp = vert_positions[neighbor_neighbor] - vert_positions[vert];
798
799 /* Weight by how close we are to our target distance from vd.co. */
800 float w = (1.0f + fabsf(math::length(vert_disp) / strength - 1.0f));
801
802 /* TODO: use cotangents (or at least face areas) here. */
803 float len = math::distance(vert_positions[neighbor_neighbor], nco);
804 if (len > 0.0f) {
805 len = strength / len;
806 }
807 else { /* Coincident point. */
808 len = 1.0f;
809 }
810
811 /* Multiply weight with edge lengths (in the future this will be
812 * cotangent weights or face areas). */
813 w *= len;
814
815 /* Build directional weight. */
816
817 /* Project into vertex plane. */
818 vert_disp += no * -math::dot(no, vert_disp);
819 const float3 vert_disp_norm = math::normalize(vert_disp);
820
821 if (math::dot(current_disp_norm, vert_disp_norm) >= 0.0f) {
822 continue;
823 }
824
825 const float4 &neighbor_color = ss.cache->paint_brush.prev_colors[neighbor_neighbor];
826 float color_interp = -math::dot(current_disp_norm, vert_disp_norm);
827
828 /* Square directional weight to get a somewhat sharper result. */
829 w *= color_interp * color_interp;
830
831 accum += neighbor_color * w;
832 totw += w;
833 }
834 }
835
836 if (totw != 0.0f) {
837 accum /= totw;
838 }
839
841 faces, corner_verts, vert_to_face_map, color_attribute.span, color_attribute.domain, vert);
842 blend_color_interpolate_float(col, ss.cache->paint_brush.prev_colors[vert], accum, factors[i]);
843 color_vert_set(faces,
844 corner_verts,
845 vert_to_face_map,
846 color_attribute.domain,
847 vert,
848 col,
849 color_attribute.span);
850 }
851}
852
853void do_smear_brush(const Depsgraph &depsgraph,
854 const Sculpt &sd,
855 Object &ob,
856 const IndexMask &node_mask)
857{
863
864 Mesh &mesh = *id_cast<Mesh *>(ob.data);
865 if (ss.cache->bstrength == 0.0f) {
866 return;
867 }
868
869 const OffsetIndices<int> faces = mesh.faces();
870 const Span<int> corner_verts = mesh.corner_verts();
871 const GroupedSpan<int> vert_to_face_map = mesh.vert_to_face_map();
872 const Span<float3> vert_positions = bke::pbvh::vert_positions_eval(depsgraph, ob);
873 const Span<float3> vert_normals = bke::pbvh::vert_normals_eval(depsgraph, ob);
874 const MeshAttributeData attribute_data(mesh);
875
877 if (!color_attribute) {
878 return;
879 }
880
881 if (ss.cache->paint_brush.prev_colors.is_empty()) {
882 ss.cache->paint_brush.prev_colors = Array<float4>(mesh.verts_num);
883 threading::parallel_for(IndexRange(mesh.verts_num), 1024, [&](const IndexRange range) {
884 for (const int vert : range) {
885 ss.cache->paint_brush.prev_colors[vert] = color_vert_get(faces,
886 corner_verts,
887 vert_to_face_map,
888 color_attribute.span,
889 color_attribute.domain,
890 vert);
891 }
892 });
893 }
894
895 /* Smear mode. */
896 node_mask.foreach_index(
897 [&](const int i) {
898 for (const int vert : nodes[i].verts()) {
899 ss.cache->paint_brush.prev_colors[vert] = color_vert_get(faces,
900 corner_verts,
901 vert_to_face_map,
902 color_attribute.span,
903 color_attribute.domain,
904 vert);
905 }
906 },
909 node_mask.foreach_index(
910 [&](const int i) {
911 ColorPaintLocalData &tls = all_tls.local();
912 do_smear_brush_task(depsgraph,
913 ob,
914 vert_positions,
915 vert_normals,
916 faces,
917 corner_verts,
918 vert_to_face_map,
919 attribute_data,
920 brush,
921 nodes[i],
922 tls,
923 color_attribute);
924 },
925 exec_mode::grain_size(1));
926
927 pbvh.tag_attribute_changed(node_mask, mesh.active_color_attribute);
928 color_attribute.finish();
929}
930
931void do_blur_brush(const Depsgraph &depsgraph,
932 const Sculpt &sd,
933 Object &ob,
934 const IndexMask &node_mask)
935{
941
942 if (!ss.cache->paint_brush.density_seed) {
943 ss.cache->paint_brush.density_seed = BLI_hash_int_01(ss.cache->location_symm[0] * 1000);
944 }
945
946 Mesh &mesh = *id_cast<Mesh *>(ob.data);
947 const Span<float3> vert_positions = bke::pbvh::vert_positions_eval(depsgraph, ob);
948 const Span<float3> vert_normals = bke::pbvh::vert_normals_eval(depsgraph, ob);
949 const OffsetIndices<int> faces = mesh.faces();
950 const Span<int> corner_verts = mesh.corner_verts();
951 const GroupedSpan<int> vert_to_face_map = mesh.vert_to_face_map();
952 const MeshAttributeData attribute_data(mesh);
954 if (!color_attribute) {
955 return;
956 }
957
959 node_mask.foreach_index(
960 [&](const int i) {
961 ColorPaintLocalData &tls = all_tls.local();
962 do_color_smooth_task(depsgraph,
963 ob,
964 vert_positions,
965 vert_normals,
966 faces,
967 corner_verts,
968 vert_to_face_map,
969 attribute_data,
970 brush,
971 nodes[i],
972 tls,
973 color_attribute);
974 },
976 pbvh.tag_attribute_changed(node_mask, mesh.active_color_attribute);
977 color_attribute.finish();
978}
979
980} // namespace blender::ed::sculpt_paint::color
CustomData interface, see also DNA_customdata_types.h.
A BVH for high poly meshes.
#define PRF_scope(category)
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
Span< T > as_span() const
Definition BLI_array.hh:254
MutableSpan< T > as_mutable_span()
Definition BLI_array.hh:259
bool is() const
const CPPType & type() const
const CPPType & type() const
const void * data() const
constexpr int64_t size() const
constexpr IndexRange()=default
constexpr int64_t start() const
constexpr Span< T > as_span() const
Definition BLI_span.hh:667
constexpr IndexRange index_range() const
Definition BLI_span.hh:676
constexpr Span slice(int64_t start, int64_t size) const
Definition BLI_span.hh:141
constexpr int64_t size() const
Definition BLI_span.hh:256
constexpr IndexRange index_range() const
Definition BLI_span.hh:405
GAttributeReader lookup(const StringRef name) const
GSpanAttributeWriter lookup_for_write_span(StringRef name)
void foreach_index(Fn &&fn, Mode mode=exec_mode::serial) const
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
ccl_device_inline float2 fmod(const float2 a, const float b)
#define T
int face_find_corner_from_vert(const IndexRange face, const Span< int > corner_verts, const int vert)
Definition BKE_mesh.hh:327
bool is_color_attribute(const AttributeMetaData &meta_data)
pbvh::Tree * pbvh_get(Object &object)
Span< float3 > vert_normals_eval(const Depsgraph &depsgraph, const Object &object_orig)
Definition pbvh.cc:952
Span< float3 > vert_positions_eval(const Depsgraph &depsgraph, const Object &object_orig)
Definition pbvh.cc:933
AttrType cpp_type_to_attribute_type(const CPPType &type)
void calc_vert_factors(const Depsgraph &depsgraph, const Object &object, const Cache &automasking, const bke::pbvh::MeshNode &node, Span< int > verts, MutableSpan< float > factors)
static void do_paint_brush_task(const Depsgraph &depsgraph, Object &object, const Span< float3 > vert_positions, const Span< float3 > vert_normals, const OffsetIndices< int > faces, const Span< int > corner_verts, const GroupedSpan< int > vert_to_face_map, const MeshAttributeData &attribute_data, const Paint &paint, const Brush &brush, const float4x4 &mat, const float4 wet_mix_sampled_color, bke::pbvh::MeshNode &node, ColorPaintLocalData &tls, const MutableSpan< float4 > mix_colors, bke::GSpanAttributeWriter &color_attribute)
bke::GSpanAttributeWriter active_color_attribute_for_write(Mesh &mesh)
static void do_sample_wet_paint_task(const Depsgraph &depsgraph, const Object &object, const Span< float3 > vert_positions, const Span< float3 > vert_normals, const OffsetIndices< int > faces, const Span< int > corner_verts, const GroupedSpan< int > vert_to_face_map, const MeshAttributeData &attribute_data, const GSpan color_attribute, const bke::AttrDomain color_domain, const Brush &brush, const bke::pbvh::MeshNode &node, ColorPaintLocalData &tls, SampleWetPaintData &swptd)
void gather_colors_vert(OffsetIndices< int > faces, Span< int > corner_verts, GroupedSpan< int > vert_to_face_map, GSpan color_attribute, bke::AttrDomain color_domain, Span< int > verts, MutableSpan< float4 > r_colors)
void do_smear_brush(const Depsgraph &depsgraph, const Sculpt &sd, Object &ob, const IndexMask &node_mask)
float4 color_vert_get(OffsetIndices< int > faces, Span< int > corner_verts, GroupedSpan< int > vert_to_face_map, GSpan color_attribute, bke::AttrDomain color_domain, int vert)
void gather_colors(GSpan color_attribute, Span< int > indices, MutableSpan< float4 > r_colors)
void color_vert_set(OffsetIndices< int > faces, Span< int > corner_verts, GroupedSpan< int > vert_to_face_map, bke::AttrDomain color_domain, int vert, const float4 &color, GMutableSpan color_attribute)
void swap_gathered_colors(Span< int > indices, GMutableSpan color_attribute, MutableSpan< float4 > r_colors)
void from_float(const float4 &src, T &dst)
static void do_smear_brush_task(const Depsgraph &depsgraph, Object &object, const Span< float3 > vert_positions, const Span< float3 > vert_normals, const OffsetIndices< int > faces, const Span< int > corner_verts, const GroupedSpan< int > vert_to_face_map, const MeshAttributeData &attribute_data, const Brush &brush, bke::pbvh::MeshNode &node, ColorPaintLocalData &tls, bke::GSpanAttributeWriter &color_attribute)
void do_paint_brush(const Depsgraph &depsgraph, const Sculpt &sd, Object &ob, const IndexMask &node_mask)
bke::GAttributeReader active_color_attribute(const Mesh &mesh)
void to_static_color_type(const CPPType &type, const Func &func)
static void do_color_smooth_task(const Depsgraph &depsgraph, const Object &object, const Span< float3 > vert_positions, const Span< float3 > vert_normals, const OffsetIndices< int > faces, const Span< int > corner_verts, const GroupedSpan< int > vert_to_face_map, const MeshAttributeData &attribute_data, const Brush &brush, const bke::pbvh::MeshNode &node, ColorPaintLocalData &tls, bke::GSpanAttributeWriter &color_attribute)
float4 color_corner_get(const GSpan color_attribute, int corner_index)
void do_blur_brush(const Depsgraph &depsgraph, const Sculpt &sd, Object &ob, const IndexMask &node_mask)
float4 to_float(const T &src)
void neighbor_color_average(const OffsetIndices< int > faces, const Span< int > corner_verts, const GroupedSpan< int > vert_to_face_map, const GSpan color_attribute, const bke::AttrDomain color_domain, const GroupedSpan< int > vert_neighbors, const MutableSpan< float4 > smooth_colors)
void fill_factor_from_hide_and_mask(Span< bool > hide_vert, Span< float > mask, Span< int > verts, MutableSpan< float > r_factors)
Definition sculpt.cc:7128
float4x4 cube_tip_init(const Object &ob, const Brush &brush)
Definition sculpt.cc:6459
void calc_brush_strength_factors(const StrokeCache &cache, const Brush &brush, Span< float > distances, MutableSpan< float > factors)
Definition sculpt.cc:7501
void apply_hardness_to_distances(float radius, float hardness, MutableSpan< float > distances)
Definition sculpt.cc:7473
GroupedSpan< int > calc_vert_neighbors(OffsetIndices< int > faces, Span< int > corner_verts, GroupedSpan< int > vert_to_face, Span< bool > hide_poly, Span< int > verts, Vector< int > &r_offset_data, Vector< int > &r_data)
Definition sculpt.cc:7949
void filter_distances_with_radius(float radius, Span< float > distances, MutableSpan< float > factors)
Definition sculpt.cc:7422
void filter_region_clip_factors(const SculptSession &ss, Span< float3 > vert_positions, Span< int > verts, MutableSpan< float > factors)
Definition sculpt.cc:7285
void scale_factors(MutableSpan< float > factors, float strength)
Definition sculpt.cc:7856
void scatter_data_mesh(Span< T > src, Span< int > indices, MutableSpan< T > dst)
Span< float4 > orig_color_data_get_mesh(const Object &object, const bke::pbvh::MeshNode &node)
void calc_brush_distances(const SculptSession &ss, Span< float3 > vert_positions, Span< int > vert, eBrushFalloffShape falloff_shape, MutableSpan< float > r_distances)
Definition sculpt.cc:7370
void gather_data_mesh(Span< T > src, Span< int > indices, MutableSpan< T > dst)
void calc_front_face(const float3 &view_normal, Span< float3 > normals, MutableSpan< float > factors)
Definition sculpt.cc:7223
void calc_brush_cube_distances(const Brush &brush, const Span< T > positions, const MutableSpan< float > r_distances)
Definition sculpt.cc:7435
void calc_local_positions(Span< float3 > vert_positions, Span< int > verts, const float4x4 &mat, const float3 &plane_center, const float3 &view_normal, eBrushFalloffShape falloff_shape, MutableSpan< float3 > local_positions)
Definition sculpt.cc:6619
void calc_brush_texture_factors(PaintMode paint_mode, const SculptSession &ss, const Brush &brush, Span< float3 > vert_positions, Span< int > vert, MutableSpan< float > factors)
Definition sculpt.cc:7513
Span< int > vert_neighbors_get_mesh(const OffsetIndices< int > faces, const Span< int > corner_verts, const GroupedSpan< int > vert_to_face, const Span< bool > hide_poly, const int vert, Vector< int > &r_neighbors)
Definition sculpt.cc:534
constexpr ParallelGrainSize grain_size(int grain_size)
T clamp(const T &a, const T &min, const T &max)
T distance(const T &a, const T &b)
T length(const VecBase< T, Size > &a)
T dot(const QuaternionBase< T > &a, const QuaternionBase< T > &b)
bool is_zero(const T &a)
MatBase< T, NumCol, NumRow > normalize(const MatBase< T, NumCol, NumRow > &a)
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
Value parallel_reduce(IndexRange range, int64_t grain_size, const Value &identity, const Function &function, const Reduction &reduction)
Definition BLI_task.hh:127
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
MINLINE void rgba_uchar_to_float(float r_col[4], const unsigned char col_ub[4])
@ BRUSH_STROKE_ANCHORED
MINLINE void blend_color_mix_float(float dst[4], const float src1[4], const float src2[4])
@ BRUSH_GRADIENT_SPACING_CLAMP
@ BRUSH_GRADIENT_SPACING_REPEAT
@ BRUSH_GRADIENT_PRESSURE
bool BKE_colorband_evaluate(const ColorBand *coba, float in, float out[4])
Definition colorband.cc:399
void BKE_brush_calc_curve_factors(eBrushCurvePreset preset, const CurveMapping *cumap, Span< float > distances, float brush_radius, MutableSpan< float > factors)
Definition brush.cc:1561
MatBase< float, 4, 4 > float4x4
BLI_INLINE float BLI_hash_int_01(unsigned int k)
Definition BLI_hash_c.hh:94
VecBase< float, 4 > float4
MINLINE void blend_color_interpolate_float(float dst[4], const float src1[4], const float src2[4], float t)
unsigned char uchar
Definition BKE_text.h:58
BLI_INLINE void IMB_colormanagement_srgb_to_scene_linear_v3(float scene_linear[3], const float srgb[3])
void IMB_blend_color_float(float dst[4], const float src1[4], const float src2[4], IMB_BlendMode mode)
Definition rectop.cc:118
const Brush * BKE_paint_brush_for_read(const Paint *paint)
bool BKE_brush_has_cube_tip(const Brush *brush, PaintMode paint_mode)
Definition brush.cc:1816
MINLINE bool is_finite_v4(const float v[4]) ATTR_WARN_UNUSED_RESULT
std::optional< BrushColorJitterSettings > BKE_brush_color_jitter_get_settings(const Paint *paint, const Brush *brush)
Definition brush.cc:1262
float3 BKE_brush_color_get(const Paint *paint, const Brush *brush)
Definition brush.cc:1253
ColorSceneLinear4f< eAlpha::Premultiplied > ColorGeometry4f
MINLINE void copy_v4_v4(float r[4], const float a[4])
@ BRUSH_SMEAR_DEFORM_EXPAND
@ BRUSH_SMEAR_DEFORM_DRAG
@ BRUSH_SMEAR_DEFORM_PINCH
VecBase< float, 3 > float3
BLI_INLINE void IMB_colormanagement_scene_linear_to_srgb_v3(float srgb[3], const float scene_linear[3])
float3 BKE_paint_randomize_color(const BrushColorJitterSettings &color_jitter, const float3 &initial_hsv_jitter, const float distance, const float pressure, const float3 &color)
float BKE_brush_alpha_get(const Paint *paint, const Brush *brush)
Definition brush.cc:1454
ColorSceneLinearByteEncoded4b< eAlpha::Premultiplied > ColorGeometry4b
MINLINE void rgba_float_to_uchar(unsigned char r_col[4], const float col_f[4])
float3 BKE_brush_secondary_color_get(const Paint *paint, const Brush *brush)
Definition brush.cc:1297
#define fabsf
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
bke::ObjectRuntime * runtime
ed::sculpt_paint::StrokeCache * cache
Definition BKE_paint.hh:409
std::unique_ptr< auto_mask::Cache > automasking
struct blender::ed::sculpt_paint::StrokeCache::@346250254335162214324031351310170235363204044376 paint_brush
void resize(int new_size)
Definition vector.hh:94