Blender V5.3
paint_stroke.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#include <algorithm>
10#include <cfloat>
11#include <cmath>
12#include <fmt/format.h>
13
14#include "MEM_guardedalloc.h"
15
16#include "BLI_math_matrix.hh"
17#include "BLI_math_matrix_c.hh"
18#include "BLI_math_vector_c.hh"
19#include "BLI_rand.hh"
20#include "BLI_utildefines.hh"
21
22#include "DNA_brush_types.h"
23#include "DNA_curve_types.h"
24#include "DNA_object_types.h"
25#include "DNA_scene_types.h"
26
27#include "PRF_profile.hh"
28
29#include "RNA_access.hh"
30
31#include "BKE_brush.hh"
32#include "BKE_colortools.hh"
33#include "BKE_context.hh"
34#include "BKE_curve.hh"
35#include "BKE_global.hh"
36#include "BKE_image.hh"
37#include "BKE_paint.hh"
38#include "BKE_paint_types.hh"
39
40#include "WM_api.hh"
41#include "WM_types.hh"
42
43#include "GPU_immediate.hh"
44#include "GPU_state.hh"
45
46#include "ED_screen.hh"
47#include "ED_view3d.hh"
48
49#include "IMB_imbuf_types.hh"
50
51#include "paint_intern.hh"
52
53#include "mesh/sculpt_cloth.hh"
54#include "mesh/sculpt_intern.hh"
55
57
58/*** Cursors ***/
60 const int2 &xy,
61 const float2 & /*tilt*/,
62 void *customdata)
63{
64 PaintStroke *data = static_cast<PaintStroke *>(customdata);
65
69 ARegion *region = CTX_wm_region(C);
70
71 if ((mode == PaintMode::GPencil) && (paint->flags & PAINT_SHOW_BRUSH) == 0) {
72 return;
73 }
74
75 if (data && brush) {
76 GPU_line_smooth(true);
78
79 const uint pos = GPU_vertformat_attr_add(
80 immVertexFormat(), "pos", gpu::VertAttrType::SFLOAT_32_32);
82 const uchar4 color = uchar4(255, 100, 100, 128);
84
86 immVertex2fv(pos, float2(xy));
87 immVertex2f(pos,
88 data->last_mouse_position[0] + region->winrct.xmin,
89 data->last_mouse_position[1] + region->winrct.ymin);
90
91 immEnd();
92
94
96 GPU_line_smooth(false);
97 }
98}
99
101 const int2 &xy,
102 const float2 & /*tilt*/,
103 void *customdata)
104{
105 PaintStroke *stroke = static_cast<PaintStroke *>(customdata);
106
107 GPU_line_smooth(true);
108
109 const uint shdr_pos = GPU_vertformat_attr_add(
110 immVertexFormat(), "pos", gpu::VertAttrType::SFLOAT_32_32);
111
113
114 float4 viewport_size;
115 GPU_viewport_size_get_f(viewport_size);
116 immUniform2f("viewport_size", viewport_size[2], viewport_size[3]);
117
118 immUniform1i("colors_len", 2); /* "advanced" mode */
119 immUniform4f("color", 0.0f, 0.0f, 0.0f, 0.5);
120 immUniform4f("color2", 1.0f, 1.0f, 1.0f, 0.5);
121 immUniform1f("dash_width", 6.0f);
122 immUniform1f("udash_factor", 0.5f);
123
125
126 const ARegion *region = stroke->vc.region;
127
128 if (stroke->constrain_line) {
129 immVertex2f(shdr_pos,
130 stroke->last_mouse_position[0] + region->winrct.xmin,
131 stroke->last_mouse_position[1] + region->winrct.ymin);
132
133 immVertex2f(shdr_pos,
134 stroke->constrained_pos[0] + region->winrct.xmin,
135 stroke->constrained_pos[1] + region->winrct.ymin);
136 }
137 else {
138 immVertex2f(shdr_pos,
139 stroke->last_mouse_position[0] + region->winrct.xmin,
140 stroke->last_mouse_position[1] + region->winrct.ymin);
141
142 immVertex2fv(shdr_pos, float2(xy));
143 }
144
145 immEnd();
146
148
149 GPU_line_smooth(false);
150}
151
153{
154 switch (mode) {
156 if (ELEM(brush.sculpt_brush_type,
164 {
165 return false;
166 }
168 return false;
169 }
170 else {
171 return true;
172 }
173 default:
174 break;
175 }
176
177 return true;
178}
179
181{
182 switch (mode) {
184 if (!USER_EXPERIMENTAL_TEST(&U, use_3d_texture_paint) ||
186 {
187 return false;
188 }
189 return brush.flag & BRUSH_SCENE_SPACING;
191 return brush.flag & BRUSH_SCENE_SPACING;
192 default:
193 break;
194 }
195 return false;
196}
197
199{
201}
202
204 const PaintMode mode)
205{
206 if (brush.stroke_method == BRUSH_STROKE_ANCHORED) {
207 return false;
208 }
209
210 switch (mode) {
212 if (ELEM(brush.sculpt_brush_type,
221 {
222 return false;
223 }
224 else {
225 return true;
226 }
227 default:
228 break;
229 }
230
231 return true;
232}
233
234bool PaintStroke::update(bContext *C,
235 const Brush &brush,
236 const PaintMode mode,
237 const float mouse_init[2],
238 float mouse[2],
239 const float pressure,
240 float r_location[3],
241 bool *r_location_is_set)
242{
246 bke::PaintRuntime &paint_runtime = *paint->runtime;
247 bool location_sampled = false;
248 bool location_success = false;
249 /* Use to perform all operations except applying the stroke,
250 * needed for operations that require cursor motion (rake). */
251 bool is_dry_run = false;
252 bool do_random = false;
253 bool do_random_mask = false;
254 *r_location_is_set = false;
255 /* XXX: Use pressure value from first brush step for brushes which don't
256 * support strokes (grab, thumb). They depends on initial state and
257 * brush coord/pressure/etc.
258 * It's more an events design issue, which doesn't split coordinate/pressure/angle
259 * changing events. We should avoid this after events system re-design */
260 if (!input_init_) {
261 copy_v2_v2(initial_mouse_, mouse);
262 copy_v2_v2(paint_runtime.last_rake, mouse);
263 copy_v2_v2(paint_runtime.tex_mouse, mouse);
264 copy_v2_v2(paint_runtime.mask_tex_mouse, mouse);
265 cached_size_pressure_ = pressure;
266
267 input_init_ = true;
268 }
269
271 copy_v2_v2(paint_runtime.tex_mouse, mouse);
272 copy_v2_v2(paint_runtime.mask_tex_mouse, mouse);
273 }
274
275 /* Truly temporary data that isn't stored in properties */
276
277 paint_runtime.stroke_active = true;
278 const float pressure_to_evaluate = paint_supports_dynamic_size(brush, mode) ?
279 pressure :
280 cached_size_pressure_;
283 brush.curve_size, 0, pressure_to_evaluate) :
284 1.0f;
285
286 paint_runtime.pixel_radius = BKE_brush_radius_get(paint, &brush) *
287 paint_runtime.size_pressure_value;
289
291
296 {
297 do_random = true;
298 }
299
302 }
303 else {
304 copy_v2_v2(paint_runtime.tex_mouse, mouse);
305 }
306
307 /* take care of mask texture, if any */
308 if (brush.mask_mtex.tex) {
309
314 {
315 do_random_mask = true;
316 }
317
320 }
321 else {
322 copy_v2_v2(paint_runtime.mask_tex_mouse, mouse);
323 }
324 }
325 }
326
328 bool hit = false;
329 float2 halfway;
330
331 const float dx = mouse[0] - initial_mouse_[0];
332 const float dy = mouse[1] - initial_mouse_[1];
333
334 paint_runtime.anchored_size = paint_runtime.pixel_radius = sqrtf(dx * dx + dy * dy);
335
336 paint_runtime.brush_rotation = paint_runtime.brush_rotation_sec = atan2f(dy, dx) +
337 float(0.5f * M_PI);
338
340 halfway[0] = dx * 0.5f + initial_mouse_[0];
341 halfway[1] = dy * 0.5f + initial_mouse_[1];
342
343 if (mode != PaintMode::Texture2D) {
344 if (std::optional<float3> location = this->get_location(halfway, original_)) {
345 copy_v3_v3(r_location, *location);
346 hit = true;
347 location_sampled = true;
348 location_success = true;
349 *r_location_is_set = true;
350 }
351 else if (!paint_brush_type_require_location(brush, mode)) {
352 hit = true;
353 }
354 }
355 else {
356 hit = true;
357 }
358 }
359 if (hit) {
360 copy_v2_v2(paint_runtime.anchored_initial_mouse, halfway);
361 copy_v2_v2(paint_runtime.tex_mouse, halfway);
362 copy_v2_v2(paint_runtime.mask_tex_mouse, halfway);
363 copy_v2_v2(mouse, halfway);
364 paint_runtime.anchored_size /= 2.0f;
365 paint_runtime.pixel_radius /= 2.0f;
366 stroke_distance_ = paint_runtime.pixel_radius;
367 }
368 else {
369 copy_v2_v2(paint_runtime.anchored_initial_mouse, initial_mouse_);
370 copy_v2_v2(mouse, initial_mouse_);
371 stroke_distance_ = paint_runtime.pixel_radius;
372 }
373 paint_runtime.pixel_radius /= zoom_2d_;
374 paint_runtime.draw_anchored = true;
375 }
376 else {
377 /* curve strokes do their own rake calculation */
380 *paint, brush, mouse_init, mode, true, !rake_started_))
381 {
382 /* Not enough motion to define an angle. */
383 if (!rake_started_) {
384 is_dry_run = true;
385 }
386 }
387 else {
388 rake_started_ = true;
389 }
390 }
391 }
392
393 if ((do_random || do_random_mask) && !rng_) {
394 /* Lazy initialization. */
396 }
397
398 if (do_random) {
400 paint_runtime.brush_rotation += -brush.mtex.random_angle / 2.0f +
401 brush.mtex.random_angle * rng_->get_float();
402 }
403 }
404
405 if (do_random_mask) {
407 paint_runtime.brush_rotation_sec += -brush.mask_mtex.random_angle / 2.0f +
408 brush.mask_mtex.random_angle * rng_->get_float();
409 }
410 }
411
412 if (!location_sampled) {
413 if (mode != PaintMode::Texture2D) {
414 if (std::optional<float3> location = this->get_location(mouse, original_)) {
415 copy_v3_v3(r_location, *location);
416 location_success = true;
417 *r_location_is_set = true;
418 }
419 else if (!paint_brush_type_require_location(brush, mode)) {
420 location_success = true;
421 }
422 }
423 else {
424 zero_v3(r_location);
425 location_success = true;
426 /* don't set 'r_location_is_set', since we don't want to use the value. */
427 }
428 }
429
430 return location_success && !is_dry_run;
431}
432
434{
435 /* Only these stroke modes support dash lines */
437}
438
439static bool paint_stroke_use_jitter(const PaintMode mode, const Brush &brush, const bool invert)
440{
441 bool use_jitter = brush.flag & BRUSH_ABSOLUTE_JITTER ? brush.jitter_absolute != 0 :
442 brush.jitter != 0;
443
444 /* jitter-ed brush gives weird and unpredictable result for this
445 * kinds of stroke, so manually disable jitter usage (sergey) */
446 use_jitter &= (ELEM(brush.stroke_method, BRUSH_STROKE_DRAG_DOT, BRUSH_STROKE_ANCHORED)) == 0;
447 use_jitter &= !ELEM(mode, PaintMode::Texture2D, PaintMode::Texture3D) ||
448 !(invert && brush.image_brush_type == IMAGE_PAINT_BRUSH_TYPE_CLONE);
449
450 return use_jitter;
451}
452
454 PaintMode mode,
455 const Brush &brush,
456 float pressure,
457 BrushStrokeMode stroke_mode,
458 float zoom_2d,
459 const float2 &mval)
460{
461 if (paint_stroke_use_jitter(mode, brush, stroke_mode == BrushStrokeMode::Invert)) {
462 float factor = zoom_2d;
463
464 if (brush.flag & BRUSH_JITTER_PRESSURE) {
465 factor *= BKE_curvemapping_evaluateF(brush.curve_jitter, 0, pressure);
466 }
467
468 float2 jittered_position = BKE_brush_jitter_pos(*paint, brush, mval);
469
470 /* XXX: meh, this is round about because
471 * BKE_brush_jitter_pos isn't written in the best way to
472 * be reused here */
473 if (factor != 1.0f) {
474 const float2 delta = (jittered_position - mval) * factor;
475 return mval + delta;
476 }
477 return jittered_position;
478 }
479
480 return mval;
481}
482
483/* Put the location of the next stroke dot into the stroke RNA and apply it to the mesh */
484void PaintStroke::add_step(bContext *C, wmOperator *op, const float2 mval, float pressure)
485{
488 const Brush &brush = *BKE_paint_brush_for_read(this->paint);
489 bke::PaintRuntime *paint_runtime = this->paint->runtime;
490
491/* the following code is adapted from texture paint. It may not be needed but leaving here
492 * just in case for reference (code in texpaint removed as part of refactoring).
493 * It's strange that only texpaint had these guards. */
494#if 0
495 /* special exception here for too high pressure values on first touch in
496 * windows for some tablets, then we just skip first touch. */
497 if (tablet && (pressure >= 0.99f) &&
498 ((pop->s.brush.flag & BRUSH_SPACING_PRESSURE) ||
499 BKE_brush_use_alpha_pressure(pop->s.brush) || BKE_brush_use_size_pressure(pop->s.brush)))
500 {
501 return;
502 }
503
504 /* This can be removed once fixed properly in
505 * BKE_brush_painter_paint(
506 * BrushPainter *painter, BrushFunc func,
507 * float *pos, double time, float pressure, void *user);
508 * at zero pressure we should do nothing 1/2^12 is 0.0002
509 * which is the sensitivity of the most sensitive pen tablet available */
510 if (tablet && (pressure < 0.0002f) &&
511 ((pop->s.brush.flag & BRUSH_SPACING_PRESSURE) ||
512 BKE_brush_use_alpha_pressure(pop->s.brush) || BKE_brush_use_size_pressure(pop->s.brush)))
513 {
514 return;
515 }
516#endif
517
518 /* copy last position -before- jittering, or space fill code
519 * will create too many dabs */
520 this->last_mouse_position = mval;
521 last_pressure_ = pressure;
522
525 if (std::optional<float3> location = this->get_location(this->last_mouse_position, original_))
526 {
527 last_world_space_position_ = math::transform_point(this->vc.obact->object_to_world(),
528 *location);
529 }
530 else {
531 last_world_space_position_ += last_scene_spacing_delta_;
532 }
533 }
534
535 /* Get jitter position (same as mval if no jitter is used). */
537 this->paint, mode, brush, pressure, stroke_mode_, zoom_2d_, mval);
538
539 float3 location;
540 bool is_location_is_set;
541 paint_runtime->last_hit = update(
542 C, brush, mode, mval, mouse_out, pressure, location, &is_location_is_set);
543 if (is_location_is_set) {
544 copy_v3_v3(paint_runtime->last_location, location);
545 }
546 if (!paint_runtime->last_hit) {
547 return;
548 }
549
550 /* Dash */
551 bool add_step = true;
553 const int dash_samples = tot_samples_ % brush.dash_samples;
554 const float dash = float(dash_samples) / float(brush.dash_samples);
555 if (dash > brush.dash_ratio) {
556 add_step = false;
557 }
558 }
559
560 /* Add to stroke */
561 if (add_step) {
563 step.size = paint_runtime->pixel_radius;
564 step.location = location;
565 /* Mouse coordinates modified by the stroke type options. */
566 step.mouse = mouse_out;
567 /* Original mouse coordinates. */
568 step.mouse_event = mval;
569 step.pressure = pressure;
570 step.tilt = tilt_;
571
572 this->update_step(op, step);
573 }
574
575 tot_samples_++;
576}
577
578/* Returns zero if no sculpt changes should be made, non-zero otherwise */
579static bool paint_smooth_stroke(const Brush &brush,
580 const PaintSample *sample,
581 const PaintMode mode,
582 const BrushSwitchMode brush_switch_mode,
583 float zoom_2d,
584 float2 last_mouse_position,
585 float last_pressure,
586 float2 &r_mouse,
587 float &r_pressure)
588{
589 if (paint_supports_smooth_stroke(brush, mode, brush_switch_mode)) {
590 const float radius = brush.smooth_stroke_radius * zoom_2d;
591 const float u = brush.smooth_stroke_factor;
592
593 /* If the mouse is moving within the radius of the last move,
594 * don't update the mouse position. This allows sharp turns. */
595 if (math::distance_squared(last_mouse_position, sample->mouse) < square_f(radius)) {
596 return false;
597 }
598
599 r_mouse = math::interpolate(sample->mouse, last_mouse_position, u);
600 r_pressure = math::interpolate(sample->pressure, last_pressure, u);
601 }
602 else {
603 r_mouse = sample->mouse;
604 r_pressure = sample->pressure;
605 }
606
607 return true;
608}
609
611 const Paint *paint,
612 const PaintMode paint_mode,
613 const Brush *brush,
614 float3 last_world_space_position,
615 float zoom_2d,
616 const float size_factor,
617 const float pressure)
618{
619 float size_clamp = 0.0f;
620 if (paint_stroke_use_scene_spacing(*brush, paint_mode)) {
621 const float3 last_object_space_position = math::transform_point(vc.obact->world_to_object(),
622 last_world_space_position);
623 size_clamp = object_space_radius_get(
624 vc, *paint, *brush, last_object_space_position, size_factor);
625 }
626 else {
627 /* brushes can have a minimum size of 1.0 but with pressure it can be smaller than a pixel
628 * causing very high step sizes, hanging blender #32381. */
629 size_clamp = max_ff(1.0f, BKE_brush_radius_get(paint, brush) * size_factor);
630 }
631
632 float spacing = brush->spacing;
633
634 /* apply spacing pressure */
635 if (brush->stroke_method == BRUSH_STROKE_SPACE && brush->flag & BRUSH_SPACING_PRESSURE) {
636 const float spacing_factor = BKE_curvemapping_evaluateF(brush->curve_spacing, 0, pressure);
637 spacing = spacing * (1.5f - spacing_factor);
638 }
639
641 /* The spacing in tools that use the cloth solver should not be affected by the brush radius to
642 * avoid affecting the simulation update rate when changing the radius of the brush.
643 * With a value of 100 and the brush default of 10 for spacing, a simulation step runs every 2
644 * pixels movement of the cursor. */
645 size_clamp = 100.0f;
646 }
647
648 /* stroke system is used for 2d paint too, so we need to account for
649 * the fact that brush can be scaled there. */
650 spacing *= zoom_2d;
651
652 if (paint_stroke_use_scene_spacing(*brush, paint_mode)) {
653 /* Low pressure on size (with tablets) can cause infinite recursion in paint_space_stroke(),
654 * see #129853. */
655
656 /* Calculates the 3D distance that one pixel of movement would cover in object space. Using
657 * FLT_MIN can cause long lag, see #160057. */
658 const float min_spacing = paint_calc_object_space_radius(
659 vc, math::transform_point(vc.obact->world_to_object(), last_world_space_position), 1.0f);
660 return max_ff(min_spacing, size_clamp * spacing / 50.0f);
661 }
662 return max_ff(zoom_2d, size_clamp * spacing / 50.0f);
663}
664
666 const Paint *paint,
667 const PaintMode paint_mode,
668 const Brush *brush,
669 float3 last_world_space_position,
670 float zoom_2d)
671{
672 /* Unlike many paint pressure curves, spacing assumes that a stroke without pressure (e.g. with
673 * the mouse, or with the setting turned off) represents an input of 0.5, not 1.0. */
675 vc, paint, paint_mode, brush, last_world_space_position, zoom_2d, 1.0f, 0.5f);
676}
677
678static float paint_stroke_overlapped_curve(const Brush &br, const float x, const float spacing)
679{
680 /* Avoid division by small numbers, can happen
681 * on some pen setups. See #105341.
682 */
683
684 const float clamped_spacing = max_ff(spacing, 0.1f);
685
686 const int n = 100 / clamped_spacing;
687 const float h = clamped_spacing / 50.0f;
688 const float x0 = x - 1;
689
690 float sum = 0;
691 for (int i = 0; i < n; i++) {
692 float xx = fabsf(x0 + i * h);
693
694 if (xx < 1.0f) {
695 sum += BKE_brush_curve_strength(&br, xx, 1);
696 }
697 }
698
699 return sum;
700}
701
702static float paint_stroke_integrate_overlap(const Brush &br, const float factor)
703{
704 const float spacing = br.spacing * factor;
705
706 if (!(br.flag & BRUSH_SPACE_ATTEN && (br.spacing < 100))) {
707 return 1.0;
708 }
709
710 constexpr int m = 10;
711 float g = 1.0f / m;
712 float max = 0;
713 for (int i = 0; i < m; i++) {
714 const float overlap = fabs(paint_stroke_overlapped_curve(br, i * g, spacing));
715
716 max = std::max(overlap, max);
717 }
718
719 if (max == 0.0f) {
720 return 1.0f;
721 }
722 return 1.0f / max;
723}
724
726 const Paint *paint,
727 const PaintMode paint_mode,
728 const Brush *brush,
729 float3 last_world_space_position,
730 float zoom_2d,
731 const float last_pressure,
732 const float pressure,
733 const float pressure_delta,
734 const float length)
735{
737 const float max_size_factor = BKE_curvemapping_evaluateF(brush->curve_size, 0, 1.0f);
738 /* use pressure to modify size. set spacing so that at 100%, the circles
739 * are aligned nicely with no overlap. for this the spacing needs to be
740 * the average of the previous and next size. */
741 const float s = paint_space_stroke_spacing(vc,
742 paint,
743 paint_mode,
744 brush,
745 last_world_space_position,
746 zoom_2d,
747 max_size_factor,
748 pressure);
749 const float q = s * pressure_delta / (2.0f * length);
750 const float pressure_fac = (1.0f + q) / (1.0f - q);
751
752 const float last_size_factor = BKE_curvemapping_evaluateF(brush->curve_size, 0, last_pressure);
753 const float new_size_factor = BKE_curvemapping_evaluateF(
754 brush->curve_size, 0, last_pressure * pressure_fac);
755
756 /* average spacing */
757 const float last_spacing = paint_space_stroke_spacing(vc,
758 paint,
759 paint_mode,
760 brush,
761 last_world_space_position,
762 zoom_2d,
763 last_size_factor,
764 pressure);
765 const float new_spacing = paint_space_stroke_spacing(vc,
766 paint,
767 paint_mode,
768 brush,
769 last_world_space_position,
770 zoom_2d,
771 new_size_factor,
772 pressure);
773
774 return 0.5f * (last_spacing + new_spacing);
775 }
776
777 /* no size pressure */
779 vc, paint, paint_mode, brush, last_world_space_position, zoom_2d, 1.0f, pressure);
780}
781
782/* For brushes with stroke spacing enabled, moves mouse in steps
783 * towards the final mouse location. */
784int PaintStroke::space_stroke(bContext *C,
785 wmOperator *op,
786 const float2 final_mouse,
787 const float final_pressure)
788{
790 const ARegion *region = CTX_wm_region(C);
791 bke::PaintRuntime *paint_runtime = this->paint->runtime;
795
796 float2 mouse_delta = final_mouse - this->last_mouse_position;
797 float length = normalize_v2(mouse_delta);
798
799 float3 world_space_position_delta;
800 const bool use_scene_spacing = paint_stroke_use_scene_spacing(brush, mode);
801 if (use_scene_spacing) {
803 const std::optional<float3> hit = this->get_location(final_mouse, original_);
804 float3 world_space_position = math::transform_point(this->vc.obact->object_to_world(),
805 hit.value_or(float3(0.0f)));
806 if (hit && stroke_over_mesh_) {
807 world_space_position_delta = world_space_position - last_world_space_position_;
808 length = math::length(world_space_position_delta);
809 stroke_over_mesh_ = true;
810 }
811 else {
812 length = 0.0f;
813 world_space_position_delta = {0.0f, 0.0f, 0.0f};
814 stroke_over_mesh_ = hit.has_value();
815 if (stroke_over_mesh_) {
816 last_world_space_position_ = world_space_position;
817 }
818 }
819 }
820
821 float pressure = last_pressure_;
822 float pressure_delta = final_pressure - last_pressure_;
823 const float no_pressure_spacing = paint_space_stroke_spacing_no_pressure(
824 this->vc, &paint, this->paint_mode, &brush, last_world_space_position_, zoom_2d_);
825 int count = 0;
826 while (length > 0.0f) {
827 const float spacing = paint_space_stroke_spacing_variable(this->vc,
828 &paint,
829 this->paint_mode,
830 &brush,
831 last_world_space_position_,
832 zoom_2d_,
833 last_pressure_,
834 pressure,
835 pressure_delta,
836 length);
837 BLI_assert(spacing >= 0.0f);
838
839 if (length >= spacing) {
840 float2 mouse;
841 if (use_scene_spacing) {
842 float3 final_world_space_position;
843 world_space_position_delta = math::normalize(world_space_position_delta);
844 final_world_space_position = world_space_position_delta * spacing +
845 last_world_space_position_;
846 ED_view3d_project_v2(region, final_world_space_position, mouse);
847
848 last_scene_spacing_delta_ = world_space_position_delta * spacing;
849 }
850 else {
851 mouse = this->last_mouse_position + mouse_delta * spacing;
852 }
853 pressure = last_pressure_ + (spacing / length) * pressure_delta;
854
856 brush, spacing / no_pressure_spacing);
857
858 stroke_distance_ += spacing / zoom_2d_;
859 this->add_step(C, op, mouse, pressure);
860
861 length -= spacing;
862 pressure = last_pressure_;
863 pressure_delta = final_pressure - last_pressure_;
864
865 count++;
866 }
867 else {
868 break;
869 }
870 }
871
872 return count;
873}
874
876{
877 return U.tablet_flag & USER_TABLET_SHOW_DEBUG_VALUES;
878}
879
880/**** Public API ****/
881
883 wmOperator *op,
884 const wmEvent *event,
885 const PaintMode paint_mode)
886{
887 this->paint_mode = paint_mode;
891 bke::PaintRuntime *paint_runtime = this->paint->runtime;
892 this->brush = BKE_paint_brush(this->paint);
894
895 this->evil_C = C;
897 this->object = CTX_data_active_object(C);
898 this->scene = CTX_data_scene(C);
899
900 if (event) {
901 WM_event_tablet_data(event, &pen_flip_, nullptr);
902 RNA_boolean_set(op->ptr, "pen_flip", pen_flip_);
903 event_type_ = event->type;
904 }
905 else {
906 pen_flip_ = RNA_boolean_get(op->ptr, "pen_flip");
907 event_type_ = 0;
908 }
909
910 stroke_mode_ = BrushStrokeMode(RNA_enum_get(op->ptr, "mode"));
911 brush_switch_mode_ = BrushSwitchMode(RNA_enum_get(op->ptr, "brush_toggle"));
912
913 original_ = paint_brush_type_raycast_original(*this->brush,
915
916 float zoomx;
917 float zoomy;
918 get_imapaint_zoom(C, &zoomx, &zoomy);
919 zoom_2d_ = std::max(zoomx, zoomy);
920
921 /* Check here if color sampling the main brush should do color conversion. This is done here
922 * to avoid locking up to get the image buffer during sampling. */
923 paint_runtime->do_linear_conversion = false;
924 paint_runtime->colorspace = nullptr;
925
926 if (this->brush->mtex.tex && this->brush->mtex.tex->type == TEX_IMAGE &&
927 this->brush->mtex.tex->ima)
928 {
929 ImBuf *tex_ibuf = BKE_image_acquire_ibuf(
930 this->brush->mtex.tex->ima, &this->brush->mtex.tex->iuser, nullptr);
931 if (tex_ibuf && tex_ibuf->float_data() == nullptr) {
932 paint_runtime->do_linear_conversion = true;
933 paint_runtime->colorspace = tex_ibuf->byte_buffer.colorspace;
934 }
935 BKE_image_release_ibuf(this->brush->mtex.tex->ima, tex_ibuf, nullptr);
936 }
937
938 if (stroke_mode_ == BrushStrokeMode::Invert) {
939 if (this->brush->stroke_method == BRUSH_STROKE_CURVE) {
940 RNA_enum_set(op->ptr, "mode", int(BrushStrokeMode::Normal));
941 }
942 }
943 /* initialize here */
944 paint_runtime->overlap_factor = 1.0;
945 paint_runtime->stroke_active = true;
946
947 if (rv3d) {
948 rv3d->rflag |= RV3D_PAINTING;
949 }
950
951 /* Preserve location from last stroke while applying and resetting
952 * ups->average_stroke_counter to 1.
953 */
954 if (paint_runtime->average_stroke_counter) {
955 mul_v3_fl(paint_runtime->average_stroke_accum,
956 1.0f / float(paint_runtime->average_stroke_counter));
957 paint_runtime->average_stroke_counter = 1;
958 }
959
960 /* initialize here to avoid initialization conflict with threaded strokes */
962 if (this->paint->flags & PAINT_USE_CAVITY_MASK) {
964 }
965
967
968 paint_runtime->start_pixel_radius = BKE_brush_radius_get(this->paint, this->brush);
969}
970
971void PaintStroke::done(bContext *C, const bool is_cancel)
972{
975 ED_workspace_status_text(C, nullptr);
976 }
977 bke::PaintRuntime *paint_runtime = this->paint->runtime;
978
979 /* reset rotation here to avoid doing so in cursor display */
980 if (this->brush) {
981 if (!(this->brush->mtex.brush_angle_mode & MTEX_ANGLE_RAKE)) {
982 paint_runtime->brush_rotation = 0.0f;
983 }
984
985 if (!(this->brush->mask_mtex.brush_angle_mode & MTEX_ANGLE_RAKE)) {
986 paint_runtime->brush_rotation_sec = 0.0f;
987 }
988 }
989
990 /* TODO: Is this stroke_started_ guard necessary? */
991 if (stroke_started_) {
992 this->redraw(true);
993 }
994
995 this->done(is_cancel, stroke_started_);
996
997 if (RegionView3D *rv3d = CTX_wm_region_view3d(C)) {
998 rv3d->rflag &= ~RV3D_PAINTING;
999 }
1000
1002
1003 paint_runtime->draw_anchored = false;
1004 paint_runtime->stroke_active = false;
1005
1006 if (timer_) {
1008 }
1009
1010 if (stroke_cursor_) {
1011 WM_paint_cursor_end(static_cast<wmPaintCursor *>(stroke_cursor_));
1012 }
1013}
1014
1019
1020bool paint_space_stroke_enabled(const Brush &br, const PaintMode mode)
1021{
1023 return false;
1024 }
1025
1027 /* The Cloth Brush is a special case for stroke spacing. Even if it has grab modes which do
1028 * not support dynamic size, stroke spacing needs to be enabled so it is possible to control
1029 * whether the simulation runs constantly or only when the brush moves when using the cloth
1030 * grab brushes. */
1031 return true;
1032 }
1033
1034 if (mode == PaintMode::SculptCurves &&
1036 {
1037 return false;
1038 }
1039
1041 /* No spacing needed for now. */
1042 return false;
1043 }
1044
1045 return paint_supports_dynamic_size(br, mode);
1046}
1047
1064
1066{
1068 return false;
1069 }
1070
1071 switch (mode) {
1072 case PaintMode::Sculpt:
1074 break;
1075
1076 case PaintMode::Texture2D: /* fall through */
1079 return false;
1080 }
1081 break;
1082
1083 default:
1084 break;
1085 }
1086 return true;
1087}
1088
1090 const PaintMode mode,
1091 const BrushSwitchMode brush_switch_mode)
1092{
1093 /* The grease pencil draw tool needs to enable this when the `stroke_mode` is set to
1094 * `BrushSwitchMode::Smooth`. */
1095 if (mode == PaintMode::GPencil &&
1096 eBrushGPaintType(brush.gpencil_brush_type) == GPAINT_BRUSH_TYPE_DRAW &&
1097 brush_switch_mode == BrushSwitchMode::Smooth)
1098 {
1099 return true;
1100 }
1101 if (!(brush.flag & BRUSH_SMOOTH_STROKE) ||
1103 {
1104 return false;
1105 }
1106
1107 switch (mode) {
1108 case PaintMode::Sculpt:
1110 return false;
1111 }
1112 break;
1113 default:
1114 break;
1115 }
1116 return true;
1117}
1118
1120{
1121 /* omit: PAINT_WEIGHT, PAINT_SCULPT_UV, PAINT_INVALID */
1122 return ELEM(
1124}
1125
1127{
1129 return false;
1130 }
1131
1132 switch (mode) {
1133 case PaintMode::Sculpt:
1134 if (sculpt_is_grab_tool(br)) {
1135 return false;
1136 }
1137 break;
1138 default:
1139 break;
1140 }
1141 return true;
1142}
1143
1144void PaintStroke::add_sample(const int input_samples,
1145 const float x,
1146 const float y,
1147 const float pressure)
1148{
1150 PaintSample *sample = &samples_[cur_sample_];
1151 const int max_samples = std::clamp(input_samples, 1, PAINT_MAX_INPUT_SAMPLES);
1152
1153 sample->mouse[0] = x;
1154 sample->mouse[1] = y;
1155 sample->pressure = pressure;
1156
1157 cur_sample_++;
1158 if (cur_sample_ >= max_samples) {
1159 cur_sample_ = 0;
1160 }
1161 if (num_samples_ < max_samples) {
1162 num_samples_++;
1163 }
1164}
1165
1166void PaintStroke::calc_average_sample(PaintSample *average)
1167{
1168 BLI_assert(num_samples_ > 0);
1169
1170 for (int i = 0; i < num_samples_; i++) {
1171 average->mouse += samples_[i].mouse;
1172 average->pressure += samples_[i].pressure;
1173 }
1174
1175 average->mouse /= num_samples_;
1176 average->pressure /= num_samples_;
1177
1178 // printf("avg=(%f, %f), num=%d\n", average->mouse[0], average->mouse[1], stroke->num_samples);
1179}
1180
1185void PaintStroke::lines_spacing(bContext *C,
1186 wmOperator *op,
1187 const float spacing,
1188 float *length_residue,
1189 const float2 old_pos,
1190 const float2 new_pos)
1191{
1194 bke::PaintRuntime *paint_runtime = paint->runtime;
1195 const Brush &brush = *BKE_paint_brush(paint);
1197 const ARegion *region = CTX_wm_region(C);
1198
1199 const bool use_scene_spacing = paint_stroke_use_scene_spacing(brush, mode);
1200
1201 float2 mouse_delta;
1202 float length;
1203 float3 world_space_position_delta;
1204 float3 world_space_position_old;
1205
1206 this->last_mouse_position = old_pos;
1207
1208 if (use_scene_spacing) {
1210 const std::optional<float3> hit_old = this->get_location(old_pos, original_);
1211
1212 const std::optional<float3> hit_new = this->get_location(new_pos, original_);
1213
1214 world_space_position_old = math::transform_point(this->vc.obact->object_to_world(),
1215 hit_old.value_or(float3(0.0f)));
1216 float3 world_space_position_new = math::transform_point(this->vc.obact->object_to_world(),
1217 hit_new.value_or(float3(0.0f)));
1218 if (hit_old && hit_new && stroke_over_mesh_) {
1219 world_space_position_delta = world_space_position_new - world_space_position_old;
1220 length = math::length(world_space_position_delta);
1221 stroke_over_mesh_ = true;
1222 }
1223 else {
1224 length = 0.0f;
1225 world_space_position_delta = {0.0f, 0.0f, 0.0f};
1226 stroke_over_mesh_ = hit_new.has_value();
1227 if (stroke_over_mesh_) {
1228 last_world_space_position_ = world_space_position_old;
1229 }
1230 }
1231 }
1232 else {
1233 mouse_delta = new_pos - old_pos;
1234 mouse_delta = math::normalize_and_get_length(mouse_delta, length);
1235 }
1236
1237 BLI_assert(length >= 0.0f);
1238
1239 if (length == 0.0f) {
1240 return;
1241 }
1242
1243 float2 mouse;
1244 while (length > 0.0f) {
1245 float spacing_final = spacing - *length_residue;
1246 length += *length_residue;
1247 *length_residue = 0.0;
1248
1249 if (length >= spacing) {
1250 if (use_scene_spacing) {
1251 world_space_position_delta = math::normalize(world_space_position_delta);
1252 const float3 final_world_space_position = world_space_position_delta * spacing_final +
1253 world_space_position_old;
1254 ED_view3d_project_v2(region, final_world_space_position, mouse);
1255 }
1256 else {
1257 mouse = this->last_mouse_position + mouse_delta * spacing_final;
1258 }
1259
1260 paint_runtime->overlap_factor = paint_stroke_integrate_overlap(brush, 1.0);
1261
1262 stroke_distance_ += spacing / zoom_2d_;
1263 this->add_step(C, op, mouse, 1.0);
1264
1265 length -= spacing;
1266 spacing_final = spacing;
1267 }
1268 else {
1269 break;
1270 }
1271 }
1272
1273 *length_residue = length;
1274}
1275
1276void PaintStroke::line_end(bContext *C, wmOperator *op, const float2 mouse)
1277{
1279 Brush *br = this->brush;
1280 bke::PaintRuntime *paint_runtime = this->paint->runtime;
1281 if (stroke_started_ && br->stroke_method == BRUSH_STROKE_LINE) {
1282 paint_runtime->overlap_factor = paint_stroke_integrate_overlap(*br, 1.0);
1283
1284 this->add_step(C, op, this->last_mouse_position, 1.0);
1285 this->space_stroke(C, op, mouse, 1.0);
1286 }
1287}
1288
1289bool PaintStroke::curve_end(bContext *C, wmOperator *op)
1290{
1292 const Brush &br = *this->brush;
1293 if (br.stroke_method != BRUSH_STROKE_CURVE) {
1294 return false;
1295 }
1296
1298 bke::PaintRuntime *paint_runtime = paint->runtime;
1299 const float no_pressure_spacing = paint_space_stroke_spacing_no_pressure(
1300 this->vc, paint, this->paint_mode, this->brush, last_world_space_position_, zoom_2d_);
1301 const PaintCurve *pc = br.paint_curve;
1302
1303 if (!pc) {
1304 return true;
1305 }
1306
1307 const PaintCurvePoint *pcp = pc->points;
1308 paint_runtime->overlap_factor = paint_stroke_integrate_overlap(br, 1.0);
1309
1310 float length_residue = 0.0f;
1311 for (int i = 0; i < pc->tot_points - 1; i++, pcp++) {
1312 float data[(PAINT_CURVE_NUM_SEGMENTS + 1) * 2];
1313 float tangents[(PAINT_CURVE_NUM_SEGMENTS + 1) * 2];
1314 const PaintCurvePoint *pcp_next = pcp + 1;
1315 bool do_rake = false;
1316
1317 for (int j = 0; j < 2; j++) {
1318 BKE_curve_forward_diff_bezier(pcp->bez.vec[1][j],
1319 pcp->bez.vec[2][j],
1320 pcp_next->bez.vec[0][j],
1321 pcp_next->bez.vec[1][j],
1322 data + j,
1324 sizeof(float[2]));
1325 }
1326
1327 if ((br.mtex.brush_angle_mode & MTEX_ANGLE_RAKE) ||
1328 (br.mask_mtex.brush_angle_mode & MTEX_ANGLE_RAKE))
1329 {
1330 do_rake = true;
1331 for (int j = 0; j < 2; j++) {
1332 BKE_curve_forward_diff_tangent_bezier(pcp->bez.vec[1][j],
1333 pcp->bez.vec[2][j],
1334 pcp_next->bez.vec[0][j],
1335 pcp_next->bez.vec[1][j],
1336 tangents + j,
1338 sizeof(float[2]));
1339 }
1340 }
1341
1342 for (int j = 0; j < PAINT_CURVE_NUM_SEGMENTS; j++) {
1343 if (do_rake) {
1344 const float rotation = atan2f(tangents[2 * j + 1], tangents[2 * j]) + float(0.5f * M_PI);
1346 }
1347
1348 if (!stroke_started_) {
1349 last_pressure_ = 1.0;
1350 copy_v2_v2(this->last_mouse_position, data + 2 * j);
1351
1354 std::optional<float3> hit_position = this->get_location(data + 2 * j, original_);
1355 stroke_over_mesh_ = hit_position.has_value();
1356 mul_m4_v3(this->vc.obact->object_to_world().ptr(), hit_position.value_or(float3(0.0f)));
1357 }
1358
1359 stroke_started_ = this->test_start(op, this->last_mouse_position);
1360
1361 if (stroke_started_) {
1362 this->add_step(C, op, data + 2 * j, 1.0);
1363 this->lines_spacing(
1364 C, op, no_pressure_spacing, &length_residue, data + 2 * j, data + 2 * (j + 1));
1365 }
1366 }
1367 else {
1368 this->lines_spacing(
1369 C, op, no_pressure_spacing, &length_residue, data + 2 * j, data + 2 * (j + 1));
1370 }
1371 }
1372 }
1373
1374 this->done(C, false);
1375
1376 return true;
1377}
1378
1379static void paint_stroke_line_constrain(float2 last_mouse_position,
1380 float2 &constrained_pos,
1381 float2 &mouse)
1382{
1383 float2 line = mouse - last_mouse_position;
1384 float angle = atan2f(line[1], line[0]);
1385 const float len = math::length(line);
1386
1387 /* divide angle by PI/4 */
1388 angle = 4.0f * angle / float(M_PI);
1389
1390 /* now take residue */
1391 const float res = angle - floorf(angle);
1392
1393 /* residue decides how close we are at a certain angle */
1394 if (res <= 0.5f) {
1396 }
1397 else {
1398 angle = (floorf(angle) + 1.0f) * float(M_PI_4);
1399 }
1400
1401 mouse[0] = constrained_pos[0] = len * cosf(angle) + last_mouse_position[0];
1402 mouse[1] = constrained_pos[1] = len * sinf(angle) + last_mouse_position[1];
1403}
1404
1406{
1408 /* TODO: Temporary, used to facilitate removing bContext usage in subclasses */
1409 this->evil_C = C;
1410
1412 const Brush *br = this->brush = BKE_paint_brush(paint);
1413 if (paint == nullptr || br == nullptr) {
1414 /* In some circumstances, the context may change during modal execution. In this case,
1415 * we need to cancel the operator. See #147544 and related issues for further information. */
1416 this->done(C, true);
1417 return OPERATOR_CANCELLED;
1418 }
1420 bke::PaintRuntime &paint_runtime = *paint->runtime;
1421 bool first_dab = false;
1422 bool first_modal = false;
1423 bool needs_redraw = false;
1424
1425 if (event->type == INBETWEEN_MOUSEMOVE &&
1427 {
1429 }
1430
1431 /* see if tablet affects event. Line, anchored and drag dot strokes do not support pressure */
1432 const float tablet_pressure = WM_event_tablet_data(event, nullptr, nullptr);
1433 float pressure =
1435 1.0f :
1436 tablet_pressure;
1437
1439 std::string msg = fmt::format("Tablet Pressure: {:.4f}", pressure);
1440 ED_workspace_status_text(C, msg.c_str());
1441 }
1442
1443 /* When processing a timer event the pressure from the event is 0, so use the last valid
1444 * pressure. */
1445 if (event->type == TIMER) {
1446 pressure = last_tablet_event_pressure_;
1447 }
1448 else {
1449 last_tablet_event_pressure_ = pressure;
1450 }
1451
1452 const int input_samples = BKE_brush_input_samples_get(paint, br);
1453 this->add_sample(input_samples, event->mval[0], event->mval[1], pressure);
1454
1455 PaintSample sample_average;
1456 this->calc_average_sample(&sample_average);
1457
1458 /* Tilt. */
1459 if (WM_event_is_tablet(event)) {
1460 tilt_ = event->tablet.tilt;
1461 }
1462
1463#ifdef WITH_INPUT_NDOF
1464 /* let NDOF motion pass through to the 3D view so we can paint and rotate simultaneously!
1465 * this isn't perfect... even when an extra MOUSEMOVE is spoofed, the stroke discards it
1466 * since the 2D deltas are zero -- code in this file needs to be updated to use the
1467 * post-NDOF_MOTION MOUSEMOVE */
1468 if (event->type == NDOF_MOTION) {
1469 return OPERATOR_PASS_THROUGH;
1470 }
1471#endif
1472
1473 /* one time initialization */
1474 if (!stroke_init_) {
1475 if (this->curve_end(C, op)) {
1476 return OPERATOR_FINISHED;
1477 }
1478
1479 stroke_init_ = true;
1480 first_modal = true;
1481 }
1482
1483 /* one time stroke initialization */
1484 if (!stroke_started_) {
1485 pen_flip_ = RNA_boolean_get(op->ptr, "pen_flip");
1486
1487 last_pressure_ = sample_average.pressure;
1488 this->last_mouse_position = sample_average.mouse;
1489 if (paint_stroke_use_scene_spacing(*br, mode)) {
1491 std::optional<float3> hit_position = this->get_location(sample_average.mouse, original_);
1492 stroke_over_mesh_ = hit_position.has_value();
1493 last_world_space_position_ = math::transform_point(this->vc.obact->object_to_world(),
1494 hit_position.value_or(float3(0.0f)));
1495 }
1496 stroke_started_ = this->test_start(op, sample_average.mouse);
1497
1498 if (stroke_started_) {
1499 /* StrokeTestStart often updates the currently active brush so we need to re-retrieve it
1500 * here. */
1501 br = BKE_paint_brush(paint);
1502
1503 if (paint_supports_smooth_stroke(*br, mode, brush_switch_mode_)) {
1504
1505 stroke_cursor_ = WM_paint_cursor_activate(
1507 }
1508
1510 timer_ = WM_event_timer_add(CTX_wm_manager(C), CTX_wm_window(C), TIMER, this->brush->rate);
1511 }
1512
1513 if (br->stroke_method == BRUSH_STROKE_LINE) {
1514 stroke_cursor_ = WM_paint_cursor_activate(
1516 }
1517
1518 first_dab = true;
1519 }
1520 }
1521
1522 /* Cancel */
1523 if (event->type == EVT_MODAL_MAP && event->val == PAINT_STROKE_MODAL_CANCEL) {
1524 if (op->type->cancel) {
1525 if (this->test_cancel()) {
1526 op->type->cancel(C, op);
1527 return OPERATOR_CANCELLED;
1528 }
1529 }
1530 BKE_report(op->reports, RPT_WARNING, "Cancelling this stroke is unsupported");
1531 }
1532
1533 /* Handles shift-key active smooth toggling during a grease pencil stroke. */
1534 if (mode == PaintMode::GPencil) {
1535 if (event->modifier & KM_SHIFT) {
1536 brush_switch_mode_ = BrushSwitchMode::Smooth;
1537 if (!stroke_cursor_) {
1538 stroke_cursor_ = WM_paint_cursor_activate(
1540 }
1541 }
1542 else {
1543 stroke_mode_ = BrushStrokeMode::Normal;
1544 if (stroke_cursor_ != nullptr) {
1545 WM_paint_cursor_end(static_cast<wmPaintCursor *>(stroke_cursor_));
1546 stroke_cursor_ = nullptr;
1547 }
1548 }
1549 }
1550
1551 float2 mouse;
1552 if (event->type == event_type_ && !first_modal) {
1553 if (event->val == KM_RELEASE) {
1554 mouse = {float(event->mval[0]), float(event->mval[1])};
1555 if (this->constrain_line) {
1556 paint_stroke_line_constrain(this->last_mouse_position, this->constrained_pos, mouse);
1557 }
1558 this->line_end(C, op, mouse);
1559 this->done(C, false);
1560 return OPERATOR_FINISHED;
1561 }
1562 }
1563 else if (ELEM(event->type, EVT_RETKEY, EVT_SPACEKEY)) {
1564 this->line_end(C, op, sample_average.mouse);
1565 this->done(C, false);
1566 return OPERATOR_FINISHED;
1567 }
1568 else if (br->stroke_method == BRUSH_STROKE_LINE) {
1569 if (event->modifier & KM_ALT) {
1570 this->constrain_line = true;
1571 }
1572 else {
1573 this->constrain_line = false;
1574 }
1575
1576 mouse = {float(event->mval[0]), float(event->mval[1])};
1577 if (this->constrain_line) {
1578 paint_stroke_line_constrain(this->last_mouse_position, this->constrained_pos, mouse);
1579 }
1580 if (stroke_started_ && (first_modal || ISMOUSE_MOTION(event->type))) {
1583 {
1584 copy_v2_v2(paint_runtime.last_rake, this->last_mouse_position);
1585 }
1586 BKE_paint_calculate_rake_rotation(*paint, *br, mouse, mode, true, true);
1587 }
1588 }
1589 else if (first_modal ||
1590 /* regular dabs */
1591 (!(br->stroke_method == BRUSH_STROKE_AIRBRUSH) && ISMOUSE_MOTION(event->type)) ||
1592 /* airbrush */
1593 ((br->stroke_method == BRUSH_STROKE_AIRBRUSH) && event->type == TIMER &&
1594 event->customdata == timer_))
1595 {
1596 if (paint_smooth_stroke(*this->brush,
1597 &sample_average,
1598 mode,
1599 brush_switch_mode_,
1600 zoom_2d_,
1601 this->last_mouse_position,
1602 last_pressure_,
1603 mouse,
1604 pressure))
1605 {
1606 if (stroke_started_) {
1607 if (paint_space_stroke_enabled(*br, mode)) {
1608 if (this->space_stroke(C, op, mouse, pressure)) {
1609 needs_redraw = true;
1610 }
1611 }
1612 else {
1613 const float2 mouse_delta = mouse - this->last_mouse_position;
1614 stroke_distance_ += math::length(mouse_delta);
1615 this->add_step(C, op, mouse, pressure);
1616 needs_redraw = true;
1617 }
1618 }
1619 }
1620 }
1621
1622 /* we want the stroke to have the first daub at the start location
1623 * instead of waiting till we have moved the space distance */
1624 if (first_dab && paint_space_stroke_enabled(*br, mode) && !(br->flag & BRUSH_SMOOTH_STROKE)) {
1625 paint_runtime.overlap_factor = paint_stroke_integrate_overlap(*br, 1.0);
1626 this->add_step(C, op, sample_average.mouse, sample_average.pressure);
1627 needs_redraw = true;
1628 }
1629
1630 /* Don't update the paint cursor in #INBETWEEN_MOUSEMOVE events. */
1631 if (event->type != INBETWEEN_MOUSEMOVE) {
1632 wmWindow *window = CTX_wm_window(C);
1633 ARegion *region = CTX_wm_region(C);
1634
1635 if (region && (paint->flags & PAINT_SHOW_BRUSH)) {
1636 WM_paint_cursor_tag_redraw(window, region);
1637 }
1638 }
1639
1640 /* Draw for all events (even in between) otherwise updating the brush
1641 * display is noticeably delayed.
1642 */
1643 if (needs_redraw) {
1644 this->redraw(false);
1645 }
1646
1648}
1649
1651{
1653 /* TODO: Temporary, used to facilitate removing bContext usage in subclasses */
1654 this->evil_C = C;
1655
1657 PropertyRNA *prop = RNA_struct_find_property(op->ptr, "override_location");
1658 const bool override_location = prop && RNA_property_boolean_get(op->ptr, prop) &&
1659 mode != PaintMode::Texture2D;
1660
1661 RNA_BEGIN (op->ptr, itemptr, "stroke") {
1662 float2 mval;
1663 RNA_float_get_array(&itemptr, "mouse_event", mval);
1664
1665 if (!stroke_started_) {
1666 stroke_started_ = this->test_start(op, mval);
1667 }
1668
1669 if (!stroke_started_) {
1670 continue;
1671 }
1672
1673 /* This mimics `add_step` to update various properties on PaintRuntime. */
1675 RNA_float_get_array(&itemptr, "location", step.location);
1676 RNA_float_get_array(&itemptr, "mouse_event", step.mouse_event);
1677 step.pressure = RNA_float_get(&itemptr, "pressure");
1679 this->paint, mode, *this->brush, step.pressure, stroke_mode_, zoom_2d_, mval);
1680 step.size = RNA_float_get(&itemptr, "size");
1681 step.tilt = {RNA_float_get(&itemptr, "x_tilt"), RNA_float_get(&itemptr, "y_tilt")};
1682 /* TODO: Note, this misses both `time` and `is_start`, but neither of those systems
1683 * (UV / Annotations) use this base class */
1684
1685 /* TODO: This process misses updating some values at the moment, see `add_step` */
1686 float3 dummy_location;
1687 bool dummy_is_set;
1688 this->update(
1689 C, *this->brush, mode, mval, step.mouse, step.pressure, dummy_location, &dummy_is_set);
1690
1691 if (override_location) {
1692 if (std::optional<float3> location = this->get_location(step.mouse, false)) {
1693 step.location = *location;
1694 this->update_step(op, step);
1695 }
1696 }
1697 else {
1698 this->update_step(op, step);
1699 }
1700 }
1701 RNA_END;
1702
1703 const bool ok = stroke_started_;
1704
1705 this->done(C, !ok);
1706
1708}
1709
1711{
1713 this->done(C, false);
1714}
1715
1717{
1719 this->done(C, true);
1720}
1721
1722static const bToolRef *brush_tool_get(const ScrArea *area, const ARegion *region)
1723{
1724 if ((area && ELEM(area->spacetype, SPACE_VIEW3D, SPACE_IMAGE)) &&
1725 (region && region->regiontype == RGN_TYPE_WINDOW))
1726 {
1727 if (area->runtime.tool && area->runtime.tool->runtime &&
1729 {
1730 return area->runtime.tool;
1731 }
1732 }
1733 return nullptr;
1734}
1735
1737{
1739 const Object *ob = CTX_data_active_object(C);
1740 const ScrArea *area = CTX_wm_area(C);
1741 const ARegion *region = CTX_wm_region(C);
1742 return paint_brush_tool_poll(area, region, paint, ob);
1743}
1744
1746 const ARegion *region,
1747 const Paint *paint,
1748 const Object *ob)
1749{
1750 if (!paint) {
1751 return false;
1752 }
1753
1755 return false;
1756 }
1757
1758 const bToolRef *tref = brush_tool_get(area, region);
1759 if (!tref) {
1760 return false;
1761 }
1762
1763 if (ob) {
1764 return true;
1765 }
1766
1767 /* Be permissive painting in the Image Editor without an active object. */
1769}
1770
1772{
1774
1775 if (!paint) {
1776 return false;
1777 }
1778
1780 return false;
1781 }
1782
1783 const ScrArea *area = CTX_wm_area(C);
1784 const ARegion *region = CTX_wm_region(C);
1785 const bToolRef *tref = brush_tool_get(area, region);
1786
1787 if (!tref) {
1788 return false;
1789 }
1790
1791 /* Don't use brush cursor when the tool sets its own cursor. */
1792 if (tref->runtime->cursor != WM_CURSOR_DEFAULT) {
1793 return false;
1794 }
1795
1797 return true;
1798 }
1799
1800 /* Be permissive painting in the Image Editor without an active object. */
1802}
1803
1804} // namespace blender::ed::sculpt_paint
#define M_PI
#define M_PI_4
Object is a sort of wrapper for general info.
#define PAINT_MAX_INPUT_SAMPLES
#define RGN_TYPE_ANY
#define SPACE_TYPE_ANY
#define USER_EXPERIMENTAL_TEST(userdef, member)
Read Blender memory allocation and freeing.
#define PRF_scope(category)
#define RNA_BEGIN(sptr, itemptr, propname)
#define RNA_END
#define C
Definition RandGen.cpp:29
#define BLI_assert(a)
#define ELEM(...)
btScalar max
SIMD_FORCE_INLINE btScalar angle(const btVector3 &v) const
Return the angle between this and another vector.
Definition btVector3.h:356
SIMD_FORCE_INLINE btScalar length() const
Return the length of the vector.
Definition btVector3.h:257
static RandomNumberGenerator from_random_seed()
Definition rand.cc:288
VecBase< float, D > step(VecOp< float, D >, VecOp< float, D >) RET
float length(VecOp< float, D >) RET
float2x2 invert(float2x2 mat)
float average(float2 a)
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
ccl_device_inline dual3 fabs(const ccl_private dual3 &a)
Definition math_dual.h:241
bool supports_size_pressure(const Brush &brush)
Definition brush.cc:2061
void common_pressure_curves_init(Brush &brush)
Definition brush.cc:1549
bool implements_3d_texture_paint(const Brush &brush)
Definition brush.cc:1850
void set_overlay_brush_override(Paint &paint, eOverlayFlags flag)
bool is_cloth_deform_brush(const Brush &brush)
static void paint_draw_line_cursor(bContext *, const int2 &xy, const float2 &, void *customdata)
bool paint_supports_texture(PaintMode mode)
static float paint_space_stroke_spacing_no_pressure(const ViewContext &vc, const Paint *paint, const PaintMode paint_mode, const Brush *brush, float3 last_world_space_position, float zoom_2d)
static bool paint_brush_type_require_inbetween_mouse_events(const Brush &brush, const PaintMode mode)
static bool sculpt_is_grab_tool(const Brush &br)
static float paint_stroke_overlapped_curve(const Brush &br, const float x, const float spacing)
static bool paint_stroke_use_jitter(const PaintMode mode, const Brush &brush, const bool invert)
bool paint_supports_dynamic_tex_coords(const Brush &br, PaintMode mode)
bool paint_supports_dynamic_size(const Brush &br, PaintMode mode)
float2 paint_stroke_jitter_pos(Paint *paint, PaintMode mode, const Brush &brush, float pressure, BrushStrokeMode stroke_mode, float zoom_2d, const float2 &mval)
static float paint_space_stroke_spacing_variable(ViewContext &vc, const Paint *paint, const PaintMode paint_mode, const Brush *brush, float3 last_world_space_position, float zoom_2d, const float last_pressure, const float pressure, const float pressure_delta, const float length)
static bool print_pressure_status_enabled()
static bool paint_brush_type_require_location(const Brush &brush, const PaintMode mode)
static float paint_stroke_integrate_overlap(const Brush &br, const float factor)
static bool paint_stroke_use_dash(const Brush &brush)
static void paint_draw_smooth_cursor(bContext *C, const int2 &xy, const float2 &, void *customdata)
static bool paint_brush_type_raycast_original(const Brush &brush, PaintMode)
bool paint_brush_cursor_poll(bContext *C)
float object_space_radius_get(const ViewContext &vc, const Paint &paint, const Brush &brush, const float3 &location, const float scale_factor)
Definition sculpt.cc:121
static bool curves_sculpt_brush_uses_spacing(const eBrushCurvesSculptType tool)
bool paint_space_stroke_enabled(const Brush &br, PaintMode mode)
bool paint_supports_smooth_stroke(const Brush &brush, PaintMode mode, BrushSwitchMode brush_switch_mode)
static const bToolRef * brush_tool_get(const ScrArea *area, const ARegion *region)
static void paint_stroke_line_constrain(float2 last_mouse_position, float2 &constrained_pos, float2 &mouse)
static bool paint_stroke_use_scene_spacing(const Brush &brush, const PaintMode mode)
static bool paint_smooth_stroke(const Brush &brush, const PaintSample *sample, const PaintMode mode, const BrushSwitchMode brush_switch_mode, float zoom_2d, float2 last_mouse_position, float last_pressure, float2 &r_mouse, float &r_pressure)
bool paint_brush_tool_poll(bContext *C)
static float paint_space_stroke_spacing(const ViewContext &vc, const Paint *paint, const PaintMode paint_mode, const Brush *brush, float3 last_world_space_position, float zoom_2d, const float size_factor, const float pressure)
T length(const VecBase< T, Size > &a)
QuaternionBase< T > normalize_and_get_length(const QuaternionBase< T > &q, T &out_length)
T interpolate(const T &a, const T &b, const FactorT &t)
MatBase< T, NumCol, NumRow > normalize(const MatBase< T, NumCol, NumRow > &a)
T distance_squared(const VecBase< T, Size > &a, const VecBase< T, Size > &b)
VecBase< T, 3 > transform_point(const CartesianBasis &basis, const VecBase< T, 3 > &v)
UserDef U
Definition blender.cc:58
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
float WM_event_tablet_data(const wmEvent *event, bool *r_pen_flip, float r_tilt[2])
VecBase< uint8_t, 4 > uchar4
ScrArea * CTX_wm_area(const bContext *C)
int RNA_enum_get(PointerRNA *ptr, const char *name)
bool get_imapaint_zoom(bContext *C, float *zoomx, float *zoomy)
@ RPT_WARNING
Definition BKE_report.hh:40
void BKE_curve_forward_diff_tangent_bezier(float q0, float q1, float q2, float q3, float *p, int it, int stride)
Definition curve.cc:1724
void immEnd()
PropertyRNA * RNA_struct_find_property(PointerRNA *ptr, const char *identifier)
ViewContext ED_view3d_viewcontext_init(bContext *C, Depsgraph *depsgraph)
MINLINE void copy_v3_v3(float r[3], const float a[3])
unsigned int uint
Definition BKE_text.h:55
@ BRUSH_STROKE_DRAG_DOT
@ BRUSH_STROKE_ANCHORED
@ BRUSH_STROKE_AIRBRUSH
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)
void immUniform2f(const char *name, float x, float y)
MINLINE void copy_v2_v2(float r[2], const float a[2])
GPUVertFormat * immVertexFormat()
float BKE_brush_radius_get(const Paint *paint, const Brush *brush)
Definition brush.cc:1366
void immBegin(GPUPrimType, uint vertex_len)
void GPU_line_smooth(bool enable)
Definition gpu_state.cc:80
wmWindow * CTX_wm_window(const bContext *C)
MINLINE float normalize_v2(float n[2])
void RNA_enum_set(PointerRNA *ptr, const char *name, int value)
void ED_view3d_project_v2(const ARegion *region, const float world[3], float r_region_co[2])
@ BRUSH_CLOTH_DEFORM_GRAB
void immUnbindProgram()
bool BKE_brush_use_alpha_pressure(const Brush *brush)
Definition brush.cc:1385
MINLINE void mul_v3_fl(float r[3], float f)
@ GPU_BLEND_NONE
Definition GPU_state.hh:88
@ GPU_BLEND_ALPHA
Definition GPU_state.hh:90
@ SCULPT_BRUSH_TYPE_CLOTH
@ SCULPT_BRUSH_TYPE_GRAB
@ SCULPT_BRUSH_TYPE_POSE
@ SCULPT_BRUSH_TYPE_ROTATE
@ SCULPT_BRUSH_TYPE_BOUNDARY
@ SCULPT_BRUSH_TYPE_SNAKE_HOOK
@ SCULPT_BRUSH_TYPE_THUMB
@ SCULPT_BRUSH_TYPE_ELASTIC_DEFORM
@ INBETWEEN_MOUSEMOVE
bool RNA_property_boolean_get(PointerRNA *ptr, PropertyRNA *prop)
VecBase< float, 4 > float4
void immBindBuiltinProgram(GPUBuiltinShader shader_id)
void mul_m4_v3(const float M[4][4], float r[3])
bool WM_event_is_tablet(const wmEvent *event)
void BKE_curvemapping_init(CurveMapping *cumap)
float2 BKE_brush_jitter_pos(const Paint &paint, const Brush &brush, const float2 &pos)
Definition brush.cc:1509
@ GPAINT_BRUSH_TYPE_DRAW
VecBase< int32_t, 2 > int2
VecBase< float, 2 > float2
void immUniform1f(const char *name, float x)
void immVertex2f(uint attr_id, float x, float y)
void GPU_viewport_size_get_f(float coords[4])
Definition gpu_state.cc:258
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:162
BMesh const char void * data
@ GPU_SHADER_3D_LINE_DASHED_UNIFORM_COLOR
wmWindowManager * CTX_wm_manager(const bContext *C)
Depsgraph * CTX_data_ensure_evaluated_depsgraph(const bContext *C, bool rna_write_check=false)
PaintMode BKE_paintmode_get_from_tool(const bToolRef *tref)
@ IMAGE_PAINT_BRUSH_TYPE_FILL
@ IMAGE_PAINT_BRUSH_TYPE_CLONE
float BKE_brush_curve_strength(eBrushCurvePreset preset, const CurveMapping *cumap, float distance, float brush_radius)
Definition brush.cc:1691
@ CURVES_SCULPT_BRUSH_TYPE_ADD
@ CURVES_SCULPT_BRUSH_TYPE_DENSITY
RegionView3D * CTX_wm_region_view3d(const bContext *C)
void BKE_paint_update_brush_rake_rotation(Paint &paint, const Brush &brush, float rotation)
bool RNA_boolean_get(PointerRNA *ptr, const char *name)
MINLINE void zero_v3(float r[3])
uint GPU_vertformat_attr_add(GPUVertFormat *format, StringRef name, gpu::VertAttrType type)
void RNA_boolean_set(PointerRNA *ptr, const char *name, bool value)
void BKE_brush_randomize_texture_coords(Paint *paint, bool mask)
Definition brush.cc:1533
wmPaintCursor * WM_paint_cursor_activate(short space_type, short region_type, wmPaintCursorPoll poll, wmPaintCursorDraw draw, void *customdata)
bool BKE_brush_use_size_pressure(const Brush *brush)
Definition brush.cc:1380
ImBuf * BKE_image_acquire_ibuf(Image *ima, ImageUser *iuser, void **r_lock)
const Brush * BKE_paint_brush_for_read(const Paint *paint)
@ WM_CURSOR_DEFAULT
Definition wm_cursors.hh:17
void immUniform4f(const char *name, float x, float y, float z, float w)
Paint * BKE_paint_get_active_from_context(const bContext *C)
MINLINE float max_ff(float a, float b)
Paint * BKE_paint_get_active_from_paintmode(Scene *sce, PaintMode mode)
float paint_calc_object_space_radius(const ViewContext &vc, const float3 &center, float pixel_radius)
MINLINE float square_f(float a)
@ KM_RELEASE
Definition WM_types.hh:325
Brush * BKE_paint_brush(Paint *paint)
@ BRUSH_SPACING_PRESSURE
@ BRUSH_JITTER_PRESSURE
@ BRUSH_ABSOLUTE_JITTER
@ USER_TABLET_SHOW_DEBUG_VALUES
Scene * CTX_data_scene(const bContext *C)
void WM_event_timer_remove(wmWindowManager *wm, wmWindow *, wmTimer *timer)
void GPU_blend(GPUBlend blend)
Definition gpu_state.cc:44
wmTimer * WM_event_timer_add(wmWindowManager *wm, wmWindow *win, const wmEventType event_type, const double time_step)
void immUniform1i(const char *name, int x)
VecBase< float, 3 > float3
void immUniformColor4ubv(const unsigned char rgba[4])
void immVertex2fv(uint attr_id, const float data[2])
void BKE_curve_forward_diff_bezier(float q0, float q1, float q2, float q3, float *p, int it, int stride)
Definition curve.cc:1696
Object * CTX_data_active_object(const bContext *C)
ARegion * CTX_wm_region(const bContext *C)
void ED_workspace_status_text(bContext *C, const char *str)
Definition area.cc:1153
PaintMode BKE_paintmode_get_active_from_context(const bContext *C)
float BKE_curvemapping_evaluateF(const CurveMapping *cumap, int cur, float value)
float RNA_float_get(PointerRNA *ptr, const char *name)
void WM_paint_cursor_tag_redraw(wmWindow *win, ARegion *)
Definition wm_draw.cc:1698
void RNA_float_get_array(PointerRNA *ptr, const char *name, float *values)
bool WM_paint_cursor_end(wmPaintCursor *handle)
void BKE_image_release_ibuf(Image *ima, ImBuf *ibuf, void *lock)
int BKE_brush_input_samples_get(const Paint *paint, const Brush *brush)
Definition brush.cc:1485
#define PAINT_STROKE_MODAL_CANCEL
#define PAINT_CURVE_NUM_SEGMENTS
#define floorf
#define fabsf
#define sqrtf
#define sinf
#define cosf
#define atan2f
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
eRegion_Type regiontype
eBrushImagePaintType image_brush_type
eBrushSculptType sculpt_brush_type
struct MTex mask_mtex
eBrushClothDeformType cloth_deform_type
eBrushCurvesSculptType curves_sculpt_brush_type
struct CurveMapping * curve_size
eOverlayFlags overlay_flags
eBrushStrokeType stroke_method
const ColorSpace * colorspace
ImBufByteBuffer byte_buffer
const float * float_data() const
eMTex_BrushMapMode brush_map_mode
eMTex_BrushAngleMode brush_angle_mode
struct CurveMapping * cavity_curve
struct UnifiedPaintSettings unified_paint_settings
bke::PaintRuntime * runtime
eRegionView3D_Flag rflag
ScrArea_Runtime runtime
struct Image * ima
ebToolRef_Runtime_Flag flag
bToolRef_Runtime * runtime
const ocio::ColorSpace * colorspace
virtual void done(bool is_cancel, bool stroke_started)=0
virtual void redraw(bool final)=0
virtual std::optional< float3 > get_location(float2 mouse, bool force_original)=0
virtual bool test_start(wmOperator *op, float2 mouse)=0
wmOperatorStatus modal(bContext *C, wmOperator *op, const wmEvent *event)
wmOperatorStatus exec(bContext *C, wmOperator *op)
virtual void update_step(wmOperator *op, const StrokeStep &stroke_step)=0
wmEventType type
Definition WM_types.hh:817
wmEventModifierFlag modifier
Definition WM_types.hh:834
void(*) cancel(bContext *C, wmOperator *op)
Definition WM_types.hh:1149
struct ReportList * reports
struct wmOperatorType * type
#define ISMOUSE_MOTION(event_type)