Blender V5.3
paint_image_proj.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
9
10#include <algorithm>
11#include <cfloat>
12#include <climits>
13#include <cmath>
14#include <cstring>
15
16#include "IMB_imbuf_types.hh"
17#include "MEM_guardedalloc.h"
18
19#ifdef WIN32
20# include "BLI_winstuff.hh"
21#endif
22
23#include "BLI_linklist.hh"
24#include "BLI_listbase.hh"
25#include "BLI_math_base_safe.hh"
26#include "BLI_math_bits.hh"
28#include "BLI_math_color_c.hh"
29#include "BLI_math_geom_c.hh"
30#include "BLI_math_vector.hh"
31#include "BLI_memarena.hh"
32#include "BLI_rect.hh"
33#include "BLI_string.hh"
34#include "BLI_string_utf8.hh"
35#include "BLI_task_c.hh"
36#include "BLI_threads.hh"
37#include "BLI_utildefines.hh"
38
39#include "atomic_ops.h"
40
41#include "BLT_translation.hh"
42
43#include "IMB_imbuf.hh"
44#include "IMB_interp.hh"
45#include "IMB_partial_update.hh"
46
47#include "DNA_brush_types.h"
49#include "DNA_defs.h"
50#include "DNA_material_types.h"
51#include "DNA_mesh_types.h"
52#include "DNA_node_types.h"
53#include "DNA_object_enums.h"
54#include "DNA_object_types.h"
55#include "DNA_scene_types.h"
56#include "DNA_screen_types.h"
57#include "DNA_space_types.h"
58
59#include "BKE_attribute.h"
60#include "BKE_attribute.hh"
62#include "BKE_brush.hh"
63#include "BKE_camera.h"
64#include "BKE_colorband.hh"
65#include "BKE_colortools.hh"
66#include "BKE_context.hh"
67#include "BKE_customdata.hh"
68#include "BKE_global.hh"
69#include "BKE_idprop.hh"
70#include "BKE_image.hh"
71#include "BKE_image_gpu.hh"
72#include "BKE_layer.hh"
73#include "BKE_library.hh"
74#include "BKE_main.hh"
76#include "BKE_material.hh"
77#include "BKE_mesh.hh"
78#include "BKE_mesh_mapping.hh"
79#include "BKE_node.hh"
81#include "BKE_node_runtime.hh"
82#include "BKE_object.hh"
83#include "BKE_paint.hh"
84#include "BKE_paint_types.hh"
85#include "BKE_report.hh"
86#include "BKE_scene.hh"
87#include "BKE_screen.hh"
88
89#include "DEG_depsgraph.hh"
91
92#include "ED_image.hh"
93#include "ED_node.hh"
94#include "ED_object.hh"
95#include "ED_paint.hh"
96#include "ED_screen.hh"
97#include "ED_sculpt.hh"
98#include "ED_uvedit.hh"
99#include "ED_view3d.hh"
100#include "ED_view3d_offscreen.hh"
101
102#include "GPU_capabilities.hh"
103#include "GPU_init_exit.hh"
104
105#include "NOD_defaults.hh"
106#include "NOD_shader.h"
107
108#include "UI_interface_layout.hh"
109#include "UI_resources.hh"
110
111#include "WM_api.hh"
112#include "WM_types.hh"
113
114#include "RNA_access.hh"
115#include "RNA_define.hh"
116#include "RNA_enum_types.hh"
117#include "RNA_types.hh"
118
119#include "IMB_colormanagement.hh"
120
121// #include "bmesh_tools.hh"
122
123#include "../paint_intern.hh"
124
125namespace blender {
126
128
129/* Defines and Structs */
130/* unit_float_to_uchar_clamp as inline function */
131BLI_INLINE uchar f_to_char(const float val)
132{
133 return unit_float_to_uchar_clamp(val);
134}
135
136/* ProjectionPaint defines */
137
138/* approx the number of buckets to have under the brush,
139 * used with the brush size to set the ps->buckets_x and ps->buckets_y value.
140 *
141 * When 3 - a brush should have ~9 buckets under it at once
142 * ...this helps for threading while painting as well as
143 * avoiding initializing pixels that won't touch the brush */
144#define PROJ_BUCKET_BRUSH_DIV 4
145
146#define PROJ_BUCKET_RECT_MIN 4
147#define PROJ_BUCKET_RECT_MAX 256
148
149#define PROJ_BOUNDBOX_DIV 8
150#define PROJ_BOUNDBOX_SQUARED (PROJ_BOUNDBOX_DIV * PROJ_BOUNDBOX_DIV)
151
152// #define PROJ_DEBUG_PAINT 1
153// #define PROJ_DEBUG_NOSEAMBLEED 1
154// #define PROJ_DEBUG_PRINT_CLIP 1
155#define PROJ_DEBUG_WINCLIP 1
156
157#ifndef PROJ_DEBUG_NOSEAMBLEED
158/* projectFaceSeamFlags options */
159// #define PROJ_FACE_IGNORE (1<<0) /* When the face is hidden, back-facing or occluded. */
160// #define PROJ_FACE_INIT (1<<1) /* When we have initialized the faces data */
161
162/* If this face has a seam on any of its edges. */
163# define PROJ_FACE_SEAM0 (1 << 0)
164# define PROJ_FACE_SEAM1 (1 << 1)
165# define PROJ_FACE_SEAM2 (1 << 2)
166
167# define PROJ_FACE_NOSEAM0 (1 << 4)
168# define PROJ_FACE_NOSEAM1 (1 << 5)
169# define PROJ_FACE_NOSEAM2 (1 << 6)
170
171/* If the seam is completely initialized, including adjacent seams. */
172# define PROJ_FACE_SEAM_INIT0 (1 << 8)
173# define PROJ_FACE_SEAM_INIT1 (1 << 9)
174# define PROJ_FACE_SEAM_INIT2 (1 << 10)
175
176# define PROJ_FACE_DEGENERATE (1 << 12)
177
178/* face winding */
179# define PROJ_FACE_WINDING_INIT 1
180# define PROJ_FACE_WINDING_CW 2
181
182/* a slightly scaled down face is used to get fake 3D location for edge pixels in the seams
183 * as this number approaches 1.0f the likelihood increases of float precision errors where
184 * it is occluded by an adjacent face */
185# define PROJ_FACE_SCALE_SEAM 0.99f
186#endif /* PROJ_DEBUG_NOSEAMBLEED */
187
188#define PROJ_SRC_VIEW 1
189#define PROJ_SRC_IMAGE_CAM 2
190#define PROJ_SRC_IMAGE_VIEW 3
191#define PROJ_SRC_VIEW_FILL 4
192
193#define PROJ_VIEW_DATA_ID "view_data"
194/* viewmat + winmat + clip_start + clip_end + is_ortho */
195#define PROJ_VIEW_DATA_SIZE (4 * 4 + 4 * 4 + 3)
196
197#define PROJ_BUCKET_NULL 0
198#define PROJ_BUCKET_INIT (1 << 0)
199// #define PROJ_BUCKET_CLONE_INIT (1<<1)
200
201/* used for testing doubles, if a point is on a line etc */
202#define PROJ_GEOM_TOLERANCE 0.00075f
203#define PROJ_PIXEL_TOLERANCE 0.01f
204
205/* vert flags */
206#define PROJ_VERT_CULL 1
207
208/* to avoid locking in tile initialization */
209#define TILE_PENDING POINTER_FROM_INT(-1)
210
211struct ProjPaintState;
212
222
223 /* Regenerated in project_paint_op to avoid calling `_for_write()` in threaded loops. */
224 float *float_data_mut = nullptr;
226
229 volatile const void **undoRect;
234 bool **valid;
235 bool touch;
241};
242
247 /* Support for painting from multiple views at once,
248 * currently used to implement symmetry painting,
249 * we can assume at least the first is set while painting. */
251
252 /* Store initial starting points for perlin noise on the beginning of each stroke when using
253 * color jitter. */
254 std::optional<float3> initial_hsv_jitter;
255
258
260
262
263 /* trick to bypass regular paint and allow clone picking */
265
266 /* In ProjPaintState, only here for convenience */
270};
271
273 float seam_uvs[2][2];
274 float seam_puvs[2][2];
276};
277
278struct VertSeam;
279
280/* Main projection painting struct passed to all projection painting functions */
285 Depsgraph *depsgraph;
287 /* PROJ_SRC_**** */
289
290 /* Scene linear paint color. It can change depending on inverted mode or not. */
292 float dither;
293
296
302 short blend;
305
308 /* for symmetry, we need to store modified object matrix */
309 float obmat[4][4];
310 float obmat_imat[4][4];
311 /* end similarities with ImagePaintState */
312
317
318 /* projection painting only */
327
334
337
340
341 /* The following are all updated for symmetry passes. */
345 float screenMin[2];
346 float screenMax[2];
351 int winx, winy;
352
353 /* options for projection painting */
359
377
389#ifndef PROJ_DEBUG_NOSEAMBLEED
392#endif
393 /* clone vars */
394 float cloneOffset[2];
395
397 float projectMat[4][4];
399 float projectMatInv[4][4];
401 float viewDir[3];
403 float viewPos[3];
405
406 /* reproject vars */
411
412 /* threads */
414 int bucketMin[2];
415 int bucketMax[2];
418
421
422 /* -------------------------------------------------------------------- */
423 /* Vars shared between multiple views (keep last) */
428
429#define PROJ_PAINT_STATE_SHARED_MEMCPY(ps_dst, ps_src) \
430 MEMCPY_STRUCT_AFTER(ps_dst, ps_src, is_shared_user)
431
432#define PROJ_PAINT_STATE_SHARED_CLEAR(ps) MEMSET_STRUCT_AFTER(ps, 0, is_shared_user)
433
435
438 float *cavities;
439
440#ifndef PROJ_DEBUG_NOSEAMBLEED
451 ListBaseT<VertSeam> *vertSeams;
452#endif
453
455
460
466 std::optional<bool> select_poly_single;
467 const bool *select_poly_eval;
468 std::optional<bool> hide_poly_single;
469 const bool *hide_poly_eval;
470 std::optional<int> material_index_single;
475
477
484 std::optional<float2> poly_to_loop_uv_single;
487
488 /* Actual material for each index, either from object or Mesh datablock... */
490};
491
499
503 float f[4];
504};
505
506struct ProjPixel {
508 float projCoSS[2];
509 float worldCoSS[3];
510
511 short x_px, y_px;
512
519
520 /* for various reasons we may want to mask out painting onto this pixel */
522
523 /* Only used when the airbrush is disabled.
524 * Store the max mask value to avoid painting over an area with a lower opacity
525 * with an advantage that we can avoid touching the pixel at all, if the
526 * new mask value is lower than mask_accum */
528
529 /* horrible hack, store tile valid flag pointer here to re-validate tiles
530 * used for anchored and drag-dot strokes */
531 bool *valid;
532
535 /* For write access */
537};
538
543
544/* undo tile pushing */
551
552struct VertSeam {
554 int tri;
556 float angle;
558 float uv[2];
559};
560
561/* -------------------------------------------------------------------- */
564
565#define PS_CORNER_TRI_AS_VERT_INDEX_3(ps, tri) \
566 ps->corner_verts_eval[tri[0]], ps->corner_verts_eval[tri[1]], ps->corner_verts_eval[tri[2]],
567
568#define PS_CORNER_TRI_AS_UV_3(uvlayer, face_i, tri) \
569 uvlayer[face_i][tri[0]], uvlayer[face_i][tri[1]], uvlayer[face_i][tri[2]],
570
571#define PS_CORNER_TRI_ASSIGN_UV_3(uv_tri, uvlayer, face_i, tri) \
572 { \
573 (uv_tri)[0] = uvlayer[face_i][tri[0]]; \
574 (uv_tri)[1] = uvlayer[face_i][tri[1]]; \
575 (uv_tri)[2] = uvlayer[face_i][tri[2]]; \
576 } \
577 ((void)0)
578
580
581/* Finish projection painting structs */
582
583static int project_paint_face_paint_tile(Image *ima, const float *uv)
584{
585 if (ima == nullptr || ima->source != IMA_SRC_TILED) {
586 return 0;
587 }
588
589 /* Currently, faces are assumed to belong to one tile, so checking the first loop is enough. */
590 int tx = int(uv[0]);
591 int ty = int(uv[1]);
592 return 1001 + 10 * ty + tx;
593}
594
595static Material *tex_get_material(const ProjPaintState *ps, int face_i)
596{
597 int mat_nr = 0;
598 if (ps->material_index_single) {
599 mat_nr = ps->material_index_single.value();
600 }
601 else if (ps->material_indices) {
602 mat_nr = ps->material_indices[face_i];
603 }
604 if (mat_nr >= 0 && mat_nr <= ps->ob->totcol) {
605 return ps->mat_array[mat_nr];
606 }
607
608 return nullptr;
609}
610
612{
613 const int face_i = ps->corner_tri_faces_eval[tri_index];
614 Material *ma = tex_get_material(ps, face_i);
615 return ma ? ma->texpaintslot + ma->paint_active_slot : nullptr;
616}
617
618static Image *project_paint_face_paint_image(const ProjPaintState *ps, int tri_index)
619{
620 if (ps->do_stencil_brush) {
621 return ps->stencil_ima;
622 }
623
624 const int face_i = ps->corner_tri_faces_eval[tri_index];
625 Material *ma = tex_get_material(ps, face_i);
626 TexPaintSlot *slot = ma ? ma->texpaintslot + ma->paint_active_slot : nullptr;
627 return slot ? slot->ima : ps->canvas_ima;
628}
629
631{
632 const int face_i = ps->corner_tri_faces_eval[tri_index];
633 Material *ma = tex_get_material(ps, face_i);
634 return ma ? ma->texpaintslot + ma->paint_clone_slot : nullptr;
635}
636
637static Image *project_paint_face_clone_image(const ProjPaintState *ps, int tri_index)
638{
639 const int face_i = ps->corner_tri_faces_eval[tri_index];
640 Material *ma = tex_get_material(ps, face_i);
641 TexPaintSlot *slot = ma ? ma->texpaintslot + ma->paint_clone_slot : nullptr;
642 return slot ? slot->ima : ps->clone_ima;
643}
644
648static int project_bucket_offset(const ProjPaintState *ps, const float projCoSS[2])
649{
650 /* If we were not dealing with screen-space 2D coords we could simple do...
651 * ps->bucketRect[x + (y*ps->buckets_y)] */
652
653 /* please explain?
654 * projCoSS[0] - ps->screenMin[0] : zero origin
655 * ... / ps->screen_width : range from 0.0 to 1.0
656 * ... * ps->buckets_x : use as a bucket index
657 *
658 * Second multiplication does similar but for vertical offset
659 */
660 return int(((projCoSS[0] - ps->screenMin[0]) / ps->screen_width) * ps->buckets_x) +
661 (int(((projCoSS[1] - ps->screenMin[1]) / ps->screen_height) * ps->buckets_y) *
662 ps->buckets_x);
663}
664
665static int project_bucket_offset_safe(const ProjPaintState *ps, const float projCoSS[2])
666{
667 int bucket_index = project_bucket_offset(ps, projCoSS);
668
669 if (bucket_index < 0 || bucket_index >= ps->buckets_x * ps->buckets_y) {
670 return -1;
671 }
672 return bucket_index;
673}
674
675static float VecZDepthOrtho(
676 const float pt[2], const float v1[3], const float v2[3], const float v3[3], float w[3])
677{
678 barycentric_weights_v2(v1, v2, v3, pt, w);
679 return (v1[2] * w[0]) + (v2[2] * w[1]) + (v3[2] * w[2]);
680}
681
682static float VecZDepthPersp(
683 const float pt[2], const float v1[4], const float v2[4], const float v3[4], float w[3])
684{
685 float wtot_inv, wtot;
686 float w_tmp[3];
687
688 barycentric_weights_v2_persp(v1, v2, v3, pt, w);
689 /* for the depth we need the weights to match what
690 * barycentric_weights_v2 would return, in this case its easiest just to
691 * undo the 4th axis division and make it unit-sum
692 *
693 * don't call barycentric_weights_v2() because our callers expect 'w'
694 * to be weighted from the perspective */
695 w_tmp[0] = w[0] * v1[3];
696 w_tmp[1] = w[1] * v2[3];
697 w_tmp[2] = w[2] * v3[3];
698
699 wtot = w_tmp[0] + w_tmp[1] + w_tmp[2];
700
701 if (wtot != 0.0f) {
702 wtot_inv = 1.0f / wtot;
703
704 w_tmp[0] = w_tmp[0] * wtot_inv;
705 w_tmp[1] = w_tmp[1] * wtot_inv;
706 w_tmp[2] = w_tmp[2] * wtot_inv;
707 }
708 else { /* dummy values for zero area face */
709 w_tmp[0] = w_tmp[1] = w_tmp[2] = 1.0f / 3.0f;
710 }
711 /* done mimicking barycentric_weights_v2() */
712
713 return (v1[2] * w_tmp[0]) + (v2[2] * w_tmp[1]) + (v3[2] * w_tmp[2]);
714}
715
716/* Return the top-most face index that the screen space coord 'pt' touches (or -1) */
717static int project_paint_PickFace(const ProjPaintState *ps, const float pt[2], float w[3])
718{
719 LinkNode *node;
720 float w_tmp[3];
721 int bucket_index;
722 int best_tri_index = -1;
723 float z_depth_best = FLT_MAX, z_depth;
724
725 bucket_index = project_bucket_offset_safe(ps, pt);
726 if (bucket_index == -1) {
727 return -1;
728 }
729
730 /* we could return 0 for 1 face buckets, as long as this function assumes
731 * that the point its testing is only every originated from an existing face */
732
733 for (node = ps->bucketFaces[bucket_index]; node; node = node->next) {
734 const int tri_index = POINTER_AS_INT(node->link);
735 const int3 &tri = ps->corner_tris_eval[tri_index];
736 const float *vtri_ss[3] = {
737 ps->screenCoords[ps->corner_verts_eval[tri[0]]],
738 ps->screenCoords[ps->corner_verts_eval[tri[1]]],
739 ps->screenCoords[ps->corner_verts_eval[tri[2]]],
740 };
741
742 if (isect_point_tri_v2(pt, UNPACK3(vtri_ss))) {
743 if (ps->is_ortho) {
744 z_depth = VecZDepthOrtho(pt, UNPACK3(vtri_ss), w_tmp);
745 }
746 else {
747 z_depth = VecZDepthPersp(pt, UNPACK3(vtri_ss), w_tmp);
748 }
749
750 if (z_depth < z_depth_best) {
751 best_tri_index = tri_index;
752 z_depth_best = z_depth;
753 copy_v3_v3(w, w_tmp);
754 }
755 }
756 }
757
759 return best_tri_index;
760}
761
762/* Set the top-most face color that the screen space coord 'pt' touches
763 * (or return 0 if none touch) */
765 const ProjPaintState *ps, const float pt[2], float *rgba_fp, uchar *rgba, const bool interp)
766{
767 const float *tri_uv[3];
768 float w[3], uv[2];
769 int tri_index;
770 Image *ima;
771 ImBuf *ibuf;
772
773 tri_index = project_paint_PickFace(ps, pt, w);
774
775 if (tri_index == -1) {
776 return false;
777 }
778
779 const int3 &tri = ps->corner_tris_eval[tri_index];
780 if (ps->poly_to_loop_uv_single) {
781 copy_v2_v2(uv, ps->poly_to_loop_uv_single.value());
782 }
783 else {
785 tri_uv, ps->poly_to_loop_uv, ps->corner_tri_faces_eval[tri_index], tri);
786 interp_v2_v2v2v2(uv, UNPACK3(tri_uv), w);
787 }
788
789 ima = project_paint_face_paint_image(ps, tri_index);
791 int tile_number;
792 if (ps->poly_to_loop_uv_single) {
793 tile_number = project_paint_face_paint_tile(ima, ps->poly_to_loop_uv_single.value());
794 }
795 else {
796 tile_number = project_paint_face_paint_tile(ima, tri_uv[0]);
797 }
798 /* XXX get appropriate ImageUser instead */
799 ImageUser iuser;
800 BKE_imageuser_default(&iuser);
801 iuser.tile = tile_number;
802 iuser.framenr = ima->lastframe;
803 ibuf = BKE_image_acquire_ibuf(ima, &iuser, nullptr);
804 if (ibuf == nullptr) {
805 return false;
806 }
807
808 float x = uv[0] * ibuf->x;
809 float y = uv[1] * ibuf->y;
810 if (interp) {
811 x -= 0.5f;
812 y -= 0.5f;
813 }
814
815 if (ibuf->float_data()) {
818 col = math::clamp(col, 0.0f, 1.0f);
819 if (rgba_fp) {
820 memcpy(rgba_fp, &col, sizeof(col));
821 }
822 else {
824 }
825 }
826 else {
829 if (rgba) {
830 memcpy(rgba, &col, sizeof(col));
831 }
832 else {
833 straight_uchar_to_premul_float(rgba_fp, col);
834 }
835 }
836 BKE_image_release_ibuf(ima, ibuf, nullptr);
837 return true;
838}
839
848static int project_paint_occlude_ptv(const float pt[3],
849 const float v1[4],
850 const float v2[4],
851 const float v3[4],
852 float w[3],
853 const bool is_ortho)
854{
855 /* if all are behind us, return false */
856 if (v1[2] > pt[2] && v2[2] > pt[2] && v3[2] > pt[2]) {
857 return 0;
858 }
859
860 /* do a 2D point in try intersection */
861 if (!isect_point_tri_v2(pt, v1, v2, v3)) {
862 return 0;
863 }
864
865 /* From here on we know there IS an intersection */
866 /* if ALL of the verts are in front of us then we know it intersects ? */
867 if (v1[2] < pt[2] && v2[2] < pt[2] && v3[2] < pt[2]) {
868 return 1;
869 }
870
871 /* we intersect? - find the exact depth at the point of intersection */
872 /* Is this point is occluded by another face? */
873 if (is_ortho) {
874 if (VecZDepthOrtho(pt, v1, v2, v3, w) < pt[2]) {
875 return 2;
876 }
877 }
878 else {
879 if (VecZDepthPersp(pt, v1, v2, v3, w) < pt[2]) {
880 return 2;
881 }
882 }
883 return -1;
884}
885
886static int project_paint_occlude_ptv_clip(const float pt[3],
887 const float v1[4],
888 const float v2[4],
889 const float v3[4],
890 const float v1_3d[3],
891 const float v2_3d[3],
892 const float v3_3d[3],
893 float w[3],
894 const bool is_ortho,
895 RegionView3D *rv3d)
896{
897 float wco[3];
898 int ret = project_paint_occlude_ptv(pt, v1, v2, v3, w, is_ortho);
899
900 if (ret <= 0) {
901 return ret;
902 }
903
904 if (ret == 1) { /* weights not calculated */
905 if (is_ortho) {
906 barycentric_weights_v2(v1, v2, v3, pt, w);
907 }
908 else {
909 barycentric_weights_v2_persp(v1, v2, v3, pt, w);
910 }
911 }
912
913 /* Test if we're in the clipped area, */
914 interp_v3_v3v3v3(wco, v1_3d, v2_3d, v3_3d, w);
915
916 if (!ED_view3d_clipping_test(rv3d, wco, true)) {
917 return 1;
918 }
919
920 return -1;
921}
922
923/* Check if a screen-space location is occluded by any other faces
924 * check, pixelScreenCo must be in screen-space, its Z-Depth only needs to be used for comparison
925 * and doesn't need to be correct in relation to X and Y coords
926 * (this is the case in perspective view) */
928 LinkNode *bucketFace,
929 const int orig_face,
930 const float pixelScreenCo[4])
931{
932 int isect_ret;
933 const bool do_clip = RV3D_CLIPPING_ENABLED(ps->v3d, ps->rv3d);
934
935 /* we could return false for 1 face buckets, as long as this function assumes
936 * that the point its testing is only every originated from an existing face */
937
938 for (; bucketFace; bucketFace = bucketFace->next) {
939 const int tri_index = POINTER_AS_INT(bucketFace->link);
940
941 if (orig_face != tri_index) {
942 const int3 &tri = ps->corner_tris_eval[tri_index];
943 const float *vtri_ss[3] = {
944 ps->screenCoords[ps->corner_verts_eval[tri[0]]],
945 ps->screenCoords[ps->corner_verts_eval[tri[1]]],
946 ps->screenCoords[ps->corner_verts_eval[tri[2]]],
947 };
948 float w[3];
949
950 if (do_clip) {
951 const float *vtri_co[3] = {
952 ps->vert_positions_eval[ps->corner_verts_eval[tri[0]]],
953 ps->vert_positions_eval[ps->corner_verts_eval[tri[1]]],
954 ps->vert_positions_eval[ps->corner_verts_eval[tri[2]]],
955 };
957 pixelScreenCo, UNPACK3(vtri_ss), UNPACK3(vtri_co), w, ps->is_ortho, ps->rv3d);
958 }
959 else {
960 isect_ret = project_paint_occlude_ptv(pixelScreenCo, UNPACK3(vtri_ss), w, ps->is_ortho);
961 }
962
963 if (isect_ret >= 1) {
964 /* TODO: we may want to cache the first hit,
965 * it is not possible to swap the face order in the list anymore */
966 return true;
967 }
968 }
969 }
970 return false;
971}
972
973/* Basic line intersection, could move to math_geom.c, 2 points with a horizontal line
974 * 1 for an intersection, 2 if the first point is aligned, 3 if the second point is aligned. */
975#define ISECT_TRUE 1
976#define ISECT_TRUE_P1 2
977#define ISECT_TRUE_P2 3
978static int line_isect_y(const float p1[2], const float p2[2], const float y_level, float *x_isect)
979{
980 float y_diff;
981
982 /* are we touching the first point? - no interpolation needed */
983 if (y_level == p1[1]) {
984 *x_isect = p1[0];
985 return ISECT_TRUE_P1;
986 }
987 /* are we touching the second point? - no interpolation needed */
988 if (y_level == p2[1]) {
989 *x_isect = p2[0];
990 return ISECT_TRUE_P2;
991 }
992
994 y_diff = fabsf(p1[1] - p2[1]);
995
996 if (y_diff < 0.000001f) {
997 *x_isect = (p1[0] + p2[0]) * 0.5f;
998 return ISECT_TRUE;
999 }
1000
1001 if (p1[1] > y_level && p2[1] < y_level) {
1002 /* `p1[1] - p2[1]`. */
1003 *x_isect = (p2[0] * (p1[1] - y_level) + p1[0] * (y_level - p2[1])) / y_diff;
1004 return ISECT_TRUE;
1005 }
1006 if (p1[1] < y_level && p2[1] > y_level) {
1007 /* `p2[1] - p1[1]`. */
1008 *x_isect = (p2[0] * (y_level - p1[1]) + p1[0] * (p2[1] - y_level)) / y_diff;
1009 return ISECT_TRUE;
1010 }
1011 return 0;
1012}
1013
1014static int line_isect_x(const float p1[2], const float p2[2], const float x_level, float *y_isect)
1015{
1016 float x_diff;
1017
1018 if (x_level == p1[0]) { /* are we touching the first point? - no interpolation needed */
1019 *y_isect = p1[1];
1020 return ISECT_TRUE_P1;
1021 }
1022 if (x_level == p2[0]) { /* are we touching the second point? - no interpolation needed */
1023 *y_isect = p2[1];
1024 return ISECT_TRUE_P2;
1025 }
1026
1027 /* yuck, horizontal line, we can't do much here */
1028 x_diff = fabsf(p1[0] - p2[0]);
1029
1030 /* yuck, vertical line, we can't do much here */
1031 if (x_diff < 0.000001f) {
1032 *y_isect = (p1[0] + p2[0]) * 0.5f;
1033 return ISECT_TRUE;
1034 }
1035
1036 if (p1[0] > x_level && p2[0] < x_level) {
1037 /* `p1[0] - p2[0]`. */
1038 *y_isect = (p2[1] * (p1[0] - x_level) + p1[1] * (x_level - p2[0])) / x_diff;
1039 return ISECT_TRUE;
1040 }
1041 if (p1[0] < x_level && p2[0] > x_level) {
1042 /* `p2[0] - p1[0]`. */
1043 *y_isect = (p2[1] * (x_level - p1[0]) + p1[1] * (p2[0] - x_level)) / x_diff;
1044 return ISECT_TRUE;
1045 }
1046 return 0;
1047}
1048
1049/* simple func use for comparing UV locations to check if there are seams.
1050 * Its possible this gives incorrect results, when the UVs for 1 face go into the next
1051 * tile, but do not do this for the adjacent face, it could return a false positive.
1052 * This is so unlikely that Id not worry about it. */
1053#ifndef PROJ_DEBUG_NOSEAMBLEED
1054static bool cmp_uv(const float vec2a[2], const float vec2b[2])
1055{
1056 /* if the UVs are not between 0.0 and 1.0 */
1057 float xa = fmodf(vec2a[0], 1.0f);
1058 float ya = fmodf(vec2a[1], 1.0f);
1059
1060 float xb = fmodf(vec2b[0], 1.0f);
1061 float yb = fmodf(vec2b[1], 1.0f);
1062
1063 if (xa < 0.0f) {
1064 xa += 1.0f;
1065 }
1066 if (ya < 0.0f) {
1067 ya += 1.0f;
1068 }
1069
1070 if (xb < 0.0f) {
1071 xb += 1.0f;
1072 }
1073 if (yb < 0.0f) {
1074 yb += 1.0f;
1075 }
1076
1077 return ((fabsf(xa - xb) < PROJ_GEOM_TOLERANCE) && (fabsf(ya - yb) < PROJ_GEOM_TOLERANCE)) ?
1078 true :
1079 false;
1080}
1081#endif
1082
1083/* set min_px and max_px to the image space bounds of the UV coords
1084 * return zero if there is no area in the returned rectangle */
1085#ifndef PROJ_DEBUG_NOSEAMBLEED
1086static bool pixel_bounds_uv(const float uv_quad[4][2],
1087 const int ibuf_x,
1088 const int ibuf_y,
1089 rcti *r_bounds_px)
1090{
1091 /* UV bounds */
1092 float min_uv[2], max_uv[2];
1093
1094 INIT_MINMAX2(min_uv, max_uv);
1095
1096 minmax_v2v2_v2(min_uv, max_uv, uv_quad[0]);
1097 minmax_v2v2_v2(min_uv, max_uv, uv_quad[1]);
1098 minmax_v2v2_v2(min_uv, max_uv, uv_quad[2]);
1099 minmax_v2v2_v2(min_uv, max_uv, uv_quad[3]);
1100
1101 r_bounds_px->xmin = int(ibuf_x * min_uv[0]);
1102 r_bounds_px->ymin = int(ibuf_y * min_uv[1]);
1103
1104 r_bounds_px->xmax = int(ibuf_x * max_uv[0]) + 1;
1105 r_bounds_px->ymax = int(ibuf_y * max_uv[1]) + 1;
1106
1107 // printf("%d %d %d %d\n", min_px[0], min_px[1], max_px[0], max_px[1]);
1108
1109 /* face uses no UV area when quantized to pixels? */
1110 return (r_bounds_px->xmin == r_bounds_px->xmax || r_bounds_px->ymin == r_bounds_px->ymax) ?
1111 false :
1112 true;
1113}
1114#endif
1115
1117 float (*uv)[2], const int ibuf_x, const int ibuf_y, int tot, rcti *r_bounds_px)
1118{
1119 /* UV bounds */
1120 float min_uv[2], max_uv[2];
1121
1122 if (tot == 0) {
1123 return false;
1124 }
1125
1126 INIT_MINMAX2(min_uv, max_uv);
1127
1128 while (tot--) {
1129 minmax_v2v2_v2(min_uv, max_uv, (*uv));
1130 uv++;
1131 }
1132
1133 r_bounds_px->xmin = int(ibuf_x * min_uv[0]);
1134 r_bounds_px->ymin = int(ibuf_y * min_uv[1]);
1135
1136 r_bounds_px->xmax = int(ibuf_x * max_uv[0]) + 1;
1137 r_bounds_px->ymax = int(ibuf_y * max_uv[1]) + 1;
1138
1139 // printf("%d %d %d %d\n", min_px[0], min_px[1], max_px[0], max_px[1]);
1140
1141 /* face uses no UV area when quantized to pixels? */
1142 return (r_bounds_px->xmin == r_bounds_px->xmax || r_bounds_px->ymin == r_bounds_px->ymax) ?
1143 false :
1144 true;
1145}
1146
1147#ifndef PROJ_DEBUG_NOSEAMBLEED
1148
1149static void project_face_winding_init(const ProjPaintState *ps, const int tri_index)
1150{
1151 /* detect the winding of faces in uv space */
1152 const int3 &tri = ps->corner_tris_eval[tri_index];
1153 const int face_i = ps->corner_tri_faces_eval[tri_index];
1154 float winding;
1155 if (ps->poly_to_loop_uv_single) {
1156 /* Could also just init this to 0 */
1157 winding = cross_tri_v2(ps->poly_to_loop_uv_single.value(),
1158 ps->poly_to_loop_uv_single.value(),
1159 ps->poly_to_loop_uv_single.value());
1160 }
1161 else {
1162 const float *tri_uv[3] = {PS_CORNER_TRI_AS_UV_3(ps->poly_to_loop_uv, face_i, tri)};
1163 winding = cross_tri_v2(tri_uv[0], tri_uv[1], tri_uv[2]);
1164 }
1165
1166 if (winding > 0) {
1167 ps->faceWindingFlags[tri_index] |= PROJ_FACE_WINDING_CW;
1168 }
1169
1170 ps->faceWindingFlags[tri_index] |= PROJ_FACE_WINDING_INIT;
1171}
1172
1173/* This function returns 1 if this face has a seam along the 2 face-vert indices
1174 * 'orig_i1_fidx' and 'orig_i2_fidx' */
1175static bool check_seam(const ProjPaintState *ps,
1176 const int orig_face,
1177 const int orig_i1_fidx,
1178 const int orig_i2_fidx,
1179 int *other_face,
1180 int *orig_fidx)
1181{
1182 const int3 &orig_tri = ps->corner_tris_eval[orig_face];
1183 const int orig_poly_i = ps->corner_tri_faces_eval[orig_face];
1184 const float *orig_tri_uv[3];
1185 if (!ps->poly_to_loop_uv_single) {
1186 PS_CORNER_TRI_ASSIGN_UV_3(orig_tri_uv, ps->poly_to_loop_uv, orig_poly_i, orig_tri);
1187 }
1188 /* vert indices from face vert order indices */
1189 const uint i1 = ps->corner_verts_eval[orig_tri[orig_i1_fidx]];
1190 const uint i2 = ps->corner_verts_eval[orig_tri[orig_i2_fidx]];
1191 LinkNode *node;
1192 /* index in face */
1193 int i1_fidx = -1, i2_fidx = -1;
1194
1195 for (node = ps->vertFaces[i1]; node; node = node->next) {
1196 const int tri_index = POINTER_AS_INT(node->link);
1197
1198 if (tri_index != orig_face) {
1199 const int3 &tri = ps->corner_tris_eval[tri_index];
1200 const int face_i = ps->corner_tri_faces_eval[tri_index];
1201 const int vert_tri[3] = {PS_CORNER_TRI_AS_VERT_INDEX_3(ps, tri)};
1202 /* could check if the 2 faces images match here,
1203 * but then there wouldn't be a way to return the opposite face's info */
1204
1205 /* We need to know the order of the verts in the adjacent face
1206 * set the i1_fidx and i2_fidx to (0,1,2,3) */
1207 i1_fidx = BKE_MESH_TESSTRI_VINDEX_ORDER(vert_tri, i1);
1208 i2_fidx = BKE_MESH_TESSTRI_VINDEX_ORDER(vert_tri, i2);
1209
1210 /* Only need to check if 'i2_fidx' is valid because
1211 * we know i1_fidx is the same vert on both faces. */
1212 if (i2_fidx != -1) {
1213 const float *tri_uv[3];
1214 if (!ps->poly_to_loop_uv_single) {
1215 PS_CORNER_TRI_ASSIGN_UV_3(tri_uv, ps->poly_to_loop_uv, face_i, tri);
1216 }
1217 Image *tpage = project_paint_face_paint_image(ps, tri_index);
1218 Image *orig_tpage = project_paint_face_paint_image(ps, orig_face);
1219 int tile;
1220 int orig_tile;
1221 if (ps->poly_to_loop_uv_single) {
1223 orig_tile = project_paint_face_paint_tile(orig_tpage,
1224 ps->poly_to_loop_uv_single.value());
1225 }
1226 else {
1227 tile = project_paint_face_paint_tile(tpage, tri_uv[0]);
1228 orig_tile = project_paint_face_paint_tile(orig_tpage, orig_tri_uv[0]);
1229 }
1230
1231 BLI_assert(i1_fidx != -1);
1232
1233 /* This IS an adjacent face!, now lets check if the UVs are ok */
1234
1235 /* set up the other face */
1236 *other_face = tri_index;
1237
1238 /* we check if difference is 1 here, else we might have a case of edge 2-0 for a tri */
1239 *orig_fidx = (i1_fidx < i2_fidx && (i2_fidx - i1_fidx == 1)) ? i1_fidx : i2_fidx;
1240
1241 /* initialize face winding if needed */
1242 if ((ps->faceWindingFlags[tri_index] & PROJ_FACE_WINDING_INIT) == 0) {
1243 project_face_winding_init(ps, tri_index);
1244 }
1245
1246 bool has_same_image;
1247 if (ps->poly_to_loop_uv_single) {
1248 has_same_image = (orig_tpage == tpage) && (orig_tile == tile);
1249 }
1250 else {
1251 has_same_image = (orig_tpage == tpage) && (orig_tile == tile) &&
1252 cmp_uv(orig_tri_uv[orig_i1_fidx], tri_uv[i1_fidx]) &&
1253 cmp_uv(orig_tri_uv[orig_i2_fidx], tri_uv[i2_fidx]);
1254 }
1255
1256 /* first test if they have the same image */
1257 if (has_same_image) {
1258 /* if faces don't have the same winding in uv space,
1259 * they are on the same side so edge is boundary */
1260 if ((ps->faceWindingFlags[tri_index] & PROJ_FACE_WINDING_CW) !=
1261 (ps->faceWindingFlags[orig_face] & PROJ_FACE_WINDING_CW))
1262 {
1263 return true;
1264 }
1265
1266 // printf("SEAM (NONE)\n");
1267 return false;
1268 }
1269 // printf("SEAM (UV GAP)\n");
1270 return true;
1271 }
1272 }
1273 }
1274 // printf("SEAM (NO FACE)\n");
1275 *other_face = -1;
1276 return true;
1277}
1278
1280 uint loop_index,
1281 uint vert_index,
1282 VertSeam **r_seam)
1283{
1284 ListBaseT<VertSeam> *vert_seams = &ps->vertSeams[vert_index];
1285 VertSeam *seam = vert_seams->first();
1286 VertSeam *adjacent = nullptr;
1287
1288 while (seam->loop != loop_index) {
1289 seam = seam->next;
1290 }
1291
1292 if (r_seam) {
1293 *r_seam = seam;
1294 }
1295
1296 /* Circulate through the (sorted) vert seam array, in the direction of the seam normal,
1297 * until we find the first opposing seam, matching in UV space. */
1298 if (seam->normal_cw) {
1299 LISTBASE_CIRCULAR_BACKWARD_BEGIN (VertSeam *, vert_seams, adjacent, seam) {
1300 if ((adjacent->normal_cw != seam->normal_cw) && cmp_uv(adjacent->uv, seam->uv)) {
1301 break;
1302 }
1303 }
1304 LISTBASE_CIRCULAR_BACKWARD_END(VertSeam *, vert_seams, adjacent, seam);
1305 }
1306 else {
1307 LISTBASE_CIRCULAR_FORWARD_BEGIN (VertSeam *, vert_seams, adjacent, seam) {
1308 if ((adjacent->normal_cw != seam->normal_cw) && cmp_uv(adjacent->uv, seam->uv)) {
1309 break;
1310 }
1311 }
1312 LISTBASE_CIRCULAR_FORWARD_END(VertSeam *, vert_seams, adjacent, seam);
1313 }
1314
1315 BLI_assert(adjacent);
1316
1317 return adjacent;
1318}
1319
1320/* Computes the normal of two seams at their intersection,
1321 * and returns the angle between the seam and its normal. */
1322static float compute_seam_normal(VertSeam *seam, VertSeam *adj, float r_no[2])
1323{
1324 const float PI_2 = M_PI * 2.0f;
1325 float angle[2];
1326 float angle_rel, angle_no;
1327
1328 if (seam->normal_cw) {
1329 angle[0] = adj->angle;
1330 angle[1] = seam->angle;
1331 }
1332 else {
1333 angle[0] = seam->angle;
1334 angle[1] = adj->angle;
1335 }
1336
1337 angle_rel = angle[1] - angle[0];
1338
1339 if (angle_rel < 0.0f) {
1340 angle_rel += PI_2;
1341 }
1342
1343 angle_rel *= 0.5f;
1344
1345 angle_no = angle_rel + angle[0];
1346
1347 if (angle_no > M_PI) {
1348 angle_no -= PI_2;
1349 }
1350
1351 r_no[0] = cosf(angle_no);
1352 r_no[1] = sinf(angle_no);
1353
1354 return angle_rel;
1355}
1356
1357/* Calculate outset UVs, this is not the same as simply scaling the UVs,
1358 * since the outset coords are a margin that keep an even distance from the original UVs,
1359 * note that the image aspect is taken into account */
1360static void uv_image_outset(const ProjPaintState *ps,
1361 float (*orig_uv)[2],
1362 float (*puv)[2],
1363 uint tri_index,
1364 const int ibuf_x,
1365 const int ibuf_y)
1366{
1367 int fidx[2];
1368 uint loop_index;
1369 uint vert[2];
1370 const int3 &tri = ps->corner_tris_eval[tri_index];
1371
1372 float ibuf_inv[2];
1373
1374 ibuf_inv[0] = 1.0f / float(ibuf_x);
1375 ibuf_inv[1] = 1.0f / float(ibuf_y);
1376
1377 for (fidx[0] = 0; fidx[0] < 3; fidx[0]++) {
1378 LoopSeamData *seam_data;
1379 float (*seam_uvs)[2];
1380 float ang[2];
1381
1382 if ((ps->faceSeamFlags[tri_index] & (PROJ_FACE_SEAM0 << fidx[0])) == 0) {
1383 continue;
1384 }
1385
1386 loop_index = tri[fidx[0]];
1387
1388 seam_data = &ps->loopSeamData[loop_index];
1389 seam_uvs = seam_data->seam_uvs;
1390
1391 if (seam_uvs[0][0] != FLT_MAX) {
1392 continue;
1393 }
1394
1395 fidx[1] = (fidx[0] == 2) ? 0 : fidx[0] + 1;
1396
1397 vert[0] = ps->corner_verts_eval[loop_index];
1398 vert[1] = ps->corner_verts_eval[tri[fidx[1]]];
1399
1400 for (uint i = 0; i < 2; i++) {
1401 VertSeam *seam;
1402 VertSeam *adj = find_adjacent_seam(ps, loop_index, vert[i], &seam);
1403 float no[2];
1404 float len_fact;
1405 float tri_ang;
1406
1407 ang[i] = compute_seam_normal(seam, adj, no);
1408 tri_ang = ang[i] - M_PI_2;
1409
1410 if (tri_ang > 0.0f) {
1411 const float dist = ps->seam_bleed_px * tanf(tri_ang);
1412 seam_data->corner_dist_sq[i] = square_f(dist);
1413 }
1414 else {
1415 seam_data->corner_dist_sq[i] = 0.0f;
1416 }
1417
1418 len_fact = cosf(tri_ang);
1419 len_fact = UNLIKELY(len_fact < FLT_EPSILON) ? FLT_MAX : (1.0f / len_fact);
1420
1421 /* Clamp the length factor, see: #62236. */
1422 len_fact = std::min(len_fact, 10.0f);
1423
1424 mul_v2_fl(no, ps->seam_bleed_px * len_fact);
1425
1426 add_v2_v2v2(seam_data->seam_puvs[i], puv[fidx[i]], no);
1427
1428 mul_v2_v2v2(seam_uvs[i], seam_data->seam_puvs[i], ibuf_inv);
1429 }
1430
1431 /* Handle convergent normals (can self-intersect). */
1432 if ((ang[0] + ang[1]) < M_PI) {
1433 if (isect_seg_seg_v2_simple(orig_uv[fidx[0]], seam_uvs[0], orig_uv[fidx[1]], seam_uvs[1])) {
1434 float isect_co[2];
1435
1437 orig_uv[fidx[0]], seam_uvs[0], orig_uv[fidx[1]], seam_uvs[1], isect_co);
1438
1439 copy_v2_v2(seam_uvs[0], isect_co);
1440 copy_v2_v2(seam_uvs[1], isect_co);
1441 }
1442 }
1443 }
1444}
1445
1447 MemArena *arena,
1448 const int tri_index,
1449 const int fidx1,
1450 const int ibuf_x,
1451 const int ibuf_y)
1452{
1453 const int3 &tri = ps->corner_tris_eval[tri_index];
1454 const int face_i = ps->corner_tri_faces_eval[tri_index];
1455 const float *tri_uv[3];
1456 if (!ps->poly_to_loop_uv_single) {
1457 PS_CORNER_TRI_ASSIGN_UV_3(tri_uv, ps->poly_to_loop_uv, face_i, tri);
1458 }
1459 const int fidx[2] = {fidx1, ((fidx1 + 1) % 3)};
1460 float vec[2];
1461
1462 VertSeam *vseam = static_cast<VertSeam *>(BLI_memarena_alloc(arena, sizeof(VertSeam[2])));
1463
1464 vseam->prev = nullptr;
1465 vseam->next = nullptr;
1466
1467 vseam->tri = tri_index;
1468 vseam->loop = tri[fidx[0]];
1469
1470 if (ps->poly_to_loop_uv_single) {
1471 copy_v2_v2(vec, ps->poly_to_loop_uv_single.value());
1472 }
1473 else {
1474 sub_v2_v2v2(vec, tri_uv[fidx[1]], tri_uv[fidx[0]]);
1475 }
1476
1477 vec[0] *= ibuf_x;
1478 vec[1] *= ibuf_y;
1479 vseam->angle = atan2f(vec[1], vec[0]);
1480
1481 /* If the face winding data is not initialized, something must be wrong. */
1482 BLI_assert((ps->faceWindingFlags[tri_index] & PROJ_FACE_WINDING_INIT) != 0);
1483 vseam->normal_cw = (ps->faceWindingFlags[tri_index] & PROJ_FACE_WINDING_CW);
1484
1485 if (ps->poly_to_loop_uv_single) {
1486 copy_v2_v2(vseam->uv, ps->poly_to_loop_uv_single.value());
1487 }
1488 else {
1489 copy_v2_v2(vseam->uv, tri_uv[fidx[0]]);
1490 }
1491
1492 vseam[1] = vseam[0];
1493 vseam[1].angle += vseam[1].angle > 0.0f ? -M_PI : M_PI;
1494 vseam[1].normal_cw = !vseam[1].normal_cw;
1495 if (ps->poly_to_loop_uv_single) {
1496 copy_v2_v2(vseam->uv, ps->poly_to_loop_uv_single.value());
1497 }
1498 else {
1499 copy_v2_v2(vseam[1].uv, tri_uv[fidx[1]]);
1500 }
1501
1502 for (uint i = 0; i < 2; i++) {
1503 const int vert = ps->corner_verts_eval[tri[fidx[i]]];
1504 ListBaseT<VertSeam> *list = &ps->vertSeams[vert];
1505 VertSeam *item = list->first();
1506
1507 while (item && item->angle < vseam[i].angle) {
1508 item = item->next;
1509 }
1510
1511 BLI_insertlinkbefore(list, item, &vseam[i]);
1512 }
1513}
1514
1522 MemArena *arena,
1523 const int tri_index,
1524 const uint vert_index,
1525 bool init_all,
1526 const int ibuf_x,
1527 const int ibuf_y)
1528{
1529 /* vars for the other face, we also set its flag */
1530 int other_face, other_fidx;
1531 /* next fidx in the face (0,1,2,3) -> (1,2,3,0) or (0,1,2) -> (1,2,0) for a tri */
1532 int fidx[2] = {2, 0};
1533 const int3 &tri = ps->corner_tris_eval[tri_index];
1534 LinkNode *node;
1535
1536 /* initialize face winding if needed */
1537 if ((ps->faceWindingFlags[tri_index] & PROJ_FACE_WINDING_INIT) == 0) {
1538 project_face_winding_init(ps, tri_index);
1539 }
1540
1541 do {
1542 if (init_all || (ps->corner_verts_eval[tri[fidx[0]]] == vert_index) ||
1543 (ps->corner_verts_eval[tri[fidx[1]]] == vert_index))
1544 {
1545 if ((ps->faceSeamFlags[tri_index] &
1546 (PROJ_FACE_SEAM0 << fidx[0] | PROJ_FACE_NOSEAM0 << fidx[0])) == 0)
1547 {
1548 if (check_seam(ps, tri_index, fidx[0], fidx[1], &other_face, &other_fidx)) {
1549 ps->faceSeamFlags[tri_index] |= PROJ_FACE_SEAM0 << fidx[0];
1550 insert_seam_vert_array(ps, arena, tri_index, fidx[0], ibuf_x, ibuf_y);
1551
1552 if (other_face != -1) {
1553 /* Check if the other seam is already set.
1554 * We don't want to insert it in the list twice. */
1555 if ((ps->faceSeamFlags[other_face] & (PROJ_FACE_SEAM0 << other_fidx)) == 0) {
1556 ps->faceSeamFlags[other_face] |= PROJ_FACE_SEAM0 << other_fidx;
1557 insert_seam_vert_array(ps, arena, other_face, other_fidx, ibuf_x, ibuf_y);
1558 }
1559 }
1560 }
1561 else {
1562 ps->faceSeamFlags[tri_index] |= PROJ_FACE_NOSEAM0 << fidx[0];
1563 ps->faceSeamFlags[tri_index] |= PROJ_FACE_SEAM_INIT0 << fidx[0];
1564
1565 if (other_face != -1) {
1566 /* second 4 bits for disabled */
1567 ps->faceSeamFlags[other_face] |= PROJ_FACE_NOSEAM0 << other_fidx;
1568 ps->faceSeamFlags[other_face] |= PROJ_FACE_SEAM_INIT0 << other_fidx;
1569 }
1570 }
1571 }
1572 }
1573
1574 fidx[1] = fidx[0];
1575 } while (fidx[0]--);
1576
1577 if (init_all) {
1578 char checked_verts = 0;
1579
1580 fidx[0] = 2;
1581 fidx[1] = 0;
1582
1583 do {
1584 if ((ps->faceSeamFlags[tri_index] & (PROJ_FACE_SEAM_INIT0 << fidx[0])) == 0) {
1585 for (uint i = 0; i < 2; i++) {
1586 uint vert;
1587
1588 if ((checked_verts & (1 << fidx[i])) != 0) {
1589 continue;
1590 }
1591
1592 vert = ps->corner_verts_eval[tri[fidx[i]]];
1593
1594 for (node = ps->vertFaces[vert]; node; node = node->next) {
1595 const int tri = POINTER_AS_INT(node->link);
1596
1597 project_face_seams_init(ps, arena, tri, vert, false, ibuf_x, ibuf_y);
1598 }
1599
1600 checked_verts |= 1 << fidx[i];
1601 }
1602
1603 ps->faceSeamFlags[tri_index] |= PROJ_FACE_SEAM_INIT0 << fidx[0];
1604 }
1605
1606 fidx[1] = fidx[0];
1607 } while (fidx[0]--);
1608 }
1609}
1610#endif // PROJ_DEBUG_NOSEAMBLEED
1611
1612/* Converts a UV location to a 3D screen-space location
1613 * Takes a 'uv' and 3 UV coords, and sets the values of pixelScreenCo
1614 *
1615 * This is used for finding a pixels location in screen-space for painting */
1616static void screen_px_from_ortho(const float uv[2],
1617 const float v1co[3],
1618 const float v2co[3],
1619 const float v3co[3], /* Screen-space coords */
1620 const float uv1co[2],
1621 const float uv2co[2],
1622 const float uv3co[2],
1623 float pixelScreenCo[4],
1624 float w[3])
1625{
1626 barycentric_weights_v2(uv1co, uv2co, uv3co, uv, w);
1627 interp_v3_v3v3v3(pixelScreenCo, v1co, v2co, v3co, w);
1628}
1629
1630/* same as screen_px_from_ortho except we
1631 * do perspective correction on the pixel coordinate */
1632static void screen_px_from_persp(const float uv[2],
1633 const float v1co[4],
1634 const float v2co[4],
1635 const float v3co[4], /* screen-space coords */
1636 const float uv1co[2],
1637 const float uv2co[2],
1638 const float uv3co[2],
1639 float pixelScreenCo[4],
1640 float w[3])
1641{
1642 float w_int[3];
1643 float wtot_inv, wtot;
1644 barycentric_weights_v2(uv1co, uv2co, uv3co, uv, w);
1645
1646 /* re-weight from the 4th coord of each screen vert */
1647 w_int[0] = w[0] * v1co[3];
1648 w_int[1] = w[1] * v2co[3];
1649 w_int[2] = w[2] * v3co[3];
1650
1651 wtot = w_int[0] + w_int[1] + w_int[2];
1652
1653 if (wtot > 0.0f) {
1654 wtot_inv = 1.0f / wtot;
1655 w_int[0] *= wtot_inv;
1656 w_int[1] *= wtot_inv;
1657 w_int[2] *= wtot_inv;
1658 }
1659 else {
1660 /* Dummy values for zero area face. */
1661 w[0] = w[1] = w[2] = w_int[0] = w_int[1] = w_int[2] = 1.0f / 3.0f;
1662 }
1663 /* done re-weighting */
1664
1665 /* do interpolation based on projected weight */
1666 interp_v3_v3v3v3(pixelScreenCo, v1co, v2co, v3co, w_int);
1667}
1668
1677static void screen_px_to_vector_persp(int winx,
1678 int winy,
1679 const float projmat_inv[4][4],
1680 const float view_pos[3],
1681 const float co_px[2],
1682 float r_dir[3])
1683{
1684 r_dir[0] = 2.0f * (co_px[0] / winx) - 1.0f;
1685 r_dir[1] = 2.0f * (co_px[1] / winy) - 1.0f;
1686 r_dir[2] = -0.5f;
1687 mul_project_m4_v3(const_cast<float (*)[4]>(projmat_inv), r_dir);
1688 sub_v3_v3(r_dir, view_pos);
1689}
1690
1700 const float p[2],
1701 const float v1[3],
1702 const float v2[3])
1703{
1704 const float zero[3] = {0};
1705 float v1_proj[3], v2_proj[3];
1706 float dir[3];
1707
1708 screen_px_to_vector_persp(ps->winx, ps->winy, ps->projectMatInv, ps->viewPos, p, dir);
1709
1710 sub_v3_v3v3(v1_proj, v1, ps->viewPos);
1711 sub_v3_v3v3(v2_proj, v2, ps->viewPos);
1712
1713 project_plane_v3_v3v3(v1_proj, v1_proj, dir);
1714 project_plane_v3_v3v3(v2_proj, v2_proj, dir);
1715
1716 return line_point_factor_v2(zero, v1_proj, v2_proj);
1717}
1718
1720 const float *tri_uv[3], ImBuf *ibuf_other, const float w[3], uchar rgba_ub[4], float rgba_f[4])
1721{
1722 float uv_other[2];
1723
1724 interp_v2_v2v2v2(uv_other, UNPACK3(tri_uv), w);
1725
1726 float x = uv_other[0] * ibuf_other->x - 0.5f;
1727 float y = uv_other[1] * ibuf_other->y - 0.5f;
1728
1729 if (ibuf_other->float_data()) {
1730 float4 col = imbuf::interpolate_bilinear_wrap_fl(ibuf_other, x, y);
1731 col = math::clamp(col, 0.0f, 1.0f);
1732 memcpy(rgba_f, &col, sizeof(col));
1733 }
1734 else {
1735 uchar4 col = imbuf::interpolate_bilinear_wrap_byte(ibuf_other, x, y);
1736 memcpy(rgba_ub, &col, sizeof(col));
1737 }
1738}
1739
1740/* run this outside project_paint_uvpixel_init since pixels with mask 0 don't need init */
1742 const int tri_index,
1743 const float w[3])
1744{
1745 float mask;
1746
1747 /* Image Mask */
1748 if (ps->do_layer_stencil) {
1749 /* another UV maps image is masking this one's */
1750 ImBuf *ibuf_other;
1751 Image *other_tpage = ps->stencil_ima;
1752
1753 if (other_tpage && (ibuf_other = BKE_image_acquire_ibuf(other_tpage, nullptr, nullptr))) {
1754 const int3 &tri_other = ps->corner_tris_eval[tri_index];
1755 const float *other_tri_uv[3] = {ps->uv_map_stencil_eval[tri_other[0]],
1756 ps->uv_map_stencil_eval[tri_other[1]],
1757 ps->uv_map_stencil_eval[tri_other[2]]};
1758
1759 /* #BKE_image_acquire_ibuf - TODO: this may be slow. */
1760 uchar rgba_ub[4];
1761 float rgba_f[4];
1762
1763 project_face_pixel(other_tri_uv, ibuf_other, w, rgba_ub, rgba_f);
1764
1765 if (ibuf_other->float_data()) { /* from float to float */
1766 mask = ((rgba_f[0] + rgba_f[1] + rgba_f[2]) * (1.0f / 3.0f)) * rgba_f[3];
1767 }
1768 else { /* from char to float */
1769 mask = ((rgba_ub[0] + rgba_ub[1] + rgba_ub[2]) * (1.0f / (255.0f * 3.0f))) *
1770 (rgba_ub[3] * (1.0f / 255.0f));
1771 }
1772
1773 BKE_image_release_ibuf(other_tpage, ibuf_other, nullptr);
1774
1775 if (!ps->do_layer_stencil_inv) {
1776 /* matching the gimps layer mask black/white rules, white==full opacity */
1777 mask = (1.0f - mask);
1778 }
1779
1780 if (mask == 0.0f) {
1781 return 0.0f;
1782 }
1783 }
1784 else {
1785 return 0.0f;
1786 }
1787 }
1788 else {
1789 mask = 1.0f;
1790 }
1791
1792 if (ps->do_mask_cavity) {
1793 const int3 &tri = ps->corner_tris_eval[tri_index];
1794 const int vert_tri[3] = {PS_CORNER_TRI_AS_VERT_INDEX_3(ps, tri)};
1795 float ca1, ca2, ca3, ca_mask;
1796 ca1 = ps->cavities[vert_tri[0]];
1797 ca2 = ps->cavities[vert_tri[1]];
1798 ca3 = ps->cavities[vert_tri[2]];
1799
1800 ca_mask = w[0] * ca1 + w[1] * ca2 + w[2] * ca3;
1801 ca_mask = BKE_curvemapping_evaluateF(ps->cavity_curve, 0, ca_mask);
1802 CLAMP(ca_mask, 0.0f, 1.0f);
1803 mask *= ca_mask;
1804 }
1805
1806 /* calculate mask */
1807 if (ps->do_mask_normal) {
1808 const int3 &tri = ps->corner_tris_eval[tri_index];
1809 const int vert_tri[3] = {PS_CORNER_TRI_AS_VERT_INDEX_3(ps, tri)};
1810 float no[3], angle_cos;
1811
1812 {
1813 const float *no1, *no2, *no3;
1814 no1 = ps->corner_normals_eval[tri[0]];
1815 no2 = ps->corner_normals_eval[tri[1]];
1816 no3 = ps->corner_normals_eval[tri[2]];
1817
1818 no[0] = w[0] * no1[0] + w[1] * no2[0] + w[2] * no3[0];
1819 no[1] = w[0] * no1[1] + w[1] * no2[1] + w[2] * no3[1];
1820 no[2] = w[0] * no1[2] + w[1] * no2[2] + w[2] * no3[2];
1821 normalize_v3(no);
1822 }
1823
1824 if (ps->is_flip_object) [[unlikely]] {
1825 negate_v3(no);
1826 }
1827
1828 /* now we can use the normal as a mask */
1829 if (ps->is_ortho) {
1830 angle_cos = dot_v3v3(ps->viewDir, no);
1831 }
1832 else {
1833 /* Annoying but for the perspective view we need to get the pixels location in 3D space :/ */
1834 float viewDirPersp[3];
1835 const float *co1, *co2, *co3;
1836 co1 = ps->vert_positions_eval[vert_tri[0]];
1837 co2 = ps->vert_positions_eval[vert_tri[1]];
1838 co3 = ps->vert_positions_eval[vert_tri[2]];
1839
1840 /* Get the direction from the viewPoint to the pixel and normalize */
1841 viewDirPersp[0] = (ps->viewPos[0] - (w[0] * co1[0] + w[1] * co2[0] + w[2] * co3[0]));
1842 viewDirPersp[1] = (ps->viewPos[1] - (w[0] * co1[1] + w[1] * co2[1] + w[2] * co3[1]));
1843 viewDirPersp[2] = (ps->viewPos[2] - (w[0] * co1[2] + w[1] * co2[2] + w[2] * co3[2]));
1844 normalize_v3(viewDirPersp);
1845 if (ps->is_flip_object) [[unlikely]] {
1846 negate_v3(viewDirPersp);
1847 }
1848
1849 angle_cos = dot_v3v3(viewDirPersp, no);
1850 }
1851
1852 /* If back-face culling is disabled, allow painting on back faces. */
1853 if (!ps->do_backfacecull) {
1854 angle_cos = fabsf(angle_cos);
1855 }
1856
1857 if (angle_cos <= ps->normal_angle__cos) {
1858 /* Outsize the normal limit. */
1859 return 0.0f;
1860 }
1861 if (angle_cos < ps->normal_angle_inner__cos) {
1862 mask *= (ps->normal_angle - acosf(angle_cos)) / ps->normal_angle_range;
1863 } /* otherwise no mask normal is needed, we're within the limit */
1864 }
1865
1866 /* This only works when the opacity doesn't change while painting, stylus pressure messes with
1867 * this so don't use it. */
1868 // if (ps->is_airbrush == 0) mask *= BKE_brush_alpha_get(ps->brush);
1869
1870 return mask;
1871}
1872
1873static int project_paint_pixel_sizeof(const short brush_type)
1874{
1876 return sizeof(ProjPixelClone);
1877 }
1878 return sizeof(ProjPixel);
1879}
1880
1881static int project_paint_undo_subtiles(const TileInfo *tinf, int tx, int ty)
1882{
1883 ProjPaintImage *pjIma = tinf->pjima;
1884 int tile_index = tx + ty * tinf->tile_width;
1885 bool generate_tile = false;
1886
1887 /* double check lock to avoid locking */
1888 if (!pjIma->undoRect[tile_index]) [[unlikely]] {
1889 if (tinf->lock) {
1890 BLI_spin_lock(tinf->lock);
1891 }
1892 if (!pjIma->undoRect[tile_index]) [[likely]] {
1894 generate_tile = true;
1895 }
1896 if (tinf->lock) {
1897 BLI_spin_unlock(tinf->lock);
1898 }
1899 }
1900
1901 if (generate_tile) {
1903 volatile const void *undorect = nullptr;
1904 if (tinf->masked) {
1905 if (const ImBuf *ibuf = ED_image_paint_tile_push(undo_tiles,
1906 pjIma->ima,
1907 pjIma->ibuf,
1908 &pjIma->iuser,
1909 tx,
1910 ty,
1911 &pjIma->maskRect[tile_index],
1912 &pjIma->valid[tile_index]))
1913 {
1914 if (ibuf->float_data()) {
1915 undorect = ibuf->float_data();
1916 }
1917 else {
1918 undorect = ibuf->byte_data();
1919 }
1920 }
1921 }
1922 else {
1923 if (const ImBuf *ibuf = ED_image_paint_tile_push(undo_tiles,
1924 pjIma->ima,
1925 pjIma->ibuf,
1926 &pjIma->iuser,
1927 tx,
1928 ty,
1929 nullptr,
1930 &pjIma->valid[tile_index]))
1931 {
1932 if (ibuf->float_data()) {
1933 undorect = ibuf->float_data();
1934 }
1935 else {
1936 undorect = ibuf->byte_data();
1937 }
1938 }
1939 }
1940
1941 IMB_mark_dirty(pjIma->ibuf);
1942 /* tile ready, publish */
1943 if (tinf->lock) {
1944 BLI_spin_lock(tinf->lock);
1945 }
1946 pjIma->undoRect[tile_index] = undorect;
1947 if (tinf->lock) {
1948 BLI_spin_unlock(tinf->lock);
1949 }
1950 }
1951
1952 return tile_index;
1953}
1954
1955/* run this function when we know a bucket's, face's pixel can be initialized,
1956 * return the ProjPixel which is added to 'ps->bucketRect[bucket_index]' */
1958 MemArena *arena,
1959 const TileInfo *tinf,
1960 int x_px,
1961 int y_px,
1962 const float mask,
1963 const int tri_index,
1964 const float pixelScreenCo[4],
1965 const float world_spaceCo[3],
1966 const float w[3])
1967{
1968 ProjPixel *projPixel;
1969 int x_tile, y_tile;
1970 int x_round, y_round;
1971 int tile_offset;
1972 /* Volatile is important here to ensure pending check is not optimized away by compiler. */
1973 volatile int tile_index;
1974
1975 ProjPaintImage *projima = tinf->pjima;
1976 ImBuf *ibuf = projima->ibuf;
1977 /* wrap pixel location */
1978
1979 x_px = mod_i(x_px, ibuf->x);
1980 y_px = mod_i(y_px, ibuf->y);
1981
1983 projPixel = static_cast<ProjPixel *>(BLI_memarena_alloc(arena, ps->pixel_sizeof));
1984
1985 /* calculate the undo tile offset of the pixel, used to store the original
1986 * pixel color and accumulated mask if any */
1987 x_tile = x_px >> ED_IMAGE_UNDO_TILE_BITS;
1988 y_tile = y_px >> ED_IMAGE_UNDO_TILE_BITS;
1989
1990 x_round = x_tile * ED_IMAGE_UNDO_TILE_SIZE;
1991 y_round = y_tile * ED_IMAGE_UNDO_TILE_SIZE;
1992 // memset(projPixel, 0, size);
1993
1994 tile_offset = (x_px - x_round) + (y_px - y_round) * ED_IMAGE_UNDO_TILE_SIZE;
1995 tile_index = project_paint_undo_subtiles(tinf, x_tile, y_tile);
1996
1997 /* other thread may be initializing the tile so wait here */
1998 while (projima->undoRect[tile_index] == TILE_PENDING) {
1999 /* pass */
2000 }
2001
2005
2006 projPixel->valid = projima->valid[tile_index];
2007 projPixel->pixel_offset = (x_px + y_px * ibuf->x) * 4;
2008
2009 if (ibuf->float_data()) {
2010 projPixel->origColor.f_pt = static_cast<float *>(
2011 const_cast<void *>(projima->undoRect[tile_index])) +
2012 4 * tile_offset;
2013 zero_v4(projPixel->newColor.f);
2014 }
2015 else {
2016 projPixel->origColor.uint_pt = static_cast<uint *>(
2017 const_cast<void *>(projima->undoRect[tile_index])) +
2018 tile_offset;
2019 projPixel->newColor.uint_ = 0;
2020 }
2021
2022 /* Screen-space unclamped, we could keep its z and w values but don't need them at the moment. */
2024 copy_v3_v3(projPixel->worldCoSS, world_spaceCo);
2025 }
2026
2027 copy_v2_v2(projPixel->projCoSS, pixelScreenCo);
2028
2029 projPixel->x_px = x_px;
2030 projPixel->y_px = y_px;
2031
2032 projPixel->mask = ushort(mask * 65535);
2033 if (ps->do_masking) {
2034 projPixel->mask_accum = projima->maskRect[tile_index] + tile_offset;
2035 }
2036 else {
2037 projPixel->mask_accum = nullptr;
2038 }
2039
2040 /* which bounding box cell are we in?, needed for undo */
2041 projPixel->bb_cell_index = int((float(x_px) / float(ibuf->x)) * PROJ_BOUNDBOX_DIV) +
2042 int((float(y_px) / float(ibuf->y)) * PROJ_BOUNDBOX_DIV) *
2044
2045 /* done with view3d_project_float inline */
2047 if (ps->poly_to_loop_uv_clone) {
2048 ImBuf *ibuf_other;
2049 Image *other_tpage = project_paint_face_clone_image(ps, tri_index);
2050
2051 if (other_tpage && (ibuf_other = BKE_image_acquire_ibuf(other_tpage, nullptr, nullptr))) {
2052 const int3 &tri_other = ps->corner_tris_eval[tri_index];
2053 const int poly_other_i = ps->corner_tri_faces_eval[tri_index];
2054 const float *other_tri_uv[3] = {
2055 PS_CORNER_TRI_AS_UV_3(ps->poly_to_loop_uv_clone, poly_other_i, tri_other)};
2056
2057 /* #BKE_image_acquire_ibuf - TODO: this may be slow. */
2058
2059 if (ibuf->float_data()) {
2060 if (ibuf_other->float_data()) { /* from float to float */
2061 project_face_pixel(other_tri_uv,
2062 ibuf_other,
2063 w,
2064 nullptr,
2065 (reinterpret_cast<ProjPixelClone *>(projPixel))->clonepx.f);
2066 }
2067 else { /* from char to float */
2068 uchar rgba_ub[4];
2069 float rgba[4];
2070 project_face_pixel(other_tri_uv, ibuf_other, w, rgba_ub, nullptr);
2071 rgba_uchar_to_float(rgba, rgba_ub);
2073 ibuf_other->byte_buffer.colorspace);
2074 straight_to_premul_v4_v4((reinterpret_cast<ProjPixelClone *>(projPixel))->clonepx.f,
2075 rgba);
2076 }
2077 }
2078 else {
2079 if (ibuf_other->float_data()) { /* float to char */
2080 float rgba[4];
2081 project_face_pixel(other_tri_uv, ibuf_other, w, nullptr, rgba);
2084 rgba_float_to_uchar((reinterpret_cast<ProjPixelClone *>(projPixel))->clonepx.ch, rgba);
2085 }
2086 else { /* char to char */
2087 project_face_pixel(other_tri_uv,
2088 ibuf_other,
2089 w,
2090 (reinterpret_cast<ProjPixelClone *>(projPixel))->clonepx.ch,
2091 nullptr);
2092 }
2093 }
2094
2095 BKE_image_release_ibuf(other_tpage, ibuf_other, nullptr);
2096 }
2097 else {
2098 if (ibuf->float_data()) {
2099 (reinterpret_cast<ProjPixelClone *>(projPixel))->clonepx.f[3] = 0;
2100 }
2101 else {
2102 (reinterpret_cast<ProjPixelClone *>(projPixel))->clonepx.ch[3] = 0;
2103 }
2104 }
2105 }
2106 else {
2107 float co[2];
2108 sub_v2_v2v2(co, projPixel->projCoSS, ps->cloneOffset);
2109
2110 /* no need to initialize the bucket, we're only checking buckets faces and for this
2111 * the faces are already initialized in project_paint_delayed_face_init(...) */
2112 if (ibuf->float_data()) {
2114 ps, co, (reinterpret_cast<ProjPixelClone *>(projPixel))->clonepx.f, nullptr, true))
2115 {
2116 /* zero alpha - ignore */
2117 (reinterpret_cast<ProjPixelClone *>(projPixel))->clonepx.f[3] = 0;
2118 }
2119 }
2120 else {
2122 co,
2123 nullptr,
2124 (reinterpret_cast<ProjPixelClone *>(projPixel))->clonepx.ch,
2125 true))
2126 {
2127 /* zero alpha - ignore */
2128 (reinterpret_cast<ProjPixelClone *>(projPixel))->clonepx.ch[3] = 0;
2129 }
2130 }
2131 }
2132 }
2133
2134 /* pointer arithmetic */
2135 projPixel->image_index = projima - ps->projImages;
2136
2137 return projPixel;
2138}
2139
2140static bool line_clip_rect2f(const rctf *cliprect,
2141 const rctf *rect,
2142 const float l1[2],
2143 const float l2[2],
2144 float l1_clip[2],
2145 float l2_clip[2])
2146{
2147 /* first account for horizontal, then vertical lines */
2148 /* Horizontal. */
2149 if (fabsf(l1[1] - l2[1]) < PROJ_PIXEL_TOLERANCE) {
2150 /* is the line out of range on its Y axis? */
2151 if (l1[1] < rect->ymin || l1[1] > rect->ymax) {
2152 return false;
2153 }
2154 /* line is out of range on its X axis */
2155 if ((l1[0] < rect->xmin && l2[0] < rect->xmin) || (l1[0] > rect->xmax && l2[0] > rect->xmax)) {
2156 return false;
2157 }
2158
2159 /* This is a single point (or close to). */
2160 if (fabsf(l1[0] - l2[0]) < PROJ_PIXEL_TOLERANCE) {
2161 if (BLI_rctf_isect_pt_v(rect, l1)) {
2162 copy_v2_v2(l1_clip, l1);
2163 copy_v2_v2(l2_clip, l2);
2164 return true;
2165 }
2166 return false;
2167 }
2168
2169 copy_v2_v2(l1_clip, l1);
2170 copy_v2_v2(l2_clip, l2);
2171 CLAMP(l1_clip[0], rect->xmin, rect->xmax);
2172 CLAMP(l2_clip[0], rect->xmin, rect->xmax);
2173 return true;
2174 }
2175 if (fabsf(l1[0] - l2[0]) < PROJ_PIXEL_TOLERANCE) {
2176 /* is the line out of range on its X axis? */
2177 if (l1[0] < rect->xmin || l1[0] > rect->xmax) {
2178 return false;
2179 }
2180
2181 /* line is out of range on its Y axis */
2182 if ((l1[1] < rect->ymin && l2[1] < rect->ymin) || (l1[1] > rect->ymax && l2[1] > rect->ymax)) {
2183 return false;
2184 }
2185
2186 /* This is a single point (or close to). */
2187 if (fabsf(l1[1] - l2[1]) < PROJ_PIXEL_TOLERANCE) {
2188 if (BLI_rctf_isect_pt_v(rect, l1)) {
2189 copy_v2_v2(l1_clip, l1);
2190 copy_v2_v2(l2_clip, l2);
2191 return true;
2192 }
2193 return false;
2194 }
2195
2196 copy_v2_v2(l1_clip, l1);
2197 copy_v2_v2(l2_clip, l2);
2198 CLAMP(l1_clip[1], rect->ymin, rect->ymax);
2199 CLAMP(l2_clip[1], rect->ymin, rect->ymax);
2200 return true;
2201 }
2202
2203 float isect;
2204 short ok1 = 0;
2205 short ok2 = 0;
2206
2207 /* Done with vertical lines */
2208
2209 /* are either of the points inside the rectangle ? */
2210 if (BLI_rctf_isect_pt_v(rect, l1)) {
2211 copy_v2_v2(l1_clip, l1);
2212 ok1 = 1;
2213 }
2214
2215 if (BLI_rctf_isect_pt_v(rect, l2)) {
2216 copy_v2_v2(l2_clip, l2);
2217 ok2 = 1;
2218 }
2219
2220 /* line inside rect */
2221 if (ok1 && ok2) {
2222 return true;
2223 }
2224
2225 /* top/bottom */
2226 if (line_isect_y(l1, l2, rect->ymin, &isect) && (isect >= cliprect->xmin) &&
2227 (isect <= cliprect->xmax))
2228 {
2229 if (l1[1] < l2[1]) { /* line 1 is outside */
2230 l1_clip[0] = isect;
2231 l1_clip[1] = rect->ymin;
2232 ok1 = 1;
2233 }
2234 else {
2235 l2_clip[0] = isect;
2236 l2_clip[1] = rect->ymin;
2237 ok2 = 2;
2238 }
2239 }
2240
2241 if (ok1 && ok2) {
2242 return true;
2243 }
2244
2245 if (line_isect_y(l1, l2, rect->ymax, &isect) && (isect >= cliprect->xmin) &&
2246 (isect <= cliprect->xmax))
2247 {
2248 if (l1[1] > l2[1]) { /* line 1 is outside */
2249 l1_clip[0] = isect;
2250 l1_clip[1] = rect->ymax;
2251 ok1 = 1;
2252 }
2253 else {
2254 l2_clip[0] = isect;
2255 l2_clip[1] = rect->ymax;
2256 ok2 = 2;
2257 }
2258 }
2259
2260 if (ok1 && ok2) {
2261 return true;
2262 }
2263
2264 /* left/right */
2265 if (line_isect_x(l1, l2, rect->xmin, &isect) && (isect >= cliprect->ymin) &&
2266 (isect <= cliprect->ymax))
2267 {
2268 if (l1[0] < l2[0]) { /* line 1 is outside */
2269 l1_clip[0] = rect->xmin;
2270 l1_clip[1] = isect;
2271 ok1 = 1;
2272 }
2273 else {
2274 l2_clip[0] = rect->xmin;
2275 l2_clip[1] = isect;
2276 ok2 = 2;
2277 }
2278 }
2279
2280 if (ok1 && ok2) {
2281 return true;
2282 }
2283
2284 if (line_isect_x(l1, l2, rect->xmax, &isect) && (isect >= cliprect->ymin) &&
2285 (isect <= cliprect->ymax))
2286 {
2287 if (l1[0] > l2[0]) { /* line 1 is outside */
2288 l1_clip[0] = rect->xmax;
2289 l1_clip[1] = isect;
2290 ok1 = 1;
2291 }
2292 else {
2293 l2_clip[0] = rect->xmax;
2294 l2_clip[1] = isect;
2295 ok2 = 2;
2296 }
2297 }
2298
2299 if (ok1 && ok2) {
2300 return true;
2301 }
2302 return false;
2303}
2304
2310#ifndef PROJ_DEBUG_NOSEAMBLEED
2311
2312static void scale_tri(float insetCos[3][3], const float *origCos[3], const float inset)
2313{
2314 float cent[3];
2315 cent[0] = (origCos[0][0] + origCos[1][0] + origCos[2][0]) * (1.0f / 3.0f);
2316 cent[1] = (origCos[0][1] + origCos[1][1] + origCos[2][1]) * (1.0f / 3.0f);
2317 cent[2] = (origCos[0][2] + origCos[1][2] + origCos[2][2]) * (1.0f / 3.0f);
2318
2319 sub_v3_v3v3(insetCos[0], origCos[0], cent);
2320 sub_v3_v3v3(insetCos[1], origCos[1], cent);
2321 sub_v3_v3v3(insetCos[2], origCos[2], cent);
2322
2323 mul_v3_fl(insetCos[0], inset);
2324 mul_v3_fl(insetCos[1], inset);
2325 mul_v3_fl(insetCos[2], inset);
2326
2327 add_v3_v3(insetCos[0], cent);
2328 add_v3_v3(insetCos[1], cent);
2329 add_v3_v3(insetCos[2], cent);
2330}
2331#endif // PROJ_DEBUG_NOSEAMBLEED
2332
2333static float len_squared_v2v2_alt(const float v1[2], const float v2_1, const float v2_2)
2334{
2335 float x, y;
2336
2337 x = v1[0] - v2_1;
2338 y = v1[1] - v2_2;
2339 return x * x + y * y;
2340}
2341
2348static bool project_bucket_isect_circle(const float cent[2],
2349 const float radius_squared,
2350 const rctf *bucket_bounds)
2351{
2352
2353 /* Would normally to a simple intersection test,
2354 * however we know the bounds of these 2 already intersect so we only need to test
2355 * if the center is inside the vertical or horizontal bounds on either axis,
2356 * this is even less work than an intersection test.
2357 */
2358#if 0
2359 if (BLI_rctf_isect_pt_v(bucket_bounds, cent)) {
2360 return true;
2361 }
2362#endif
2363
2364 if ((bucket_bounds->xmin <= cent[0] && bucket_bounds->xmax >= cent[0]) ||
2365 (bucket_bounds->ymin <= cent[1] && bucket_bounds->ymax >= cent[1]))
2366 {
2367 return true;
2368 }
2369
2370 /* out of bounds left */
2371 if (cent[0] < bucket_bounds->xmin) {
2372 /* lower left out of radius test */
2373 if (cent[1] < bucket_bounds->ymin) {
2374 return (len_squared_v2v2_alt(cent, bucket_bounds->xmin, bucket_bounds->ymin) <
2375 radius_squared) ?
2376 true :
2377 false;
2378 }
2379 /* top left test */
2380 if (cent[1] > bucket_bounds->ymax) {
2381 return (len_squared_v2v2_alt(cent, bucket_bounds->xmin, bucket_bounds->ymax) <
2382 radius_squared) ?
2383 true :
2384 false;
2385 }
2386 }
2387 else if (cent[0] > bucket_bounds->xmax) {
2388 /* lower right out of radius test */
2389 if (cent[1] < bucket_bounds->ymin) {
2390 return (len_squared_v2v2_alt(cent, bucket_bounds->xmax, bucket_bounds->ymin) <
2391 radius_squared) ?
2392 true :
2393 false;
2394 }
2395 /* top right test */
2396 if (cent[1] > bucket_bounds->ymax) {
2397 return (len_squared_v2v2_alt(cent, bucket_bounds->xmax, bucket_bounds->ymax) <
2398 radius_squared) ?
2399 true :
2400 false;
2401 }
2402 }
2403
2404 return false;
2405}
2406
2407/* Note for #rect_to_uvspace_ortho() and #rect_to_uvspace_persp()
2408 * in ortho view this function gives good results when bucket_bounds are outside the triangle
2409 * however in some cases, perspective view will mess up with faces
2410 * that have minimal screen-space area (viewed from the side).
2411 *
2412 * for this reason its not reliable in this case so we'll use the Simple Barycentric'
2413 * functions that only account for points inside the triangle.
2414 * however switching back to this for ortho is always an option. */
2415
2416static void rect_to_uvspace_ortho(const rctf *bucket_bounds,
2417 const float *v1coSS,
2418 const float *v2coSS,
2419 const float *v3coSS,
2420 const float *uv1co,
2421 const float *uv2co,
2422 const float *uv3co,
2423 float bucket_bounds_uv[4][2],
2424 const int flip)
2425{
2426 float uv[2];
2427 float w[3];
2428
2429 /* get the UV space bounding box */
2430 uv[0] = bucket_bounds->xmax;
2431 uv[1] = bucket_bounds->ymin;
2432 barycentric_weights_v2(v1coSS, v2coSS, v3coSS, uv, w);
2433 interp_v2_v2v2v2(bucket_bounds_uv[flip ? 3 : 0], uv1co, uv2co, uv3co, w);
2434
2435 // uv[0] = bucket_bounds->xmax; // set above
2436 uv[1] = bucket_bounds->ymax;
2437 barycentric_weights_v2(v1coSS, v2coSS, v3coSS, uv, w);
2438 interp_v2_v2v2v2(bucket_bounds_uv[flip ? 2 : 1], uv1co, uv2co, uv3co, w);
2439
2440 uv[0] = bucket_bounds->xmin;
2441 // uv[1] = bucket_bounds->ymax; // set above
2442 barycentric_weights_v2(v1coSS, v2coSS, v3coSS, uv, w);
2443 interp_v2_v2v2v2(bucket_bounds_uv[flip ? 1 : 2], uv1co, uv2co, uv3co, w);
2444
2445 // uv[0] = bucket_bounds->xmin; // set above
2446 uv[1] = bucket_bounds->ymin;
2447 barycentric_weights_v2(v1coSS, v2coSS, v3coSS, uv, w);
2448 interp_v2_v2v2v2(bucket_bounds_uv[flip ? 0 : 3], uv1co, uv2co, uv3co, w);
2449}
2450
2454static void rect_to_uvspace_persp(const rctf *bucket_bounds,
2455 const float *v1coSS,
2456 const float *v2coSS,
2457 const float *v3coSS,
2458 const float *uv1co,
2459 const float *uv2co,
2460 const float *uv3co,
2461 float bucket_bounds_uv[4][2],
2462 const int flip)
2463{
2464 float uv[2];
2465 float w[3];
2466
2467 /* get the UV space bounding box */
2468 uv[0] = bucket_bounds->xmax;
2469 uv[1] = bucket_bounds->ymin;
2470 barycentric_weights_v2_persp(v1coSS, v2coSS, v3coSS, uv, w);
2471 interp_v2_v2v2v2(bucket_bounds_uv[flip ? 3 : 0], uv1co, uv2co, uv3co, w);
2472
2473 // uv[0] = bucket_bounds->xmax; // set above
2474 uv[1] = bucket_bounds->ymax;
2475 barycentric_weights_v2_persp(v1coSS, v2coSS, v3coSS, uv, w);
2476 interp_v2_v2v2v2(bucket_bounds_uv[flip ? 2 : 1], uv1co, uv2co, uv3co, w);
2477
2478 uv[0] = bucket_bounds->xmin;
2479 // uv[1] = bucket_bounds->ymax; // set above
2480 barycentric_weights_v2_persp(v1coSS, v2coSS, v3coSS, uv, w);
2481 interp_v2_v2v2v2(bucket_bounds_uv[flip ? 1 : 2], uv1co, uv2co, uv3co, w);
2482
2483 // uv[0] = bucket_bounds->xmin; // set above
2484 uv[1] = bucket_bounds->ymin;
2485 barycentric_weights_v2_persp(v1coSS, v2coSS, v3coSS, uv, w);
2486 interp_v2_v2v2v2(bucket_bounds_uv[flip ? 0 : 3], uv1co, uv2co, uv3co, w);
2487}
2488
2489/* This works as we need it to but we can save a few steps and not use it */
2490
2491#if 0
2492static float angle_2d_clockwise(const float p1[2], const float p2[2], const float p3[2])
2493{
2494 float v1[2], v2[2];
2495
2496 v1[0] = p1[0] - p2[0];
2497 v1[1] = p1[1] - p2[1];
2498 v2[0] = p3[0] - p2[0];
2499 v2[1] = p3[1] - p2[1];
2500
2501 return -atan2f(v1[0] * v2[1] - v1[1] * v2[0], v1[0] * v2[0] + v1[1] * v2[1]);
2502}
2503#endif
2504
2505#define ISECT_1 (1)
2506#define ISECT_2 (1 << 1)
2507#define ISECT_3 (1 << 2)
2508#define ISECT_4 (1 << 3)
2509#define ISECT_ALL3 ((1 << 3) - 1)
2510#define ISECT_ALL4 ((1 << 4) - 1)
2511
2512/* limit must be a fraction over 1.0f */
2514 const float pt[2], const float v1[2], const float v2[2], const float v3[2], const float limit)
2515{
2516 return ((area_tri_v2(pt, v1, v2) + area_tri_v2(pt, v2, v3) + area_tri_v2(pt, v3, v1)) /
2517 area_tri_v2(v1, v2, v3)) < limit;
2518}
2519
2524static int float_z_sort_flip(const void *p1, const void *p2)
2525{
2526 return ((static_cast<float *>(const_cast<void *>(p1)))[2] <
2527 (static_cast<float *>(const_cast<void *>(p2)))[2] ?
2528 1 :
2529 -1);
2530}
2531
2532static int float_z_sort(const void *p1, const void *p2)
2533{
2534 return ((static_cast<float *>(const_cast<void *>(p1)))[2] <
2535 (static_cast<float *>(const_cast<void *>(p2)))[2] ?
2536 -1 :
2537 1);
2538}
2539
2540/* assumes one point is within the rectangle */
2541static bool line_rect_clip(const rctf *rect,
2542 const float l1[4],
2543 const float l2[4],
2544 const float uv1[2],
2545 const float uv2[2],
2546 float uv[2],
2547 bool is_ortho)
2548{
2549 float min = FLT_MAX, tmp;
2550 float xlen = l2[0] - l1[0];
2551 float ylen = l2[1] - l1[1];
2552
2553 /* 0.1 might seem too much, but remember, this is pixels! */
2554 if (xlen > 0.1f) {
2555 if ((l1[0] - rect->xmin) * (l2[0] - rect->xmin) <= 0) {
2556 tmp = rect->xmin;
2557 min = min_ff((tmp - l1[0]) / xlen, min);
2558 }
2559 else if ((l1[0] - rect->xmax) * (l2[0] - rect->xmax) < 0) {
2560 tmp = rect->xmax;
2561 min = min_ff((tmp - l1[0]) / xlen, min);
2562 }
2563 }
2564
2565 if (ylen > 0.1f) {
2566 if ((l1[1] - rect->ymin) * (l2[1] - rect->ymin) <= 0) {
2567 tmp = rect->ymin;
2568 min = min_ff((tmp - l1[1]) / ylen, min);
2569 }
2570 else if ((l1[1] - rect->ymax) * (l2[1] - rect->ymax) < 0) {
2571 tmp = rect->ymax;
2572 min = min_ff((tmp - l1[1]) / ylen, min);
2573 }
2574 }
2575
2576 if (min == FLT_MAX) {
2577 return false;
2578 }
2579
2580 tmp = (is_ortho) ? 1.0f : (l1[3] + min * (l2[3] - l1[3]));
2581
2582 uv[0] = (uv1[0] + min / tmp * (uv2[0] - uv1[0]));
2583 uv[1] = (uv1[1] + min / tmp * (uv2[1] - uv1[1]));
2584
2585 return true;
2586}
2587
2588static void project_bucket_clip_face(const bool is_ortho,
2589 const bool is_flip_object,
2590 const rctf *cliprect,
2591 const rctf *bucket_bounds,
2592 const float *v1coSS,
2593 const float *v2coSS,
2594 const float *v3coSS,
2595 const float *uv1co,
2596 const float *uv2co,
2597 const float *uv3co,
2598 float bucket_bounds_uv[8][2],
2599 int *tot,
2600 bool cull)
2601{
2602 int inside_bucket_flag = 0;
2603 int inside_face_flag = 0;
2604 int flip;
2605 bool collinear = false;
2606
2607 float bucket_bounds_ss[4][2];
2608
2609 /* detect pathological case where face the three vertices are almost collinear in screen space.
2610 * mostly those will be culled but when flood filling or with
2611 * smooth shading it's a possibility */
2612 if (std::min({dist_squared_to_line_v2(v1coSS, v2coSS, v3coSS),
2613 dist_squared_to_line_v2(v2coSS, v3coSS, v1coSS),
2614 dist_squared_to_line_v2(v3coSS, v1coSS, v2coSS)}) < PROJ_PIXEL_TOLERANCE)
2615 {
2616 collinear = true;
2617 }
2618
2619 /* get the UV space bounding box */
2620 inside_bucket_flag |= int(BLI_rctf_isect_pt_v(bucket_bounds, v1coSS));
2621 inside_bucket_flag |= int(BLI_rctf_isect_pt_v(bucket_bounds, v2coSS)) << 1;
2622 inside_bucket_flag |= int(BLI_rctf_isect_pt_v(bucket_bounds, v3coSS)) << 2;
2623
2624 if (inside_bucket_flag == ISECT_ALL3) {
2625 /* is_flip_object is used here because we use the face winding */
2626 flip = (((line_point_side_v2(v1coSS, v2coSS, v3coSS) > 0.0f) != is_flip_object) !=
2627 (line_point_side_v2(uv1co, uv2co, uv3co) > 0.0f));
2628
2629 /* All screen-space points are inside the bucket bounding box,
2630 * this means we don't need to clip and can simply return the UVs. */
2631 if (flip) { /* facing the back? */
2632 copy_v2_v2(bucket_bounds_uv[0], uv3co);
2633 copy_v2_v2(bucket_bounds_uv[1], uv2co);
2634 copy_v2_v2(bucket_bounds_uv[2], uv1co);
2635 }
2636 else {
2637 copy_v2_v2(bucket_bounds_uv[0], uv1co);
2638 copy_v2_v2(bucket_bounds_uv[1], uv2co);
2639 copy_v2_v2(bucket_bounds_uv[2], uv3co);
2640 }
2641
2642 *tot = 3;
2643 return;
2644 }
2645 /* Handle pathological case here,
2646 * no need for further intersections below since triangle area is almost zero. */
2647 if (collinear) {
2648 int flag;
2649
2650 (*tot) = 0;
2651
2652 if (cull) {
2653 return;
2654 }
2655
2656 if (inside_bucket_flag & ISECT_1) {
2657 copy_v2_v2(bucket_bounds_uv[*tot], uv1co);
2658 (*tot)++;
2659 }
2660
2661 flag = inside_bucket_flag & (ISECT_1 | ISECT_2);
2662 if (flag && flag != (ISECT_1 | ISECT_2)) {
2663 if (line_rect_clip(
2664 bucket_bounds, v1coSS, v2coSS, uv1co, uv2co, bucket_bounds_uv[*tot], is_ortho))
2665 {
2666 (*tot)++;
2667 }
2668 }
2669
2670 if (inside_bucket_flag & ISECT_2) {
2671 copy_v2_v2(bucket_bounds_uv[*tot], uv2co);
2672 (*tot)++;
2673 }
2674
2675 flag = inside_bucket_flag & (ISECT_2 | ISECT_3);
2676 if (flag && flag != (ISECT_2 | ISECT_3)) {
2677 if (line_rect_clip(
2678 bucket_bounds, v2coSS, v3coSS, uv2co, uv3co, bucket_bounds_uv[*tot], is_ortho))
2679 {
2680 (*tot)++;
2681 }
2682 }
2683
2684 if (inside_bucket_flag & ISECT_3) {
2685 copy_v2_v2(bucket_bounds_uv[*tot], uv3co);
2686 (*tot)++;
2687 }
2688
2689 flag = inside_bucket_flag & (ISECT_3 | ISECT_1);
2690 if (flag && flag != (ISECT_3 | ISECT_1)) {
2691 if (line_rect_clip(
2692 bucket_bounds, v3coSS, v1coSS, uv3co, uv1co, bucket_bounds_uv[*tot], is_ortho))
2693 {
2694 (*tot)++;
2695 }
2696 }
2697
2698 if ((*tot) < 3) {
2699 /* no intersections to speak of, but more probable is that all face is just outside the
2700 * rectangle and culled due to float precision issues. Since above tests have failed,
2701 * just dump triangle as is for painting */
2702 *tot = 0;
2703 copy_v2_v2(bucket_bounds_uv[*tot], uv1co);
2704 (*tot)++;
2705 copy_v2_v2(bucket_bounds_uv[*tot], uv2co);
2706 (*tot)++;
2707 copy_v2_v2(bucket_bounds_uv[*tot], uv3co);
2708 (*tot)++;
2709 return;
2710 }
2711
2712 return;
2713 }
2714
2715 /* Get the UV space bounding box. */
2716 /* Use #IsectPT2Df_limit here so we catch points are touching the triangles edge
2717 * (or a small fraction over) */
2718 bucket_bounds_ss[0][0] = bucket_bounds->xmax;
2719 bucket_bounds_ss[0][1] = bucket_bounds->ymin;
2720 inside_face_flag |= (IsectPT2Df_limit(
2721 bucket_bounds_ss[0], v1coSS, v2coSS, v3coSS, 1 + PROJ_GEOM_TOLERANCE) ?
2722 ISECT_1 :
2723 0);
2724
2725 bucket_bounds_ss[1][0] = bucket_bounds->xmax;
2726 bucket_bounds_ss[1][1] = bucket_bounds->ymax;
2727 inside_face_flag |= (IsectPT2Df_limit(
2728 bucket_bounds_ss[1], v1coSS, v2coSS, v3coSS, 1 + PROJ_GEOM_TOLERANCE) ?
2729 ISECT_2 :
2730 0);
2731
2732 bucket_bounds_ss[2][0] = bucket_bounds->xmin;
2733 bucket_bounds_ss[2][1] = bucket_bounds->ymax;
2734 inside_face_flag |= (IsectPT2Df_limit(
2735 bucket_bounds_ss[2], v1coSS, v2coSS, v3coSS, 1 + PROJ_GEOM_TOLERANCE) ?
2736 ISECT_3 :
2737 0);
2738
2739 bucket_bounds_ss[3][0] = bucket_bounds->xmin;
2740 bucket_bounds_ss[3][1] = bucket_bounds->ymin;
2741 inside_face_flag |= (IsectPT2Df_limit(
2742 bucket_bounds_ss[3], v1coSS, v2coSS, v3coSS, 1 + PROJ_GEOM_TOLERANCE) ?
2743 ISECT_4 :
2744 0);
2745
2746 flip = ((line_point_side_v2(v1coSS, v2coSS, v3coSS) > 0.0f) !=
2747 (line_point_side_v2(uv1co, uv2co, uv3co) > 0.0f));
2748
2749 if (inside_face_flag == ISECT_ALL4) {
2750 /* Bucket is totally inside the screen-space face, we can safely use weights. */
2751
2752 if (is_ortho) {
2754 bucket_bounds, v1coSS, v2coSS, v3coSS, uv1co, uv2co, uv3co, bucket_bounds_uv, flip);
2755 }
2756 else {
2758 bucket_bounds, v1coSS, v2coSS, v3coSS, uv1co, uv2co, uv3co, bucket_bounds_uv, flip);
2759 }
2760
2761 *tot = 4;
2762 return;
2763 }
2764
2765 {
2766 /* The Complicated Case!
2767 *
2768 * The 2 cases above are where the face is inside the bucket
2769 * or the bucket is inside the face.
2770 *
2771 * we need to make a convex poly-line from the intersection between the screen-space face
2772 * and the bucket bounds.
2773 *
2774 * There are a number of ways this could be done, currently it just collects all
2775 * intersecting verts, and line intersections, then sorts them clockwise, this is
2776 * a lot easier than evaluating the geometry to do a correct clipping on both shapes.
2777 */
2778
2779 /* Add a bunch of points, we know must make up the convex hull
2780 * which is the clipped rect and triangle */
2781
2782 /* Maximum possible 6 intersections when using a rectangle and triangle */
2783
2784 /* The 3rd float is used to store angle for qsort(), NOT as a Z location */
2785 float isectVCosSS[8][3];
2786 float v1_clipSS[2], v2_clipSS[2];
2787 float w[3];
2788
2789 /* calc center */
2790 float cent[2] = {0.0f, 0.0f};
2791 // float up[2] = {0.0f, 1.0f};
2792 bool doubles;
2793
2794 (*tot) = 0;
2795
2796 if (inside_face_flag & ISECT_1) {
2797 copy_v2_v2(isectVCosSS[*tot], bucket_bounds_ss[0]);
2798 (*tot)++;
2799 }
2800 if (inside_face_flag & ISECT_2) {
2801 copy_v2_v2(isectVCosSS[*tot], bucket_bounds_ss[1]);
2802 (*tot)++;
2803 }
2804 if (inside_face_flag & ISECT_3) {
2805 copy_v2_v2(isectVCosSS[*tot], bucket_bounds_ss[2]);
2806 (*tot)++;
2807 }
2808 if (inside_face_flag & ISECT_4) {
2809 copy_v2_v2(isectVCosSS[*tot], bucket_bounds_ss[3]);
2810 (*tot)++;
2811 }
2812
2813 if (inside_bucket_flag & ISECT_1) {
2814 copy_v2_v2(isectVCosSS[*tot], v1coSS);
2815 (*tot)++;
2816 }
2817 if (inside_bucket_flag & ISECT_2) {
2818 copy_v2_v2(isectVCosSS[*tot], v2coSS);
2819 (*tot)++;
2820 }
2821 if (inside_bucket_flag & ISECT_3) {
2822 copy_v2_v2(isectVCosSS[*tot], v3coSS);
2823 (*tot)++;
2824 }
2825
2826 if ((inside_bucket_flag & (ISECT_1 | ISECT_2)) != (ISECT_1 | ISECT_2)) {
2827 if (line_clip_rect2f(cliprect, bucket_bounds, v1coSS, v2coSS, v1_clipSS, v2_clipSS)) {
2828 if ((inside_bucket_flag & ISECT_1) == 0) {
2829 copy_v2_v2(isectVCosSS[*tot], v1_clipSS);
2830 (*tot)++;
2831 }
2832 if ((inside_bucket_flag & ISECT_2) == 0) {
2833 copy_v2_v2(isectVCosSS[*tot], v2_clipSS);
2834 (*tot)++;
2835 }
2836 }
2837 }
2838
2839 if ((inside_bucket_flag & (ISECT_2 | ISECT_3)) != (ISECT_2 | ISECT_3)) {
2840 if (line_clip_rect2f(cliprect, bucket_bounds, v2coSS, v3coSS, v1_clipSS, v2_clipSS)) {
2841 if ((inside_bucket_flag & ISECT_2) == 0) {
2842 copy_v2_v2(isectVCosSS[*tot], v1_clipSS);
2843 (*tot)++;
2844 }
2845 if ((inside_bucket_flag & ISECT_3) == 0) {
2846 copy_v2_v2(isectVCosSS[*tot], v2_clipSS);
2847 (*tot)++;
2848 }
2849 }
2850 }
2851
2852 if ((inside_bucket_flag & (ISECT_3 | ISECT_1)) != (ISECT_3 | ISECT_1)) {
2853 if (line_clip_rect2f(cliprect, bucket_bounds, v3coSS, v1coSS, v1_clipSS, v2_clipSS)) {
2854 if ((inside_bucket_flag & ISECT_3) == 0) {
2855 copy_v2_v2(isectVCosSS[*tot], v1_clipSS);
2856 (*tot)++;
2857 }
2858 if ((inside_bucket_flag & ISECT_1) == 0) {
2859 copy_v2_v2(isectVCosSS[*tot], v2_clipSS);
2860 (*tot)++;
2861 }
2862 }
2863 }
2864
2865 if ((*tot) < 3) { /* no intersections to speak of */
2866 *tot = 0;
2867 return;
2868 }
2869
2870 /* now we have all points we need, collect their angles and sort them clockwise */
2871
2872 for (int i = 0; i < (*tot); i++) {
2873 cent[0] += isectVCosSS[i][0];
2874 cent[1] += isectVCosSS[i][1];
2875 }
2876 cent[0] = cent[0] / float(*tot);
2877 cent[1] = cent[1] / float(*tot);
2878
2879 /* Collect angles for every point around the center point */
2880
2881#if 0 /* uses a few more cycles than the above loop */
2882 for (int i = 0; i < (*tot); i++) {
2883 isectVCosSS[i][2] = angle_2d_clockwise(up, cent, isectVCosSS[i]);
2884 }
2885#endif
2886
2887 /* Abuse this var for the loop below */
2888 v1_clipSS[0] = cent[0];
2889 v1_clipSS[1] = cent[1] + 1.0f;
2890
2891 for (int i = 0; i < (*tot); i++) {
2892 v2_clipSS[0] = isectVCosSS[i][0] - cent[0];
2893 v2_clipSS[1] = isectVCosSS[i][1] - cent[1];
2894 isectVCosSS[i][2] = atan2f(v1_clipSS[0] * v2_clipSS[1] - v1_clipSS[1] * v2_clipSS[0],
2895 v1_clipSS[0] * v2_clipSS[0] + v1_clipSS[1] * v2_clipSS[1]);
2896 }
2897
2898 if (flip) {
2899 qsort(isectVCosSS, *tot, sizeof(float[3]), float_z_sort_flip);
2900 }
2901 else {
2902 qsort(isectVCosSS, *tot, sizeof(float[3]), float_z_sort);
2903 }
2904
2905 doubles = true;
2906 while (doubles == true) {
2907 doubles = false;
2908
2909 for (int i = 0; i < (*tot); i++) {
2910 if (fabsf(isectVCosSS[(i + 1) % *tot][0] - isectVCosSS[i][0]) < PROJ_PIXEL_TOLERANCE &&
2911 fabsf(isectVCosSS[(i + 1) % *tot][1] - isectVCosSS[i][1]) < PROJ_PIXEL_TOLERANCE)
2912 {
2913 for (int j = i; j < (*tot) - 1; j++) {
2914 isectVCosSS[j][0] = isectVCosSS[j + 1][0];
2915 isectVCosSS[j][1] = isectVCosSS[j + 1][1];
2916 }
2917 /* keep looking for more doubles */
2918 doubles = true;
2919 (*tot)--;
2920 }
2921 }
2922
2923 /* its possible there is only a few left after remove doubles */
2924 if ((*tot) < 3) {
2925 // printf("removed too many doubles B\n");
2926 *tot = 0;
2927 return;
2928 }
2929 }
2930
2931 if (is_ortho) {
2932 for (int i = 0; i < (*tot); i++) {
2933 barycentric_weights_v2(v1coSS, v2coSS, v3coSS, isectVCosSS[i], w);
2934 interp_v2_v2v2v2(bucket_bounds_uv[i], uv1co, uv2co, uv3co, w);
2935 }
2936 }
2937 else {
2938 for (int i = 0; i < (*tot); i++) {
2939 barycentric_weights_v2_persp(v1coSS, v2coSS, v3coSS, isectVCosSS[i], w);
2940 interp_v2_v2v2v2(bucket_bounds_uv[i], uv1co, uv2co, uv3co, w);
2941 }
2942 }
2943 }
2944
2945#ifdef PROJ_DEBUG_PRINT_CLIP
2946 /* include this at the bottom of the above function to debug the output */
2947
2948 {
2949 /* If there are ever any problems, */
2950 float test_uv[4][2];
2951 int i;
2952 if (is_ortho) {
2954 bucket_bounds, v1coSS, v2coSS, v3coSS, uv1co, uv2co, uv3co, test_uv, flip);
2955 }
2956 else {
2958 bucket_bounds, v1coSS, v2coSS, v3coSS, uv1co, uv2co, uv3co, test_uv, flip);
2959 }
2960 printf("( [(%f,%f), (%f,%f), (%f,%f), (%f,%f)], ",
2961 test_uv[0][0],
2962 test_uv[0][1],
2963 test_uv[1][0],
2964 test_uv[1][1],
2965 test_uv[2][0],
2966 test_uv[2][1],
2967 test_uv[3][0],
2968 test_uv[3][1]);
2969
2970 printf(" [(%f,%f), (%f,%f), (%f,%f)], ",
2971 uv1co[0],
2972 uv1co[1],
2973 uv2co[0],
2974 uv2co[1],
2975 uv3co[0],
2976 uv3co[1]);
2977
2978 printf("[");
2979 for (int i = 0; i < (*tot); i++) {
2980 printf("(%f, %f),", bucket_bounds_uv[i][0], bucket_bounds_uv[i][1]);
2981 }
2982 printf("]),\\\n");
2983 }
2984#endif
2985}
2986
3034
3035#undef ISECT_1
3036#undef ISECT_2
3037#undef ISECT_3
3038#undef ISECT_4
3039#undef ISECT_ALL3
3040#undef ISECT_ALL4
3041
3042/* checks if pt is inside a convex 2D polyline, the polyline must be ordered rotating clockwise
3043 * otherwise it would have to test for mixed (line_point_side_v2 > 0.0f) cases */
3044static bool IsectPoly2Df(const float pt[2], const float uv[][2], const int tot)
3045{
3046 int i;
3047 if (line_point_side_v2(uv[tot - 1], uv[0], pt) < 0.0f) {
3048 return false;
3049 }
3050
3051 for (i = 1; i < tot; i++) {
3052 if (line_point_side_v2(uv[i - 1], uv[i], pt) < 0.0f) {
3053 return false;
3054 }
3055 }
3056
3057 return true;
3058}
3059static bool IsectPoly2Df_twoside(const float pt[2], const float uv[][2], const int tot)
3060{
3061 const bool side = (line_point_side_v2(uv[tot - 1], uv[0], pt) > 0.0f);
3062
3063 for (int i = 1; i < tot; i++) {
3064 if ((line_point_side_v2(uv[i - 1], uv[i], pt) > 0.0f) != side) {
3065 return false;
3066 }
3067 }
3068
3069 return true;
3070}
3071
3072/* One of the most important function for projection painting,
3073 * since it selects the pixels to be added into each bucket.
3074 *
3075 * initialize pixels from this face where it intersects with the bucket_index,
3076 * optionally initialize pixels for removing seams */
3078 const int thread_index,
3079 const int bucket_index,
3080 const int tri_index,
3081 const int image_index,
3082 const rctf *clip_rect,
3083 const rctf *bucket_bounds,
3084 ImBuf *ibuf)
3085{
3086 /* Projection vars, to get the 3D locations into screen space. */
3087 MemArena *arena = ps->arena_mt[thread_index];
3088 LinkNode **bucketPixelNodes = ps->bucketRect + bucket_index;
3089 LinkNode *bucketFaceNodes = ps->bucketFaces[bucket_index];
3090 bool threaded = (ps->thread_tot > 1);
3091
3092 TileInfo tinf = {
3093 ps->tile_lock,
3094 ps->do_masking,
3096 ps->projImages + image_index,
3097 };
3098
3099 const int3 &tri = ps->corner_tris_eval[tri_index];
3100 const int face_i = ps->corner_tri_faces_eval[tri_index];
3101 const int vert_tri[3] = {PS_CORNER_TRI_AS_VERT_INDEX_3(ps, tri)};
3102 const float *tri_uv[3];
3103 if (!ps->poly_to_loop_uv_single) {
3104 PS_CORNER_TRI_ASSIGN_UV_3(tri_uv, ps->poly_to_loop_uv, face_i, tri);
3105 }
3106
3107 /* UV/pixel seeking data */
3108 /* Image X/Y-Pixel */
3109 int x, y;
3110 float mask;
3111 /* Image floating point UV - same as x, y but from 0.0-1.0 */
3112 float uv[2];
3113
3114 /* vert co screen-space, these will be assigned to vert_tri[0-2] */
3115 const float *v1coSS, *v2coSS, *v3coSS;
3116
3117 /* Vertex screen-space coords. */
3118 const float *vCo[3];
3119
3120 float w[3], wco[3];
3121
3122 /* for convenience only, these will be assigned to tri_uv[0],1,2 or tri_uv[0],2,3 */
3123 float *uv1co, *uv2co, *uv3co;
3124 float pixelScreenCo[4];
3125 bool do_3d_mapping = ps->brush->mtex.brush_map_mode == MTEX_MAP_MODE_3D;
3126
3127 /* Image-space bounds. */
3128 rcti bounds_px;
3129 /* Variables for getting UV-space bounds. */
3130
3131 /* Bucket bounds in UV space so we can init pixels only for this face. */
3132 float tri_uv_pxoffset[3][2];
3133 float xhalfpx, yhalfpx;
3134 const float ibuf_xf = float(ibuf->x), ibuf_yf = float(ibuf->y);
3135
3136 /* for early loop exit */
3137 int has_x_isect = 0, has_isect = 0;
3138
3139 float uv_clip[8][2];
3140 int uv_clip_tot;
3141 const bool is_ortho = ps->is_ortho;
3142 const bool is_flip_object = ps->is_flip_object;
3143 const bool do_backfacecull = ps->do_backfacecull;
3144 const bool do_clip = RV3D_CLIPPING_ENABLED(ps->v3d, ps->rv3d);
3145
3146 vCo[0] = ps->vert_positions_eval[vert_tri[0]];
3147 vCo[1] = ps->vert_positions_eval[vert_tri[1]];
3148 vCo[2] = ps->vert_positions_eval[vert_tri[2]];
3149
3150 /* Use tri_uv_pxoffset instead of tri_uv so we can offset the UV half a pixel
3151 * this is done so we can avoid offsetting all the pixels by 0.5 which causes
3152 * problems when wrapping negative coords */
3153 xhalfpx = (0.5f + (PROJ_PIXEL_TOLERANCE * (1.0f / 3.0f))) / ibuf_xf;
3154 yhalfpx = (0.5f + (PROJ_PIXEL_TOLERANCE * (1.0f / 4.0f))) / ibuf_yf;
3155
3156 /* Note about (PROJ_GEOM_TOLERANCE/x) above...
3157 * Needed to add this offset since UV coords are often quads aligned to pixels.
3158 * In this case pixels can be exactly between 2 triangles causing nasty
3159 * artifacts.
3160 *
3161 * This workaround can be removed and painting will still work on most cases
3162 * but since the first thing most people try is painting onto a quad- better make it work.
3163 */
3164
3165 if (ps->poly_to_loop_uv_single) {
3166 float2 offset = ps->poly_to_loop_uv_single.value() - float2(xhalfpx, yhalfpx);
3167 copy_v2_v2(tri_uv_pxoffset[0], offset);
3168 copy_v2_v2(tri_uv_pxoffset[1], offset);
3169 copy_v2_v2(tri_uv_pxoffset[2], offset);
3170 }
3171 else {
3172 tri_uv_pxoffset[0][0] = tri_uv[0][0] - xhalfpx;
3173 tri_uv_pxoffset[0][1] = tri_uv[0][1] - yhalfpx;
3174
3175 tri_uv_pxoffset[1][0] = tri_uv[1][0] - xhalfpx;
3176 tri_uv_pxoffset[1][1] = tri_uv[1][1] - yhalfpx;
3177
3178 tri_uv_pxoffset[2][0] = tri_uv[2][0] - xhalfpx;
3179 tri_uv_pxoffset[2][1] = tri_uv[2][1] - yhalfpx;
3180 }
3181
3182 {
3183 uv1co = tri_uv_pxoffset[0]; /* Was `tri_uv[i1];`. */
3184 uv2co = tri_uv_pxoffset[1]; /* Was `tri_uv[i2];`. */
3185 uv3co = tri_uv_pxoffset[2]; /* Was `tri_uv[i3];`. */
3186
3187 v1coSS = ps->screenCoords[vert_tri[0]];
3188 v2coSS = ps->screenCoords[vert_tri[1]];
3189 v3coSS = ps->screenCoords[vert_tri[2]];
3190
3191 /* This function gives is a concave polyline in UV space from the clipped tri. */
3192 project_bucket_clip_face(is_ortho,
3193 is_flip_object,
3194 clip_rect,
3195 bucket_bounds,
3196 v1coSS,
3197 v2coSS,
3198 v3coSS,
3199 uv1co,
3200 uv2co,
3201 uv3co,
3202 uv_clip,
3203 &uv_clip_tot,
3204 do_backfacecull || ps->do_occlude);
3205
3206 /* Sometimes this happens, better just allow for 8 intersections
3207 * even though there should be max 6 */
3208#if 0
3209 if (uv_clip_tot > 6) {
3210 printf("this should never happen! %d\n", uv_clip_tot);
3211 }
3212#endif
3213
3214 if (pixel_bounds_array(uv_clip, ibuf->x, ibuf->y, uv_clip_tot, &bounds_px)) {
3215#if 0
3216 project_paint_undo_tiles_init(
3217 &bounds_px, ps->projImages + image_index, tmpibuf, tile_width, threaded, ps->do_masking);
3218#endif
3219 /* clip face and */
3220
3221 has_isect = 0;
3222 for (y = bounds_px.ymin; y < bounds_px.ymax; y++) {
3223 // uv[1] = (float(y) + 0.5f) / float(ibuf->y);
3224 /* use pixel offset UV coords instead */
3225 uv[1] = float(y) / ibuf_yf;
3226
3227 has_x_isect = 0;
3228 for (x = bounds_px.xmin; x < bounds_px.xmax; x++) {
3229 // uv[0] = (float(x) + 0.5f) / float(ibuf->x);
3230 /* use pixel offset UV coords instead */
3231 uv[0] = float(x) / ibuf_xf;
3232
3233 /* Note about IsectPoly2Df_twoside, checking the face or uv flipping doesn't work,
3234 * could check the poly direction but better to do this */
3235 if ((do_backfacecull == true && IsectPoly2Df(uv, uv_clip, uv_clip_tot)) ||
3236 (do_backfacecull == false && IsectPoly2Df_twoside(uv, uv_clip, uv_clip_tot)))
3237 {
3238
3239 has_x_isect = has_isect = 1;
3240
3241 if (is_ortho) {
3243 uv, v1coSS, v2coSS, v3coSS, uv1co, uv2co, uv3co, pixelScreenCo, w);
3244 }
3245 else {
3247 uv, v1coSS, v2coSS, v3coSS, uv1co, uv2co, uv3co, pixelScreenCo, w);
3248 }
3249
3250 /* A pity we need to get the world-space pixel location here
3251 * because it is a relatively expensive operation. */
3252 if (do_clip || do_3d_mapping) {
3253 interp_v3_v3v3v3(wco,
3254 ps->vert_positions_eval[vert_tri[0]],
3255 ps->vert_positions_eval[vert_tri[1]],
3256 ps->vert_positions_eval[vert_tri[2]],
3257 w);
3258 if (do_clip && ED_view3d_clipping_test(ps->rv3d, wco, true)) {
3259 /* Watch out that no code below this needs to run */
3260 continue;
3261 }
3262 }
3263
3264 /* Is this UV visible from the view? - ray-trace */
3265 /* project_paint_PickFace is less complex, use for testing */
3266 // if (project_paint_PickFace(ps, pixelScreenCo, w, &side) == tri_index) {
3267 if ((ps->do_occlude == false) ||
3268 !project_bucket_point_occluded(ps, bucketFaceNodes, tri_index, pixelScreenCo))
3269 {
3270 mask = project_paint_uvpixel_mask(ps, tri_index, w);
3271
3272 if (mask > 0.0f) {
3274 bucketPixelNodes,
3276 ps, arena, &tinf, x, y, mask, tri_index, pixelScreenCo, wco, w),
3277 arena);
3278 }
3279 }
3280 }
3281 // #if 0
3282 else if (has_x_isect) {
3283 /* assuming the face is not a bow-tie - we know we can't intersect again on the X */
3284 break;
3285 }
3286 // #endif
3287 }
3288
3289#if 0 /* TODO: investigate why this doesn't work sometimes! it should! */
3290 /* no intersection for this entire row,
3291 * after some intersection above means we can quit now */
3292 if (has_x_isect == 0 && has_isect) {
3293 break;
3294 }
3295#endif
3296 }
3297 }
3298 }
3299
3300#ifndef PROJ_DEBUG_NOSEAMBLEED
3301 if (ps->seam_bleed_px > 0.0f && !(ps->faceSeamFlags[tri_index] & PROJ_FACE_DEGENERATE)) {
3302 int face_seam_flag;
3303
3304 if (threaded) {
3305 /* Other threads could be modifying these vars. */
3307 }
3308
3309 face_seam_flag = ps->faceSeamFlags[tri_index];
3310
3311 /* are any of our edges un-initialized? */
3312 if ((face_seam_flag & PROJ_FACE_SEAM_INIT0) == 0 ||
3313 (face_seam_flag & PROJ_FACE_SEAM_INIT1) == 0 ||
3314 (face_seam_flag & PROJ_FACE_SEAM_INIT2) == 0)
3315 {
3316 project_face_seams_init(ps, arena, tri_index, 0, true, ibuf->x, ibuf->y);
3317 face_seam_flag = ps->faceSeamFlags[tri_index];
3318# if 0
3319 printf("seams - %d %d %d %d\n",
3320 flag & PROJ_FACE_SEAM0,
3321 flag & PROJ_FACE_SEAM1,
3322 flag & PROJ_FACE_SEAM2);
3323# endif
3324 }
3325
3326 if ((face_seam_flag & (PROJ_FACE_SEAM0 | PROJ_FACE_SEAM1 | PROJ_FACE_SEAM2)) == 0) {
3327
3328 if (threaded) {
3329 /* Other threads could be modifying these vars. */
3331 }
3332 }
3333 else {
3334 /* we have a seam - deal with it! */
3335
3336 /* Inset face coords.
3337 * - screen-space in orthographic view.
3338 * - world-space in perspective view.
3339 */
3340 float insetCos[3][3];
3341
3342 /* Vertex screen-space coords. */
3343 const float *vCoSS[3];
3344
3345 /* Store the screen-space coords of the face,
3346 * clipped by the bucket's screen aligned rectangle. */
3347 float bucket_clip_edges[2][2];
3348 float edge_verts_inset_clip[2][3];
3349 /* face edge pairs - loop through these:
3350 * ((0,1), (1,2), (2,3), (3,0)) or ((0,1), (1,2), (2,0)) for a tri */
3351 int fidx1, fidx2;
3352
3353 float seam_subsection[4][2];
3354 float fac1, fac2;
3355
3356 /* Pixel-space UVs. */
3357 float tri_puv[3][2];
3358
3359 tri_puv[0][0] = tri_uv_pxoffset[0][0] * ibuf->x;
3360 tri_puv[0][1] = tri_uv_pxoffset[0][1] * ibuf->y;
3361
3362 tri_puv[1][0] = tri_uv_pxoffset[1][0] * ibuf->x;
3363 tri_puv[1][1] = tri_uv_pxoffset[1][1] * ibuf->y;
3364
3365 tri_puv[2][0] = tri_uv_pxoffset[2][0] * ibuf->x;
3366 tri_puv[2][1] = tri_uv_pxoffset[2][1] * ibuf->y;
3367
3368 if ((ps->faceSeamFlags[tri_index] & PROJ_FACE_SEAM0) ||
3369 (ps->faceSeamFlags[tri_index] & PROJ_FACE_SEAM1) ||
3370 (ps->faceSeamFlags[tri_index] & PROJ_FACE_SEAM2))
3371 {
3372 uv_image_outset(ps, tri_uv_pxoffset, tri_puv, tri_index, ibuf->x, ibuf->y);
3373 }
3374
3375 /* ps->loopSeamUVs can't be modified when threading, now this is done we can unlock. */
3376 if (threaded) {
3377 /* Other threads could be modifying these vars */
3379 }
3380
3381 vCoSS[0] = ps->screenCoords[vert_tri[0]];
3382 vCoSS[1] = ps->screenCoords[vert_tri[1]];
3383 vCoSS[2] = ps->screenCoords[vert_tri[2]];
3384
3385 /* PROJ_FACE_SCALE_SEAM must be slightly less than 1.0f */
3386 if (is_ortho) {
3387 scale_tri(insetCos, vCoSS, PROJ_FACE_SCALE_SEAM);
3388 }
3389 else {
3390 scale_tri(insetCos, vCo, PROJ_FACE_SCALE_SEAM);
3391 }
3392
3393 for (fidx1 = 0; fidx1 < 3; fidx1++) {
3394 /* next fidx in the face (0,1,2) -> (1,2,0) */
3395 fidx2 = (fidx1 == 2) ? 0 : fidx1 + 1;
3396
3397 if ((face_seam_flag & (1 << fidx1)) && /* 1<<fidx1 -> PROJ_FACE_SEAM# */
3398 line_clip_rect2f(clip_rect,
3399 bucket_bounds,
3400 vCoSS[fidx1],
3401 vCoSS[fidx2],
3402 bucket_clip_edges[0],
3403 bucket_clip_edges[1]))
3404 {
3405 /* Avoid div by zero. */
3406 if (len_squared_v2v2(vCoSS[fidx1], vCoSS[fidx2]) > FLT_EPSILON) {
3407 uint loop_idx = ps->corner_tris_eval[tri_index][fidx1];
3408 LoopSeamData *seam_data = &ps->loopSeamData[loop_idx];
3409 float (*seam_uvs)[2] = seam_data->seam_uvs;
3410
3411 if (is_ortho) {
3412 fac1 = line_point_factor_v2(bucket_clip_edges[0], vCoSS[fidx1], vCoSS[fidx2]);
3413 fac2 = line_point_factor_v2(bucket_clip_edges[1], vCoSS[fidx1], vCoSS[fidx2]);
3414 }
3415 else {
3417 ps, bucket_clip_edges[0], vCo[fidx1], vCo[fidx2]);
3419 ps, bucket_clip_edges[1], vCo[fidx1], vCo[fidx2]);
3420 }
3421
3423 seam_subsection[0], tri_uv_pxoffset[fidx1], tri_uv_pxoffset[fidx2], fac1);
3425 seam_subsection[1], tri_uv_pxoffset[fidx1], tri_uv_pxoffset[fidx2], fac2);
3426
3427 interp_v2_v2v2(seam_subsection[2], seam_uvs[0], seam_uvs[1], fac2);
3428 interp_v2_v2v2(seam_subsection[3], seam_uvs[0], seam_uvs[1], fac1);
3429
3430 /* if the bucket_clip_edges values Z values was kept we could avoid this
3431 * Inset needs to be added so occlusion tests won't hit adjacent faces */
3432 interp_v3_v3v3(edge_verts_inset_clip[0], insetCos[fidx1], insetCos[fidx2], fac1);
3433 interp_v3_v3v3(edge_verts_inset_clip[1], insetCos[fidx1], insetCos[fidx2], fac2);
3434
3435 if (pixel_bounds_uv(seam_subsection, ibuf->x, ibuf->y, &bounds_px)) {
3436 /* bounds between the seam rect and the uvspace bucket pixels */
3437
3438 has_isect = 0;
3439 for (y = bounds_px.ymin; y < bounds_px.ymax; y++) {
3440 // uv[1] = (float(y) + 0.5f) / float(ibuf->y);
3441 /* use offset uvs instead */
3442 uv[1] = float(y) / ibuf_yf;
3443
3444 has_x_isect = 0;
3445 for (x = bounds_px.xmin; x < bounds_px.xmax; x++) {
3446 const float puv[2] = {float(x), float(y)};
3447 bool in_bounds;
3448 // uv[0] = (float(x) + 0.5f) / float(ibuf->x);
3449 /* use offset uvs instead */
3450 uv[0] = float(x) / ibuf_xf;
3451
3452 /* test we're inside uvspace bucket and triangle bounds */
3453 if (equals_v2v2(seam_uvs[0], seam_uvs[1])) {
3454 in_bounds = isect_point_tri_v2(uv, UNPACK3(seam_subsection));
3455 }
3456 else {
3457 in_bounds = isect_point_quad_v2(uv, UNPACK4(seam_subsection));
3458 }
3459
3460 if (in_bounds) {
3461 if ((seam_data->corner_dist_sq[0] > 0.0f) &&
3462 (len_squared_v2v2(puv, seam_data->seam_puvs[0]) <
3463 seam_data->corner_dist_sq[0]) &&
3464 (len_squared_v2v2(puv, tri_puv[fidx1]) > ps->seam_bleed_px_sq))
3465 {
3466 in_bounds = false;
3467 }
3468 else if ((seam_data->corner_dist_sq[1] > 0.0f) &&
3469 (len_squared_v2v2(puv, seam_data->seam_puvs[1]) <
3470 seam_data->corner_dist_sq[1]) &&
3471 (len_squared_v2v2(puv, tri_puv[fidx2]) > ps->seam_bleed_px_sq))
3472 {
3473 in_bounds = false;
3474 }
3475 }
3476
3477 if (in_bounds) {
3478 float pixel_on_edge[4];
3479 float fac;
3480
3481 if (is_ortho) {
3483 uv, v1coSS, v2coSS, v3coSS, uv1co, uv2co, uv3co, pixelScreenCo, w);
3484 }
3485 else {
3487 uv, v1coSS, v2coSS, v3coSS, uv1co, uv2co, uv3co, pixelScreenCo, w);
3488 }
3489
3490 /* We need the coord of the pixel on the edge, for the occlusion query. */
3491 fac = resolve_quad_u_v2(uv, UNPACK4(seam_subsection));
3493 pixel_on_edge, edge_verts_inset_clip[0], edge_verts_inset_clip[1], fac);
3494
3495 if (!is_ortho) {
3496 pixel_on_edge[3] = 1.0f;
3497 /* cast because of const */
3498 mul_m4_v4(const_cast<float (*)[4]>(ps->projectMat), pixel_on_edge);
3499 pixel_on_edge[0] = float(ps->winx * 0.5f) +
3500 (ps->winx * 0.5f) * pixel_on_edge[0] / pixel_on_edge[3];
3501 pixel_on_edge[1] = float(ps->winy * 0.5f) +
3502 (ps->winy * 0.5f) * pixel_on_edge[1] / pixel_on_edge[3];
3503 /* Use the depth for bucket point occlusion */
3504 pixel_on_edge[2] = pixel_on_edge[2] / pixel_on_edge[3];
3505 }
3506
3507 if ((ps->do_occlude == false) ||
3509 ps, bucketFaceNodes, tri_index, pixel_on_edge))
3510 {
3511 /* A pity we need to get the world-space pixel location here
3512 * because it is a relatively expensive operation. */
3513 if (do_clip || do_3d_mapping) {
3514 interp_v3_v3v3v3(wco, vCo[0], vCo[1], vCo[2], w);
3515
3516 if (do_clip && ED_view3d_clipping_test(ps->rv3d, wco, true)) {
3517 /* Watch out that no code below
3518 * this needs to run */
3519 continue;
3520 }
3521 }
3522
3523 mask = project_paint_uvpixel_mask(ps, tri_index, w);
3524
3525 if (mask > 0.0f) {
3527 bucketPixelNodes,
3529 ps, arena, &tinf, x, y, mask, tri_index, pixelScreenCo, wco, w),
3530 arena);
3531 }
3532 }
3533 }
3534 else if (has_x_isect) {
3535 /* assuming the face is not a bow-tie - we know
3536 * we can't intersect again on the X */
3537 break;
3538 }
3539 }
3540
3541# if 0 /* TODO: investigate why this doesn't work sometimes! it should! */
3542 /* no intersection for this entire row,
3543 * after some intersection above means we can quit now */
3544 if (has_x_isect == 0 && has_isect) {
3545 break;
3546 }
3547# endif
3548 }
3549 }
3550 }
3551 }
3552 }
3553 }
3554 }
3555#else
3556 UNUSED_VARS(vCo, threaded);
3557#endif /* PROJ_DEBUG_NOSEAMBLEED */
3558}
3559
3565 const float min[2],
3566 const float max[2],
3567 int bucketMin[2],
3568 int bucketMax[2])
3569{
3570 /* divide by bucketWidth & bucketHeight so the bounds are offset in bucket grid units */
3571
3572 /* XXX(jwilkins ): the offset of 0.5 is always truncated to zero and the offset of 1.5f
3573 * is always truncated to 1, is this really correct? */
3574
3575 /* these offsets of 0.5 and 1.5 seem odd but they are correct */
3576 bucketMin[0] = int(int((float(min[0] - ps->screenMin[0]) / ps->screen_width) * ps->buckets_x) +
3577 0.5f);
3578 bucketMin[1] = int(int((float(min[1] - ps->screenMin[1]) / ps->screen_height) * ps->buckets_y) +
3579 0.5f);
3580
3581 bucketMax[0] = int(int((float(max[0] - ps->screenMin[0]) / ps->screen_width) * ps->buckets_x) +
3582 1.5f);
3583 bucketMax[1] = int(int((float(max[1] - ps->screenMin[1]) / ps->screen_height) * ps->buckets_y) +
3584 1.5f);
3585
3586 /* in case the rect is outside the mesh 2d bounds */
3587 CLAMP(bucketMin[0], 0, ps->buckets_x);
3588 CLAMP(bucketMin[1], 0, ps->buckets_y);
3589
3590 CLAMP(bucketMax[0], 0, ps->buckets_x);
3591 CLAMP(bucketMax[1], 0, ps->buckets_y);
3592}
3593
3594/* set bucket_bounds to a screen space-aligned floating point bound-box */
3596 const int bucket_x,
3597 const int bucket_y,
3598 rctf *r_bucket_bounds)
3599{
3600 /* left */
3601 r_bucket_bounds->xmin = (ps->screenMin[0] + ((bucket_x) * (ps->screen_width / ps->buckets_x)));
3602 /* right */
3603 r_bucket_bounds->xmax = (ps->screenMin[0] +
3604 ((bucket_x + 1) * (ps->screen_width / ps->buckets_x)));
3605
3606 /* bottom */
3607 r_bucket_bounds->ymin = (ps->screenMin[1] + ((bucket_y) * (ps->screen_height / ps->buckets_y)));
3608 /* top */
3609 r_bucket_bounds->ymax = (ps->screenMin[1] +
3610 ((bucket_y + 1) * (ps->screen_height / ps->buckets_y)));
3611}
3612
3613/* Fill this bucket with pixels from the faces that intersect it.
3614 *
3615 * have bucket_bounds as an argument so we don't need to give bucket_x/y the rect function needs */
3617 const int thread_index,
3618 const int bucket_index,
3619 const rctf *clip_rect,
3620 const rctf *bucket_bounds)
3621{
3622 LinkNode *node;
3623 int tri_index, image_index = 0;
3624 ImBuf *ibuf = nullptr;
3625 Image *tpage_last = nullptr, *tpage;
3626 int tile_last = 0;
3627
3628 if (ps->image_tot == 1) {
3629 /* Simple loop, no context switching */
3630 ibuf = ps->projImages[0].ibuf;
3631
3632 for (node = ps->bucketFaces[bucket_index]; node; node = node->next) {
3634 thread_index,
3635 bucket_index,
3636 POINTER_AS_INT(node->link),
3637 0,
3638 clip_rect,
3639 bucket_bounds,
3640 ibuf);
3641 }
3642 }
3643 else {
3644
3645 /* More complicated loop, switch between images */
3646 for (node = ps->bucketFaces[bucket_index]; node; node = node->next) {
3647 tri_index = POINTER_AS_INT(node->link);
3648
3649 const int3 &tri = ps->corner_tris_eval[tri_index];
3650 const int face_i = ps->corner_tri_faces_eval[tri_index];
3651 const float *tri_uv[3] = {PS_CORNER_TRI_AS_UV_3(ps->poly_to_loop_uv, face_i, tri)};
3652
3653 /* Image context switching */
3654 tpage = project_paint_face_paint_image(ps, tri_index);
3655 int tile = project_paint_face_paint_tile(tpage, tri_uv[0]);
3656 if (tpage_last != tpage || tile_last != tile) {
3657 tpage_last = tpage;
3658 tile_last = tile;
3659
3660 ibuf = nullptr;
3661 for (image_index = 0; image_index < ps->image_tot; image_index++) {
3662 ProjPaintImage *projIma = &ps->projImages[image_index];
3663 if ((projIma->ima == tpage) && (projIma->iuser.tile == tile)) {
3664 ibuf = projIma->ibuf;
3665 break;
3666 }
3667 }
3668 BLI_assert(ibuf != nullptr);
3669 }
3670 /* context switching done */
3671
3673 ps, thread_index, bucket_index, tri_index, image_index, clip_rect, bucket_bounds, ibuf);
3674 }
3675 }
3676
3677 ps->bucketFlags[bucket_index] |= PROJ_BUCKET_INIT;
3678}
3679
3680/* We want to know if a bucket and a face overlap in screen-space.
3681 *
3682 * NOTE: if this ever returns false positives its not that bad, since a face in the bounding area
3683 * will have its pixels calculated when it might not be needed later, (at the moment at least)
3684 * obviously it shouldn't have bugs though. */
3685
3687 int bucket_x,
3688 int bucket_y,
3689 const int3 &tri)
3690{
3691 /* TODO: replace this with a trickier method that uses side-of-line for all
3692 * #ProjPaintState.screenCoords edges against the closest bucket corner. */
3693 const int vert_tri[3] = {PS_CORNER_TRI_AS_VERT_INDEX_3(ps, tri)};
3694 rctf bucket_bounds;
3695 float p1[2], p2[2], p3[2], p4[2];
3696 const float *v, *v1, *v2, *v3;
3697 int fidx;
3698
3699 project_bucket_bounds(ps, bucket_x, bucket_y, &bucket_bounds);
3700
3701 /* Is one of the faces verts in the bucket bounds? */
3702
3703 fidx = 2;
3704 do {
3705 v = ps->screenCoords[vert_tri[fidx]];
3706 if (BLI_rctf_isect_pt_v(&bucket_bounds, v)) {
3707 return true;
3708 }
3709 } while (fidx--);
3710
3711 v1 = ps->screenCoords[vert_tri[0]];
3712 v2 = ps->screenCoords[vert_tri[1]];
3713 v3 = ps->screenCoords[vert_tri[2]];
3714
3715 p1[0] = bucket_bounds.xmin;
3716 p1[1] = bucket_bounds.ymin;
3717 p2[0] = bucket_bounds.xmin;
3718 p2[1] = bucket_bounds.ymax;
3719 p3[0] = bucket_bounds.xmax;
3720 p3[1] = bucket_bounds.ymax;
3721 p4[0] = bucket_bounds.xmax;
3722 p4[1] = bucket_bounds.ymin;
3723
3724 if (isect_point_tri_v2(p1, v1, v2, v3) || isect_point_tri_v2(p2, v1, v2, v3) ||
3725 isect_point_tri_v2(p3, v1, v2, v3) || isect_point_tri_v2(p4, v1, v2, v3) ||
3726 /* we can avoid testing v3,v1 because another intersection MUST exist if this intersects */
3727 (isect_seg_seg_v2(p1, p2, v1, v2) || isect_seg_seg_v2(p1, p2, v2, v3)) ||
3728 (isect_seg_seg_v2(p2, p3, v1, v2) || isect_seg_seg_v2(p2, p3, v2, v3)) ||
3729 (isect_seg_seg_v2(p3, p4, v1, v2) || isect_seg_seg_v2(p3, p4, v2, v3)) ||
3730 (isect_seg_seg_v2(p4, p1, v1, v2) || isect_seg_seg_v2(p4, p1, v2, v3)))
3731 {
3732 return true;
3733 }
3734
3735 return false;
3736}
3737
3738/* Add faces to the bucket but don't initialize its pixels
3739 * TODO: when painting occluded, sort the faces on their min-Z
3740 * and only add faces that faces that are not occluded */
3742 const int3 &corner_tri,
3743 const int tri_index)
3744{
3745 const int vert_tri[3] = {PS_CORNER_TRI_AS_VERT_INDEX_3(ps, corner_tri)};
3746 float min[2], max[2], *vCoSS;
3747 /* for ps->bucketRect indexing */
3748 int bucketMin[2], bucketMax[2];
3749 int fidx, bucket_x, bucket_y;
3750 /* for early loop exit */
3751 int has_x_isect = -1, has_isect = 0;
3752 /* just use the first thread arena since threading has not started yet */
3753 MemArena *arena = ps->arena_mt[0];
3754
3756
3757 fidx = 2;
3758 do {
3759 vCoSS = ps->screenCoords[vert_tri[fidx]];
3760 minmax_v2v2_v2(min, max, vCoSS);
3761 } while (fidx--);
3762
3763 project_paint_bucket_bounds(ps, min, max, bucketMin, bucketMax);
3764
3765 for (bucket_y = bucketMin[1]; bucket_y < bucketMax[1]; bucket_y++) {
3766 has_x_isect = 0;
3767 for (bucket_x = bucketMin[0]; bucket_x < bucketMax[0]; bucket_x++) {
3768 if (project_bucket_face_isect(ps, bucket_x, bucket_y, corner_tri)) {
3769 int bucket_index = bucket_x + (bucket_y * ps->buckets_x);
3770 BLI_linklist_prepend_arena(&ps->bucketFaces[bucket_index],
3771 /* cast to a pointer to shut up the compiler */
3772 POINTER_FROM_INT(tri_index),
3773 arena);
3774
3775 has_x_isect = has_isect = 1;
3776 }
3777 else if (has_x_isect) {
3778 /* assuming the face is not a bow-tie - we know we can't intersect again on the X */
3779 break;
3780 }
3781 }
3782
3783 /* no intersection for this entire row,
3784 * after some intersection above means we can quit now */
3785 if (has_x_isect == 0 && has_isect) {
3786 break;
3787 }
3788 }
3789
3790#ifndef PROJ_DEBUG_NOSEAMBLEED
3791 if (ps->seam_bleed_px > 0.0f) {
3792 /* set as uninitialized */
3793 ps->loopSeamData[corner_tri[0]].seam_uvs[0][0] = FLT_MAX;
3794 ps->loopSeamData[corner_tri[1]].seam_uvs[0][0] = FLT_MAX;
3795 ps->loopSeamData[corner_tri[2]].seam_uvs[0][0] = FLT_MAX;
3796 }
3797#endif
3798}
3799
3800static void proj_paint_state_viewport_init(ProjPaintState *ps, const char symmetry_flag)
3801{
3802 float mat[3][3];
3803 float viewmat[4][4];
3804 float viewinv[4][4];
3805
3806 ps->viewDir[0] = 0.0f;
3807 ps->viewDir[1] = 0.0f;
3808 ps->viewDir[2] = 1.0f;
3809
3810 copy_m4_m4(ps->obmat, ps->ob->object_to_world().ptr());
3811
3812 if (symmetry_flag) {
3813 int i;
3814 for (i = 0; i < 3; i++) {
3815 if ((symmetry_flag >> i) & 1) {
3816 negate_v3(ps->obmat[i]);
3817 ps->is_flip_object = !ps->is_flip_object;
3818 }
3819 }
3820 }
3821
3822 invert_m4_m4(ps->obmat_imat, ps->obmat);
3823
3825 /* normal drawing */
3826 ps->winx = ps->region->winx;
3827 ps->winy = ps->region->winy;
3828
3829 copy_m4_m4(viewmat, ps->rv3d->viewmat);
3830 copy_m4_m4(viewinv, ps->rv3d->viewinv);
3831
3833 copy_m4_m4(ps->projectMat, projection.ptr());
3834
3836 ps->depsgraph, ps->v3d, ps->rv3d, true, &ps->clip_start, &ps->clip_end);
3837 }
3838 else {
3839 /* re-projection */
3840 float winmat[4][4];
3841 float vmat[4][4];
3842
3843 ps->winx = ps->reproject_ibuf->x;
3844 ps->winy = ps->reproject_ibuf->y;
3845
3846 if (ps->source == PROJ_SRC_IMAGE_VIEW) {
3847 /* image stores camera data, tricky */
3848 IDProperty *idgroup = IDP_GetProperties(&ps->reproject_image->id);
3850
3851 const float *array = IDP_array_float_get(view_data);
3852
3853 /* use image array, written when creating image */
3854 memcpy(winmat, array, sizeof(winmat));
3855 array += sizeof(winmat) / sizeof(float);
3856 memcpy(viewmat, array, sizeof(viewmat));
3857 array += sizeof(viewmat) / sizeof(float);
3858 ps->clip_start = array[0];
3859 ps->clip_end = array[1];
3860 ps->is_ortho = bool(array[2]);
3861
3862 invert_m4_m4(viewinv, viewmat);
3863 }
3864 else if (ps->source == PROJ_SRC_IMAGE_CAM) {
3865 Object *cam_ob_eval = DEG_get_evaluated(ps->depsgraph, ps->scene->camera);
3866 CameraParams params;
3867
3868 /* viewmat & viewinv */
3869 copy_m4_m4(viewinv, cam_ob_eval->object_to_world().ptr());
3870 normalize_m4(viewinv);
3871 invert_m4_m4(viewmat, viewinv);
3872
3873 /* window matrix, clipping and ortho */
3874 BKE_camera_params_init(&params);
3875 BKE_camera_params_from_object(&params, cam_ob_eval);
3876 BKE_camera_params_compute_viewplane(&params, ps->winx, ps->winy, 1.0f, 1.0f);
3878
3879 copy_m4_m4(winmat, params.winmat);
3880 ps->clip_start = params.clip_start;
3881 ps->clip_end = params.clip_end;
3882 ps->is_ortho = params.is_ortho;
3883 }
3884 else {
3885 BLI_assert(0);
3886 }
3887
3888 /* same as #ED_view3d_ob_project_mat_get */
3889 mul_m4_m4m4(vmat, viewmat, ps->obmat);
3890 mul_m4_m4m4(ps->projectMat, winmat, vmat);
3891 }
3892
3894
3895 /* viewDir - object relative */
3896 copy_m3_m4(mat, viewinv);
3897 mul_m3_v3(mat, ps->viewDir);
3898 copy_m3_m4(mat, ps->obmat_imat);
3899 mul_m3_v3(mat, ps->viewDir);
3900 normalize_v3(ps->viewDir);
3901
3902 if (ps->is_flip_object) [[unlikely]] {
3903 negate_v3(ps->viewDir);
3904 }
3905
3906 /* viewPos - object relative */
3907 copy_v3_v3(ps->viewPos, viewinv[3]);
3908 copy_m3_m4(mat, ps->obmat_imat);
3909 mul_m3_v3(mat, ps->viewPos);
3910 add_v3_v3(ps->viewPos, ps->obmat_imat[3]);
3911}
3912
3913static void proj_paint_state_screen_coords_init(ProjPaintState *ps, const int diameter)
3914{
3915 float *projScreenCo;
3916 float projMargin;
3917 int a;
3918
3920
3922 "ProjectPaint ScreenVerts");
3923 projScreenCo = *ps->screenCoords;
3924
3925 if (ps->is_ortho) {
3926 for (a = 0; a < ps->totvert_eval; a++, projScreenCo += 4) {
3927 mul_v3_m4v3(projScreenCo, ps->projectMat, ps->vert_positions_eval[a]);
3928
3929 /* screen space, not clamped */
3930 projScreenCo[0] = float(ps->winx * 0.5f) + (ps->winx * 0.5f) * projScreenCo[0];
3931 projScreenCo[1] = float(ps->winy * 0.5f) + (ps->winy * 0.5f) * projScreenCo[1];
3932 minmax_v2v2_v2(ps->screenMin, ps->screenMax, projScreenCo);
3933 }
3934 }
3935 else {
3936 for (a = 0; a < ps->totvert_eval; a++, projScreenCo += 4) {
3937 copy_v3_v3(projScreenCo, ps->vert_positions_eval[a]);
3938 projScreenCo[3] = 1.0f;
3939
3940 mul_m4_v4(ps->projectMat, projScreenCo);
3941
3942 if (projScreenCo[3] > ps->clip_start) {
3943 /* screen space, not clamped */
3944 projScreenCo[0] = float(ps->winx * 0.5f) +
3945 (ps->winx * 0.5f) * projScreenCo[0] / projScreenCo[3];
3946 projScreenCo[1] = float(ps->winy * 0.5f) +
3947 (ps->winy * 0.5f) * projScreenCo[1] / projScreenCo[3];
3948 /* Use the depth for bucket point occlusion */
3949 projScreenCo[2] = projScreenCo[2] / projScreenCo[3];
3950 minmax_v2v2_v2(ps->screenMin, ps->screenMax, projScreenCo);
3951 }
3952 else {
3953 /* TODO: deal with cases where 1 side of a face goes behind the view ?
3954 *
3955 * After some research this is actually very tricky, only option is to
3956 * clip the derived mesh before painting, which is a Pain */
3957 projScreenCo[0] = FLT_MAX;
3958 }
3959 }
3960 }
3961
3962 /* If this border is not added we get artifacts for faces that
3963 * have a parallel edge and at the bounds of the 2D projected verts eg
3964 * - a single screen aligned quad */
3965 projMargin = (ps->screenMax[0] - ps->screenMin[0]) * 0.000001f;
3966 ps->screenMax[0] += projMargin;
3967 ps->screenMin[0] -= projMargin;
3968 projMargin = (ps->screenMax[1] - ps->screenMin[1]) * 0.000001f;
3969 ps->screenMax[1] += projMargin;
3970 ps->screenMin[1] -= projMargin;
3971
3972 if (ps->source == PROJ_SRC_VIEW) {
3973#ifdef PROJ_DEBUG_WINCLIP
3974 CLAMP(ps->screenMin[0], float(-diameter), float(ps->winx + diameter));
3975 CLAMP(ps->screenMax[0], float(-diameter), float(ps->winx + diameter));
3976
3977 CLAMP(ps->screenMin[1], float(-diameter), float(ps->winy + diameter));
3978 CLAMP(ps->screenMax[1], float(-diameter), float(ps->winy + diameter));
3979#else
3980 UNUSED_VARS(diameter);
3981#endif
3982 }
3983 else if (ps->source != PROJ_SRC_VIEW_FILL) { /* re-projection, use bounds */
3984 ps->screenMin[0] = 0;
3985 ps->screenMax[0] = float(ps->winx);
3986
3987 ps->screenMin[1] = 0;
3988 ps->screenMax[1] = float(ps->winy);
3989 }
3990}
3991
3993{
3994 float *cavities;
3995 int a;
3996
3997 if (ps->do_mask_cavity) {
3998 int *counter = MEM_new_array_zeroed<int>(ps->totvert_eval, "counter");
3999 float (*edges)[3] = MEM_new_array_zeroed<float[3]>(ps->totvert_eval, "edges");
4000 ps->cavities = MEM_new_array_uninitialized<float>(ps->totvert_eval, "ProjectPaint Cavities");
4001 cavities = ps->cavities;
4002
4003 for (const int64_t i : ps->edges_eval.index_range()) {
4004 const int2 &edge = ps->edges_eval[i];
4005 float e[3];
4006 sub_v3_v3v3(e, ps->vert_positions_eval[edge[0]], ps->vert_positions_eval[edge[1]]);
4007 normalize_v3(e);
4008 add_v3_v3(edges[edge[1]], e);
4009 counter[edge[1]]++;
4010 sub_v3_v3(edges[edge[0]], e);
4011 counter[edge[0]]++;
4012 }
4013 for (a = 0; a < ps->totvert_eval; a++) {
4014 if (counter[a] > 0) {
4015 mul_v3_fl(edges[a], 1.0f / counter[a]);
4016 /* Augment the difference. */
4017 cavities[a] = safe_acosf(10.0f * dot_v3v3(ps->vert_normals[a], edges[a])) * float(M_1_PI);
4018 }
4019 else {
4020 cavities[a] = 0.0;
4021 }
4022 }
4023
4024 MEM_delete(counter);
4025 MEM_delete(edges);
4026 }
4027}
4028
4029#ifndef PROJ_DEBUG_NOSEAMBLEED
4031{
4032 if (ps->seam_bleed_px > 0.0f) {
4033 ps->vertFaces = MEM_new_array_zeroed<LinkNode *>(ps->totvert_eval, "paint-vertFaces");
4037 "paint-loopSeamUVs");
4039 }
4040}
4041#endif
4042
4043static void proj_paint_state_thread_init(ProjPaintState *ps, const bool reset_threads)
4044{
4045 /* Thread stuff
4046 *
4047 * very small brushes run a lot slower multi-threaded since the advantage with
4048 * threads is being able to fill in multiple buckets at once.
4049 * Only use threads for bigger brushes. */
4050
4052
4053 /* workaround for #35057, disable threading if diameter is less than is possible for
4054 * optimum bucket number generation */
4055 if (reset_threads) {
4056 ps->thread_tot = 1;
4057 }
4058
4059 if (ps->is_shared_user == false) {
4060 if (ps->thread_tot > 1) {
4061 ps->tile_lock = MEM_new_uninitialized<SpinLock>("projpaint_tile_lock");
4063 }
4064 }
4065
4066 for (int a = 0; a < ps->thread_tot; a++) {
4067 ps->arena_mt[a] = BLI_memarena_new(MEM_SIZE_OPTIMAL(1 << 16), "project paint arena");
4068 }
4069}
4070
4072{
4073 if (ps->do_backfacecull && ps->do_mask_normal) {
4074 float viewDirPersp[3];
4075 float no[3];
4076 int a;
4077
4078 ps->vertFlags = MEM_new_array_zeroed<char>(ps->totvert_eval, "paint-vertFlags");
4079
4080 for (a = 0; a < ps->totvert_eval; a++) {
4081 copy_v3_v3(no, ps->vert_normals[a]);
4082 if (ps->is_flip_object) [[unlikely]] {
4083 negate_v3(no);
4084 }
4085
4086 if (ps->is_ortho) {
4087 if (dot_v3v3(ps->viewDir, no) <= ps->normal_angle__cos) {
4088 /* 1 vert of this face is towards us */
4089 ps->vertFlags[a] |= PROJ_VERT_CULL;
4090 }
4091 }
4092 else {
4093 sub_v3_v3v3(viewDirPersp, ps->viewPos, ps->vert_positions_eval[a]);
4094 normalize_v3(viewDirPersp);
4095 if (ps->is_flip_object) [[unlikely]] {
4096 negate_v3(viewDirPersp);
4097 }
4098 if (dot_v3v3(viewDirPersp, no) <= ps->normal_angle__cos) {
4099 /* 1 vert of this face is towards us */
4100 ps->vertFlags[a] |= PROJ_VERT_CULL;
4101 }
4102 }
4103 }
4104 }
4105 else {
4106 ps->vertFlags = nullptr;
4107 }
4108}
4109
4110#ifndef PROJ_DEBUG_NOSEAMBLEED
4112 MemArena *arena,
4113 const int3 &corner_tri,
4114 const int tri_index)
4115{
4116 /* add face user if we have bleed enabled, set the UV seam flags later */
4117 /* annoying but we need to add all faces even ones we never use elsewhere */
4118 if (ps->seam_bleed_px > 0.0f) {
4119 const int face_i = ps->corner_tri_faces_eval[tri_index];
4120 const float *tri_uv[3];
4121 if (!ps->poly_to_loop_uv_single) {
4122 PS_CORNER_TRI_ASSIGN_UV_3(tri_uv, ps->poly_to_loop_uv, face_i, corner_tri);
4123 }
4124
4125 /* Check for degenerate triangles. Degenerate faces cause trouble with bleed computations.
4126 * Ideally this would be checked later, not to add to the cost of computing non-degenerate
4127 * triangles, but that would allow other triangles to still find adjacent seams on degenerate
4128 * triangles, potentially causing incorrect results. */
4129 float area_tri = 0.0f;
4130 if (!ps->poly_to_loop_uv_single) {
4131 area_tri = area_tri_v2(UNPACK3(tri_uv));
4132 }
4133 if (area_tri > 0.0f) {
4134 const int vert_tri[3] = {PS_CORNER_TRI_AS_VERT_INDEX_3(ps, corner_tri)};
4135 void *tri_index_p = POINTER_FROM_INT(tri_index);
4136
4137 BLI_linklist_prepend_arena(&ps->vertFaces[vert_tri[0]], tri_index_p, arena);
4138 BLI_linklist_prepend_arena(&ps->vertFaces[vert_tri[1]], tri_index_p, arena);
4139 BLI_linklist_prepend_arena(&ps->vertFaces[vert_tri[2]], tri_index_p, arena);
4140 }
4141 else {
4142 ps->faceSeamFlags[tri_index] |= PROJ_FACE_DEGENERATE;
4143 }
4144 }
4145}
4146#endif
4147
4148/* Return true if evaluated mesh can be painted on, false otherwise */
4150{
4151 Depsgraph *depsgraph = CTX_data_ensure_evaluated_depsgraph(C);
4152 Object *ob = ps->ob;
4153
4154 const Object *ob_eval = DEG_get_evaluated(depsgraph, ob);
4156 if (!ps->mesh_eval) {
4157 return false;
4158 }
4159
4160 if (ps->mesh_eval->uv_map_names().is_empty()) {
4161 ps->mesh_eval = nullptr;
4162 return false;
4163 }
4164
4165 /* Build final material array, we use this a lot here. */
4166 /* materials start from 1, default material is 0 */
4167 const int totmat = ob->totcol + 1;
4168 ps->mat_array = MEM_new_array_uninitialized<Material *>(totmat, __func__);
4169 /* We leave last material as empty - rationale here is being able to index
4170 * the materials by using the mf->mat_nr directly and leaving the last
4171 * material as nullptr in case no materials exist on mesh, so indexing will not fail. */
4172 for (int i = 0; i < totmat - 1; i++) {
4173 ps->mat_array[i] = BKE_object_material_get(ob, i + 1);
4174 }
4175 ps->mat_array[totmat - 1] = nullptr;
4176
4177 ps->vert_positions_eval = ps->mesh_eval->vert_positions();
4178 ps->vert_normals = ps->mesh_eval->vert_normals();
4179 ps->corner_normals_eval = ps->mesh_eval->corner_normals();
4180 ps->edges_eval = ps->mesh_eval->edges();
4181 ps->faces_eval = ps->mesh_eval->faces();
4182 ps->corner_verts_eval = ps->mesh_eval->corner_verts();
4183 ps->select_poly_eval = nullptr;
4184 ps->hide_poly_eval = nullptr;
4185 ps->material_indices = nullptr;
4186 const bke::AttributeAccessor attributes = ps->mesh_eval->attributes();
4187 if (const bke::GAttributeReader attr = attributes.lookup(".select_poly")) {
4188 if (attr.domain == bke::AttrDomain::Face && attr.varray.type().is<bool>()) {
4189 if (attr.varray.is_single()) {
4190 ps->select_poly_single = attr.varray.typed<bool>().get_internal_single();
4191 }
4192 else if (attr.varray.is_span()) {
4193 ps->select_poly_eval = attr.varray.get_internal_span().typed<bool>().data();
4194 }
4195 }
4196 }
4197 if (const bke::GAttributeReader attr = attributes.lookup(".hide_poly")) {
4198 if (attr.domain == bke::AttrDomain::Face && attr.varray.type().is<bool>()) {
4199 if (attr.varray.is_single()) {
4200 ps->hide_poly_single = attr.varray.typed<bool>().get_internal_single();
4201 }
4202 else if (attr.varray.is_span()) {
4203 ps->hide_poly_eval = attr.varray.get_internal_span().typed<bool>().data();
4204 }
4205 }
4206 }
4207 if (const bke::GAttributeReader attr = attributes.lookup("material_index")) {
4208 if (attr.domain == bke::AttrDomain::Face && attr.varray.type().is<int>()) {
4209 if (attr.varray.is_single()) {
4210 ps->material_index_single = attr.varray.typed<int>().get_internal_single();
4211 }
4212 else if (attr.varray.is_span()) {
4213 ps->material_indices = attr.varray.get_internal_span().typed<int>().data();
4214 }
4215 }
4216 }
4217
4218 ps->totvert_eval = ps->mesh_eval->verts_num;
4221
4222 ps->corner_tris_eval = ps->mesh_eval->corner_tris();
4223 ps->corner_tri_faces_eval = ps->mesh_eval->corner_tri_faces();
4224
4226 "proj_paint_mtfaces");
4227
4228 return true;
4229}
4230
4236
4238{
4239 const Mesh &mesh_orig = *id_cast<const Mesh *>(ps->ob->data);
4240 const bke::AttributeAccessor attributes = ps->mesh_eval->attributes();
4241 const float2 *uv_map_clone_base = nullptr;
4242
4243 /* use clone mtface? */
4244 if (ps->do_layer_clone) {
4246 "proj_paint_mtfaces");
4247
4248 if (const bke::GAttributeReader attr = attributes.lookup(mesh_orig.clone_uv_map_attribute)) {
4249 if (attr.domain == bke::AttrDomain::Corner && attr.varray.type().is<float2>()) {
4250 if (attr.varray.is_span()) {
4251 uv_map_clone_base = attr.varray.get_internal_span().typed<float2>().data();
4252 }
4253 }
4254 }
4255
4256 if (uv_map_clone_base == nullptr) {
4257 /* get active instead */
4258 if (const bke::GAttributeReader attr = attributes.lookup(mesh_orig.active_uv_map_name())) {
4259 if (attr.domain == bke::AttrDomain::Corner && attr.varray.type().is<float2>()) {
4260 if (attr.varray.is_span()) {
4261 uv_map_clone_base = attr.varray.get_internal_span().typed<float2>().data();
4262 }
4263 }
4264 }
4265 }
4266 }
4267
4268 memset(layer_clone, 0, sizeof(*layer_clone));
4269 layer_clone->uv_map_clone_base = uv_map_clone_base;
4270}
4271
4272/* Return true if face should be skipped, false otherwise */
4275 const TexPaintSlot *slot,
4276 const int tri_index)
4277{
4278 const bke::AttributeAccessor attributes = ps->mesh_eval->attributes();
4279 const StringRef active_uv_map_name = ps->mesh_eval->active_uv_map_name();
4280 if (ps->do_layer_clone) {
4281 if (ps->do_material_slots) {
4282 lc->slot_clone = project_paint_face_clone_slot(ps, tri_index);
4283 /* all faces should have a valid slot, reassert here */
4284 if (ELEM(lc->slot_clone, nullptr, slot)) {
4285 return true;
4286 }
4287 }
4288 else if (ps->clone_ima == ps->canvas_ima) {
4289 return true;
4290 }
4291
4292 if (ps->do_material_slots) {
4293 if (lc->slot_clone != lc->slot_last_clone) {
4294 if (lc->slot_clone->uvname) {
4295 if (const bke::GAttributeReader attr = attributes.lookup(lc->slot_clone->uvname)) {
4296 if (attr.domain == bke::AttrDomain::Corner && attr.varray.type().is<float2>()) {
4297 if (attr.varray.is_span()) {
4298 lc->uv_map_clone_base = attr.varray.get_internal_span().typed<float2>().data();
4299 }
4300 }
4301 }
4302 }
4303 if (!lc->uv_map_clone_base) {
4304 if (const bke::GAttributeReader attr = attributes.lookup(active_uv_map_name)) {
4305 if (attr.domain == bke::AttrDomain::Corner && attr.varray.type().is<float2>()) {
4306 if (attr.varray.is_span()) {
4307 lc->uv_map_clone_base = attr.varray.get_internal_span().typed<float2>().data();
4308 }
4309 }
4310 }
4311 }
4312 lc->slot_last_clone = lc->slot_clone;
4313 }
4314 }
4315
4316 /* will set multiple times for 4+ sided poly */
4318 }
4319 return false;
4320}
4321
4327
4329{
4330 *face_lookup = {};
4331 Mesh *orig_mesh = id_cast<Mesh *>(ps->ob->data);
4332 face_lookup->index_mp_to_orig = static_cast<const int *>(
4334 const bke::AttributeAccessor attributes = orig_mesh->attributes();
4335 if (ps->do_face_sel) {
4336 face_lookup->select_poly_orig = *attributes.lookup_or_default<bool>(
4337 ".select_poly", bke::AttrDomain::Face, false);
4338 }
4339 face_lookup->hide_poly_orig = *attributes.lookup_or_default<bool>(
4340 ".hide_poly", bke::AttrDomain::Face, false);
4341}
4342
4343/* Return true if face should be considered paintable, false otherwise */
4345 const ProjPaintFaceLookup *face_lookup,
4346 const int tri_i)
4347{
4348 if (ps->do_face_sel) {
4349 int orig_index;
4350 const int face_i = ps->corner_tri_faces_eval[tri_i];
4351 if ((face_lookup->index_mp_to_orig != nullptr) &&
4352 ((orig_index = (face_lookup->index_mp_to_orig[face_i])) != ORIGINDEX_NONE))
4353 {
4354 return face_lookup->select_poly_orig[orig_index];
4355 }
4356 if (ps->select_poly_single) {
4357 return ps->select_poly_single.value();
4358 }
4359 if (ps->select_poly_eval) {
4360 return ps->select_poly_eval[face_i];
4361 }
4362 return false;
4363 }
4364 int orig_index;
4365 const int face_i = ps->corner_tri_faces_eval[tri_i];
4366 if ((face_lookup->index_mp_to_orig != nullptr) &&
4367 ((orig_index = (face_lookup->index_mp_to_orig[face_i])) != ORIGINDEX_NONE))
4368 {
4369 return !face_lookup->hide_poly_orig[orig_index];
4370 }
4371 if (ps->hide_poly_single) {
4372 return !ps->hide_poly_single.value();
4373 }
4374 if (ps->hide_poly_eval) {
4375 return !ps->hide_poly_eval[face_i];
4376 }
4377 return true;
4378}
4379
4381 const float *v1;
4382 const float *v2;
4383 const float *v3;
4384};
4385
4387 const int3 &corner_tri,
4388 ProjPaintFaceCoSS *coSS)
4389{
4390 const int vert_tri[3] = {PS_CORNER_TRI_AS_VERT_INDEX_3(ps, corner_tri)};
4391 coSS->v1 = ps->screenCoords[vert_tri[0]];
4392 coSS->v2 = ps->screenCoords[vert_tri[1]];
4393 coSS->v3 = ps->screenCoords[vert_tri[2]];
4394}
4395
4396/* Return true if face should be culled, false otherwise */
4398{
4399 if (!ps->is_ortho) {
4400 if (coSS->v1[0] == FLT_MAX || coSS->v2[0] == FLT_MAX || coSS->v3[0] == FLT_MAX) {
4401 return true;
4402 }
4403 }
4404 return false;
4405}
4406
4407#ifdef PROJ_DEBUG_WINCLIP
4408/* Return true if face should be culled, false otherwise */
4409static bool project_paint_winclip(const ProjPaintState *ps, const ProjPaintFaceCoSS *coSS)
4410{
4411 /* ignore faces outside the view */
4412 return ((ps->source != PROJ_SRC_VIEW_FILL) &&
4413 ((coSS->v1[0] < ps->screenMin[0] && coSS->v2[0] < ps->screenMin[0] &&
4414 coSS->v3[0] < ps->screenMin[0]) ||
4415
4416 (coSS->v1[0] > ps->screenMax[0] && coSS->v2[0] > ps->screenMax[0] &&
4417 coSS->v3[0] > ps->screenMax[0]) ||
4418
4419 (coSS->v1[1] < ps->screenMin[1] && coSS->v2[1] < ps->screenMin[1] &&
4420 coSS->v3[1] < ps->screenMin[1]) ||
4421
4422 (coSS->v1[1] > ps->screenMax[1] && coSS->v2[1] > ps->screenMax[1] &&
4423 coSS->v3[1] > ps->screenMax[1])));
4424}
4425#endif /* PROJ_DEBUG_WINCLIP */
4426
4432
4434 MemArena *arena,
4435 ListBaseT<PrepareImageEntry> *used_images)
4436{
4437 ProjPaintImage *projIma;
4438 PrepareImageEntry *entry;
4439
4440 /* build an array of images we use */
4441 projIma = ps->projImages = static_cast<ProjPaintImage *>(
4442 BLI_memarena_alloc(arena, sizeof(ProjPaintImage) * ps->image_tot));
4443
4444 for (entry = used_images->first(); entry; entry = entry->next, projIma++) {
4445 projIma->iuser = entry->iuser;
4446 int size;
4447 projIma->ima = entry->ima;
4448 projIma->touch = false;
4449 projIma->ibuf = BKE_image_acquire_ibuf(projIma->ima, &projIma->iuser, nullptr);
4450 if (projIma->ibuf == nullptr) {
4451 projIma->iuser.tile = 0;
4452 projIma->ibuf = BKE_image_acquire_ibuf(projIma->ima, &projIma->iuser, nullptr);
4453 BLI_assert(projIma->ibuf != nullptr);
4454 }
4455 size = sizeof(void **) * ED_IMAGE_UNDO_TILE_NUMBER(projIma->ibuf->x) *
4456 ED_IMAGE_UNDO_TILE_NUMBER(projIma->ibuf->y);
4457 projIma->partRedrawRect = static_cast<ImagePaintPartialRedraw *>(
4460 projIma->undoRect = static_cast<volatile const void **>(BLI_memarena_alloc(arena, size));
4461 memset(static_cast<void *>(projIma->undoRect), 0, size);
4462 projIma->maskRect = static_cast<ushort **>(BLI_memarena_alloc(arena, size));
4463 memset(projIma->maskRect, 0, size);
4464 projIma->valid = static_cast<bool **>(BLI_memarena_alloc(arena, size));
4465 memset(projIma->valid, 0, size);
4466 }
4467}
4468
4471 Image *ima = nullptr;
4472 int tile = -1;
4473 int image_index = -1;
4474};
4475
4481 ListBaseT<PrepareImageEntry> &used_images,
4482 Image *tpage,
4483 const int tile,
4485{
4486 if (state.ima == tpage && state.tile == tile) {
4487 return state.image_index;
4488 }
4489
4490 int image_index = 0;
4491 for (PrepareImageEntry *e = used_images.first(); e; e = e->next, image_index++) {
4492 if (e->ima == tpage && e->iuser.tile == tile) {
4493 break;
4494 }
4495 }
4496
4497 if (image_index == ps->image_tot) {
4498 /* XXX get appropriate ImageUser instead */
4499 ImageUser iuser;
4500 BKE_imageuser_default(&iuser);
4501 iuser.tile = tile;
4502 iuser.framenr = tpage->lastframe;
4503 if (BKE_image_has_ibuf(tpage, &iuser)) {
4504 PrepareImageEntry *e = MEM_new<PrepareImageEntry>("PrepareImageEntry");
4505 e->ima = tpage;
4506 e->iuser = iuser;
4507 BLI_addtail(&used_images, e);
4508 ps->image_tot++;
4509 }
4510 else {
4511 image_index = -1;
4512 }
4513 }
4514
4515 state.ima = tpage;
4516 state.tile = tile;
4517 state.image_index = image_index;
4518 return image_index;
4519}
4520
4522 MemArena *arena,
4523 const ProjPaintFaceLookup *face_lookup,
4524 ProjPaintLayerClone *layer_clone,
4525 const float2 *uv_map_base,
4526 const bool is_multi_view)
4527{
4528 const bke::AttributeAccessor attributes = ps->mesh_eval->attributes();
4529 const StringRef active_uv_name = ps->mesh_eval->active_uv_map_name();
4530 /* Image Vars - keep track of images we have used */
4531 ListBaseT<PrepareImageEntry> used_images = {nullptr};
4532
4533 PrepareImageState image_state;
4534 Image *tpage;
4535 TexPaintSlot *slot_last = nullptr;
4536 TexPaintSlot *slot = nullptr;
4537 int tile;
4538 int tri_index;
4539 int prev_poly = -1;
4540 const Span<int3> corner_tris = ps->corner_tris_eval;
4541 const Span<int> tri_faces = ps->corner_tri_faces_eval;
4542
4543 std::optional<float2> single_value;
4544
4545 BLI_assert(ps->image_tot == 0);
4546
4547 for (tri_index = 0; tri_index < ps->corner_tris_eval.size(); tri_index++) {
4548 bool is_face_paintable;
4549 bool skip_tri = false;
4550
4551 is_face_paintable = project_paint_check_face_paintable(ps, face_lookup, tri_index);
4552
4553 if (!ps->do_stencil_brush) {
4554 slot = project_paint_face_paint_slot(ps, tri_index);
4555 /* all faces should have a valid slot, reassert here */
4556 if (slot == nullptr) {
4557 if (const bke::GAttributeReader attr = attributes.lookup(active_uv_name)) {
4558 if (attr.domain == bke::AttrDomain::Corner && attr.varray.type().is<float2>()) {
4559 if (attr.varray.is_span()) {
4560 uv_map_base = attr.varray.get_internal_span().typed<float2>().data();
4561 }
4562 else if (attr.varray.is_single()) {
4563 single_value = attr.varray.typed<float2>().get_internal_single();
4564 }
4565 }
4566 }
4567 tpage = ps->canvas_ima;
4568 }
4569 else {
4570 if (slot != slot_last) {
4571 if (slot->uvname) {
4572 if (const bke::GAttributeReader attr = attributes.lookup(slot->uvname)) {
4573 if (attr.domain == bke::AttrDomain::Corner && attr.varray.type().is<float2>()) {
4574 if (attr.varray.is_span()) {
4575 uv_map_base = attr.varray.get_internal_span().typed<float2>().data();
4576 }
4577 else if (attr.varray.is_single()) {
4578 single_value = attr.varray.typed<float2>().get_internal_single();
4579 }
4580 }
4581 }
4582 }
4583
4584 if (!uv_map_base) {
4585 if (const bke::GAttributeReader attr = attributes.lookup(active_uv_name)) {
4586 if (attr.domain == bke::AttrDomain::Corner && attr.varray.type().is<float2>()) {
4587 if (attr.varray.is_span()) {
4588 uv_map_base = attr.varray.get_internal_span().typed<float2>().data();
4589 }
4590 else if (attr.varray.is_single()) {
4591 single_value = attr.varray.typed<float2>().get_internal_single();
4592 }
4593 }
4594 }
4595 }
4596 slot_last = slot;
4597 }
4598
4599 /* Don't allow painting on linked images. */
4600 if (slot->ima != nullptr &&
4601 (!ID_IS_EDITABLE(slot->ima) || ID_IS_OVERRIDE_LIBRARY(slot->ima)))
4602 {
4603 skip_tri = true;
4604 tpage = nullptr;
4605 }
4606
4607 /* Don't allow using the same image for painting and stenciling. */
4608 if (slot->ima == ps->stencil_ima) {
4609 /* Delay continuing the loop until after loop_uvs and bleed faces are initialized.
4610 * While this shouldn't be used, face-winding reads all faces.
4611 * It's less trouble to set all faces to valid UVs,
4612 * avoiding nullptr checks all over. */
4613 skip_tri = true;
4614 tpage = nullptr;
4615 }
4616 else {
4617 tpage = slot->ima;
4618 }
4619 }
4620 }
4621 else {
4622 tpage = ps->stencil_ima;
4623 }
4624 BLI_assert(uv_map_base != nullptr || single_value.has_value());
4625
4626 if (single_value.has_value()) {
4627 ps->poly_to_loop_uv_single = single_value;
4628 tile = project_paint_face_paint_tile(tpage, single_value.value());
4629 }
4630 else {
4631 ps->poly_to_loop_uv[tri_faces[tri_index]] = uv_map_base;
4632 tile = project_paint_face_paint_tile(tpage, uv_map_base[corner_tris[tri_index][0]]);
4633 }
4634
4635#ifndef PROJ_DEBUG_NOSEAMBLEED
4636 project_paint_bleed_add_face_user(ps, arena, corner_tris[tri_index], tri_index);
4637#endif
4638
4639 if (skip_tri || project_paint_clone_face_skip(ps, layer_clone, slot, tri_index)) {
4640 continue;
4641 }
4642
4643 if (!is_face_paintable || !tpage) {
4644 continue;
4645 }
4646
4647 int image_index = -1;
4648
4649 /* With symmetry, prepare images regardless of culling as `projImages` is shared between
4650 * passes and a face culled in this pass may not be culled in another, see !164840. */
4651 if (is_multi_view) {
4652 image_index = project_paint_prepare_image_index(ps, used_images, tpage, tile, image_state);
4653 }
4654
4655 /* Skip culled faces, these aren't added to `bucketFaces`,
4656 * see #project_paint_delayed_face_init() below. */
4657 {
4658 ProjPaintFaceCoSS coSS;
4659 proj_paint_face_coSS_init(ps, corner_tris[tri_index], &coSS);
4660
4661 if (project_paint_flt_max_cull(ps, &coSS)) {
4662 continue;
4663 }
4664
4665#ifdef PROJ_DEBUG_WINCLIP
4666 if (project_paint_winclip(ps, &coSS)) {
4667 continue;
4668 }
4669
4670#endif // PROJ_DEBUG_WINCLIP
4671
4672 /* Back-face culls individual triangles but mask normal will use face. */
4673 if (ps->do_backfacecull) {
4674 if (ps->do_mask_normal) {
4675 if (prev_poly != tri_faces[tri_index]) {
4676 bool culled = true;
4677 const IndexRange poly = ps->faces_eval[tri_faces[tri_index]];
4678 prev_poly = tri_faces[tri_index];
4679 for (const int corner : poly) {
4680 if (!(ps->vertFlags[ps->corner_verts_eval[corner]] & PROJ_VERT_CULL)) {
4681 culled = false;
4682 break;
4683 }
4684 }
4685
4686 if (culled) {
4687 /* poly loops - 2 is number of triangles for poly,
4688 * but counter gets incremented when continuing, so decrease by 3 */
4689 int poly_tri = poly.size() - 3;
4690 tri_index += poly_tri;
4691 continue;
4692 }
4693 }
4694 }
4695 else {
4696 if ((line_point_side_v2(coSS.v1, coSS.v2, coSS.v3) < 0.0f) != ps->is_flip_object) {
4697 continue;
4698 }
4699 }
4700 }
4701 }
4702
4703 if (!is_multi_view) {
4704 image_index = project_paint_prepare_image_index(ps, used_images, tpage, tile, image_state);
4705 }
4706
4707 if (image_index != -1) {
4708 /* Initialize the faces screen pixels */
4709 /* Add this to a list to initialize later */
4710 project_paint_delayed_face_init(ps, corner_tris[tri_index], tri_index);
4711 }
4712 }
4713
4714 /* Build an array of images we use. */
4715 if (ps->is_shared_user == false) {
4716 project_paint_build_proj_ima(ps, arena, &used_images);
4717 }
4718
4719 /* we have built the array, discard the linked list */
4720 used_images.free_no_destruct();
4721}
4722
4723/* run once per stroke before projection painting */
4724static void project_paint_begin(const bContext *C,
4725 ProjPaintState *ps,
4726 const bool is_multi_view,
4727 const char symmetry_flag)
4728{
4729 ProjPaintLayerClone layer_clone;
4730 ProjPaintFaceLookup face_lookup;
4731 const float2 *uv_map_base = nullptr;
4732
4733 /* At the moment this is just ps->arena_mt[0], but use this to show were not multi-threading. */
4734 MemArena *arena;
4735
4736 const int diameter = BKE_brush_size_get(ps->paint, ps->brush);
4737
4738 bool reset_threads = false;
4739
4740 /* ---- end defines ---- */
4741
4742 if (ps->source == PROJ_SRC_VIEW) {
4743 /* faster clipping lookups */
4744 ED_view3d_clipping_local(ps->rv3d, ps->ob->object_to_world().ptr());
4745 }
4746
4747 const Mesh &mesh_orig = *id_cast<const Mesh *>(ps->ob->data);
4748
4749 ps->do_face_sel = ((mesh_orig.editflag & ME_EDIT_PAINT_FACE_SEL) != 0);
4750 ps->is_flip_object = (ps->ob->transflag & OB_NEG_SCALE) != 0;
4751
4752 /* paint onto the derived mesh */
4753 if (ps->is_shared_user == false) {
4755 return;
4756 }
4757 }
4758
4759 const bke::AttributeAccessor attributes = ps->mesh_eval->attributes();
4760
4761 proj_paint_face_lookup_init(ps, &face_lookup);
4762 proj_paint_layer_clone_init(ps, &layer_clone);
4763
4764 if (ps->do_layer_stencil || ps->do_stencil_brush) {
4765 if (const bke::GAttributeReader attr = attributes.lookup(mesh_orig.stencil_uv_map_attribute)) {
4766 if (attr.domain == bke::AttrDomain::Corner && attr.varray.type().is<float2>()) {
4767 if (attr.varray.is_span()) {
4768 ps->uv_map_stencil_eval = attr.varray.get_internal_span().typed<float2>().data();
4769 }
4770 }
4771 }
4772
4773 if (ps->uv_map_stencil_eval == nullptr) {
4774 /* get active instead */
4775 if (const bke::GAttributeReader attr = attributes.lookup(mesh_orig.active_uv_map_name())) {
4776 if (attr.domain == bke::AttrDomain::Corner && attr.varray.type().is<float2>()) {
4777 if (attr.varray.is_span()) {
4778 ps->uv_map_stencil_eval = attr.varray.get_internal_span().typed<float2>().data();
4779 }
4780 }
4781 }
4782 }
4783
4784 if (ps->do_stencil_brush) {
4785 uv_map_base = ps->uv_map_stencil_eval;
4786 }
4787 }
4788
4789 /* when using sub-surface or multi-resolution,
4790 * mesh-data arrays are thrown away, we need to keep a copy. */
4791 if (ps->is_shared_user == false) {
4793 }
4794
4795 proj_paint_state_viewport_init(ps, symmetry_flag);
4796
4797 /* calculate vert screen coords
4798 * run this early so we can calculate the x/y resolution of our bucket rect */
4800
4801 /* only for convenience */
4802 ps->screen_width = ps->screenMax[0] - ps->screenMin[0];
4803 ps->screen_height = ps->screenMax[1] - ps->screenMin[1];
4804
4805 ps->buckets_x = int(ps->screen_width / (float(diameter) / PROJ_BUCKET_BRUSH_DIV));
4806 ps->buckets_y = int(ps->screen_height / (float(diameter) / PROJ_BUCKET_BRUSH_DIV));
4807
4808 // printf("\tscreenspace bucket division x:%d y:%d\n", ps->buckets_x, ps->buckets_y);
4809
4811 reset_threads = true;
4812 }
4813
4814 /* Really high values could cause problems since it has to allocate a few
4815 * `(ps->buckets_x * ps->buckets_y)` sized arrays. */
4818
4820 "paint-bucketRect");
4822 "paint-bucketFaces");
4823
4825 "paint-bucketFaces");
4826#ifndef PROJ_DEBUG_NOSEAMBLEED
4827 if (ps->is_shared_user == false) {
4829 }
4830#endif
4831
4832 proj_paint_state_thread_init(ps, reset_threads);
4833 arena = ps->arena_mt[0];
4834
4836
4838 ps, arena, &face_lookup, &layer_clone, uv_map_base, is_multi_view);
4839}
4840
4841static void paint_proj_begin_clone(ProjPaintState *ps, const float mouse[2])
4842{
4843 /* setup clone offset */
4845 float projCo[4];
4846 copy_v3_v3(projCo, ps->scene->cursor.location);
4847 mul_m4_v3(ps->obmat_imat, projCo);
4848
4849 projCo[3] = 1.0f;
4850 mul_m4_v4(ps->projectMat, projCo);
4851 ps->cloneOffset[0] = mouse[0] -
4852 (float(ps->winx * 0.5f) + (ps->winx * 0.5f) * projCo[0] / projCo[3]);
4853 ps->cloneOffset[1] = mouse[1] -
4854 (float(ps->winy * 0.5f) + (ps->winy * 0.5f) * projCo[1] / projCo[3]);
4855 }
4856}
4857
4859{
4860 int a;
4861
4862 /* dereference used image buffers */
4863 if (ps->is_shared_user == false) {
4864 ProjPaintImage *projIma;
4865 for (a = 0, projIma = ps->projImages; a < ps->image_tot; a++, projIma++) {
4866 BKE_image_release_ibuf(projIma->ima, projIma->ibuf, nullptr);
4867 DEG_id_tag_update(&projIma->ima->id, 0);
4868 }
4869 }
4870
4871 if (ps->reproject_ibuf_free_float) {
4873 }
4874 if (ps->reproject_ibuf_free_uchar) {
4876 }
4878
4879 MEM_delete(ps->screenCoords);
4880 MEM_delete(ps->bucketRect);
4881 MEM_delete(ps->bucketFaces);
4882 MEM_delete(ps->bucketFlags);
4883
4884 if (ps->is_shared_user == false) {
4885 if (ps->mat_array != nullptr) {
4886 MEM_delete(ps->mat_array);
4887 }
4888
4889 /* must be set for non-shared */
4891 if (ps->poly_to_loop_uv) {
4892 MEM_delete(ps->poly_to_loop_uv);
4893 }
4894
4895 if (ps->do_layer_clone) {
4896 MEM_delete(ps->poly_to_loop_uv_clone);
4897 }
4898 if (ps->thread_tot > 1) {
4900 /* The void cast is needed when building without TBB. */
4901 MEM_delete_void((void *)ps->tile_lock);
4902 }
4903
4904#ifndef PROJ_DEBUG_NOSEAMBLEED
4905 if (ps->seam_bleed_px > 0.0f) {
4906 MEM_delete(ps->vertFaces);
4907 MEM_delete(ps->faceSeamFlags);
4908 MEM_delete(ps->faceWindingFlags);
4909 MEM_delete(ps->loopSeamData);
4910 MEM_delete(ps->vertSeams);
4911 }
4912#endif
4913
4914 if (ps->do_mask_cavity) {
4915 MEM_delete(ps->cavities);
4916 }
4917
4918 ps->mesh_eval = nullptr;
4919 }
4920
4921 if (ps->blurkernel) {
4923 MEM_delete(ps->blurkernel);
4924 }
4925
4926 if (ps->vertFlags) {
4927 MEM_delete(ps->vertFlags);
4928 }
4929
4930 for (a = 0; a < ps->thread_tot; a++) {
4932 }
4933}
4934
4935/* 1 = an undo, -1 is a redo. */
4940
4942{
4943 int tot = PROJ_BOUNDBOX_SQUARED;
4944 while (tot--) {
4946 pr++;
4947 }
4948}
4949
4951 ImagePaintPartialRedraw *pr_other,
4952 int tot)
4953{
4954 bool touch = false;
4955 while (tot--) {
4957 if (!BLI_rcti_is_empty(&pr->dirty_region)) {
4958 touch = true;
4959 }
4960
4961 pr++;
4962 pr_other++;
4963 }
4964
4965 return touch;
4966}
4967
4968/* Loop over all images on this mesh and update any we have touched */
4970{
4972 ProjPaintImage *projIma;
4973 int a, i;
4974 bool redraw = false;
4975
4976 for (a = 0, projIma = ps->projImages; a < ps->image_tot; a++, projIma++) {
4977 if (projIma->touch) {
4978 /* look over each bound cell */
4979 for (i = 0; i < PROJ_BOUNDBOX_SQUARED; i++) {
4980 pr = &(projIma->partRedrawRect[i]);
4981 if (BLI_rcti_is_valid(&pr->dirty_region)) {
4983 imapaint_image_update(projIma->ibuf);
4984 redraw = true;
4985 }
4986
4988 }
4989
4990 /* clear for reuse */
4991 projIma->touch = false;
4992 }
4993 }
4994
4995 return redraw;
4996}
4997
4998/* run this per painting onto each mouse location */
4999static bool project_bucket_iter_init(ProjPaintState *ps, const float mval_f[2])
5000{
5001 if (ps->source == PROJ_SRC_VIEW) {
5002 float min_brush[2], max_brush[2];
5003 const float radius = ps->brush_size;
5004
5005 /* so we don't have a bucket bounds that is way too small to paint into */
5006#if 0
5007 /* This doesn't work yet. */
5008 if (radius < 1.0f) {
5009 radius = 1.0f;
5010 }
5011#endif
5012
5013 min_brush[0] = mval_f[0] - radius;
5014 min_brush[1] = mval_f[1] - radius;
5015
5016 max_brush[0] = mval_f[0] + radius;
5017 max_brush[1] = mval_f[1] + radius;
5018
5019 /* offset to make this a valid bucket index */
5020 project_paint_bucket_bounds(ps, min_brush, max_brush, ps->bucketMin, ps->bucketMax);
5021
5022 /* mouse outside the model areas? */
5023 if (ps->bucketMin[0] == ps->bucketMax[0] || ps->bucketMin[1] == ps->bucketMax[1]) {
5024 return false;
5025 }
5026 }
5027 else { /* reproject: PROJ_SRC_* */
5028 ps->bucketMin[0] = 0;
5029 ps->bucketMin[1] = 0;
5030
5031 ps->bucketMax[0] = ps->buckets_x;
5032 ps->bucketMax[1] = ps->buckets_y;
5033 }
5034
5035 ps->context_bucket_index = ps->bucketMin[0] + ps->bucketMin[1] * ps->buckets_x;
5036 return true;
5037}
5038
5040 int *bucket_index,
5041 rctf *bucket_bounds,
5042 const float mval[2])
5043{
5044 const int diameter = 2 * ps->brush_size;
5045
5046 const int max_bucket_idx = ps->bucketMax[0] + (ps->bucketMax[1] - 1) * ps->buckets_x;
5047
5048 for (int bidx = atomic_fetch_and_add_int32(&ps->context_bucket_index, 1); bidx < max_bucket_idx;
5050 {
5051 const int bucket_y = bidx / ps->buckets_x;
5052 const int bucket_x = bidx - (bucket_y * ps->buckets_x);
5053
5054 BLI_assert(bucket_y >= ps->bucketMin[1] && bucket_y < ps->bucketMax[1]);
5055 if (bucket_x >= ps->bucketMin[0] && bucket_x < ps->bucketMax[0]) {
5056 /* Use bucket_bounds for #project_bucket_isect_circle and #project_bucket_init. */
5057 project_bucket_bounds(ps, bucket_x, bucket_y, bucket_bounds);
5058
5059 if ((ps->source != PROJ_SRC_VIEW) ||
5060 project_bucket_isect_circle(mval, float(diameter * diameter), bucket_bounds))
5061 {
5062 *bucket_index = bidx;
5063
5064 return true;
5065 }
5066 }
5067 }
5068
5069 return false;
5070}
5071
5072/* Each thread gets one of these, also used as an argument to pass to project_paint_op */
5074 /* args */
5076 float prevmval[2];
5077 float mval[2];
5078
5079 /* Annoying but we need to have image bounds per thread,
5080 * then merge into ps->projectPartialRedraws. */
5081
5082 /* array of partial redraws */
5084
5085 /* thread settings */
5087
5089};
5090
5091static void do_projectpaint_clone(ProjPaintState *ps, ProjPixel *projPixel, float mask)
5092{
5093 uint8_t *data = ps->projImages[projPixel->image_index].byte_data_mut + projPixel->pixel_offset;
5094 const uchar *clone_pt = (reinterpret_cast<ProjPixelClone *>(projPixel))->clonepx.ch;
5095
5096 if (clone_pt[3]) {
5097 uchar clone_rgba[4];
5098
5099 clone_rgba[0] = clone_pt[0];
5100 clone_rgba[1] = clone_pt[1];
5101 clone_rgba[2] = clone_pt[2];
5102 clone_rgba[3] = uchar(clone_pt[3] * mask);
5103
5104 if (ps->do_masking) {
5105 IMB_blend_color_byte(data, projPixel->origColor.ch_pt, clone_rgba, IMB_BlendMode(ps->blend));
5106 }
5107 else {
5108 IMB_blend_color_byte(data, data, clone_rgba, IMB_BlendMode(ps->blend));
5109 }
5110 }
5111}
5112
5113static void do_projectpaint_clone_f(ProjPaintState *ps, ProjPixel *projPixel, float mask)
5114{
5115 float *data = ps->projImages[projPixel->image_index].float_data_mut + projPixel->pixel_offset;
5116 const float *clone_pt = (reinterpret_cast<ProjPixelClone *>(projPixel))->clonepx.f;
5117
5118 if (clone_pt[3]) {
5119 float clone_rgba[4];
5120
5121 mul_v4_v4fl(clone_rgba, clone_pt, mask);
5122
5123 if (ps->do_masking) {
5124 IMB_blend_color_float(data, projPixel->origColor.f_pt, clone_rgba, IMB_BlendMode(ps->blend));
5125 }
5126 else {
5128 }
5129 }
5130}
5131
5138 ProjPixel *projPixel,
5139 float mask,
5140 MemArena *smearArena,
5141 LinkNode **smearPixels,
5142 const float co[2])
5143{
5144 const uint8_t *data = ps->projImages[projPixel->image_index].byte_data_mut +
5145 projPixel->pixel_offset;
5146 uchar rgba_ub[4];
5147
5148 if (project_paint_PickColor(ps, co, nullptr, rgba_ub, true) == 0) {
5149 return;
5150 }
5151
5153 (reinterpret_cast<ProjPixelClone *>(projPixel))->clonepx.ch, data, rgba_ub, mask);
5154 BLI_linklist_prepend_arena(smearPixels, static_cast<void *>(projPixel), smearArena);
5155}
5156
5158 ProjPixel *projPixel,
5159 float mask,
5160 MemArena *smearArena,
5161 LinkNode **smearPixels_f,
5162 const float co[2])
5163{
5164 const float *data = ps->projImages[projPixel->image_index].float_data_mut +
5165 projPixel->pixel_offset;
5166 float rgba[4];
5167
5168 if (project_paint_PickColor(ps, co, rgba, nullptr, true) == 0) {
5169 return;
5170 }
5171
5173 (reinterpret_cast<ProjPixelClone *>(projPixel))->clonepx.f, data, rgba, mask);
5174 BLI_linklist_prepend_arena(smearPixels_f, static_cast<void *>(projPixel), smearArena);
5175}
5176
5178 ProjPixel *projPixel,
5179 float mask,
5180 MemArena *softenArena,
5181 LinkNode **softenPixels)
5182{
5183 float *data = ps->projImages[projPixel->image_index].float_data_mut + projPixel->pixel_offset;
5184 float accum_tot = 0.0f;
5185 int xk, yk;
5186 BlurKernel *kernel = ps->blurkernel;
5187 float *rgba = projPixel->newColor.f;
5188
5189 /* rather than painting, accumulate surrounding colors */
5190 zero_v4(rgba);
5191
5192 for (yk = 0; yk < kernel->side; yk++) {
5193 for (xk = 0; xk < kernel->side; xk++) {
5194 float rgba_tmp[4];
5195 float co_ofs[2] = {2.0f * xk - 1.0f, 2.0f * yk - 1.0f};
5196
5197 add_v2_v2(co_ofs, projPixel->projCoSS);
5198
5199 if (project_paint_PickColor(ps, co_ofs, rgba_tmp, nullptr, true)) {
5200 float weight = kernel->wdata[xk + yk * kernel->side];
5201 mul_v4_fl(rgba_tmp, weight);
5202 add_v4_v4(rgba, rgba_tmp);
5203 accum_tot += weight;
5204 }
5205 }
5206 }
5207
5208 if (accum_tot != 0) [[likely]] {
5209 mul_v4_fl(rgba, 1.0f / accum_tot);
5210
5211 if (ps->mode == BrushStrokeMode::Invert) {
5212 /* subtract blurred image from normal image gives high pass filter */
5213 sub_v3_v3v3(rgba, data, rgba);
5214
5215 /* now rgba_ub contains the edge result, but this should be converted to luminance to avoid
5216 * colored speckles appearing in final image, and also to check for threshold */
5217 rgba[0] = rgba[1] = rgba[2] = IMB_colormanagement_get_luminance(rgba);
5218 if (fabsf(rgba[0]) > ps->brush->sharp_threshold) {
5219 float alpha = data[3];
5220 data[3] = rgba[3] = mask;
5221
5222 /* add to enhance edges */
5223 blend_color_add_float(rgba, data, rgba);
5224 rgba[3] = alpha;
5225 }
5226 else {
5227 return;
5228 }
5229 }
5230 else {
5232 }
5233
5234 BLI_linklist_prepend_arena(softenPixels, static_cast<void *>(projPixel), softenArena);
5235 }
5236}
5237
5239 ProjPixel *projPixel,
5240 float mask,
5241 MemArena *softenArena,
5242 LinkNode **softenPixels)
5243{
5244 uint8_t *data = ps->projImages[projPixel->image_index].byte_data_mut + projPixel->pixel_offset;
5245 float accum_tot = 0;
5246 int xk, yk;
5247 BlurKernel *kernel = ps->blurkernel;
5248 /* convert to byte after */
5249 float rgba[4];
5250
5251 /* rather than painting, accumulate surrounding colors */
5252 zero_v4(rgba);
5253
5254 for (yk = 0; yk < kernel->side; yk++) {
5255 for (xk = 0; xk < kernel->side; xk++) {
5256 float rgba_tmp[4];
5257 float co_ofs[2] = {2.0f * xk - 1.0f, 2.0f * yk - 1.0f};
5258
5259 add_v2_v2(co_ofs, projPixel->projCoSS);
5260
5261 if (project_paint_PickColor(ps, co_ofs, rgba_tmp, nullptr, true)) {
5262 float weight = kernel->wdata[xk + yk * kernel->side];
5263 mul_v4_fl(rgba_tmp, weight);
5264 add_v4_v4(rgba, rgba_tmp);
5265 accum_tot += weight;
5266 }
5267 }
5268 }
5269
5270 if (accum_tot != 0) [[likely]] {
5271 uchar *rgba_ub = projPixel->newColor.ch;
5272
5273 mul_v4_fl(rgba, 1.0f / accum_tot);
5274
5275 if (ps->mode == BrushStrokeMode::Invert) {
5276 float rgba_pixel[4];
5277
5279
5280 /* subtract blurred image from normal image gives high pass filter */
5281 sub_v3_v3v3(rgba, rgba_pixel, rgba);
5282 /* now rgba_ub contains the edge result, but this should be converted to luminance to avoid
5283 * colored speckles appearing in final image, and also to check for threshold */
5284 rgba[0] = rgba[1] = rgba[2] = IMB_colormanagement_get_luminance(rgba);
5285 if (fabsf(rgba[0]) > ps->brush->sharp_threshold) {
5286 float alpha = rgba_pixel[3];
5287 rgba[3] = rgba_pixel[3] = mask;
5288
5289 /* add to enhance edges */
5290 blend_color_add_float(rgba, rgba_pixel, rgba);
5291
5292 rgba[3] = alpha;
5293 premul_float_to_straight_uchar(rgba_ub, rgba);
5294 }
5295 else {
5296 return;
5297 }
5298 }
5299 else {
5300 premul_float_to_straight_uchar(rgba_ub, rgba);
5301 blend_color_interpolate_byte(rgba_ub, data, rgba_ub, mask);
5302 }
5303 BLI_linklist_prepend_arena(softenPixels, static_cast<void *>(projPixel), softenArena);
5304 }
5305}
5306
5308 ProjPixel *projPixel,
5309 const float texrgb[3],
5310 float mask,
5311 float dither,
5312 int u,
5313 int v)
5314{
5315 const ProjPaintImage *img = &ps->projImages[projPixel->image_index];
5316 uint8_t *data = ps->projImages[projPixel->image_index].byte_data_mut + projPixel->pixel_offset;
5317 float rgb[3];
5318 uchar rgba_ub[4];
5319
5320 if (ps->is_texbrush) {
5321 mul_v3_v3v3(rgb, texrgb, ps->paint_color_linear);
5322 if (img->is_srgb) {
5323 /* Fast-ish path for sRGB. */
5325 }
5326 else if (img->byte_colorspace) {
5327 /* Slow path with arbitrary colorspace. */
5329 }
5330 }
5331 else {
5332 copy_v3_v3(rgb, img->paint_color_byte);
5333 }
5334
5335 if (dither > 0.0f) {
5336 float_to_byte_dither_v3(rgba_ub, rgb, dither, u, v);
5337 }
5338 else {
5339 unit_float_to_uchar_clamp_v3(rgba_ub, rgb);
5340 }
5341 rgba_ub[3] = f_to_char(mask);
5342
5343 if (ps->do_masking) {
5344 IMB_blend_color_byte(data, projPixel->origColor.ch_pt, rgba_ub, IMB_BlendMode(ps->blend));
5345 }
5346 else {
5348 }
5349}
5350
5352 ProjPixel *projPixel,
5353 const float texrgb[3],
5354 float mask)
5355{
5356 float *data = ps->projImages[projPixel->image_index].float_data_mut + projPixel->pixel_offset;
5357 float rgba[4];
5358
5359 copy_v3_v3(rgba, ps->paint_color_linear);
5360
5361 if (ps->is_texbrush) {
5362 mul_v3_v3(rgba, texrgb);
5363 }
5364
5365 mul_v3_fl(rgba, mask);
5366 rgba[3] = mask;
5367
5368 if (ps->do_masking) {
5370 }
5371 else {
5373 }
5374}
5375
5376static void do_projectpaint_mask(ProjPaintState *ps, ProjPixel *projPixel, float mask)
5377{
5378 uint8_t *data = ps->projImages[projPixel->image_index].byte_data_mut + projPixel->pixel_offset;
5379 uchar rgba_ub[4];
5380 rgba_ub[0] = rgba_ub[1] = rgba_ub[2] = ps->stencil_value * 255.0f;
5381 rgba_ub[3] = f_to_char(mask);
5382
5383 if (ps->do_masking) {
5384 IMB_blend_color_byte(data, projPixel->origColor.ch_pt, rgba_ub, IMB_BlendMode(ps->blend));
5385 }
5386 else {
5388 }
5389}
5390
5391static void do_projectpaint_mask_f(ProjPaintState *ps, ProjPixel *projPixel, float mask)
5392{
5393 float *data = ps->projImages[projPixel->image_index].float_data_mut + projPixel->pixel_offset;
5394 float rgba[4];
5395 rgba[0] = rgba[1] = rgba[2] = ps->stencil_value;
5396 rgba[3] = mask;
5397
5398 if (ps->do_masking) {
5400 }
5401 else {
5403 }
5404}
5405
5407 const ProjPixel *projPixel)
5408{
5409 rcti rect_to_add;
5411 &rect_to_add, projPixel->x_px, projPixel->x_px + 1, projPixel->y_px, projPixel->y_px + 1);
5412 BLI_rcti_do_minmax_rcti(&cell->dirty_region, &rect_to_add);
5413}
5414
5415static void copy_original_alpha_channel(ProjPaintState *ps, ProjPixel *pixel, bool is_floatbuf)
5416{
5417 /* Use the original alpha channel data instead of the modified one */
5418 if (is_floatbuf) {
5419 float *data = ps->projImages[pixel->image_index].float_data_mut + pixel->pixel_offset;
5420 /* slightly more involved case since floats are in premultiplied space we need
5421 * to make sure alpha is consistent, see #44627 */
5422 float rgb_straight[4];
5423 premul_to_straight_v4_v4(rgb_straight, data);
5424 rgb_straight[3] = pixel->origColor.f_pt[3];
5425 straight_to_premul_v4_v4(data, rgb_straight);
5426 }
5427 else {
5429 data[3] = pixel->origColor.ch_pt[3];
5430 }
5431}
5432
5433/* Run this for single and multi-threaded painting. */
5434static void do_projectpaint_thread(TaskPool *__restrict /*pool*/, void *ph_v)
5435{
5436 /* First unpack args from the struct */
5437 ProjPaintState *ps = (static_cast<ProjectHandle *>(ph_v))->ps;
5438 ProjPaintImage *projImages = (static_cast<ProjectHandle *>(ph_v))->projImages;
5439 const float *lastpos = (static_cast<ProjectHandle *>(ph_v))->prevmval;
5440 const float *pos = (static_cast<ProjectHandle *>(ph_v))->mval;
5441 const int thread_index = (static_cast<ProjectHandle *>(ph_v))->thread_index;
5442 ImagePool *pool = (static_cast<ProjectHandle *>(ph_v))->pool;
5443 /* Done with args from ProjectHandle */
5444
5445 LinkNode *node;
5446 ProjPixel *projPixel;
5447 Brush *brush = ps->brush;
5448
5449 int last_index = -1;
5450 ProjPaintImage *last_projIma = nullptr;
5451 ImagePaintPartialRedraw *last_partial_redraw_cell;
5452
5453 float dist_sq, dist;
5454
5455 float falloff;
5456 int bucket_index;
5457 bool is_floatbuf = false;
5458 const short brush_type = ps->brush_type;
5459 rctf bucket_bounds;
5460
5461 /* for smear only */
5462 float pos_ofs[2] = {0};
5463 float co[2];
5464 ushort mask_short;
5465 const float brush_alpha = BKE_brush_alpha_get(ps->paint, brush);
5466 const float brush_radius = ps->brush_size;
5467 /* avoid a square root with every dist comparison */
5468 const float brush_radius_sq = brush_radius * brush_radius;
5469
5470 const bool lock_alpha = ELEM(brush->blend, IMB_BLEND_ERASE_ALPHA, IMB_BLEND_ADD_ALPHA) ?
5471 false :
5472 (brush->flag & BRUSH_LOCK_ALPHA) != 0;
5473
5474 LinkNode *smearPixels = nullptr;
5475 LinkNode *smearPixels_f = nullptr;
5476 /* mem arena for this brush projection only */
5477 MemArena *smearArena = nullptr;
5478
5479 LinkNode *softenPixels = nullptr;
5480 LinkNode *softenPixels_f = nullptr;
5481 /* mem arena for this brush projection only */
5482 MemArena *softenArena = nullptr;
5483
5484 if (brush_type == IMAGE_PAINT_BRUSH_TYPE_SMEAR) {
5485 pos_ofs[0] = pos[0] - lastpos[0];
5486 pos_ofs[1] = pos[1] - lastpos[1];
5487
5488 smearArena = BLI_memarena_new(MEM_SIZE_OPTIMAL(1 << 16), "paint smear arena");
5489 }
5490 else if (brush_type == IMAGE_PAINT_BRUSH_TYPE_SOFTEN) {
5491 softenArena = BLI_memarena_new(MEM_SIZE_OPTIMAL(1 << 16), "paint soften arena");
5492 }
5493
5494 // printf("brush bounds %d %d %d %d\n",
5495 // bucketMin[0], bucketMin[1], bucketMax[0], bucketMax[1]);
5496
5497 while (project_bucket_iter_next(ps, &bucket_index, &bucket_bounds, pos)) {
5498
5499 /* Check this bucket and its faces are initialized */
5500 if (ps->bucketFlags[bucket_index] == PROJ_BUCKET_NULL) {
5501 rctf clip_rect = bucket_bounds;
5502 clip_rect.xmin -= PROJ_PIXEL_TOLERANCE;
5503 clip_rect.xmax += PROJ_PIXEL_TOLERANCE;
5504 clip_rect.ymin -= PROJ_PIXEL_TOLERANCE;
5505 clip_rect.ymax += PROJ_PIXEL_TOLERANCE;
5506 /* No pixels initialized */
5507 project_bucket_init(ps, thread_index, bucket_index, &clip_rect, &bucket_bounds);
5508 }
5509
5510 if (ps->source != PROJ_SRC_VIEW) {
5511
5512 /* Re-Projection, simple, no brushes! */
5513
5514 for (node = ps->bucketRect[bucket_index]; node; node = node->next) {
5515 projPixel = static_cast<ProjPixel *>(node->link);
5516
5517 /* copy of code below */
5518 if (last_index != projPixel->image_index) {
5519 last_index = projPixel->image_index;
5520 last_projIma = projImages + last_index;
5521
5522 last_projIma->touch = true;
5523 is_floatbuf = (last_projIma->ibuf->float_data() != nullptr);
5524 }
5525
5526 uint8_t *byte_data = ps->projImages[projPixel->image_index].byte_data_mut;
5527 float *float_data = ps->projImages[projPixel->image_index].float_data_mut;
5528
5529 BLI_assert(byte_data != nullptr || float_data != nullptr);
5530
5531 /* end copy */
5532
5533 /* fill brushes */
5534 if (ps->source == PROJ_SRC_VIEW_FILL) {
5535 if (brush->flag & BRUSH_USE_GRADIENT) {
5536 /* these could probably be cached instead of being done per pixel */
5537 float tangent[2];
5538 float line_len_sq_inv, line_len;
5539 float f;
5540 float color_f[4];
5541 const float p[2] = {
5542 projPixel->projCoSS[0] - lastpos[0],
5543 projPixel->projCoSS[1] - lastpos[1],
5544 };
5545
5546 sub_v2_v2v2(tangent, pos, lastpos);
5547 line_len = len_squared_v2(tangent);
5548 line_len_sq_inv = 1.0f / line_len;
5549 line_len = sqrtf(line_len);
5550
5551 switch (brush->gradient_fill_mode) {
5552 case BRUSH_GRADIENT_LINEAR: {
5553 f = dot_v2v2(p, tangent) * line_len_sq_inv;
5554 break;
5555 }
5557 default: {
5558 f = len_v2(p) / line_len;
5559 break;
5560 }
5561 }
5562 BKE_colorband_evaluate(brush->gradient, f, color_f);
5563 color_f[3] *= float(projPixel->mask) * (1.0f / 65535.0f) * brush_alpha;
5564
5565 if (is_floatbuf) {
5566 /* Convert to premutliplied. */
5567 mul_v3_fl(color_f, color_f[3]);
5568 IMB_blend_color_float(float_data + projPixel->pixel_offset,
5569 projPixel->origColor.f_pt,
5570 color_f,
5571 IMB_BlendMode(ps->blend));
5572 }
5573 else {
5574 const ProjPaintImage *img = &ps->projImages[projPixel->image_index];
5575 if (img->is_srgb) {
5577 }
5578 else if (img->byte_colorspace) {
5580 }
5581
5582 if (ps->dither > 0.0f) {
5584 projPixel->newColor.ch, color_f, ps->dither, projPixel->x_px, projPixel->y_px);
5585 }
5586 else {
5587 unit_float_to_uchar_clamp_v3(projPixel->newColor.ch, color_f);
5588 }
5589 projPixel->newColor.ch[3] = unit_float_to_uchar_clamp(color_f[3]);
5590 IMB_blend_color_byte(byte_data + projPixel->pixel_offset,
5591 projPixel->origColor.ch_pt,
5592 projPixel->newColor.ch,
5593 IMB_BlendMode(ps->blend));
5594 }
5595 }
5596 else {
5597 if (is_floatbuf) {
5598 float newColor_f[4];
5599 newColor_f[3] = float(projPixel->mask) * (1.0f / 65535.0f) * brush_alpha;
5600 copy_v3_v3(newColor_f, ps->paint_color_linear);
5601
5602 IMB_blend_color_float(float_data + projPixel->pixel_offset,
5603 projPixel->origColor.f_pt,
5604 newColor_f,
5605 IMB_BlendMode(ps->blend));
5606 }
5607 else {
5608 const ProjPaintImage *img = &ps->projImages[projPixel->image_index];
5609 float mask = float(projPixel->mask) * (1.0f / 65535.0f);
5610 projPixel->newColor.ch[3] = mask * 255 * brush_alpha;
5611
5613 IMB_blend_color_byte(byte_data + projPixel->pixel_offset,
5614 projPixel->origColor.ch_pt,
5615 projPixel->newColor.ch,
5616 IMB_BlendMode(ps->blend));
5617 }
5618 }
5619
5620 if (lock_alpha) {
5621 copy_original_alpha_channel(ps, projPixel, is_floatbuf);
5622 }
5623
5624 last_partial_redraw_cell = last_projIma->partRedrawRect + projPixel->bb_cell_index;
5625 image_paint_partial_redraw_expand(last_partial_redraw_cell, projPixel);
5626 }
5627 else {
5628 if (is_floatbuf) {
5629 BLI_assert(ps->reproject_ibuf->float_data() != nullptr);
5630
5632 projPixel->newColor.f,
5633 projPixel->projCoSS[0],
5634 projPixel->projCoSS[1]);
5635 if (projPixel->newColor.f[3]) {
5636 float mask = float(projPixel->mask) * (1.0f / 65535.0f);
5637
5638 mul_v4_v4fl(projPixel->newColor.f, projPixel->newColor.f, mask);
5639
5640 blend_color_mix_float(float_data + projPixel->pixel_offset,
5641 projPixel->origColor.f_pt,
5642 projPixel->newColor.f);
5643 }
5644 }
5645 else {
5646 BLI_assert(ps->reproject_ibuf->byte_data() != nullptr);
5648 projPixel->newColor.ch,
5649 projPixel->projCoSS[0],
5650 projPixel->projCoSS[1]);
5651 if (projPixel->newColor.ch[3]) {
5652 float mask = float(projPixel->mask) * (1.0f / 65535.0f);
5653 projPixel->newColor.ch[3] *= mask;
5654
5655 blend_color_mix_byte(byte_data + projPixel->pixel_offset,
5656 projPixel->origColor.ch_pt,
5657 projPixel->newColor.ch);
5658 }
5659 }
5660 }
5661 }
5662 }
5663 else {
5664 /* Normal brush painting */
5665
5666 for (node = ps->bucketRect[bucket_index]; node; node = node->next) {
5667
5668 projPixel = static_cast<ProjPixel *>(node->link);
5669
5670 dist_sq = len_squared_v2v2(projPixel->projCoSS, pos);
5671
5672 /* Faster alternative to `dist < radius` without a #sqrtf. */
5673 if (dist_sq <= brush_radius_sq) {
5674 dist = sqrtf(dist_sq);
5675
5676 falloff = BKE_brush_curve_strength_clamped(ps->brush, dist, brush_radius);
5677
5678 if (falloff > 0.0f) {
5679 float texrgb[3];
5680 float mask;
5681
5682 /* Extra mask for normal, layer stencil, etc. */
5683 float custom_mask = float(projPixel->mask) * (1.0f / 65535.0f);
5684
5685 /* Mask texture. */
5686 if (ps->is_maskbrush) {
5687 float texmask = BKE_brush_sample_masktex(
5688 ps->paint, ps->brush, projPixel->projCoSS, thread_index, pool);
5689 CLAMP(texmask, 0.0f, 1.0f);
5690 custom_mask *= texmask;
5691 }
5692
5693 /* Color texture (alpha used as mask). */
5694 if (ps->is_texbrush) {
5696 float3 samplecos;
5697 float4 texrgba;
5698
5699 /* taking 3d copy to account for 3D mapping too.
5700 * It gets concatenated during sampling */
5701 if (mtex->brush_map_mode == MTEX_MAP_MODE_3D) {
5702 copy_v3_v3(samplecos, projPixel->worldCoSS);
5703 }
5704 else {
5705 copy_v2_v2(samplecos, projPixel->projCoSS);
5706 samplecos[2] = 0.0f;
5707 }
5708
5709 /* NOTE: for clone and smear,
5710 * we only use the alpha, could be a special function */
5712 ps->paint, brush, mtex, samplecos, texrgba, thread_index, pool);
5713
5714 copy_v3_v3(texrgb, texrgba);
5715 custom_mask *= texrgba[3];
5716 }
5717 else {
5718 zero_v3(texrgb);
5719 }
5720
5721 if (ps->do_masking) {
5722 /* masking to keep brush contribution to a pixel limited. note we do not do
5723 * a simple max(mask, mask_accum), as this is very sensitive to spacing and
5724 * gives poor results for strokes crossing themselves.
5725 *
5726 * Instead we use a formula that adds up but approaches brush_alpha slowly
5727 * and never exceeds it, which gives nice smooth results. */
5728 float mask_accum = *projPixel->mask_accum;
5729 float max_mask = brush_alpha * custom_mask * falloff * 65535.0f;
5730
5731 if (brush->flag & BRUSH_ACCUMULATE) {
5732 mask = mask_accum + max_mask;
5733 }
5734 else {
5735 mask = mask_accum + (max_mask - mask_accum * falloff);
5736 }
5737
5738 mask = min_ff(mask, 65535.0f);
5739 mask_short = ushort(mask);
5740
5741 if (mask_short > *projPixel->mask_accum) {
5742 *projPixel->mask_accum = mask_short;
5743 mask = mask_short * (1.0f / 65535.0f);
5744 }
5745 else {
5746 /* Go onto the next pixel */
5747 continue;
5748 }
5749 }
5750 else {
5751 mask = brush_alpha * custom_mask * falloff;
5752 }
5753
5754 if (mask > 0.0f) {
5755
5756 /* copy of code above */
5757 if (last_index != projPixel->image_index) {
5758 last_index = projPixel->image_index;
5759 last_projIma = projImages + last_index;
5760
5761 last_projIma->touch = true;
5762 is_floatbuf = (last_projIma->ibuf->float_data() != nullptr);
5763 }
5764 /* end copy */
5765
5766 /* Validate undo tile, since we will modify it. */
5767 *projPixel->valid = true;
5768
5769 last_partial_redraw_cell = last_projIma->partRedrawRect + projPixel->bb_cell_index;
5770 image_paint_partial_redraw_expand(last_partial_redraw_cell, projPixel);
5771
5772 /* texrgb is not used for clone, smear or soften */
5773 switch (brush_type) {
5775 if (is_floatbuf) {
5776 do_projectpaint_clone_f(ps, projPixel, mask);
5777 }
5778 else {
5779 do_projectpaint_clone(ps, projPixel, mask);
5780 }
5781 break;
5783 sub_v2_v2v2(co, projPixel->projCoSS, pos_ofs);
5784
5785 if (is_floatbuf) {
5786 do_projectpaint_smear_f(ps, projPixel, mask, smearArena, &smearPixels_f, co);
5787 }
5788 else {
5789 do_projectpaint_smear(ps, projPixel, mask, smearArena, &smearPixels, co);
5790 }
5791 break;
5793 if (is_floatbuf) {
5794 do_projectpaint_soften_f(ps, projPixel, mask, softenArena, &softenPixels_f);
5795 }
5796 else {
5797 do_projectpaint_soften(ps, projPixel, mask, softenArena, &softenPixels);
5798 }
5799 break;
5801 if (is_floatbuf) {
5802 do_projectpaint_mask_f(ps, projPixel, mask);
5803 }
5804 else {
5805 do_projectpaint_mask(ps, projPixel, mask);
5806 }
5807 break;
5808 default:
5809 if (is_floatbuf) {
5810 do_projectpaint_draw_f(ps, projPixel, texrgb, mask);
5811 }
5812 else {
5814 ps, projPixel, texrgb, mask, ps->dither, projPixel->x_px, projPixel->y_px);
5815 }
5816 break;
5817 }
5818
5819 if (lock_alpha) {
5820 copy_original_alpha_channel(ps, projPixel, is_floatbuf);
5821 }
5822 }
5823
5824 /* done painting */
5825 }
5826 }
5827 }
5828 }
5829 }
5830
5831 if (brush_type == IMAGE_PAINT_BRUSH_TYPE_SMEAR) {
5832
5833 for (node = smearPixels; node; node = node->next) { /* this won't run for a float image */
5834 projPixel = static_cast<ProjPixel *>(node->link);
5835 uint8_t *data = ps->projImages[projPixel->image_index].byte_data_mut +
5836 projPixel->pixel_offset;
5837 copy_v4_v4_uchar(data, reinterpret_cast<ProjPixelClone *>(projPixel)->clonepx.ch);
5838 if (lock_alpha) {
5839 copy_original_alpha_channel(ps, projPixel, false);
5840 }
5841 }
5842
5843 for (node = smearPixels_f; node; node = node->next) {
5844 projPixel = static_cast<ProjPixel *>(node->link);
5845 float *data = ps->projImages[projPixel->image_index].float_data_mut +
5846 projPixel->pixel_offset;
5847 copy_v4_v4(data, (reinterpret_cast<ProjPixelClone *>(projPixel))->clonepx.f);
5848 if (lock_alpha) {
5849 copy_original_alpha_channel(ps, projPixel, true);
5850 }
5851 }
5852
5853 BLI_memarena_free(smearArena);
5854 }
5855 else if (brush_type == IMAGE_PAINT_BRUSH_TYPE_SOFTEN) {
5856
5857 for (node = softenPixels; node; node = node->next) { /* this won't run for a float image */
5858 projPixel = static_cast<ProjPixel *>(node->link);
5859 uint8_t *data = ps->projImages[projPixel->image_index].byte_data_mut +
5860 projPixel->pixel_offset;
5861 copy_v4_v4_uchar(data, projPixel->newColor.ch);
5862 if (lock_alpha) {
5863 copy_original_alpha_channel(ps, projPixel, false);
5864 }
5865 }
5866
5867 for (node = softenPixels_f; node; node = node->next) {
5868 projPixel = static_cast<ProjPixel *>(node->link);
5869 float *data = ps->projImages[projPixel->image_index].float_data_mut +
5870 projPixel->pixel_offset;
5871 copy_v4_v4(data, projPixel->newColor.f);
5872 if (lock_alpha) {
5873 copy_original_alpha_channel(ps, projPixel, true);
5874 }
5875 }
5876
5877 BLI_memarena_free(softenArena);
5878 }
5879}
5880
5881static bool project_paint_op(void *state, const float lastpos[2], const float pos[2])
5882{
5883 /* First unpack args from the struct */
5884 ProjPaintState *ps = static_cast<ProjPaintState *>(state);
5885 bool touch_any = false;
5886
5888 TaskPool *task_pool = nullptr;
5889 int a, i;
5890
5891 ImagePool *image_pool;
5892
5893 if (!project_bucket_iter_init(ps, pos)) {
5894 return touch_any;
5895 }
5896
5897 if (ps->thread_tot > 1) {
5899 }
5900
5901 image_pool = BKE_image_pool_new();
5902
5904 /* This means we are reprojecting an image, make sure the image has the needed data available.
5905 */
5906 bool float_dest = false;
5907 bool uchar_dest = false;
5908 /* Check if the destination images are float or uchar. */
5909 for (i = 0; i < ps->image_tot; i++) {
5910 if (ps->projImages[i].ibuf->byte_data() != nullptr) {
5911 uchar_dest = true;
5912 }
5913 if (ps->projImages[i].ibuf->float_data() != nullptr) {
5914 float_dest = true;
5915 }
5916 }
5917
5918 /* Generate missing data if needed. */
5919 if (float_dest && ps->reproject_ibuf->float_data() == nullptr) {
5921 ps->reproject_ibuf_free_float = true;
5922 }
5923 if (uchar_dest && ps->reproject_ibuf->byte_data() == nullptr) {
5925 ps->reproject_ibuf_free_uchar = true;
5926 }
5927 }
5928
5929 for (i = 0; i < ps->image_tot; i++) {
5933 }
5934
5935 /* get the threads running */
5936 for (a = 0; a < ps->thread_tot; a++) {
5937
5938 /* set defaults in handles */
5939 // memset(&handles[a], 0, sizeof(BakeShade));
5940
5941 handles[a].ps = ps;
5942 copy_v2_v2(handles[a].mval, pos);
5943 copy_v2_v2(handles[a].prevmval, lastpos);
5944
5945 /* thread specific */
5946 handles[a].thread_index = a;
5947
5948 handles[a].projImages = static_cast<ProjPaintImage *>(
5949 BLI_memarena_alloc(ps->arena_mt[a], ps->image_tot * sizeof(ProjPaintImage)));
5950
5951 memcpy(handles[a].projImages, ps->projImages, ps->image_tot * sizeof(ProjPaintImage));
5952
5953 /* image bounds */
5954 for (i = 0; i < ps->image_tot; i++) {
5955 handles[a].projImages[i].partRedrawRect = static_cast<ImagePaintPartialRedraw *>(
5958 memcpy(handles[a].projImages[i].partRedrawRect,
5961 }
5962
5963 handles[a].pool = image_pool;
5964
5965 if (task_pool != nullptr) {
5966 BLI_task_pool_push(task_pool, do_projectpaint_thread, &handles[a], false, nullptr);
5967 }
5968 }
5969
5970 if (task_pool != nullptr) { /* wait for everything to be done */
5971 BLI_task_pool_work_and_wait(task_pool);
5972 BLI_task_pool_free(task_pool);
5973 }
5974 else {
5975 do_projectpaint_thread(nullptr, &handles[0]);
5976 }
5977
5978 BKE_image_pool_free(image_pool);
5979
5980 /* move threaded bounds back into ps->projectPartialRedraws */
5981 for (i = 0; i < ps->image_tot; i++) {
5982 int touch = false;
5983 for (a = 0; a < ps->thread_tot; a++) {
5985 handles[a].projImages[i].partRedrawRect,
5987 }
5988
5989 if (touch) {
5990 ps->projImages[i].touch = true;
5991 touch_any = true;
5992 }
5993 }
5994
5995 /* Calculate pivot for rotation around selection if needed. */
5996 if (U.uiflag & USER_ORBIT_SELECTION) {
5997 float w[3];
5998 int tri_index;
5999
6000 tri_index = project_paint_PickFace(ps, pos, w);
6001
6002 if (tri_index != -1) {
6003 const int3 &tri = ps->corner_tris_eval[tri_index];
6004 const int vert_tri[3] = {PS_CORNER_TRI_AS_VERT_INDEX_3(ps, tri)};
6005 float world[3];
6006
6007 interp_v3_v3v3v3(world,
6008 ps->vert_positions_eval[vert_tri[0]],
6009 ps->vert_positions_eval[vert_tri[1]],
6010 ps->vert_positions_eval[vert_tri[2]],
6011 w);
6012
6013 mul_m4_v3(ps->obmat, world);
6015 }
6016 }
6017
6018 return touch_any;
6019}
6020
6021static void paint_proj_stroke_ps(const bContext * /*C*/,
6022 void *ps_handle_p,
6023 const float prev_pos[2],
6024 const float pos[2],
6025 const bool eraser,
6026 float pressure,
6027 float distance,
6028 float size,
6029 /* extra view */
6030 ProjPaintState *ps)
6031{
6032 ProjStrokeHandle *ps_handle = static_cast<ProjStrokeHandle *>(ps_handle_p);
6033 const Paint *paint = ps->paint;
6034 Brush *brush = ps->brush;
6035 Scene *scene = ps->scene;
6036
6037 ps->brush_size = size;
6038 ps->blend = brush->blend;
6039 if (eraser) {
6041 }
6042
6043 /* handle gradient and inverted stroke color here */
6046 brush,
6047 ps_handle->initial_hsv_jitter,
6049 distance,
6050 pressure,
6051 ps->paint_color_linear);
6052
6053 /* Cache colorspace info per image for performance. */
6054 for (int i = 0; i < ps->image_tot; i++) {
6055 ProjPaintImage *img = &ps->projImages[i];
6056 const ImBuf *ibuf = img->ibuf;
6057
6059 img->byte_colorspace = nullptr;
6060 img->is_data = false;
6061 img->is_srgb = false;
6062
6063 if (ibuf->colorspace_is_data()) {
6064 img->is_data = true;
6065 }
6066 else if (ibuf->byte_data() && ibuf->byte_buffer.colorspace) {
6069 if (img->is_srgb) {
6071 img->paint_color_byte);
6072 }
6073 else {
6075 img->byte_colorspace);
6076 }
6077 }
6078 }
6079 }
6080 else if (ps->brush_type == IMAGE_PAINT_BRUSH_TYPE_MASK) {
6081 ps->stencil_value = brush->weight;
6082
6083 if ((ps->mode == BrushStrokeMode::Invert) ^
6085 {
6086 ps->stencil_value = 1.0f - ps->stencil_value;
6087 }
6088 }
6089
6090 if (project_paint_op(ps, prev_pos, pos)) {
6091 ps_handle->need_redraw = true;
6093 }
6094}
6095
6097 void *ps_handle_p,
6098 const float prev_pos[2],
6099 const float pos[2],
6100 const bool eraser,
6101 float pressure,
6102 float distance,
6103 float size)
6104{
6105 int i;
6106 ProjStrokeHandle *ps_handle = static_cast<ProjStrokeHandle *>(ps_handle_p);
6107
6108 /* clone gets special treatment here to avoid going through image initialization */
6109 if (ps_handle->is_clone_cursor_pick) {
6110 Scene *scene = ps_handle->scene;
6111 Depsgraph *depsgraph = CTX_data_ensure_evaluated_depsgraph(C);
6112 View3D *v3d = CTX_wm_view3d(C);
6113 ARegion *region = CTX_wm_region(C);
6114 float *cursor = scene->cursor.location;
6115 const int mval_i[2] = {int(pos[0]), int(pos[1])};
6116
6118
6119 /* Ensure the depth buffer is updated for #ED_view3d_autodist. */
6121 depsgraph, region, v3d, nullptr, V3D_DEPTH_NO_GPENCIL, false, nullptr);
6122
6123 if (!ED_view3d_autodist(region, v3d, mval_i, cursor, nullptr)) {
6124 return;
6125 }
6126
6128 ED_region_tag_redraw(region);
6129
6130 return;
6131 }
6132
6133 for (i = 0; i < ps_handle->ps_views_tot; i++) {
6134 ProjPaintState *ps = ps_handle->ps_views[i];
6135 paint_proj_stroke_ps(C, ps_handle_p, prev_pos, pos, eraser, pressure, distance, size, ps);
6136 }
6137}
6138
6139/* initialize project paint settings from context */
6141 Object *ob,
6142 ProjPaintState *ps,
6143 const BrushStrokeMode mode,
6144 const BrushSwitchMode brush_switch_mode)
6145{
6146 Scene *scene = CTX_data_scene(C);
6147 ToolSettings *settings = scene->toolsettings;
6148
6149 /* brush */
6150 ps->mode = mode;
6151 ps->brush_switch_mode = brush_switch_mode;
6153 ps->brush = BKE_paint_brush(&settings->imapaint.paint);
6154 if (ps->brush) {
6155 Brush *brush = ps->brush;
6156 ps->brush_type = brush->image_brush_type;
6157 ps->blend = brush->blend;
6158 if (brush_switch_mode == BrushSwitchMode::Smooth) {
6160 }
6161 /* only check for inversion for the soften brush, elsewhere,
6162 * a resident brush inversion flag can cause issues */
6164 ps->mode = (ps->mode == BrushStrokeMode::Invert) != ((brush->flag & BRUSH_DIR_IN) != 0) ?
6167
6169 }
6170
6171 /* disable for 3d mapping also because painting on mirrored mesh can create "stripes" */
6173 ps->is_texbrush = (brush->mtex.tex && ps->brush_type == IMAGE_PAINT_BRUSH_TYPE_DRAW) ? true :
6174 false;
6175 ps->is_maskbrush = (brush->mask_mtex.tex) ? true : false;
6176 }
6177 else {
6178 /* Brush may be nullptr. */
6179 ps->do_masking = false;
6180 ps->is_texbrush = false;
6181 ps->is_maskbrush = false;
6182 }
6183
6184 /* sizeof(ProjPixel), since we alloc this a _lot_ */
6186 BLI_assert(ps->pixel_sizeof >= sizeof(ProjPixel));
6187
6188 /* these can be nullptr */
6189 ps->v3d = CTX_wm_view3d(C);
6191 ps->region = CTX_wm_region(C);
6192
6194 ps->scene = scene;
6195 /* allow override of active object */
6196 ps->ob = ob;
6197
6199 ps->stencil_ima = settings->imapaint.stencil;
6200 ps->canvas_ima = (!ps->do_material_slots) ? settings->imapaint.canvas : nullptr;
6201 ps->clone_ima = (!ps->do_material_slots) ? settings->imapaint.clone : nullptr;
6202
6204 ps->cavity_curve = settings->imapaint.paint.cavity_curve;
6205
6206 /* setup projection painting data */
6209 ps->do_occlude = !(settings->imapaint.flag & IMAGEPAINT_PROJECT_XRAY);
6211 }
6212 else {
6213 ps->do_backfacecull = ps->do_occlude = ps->do_mask_normal = false;
6214 }
6215
6218 }
6219
6221 /* deactivate stenciling for the stencil brush :) */
6223 !(ps->do_stencil_brush) && ps->stencil_ima);
6225 0);
6226
6227#ifndef PROJ_DEBUG_NOSEAMBLEED
6228 /* pixel num to bleed */
6229 ps->seam_bleed_px = settings->imapaint.seam_bleed;
6231#endif
6232
6233 if (ps->do_mask_normal) {
6235 ps->normal_angle = (ps->normal_angle_inner + 90.0f) * 0.5f;
6236 }
6237 else {
6239 }
6240
6241 ps->normal_angle_inner *= float(M_PI_2 / 90);
6242 ps->normal_angle *= float(M_PI_2 / 90);
6244
6245 if (ps->normal_angle_range <= 0.0f) {
6246 /* no need to do blending */
6247 ps->do_mask_normal = false;
6248 }
6249
6252
6253 ps->dither = settings->imapaint.dither;
6254}
6255
6257 Object *ob,
6258 const float mouse[2],
6259 const BrushStrokeMode mode,
6260 const BrushSwitchMode brush_switch_mode)
6261{
6262 ProjStrokeHandle *ps_handle;
6263 Scene *scene = CTX_data_scene(C);
6264 ToolSettings *settings = scene->toolsettings;
6265 char symmetry_flag_views[BOUNDED_ARRAY_TYPE_SIZE<decltype(ps_handle->ps_views)>()] = {0};
6266
6267 ps_handle = MEM_new<ProjStrokeHandle>("ProjStrokeHandle");
6268 ps_handle->scene = scene;
6270 ps_handle->brush = BKE_paint_brush(&settings->imapaint.paint);
6271
6272 if (BKE_brush_color_jitter_get_settings(&settings->imapaint.paint, ps_handle->brush)) {
6274 }
6275
6276 if (mode == BrushStrokeMode::Invert) {
6277 /* Bypass regular stroke logic. */
6280 ps_handle->is_clone_cursor_pick = true;
6281 return ps_handle;
6282 }
6283 }
6284
6285 ps_handle->orig_brush_size = BKE_brush_size_get(ps_handle->paint, ps_handle->brush);
6286
6288 ps_handle->symmetry_flags = mesh->symmetry & ME_SYMMETRY_XYZ;
6289 ps_handle->ps_views_tot = 1 + (pow_i(2, count_bits_i(ps_handle->symmetry_flags)) - 1);
6290 const bool is_multi_view = (ps_handle->ps_views_tot != 1);
6291
6292 for (int i = 0; i < ps_handle->ps_views_tot; i++) {
6293 ProjPaintState *ps = MEM_new<ProjPaintState>("ProjectionPaintState");
6294 ps_handle->ps_views[i] = ps;
6295 }
6296
6297 if (ps_handle->symmetry_flags) {
6298 int index = 0;
6299
6300 int x = 0;
6301 do {
6302 int y = 0;
6303 do {
6304 int z = 0;
6305 do {
6306 symmetry_flag_views[index++] = ((x ? PAINT_SYMM_X : 0) | (y ? PAINT_SYMM_Y : 0) |
6307 (z ? PAINT_SYMM_Z : 0));
6308 BLI_assert(index <= ps_handle->ps_views_tot);
6309 } while ((z++ == 0) && (ps_handle->symmetry_flags & PAINT_SYMM_Z));
6310 } while ((y++ == 0) && (ps_handle->symmetry_flags & PAINT_SYMM_Y));
6311 } while ((x++ == 0) && (ps_handle->symmetry_flags & PAINT_SYMM_X));
6312 BLI_assert(index == ps_handle->ps_views_tot);
6313 }
6314
6315 for (int i = 0; i < ps_handle->ps_views_tot; i++) {
6316 ProjPaintState *ps = ps_handle->ps_views[i];
6317
6318 project_state_init(C, ob, ps, mode, brush_switch_mode);
6319
6320 if (ps->ob == nullptr) {
6321 ps_handle->ps_views_tot = i + 1;
6322 goto fail;
6323 }
6324 }
6325
6326 /* TODO: Inspect this further. */
6327 /* Don't allow brush size below 2 */
6328 if (BKE_brush_size_get(&settings->imapaint.paint, ps_handle->brush) < 2) {
6329 BKE_brush_size_set(&settings->imapaint.paint, ps_handle->brush, 2 * U.pixelsize);
6330 }
6331
6332 /* allocate and initialize spatial data structures */
6333
6334 for (int i = 0; i < ps_handle->ps_views_tot; i++) {
6335 ProjPaintState *ps = ps_handle->ps_views[i];
6336
6340
6341 /* re-use! */
6342 if (i != 0) {
6343 ps->is_shared_user = true;
6344 PROJ_PAINT_STATE_SHARED_MEMCPY(ps, ps_handle->ps_views[0]);
6345 }
6346
6347 project_paint_begin(C, ps, is_multi_view, symmetry_flag_views[i]);
6348 if (ps->mesh_eval == nullptr) {
6349 goto fail;
6350 }
6351 /* Shared views index into the first view's `projImages`. */
6352 BLI_assert(ps->image_tot == ps_handle->ps_views[0]->image_tot);
6353
6354 paint_proj_begin_clone(ps, mouse);
6355 }
6356
6357 paint_brush_init_tex(ps_handle->brush);
6358
6359 return ps_handle;
6360
6361fail:
6362 for (int i = 0; i < ps_handle->ps_views_tot; i++) {
6363 MEM_delete(ps_handle->ps_views[i]);
6364 }
6365 MEM_delete(ps_handle);
6366 return nullptr;
6367}
6368
6369void paint_proj_redraw(const bContext *C, void *ps_handle_p, bool final)
6370{
6371 ProjStrokeHandle *ps_handle = static_cast<ProjStrokeHandle *>(ps_handle_p);
6372
6373 if (ps_handle->need_redraw) {
6374 ps_handle->need_redraw = false;
6375 }
6376 else if (!final) {
6377 return;
6378 }
6379
6380 if (final) {
6381 /* compositor listener deals with updating */
6383 }
6384 else {
6386 }
6387}
6388
6389void paint_proj_stroke_done(void *ps_handle_p, wmPaintCursor *cursor)
6390{
6391 ProjStrokeHandle *ps_handle = static_cast<ProjStrokeHandle *>(ps_handle_p);
6392
6393 if (ps_handle->is_clone_cursor_pick) {
6394 MEM_delete(ps_handle);
6395 return;
6396 }
6397
6398 for (int i = 1; i < ps_handle->ps_views_tot; i++) {
6400 }
6401
6402 BKE_brush_size_set(ps_handle->paint, ps_handle->brush, ps_handle->orig_brush_size);
6403
6404 paint_brush_exit_tex(ps_handle->brush);
6405
6406 for (int i = 0; i < ps_handle->ps_views_tot; i++) {
6407 ProjPaintState *ps;
6408 ps = ps_handle->ps_views[i];
6410 MEM_delete(ps);
6411 }
6412
6413 MEM_delete(ps_handle);
6414 if (cursor) {
6415 WM_paint_cursor_end(cursor);
6416 }
6417}
6418/* use project paint to re-apply an image */
6420{
6421 Main *bmain = CTX_data_main(C);
6422 Image *image = static_cast<Image *>(
6423 BLI_findlink(&bmain->images, RNA_enum_get(op->ptr, "image")));
6424 Scene &scene = *CTX_data_scene(C);
6425 ViewLayer &view_layer = *CTX_data_view_layer(C);
6426 ProjPaintState ps = {nullptr};
6427 int orig_brush_size;
6428 IDProperty *idgroup;
6429 IDProperty *view_data = nullptr;
6430 BKE_view_layer_synced_ensure(*bmain, &scene, &view_layer);
6431 Object *ob = BKE_view_layer_active_object_get(&view_layer);
6432 bool uvs, mat, tex;
6433
6434 if (ob == nullptr || ob->type != OB_MESH) {
6435 BKE_report(op->reports, RPT_ERROR, "No active mesh object");
6436 return OPERATOR_CANCELLED;
6437 }
6438
6439 if (!ED_paint_proj_mesh_data_check(scene, *ob, &uvs, &mat, &tex, nullptr)) {
6440 ED_paint_data_warning(op->reports, uvs, mat, tex, true);
6442 return OPERATOR_CANCELLED;
6443 }
6444
6446
6447 if (image == nullptr) {
6448 BKE_report(op->reports, RPT_ERROR, "Image could not be found");
6449 return OPERATOR_CANCELLED;
6450 }
6451
6453 ps.reproject_ibuf = BKE_image_acquire_ibuf(image, nullptr, nullptr);
6454
6455 if ((ps.reproject_ibuf == nullptr) ||
6456 ((ps.reproject_ibuf->byte_data() || ps.reproject_ibuf->float_data()) == false))
6457 {
6458 BKE_report(op->reports, RPT_ERROR, "Image data could not be found");
6459 return OPERATOR_CANCELLED;
6460 }
6461
6462 idgroup = IDP_GetProperties(&image->id);
6463
6464 if (idgroup) {
6466
6467 /* type check to make sure its ok */
6468 if (view_data != nullptr &&
6469 (view_data->len != PROJ_VIEW_DATA_SIZE || view_data->subtype != IDP_FLOAT))
6470 {
6471 BKE_report(op->reports, RPT_ERROR, "Image project data invalid");
6472 return OPERATOR_CANCELLED;
6473 }
6474 }
6475
6476 if (view_data) {
6477 /* image has stored view projection info */
6479 }
6480 else {
6482
6483 if (scene.camera == nullptr) {
6484 BKE_report(op->reports, RPT_ERROR, "No active camera set");
6485 return OPERATOR_CANCELLED;
6486 }
6487 }
6488
6489 /* override */
6490 ps.is_texbrush = false;
6491 ps.is_maskbrush = false;
6492 ps.do_masking = false;
6493 orig_brush_size = BKE_brush_size_get(ps.paint, ps.brush);
6494 /* cover the whole image */
6495 BKE_brush_size_set(ps.paint, ps.brush, 32 * U.pixelsize);
6496
6497 /* so pixels are initialized with minimal info */
6499
6501
6502 /* allocate and initialize spatial data structures */
6503 project_paint_begin(C, &ps, false, 0);
6504
6505 if (ps.mesh_eval == nullptr) {
6506 BKE_brush_size_set(ps.paint, ps.brush, orig_brush_size);
6507 BKE_report(op->reports, RPT_ERROR, "Could not get valid evaluated mesh");
6508 return OPERATOR_CANCELLED;
6509 }
6510
6512
6513 const float pos[2] = {0.0, 0.0};
6514 const float lastpos[2] = {0.0, 0.0};
6515 int a;
6516
6517 project_paint_op(&ps, lastpos, pos);
6518
6520
6521 for (a = 0; a < ps.image_tot; a++) {
6524 }
6525
6526 project_paint_end(&ps);
6527
6529
6531 BKE_brush_size_set(ps.paint, ps.brush, orig_brush_size);
6532
6533 return OPERATOR_FINISHED;
6534}
6535
6537{
6538 PropertyRNA *prop;
6539
6540 /* identifiers */
6541 ot->name = "Project Image";
6542 ot->idname = "PAINT_OT_project_image";
6543 ot->description = "Project an edited render from the active camera back onto the object";
6544
6545 /* API callbacks. */
6548
6549 /* flags */
6551
6552 prop = RNA_def_enum(ot->srna, "image", rna_enum_dummy_NULL_items, 0, "Image", "");
6555 ot->prop = prop;
6556}
6557
6559{
6560 bScreen *screen = CTX_wm_screen(C);
6561 if (!(screen && BKE_screen_find_big_area(screen, SPACE_VIEW3D, 0))) {
6562 CTX_wm_operator_poll_msg_set(C, "No 3D viewport found to create image from");
6563 return false;
6564 }
6565 if (G.background || !GPU_is_init()) {
6566 return false;
6567 }
6568 return true;
6569}
6570
6572{
6573 Image *image;
6574 ImBuf *ibuf;
6575 char filepath[FILE_MAX];
6576
6577 Main *bmain = CTX_data_main(C);
6578 Depsgraph *depsgraph = CTX_data_ensure_evaluated_depsgraph(C);
6579 Scene *scene = CTX_data_scene(C);
6580 ToolSettings *settings = scene->toolsettings;
6581 int w = settings->imapaint.screen_grab_size[0];
6582 int h = settings->imapaint.screen_grab_size[1];
6583 int maxsize;
6584 char err_out[256] = "unknown";
6585
6587 if (!area) {
6588 BKE_report(op->reports, RPT_ERROR, "No 3D viewport found to create image from");
6589 return OPERATOR_CANCELLED;
6590 }
6591
6593 if (!region) {
6594 BKE_report(op->reports, RPT_ERROR, "No 3D viewport found to create image from");
6595 return OPERATOR_CANCELLED;
6596 }
6597 RegionView3D *rv3d = static_cast<RegionView3D *>(region->regiondata);
6598
6599 RNA_string_get(op->ptr, "filepath", filepath);
6600
6601 maxsize = GPU_max_texture_size();
6602
6603 w = std::min(w, maxsize);
6604 h = std::min(h, maxsize);
6605
6606 /* Create a copy of the overlays where they are all turned off, except the
6607 * texture paint overlay opacity */
6608 View3D *v3d = area->spacedata.first_as<View3D>();
6609 View3D v3d_copy = dna::shallow_copy(*v3d);
6610 v3d_copy.gridflag = eView3D_GridFlag{};
6611 v3d_copy.flag2 = eView3D_Flag2{};
6612 v3d_copy.flag = V3D_HIDE_HELPLINES;
6613 v3d_copy.gizmo_flag = V3D_GIZMO_HIDE;
6614
6615 _DNA_internal_memzero(&v3d_copy.overlay, sizeof(View3DOverlay));
6616 v3d_copy.overlay.flag = V3D_OVERLAY_HIDE_CURSOR | V3D_OVERLAY_HIDE_TEXT |
6619 v3d_copy.overlay.texture_paint_mode_opacity = v3d->overlay.texture_paint_mode_opacity;
6620
6621 ibuf = ED_view3d_draw_offscreen_imbuf(depsgraph,
6622 scene,
6623 eDrawType(v3d_copy.shading.type),
6624 &v3d_copy,
6625 region,
6626 w,
6627 h,
6630 nullptr,
6631 false,
6632 nullptr,
6633 nullptr,
6634 true,
6635 err_out);
6636
6637 if (!ibuf) {
6638 /* NOTE(@sergey): Mostly happens when OpenGL off-screen buffer was failed to create, */
6639 /* but could be other reasons. Should be handled in the future. */
6640 BKE_reportf(op->reports, RPT_ERROR, "Failed to create OpenGL off-screen buffer: %s", err_out);
6641 return OPERATOR_CANCELLED;
6642 }
6643
6644 ibuf->filepath = filepath;
6645
6646 image = BKE_image_add_from_imbuf(bmain, ibuf, "image_view");
6647
6648 /* Drop reference to ibuf so that the image owns it */
6649 IMB_freeImBuf(ibuf);
6650
6651 if (image) {
6652 /* now for the trickiness. store the view projection here!
6653 * re-projection will reuse this */
6654 IDProperty *idgroup = IDP_EnsureProperties(&image->id);
6655
6657 array.extend(Span(reinterpret_cast<float *>(rv3d->winmat), 16));
6658 array.extend(Span(reinterpret_cast<float *>(rv3d->viewmat), 16));
6659 float clip_start;
6660 float clip_end;
6661 const bool is_ortho = ED_view3d_clip_range_get(
6662 depsgraph, v3d, rv3d, true, &clip_start, &clip_end);
6663 array.append(clip_start);
6664 array.append(clip_end);
6665 /* using float for a bool is dodgy but since its an extra member in the array...
6666 * easier than adding a single bool prop */
6667 array.append(is_ortho ? 1.0f : 0.0f);
6668 IDP_AddToGroup(idgroup, bke::idprop::create(PROJ_VIEW_DATA_ID, array.as_span()).release());
6669 }
6670
6671 return OPERATOR_FINISHED;
6672}
6673
6675{
6676 /* identifiers */
6677 ot->name = "Image from View";
6678 ot->idname = "PAINT_OT_image_from_view";
6679 ot->description = "Make an image from biggest 3D view for reprojection";
6680
6681 /* API callbacks. */
6684
6685 /* flags */
6686 ot->flag = OPTYPE_REGISTER;
6687
6689 ot->srna, "filepath", nullptr, FILE_MAX, "File Path", "Name of the file");
6690}
6691
6692/* -------------------------------------------------------------------- */
6695
6697 ReportList *reports, bool has_uvs, bool has_mat, bool has_tex, bool has_stencil)
6698{
6699 BKE_reportf(reports,
6701 "Missing%s%s%s%s detected!",
6702 !has_uvs ? RPT_(" UVs,") : "",
6703 !has_mat ? RPT_(" Materials,") : "",
6704 !has_tex ? RPT_(" Textures (or linked),") : "",
6705 !has_stencil ? RPT_(" Stencil,") : "");
6706}
6707
6709 Object &ob,
6710 bool *r_has_uvs,
6711 bool *r_has_mat,
6712 bool *r_has_tex,
6713 bool *r_has_stencil)
6714{
6715 ImagePaintSettings &imapaint = scene.toolsettings->imapaint;
6716 const Brush *br = BKE_paint_brush(&imapaint.paint);
6717 bool has_mat = true;
6718 bool has_tex = true;
6719 bool has_stencil = true;
6720 bool has_uvs = true;
6721
6723
6724 BLI_assert(ob.type == OB_MESH);
6725
6726 if (imapaint.mode == IMAGEPAINT_MODE_MATERIAL) {
6727 /* no material, add one */
6728 if (ob.totcol == 0) {
6729 has_mat = false;
6730 has_tex = false;
6731 }
6732 else {
6733 /* there may be material slots but they may be empty, check */
6734 has_mat = false;
6735 has_tex = false;
6736
6737 for (int i = 1; i < ob.totcol + 1; i++) {
6738 Material *ma = BKE_object_material_get(&ob, i);
6739
6740 if (ma && ID_IS_EDITABLE(ma) && !ID_IS_OVERRIDE_LIBRARY(ma)) {
6741 has_mat = true;
6742 if (ma->texpaintslot == nullptr) {
6743 /* refresh here just in case */
6744 BKE_texpaint_slot_refresh_cache(&scene, ma, &ob);
6745 }
6746 if (ma->texpaintslot != nullptr &&
6747 ma->texpaintslot[ma->paint_active_slot].ima != nullptr &&
6748 ID_IS_EDITABLE(ma->texpaintslot[ma->paint_active_slot].ima) &&
6749 !ID_IS_OVERRIDE_LIBRARY(ma->texpaintslot[ma->paint_active_slot].ima))
6750 {
6751 has_tex = true;
6752 break;
6753 }
6754 }
6755 }
6756 }
6757 }
6758 else if (imapaint.mode == IMAGEPAINT_MODE_IMAGE) {
6759 if (imapaint.canvas == nullptr || !ID_IS_EDITABLE(imapaint.canvas)) {
6760 has_tex = false;
6761 }
6762 }
6763
6765 int layernum = mesh->uv_map_names().size();
6766
6767 if (layernum == 0) {
6768 has_uvs = false;
6769 }
6770
6771 /* Make sure we have a stencil to paint on! */
6774
6775 if (imapaint.stencil == nullptr) {
6776 has_stencil = false;
6777 }
6778 }
6779
6780 if (!has_uvs) {
6782 }
6783 if (!has_mat) {
6785 }
6786 if (!has_tex) {
6788 }
6789 if (!has_stencil) {
6791 }
6792
6793 if (r_has_uvs) {
6794 *r_has_uvs = has_uvs;
6795 }
6796 if (r_has_mat) {
6797 *r_has_mat = has_mat;
6798 }
6799 if (r_has_tex) {
6800 *r_has_tex = has_tex;
6801 }
6802 if (r_has_stencil) {
6803 *r_has_stencil = has_stencil;
6804 }
6805
6806 return has_uvs && has_mat && has_tex && has_stencil;
6807}
6808
6810
6811/* -------------------------------------------------------------------- */
6814
6815enum {
6823};
6824
6826 {LAYER_BASE_COLOR, "BASE_COLOR", 0, "Base Color", ""},
6827 {LAYER_SPECULAR, "SPECULAR", 0, "Specular IOR Level", ""},
6828 {LAYER_ROUGHNESS, "ROUGHNESS", 0, "Roughness", ""},
6829 {LAYER_METALLIC, "METALLIC", 0, "Metallic", ""},
6830 {LAYER_NORMAL, "NORMAL", 0, "Normal", ""},
6831 {LAYER_BUMP, "BUMP", 0, "Bump", ""},
6832 {LAYER_DISPLACEMENT, "DISPLACEMENT", 0, "Displacement", ""},
6833 {0, nullptr, 0, nullptr, nullptr},
6834};
6835
6837{
6839 if (!ma) {
6840 Main *bmain = CTX_data_main(C);
6841 ma = BKE_material_add(bmain, "Material");
6843 nodes::node_tree_shader_default(C, bmain, &ma->id);
6844 }
6845 return ma;
6846}
6847
6848static Image *proj_paint_image_create(wmOperator *op, Main *bmain, bool is_data)
6849{
6850 Image *ima;
6851 float color[4] = {0.0f, 0.0f, 0.0f, 1.0f};
6852 char imagename[MAX_ID_NAME - 2] = "Material Diffuse Color";
6853 int width = 1024;
6854 int height = 1024;
6855 bool use_float = false;
6856 bool use_tiled = false;
6857 short gen_type = IMA_GENTYPE_BLANK;
6858 bool alpha = false;
6859
6860 if (op) {
6861 width = RNA_int_get(op->ptr, "width");
6862 height = RNA_int_get(op->ptr, "height");
6863 use_float = RNA_boolean_get(op->ptr, "float");
6864 use_tiled = RNA_boolean_get(op->ptr, "tiled");
6865 gen_type = RNA_enum_get(op->ptr, "generated_type");
6866 RNA_float_get_array(op->ptr, "color", color);
6867 alpha = RNA_boolean_get(op->ptr, "alpha");
6868 RNA_string_get(op->ptr, "name", imagename);
6869 }
6870
6871 if (!alpha) {
6872 color[3] = 1.0f;
6873 }
6874
6875 ima = BKE_image_add_generated(bmain,
6876 width,
6877 height,
6878 imagename,
6879 alpha ? 32 : 24,
6880 use_float,
6881 gen_type,
6882 color,
6883 false,
6884 is_data,
6885 use_tiled);
6886
6887 return ima;
6888}
6889
6893static std::optional<std::string> proj_paint_color_attribute_create(wmOperator *op, Object &ob)
6894{
6895 char name[MAX_NAME] = "";
6896 float color[4] = {0.0f, 0.0f, 0.0f, 1.0f};
6899
6900 if (op) {
6901 RNA_string_get(op->ptr, "name", name);
6902 RNA_float_get_array(op->ptr, "color", color);
6903 domain = bke::AttrDomain(RNA_enum_get(op->ptr, "domain"));
6904 type = eCustomDataType(RNA_enum_get(op->ptr, "data_type"));
6905 }
6906
6907 Mesh *mesh = id_cast<Mesh *>(ob.data);
6909 std::string unique_name = BKE_attribute_calc_unique_name(owner, name);
6910 bke::MutableAttributeAccessor attributes = mesh->attributes_for_write();
6911 if (!attributes.add(unique_name,
6912 domain,
6915 {
6916 return std::nullopt;
6917 }
6918
6919 BKE_id_attributes_active_color_set(&mesh->id, unique_name);
6920 if (!mesh->default_color_attribute) {
6921 BKE_id_attributes_default_color_set(&mesh->id, unique_name);
6922 }
6923
6925
6926 return unique_name;
6927}
6928
6936static void default_paint_slot_color_get(int layer_type, Material *ma, float color[4])
6937{
6938 switch (layer_type) {
6939 case LAYER_BASE_COLOR:
6940 case LAYER_SPECULAR:
6941 case LAYER_ROUGHNESS:
6942 case LAYER_METALLIC: {
6943 bNodeTree *ntree = nullptr;
6944 bNode *in_node = nullptr;
6945 if (ma && ma->nodetree) {
6946 ma->nodetree->ensure_topology_cache();
6947 const Span<bNode *> nodes = ma->nodetree->nodes_by_type("ShaderNodeBsdfPrincipled"_ustr);
6948 in_node = nodes.is_empty() ? nullptr : nodes.first();
6949 }
6950 if (!in_node) {
6951 /* An existing material or Principled BSDF node could not be found.
6952 * Copy default color values from a default Principled BSDF instead. */
6954 nullptr, "Temporary Shader Nodetree", ntreeType_Shader->idname.ref());
6955 in_node = bke::node_add_static_node(nullptr, *ntree, SH_NODE_BSDF_PRINCIPLED);
6956 }
6958 *in_node, SOCK_IN, UString(layer_type_items[layer_type].name));
6959 switch (in_sock->type) {
6960 case SOCK_FLOAT: {
6961 bNodeSocketValueFloat *socket_data = static_cast<bNodeSocketValueFloat *>(
6962 in_sock->default_value);
6963 copy_v3_fl(color, socket_data->value);
6964 color[3] = 1.0f;
6965 break;
6966 }
6967 case SOCK_VECTOR:
6968 case SOCK_RGBA: {
6969 bNodeSocketValueRGBA *socket_data = static_cast<bNodeSocketValueRGBA *>(
6970 in_sock->default_value);
6971 copy_v3_v3(color, socket_data->value);
6972 color[3] = 1.0f;
6973 break;
6974 }
6975 default:
6977 rgba_float_args_set(color, 0.0f, 0.0f, 0.0f, 1.0f);
6978 break;
6979 }
6980 /* Cleanup */
6981 if (ntree) {
6983 MEM_delete(ntree);
6984 }
6985 return;
6986 }
6987 case LAYER_NORMAL:
6988 /* Neutral tangent space normal map. */
6989 rgba_float_args_set(color, 0.5f, 0.5f, 1.0f, 1.0f);
6990 break;
6991 case LAYER_BUMP:
6992 case LAYER_DISPLACEMENT:
6993 /* Neutral displacement and bump map. */
6994 rgba_float_args_set(color, 0.5f, 0.5f, 0.5f, 1.0f);
6995 break;
6996 }
6997}
6998
7000{
7002 Scene *scene = CTX_data_scene(C);
7003 Material *ma;
7004 Image *ima = nullptr;
7005 CustomDataLayer *layer = nullptr;
7006
7007 if (!ob) {
7008 return false;
7009 }
7010
7012
7013 if (ma) {
7014 Main *bmain = CTX_data_main(C);
7015 int type = RNA_enum_get(op->ptr, "type");
7016 bool is_data = (type > LAYER_BASE_COLOR);
7017
7018 bNode *new_node;
7019 bNodeTree *ntree = ma->nodetree;
7020
7021 if (!ntree) {
7022 nodes::node_tree_shader_default(C, bmain, &ma->id);
7023 ntree = ma->nodetree;
7024 }
7025
7026 const ePaintCanvasSource slot_type = ob->mode == OB_MODE_SCULPT ?
7028 RNA_enum_get(op->ptr, "slot_type")) :
7030
7031 /* Create a new node. */
7032 switch (slot_type) {
7034 new_node = bke::node_add_static_node(C, *ntree, SH_NODE_TEX_IMAGE);
7035 ima = proj_paint_image_create(op, bmain, is_data);
7036 new_node->id = &ima->id;
7037 break;
7038 }
7040 new_node = bke::node_add_static_node(C, *ntree, SH_NODE_ATTRIBUTE);
7041 if (const std::optional<std::string> name = proj_paint_color_attribute_create(op, *ob)) {
7042 STRNCPY_UTF8(((NodeShaderAttribute *)new_node->storage)->name, name->c_str());
7043 }
7044 break;
7045 }
7048 return false;
7049 }
7050 bke::node_set_active(*ntree, *new_node);
7051
7052 /* Connect to first available principled BSDF node. */
7053 ntree->ensure_topology_cache();
7054 const Span<bNode *> bsdf_nodes = ntree->nodes_by_type("ShaderNodeBsdfPrincipled"_ustr);
7055 bNode *in_node = bsdf_nodes.is_empty() ? nullptr : bsdf_nodes.first();
7056 bNode *out_node = new_node;
7057
7058 if (in_node != nullptr) {
7059 bNodeSocket *out_sock = bke::node_find_socket(*out_node, SOCK_OUT, "Color"_ustr);
7060 bNodeSocket *in_sock = nullptr;
7061
7062 if (type >= LAYER_BASE_COLOR && type < LAYER_NORMAL) {
7063 in_sock = bke::node_find_socket(*in_node, SOCK_IN, UString(layer_type_items[type].name));
7064 }
7065 else if (type == LAYER_NORMAL) {
7066 bNode *nor_node;
7067 nor_node = bke::node_add_static_node(C, *ntree, SH_NODE_NORMAL_MAP);
7068
7069 in_sock = bke::node_find_socket(*nor_node, SOCK_IN, "Color"_ustr);
7070 bke::node_add_link(*ntree, *out_node, *out_sock, *nor_node, *in_sock);
7071
7072 in_sock = bke::node_find_socket(*in_node, SOCK_IN, "Normal"_ustr);
7073 out_sock = bke::node_find_socket(*nor_node, SOCK_OUT, "Normal"_ustr);
7074
7075 out_node = nor_node;
7076 }
7077 else if (type == LAYER_BUMP) {
7078 bNode *bump_node;
7079 bump_node = bke::node_add_static_node(C, *ntree, SH_NODE_BUMP);
7080
7081 in_sock = bke::node_find_socket(*bump_node, SOCK_IN, "Height"_ustr);
7082 bke::node_add_link(*ntree, *out_node, *out_sock, *bump_node, *in_sock);
7083
7084 in_sock = bke::node_find_socket(*in_node, SOCK_IN, "Normal"_ustr);
7085 out_sock = bke::node_find_socket(*bump_node, SOCK_OUT, "Normal"_ustr);
7086
7087 out_node = bump_node;
7088 }
7089 else if (type == LAYER_DISPLACEMENT) {
7090 /* Connect to the displacement output socket */
7091 const Span<bNode *> output_nodes = ntree->nodes_by_type("ShaderNodeOutputMaterial"_ustr);
7092 in_node = output_nodes.is_empty() ? nullptr : output_nodes.first();
7093
7094 if (in_node != nullptr) {
7095 in_sock = bke::node_find_socket(*in_node, SOCK_IN, UString(layer_type_items[type].name));
7096 }
7097 else {
7098 in_sock = nullptr;
7099 }
7100 }
7101
7102 /* Check if the socket in already connected to something */
7103 bNodeLink *link = in_sock ? in_sock->link : nullptr;
7104 if (in_sock != nullptr && link == nullptr) {
7105 bke::node_add_link(*ntree, *out_node, *out_sock, *in_node, *in_sock);
7106
7107 bke::node_position_relative(*out_node, *in_node, out_sock, *in_sock);
7108 }
7109 }
7110
7112 /* In case we added more than one node, position them too. */
7114
7115 if (ima) {
7116 BKE_texpaint_slot_refresh_cache(scene, ma, ob);
7117 BKE_image_signal(bmain, ima, nullptr, IMA_SIGNAL_USER_NEW_IMAGE);
7119 ED_space_image_sync(bmain, ima, false);
7120 }
7121 if (layer) {
7122 BKE_texpaint_slot_refresh_cache(scene, ma, ob);
7125 }
7126
7127 DEG_id_tag_update(&ntree->id, 0);
7131
7132 ED_paint_proj_mesh_data_check(*scene, *ob, nullptr, nullptr, nullptr, nullptr);
7133
7134 return true;
7135 }
7136
7137 return false;
7138}
7139
7140static int get_texture_layer_type(wmOperator *op, const char *prop_name)
7141{
7142 int type_value = RNA_enum_get(op->ptr, prop_name);
7143 int type = RNA_enum_from_value(layer_type_items, type_value);
7144 BLI_assert(type != -1);
7145 return type;
7146}
7147
7155
7157 int texture_type,
7158 char *dst,
7159 int dst_maxncpy)
7160{
7162 const char *base_name = ma ? &ma->id.name[2] : &ob->id.name[2];
7164 dst, dst_maxncpy, "%s %s", base_name, DATA_(layer_type_items[texture_type].name));
7165}
7166
7168 wmOperator *op,
7169 const wmEvent * /*event*/)
7170{
7173
7174 int type = get_texture_layer_type(op, "type");
7175
7176 /* Set default name. */
7177 char imagename[MAX_ID_NAME - 2];
7179 ob, type, reinterpret_cast<char *>(&imagename), sizeof(imagename));
7180 RNA_string_set(op->ptr, "name", imagename);
7181
7182 /* Set default color. Copy the color from nodes, so it matches the existing material.
7183 * Material could be null so we should have a default color. */
7184 float color[4];
7186 RNA_float_set_array(op->ptr, "color", color);
7187
7189 C, op, 300, IFACE_("Add Paint Slot"), CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Add"));
7190}
7191
7193{
7194 ui::Layout &layout = *op->layout;
7195 layout.use_property_split_set(true);
7196 layout.use_property_decorate_set(false);
7199
7200 if (ob->mode == OB_MODE_SCULPT) {
7201 slot_type = ePaintCanvasSource(RNA_enum_get(op->ptr, "slot_type"));
7202 layout.prop(op->ptr, "slot_type", ui::ITEM_R_EXPAND, std::nullopt, ICON_NONE);
7203 }
7204
7205 layout.prop(op->ptr, "name", UI_ITEM_NONE, std::nullopt, ICON_NONE);
7206
7207 switch (slot_type) {
7209 /* TODO: Deduplicate with image_new_draw */
7210 ui::Layout &dim_col = layout.column(true);
7211 dim_col.prop(op->ptr, "width", UI_ITEM_NONE, std::nullopt, ICON_NONE);
7212 dim_col.prop(op->ptr, "height", UI_ITEM_NONE, std::nullopt, ICON_NONE);
7213
7214 ui::Layout &col = layout.column(false);
7215 col.prop(op->ptr, "generated_type", UI_ITEM_NONE, std::nullopt, ICON_NONE);
7216
7217 const int gen_type = RNA_enum_get(op->ptr, "generated_type");
7218 ui::Layout &color_col = col.column(false);
7219 if (gen_type == IMA_GENTYPE_BLANK) {
7220 color_col.prop(op->ptr, "color", UI_ITEM_NONE, std::nullopt, ICON_NONE);
7221 }
7222 col.prop(op->ptr, "alpha", UI_ITEM_NONE, std::nullopt, ICON_NONE);
7223 col.prop(op->ptr, "float", UI_ITEM_NONE, std::nullopt, ICON_NONE);
7224 col.prop(op->ptr, "tiled", UI_ITEM_NONE, std::nullopt, ICON_NONE);
7225 break;
7226 }
7228 layout.prop(op->ptr, "domain", ui::ITEM_R_EXPAND, std::nullopt, ICON_NONE);
7229 layout.prop(op->ptr, "data_type", ui::ITEM_R_EXPAND, std::nullopt, ICON_NONE);
7230 layout.prop(op->ptr, "color", UI_ITEM_NONE, std::nullopt, ICON_NONE);
7231 break;
7234 break;
7235 }
7236}
7237
7238#define IMA_DEF_NAME N_("Untitled")
7239
7241{
7242 PropertyRNA *prop;
7243 static float default_color[4] = {0.0f, 0.0f, 0.0f, 1.0f};
7244
7245 static const EnumPropertyItem slot_type_items[3] = {
7246 {PAINT_CANVAS_SOURCE_IMAGE, "IMAGE", 0, "Image", ""},
7247 {PAINT_CANVAS_SOURCE_COLOR_ATTRIBUTE, "COLOR_ATTRIBUTE", 0, "Color Attribute", ""},
7248 {0, nullptr, 0, nullptr, nullptr},
7249 };
7250
7251 /* identifiers */
7252 ot->name = "Add Paint Slot";
7253 ot->description = "Add a paint slot";
7254 ot->idname = "PAINT_OT_add_texture_paint_slot";
7255
7256 /* API callbacks. */
7261
7262 /* flags */
7263 ot->flag = OPTYPE_UNDO;
7264
7265 /* Shared Properties */
7266 prop = RNA_def_enum(ot->srna,
7267 "type",
7269 0,
7270 "Material Layer Type",
7271 "Material layer type of new paint slot");
7273
7274 prop = RNA_def_enum(
7275 ot->srna, "slot_type", slot_type_items, 0, "Slot Type", "Type of new paint slot");
7276
7277 prop = RNA_def_string(
7278 ot->srna, "name", IMA_DEF_NAME, MAX_NAME, "Name", "Name for new paint slot source");
7280
7281 prop = RNA_def_float_color(
7282 ot->srna, "color", 4, nullptr, 0.0f, FLT_MAX, "Color", "Default fill color", 0.0f, 1.0f);
7284 RNA_def_property_float_array_default(prop, default_color);
7285
7286 /* Image Properties */
7287 prop = RNA_def_int(ot->srna, "width", 1024, 1, INT_MAX, "Width", "Image width", 1, 16384);
7289
7290 prop = RNA_def_int(ot->srna, "height", 1024, 1, INT_MAX, "Height", "Image height", 1, 16384);
7292
7293 RNA_def_boolean(ot->srna, "alpha", true, "Alpha", "Create an image with an alpha channel");
7294
7295 RNA_def_enum(ot->srna,
7296 "generated_type",
7299 "Generated Type",
7300 "Fill the image with a grid for UV map testing");
7301
7303 "float",
7304 false,
7305 "32-bit Float",
7306 "Create image with 32-bit floating-point bit depth");
7307
7308 RNA_def_boolean(ot->srna, "tiled", false, "Tiled", "Create a tiled image");
7309
7310 /* Color Attribute Properties */
7311 RNA_def_enum(ot->srna,
7312 "domain",
7315 "Domain",
7316 "Type of element that attribute is stored on");
7317
7318 RNA_def_enum(ot->srna,
7319 "data_type",
7322 "Data Type",
7323 "Type of data stored in attribute");
7324}
7325
7327
7328/* -------------------------------------------------------------------- */
7331
7333{
7334 /* no checks here, poll function does them for us */
7335 Main *bmain = CTX_data_main(C);
7337 Scene *scene = CTX_data_scene(C);
7338
7339 ED_uvedit_add_simple_uvs(bmain, scene, ob);
7340
7341 ED_paint_proj_mesh_data_check(*scene, *ob, nullptr, nullptr, nullptr, nullptr);
7342
7343 DEG_id_tag_update(ob->data, 0);
7346 return OPERATOR_FINISHED;
7347}
7348
7350{
7352
7353 if (!ob || ob->type != OB_MESH || ob->mode != OB_MODE_TEXTURE_PAINT) {
7354 return false;
7355 }
7356 return true;
7357}
7358
7360{
7361 /* identifiers */
7362 ot->name = "Add Simple UVs";
7363 ot->description = "Add cube map UVs on mesh";
7364 ot->idname = "PAINT_OT_add_simple_uvs";
7365
7366 /* API callbacks. */
7369
7370 /* flags */
7372}
7373
7375
7376} // namespace blender
Generic geometry attributes built on CustomData.
Camera data-block and utility functions.
CustomData interface, see also DNA_customdata_types.h.
#define ORIGINDEX_NONE
#define IDP_array_float_get(prop)
#define IMA_SIGNAL_USER_NEW_IMAGE
Definition BKE_image.hh:183
General operations, lookup, etc. for materials.
#define BKE_MESH_TESSTRI_VINDEX_ORDER(_tri, _v)
#define SH_NODE_TEX_IMAGE
#define SH_NODE_BSDF_PRINCIPLED
#define SH_NODE_BUMP
#define SH_NODE_NORMAL_MAP
#define SH_NODE_ATTRIBUTE
General operations, lookup, etc. for blender objects.
#define LISTBASE_CIRCULAR_BACKWARD_END(type, lb, lb_iter, lb_init)
#define LISTBASE_CIRCULAR_FORWARD_BEGIN(type, lb, lb_iter, lb_init)
#define LISTBASE_CIRCULAR_BACKWARD_BEGIN(type, lb, lb_iter, lb_init)
#define LISTBASE_CIRCULAR_FORWARD_END(type, lb, lb_iter, lb_init)
#define M_1_PI
#define M_PI_2
#define M_PI
#define FILE_MAX
#define STRNCPY_UTF8(dst, src)
#define BLENDER_MAX_THREADS
Compatibility-like things for windows.
#define RPT_(msgid)
#define CTX_IFACE_(context, msgid)
#define BLT_I18NCONTEXT_OPERATOR_DEFAULT
#define IFACE_(msgid)
#define DATA_(msgid)
#define MAX_ID_NAME
Definition DNA_ID.h:367
#define ID_IS_EDITABLE(_id)
Definition DNA_ID.h:708
#define ID_IS_OVERRIDE_LIBRARY(_id)
Definition DNA_ID.h:736
#define MAX_NAME
Definition DNA_defs.h:63
Object is a sort of wrapper for general info.
#define IMAGEPAINT_MODE_IMAGE
#define IMAGEPAINT_MODE_MATERIAL
#define RV3D_CLIPPING_ENABLED(v3d, rv3d)
#define ED_IMAGE_UNDO_TILE_NUMBER(size)
Definition ED_paint.hh:117
#define ED_IMAGE_UNDO_TILE_SIZE
Definition ED_paint.hh:116
#define ED_IMAGE_UNDO_TILE_BITS
Definition ED_paint.hh:115
Read Blender memory allocation and freeing.
#define C
Definition RandGen.cpp:29
#define UI_ITEM_NONE
#define NC_GEOM
Definition WM_types.hh:406
#define ND_DATA
Definition WM_types.hh:552
#define NC_SCENE
Definition WM_types.hh:391
#define NA_ADDED
Definition WM_types.hh:636
#define ND_TOOLSETTINGS
Definition WM_types.hh:468
#define NA_EDITED
Definition WM_types.hh:634
#define NC_IMAGE
Definition WM_types.hh:397
Provides wrapper around system-specific atomic primitives, and some extensions (faked-atomic operatio...
ATOMIC_INLINE int32_t atomic_fetch_and_add_int32(int32_t *p, int32_t x)
#define CLAMP(a, b, c)
#define INIT_MINMAX2(min, max)
#define UNPACK4(a)
#define BLI_assert_unreachable()
#define UNUSED_VARS(...)
#define POINTER_FROM_INT(i)
#define BLI_INLINE
#define POINTER_AS_INT(i)
#define UNPACK3(a)
#define UNLIKELY(x)
#define ELEM(...)
#define MEM_SIZE_OPTIMAL(size)
btScalar max
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
Definition btQuadWord.h:117
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
SIMD_FORCE_INLINE btScalar angle(const btVector3 &v) const
Return the angle between this and another vector.
Definition btVector3.h:356
void append(const array< T > &from)
static AttributeOwner from_id(ID *id)
constexpr int64_t size() const
constexpr Span()=default
constexpr const T & first() const
Definition BLI_span.hh:319
constexpr bool is_empty() const
Definition BLI_span.hh:264
StringRefNull ref() const
UString()=default
GAttributeReader lookup_or_default(StringRef name, AttrDomain domain, AttrType data_type, const void *default_value=nullptr) const
GAttributeReader lookup(const StringRef name) const
bool add(const StringRef name, const AttrDomain domain, const AttrType data_type, const AttributeInit &initializer)
#define fmodf(x, y)
#define acosf(x)
#define FLT_MAX
#define INT_MAX
#define FLT_EPSILON
#define layer
float distance(float, float) RET
const int tile_index
const int state
const ccl_global KernelWorkTile * tile
ccl_device_inline float interp(const float a, const float b, const float t)
Definition math_base.h:549
#define unit_float_to_uchar_clamp_v3(v1, v2)
std::unique_ptr< IDProperty, IDPropertyDeleter > create(StringRef prop_name, int32_t value, eIDPropertyFlag flags={})
Allocate a new IDProperty of type IDP_INT, set its name and value.
void stroke_track_location(Paint &paint, float3 location)
void node_position_propagate(bNode &node)
Definition node.cc:4942
void node_tree_free_tree(bNodeTree &ntree)
Definition node.cc:5224
std::optional< AttrType > custom_data_type_to_attr_type(eCustomDataType data_type)
void node_position_relative(bNode &from_node, const bNode &to_node, const bNodeSocket *from_sock, const bNodeSocket &to_sock)
Definition node.cc:4900
bNode * node_add_static_node(const bContext *C, bNodeTree &ntree, int type)
Definition node.cc:4283
bNodeLink & node_add_link(bNodeTree &ntree, bNode &fromnode, bNodeSocket &fromsock, bNode &tonode, bNodeSocket &tosock)
Definition node.cc:4638
bNodeTree * node_tree_add_tree(Main *bmain, StringRef name, StringRef idname)
Definition node.cc:4992
bNodeSocket * node_find_socket(bNode &node, eNodeSocketInOut in_out, UString identifier)
Definition node.cc:3036
void node_set_active(bNodeTree &ntree, bNode &node)
Definition node.cc:5484
internal::ShallowDataConstRef< T > shallow_copy(const T &other)
Definition DNA_defs.h:147
Object * context_active_object(const bContext *C)
bool object_active_color_init(Object &ob, const float fill_color[4])
uchar4 interpolate_nearest_wrap_byte(const ImBuf *in, float u, float v)
Definition IMB_interp.hh:44
uchar4 interpolate_bilinear_wrap_byte(const ImBuf *in, float u, float v)
Definition IMB_interp.hh:97
uchar4 interpolate_cubic_bspline_byte(const ImBuf *in, float u, float v)
float4 interpolate_nearest_wrap_fl(const ImBuf *in, float u, float v)
Definition IMB_interp.hh:48
float4 interpolate_cubic_bspline_fl(const ImBuf *in, float u, float v)
float4 interpolate_bilinear_wrap_fl(const ImBuf *in, float u, float v)
T clamp(const T &a, const T &min, const T &max)
void node_tree_shader_default(const bContext *C, Main *bmain, ID *id)
BMesh Iterators.
@ V3D_OVERLAY_HIDE_OBJECT_ORIGINS
@ V3D_OVERLAY_HIDE_MOTION_PATHS
@ V3D_OVERLAY_HIDE_OBJECT_XTRAS
@ V3D_OVERLAY_HIDE_CURSOR
MINLINE float min_ff(float a, float b)
void WM_main_add_notifier(uint type, void *reference)
static void do_projectpaint_smear_f(ProjPaintState *ps, ProjPixel *projPixel, float mask, MemArena *smearArena, LinkNode **smearPixels_f, const float co[2])
void * MEM_new_uninitialized(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
UserDef U
Definition blender.cc:58
bool BLI_rcti_is_valid(const struct rcti *rect)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
ARegion * BKE_area_find_region_active_win(const ScrArea *area)
Definition screen.cc:1037
static bool proj_paint_state_mesh_eval_init(const bContext *C, ProjPaintState *ps)
void RNA_float_set_array(PointerRNA *ptr, const char *name, const float *values)
void RNA_def_enum_funcs(PropertyRNA *prop, EnumPropertyItemFunc itemfunc)
static bool pixel_bounds_array(float(*uv)[2], const int ibuf_x, const int ibuf_y, int tot, rcti *r_bounds_px)
void mul_m3_v3(const float M[3][3], float r[3])
size_t size_t size_t BLI_snprintf_utf8(char *__restrict dst, size_t dst_maxncpy, const char *__restrict format,...) ATTR_NONNULL(1
VecBase< uint8_t, 4 > uchar4
@ BRUSH_GRADIENT_RADIAL
@ BRUSH_GRADIENT_LINEAR
MINLINE int mod_i(int i, int n)
ImBuf * ED_view3d_draw_offscreen_imbuf(Depsgraph *depsgraph, Scene *scene, eDrawType drawtype, View3D *v3d, ARegion *region, int sizex, int sizey, ImBufFlags imbuf_flag, int alpha_mode, const char *viewname, bool restore_rv3d_mats, GPUOffScreen *ofs, GPUViewport *viewport, bool use_camera_view_bounds, char err_out[256])
ScrArea * CTX_wm_area(const bContext *C)
__int64 int64_t
Definition BKE_text.h:90
static bool line_rect_clip(const rctf *rect, const float l1[4], const float l2[4], const float uv1[2], const float uv2[2], float uv[2], bool is_ortho)
static void partial_redraw_single_init(ImagePaintPartialRedraw *pr)
static int project_bucket_offset_safe(const ProjPaintState *ps, const float projCoSS[2])
const MTex * BKE_brush_color_texture_get(const Brush *brush, PaintMode paint_mode)
Definition brush.cc:977
MINLINE void rgba_uchar_to_float(float r_col[4], const unsigned char col_ub[4])
int RNA_enum_get(PointerRNA *ptr, const char *name)
void IMB_free_byte_pixels(ImBuf *ibuf)
static int float_z_sort_flip(const void *p1, const void *p2)
Object * BKE_view_layer_active_object_get(const ViewLayer *view_layer)
MINLINE unsigned char unit_float_to_uchar_clamp(float val)
@ IMAGEPAINT_MISSING_STENCIL
@ IMAGEPAINT_MISSING_MATERIAL
PropertyRNA * RNA_def_string(StructOrFunctionRNA *cont_, const char *identifier, const char *default_value, const int maxlen, const char *ui_name, const char *ui_description)
static int float_z_sort(const void *p1, const void *p2)
static bool IsectPoly2Df(const float pt[2], const float uv[][2], const int tot)
void ED_view3d_clipping_local(RegionView3D *rv3d, const float mat[4][4])
@ RPT_WARNING
Definition BKE_report.hh:40
static void project_paint_build_proj_ima(ProjPaintState *ps, MemArena *arena, ListBaseT< PrepareImageEntry > *used_images)
void BKE_id_attributes_default_color_set(struct ID *id, std::optional< StringRef > name)
MemArena * BLI_memarena_new(size_t bufsize, const char *name) ATTR_WARN_UNUSED_RESULT ATTR_RETURNS_NONNULL ATTR_NONNULL(2) ATTR_MALLOC
@ IMAGEPAINT_PROJECT_LAYER_STENCIL_INV
@ IMAGEPAINT_PROJECT_LAYER_STENCIL
@ IMAGEPAINT_PROJECT_FLAT
@ IMAGEPAINT_PROJECT_XRAY
@ IMAGEPAINT_PROJECT_LAYER_CLONE
@ IMAGEPAINT_PROJECT_BACKFACE
IDProperty * IDP_GetPropertyTypeFromGroup(const IDProperty *prop, StringRef name, char type) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition idprop.cc:839
bool BLI_rctf_isect_pt_v(const struct rctf *rect, const float xy[2])
static void proj_paint_state_viewport_init(ProjPaintState *ps, const char symmetry_flag)
static bool add_simple_uvs_poll(bContext *C)
int BKE_scene_num_threads(const Scene *scene)
Definition scene.cc:3163
static void proj_paint_layer_clone_init(ProjPaintState *ps, ProjPaintLayerClone *layer_clone)
MINLINE void copy_v3_v3(float r[3], const float a[3])
unsigned int uint
Definition BKE_text.h:55
pthread_spinlock_t SpinLock
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
MINLINE void blend_color_mix_float(float dst[4], const float src1[4], const float src2[4])
MINLINE void add_v2_v2v2(float r[2], const float a[2], const float b[2])
static wmOperatorStatus texture_paint_camera_project_exec(bContext *C, wmOperator *op)
float BKE_brush_sample_tex_3d(const Paint *paint, const Brush *br, const MTex *mtex, const float3 &point, float4 &rgba, int thread, ImagePool *pool)
Definition brush.cc:985
void copy_m3_m4(float m1[3][3], const float m2[4][4])
void ED_uvedit_add_simple_uvs(Main *bmain, const Scene *scene, Object *ob)
void ED_space_image_sync(Main *bmain, Image *image, bool ignore_render_viewer)
Definition image_edit.cc:79
static void project_face_seams_init(const ProjPaintState *ps, MemArena *arena, const int tri_index, const uint vert_index, bool init_all, const int ibuf_x, const int ibuf_y)
static void do_projectpaint_clone_f(ProjPaintState *ps, ProjPixel *projPixel, float mask)
static bool project_bucket_iter_init(ProjPaintState *ps, const float mval_f[2])
bool IMB_colormanagement_space_is_srgb(const ColorSpace *colorspace)
void BKE_image_signal(Main *bmain, Image *ima, ImageUser *iuser, int signal)
void PAINT_OT_project_image(wmOperatorType *ot)
static int project_paint_prepare_image_index(ProjPaintState *ps, ListBaseT< PrepareImageEntry > &used_images, Image *tpage, const int tile, PrepareImageState &state)
MINLINE void mul_v2_fl(float r[2], float f)
PropertyRNA * RNA_def_float_color(StructOrFunctionRNA *cont_, const char *identifier, const int len, const float *default_value, const float hardmin, const float hardmax, const char *ui_name, const char *ui_description, const float softmin, const float softmax)
static void screen_px_from_persp(const float uv[2], const float v1co[4], const float v2co[4], const float v3co[4], const float uv1co[2], const float uv2co[2], const float uv3co[2], float pixelScreenCo[4], float w[3])
void ED_image_undo_push_end()
float BKE_brush_sample_masktex(const Paint *paint, Brush *br, const float2 &point, int thread, ImagePool *pool)
Definition brush.cc:1101
void interp_v2_v2v2v2(float r[2], const float a[2], const float b[2], const float c[2], const float t[3])
void mul_v3_m4v3(float r[3], const float mat[4][4], const float vec[3])
MINLINE void copy_v2_v2(float r[2], const float a[2])
Material * BKE_object_material_get(Object *ob, short act)
void ED_image_undo_push_begin(const char *name, PaintMode paint_mode)
@ TASK_PRIORITY_HIGH
Definition BLI_task_c.hh:61
static bool project_paint_flt_max_cull(const ProjPaintState *ps, const ProjPaintFaceCoSS *coSS)
static int project_paint_occlude_ptv(const float pt[3], const float v1[4], const float v2[4], const float v3[4], float w[3], const bool is_ortho)
static void proj_paint_face_coSS_init(const ProjPaintState *ps, const int3 &corner_tri, ProjPaintFaceCoSS *coSS)
bool IDP_AddToGroup(IDProperty *group, IDProperty *prop) ATTR_NONNULL()
Definition idprop.cc:788
TaskPool * BLI_task_pool_create_suspended(void *userdata, eTaskPriority priority)
Definition task_pool.cc:423
void RNA_string_set(PointerRNA *ptr, const char *name, const char *value)
MINLINE int count_bits_i(unsigned int n)
void mul_project_m4_v3(const float mat[4][4], float vec[3])
static void uv_image_outset(const ProjPaintState *ps, float(*orig_uv)[2], float(*puv)[2], uint tri_index, const int ibuf_x, const int ibuf_y)
static void proj_paint_face_lookup_init(const ProjPaintState *ps, ProjPaintFaceLookup *face_lookup)
static int project_paint_undo_subtiles(const TileInfo *tinf, int tx, int ty)
void PAINT_OT_add_texture_paint_slot(wmOperatorType *ot)
IDProperty * IDP_GetPropertyFromGroup(const IDProperty *prop, StringRef name) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition idprop.cc:818
static wmOperatorStatus texture_paint_add_texture_paint_slot_invoke(bContext *C, wmOperator *op, const wmEvent *)
static std::optional< std::string > proj_paint_color_attribute_create(wmOperator *op, Object &ob)
void IMB_mark_dirty(ImBuf *ibuf)
MINLINE void mul_v4_v4fl(float r[4], const float a[4], float f)
bool invert_m4_m4(float inverse[4][4], const float mat[4][4])
@ IMB_BLEND_ADD_ALPHA
Definition IMB_imbuf.hh:211
@ IMB_BLEND_ERASE_ALPHA
Definition IMB_imbuf.hh:210
MINLINE float safe_acosf(float a)
MINLINE void blend_color_mix_byte(unsigned char dst[4], const unsigned char src1[4], const unsigned char src2[4])
unsigned short ushort
Definition BKE_text.h:56
PropertyRNA * RNA_def_enum(StructOrFunctionRNA *cont_, const char *identifier, const EnumPropertyItem *items, const int default_value, const char *ui_name, const char *ui_description)
static wmOperatorStatus add_simple_uvs_exec(bContext *C, wmOperator *)
void BLI_linklist_prepend_arena(LinkNode **listp, void *ptr, struct MemArena *ma) ATTR_NONNULL(1
void BKE_camera_params_compute_viewplane(CameraParams *params, int winx, int winy, float aspx, float aspy)
void BLI_rcti_init_minmax(struct rcti *rect)
Definition rct.cc:488
void barycentric_weights_v2_persp(const float v1[4], const float v2[4], const float v3[4], const float co[2], float w[3])
int isect_seg_seg_v2_point(const float v0[2], const float v1[2], const float v2[2], const float v3[2], float r_vi[2])
static void paint_proj_stroke_ps(const bContext *, void *ps_handle_p, const float prev_pos[2], const float pos[2], const bool eraser, float pressure, float distance, float size, ProjPaintState *ps)
static wmOperatorStatus texture_paint_add_texture_paint_slot_exec(bContext *C, wmOperator *op)
int RNA_int_get(PointerRNA *ptr, const char *name)
MINLINE void sub_v2_v2v2(float r[2], const float a[2], const float b[2])
static int line_isect_y(const float p1[2], const float p2[2], const float y_level, float *x_isect)
void mul_m4_m4m4(float R[4][4], const float A[4][4], const float B[4][4])
void view3d_operator_needs_gpu(const bContext *C)
void imapaint_image_update(ImBuf *ibuf)
static Image * proj_paint_image_create(wmOperator *op, Main *bmain, bool is_data)
MINLINE void straight_to_premul_v4_v4(float premul[4], const float straight[4])
void BKE_object_material_assign(Main *bmain, Object *ob, Material *ma, short act, int assign_type)
int isect_seg_seg_v2(const float v1[2], const float v2[2], const float v3[2], const float v4[2])
bool BKE_colorband_evaluate(const ColorBand *coba, float in, float out[4])
Definition colorband.cc:399
ATTR_WARN_UNUSED_RESULT const BMVert * v
static void get_default_texture_layer_name_for_object(Object *ob, int texture_type, char *dst, int dst_maxncpy)
void * paint_proj_new_stroke(bContext *C, Object *ob, const float mouse[2], const BrushStrokeMode mode, const BrushSwitchMode brush_switch_mode)
std::string BKE_attribute_calc_unique_name(const AttributeOwner &owner, StringRef name)
static void proj_paint_state_cavity_init(ProjPaintState *ps)
MatBase< float, 4, 4 > float4x4
int isect_point_tri_v2(const float pt[2], const float v1[2], const float v2[2], const float v3[2])
static wmOperatorStatus texture_paint_image_from_view_exec(bContext *C, wmOperator *op)
int GPU_max_texture_size()
MINLINE void rgb_float_to_uchar(unsigned char r_col[3], const float col_f[3])
void ED_region_tag_redraw(ARegion *region)
Definition area.cc:681
int RNA_enum_from_value(const EnumPropertyItem *item, const int value)
MINLINE float cross_tri_v2(const float v1[2], const float v2[2], const float v3[2])
void MEM_delete_void(void *vmemh)
constexpr std::enable_if_t< blenlib_internal::IsBoundedArray< T >::value, size_t > BOUNDED_ARRAY_TYPE_SIZE() noexcept
static void do_projectpaint_soften_f(ProjPaintState *ps, ProjPixel *projPixel, float mask, MemArena *softenArena, LinkNode **softenPixels)
static void project_paint_bucket_bounds(const ProjPaintState *ps, const float min[2], const float max[2], int bucketMin[2], int bucketMax[2])
PaintTileMap * ED_image_paint_tile_map_get()
BLI_INLINE uchar f_to_char(const float val)
ViewLayer * CTX_data_view_layer(const bContext *C)
static TexPaintSlot * project_paint_face_clone_slot(const ProjPaintState *ps, int tri_index)
static Image * project_paint_face_paint_image(const ProjPaintState *ps, int tri_index)
void RNA_def_property_float_array_default(PropertyRNA *prop, const float *array)
MINLINE void mul_v3_fl(float r[3], float f)
static bool project_paint_op(void *state, const float lastpos[2], const float pos[2])
static bool partial_redraw_array_merge(ImagePaintPartialRedraw *pr, ImagePaintPartialRedraw *pr_other, int tot)
void normalize_m4(float R[4][4]) ATTR_NONNULL()
static void texture_paint_add_texture_paint_slot_ui(bContext *C, wmOperator *op)
static bool line_clip_rect2f(const rctf *cliprect, const rctf *rect, const float l1[2], const float l2[2], float l1_clip[2], float l2_clip[2])
void BLI_task_pool_work_and_wait(TaskPool *pool)
Definition task_pool.cc:455
static void do_projectpaint_thread(TaskPool *__restrict, void *ph_v)
void paint_proj_redraw(const bContext *C, void *ps_handle_p, bool final)
Global G
Definition blender.cc:57
MINLINE void sub_v3_v3(float r[3], const float a[3])
VecBase< float, 4 > float4
MINLINE void zero_v4(float r[4])
void BKE_camera_params_compute_matrix(CameraParams *params)
void mul_m4_v3(const float M[4][4], float r[3])
void IMB_float_from_byte(ImBuf *ibuf)
const EnumPropertyItem * RNA_image_itemf(bContext *C, PointerRNA *ptr, PropertyRNA *prop, bool *r_free)
void * BLI_findlink(const ListBase *listbase, int number) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:541
static int project_paint_PickFace(const ProjPaintState *ps, const float pt[2], float w[3])
static float VecZDepthOrtho(const float pt[2], const float v1[3], const float v2[3], const float v3[3], float w[3])
void BLI_spin_end(SpinLock *spin)
Definition threads.cc:448
MINLINE void blend_color_interpolate_float(float dst[4], const float src1[4], const float src2[4], float t)
static void do_projectpaint_clone(ProjPaintState *ps, ProjPixel *projPixel, float mask)
void IMB_colormanagement_colorspace_to_scene_linear_v3(float pixel[3], const ColorSpace *colorspace)
bool ED_view3d_autodist(ARegion *region, View3D *v3d, const int mval[2], float mouse_worldloc[3], const float fallback_depth_pt[3])
MINLINE void blend_color_add_float(float dst[4], const float src1[4], const float src2[4])
void CTX_wm_operator_poll_msg_set(bContext *C, const char *msg)
static void image_paint_partial_redraw_expand(ImagePaintPartialRedraw *cell, const ProjPixel *projPixel)
static void partial_redraw_array_init(ImagePaintPartialRedraw *pr)
void IMB_freeImBuf(ImBuf *ibuf)
VecBase< int32_t, 2 > int2
VecBase< float, 2 > float2
VecBase< int32_t, 3 > int3
MINLINE void mul_v3_v3(float r[3], const float a[3])
MINLINE void add_v2_v2(float r[2], const float a[2])
void paint_brush_color_get(const Paint *paint, Brush *br, std::optional< float3 > &initial_hsv_jitter, bool invert, float distance, float pressure, float r_color[3])
static void do_projectpaint_mask(ProjPaintState *ps, ProjPixel *projPixel, float mask)
unsigned char uchar
Definition BKE_text.h:58
@ ME_EDIT_PAINT_FACE_SEL
static void project_face_pixel(const float *tri_uv[3], ImBuf *ibuf_other, const float w[3], uchar rgba_ub[4], float rgba_f[4])
MINLINE float area_tri_v2(const float v1[2], const float v2[2], const float v3[2])
void IMB_colormanagement_scene_linear_to_colorspace_v3(float pixel[3], const ColorSpace *colorspace)
void minmax_v2v2_v2(float min[2], float max[2], const float vec[2])
void BKE_texpaint_slot_refresh_cache(Scene *scene, Material *ma, const Object *ob)
static void project_bucket_bounds(const ProjPaintState *ps, const int bucket_x, const int bucket_y, rctf *r_bucket_bounds)
MINLINE void add_v4_v4(float r[4], const float a[4])
void DEG_id_tag_update(ID *id, unsigned int flags)
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:162
@ OB_MODE_TEXTURE_PAINT
BMesh const char void * data
Mesh * BKE_mesh_from_object(Object *ob)
T * DEG_get_evaluated(const Depsgraph *depsgraph, T *id)
static bool project_bucket_point_occluded(const ProjPaintState *ps, LinkNode *bucketFace, const int orig_face, const float pixelScreenCo[4])
static bool texture_paint_image_from_view_poll(bContext *C)
float line_point_factor_v2(const float p[2], const float l1[2], const float l2[2])
static void project_face_winding_init(const ProjPaintState *ps, const int tri_index)
Depsgraph * CTX_data_ensure_evaluated_depsgraph(const bContext *C, bool rna_write_check=false)
static void project_paint_delayed_face_init(ProjPaintState *ps, const int3 &corner_tri, const int tri_index)
MINLINE void premul_to_straight_v4(float color[4])
static int project_bucket_offset(const ProjPaintState *ps, const float projCoSS[2])
static void proj_paint_state_thread_init(ProjPaintState *ps, const bool reset_threads)
void BKE_camera_params_from_object(CameraParams *params, const struct Object *cam_ob)
void BKE_main_ensure_invariants(Main &bmain, std::optional< Span< ID * > > modified_ids=std::nullopt)
float3 BKE_paint_seed_hsv_jitter()
@ BKE_MAT_ASSIGN_USERPREF
static int project_paint_face_paint_tile(Image *ima, const float *uv)
IDProperty * IDP_GetProperties(ID *id) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition idprop.cc:1000
unsigned char uint8_t
Definition BKE_text.h:79
static bool IsectPoly2Df_twoside(const float pt[2], const float uv[][2], const int tot)
static bool project_bucket_isect_circle(const float cent[2], const float radius_squared, const rctf *bucket_bounds)
static bool proj_paint_add_slot(bContext *C, wmOperator *op)
void RNA_def_property_subtype(PropertyRNA *prop, PropertySubType subtype)
static Image * project_paint_face_clone_image(const ProjPaintState *ps, int tri_index)
@ IMAGE_PAINT_BRUSH_TYPE_SOFTEN
@ IMAGE_PAINT_BRUSH_TYPE_SMEAR
@ IMAGE_PAINT_BRUSH_TYPE_DRAW
@ IMAGE_PAINT_BRUSH_TYPE_FILL
@ IMAGE_PAINT_BRUSH_TYPE_MASK
@ IMAGE_PAINT_BRUSH_TYPE_CLONE
void paint_proj_stroke(const bContext *C, void *ps_handle_p, const float prev_pos[2], const float pos[2], const bool eraser, float pressure, float distance, float size)
bool BKE_view_layer_synced_ensure(const Main &bmain, const Scene *scene, ViewLayer *view_layer)
MINLINE void negate_v3(float r[3])
static void screen_px_to_vector_persp(int winx, int winy, const float projmat_inv[4][4], const float view_pos[3], const float co_px[2], float r_dir[3])
int BKE_brush_size_get(const Paint *paint, const Brush *brush)
Definition brush.cc:1356
bool ED_paint_proj_mesh_data_check(Scene &scene, Object &ob, bool *r_has_uvs, bool *r_has_mat, bool *r_has_tex, bool *r_has_stencil)
BlurKernel * paint_new_blur_kernel(Brush *br, bool proj)
void interp_v3_v3v3(float r[3], const float a[3], const float b[3], float t)
void IMB_blend_color_float(float dst[4], const float src1[4], const float src2[4], IMB_BlendMode mode)
Definition rectop.cc:118
void IMB_partial_update_mark_full(ImBuf *ibuf)
Image * BKE_image_add_from_imbuf(Main *bmain, ImBuf *ibuf, const char *name)
static TexPaintSlot * project_paint_face_paint_slot(const ProjPaintState *ps, int tri_index)
void PAINT_OT_image_from_view(wmOperatorType *ot)
RegionView3D * CTX_wm_region_view3d(const bContext *C)
wmOperatorStatus WM_operator_props_dialog_popup(bContext *C, wmOperator *op, int width, std::optional< std::string > title, std::optional< std::string > confirm_text, const bool cancel_default, std::optional< std::string > message, const bool show_icon)
int isect_point_quad_v2(const float p[2], const float v1[2], const float v2[2], const float v3[2], const float v4[2])
const void * CustomData_get_layer(const CustomData *data, eCustomDataType type)
static float VecZDepthPersp(const float pt[2], const float v1[4], const float v2[4], const float v3[4], float w[3])
static float screen_px_line_point_factor_v2_persp(const ProjPaintState *ps, const float p[2], const float v1[3], const float v2[3])
bool BLI_rcti_is_empty(const struct rcti *rect)
void paint_brush_exit_tex(Brush *brush)
bool RNA_boolean_get(PointerRNA *ptr, const char *name)
void BKE_brush_size_set(Paint *paint, Brush *brush, int size)
Definition brush.cc:1340
static void paint_proj_begin_clone(ProjPaintState *ps, const float mouse[2])
float resolve_quad_u_v2(const float st[2], const float st0[2], const float st1[2], const float st2[2], const float st3[2])
const EnumPropertyItem rna_enum_dummy_NULL_items[]
Definition rna_rna.cc:28
MINLINE void copy_v4_v4_uchar(unsigned char r[4], const unsigned char a[4])
void BKE_image_pool_free(ImagePool *pool)
MINLINE void zero_v3(float r[3])
bool paint_use_opacity_masking(const Paint *paint, const Brush *brush)
void mul_m4_v4(const float mat[4][4], float r[4])
MINLINE float dot_v2v2(const float a[2], const float b[2]) ATTR_WARN_UNUSED_RESULT
static bool project_bucket_face_isect(ProjPaintState *ps, int bucket_x, int bucket_y, const int3 &tri)
bool isect_seg_seg_v2_simple(const float v1[2], const float v2[2], const float v3[2], const float v4[2])
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
void BLI_insertlinkbefore(ListBase *listbase, void *vnextlink, void *vnewlink) ATTR_NONNULL(1)
Definition listbase.cc:378
MINLINE bool equals_v2v2(const float v1[2], const float v2[2]) ATTR_WARN_UNUSED_RESULT
static void project_paint_begin(const bContext *C, ProjPaintState *ps, const bool is_multi_view, const char symmetry_flag)
void BLI_task_pool_free(TaskPool *pool)
Definition task_pool.cc:441
static float len_squared_v2v2_alt(const float v1[2], const float v2_1, const float v2_2)
void project_plane_v3_v3v3(float out[3], const float p[3], const float v_plane[3])
bool ED_view3d_clip_range_get(const Depsgraph *depsgraph, const View3D *v3d, const RegionView3D *rv3d, bool use_ortho_factor, float *r_clip_start, float *r_clip_end)
MINLINE void float_to_byte_dither_v3(unsigned char b[3], const float f[3], float dither, int x, int y)
void BLI_rcti_init(struct rcti *rect, int xmin, int xmax, int ymin, int ymax)
Definition rct.cc:428
ImBuf * BKE_image_acquire_ibuf(Image *ima, ImageUser *iuser, void **r_lock)
float BKE_brush_curve_strength_clamped(const Brush *br, float p, float len)
Definition brush.cc:1751
MINLINE void straight_uchar_to_premul_float(float result[4], const unsigned char color[4])
void * BLI_memarena_alloc(MemArena *ma, size_t size) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1) ATTR_MALLOC ATTR_ALLOC_SIZE(2)
static void do_projectpaint_draw(ProjPaintState *ps, ProjPixel *projPixel, const float texrgb[3], float mask, float dither, int u, int v)
static Material * get_or_create_current_material(bContext *C, Object *ob)
void BLI_spin_unlock(SpinLock *spin)
Definition threads.cc:433
MINLINE float normalize_v3(float n[3])
BLI_INLINE float IMB_colormanagement_get_luminance(const float rgb[3])
MINLINE float len_squared_v2(const float v[2]) ATTR_WARN_UNUSED_RESULT
MINLINE void add_v3_v3(float r[3], const float a[3])
struct bke::bNodeTreeType * ntreeType_Shader
void RNA_def_property_flag(PropertyRNA *prop, PropertyFlag flag)
int pow_i(int base, int exp)
Definition math_base.cc:15
PropertyRNA * RNA_def_int(StructOrFunctionRNA *cont_, const char *identifier, const int default_value, const int hardmin, const int hardmax, const char *ui_name, const char *ui_description, const int softmin, const int softmax)
MINLINE void sub_v3_v3v3(float r[3], const float a[3], const float b[3])
void BLI_spin_init(SpinLock *spin)
Definition threads.cc:394
bool ED_view3d_clipping_test(const RegionView3D *rv3d, const float co[3], bool is_local)
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
Definition report.cc:191
static bool IsectPT2Df_limit(const float pt[2], const float v1[2], const float v2[2], const float v3[2], const float limit)
static void project_paint_prepare_all_faces(ProjPaintState *ps, MemArena *arena, const ProjPaintFaceLookup *face_lookup, ProjPaintLayerClone *layer_clone, const float2 *uv_map_base, const bool is_multi_view)
static void scale_tri(float insetCos[3][3], const float *origCos[3], const float inset)
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118
void _DNA_internal_memzero(void *dst, size_t size)
Definition dna_utils.cc:271
Main * CTX_data_main(const bContext *C)
static void do_projectpaint_smear(ProjPaintState *ps, ProjPixel *projPixel, float mask, MemArena *smearArena, LinkNode **smearPixels, const float co[2])
ATTR_WARN_UNUSED_RESULT const BMVert * v2
MINLINE void copy_v3_fl(float r[3], float f)
void IMB_byte_from_float(ImBuf *ibuf)
const ImBuf * ED_image_paint_tile_push(PaintTileMap *paint_tile_map, Image *image, ImBuf *ibuf, ImageUser *iuser, int x_tile, int y_tile, unsigned short **r_mask, bool **r_valid)
static bool cmp_uv(const float vec2a[2], const float vec2b[2])
static void default_paint_slot_color_get(int layer_type, Material *ma, float color[4])
static void project_paint_face_init(const ProjPaintState *ps, const int thread_index, const int bucket_index, const int tri_index, const int image_index, const rctf *clip_rect, const rctf *bucket_bounds, ImBuf *ibuf)
void interp_v2_v2v2(float r[2], const float a[2], const float b[2], float t)
Paint * BKE_paint_get_active_from_context(const bContext *C)
void ED_view3d_depth_override(Depsgraph *depsgraph, ARegion *region, View3D *v3d, Object *obact, eV3DDepthOverrideMode mode, bool use_overlay, ViewDepths **r_depths)
@ PAINT_CANVAS_SOURCE_IMAGE
@ PAINT_CANVAS_SOURCE_MATERIAL
@ PAINT_CANVAS_SOURCE_COLOR_ATTRIBUTE
void BKE_id_attributes_active_color_set(struct ID *id, std::optional< StringRef > name)
void paint_delete_blur_kernel(BlurKernel *kernel)
static float project_paint_uvpixel_mask(const ProjPaintState *ps, const int tri_index, const float w[3])
static void screen_px_from_ortho(const float uv[2], const float v1co[3], const float v2co[3], const float v3co[3], const float uv1co[2], const float uv2co[2], const float uv3co[2], float pixelScreenCo[4], float w[3])
@ ID_RECALC_SHADING
Definition DNA_ID.h:1094
@ ID_RECALC_SYNC_TO_EVAL
Definition DNA_ID.h:1118
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:1074
@ PROP_SKIP_SAVE
Definition RNA_types.hh:414
@ PROP_ENUM_NO_TRANSLATE
Definition RNA_types.hh:502
@ PROP_HIDDEN
Definition RNA_types.hh:408
static void project_state_init(bContext *C, Object *ob, ProjPaintState *ps, const BrushStrokeMode mode, const BrushSwitchMode brush_switch_mode)
bool BKE_image_has_ibuf(Image *ima, ImageUser *iuser)
const EnumPropertyItem rna_enum_color_attribute_domain_items[]
ScrArea * BKE_screen_find_big_area(const bScreen *screen, int spacetype, short min)
Definition screen.cc:1189
static VertSeam * find_adjacent_seam(const ProjPaintState *ps, uint loop_index, uint vert_index, VertSeam **r_seam)
MINLINE float square_f(float a)
@ V3D_DEPTH_NO_GPENCIL
Definition ED_view3d.hh:209
float4x4 ED_view3d_ob_project_mat_get_from_obmat(const RegionView3D *rv3d, const float4x4 &obmat)
IDProperty * IDP_EnsureProperties(ID *id) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition idprop.cc:1005
std::optional< BrushColorJitterSettings > BKE_brush_color_jitter_get_settings(const Paint *paint, const Brush *brush)
Definition brush.cc:1262
MINLINE float line_point_side_v2(const float l1[2], const float l2[2], const float pt[2]) ATTR_WARN_UNUSED_RESULT
Brush * BKE_paint_brush(Paint *paint)
wmOperatorStatus WM_enum_search_invoke(bContext *C, wmOperator *op, const wmEvent *)
MINLINE int square_s(short a)
static void proj_paint_state_seam_bleed_init(ProjPaintState *ps)
void DEG_relations_tag_update(Main *bmain)
static int line_isect_x(const float p1[2], const float p2[2], const float x_level, float *y_isect)
void ED_paint_data_warning(ReportList *reports, bool has_uvs, bool has_mat, bool has_tex, bool has_stencil)
Scene * CTX_data_scene(const bContext *C)
bScreen * CTX_wm_screen(const bContext *C)
PropertyRNA * RNA_def_string_file_name(StructOrFunctionRNA *cont_, const char *identifier, const char *default_value, const int maxlen, const char *ui_name, const char *ui_description)
void IMB_blend_color_byte(unsigned char dst[4], const unsigned char src1[4], const unsigned char src2[4], IMB_BlendMode mode)
Definition rectop.cc:30
void PAINT_OT_add_simple_uvs(wmOperatorType *ot)
MINLINE void copy_v4_v4(float r[4], const float a[4])
MINLINE void mul_v2_v2v2(float r[2], const float a[2], const float b[2])
void paint_proj_stroke_done(void *ps_handle_p, wmPaintCursor *cursor)
@ OPTYPE_REGISTER
Definition WM_types.hh:188
@ OPTYPE_UNDO
Definition WM_types.hh:190
static void project_paint_end(ProjPaintState *ps)
static void insert_seam_vert_array(const ProjPaintState *ps, MemArena *arena, const int tri_index, const int fidx1, const int ibuf_x, const int ibuf_y)
std::string RNA_string_get(PointerRNA *ptr, const char *name)
void copy_m4_m4(float m1[4][4], const float m2[4][4])
MINLINE float len_v2(const float v[2]) ATTR_WARN_UNUSED_RESULT
Material * BKE_material_add(Main *bmain, const char *name)
static int project_paint_pixel_sizeof(const short brush_type)
MINLINE void mul_v4_fl(float r[4], float f)
static void rect_to_uvspace_persp(const rctf *bucket_bounds, const float *v1coSS, const float *v2coSS, const float *v3coSS, const float *uv1co, const float *uv2co, const float *uv3co, float bucket_bounds_uv[4][2], const int flip)
VecBase< float, 3 > float3
BLI_INLINE void IMB_colormanagement_scene_linear_to_srgb_v3(float srgb[3], const float scene_linear[3])
static bool project_image_refresh_tagged(ProjPaintState *ps)
void interp_v3_v3v3v3(float p[3], const float v1[3], const float v2[3], const float v3[3], const float w[3])
float dist_squared_to_line_v2(const float p[2], const float l1[2], const float l2[2])
Definition math_geom.cc:294
void BKE_camera_params_init(CameraParams *params)
PropertyRNA * RNA_def_boolean(StructOrFunctionRNA *cont_, const char *identifier, const bool default_value, const char *ui_name, const char *ui_description)
@ PROP_COLOR_GAMMA
Definition RNA_types.hh:329
MINLINE void mul_v3_v3v3(float r[3], const float v1[3], const float v2[3])
const EnumPropertyItem rna_enum_image_generated_type_items[]
Definition rna_image.cc:26
void BLI_thread_unlock(int type)
Definition threads.cc:336
MINLINE float len_squared_v2v2(const float a[2], const float b[2]) ATTR_WARN_UNUSED_RESULT
bool ED_operator_object_active_editable_mesh(bContext *C)
Image * BKE_image_add_generated(Main *bmain, unsigned int width, unsigned int height, const char *name, int depth, int floatbuf, short gen_type, const float color[4], bool stereo3d, bool is_data, bool tiled)
static int project_paint_occlude_ptv_clip(const float pt[3], const float v1[4], const float v2[4], const float v3[4], const float v1_3d[3], const float v2_3d[3], const float v3_3d[3], float w[3], const bool is_ortho, RegionView3D *rv3d)
static int get_texture_layer_type(wmOperator *op, const char *prop_name)
void BKE_imageuser_default(ImageUser *iuser)
static bool project_paint_winclip(const ProjPaintState *ps, const ProjPaintFaceCoSS *coSS)
void barycentric_weights_v2(const float v1[2], const float v2[2], const float v3[2], const float co[2], float w[3])
static bool pixel_bounds_uv(const float uv_quad[4][2], const int ibuf_x, const int ibuf_y, rcti *r_bounds_px)
MINLINE void premul_float_to_straight_uchar(unsigned char *result, const float color[4])
static ProjPixel * project_paint_uvpixel_init(const ProjPaintState *ps, MemArena *arena, const TileInfo *tinf, int x_px, int y_px, const float mask, const int tri_index, const float pixelScreenCo[4], const float world_spaceCo[3], const float w[3])
void BLI_spin_lock(SpinLock *spin)
Definition threads.cc:408
static void proj_paint_state_vert_flags_init(ProjPaintState *ps)
static bool project_paint_PickColor(const ProjPaintState *ps, const float pt[2], float *rgba_fp, uchar *rgba, const bool interp)
Object * CTX_data_active_object(const bContext *C)
MINLINE float dot_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
static void project_bucket_clip_face(const bool is_ortho, const bool is_flip_object, const rctf *cliprect, const rctf *bucket_bounds, const float *v1coSS, const float *v2coSS, const float *v3coSS, const float *uv1co, const float *uv2co, const float *uv3co, float bucket_bounds_uv[8][2], int *tot, bool cull)
void BLI_task_pool_push(TaskPool *pool, TaskRunFunction run, void *taskdata, bool free_taskdata, TaskFreeFunction freedata)
Definition task_pool.cc:446
static void rect_to_uvspace_ortho(const rctf *bucket_bounds, const float *v1coSS, const float *v2coSS, const float *v3coSS, const float *uv1co, const float *uv2co, const float *uv3co, float bucket_bounds_uv[4][2], const int flip)
void set_imapaintpartial(ImagePaintPartialRedraw *ippr)
static const EnumPropertyItem layer_type_items[]
ARegion * CTX_wm_region(const bContext *C)
MINLINE void blend_color_interpolate_byte(unsigned char dst[4], const unsigned char src1[4], const unsigned char src2[4], float ft)
static void proj_paint_state_screen_coords_init(ProjPaintState *ps, const int diameter)
float BKE_curvemapping_evaluateF(const CurveMapping *cumap, int cur, float value)
static void copy_original_alpha_channel(ProjPaintState *ps, ProjPixel *pixel, bool is_floatbuf)
void IMB_free_float_pixels(ImBuf *ibuf)
static bool project_bucket_iter_next(ProjPaintState *ps, int *bucket_index, rctf *bucket_bounds, const float mval[2])
bool GPU_is_init()
void BLI_rcti_do_minmax_rcti(struct rcti *rect, const struct rcti *other)
const EnumPropertyItem rna_enum_color_attribute_type_items[]
View3D * CTX_wm_view3d(const bContext *C)
ATTR_WARN_UNUSED_RESULT const BMVert const BMEdge * e
static void do_projectpaint_draw_f(ProjPaintState *ps, ProjPixel *projPixel, const float texrgb[3], float mask)
float BKE_brush_alpha_get(const Paint *paint, const Brush *brush)
Definition brush.cc:1454
static void project_bucket_init(const ProjPaintState *ps, const int thread_index, const int bucket_index, const rctf *clip_rect, const rctf *bucket_bounds)
void BLI_thread_lock(int type)
Definition threads.cc:331
static Material * tex_get_material(const ProjPaintState *ps, int face_i)
void RNA_float_get_array(PointerRNA *ptr, const char *name, float *values)
MINLINE void rgba_float_to_uchar(unsigned char r_col[4], const float col_f[4])
ocio::ColorSpace ColorSpace
Definition BLF_api.hh:42
static void do_projectpaint_mask_f(ProjPaintState *ps, ProjPixel *projPixel, float mask)
void BLI_memarena_free(MemArena *ma) ATTR_NONNULL(1)
void paint_brush_init_tex(Brush *brush)
static void do_projectpaint_soften(ProjPaintState *ps, ProjPixel *projPixel, float mask, MemArena *softenArena, LinkNode **softenPixels)
void * MEM_new_array_zeroed(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
Mesh * BKE_object_get_evaluated_mesh(const Object *object_eval)
Definition object.cc:4436
void ED_area_tag_redraw(ScrArea *area)
Definition area.cc:784
static float compute_seam_normal(VertSeam *seam, VertSeam *adj, float r_no[2])
static bool project_paint_check_face_paintable(const ProjPaintState *ps, const ProjPaintFaceLookup *face_lookup, const int tri_i)
MINLINE void premul_to_straight_v4_v4(float straight[4], const float premul[4])
bool WM_paint_cursor_end(wmPaintCursor *handle)
static void project_paint_bleed_add_face_user(const ProjPaintState *ps, MemArena *arena, const int3 &corner_tri, const int tri_index)
void BKE_image_release_ibuf(Image *ima, ImBuf *ibuf, void *lock)
static bool check_seam(const ProjPaintState *ps, const int orig_face, const int orig_i1_fidx, const int orig_i2_fidx, int *other_face, int *orig_fidx)
MINLINE void rgba_float_args_set(float col[4], float r, float g, float b, float a)
static bool project_paint_clone_face_skip(ProjPaintState *ps, ProjPaintLayerClone *lc, const TexPaintSlot *slot, const int tri_index)
#define PS_CORNER_TRI_AS_UV_3(uvlayer, face_i, tri)
#define ISECT_TRUE_P1
#define PROJ_FACE_SEAM2
#define PROJ_SRC_VIEW_FILL
#define ISECT_ALL4
#define PROJ_VIEW_DATA_SIZE
#define PROJ_BUCKET_NULL
#define ISECT_TRUE
#define PROJ_FACE_SEAM1
#define ISECT_TRUE_P2
#define IMA_DEF_NAME
#define PROJ_BOUNDBOX_DIV
#define ISECT_4
#define PROJ_FACE_SEAM0
#define TILE_PENDING
#define PROJ_GEOM_TOLERANCE
#define PS_CORNER_TRI_ASSIGN_UV_3(uv_tri, uvlayer, face_i, tri)
#define PROJ_SRC_IMAGE_CAM
#define PROJ_FACE_NOSEAM0
#define PROJ_BUCKET_BRUSH_DIV
#define PS_CORNER_TRI_AS_VERT_INDEX_3(ps, tri)
#define PROJ_FACE_SEAM_INIT2
#define PROJ_BUCKET_RECT_MAX
#define PROJ_SRC_IMAGE_VIEW
#define PROJ_FACE_WINDING_INIT
#define PROJ_BUCKET_RECT_MIN
#define PROJ_VERT_CULL
#define PROJ_FACE_WINDING_CW
#define PROJ_PIXEL_TOLERANCE
#define ISECT_3
#define PROJ_VIEW_DATA_ID
#define PROJ_PAINT_STATE_SHARED_CLEAR(ps)
#define PROJ_PAINT_STATE_SHARED_MEMCPY(ps_dst, ps_src)
#define PROJ_BOUNDBOX_SQUARED
#define ISECT_1
#define PROJ_FACE_SEAM_INIT0
#define PROJ_BUCKET_INIT
#define ISECT_2
#define PROJ_FACE_DEGENERATE
#define PROJ_SRC_VIEW
#define ISECT_ALL3
#define PROJ_FACE_SCALE_SEAM
#define PROJ_FACE_SEAM_INIT1
#define fabsf
#define sqrtf
#define sinf
#define tanf
#define cosf
#define atan2f
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
#define min(a, b)
Definition sort.cc:38
eBrushImagePaintType image_brush_type
char name[258]
Definition DNA_ID.h:433
const ColorSpace * colorspace
ImBufByteBuffer byte_buffer
const uint8_t * byte_data() const
uint8_t * byte_data_for_write()
Definition allocimbuf.cc:60
bool colorspace_is_data() const
const float * float_data() const
std::string filepath
float * float_data_for_write()
Definition allocimbuf.cc:79
eImagePaint_MissingData missing_data
struct LinkNode * next
eMTex_BrushMapMode brush_map_mode
ListBaseT< Image > images
Definition BKE_main.hh:394
const c_style_mat & ptr() const
eMesh_EditFlag editflag
char * stencil_uv_map_attribute
char * clone_uv_map_attribute
CustomData face_data
eObject_TransFlag transflag
struct CurveMapping * cavity_curve
volatile const void ** undoRect
ImagePaintPartialRedraw * partRedrawRect
const ColorSpace * byte_colorspace
const TexPaintSlot * slot_last_clone
Span< float3 > vert_positions_eval
const float2 ** poly_to_loop_uv_clone
std::optional< bool > select_poly_single
const float2 ** poly_to_loop_uv
MemArena * arena_mt[BLENDER_MAX_THREADS]
std::optional< float2 > poly_to_loop_uv_single
const float2 * uv_map_stencil_eval
OffsetIndices< int > faces_eval
ListBaseT< VertSeam > * vertSeams
Span< float3 > corner_normals_eval
BrushSwitchMode brush_switch_mode
std::optional< bool > hide_poly_single
std::optional< int > material_index_single
ProjPaintState * ps_views[8]
std::optional< float3 > initial_hsv_jitter
struct Object * camera
View3DCursor cursor
struct ToolSettings * toolsettings
ListBaseT< SpaceLink > spacedata
ProjPaintImage * pjima
struct ImagePaintSettings imapaint
View3DOverlay overlay
eNodeSocketDatatype type
struct bNodeLink * link
void use_property_decorate_set(bool is_decorate)
void use_property_split_set(bool is_sep)
void prop(PointerRNA *ptr, PropertyRNA *prop, int index, int value, eUI_Item_Flag flag, std::optional< StringRef > name_opt, int icon, std::optional< StringRef > placeholder=std::nullopt)
Layout & column(bool align)
wmOperatorStatus(*) exec(bContext *C, wmOperator *op) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1125
const char * description
Definition WM_types.hh:1115
void(*) ui(bContext *C, wmOperator *op)
Definition WM_types.hh:1176
bool(*) poll(bContext *C) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1165
wmOperatorStatus(*) invoke(bContext *C, wmOperator *op, const wmEvent *event) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1141
struct ReportList * reports
struct wmOperatorType * type