1 /* SPDX-FileCopyrightText: 2006 by Nicholas Bishop. All rights reserved.
3 * SPDX-License-Identifier: GPL-2.0-or-later */
13 #include "BKE_attribute.hh"
14 #include "BKE_paint.hh"
15 #include "BKE_pbvh_api.hh"
17 #include "BLI_array.hh"
18 #include "BLI_bit_vector.hh"
19 #include "BLI_generic_array.hh"
20 #include "BLI_math_matrix_types.hh"
21 #include "BLI_math_vector_types.hh"
23 #include "BLI_span.hh"
24 #include "BLI_vector.hh"
26 #include "DNA_brush_enums.h"
28 #include "ED_view3d.hh"
30 namespace blender::ed::sculpt_paint
{
36 struct SimulationData
;
44 struct DistRayAABB_Precalc
;
49 struct SculptProjectVector
;
51 struct PaintModeSettings
;
52 struct WeightPaintInfo
;
57 struct wmOperatorType
;
59 /* -------------------------------------------------------------------- */
60 /** \name Sculpt Types
63 enum SculptUpdateType
{
64 SCULPT_UPDATE_COORDS
= 1 << 0,
65 SCULPT_UPDATE_MASK
= 1 << 1,
66 SCULPT_UPDATE_VISIBILITY
= 1 << 2,
67 SCULPT_UPDATE_COLOR
= 1 << 3,
68 SCULPT_UPDATE_IMAGE
= 1 << 4,
69 SCULPT_UPDATE_FACE_SET
= 1 << 5,
72 struct SculptCursorGeometryInfo
{
73 blender::float3 location
;
74 blender::float3 normal
;
75 blender::float3 active_vertex_co
;
78 #define SCULPT_VERTEX_NEIGHBOR_FIXED_CAPACITY 256
80 struct SculptVertexNeighborIter
{
82 PBVHVertRef
*neighbors
;
83 int *neighbor_indices
;
87 PBVHVertRef neighbors_fixed
[SCULPT_VERTEX_NEIGHBOR_FIXED_CAPACITY
];
88 int neighbor_indices_fixed
[SCULPT_VERTEX_NEIGHBOR_FIXED_CAPACITY
];
90 /* Internal iterator. */
100 /* Sculpt Original Data */
101 struct SculptOrigVertData
{
104 blender::ed::sculpt_paint::undo::Node
*unode
;
110 /* Original coordinate, normal, and mask. */
117 struct SculptOrigFaceData
{
118 blender::ed::sculpt_paint::undo::Node
*unode
;
120 const int *face_sets
;
124 enum eBoundaryAutomaskMode
{
125 AUTOMASK_INIT_BOUNDARY_EDGES
= 1,
126 AUTOMASK_INIT_BOUNDARY_FACE_SETS
= 2,
129 namespace blender::ed::sculpt_paint::undo
{
144 /* Storage of geometry for the undo node.
145 * Is used as a storage for either original or modified geometry. */
146 struct NodeGeometry
{
147 /* Is used for sanity check, helping with ensuring that two and only two
148 * geometry pushes happened in the undo stack. */
151 CustomData vert_data
;
152 CustomData edge_data
;
153 CustomData corner_data
;
154 CustomData face_data
;
155 int *face_offset_indices
;
156 const ImplicitSharingInfo
*face_offsets_sharing_info
;
166 char idname
[MAX_ID_NAME
]; /* Name instead of pointer. */
167 const void *node
; /* only during push, not valid afterwards! */
169 Array
<float3
> position
;
170 Array
<float3
> orig_position
;
171 Array
<float3
> normal
;
175 Array
<float4
> loop_col
;
176 Array
<float4
> orig_loop_col
;
180 /* to verify if totvert it still the same */
182 int mesh_corners_num
;
184 Array
<int> vert_indices
;
185 int unique_verts_num
;
187 Array
<int> corner_indices
;
189 BitVector
<> vert_hidden
;
190 BitVector
<> face_hidden
;
194 /** The number of grids in the entire mesh. */
196 /** A copy of #SubdivCCG::grid_size. */
198 /** Indices of grids in the PBVH node. */
200 BitGroupVector
<> grid_hidden
;
203 BMLogEntry
*bm_entry
;
207 char shapeName
[MAX_NAME
]; /* `sizeof(KeyBlock::name)`. */
209 /* Geometry modification operations.
211 * Original geometry is stored before some modification is run and is used to restore state of
212 * the object when undoing the operation
214 * Modified geometry is stored after the modification and is used to redo the modification. */
215 bool geometry_clear_pbvh
;
216 undo::NodeGeometry geometry_original
;
217 undo::NodeGeometry geometry_modified
;
219 /* Geometry at the bmesh enter moment. */
220 undo::NodeGeometry geometry_bmesh_enter
;
226 /* Sculpt Face Sets */
227 Array
<int> face_sets
;
229 Vector
<int> face_indices
;
236 /* Factor of brush to have rake point following behind
237 * (could be configurable but this is reasonable default). */
238 #define SCULPT_RAKE_BRUSH_FACTOR 0.25f
240 struct SculptRakeData
{
242 blender::float3 follow_co
;
246 /*************** Brush testing declarations ****************/
247 struct SculptBrushTest
{
248 float radius_squared
;
250 blender::float3 location
;
252 ePaintSymmetryFlags mirror_symmetry_pass
;
254 int radial_symmetry_pass
;
255 blender::float4x4 symm_rot_mat_inv
;
257 /* For circle (not sphere) projection. */
260 /* Some tool code uses a plane for its calculations. */
263 /* View3d clipping - only set rv3d for clipping */
264 RegionView3D
*clip_rv3d
;
267 using SculptBrushTestFn
= bool (*)(SculptBrushTest
*test
, const float co
[3]);
270 enum SculptFilterOrientation
{
271 SCULPT_FILTER_ORIENTATION_LOCAL
= 0,
272 SCULPT_FILTER_ORIENTATION_WORLD
= 1,
273 SCULPT_FILTER_ORIENTATION_VIEW
= 2,
276 /* Defines how transform tools are going to apply its displacement. */
277 enum SculptTransformDisplacementMode
{
278 /* Displaces the elements from their original coordinates. */
279 SCULPT_TRANSFORM_DISPLACEMENT_ORIGINAL
= 0,
280 /* Displaces the elements incrementally from their previous position. */
281 SCULPT_TRANSFORM_DISPLACEMENT_INCREMENTAL
= 1,
284 #define SCULPT_CLAY_STABILIZER_LEN 10
286 namespace blender::ed::sculpt_paint
{
291 bool enabled_axis
[3];
292 bool enabled_force_axis
[3];
295 /* Used for alternating between filter operations in filters that need to apply different ones to
296 * achieve certain effects. */
299 /* Stores the displacement produced by the laplacian step of HC smooth. */
300 float (*surface_smooth_laplacian_disp
)[3];
301 float surface_smooth_shape_preservation
;
302 float surface_smooth_current_vertex
;
304 /* Sharpen mesh filter. */
305 float sharpen_smooth_ratio
;
306 float sharpen_intensify_detail_strength
;
307 int sharpen_curvature_smooth_iterations
;
308 float *sharpen_factor
;
309 float (*detail_directions
)[3];
311 /* Filter orientation. */
312 SculptFilterOrientation orientation
;
316 float4x4 viewmat_inv
;
318 /* Displacement eraser. */
319 float (*limit_surface_co
)[3];
322 Vector
<PBVHNode
*> nodes
;
325 cloth::SimulationData
*cloth_sim
;
326 float3 cloth_sim_pinch_point
;
328 /* mask expand iteration caches */
329 int mask_update_current_it
;
330 int mask_update_last_it
;
332 float *normal_factor
;
335 float3 mask_expand_initial_co
;
342 SculptTransformDisplacementMode transform_displacement_mode
;
344 std::unique_ptr
<auto_mask::Cache
> automasking
;
345 float3 initial_normal
;
348 /* Pre-smoothed colors used by sharpening. Colors are HSL. */
349 float (*pre_smoothed_color
)[4];
352 float start_filter_strength
;
359 * This structure contains all the temporary data
360 * needed for individual brush strokes.
364 float initial_radius
;
367 float3 clip_tolerance
;
368 float4x4 clip_mirror_mtx
;
369 float2 initial_mouse
;
373 float radius_squared
;
374 float3 true_location
;
375 float3 true_last_location
;
377 float3 last_location
;
378 float stroke_distance
;
380 /* Used for alternating between deformation in brushes that need to apply different ones to
381 * achieve certain effects. */
384 /* Original pixel radius with the pressure curve applied for dyntopo detail size */
385 float dyntopo_pixel_radius
;
393 float normal_weight
; /* from brush (with optional override) */
397 /* Position of the mouse corresponding to the stroke location, modified by the paint_stroke
398 * operator according to the stroke type. */
400 /* Position of the mouse event in screen space, not modified by the stroke type. */
403 float (*prev_colors
)[4];
404 GArray
<> prev_colors_vpaint
;
406 /* Multires Displacement Smear. */
407 float (*prev_displacement
)[3];
408 float (*limit_surface_co
)[3];
410 /* The rest is temporary storage that isn't saved as a property */
412 bool first_time
; /* Beginning of stroke may do some things special */
414 /* from ED_view3d_ob_project_mat_get() */
415 float4x4 projection_mat
;
421 float special_rotation
;
422 float3 grab_delta
, grab_delta_symmetry
;
423 float3 old_grab_location
, orig_grab_location
;
425 /* screen-space rotation defined by mouse motion */
426 float rake_rotation
[4], rake_rotation_symmetry
[4];
427 bool is_rake_rotation_valid
;
428 SculptRakeData rake_data
;
433 /* Symmetry index between 0 and 7 bit combo 0 is Brush only;
434 * 1 is X mirror; 2 is Y mirror; 3 is XY; 4 is Z; 5 is XZ; 6 is YZ; 7 is XYZ */
436 /* The symmetry pass we are currently on between 0 and 7. */
437 ePaintSymmetryFlags mirror_symmetry_pass
;
438 float3 true_view_normal
;
441 /* sculpt_normal gets calculated by calc_sculpt_normal(), then the
442 * sculpt_normal_symm gets updated quickly with the usual symmetry
444 float3 sculpt_normal
;
445 float3 sculpt_normal_symm
;
447 /* Used for area texture mode, local_mat gets calculated by
448 * calc_brush_local_mat() and used in sculpt_apply_texture().
449 * Transforms from model-space coords to local area coords.
451 float4x4 brush_local_mat
;
452 /* The matrix from local area coords to model-space coords is used to calculate the vector
453 * displacement in area plane mode. */
454 float4x4 brush_local_mat_inv
;
456 float3 plane_offset
; /* used to shift the plane around when doing tiled strokes */
460 int radial_symmetry_pass
;
461 float4x4 symm_rot_mat
;
462 float4x4 symm_rot_mat_inv
;
464 /* Accumulate mode. Note: inverted for SCULPT_TOOL_DRAW_SHARP. */
467 float3 anchored_location
;
474 float wet_persistence
;
479 std::unique_ptr
<SculptPoseIKChain
> pose_ik_chain
;
481 /* Enhance Details. */
482 float (*detail_directions
)[3];
484 /* Clay Thumb brush */
485 /* Angle of the front tilting plane of the brush to simulate clay accumulation. */
486 float clay_thumb_front_angle
;
487 /* Stores pressure samples to get an stabilized strength and radius variation. */
488 float clay_pressure_stabilizer
[SCULPT_CLAY_STABILIZER_LEN
];
489 int clay_pressure_stabilizer_index
;
492 cloth::SimulationData
*cloth_sim
;
493 float3 initial_location
;
494 float3 true_initial_location
;
495 float3 initial_normal
;
496 float3 true_initial_normal
;
499 SculptBoundary
*boundaries
[PAINT_SYMM_AREAS
];
501 /* Surface Smooth Brush */
502 /* Stores the displacement produced by the laplacian step of HC smooth. */
503 float (*surface_smooth_laplacian_disp
)[3];
506 float *layer_displacement_factor
;
508 float vertex_rotation
; /* amount to rotate the vertices when using rotate brush */
511 char saved_active_brush_name
[MAX_ID_NAME
];
512 char saved_mask_brush_tool
;
513 int saved_smooth_size
; /* smooth tool copies the size of the current tool */
516 float plane_trim_squared
;
518 bool supports_gravity
;
519 float3 true_gravity_direction
;
520 float3 gravity_direction
;
522 std::unique_ptr
<auto_mask::Cache
> automasking
;
524 float4x4 stroke_local_mat
;
525 float multiplane_scrape_angle
;
527 float4 wet_mix_prev_color
;
530 rcti previous_r
; /* previous redraw rectangle */
531 rcti current_r
; /* current redraw rectangle */
536 /* -------------------------------------------------------------------- */
537 /** \name Sculpt Expand
542 enum eSculptExpandFalloffType
{
543 SCULPT_EXPAND_FALLOFF_GEODESIC
,
544 SCULPT_EXPAND_FALLOFF_TOPOLOGY
,
545 SCULPT_EXPAND_FALLOFF_TOPOLOGY_DIAGONALS
,
546 SCULPT_EXPAND_FALLOFF_NORMALS
,
547 SCULPT_EXPAND_FALLOFF_SPHERICAL
,
548 SCULPT_EXPAND_FALLOFF_BOUNDARY_TOPOLOGY
,
549 SCULPT_EXPAND_FALLOFF_BOUNDARY_FACE_SET
,
550 SCULPT_EXPAND_FALLOFF_ACTIVE_FACE_SET
,
553 enum eSculptExpandTargetType
{
554 SCULPT_EXPAND_TARGET_MASK
,
555 SCULPT_EXPAND_TARGET_FACE_SETS
,
556 SCULPT_EXPAND_TARGET_COLORS
,
559 enum eSculptExpandRecursionType
{
560 SCULPT_EXPAND_RECURSION_TOPOLOGY
,
561 SCULPT_EXPAND_RECURSION_GEODESICS
,
564 #define EXPAND_SYMM_AREAS 8
567 /* Target data elements that the expand operation will affect. */
568 eSculptExpandTargetType target
;
571 eSculptExpandFalloffType falloff_type
;
573 /* Indexed by vertex index, precalculated falloff value of that vertex (without any falloff
574 * editing modification applied). */
575 Array
<float> vert_falloff
;
576 /* Max falloff value in *vert_falloff. */
577 float max_vert_falloff
;
579 /* Indexed by base mesh face index, precalculated falloff value of that face. These values are
580 * calculated from the per vertex falloff (*vert_falloff) when needed. */
582 float max_face_falloff
;
584 /* Falloff value of the active element (vertex or base mesh face) that Expand will expand to. */
585 float active_falloff
;
587 /* When set to true, expand skips all falloff computations and considers all elements as enabled.
591 /* Initial mouse and cursor data from where the current falloff started. This data can be changed
592 * during the execution of Expand by moving the origin. */
593 float2 initial_mouse_move
;
594 float2 initial_mouse
;
595 PBVHVertRef initial_active_vertex
;
596 int initial_active_vertex_i
;
597 int initial_active_face_set
;
599 /* Maximum number of vertices allowed in the SculptSession for previewing the falloff using
600 * geodesic distances. */
601 int max_geodesic_move_preview
;
603 /* Original falloff type before starting the move operation. */
604 eSculptExpandFalloffType move_original_falloff_type
;
605 /* Falloff type using when moving the origin for preview. */
606 eSculptExpandFalloffType move_preview_falloff_type
;
608 /* Face set ID that is going to be used when creating a new Face Set. */
611 /* Face Set ID of the Face set selected for editing. */
614 /* Mouse position since the last time the origin was moved. Used for reference when moving the
615 * initial position of Expand. */
616 float2 original_mouse_move
;
618 /* Active island checks. */
619 /* Indexed by symmetry pass index, contains the connected island ID for that
620 * symmetry pass. Other connected island IDs not found in this
621 * array will be ignored by Expand. */
622 int active_connected_islands
[EXPAND_SYMM_AREAS
];
625 /* Set containing all Face Sets IDs that Expand will use to snap the new data. */
626 std::unique_ptr
<Set
<int>> snap_enabled_face_sets
;
628 /* Texture distortion data. */
631 // struct MTex *mtex;
633 /* Controls how much texture distortion will be applied to the current falloff */
634 float texture_distortion_strength
;
636 /* Cached PBVH nodes. This allows to skip gathering all nodes from the PBVH each time expand
637 * needs to update the state of the elements. */
638 Vector
<PBVHNode
*> nodes
;
640 /* Expand state options. */
642 /* Number of loops (times that the falloff is going to be repeated). */
645 /* Invert the falloff result. */
648 /* When set to true, preserves the previous state of the data and adds the new one on top. */
651 /* When set to true, the mask or colors will be applied as a gradient. */
652 bool falloff_gradient
;
654 /* When set to true, Expand will use the Brush falloff curve data to shape the gradient. */
657 /* When set to true, Expand will move the origin (initial active vertex and cursor position)
658 * instead of updating the active vertex and active falloff. */
661 /* When set to true, Expand will snap the new data to the Face Sets IDs found in
662 * *original_face_sets. */
665 /* When set to true, Expand will use the current Face Set ID to modify an existing Face Set
666 * instead of creating a new one. */
667 bool modify_active_face_set
;
669 /* When set to true, Expand will reposition the sculpt pivot to the boundary of the expand result
670 * after finishing the operation. */
671 bool reposition_pivot
;
673 /* If nothing is masked set mask of every vertex to 0. */
676 /* Color target data type related data. */
680 /* Face Sets at the first step of the expand operation, before starting modifying the active
681 * vertex and active falloff. These are not the original Face Sets of the sculpt before starting
682 * the operator as they could have been modified by Expand when initializing the operator and
683 * before starting changing the active vertex. These Face Sets are used for restoring and
684 * checking the Face Sets state while the Expand operation modal runs. */
685 Array
<int> initial_face_sets
;
687 /* Original data of the sculpt as it was before running the Expand operator. */
688 Array
<float> original_mask
;
689 Array
<int> original_face_sets
;
690 float (*original_colors
)[4];
693 int normal_falloff_blur_steps
;
704 /* -------------------------------------------------------------------- */
705 /** \name Sculpt Poll Functions
708 bool SCULPT_mode_poll(bContext
*C
);
709 bool SCULPT_mode_poll_view3d(bContext
*C
);
711 * Checks for a brush, not just sculpt mode.
713 bool SCULPT_poll(bContext
*C
);
716 * Returns true if sculpt session can handle color attributes
717 * (BKE_pbvh_type(ss->pbvh) == PBVH_FACES). If false an error
718 * message will be shown to the user. Operators should return
719 * OPERATOR_CANCELLED in this case.
721 * NOTE: Does not check if a color attribute actually exists.
722 * Calling code must handle this itself; in most cases a call to
723 * BKE_sculpt_color_layer_create_if_needed() is sufficient.
725 bool SCULPT_handles_colors_report(SculptSession
*ss
, ReportList
*reports
);
729 /* -------------------------------------------------------------------- */
730 /** \name Sculpt Update Functions
733 void SCULPT_flush_update_step(bContext
*C
, SculptUpdateType update_flags
);
734 void SCULPT_flush_update_done(const bContext
*C
, Object
*ob
, SculptUpdateType update_flags
);
736 void SCULPT_pbvh_clear(Object
*ob
);
739 * Flush displacement from deformed PBVH to original layer.
741 void SCULPT_flush_stroke_deform(Sculpt
*sd
, Object
*ob
, bool is_proxy_used
);
744 * Should be used after modifying the mask or Face Sets IDs.
746 void SCULPT_tag_update_overlays(bContext
*C
);
749 /* -------------------------------------------------------------------- */
750 /** \name Stroke Functions
754 * Do a ray-cast in the tree to find the 3d brush location
755 * (This allows us to ignore the GL depth buffer)
756 * Returns 0 if the ray doesn't hit the mesh, non-zero otherwise.
758 * If check_closest is true and the ray test fails a point closest
759 * to the ray will be found. If limit_closest_radius is true then
760 * the closest point will be tested against the active brush radius.
762 bool SCULPT_stroke_get_location_ex(bContext
*C
,
767 bool limit_closest_radius
);
769 bool SCULPT_stroke_get_location(bContext
*C
,
771 const float mouse
[2],
772 bool force_original
);
774 * Gets the normal, location and active vertex location of the geometry under the cursor. This also
775 * updates the active vertex and cursor related data of the SculptSession using the mouse position
777 bool SCULPT_cursor_geometry_info_update(bContext
*C
,
778 SculptCursorGeometryInfo
*out
,
779 const float mouse
[2],
780 bool use_sampled_normal
);
781 void SCULPT_geometry_preview_lines_update(bContext
*C
, SculptSession
*ss
, float radius
);
783 void SCULPT_stroke_modifiers_check(const bContext
*C
, Object
*ob
, const Brush
*brush
);
784 float SCULPT_raycast_init(ViewContext
*vc
,
792 ePaintSymmetryFlags
SCULPT_mesh_symmetry_xyz_get(Object
*object
);
795 * Returns true when the step belongs to the stroke that is directly performed by the brush and
796 * not by one of the symmetry passes.
798 bool SCULPT_stroke_is_main_symmetry_pass(blender::ed::sculpt_paint::StrokeCache
*cache
);
800 * Return true only once per stroke on the first symmetry pass, regardless of the symmetry passes
803 * This should be used for functionality that needs to be computed once per stroke of a particular
804 * tool (allocating memory, updating random seeds...).
806 bool SCULPT_stroke_is_first_brush_step(blender::ed::sculpt_paint::StrokeCache
*cache
);
808 * Returns true on the first brush step of each symmetry pass.
810 bool SCULPT_stroke_is_first_brush_step_of_symmetry_pass(
811 blender::ed::sculpt_paint::StrokeCache
*cache
);
815 /* -------------------------------------------------------------------- */
816 /** \name Sculpt mesh accessor API
819 struct SculptMaskWriteInfo
{
820 float *layer
= nullptr;
823 SculptMaskWriteInfo
SCULPT_mask_get_for_write(SculptSession
*ss
);
824 inline void SCULPT_mask_vert_set(const PBVHType type
,
825 const SculptMaskWriteInfo mask_write
,
831 mask_write
.layer
[vd
.index
] = value
;
834 BM_ELEM_CD_SET_FLOAT(vd
.bm_vert
, mask_write
.bm_offset
, value
);
837 *CCG_elem_mask(&vd
.key
, vd
.grid
) = value
;
842 /** Ensure random access; required for PBVH_BMESH */
843 void SCULPT_vertex_random_access_ensure(SculptSession
*ss
);
845 int SCULPT_vertex_count_get(const SculptSession
*ss
);
846 const float *SCULPT_vertex_co_get(const SculptSession
*ss
, PBVHVertRef vertex
);
848 /** Get the normal for a given sculpt vertex; do not modify the result */
849 void SCULPT_vertex_normal_get(const SculptSession
*ss
, PBVHVertRef vertex
, float no
[3]);
851 float SCULPT_mask_get_at_grids_vert_index(const SubdivCCG
&subdiv_ccg
,
854 void SCULPT_vertex_color_get(const SculptSession
*ss
, PBVHVertRef vertex
, float r_color
[4]);
855 void SCULPT_vertex_color_set(SculptSession
*ss
, PBVHVertRef vertex
, const float color
[4]);
857 bool SCULPT_vertex_is_occluded(SculptSession
*ss
, PBVHVertRef vertex
, bool original
);
859 /** Returns true if a color attribute exists in the current sculpt session. */
860 bool SCULPT_has_colors(const SculptSession
*ss
);
862 /** Returns true if the active color attribute is on loop (AttrDomain::Corner) domain. */
863 bool SCULPT_has_loop_colors(const Object
*ob
);
865 const float *SCULPT_vertex_persistent_co_get(SculptSession
*ss
, PBVHVertRef vertex
);
866 void SCULPT_vertex_persistent_normal_get(SculptSession
*ss
, PBVHVertRef vertex
, float no
[3]);
869 * Coordinates used for manipulating the base mesh when Grab Active Vertex is enabled.
871 const float *SCULPT_vertex_co_for_grab_active_get(SculptSession
*ss
, PBVHVertRef vertex
);
874 * Returns the info of the limit surface when multi-res is available,
875 * otherwise it returns the current coordinate of the vertex.
877 void SCULPT_vertex_limit_surface_get(SculptSession
*ss
, PBVHVertRef vertex
, float r_co
[3]);
880 * Returns the pointer to the coordinates that should be edited from a brush tool iterator
881 * depending on the given deformation target.
883 float *SCULPT_brush_deform_target_vertex_co_get(SculptSession
*ss
,
885 PBVHVertexIter
*iter
);
887 void SCULPT_vertex_neighbors_get(const SculptSession
*ss
,
889 bool include_duplicates
,
890 SculptVertexNeighborIter
*iter
);
892 /** Iterator over neighboring vertices. */
893 #define SCULPT_VERTEX_NEIGHBORS_ITER_BEGIN(ss, v_index, neighbor_iterator) \
894 SCULPT_vertex_neighbors_get(ss, v_index, false, &neighbor_iterator); \
895 for (neighbor_iterator.i = 0; neighbor_iterator.i < neighbor_iterator.size; \
896 neighbor_iterator.i++) \
898 neighbor_iterator.vertex = neighbor_iterator.neighbors[neighbor_iterator.i]; \
899 neighbor_iterator.index = neighbor_iterator.neighbor_indices[neighbor_iterator.i];
902 * Iterate over neighboring and duplicate vertices (for PBVH_GRIDS).
903 * Duplicates come first since they are nearest for flood-fill.
905 #define SCULPT_VERTEX_DUPLICATES_AND_NEIGHBORS_ITER_BEGIN(ss, v_index, neighbor_iterator) \
906 SCULPT_vertex_neighbors_get(ss, v_index, true, &neighbor_iterator); \
907 for (neighbor_iterator.i = neighbor_iterator.size - 1; neighbor_iterator.i >= 0; \
908 neighbor_iterator.i--) \
910 neighbor_iterator.vertex = neighbor_iterator.neighbors[neighbor_iterator.i]; \
911 neighbor_iterator.index = neighbor_iterator.neighbor_indices[neighbor_iterator.i]; \
912 neighbor_iterator.is_duplicate = (neighbor_iterator.i >= \
913 neighbor_iterator.size - neighbor_iterator.num_duplicates);
915 #define SCULPT_VERTEX_NEIGHBORS_ITER_END(neighbor_iterator) \
917 if (neighbor_iterator.neighbors != neighbor_iterator.neighbors_fixed) { \
918 MEM_freeN(neighbor_iterator.neighbors); \
922 PBVHVertRef
SCULPT_active_vertex_get(SculptSession
*ss
);
923 const float *SCULPT_active_vertex_co_get(SculptSession
*ss
);
925 /* Returns PBVH deformed vertices array if shape keys or deform modifiers are used, otherwise
926 * returns mesh original vertices array. */
927 blender::MutableSpan
<blender::float3
> SCULPT_mesh_deformed_positions_get(SculptSession
*ss
);
931 #define FAKE_NEIGHBOR_NONE -1
933 void SCULPT_fake_neighbors_ensure(Object
*ob
, float max_dist
);
934 void SCULPT_fake_neighbors_enable(Object
*ob
);
935 void SCULPT_fake_neighbors_disable(Object
*ob
);
936 void SCULPT_fake_neighbors_free(Object
*ob
);
939 void SCULPT_boundary_info_ensure(Object
*object
);
940 /* Boundary Info needs to be initialized in order to use this function. */
941 bool SCULPT_vertex_is_boundary(const SculptSession
*ss
, PBVHVertRef vertex
);
945 /* -------------------------------------------------------------------- */
946 /** \name Sculpt Visibility API
949 namespace blender::ed::sculpt_paint
{
953 bool vert_visible_get(const SculptSession
*ss
, PBVHVertRef vertex
);
954 bool vert_all_faces_visible_get(const SculptSession
*ss
, PBVHVertRef vertex
);
955 bool vert_any_face_visible_get(const SculptSession
*ss
, PBVHVertRef vertex
);
961 /* -------------------------------------------------------------------- */
962 /** \name Face Sets API
967 int active_face_set_get(const SculptSession
*ss
);
968 int vert_face_set_get(const SculptSession
*ss
, PBVHVertRef vertex
);
970 bool vert_has_face_set(const SculptSession
*ss
, PBVHVertRef vertex
, int face_set
);
971 bool vert_has_unique_face_set(const SculptSession
*ss
, PBVHVertRef vertex
);
973 bke::SpanAttributeWriter
<int> ensure_face_sets_mesh(Object
&object
);
974 int ensure_face_sets_bmesh(Object
&object
);
975 Array
<int> duplicate_face_sets(const Mesh
&mesh
);
983 /* -------------------------------------------------------------------- */
984 /** \name Original Data API
988 * Initialize a #SculptOrigVertData for accessing original vertex data;
989 * handles #BMesh, #Mesh, and multi-resolution.
991 void SCULPT_orig_vert_data_init(SculptOrigVertData
*data
,
994 blender::ed::sculpt_paint::undo::Type type
);
996 * Update a #SculptOrigVertData for a particular vertex from the PBVH iterator.
998 void SCULPT_orig_vert_data_update(SculptOrigVertData
*orig_data
, PBVHVertexIter
*iter
);
1000 * Initialize a #SculptOrigVertData for accessing original vertex data;
1001 * handles #BMesh, #Mesh, and multi-resolution.
1003 void SCULPT_orig_vert_data_unode_init(SculptOrigVertData
*data
,
1005 blender::ed::sculpt_paint::undo::Node
*unode
);
1008 /* -------------------------------------------------------------------- */
1009 /** \name Brush Utilities.
1012 bool SCULPT_tool_needs_all_pbvh_nodes(const Brush
*brush
);
1014 void SCULPT_calc_brush_plane(Sculpt
*sd
,
1016 blender::Span
<PBVHNode
*> nodes
,
1018 float r_area_co
[3]);
1020 std::optional
<blender::float3
> SCULPT_calc_area_normal(Sculpt
*sd
,
1022 blender::Span
<PBVHNode
*> nodes
);
1024 * This calculates flatten center and area normal together,
1025 * amortizing the memory bandwidth and loop overhead to calculate both at the same time.
1027 void SCULPT_calc_area_normal_and_center(Sculpt
*sd
,
1029 blender::Span
<PBVHNode
*> nodes
,
1031 float r_area_co
[3]);
1032 void SCULPT_calc_area_center(Sculpt
*sd
,
1034 blender::Span
<PBVHNode
*> nodes
,
1035 float r_area_co
[3]);
1037 PBVHVertRef
SCULPT_nearest_vertex_get(Object
*ob
,
1042 int SCULPT_plane_point_side(const float co
[3], const float plane
[4]);
1043 int SCULPT_plane_trim(const blender::ed::sculpt_paint::StrokeCache
*cache
,
1045 const float val
[3]);
1047 * Handles clipping against a mirror modifier and #SCULPT_LOCK_X/Y/Z axis flags.
1049 void SCULPT_clip(Sculpt
*sd
, SculptSession
*ss
, float co
[3], const float val
[3]);
1051 float SCULPT_brush_plane_offset_get(Sculpt
*sd
, SculptSession
*ss
);
1053 ePaintSymmetryAreas
SCULPT_get_vertex_symm_area(const float co
[3]);
1054 bool SCULPT_check_vertex_pivot_symmetry(const float vco
[3], const float pco
[3], char symm
);
1056 * Checks if a vertex is inside the brush radius from any of its mirrored axis.
1058 bool SCULPT_is_vertex_inside_brush_radius_symm(const float vertex
[3],
1059 const float br_co
[3],
1062 bool SCULPT_is_symmetry_iteration_valid(char i
, char symm
);
1063 void SCULPT_flip_v3_by_symm_area(float v
[3],
1064 ePaintSymmetryFlags symm
,
1065 ePaintSymmetryAreas symmarea
,
1066 const float pivot
[3]);
1067 void SCULPT_flip_quat_by_symm_area(float quat
[4],
1068 ePaintSymmetryFlags symm
,
1069 ePaintSymmetryAreas symmarea
,
1070 const float pivot
[3]);
1073 * Initialize a point-in-brush test
1075 void SCULPT_brush_test_init(SculptSession
*ss
, SculptBrushTest
*test
);
1077 bool SCULPT_brush_test_sphere_sq(SculptBrushTest
*test
, const float co
[3]);
1078 bool SCULPT_brush_test_cube(SculptBrushTest
*test
,
1080 const float local
[4][4],
1081 const float roundness
,
1082 const float tip_scale_x
);
1083 bool SCULPT_brush_test_circle_sq(SculptBrushTest
*test
, const float co
[3]);
1085 namespace blender::ed::sculpt_paint
{
1087 bool node_fully_masked_or_hidden(const PBVHNode
&node
);
1088 bool node_in_sphere(const PBVHNode
&node
, const float3
&location
, float radius_sq
, bool original
);
1089 bool node_in_cylinder(const DistRayAABB_Precalc
&dist_ray_precalc
,
1090 const PBVHNode
&node
,
1096 void SCULPT_combine_transform_proxies(Sculpt
*sd
, Object
*ob
);
1099 * Initialize a point-in-brush test with a given falloff shape.
1101 * \param falloff_shape: #PAINT_FALLOFF_SHAPE_SPHERE or #PAINT_FALLOFF_SHAPE_TUBE.
1102 * \return The brush falloff function.
1105 SculptBrushTestFn
SCULPT_brush_test_init_with_falloff_shape(SculptSession
*ss
,
1106 SculptBrushTest
*test
,
1107 char falloff_shape
);
1108 const float *SCULPT_brush_frontface_normal_from_falloff_shape(SculptSession
*ss
,
1109 char falloff_shape
);
1110 void SCULPT_cube_tip_init(Sculpt
*sd
, Object
*ob
, Brush
*brush
, float mat
[4][4]);
1113 * Return a multiplier for brush strength on a particular vertex.
1115 float SCULPT_brush_strength_factor(
1118 const float point
[3],
1123 const PBVHVertRef vertex
,
1125 const blender::ed::sculpt_paint::auto_mask::NodeData
*automask_data
);
1128 * Return a color of a brush texture on a particular vertex multiplied by active masks.
1130 void SCULPT_brush_strength_color(
1133 const float brush_point
[3],
1138 const PBVHVertRef vertex
,
1140 const blender::ed::sculpt_paint::auto_mask::NodeData
*automask_data
,
1144 * Calculates the vertex offset for a single vertex depending on the brush setting rgb as vector
1147 void SCULPT_calc_vertex_displacement(SculptSession
*ss
,
1153 * Tilts a normal by the x and y tilt values using the view axis.
1155 void SCULPT_tilt_apply_to_normal(float r_normal
[3],
1156 blender::ed::sculpt_paint::StrokeCache
*cache
,
1157 float tilt_strength
);
1160 * Get effective surface normal with pen tilt and tilt strength applied to it.
1162 void SCULPT_tilt_effective_normal_get(const SculptSession
*ss
, const Brush
*brush
, float r_no
[3]);
1166 /* -------------------------------------------------------------------- */
1167 /** \name Flood Fill
1170 namespace blender::ed::sculpt_paint::flood_fill
{
1173 std::queue
<PBVHVertRef
> queue
;
1174 blender::BitVector
<> visited_verts
;
1177 FillData
init_fill(SculptSession
*ss
);
1178 void add_active(Object
*ob
, SculptSession
*ss
, FillData
*flood
, float radius
);
1179 void add_initial_with_symmetry(
1180 Object
*ob
, SculptSession
*ss
, FillData
*flood
, PBVHVertRef vertex
, float radius
);
1181 void add_initial(FillData
*flood
, PBVHVertRef vertex
);
1182 void add_and_skip_initial(FillData
*flood
, PBVHVertRef vertex
);
1183 void execute(SculptSession
*ss
,
1185 FunctionRef
<bool(PBVHVertRef from_v
, PBVHVertRef to_v
, bool is_duplicate
)> func
);
1191 /* -------------------------------------------------------------------- */
1192 /** \name Dynamic topology
1195 namespace blender::ed::sculpt_paint::dyntopo
{
1201 MODIFIER
= (1 << 3),
1203 ENUM_OPERATORS(WarnFlag
, MODIFIER
);
1205 /** Enable dynamic topology; mesh will be triangulated */
1206 void enable_ex(Main
*bmain
, Depsgraph
*depsgraph
, Object
*ob
);
1207 void disable(bContext
*C
, undo::Node
*unode
);
1208 void disable_with_undo(Main
*bmain
, Depsgraph
*depsgraph
, Scene
*scene
, Object
*ob
);
1211 * Returns true if the stroke will use dynamic topology, false
1214 * Factors: some brushes like grab cannot do dynamic topology.
1215 * Others, like smooth, are better without.
1216 * Same goes for alt-key smoothing.
1218 bool stroke_is_dyntopo(const SculptSession
*ss
, const Brush
*brush
);
1220 void triangulate(BMesh
*bm
);
1222 WarnFlag
check_attribute_warning(Scene
*scene
, Object
*ob
);
1224 namespace detail_size
{
1227 * Scaling factor to match the displayed size to the actual sculpted size
1229 constexpr float RELATIVE_SCALE_FACTOR
= 0.4f
;
1232 * Converts from Sculpt#constant_detail to the PBVH max edge length.
1234 float constant_to_detail_size(const float constant_detail
, const Object
*ob
);
1237 * Converts from Sculpt#detail_percent to the PBVH max edge length.
1239 float brush_to_detail_size(const float brush_percent
, const float brush_radius
);
1242 * Converts from Sculpt#detail_size to the PBVH max edge length.
1244 float relative_to_detail_size(const float relative_detail
,
1245 const float brush_radius
,
1246 const float pixel_radius
,
1247 const float pixel_size
);
1250 * Converts from Sculpt#constant_detail to equivalent Sculpt#detail_percent value.
1252 * Corresponds to a change from Constant & Manual Detailing to Brush Detailing.
1254 float constant_to_brush_detail(const float constant_detail
,
1255 const float brush_radius
,
1259 * Converts from Sculpt#constant_detail to equivalent Sculpt#detail_size value.
1261 * Corresponds to a change from Constant & Manual Detailing to Relative Detailing.
1263 float constant_to_relative_detail(const float constant_detail
,
1264 const float brush_radius
,
1265 const float pixel_radius
,
1266 const float pixel_size
,
1273 /* -------------------------------------------------------------------- */
1274 /** \name Auto-masking.
1277 namespace blender::ed::sculpt_paint::auto_mask
{
1280 /* eAutomasking_flag. */
1282 int initial_face_set
;
1283 int initial_island_nr
;
1285 float cavity_factor
;
1286 int cavity_blur_steps
;
1287 CurveMapping
*cavity_curve
;
1289 float start_normal_limit
, start_normal_falloff
;
1290 float view_normal_limit
, view_normal_falloff
;
1292 bool topology_use_brush_limit
;
1298 bool can_reuse_mask
;
1299 uchar current_stroke_id
;
1303 SculptOrigVertData orig_data
;
1304 bool have_orig_data
;
1308 * Call before PBVH vertex iteration.
1309 * \param automask_data: pointer to an uninitialized #auto_mask::NodeData struct.
1311 NodeData
node_begin(Object
&object
, const Cache
*automasking
, PBVHNode
&node
);
1313 /* Call before factor_get and SCULPT_brush_strength_factor. */
1314 void node_update(NodeData
&automask_data
, PBVHVertexIter
&vd
);
1316 float factor_get(const Cache
*automasking
,
1319 const NodeData
*automask_data
);
1321 /* Returns the automasking cache depending on the active tool. Used for code that can run both for
1322 * brushes and filter. */
1323 Cache
*active_cache_get(SculptSession
*ss
);
1326 * Creates and initializes an automasking cache.
1328 * For automasking modes that cannot be calculated in real time,
1329 * data is also stored at the vertex level prior to the stroke starting.
1331 std::unique_ptr
<Cache
> cache_init(const Sculpt
*sd
, Object
*ob
);
1332 std::unique_ptr
<Cache
> cache_init(const Sculpt
*sd
, const Brush
*brush
, Object
*ob
);
1333 void cache_free(Cache
*automasking
);
1335 bool mode_enabled(const Sculpt
*sd
, const Brush
*br
, eAutomasking_flag mode
);
1336 bool is_enabled(const Sculpt
*sd
, const SculptSession
*ss
, const Brush
*br
);
1338 bool needs_normal(const SculptSession
*ss
, const Sculpt
*sculpt
, const Brush
*brush
);
1339 int settings_hash(const Object
&ob
, const Cache
&automasking
);
1341 bool tool_can_reuse_automask(int sculpt_tool
);
1347 /* -------------------------------------------------------------------- */
1348 /** \name Geodesic distances.
1351 namespace blender::ed::sculpt_paint::geodesic
{
1354 * Returns an array indexed by vertex index containing the geodesic distance to the closest vertex
1355 * in the initial vertex set. The caller is responsible for freeing the array.
1356 * Geodesic distances will only work when used with PBVH_FACES, for other types of PBVH it will
1357 * fallback to euclidean distances to one of the initial vertices in the set.
1359 Array
<float> distances_create(Object
*ob
, const Set
<int> &initial_verts
, float limit_radius
);
1360 Array
<float> distances_create_from_vert_and_symm(Object
*ob
,
1362 float limit_radius
);
1368 /* -------------------------------------------------------------------- */
1369 /** \name Filter API
1372 namespace blender::ed::sculpt_paint::filter
{
1374 void cache_init(bContext
*C
,
1377 undo::Type undo_type
,
1378 const float mval_fl
[2],
1379 float area_normal_radius
,
1380 float start_strength
);
1381 void cache_free(SculptSession
*ss
);
1382 void register_operator_props(wmOperatorType
*ot
);
1384 /* Filter orientation utils. */
1385 void to_orientation_space(float r_v
[3], filter::Cache
*filter_cache
);
1386 void to_object_space(float r_v
[3], filter::Cache
*filter_cache
);
1387 void zero_disabled_axis_components(float r_v
[3], filter::Cache
*filter_cache
);
1393 /* -------------------------------------------------------------------- */
1394 /** \name Cloth Simulation.
1397 namespace blender::ed::sculpt_paint::cloth
{
1399 /* Cloth Simulation. */
1401 /* Constraints were not built for this node, so it can't be simulated. */
1402 SCULPT_CLOTH_NODE_UNINITIALIZED
,
1404 /* There are constraints for the geometry in this node, but it should not be simulated. */
1405 SCULPT_CLOTH_NODE_INACTIVE
,
1407 /* There are constraints for this node and they should be used by the solver. */
1408 SCULPT_CLOTH_NODE_ACTIVE
,
1411 enum ConstraintType
{
1412 /* Constraint that creates the structure of the cloth. */
1413 SCULPT_CLOTH_CONSTRAINT_STRUCTURAL
= 0,
1414 /* Constraint that references the position of a vertex and a position in deformation_pos which
1415 * can be deformed by the tools. */
1416 SCULPT_CLOTH_CONSTRAINT_DEFORMATION
= 1,
1417 /* Constraint that references the vertex position and a editable soft-body position for
1419 SCULPT_CLOTH_CONSTRAINT_SOFTBODY
= 2,
1420 /* Constraint that references the vertex position and its initial position. */
1421 SCULPT_CLOTH_CONSTRAINT_PIN
= 3,
1424 struct LengthConstraint
{
1425 /* Elements that are affected by the constraint. */
1426 /* Element a should always be a mesh vertex with the index stored in elem_index_a as it is always
1427 * deformed. Element b could be another vertex of the same mesh or any other position (arbitrary
1428 * point, position for a previous state). In that case, elem_index_a and elem_index_b should be
1429 * the same to avoid affecting two different vertices when solving the constraints.
1430 * *elem_position points to the position which is owned by the element. */
1432 float *elem_position_a
;
1435 float *elem_position_b
;
1440 /* Index in #SimulationData.node_state of the node from where this constraint was created.
1441 * This constraints will only be used by the solver if the state is active. */
1444 ConstraintType type
;
1447 struct SimulationData
{
1448 LengthConstraint
*length_constraints
;
1449 int tot_length_constraints
;
1450 Set
<OrderedEdge
> created_length_constraints
;
1451 int capacity_length_constraints
;
1452 float *length_constraint_tweak
;
1454 /* Position anchors for deformation brushes. These positions are modified by the brush and the
1455 * final positions of the simulated vertices are updated with constraints that use these points
1457 float (*deformation_pos
)[3];
1458 float *deformation_strength
;
1462 float softbody_strength
;
1464 float (*acceleration
)[3];
1466 float (*init_pos
)[3];
1467 float (*init_no
)[3];
1468 float (*softbody_pos
)[3];
1469 float (*prev_pos
)[3];
1470 float (*last_iteration_pos
)[3];
1472 ListBase
*collider_list
;
1475 /** #PBVHNode pointer as a key, index in #SimulationData.node_state as value. */
1476 GHash
*node_state_index
;
1477 NodeSimState
*node_state
;
1479 VArraySpan
<float> mask_mesh
;
1480 int mask_cd_offset_bmesh
;
1484 /* Main cloth brush function */
1485 void do_cloth_brush(Sculpt
*sd
, Object
*ob
, Span
<PBVHNode
*> nodes
);
1487 void simulation_free(SimulationData
*cloth_sim
);
1489 /* Public functions. */
1491 SimulationData
*brush_simulation_create(Object
*ob
,
1493 float cloth_damping
,
1494 float cloth_softbody_strength
,
1495 bool use_collisions
,
1496 bool needs_deform_coords
);
1497 void brush_simulation_init(SculptSession
*ss
, SimulationData
*cloth_sim
);
1499 void sim_activate_nodes(SimulationData
*cloth_sim
, Span
<PBVHNode
*> nodes
);
1501 void brush_store_simulation_state(SculptSession
*ss
, SimulationData
*cloth_sim
);
1503 void do_simulation_step(Sculpt
*sd
, Object
*ob
, SimulationData
*cloth_sim
, Span
<PBVHNode
*> nodes
);
1505 void ensure_nodes_constraints(Sculpt
*sd
,
1507 Span
<PBVHNode
*> nodes
,
1508 SimulationData
*cloth_sim
,
1509 float initial_location
[3],
1513 * Cursor drawing function.
1515 void simulation_limits_draw(uint gpuattr
,
1517 const float location
[3],
1518 const float normal
[3],
1521 const float outline_col
[3],
1523 void plane_falloff_preview_draw(uint gpuattr
,
1525 const float outline_col
[3],
1526 float outline_alpha
);
1528 Vector
<PBVHNode
*> brush_affected_nodes_gather(SculptSession
*ss
, Brush
*brush
);
1530 bool is_cloth_deform_brush(const Brush
*brush
);
1536 /* -------------------------------------------------------------------- */
1537 /** \name Smoothing API
1540 namespace blender::ed::sculpt_paint::smooth
{
1543 * For bmesh: Average surrounding verts based on an orthogonality measure.
1544 * Naturally converges to a quad-like structure.
1546 void bmesh_four_neighbor_average(float avg
[3], float direction
[3], BMVert
*v
);
1548 void neighbor_coords_average(SculptSession
*ss
, float result
[3], PBVHVertRef vertex
);
1549 float neighbor_mask_average(SculptSession
*ss
, SculptMaskWriteInfo write_info
, PBVHVertRef vertex
);
1550 void neighbor_color_average(SculptSession
*ss
, float result
[4], PBVHVertRef vertex
);
1553 * Mask the mesh boundaries smoothing only the mesh surface without using auto-masking.
1555 void neighbor_coords_average_interior(SculptSession
*ss
, float result
[3], PBVHVertRef vertex
);
1557 void do_smooth_brush(Sculpt
*sd
, Object
*ob
, Span
<PBVHNode
*> nodes
, float bstrength
);
1558 void do_smooth_brush(Sculpt
*sd
, Object
*ob
, Span
<PBVHNode
*> nodes
);
1560 void do_smooth_mask_brush(Sculpt
*sd
, Object
*ob
, Span
<PBVHNode
*> nodes
, float bstrength
);
1562 /* Surface Smooth Brush. */
1564 void surface_smooth_laplacian_step(SculptSession
*ss
,
1567 float (*laplacian_disp
)[3],
1569 const float origco
[3],
1571 void surface_smooth_displace_step(SculptSession
*ss
,
1573 float (*laplacian_disp
)[3],
1577 void do_surface_smooth_brush(Sculpt
*sd
, Object
*ob
, Span
<PBVHNode
*> nodes
);
1580 void relax_vertex(SculptSession
*ss
,
1583 bool filter_boundary_face_sets
,
1584 float *r_final_pos
);
1591 * Expose 'calc_area_normal' externally (just for vertex paint).
1593 std::optional
<blender::float3
> SCULPT_pbvh_calc_area_normal(const Brush
*brush
,
1595 blender::Span
<PBVHNode
*> nodes
);
1598 * Flip all the edit-data across the axis/axes specified by \a symm.
1599 * Used to calculate multiple modifications to the mesh when symmetry is enabled.
1601 void SCULPT_cache_calc_brushdata_symm(blender::ed::sculpt_paint::StrokeCache
*cache
,
1602 ePaintSymmetryFlags symm
,
1605 void SCULPT_cache_free(blender::ed::sculpt_paint::StrokeCache
*cache
);
1607 /* -------------------------------------------------------------------- */
1608 /** \name Sculpt Undo
1611 namespace blender::ed::sculpt_paint::undo
{
1613 undo::Node
*push_node(const Object
&object
, const PBVHNode
*node
, undo::Type type
);
1614 undo::Node
*get_node(const PBVHNode
*node
, undo::Type type
);
1617 * Pushes an undo step using the operator name. This is necessary for
1618 * redo panels to work; operators that do not support that may use
1619 * #push_begin_ex instead if so desired.
1621 void push_begin(Object
*ob
, const wmOperator
*op
);
1624 * NOTE: #push_begin is preferred since `name`
1625 * must match operator name for redo panels to work.
1627 void push_begin_ex(Object
*ob
, const char *name
);
1628 void push_end(Object
*ob
);
1629 void push_end_ex(Object
*ob
, const bool use_nested_undo
);
1635 void SCULPT_vertcos_to_key(Object
*ob
, KeyBlock
*kb
, blender::Span
<blender::float3
> vertCos
);
1638 * Get a screen-space rectangle of the modified area.
1640 bool SCULPT_get_redraw_rect(ARegion
*region
, RegionView3D
*rv3d
, Object
*ob
, rcti
*rect
);
1644 /* -------------------------------------------------------------------- */
1645 /** \name Expand Operator
1648 namespace blender::ed::sculpt_paint::expand
{
1650 void SCULPT_OT_expand(wmOperatorType
*ot
);
1651 void modal_keymap(wmKeyConfig
*keyconf
);
1657 /* -------------------------------------------------------------------- */
1658 /** \name Gesture Operators
1661 namespace blender::ed::sculpt_paint::gesture
{
1668 enum class SelectionType
{
1674 float4x4 projviewobjmat
;
1679 /* 2D bitmap to test if a vertex is affected by the lasso shape. */
1680 blender::BitVector
<> mask_px
;
1684 /* Plane aligned to the gesture line. */
1685 float true_plane
[4];
1688 /* Planes to limit the action to the length of the gesture segment at both sides of the affected
1690 float side_plane
[2][4];
1691 float true_side_plane
[2][4];
1692 bool use_side_planes
;
1699 /* Common data used for executing a gesture operation. */
1700 struct GestureData
{
1704 /* Enabled and currently active symmetry. */
1705 ePaintSymmetryFlags symm
;
1706 ePaintSymmetryFlags symmpass
;
1708 /* Operation parameters. */
1709 ShapeType shape_type
;
1710 bool front_faces_only
;
1711 SelectionType selection_type
;
1713 Operation
*operation
;
1716 /* Screen space points that represent the gesture shape. */
1717 Array
<float2
> gesture_points
;
1719 /* View parameters. */
1720 float3 true_view_normal
;
1723 float3 true_view_origin
;
1726 float true_clip_planes
[4][4];
1727 float clip_planes
[4][4];
1729 /* These store the view origin and normal in world space, which is used in some gestures to
1730 * generate geometry aligned from the view directly in world space. */
1731 /* World space view origin and normal are not affected by object symmetry when doing symmetry
1732 * passes, so there is no separate variables with the `true_` prefix to store their original
1733 * values without symmetry modifications. */
1734 float3 world_space_view_origin
;
1735 float3 world_space_view_normal
;
1737 /* Lasso Gesture. */
1743 /* Task Callback Data. */
1744 Vector
<PBVHNode
*> nodes
;
1749 /* Common abstraction structure for gesture operations. */
1751 /* Initial setup (data updates, special undo push...). */
1752 void (*begin
)(bContext
&, GestureData
&);
1754 /* Apply the gesture action for each symmetry pass. */
1755 void (*apply_for_symmetry_pass
)(bContext
&, GestureData
&);
1757 /* Remaining actions after finishing the symmetry passes iterations
1758 * (updating data-layers, tagging PBVH updates...). */
1759 void (*end
)(bContext
&, GestureData
&);
1762 /* Determines whether or not a gesture action should be applied. */
1763 bool is_affected(GestureData
&gesture_data
, const float3
&co
, const float3
&vertex_normal
);
1765 /* Initialization functions. */
1766 std::unique_ptr
<GestureData
> init_from_box(bContext
*C
, wmOperator
*op
);
1767 std::unique_ptr
<GestureData
> init_from_lasso(bContext
*C
, wmOperator
*op
);
1768 std::unique_ptr
<GestureData
> init_from_line(bContext
*C
, wmOperator
*op
);
1770 /* Common gesture operator properties. */
1771 void operator_properties(wmOperatorType
*ot
, ShapeType shapeType
);
1773 /* Apply the gesture action to the selected nodes. */
1774 void apply(bContext
&C
, GestureData
&gesture_data
, wmOperator
&op
);
1778 namespace blender::ed::sculpt_paint::project
{
1779 void SCULPT_OT_project_line_gesture(wmOperatorType
*ot
);
1782 namespace blender::ed::sculpt_paint::trim
{
1783 void SCULPT_OT_trim_lasso_gesture(wmOperatorType
*ot
);
1784 void SCULPT_OT_trim_box_gesture(wmOperatorType
*ot
);
1789 /* -------------------------------------------------------------------- */
1790 /** \name Face Set Operators
1793 namespace blender::ed::sculpt_paint::face_set
{
1795 void SCULPT_OT_face_sets_randomize_colors(wmOperatorType
*ot
);
1796 void SCULPT_OT_face_set_change_visibility(wmOperatorType
*ot
);
1797 void SCULPT_OT_face_sets_init(wmOperatorType
*ot
);
1798 void SCULPT_OT_face_sets_create(wmOperatorType
*ot
);
1799 void SCULPT_OT_face_sets_edit(wmOperatorType
*ot
);
1801 void SCULPT_OT_face_set_lasso_gesture(wmOperatorType
*ot
);
1802 void SCULPT_OT_face_set_box_gesture(wmOperatorType
*ot
);
1808 /* -------------------------------------------------------------------- */
1809 /** \name Transform Operators
1812 namespace blender::ed::sculpt_paint
{
1814 void SCULPT_OT_set_pivot_position(wmOperatorType
*ot
);
1820 /* -------------------------------------------------------------------- */
1821 /** \name Filter Operators
1824 namespace blender::ed::sculpt_paint::filter
{
1826 void SCULPT_OT_mesh_filter(wmOperatorType
*ot
);
1827 wmKeyMap
*modal_keymap(wmKeyConfig
*keyconf
);
1831 namespace blender::ed::sculpt_paint::cloth
{
1832 void SCULPT_OT_cloth_filter(wmOperatorType
*ot
);
1835 namespace blender::ed::sculpt_paint::color
{
1836 void SCULPT_OT_color_filter(wmOperatorType
*ot
);
1841 /* -------------------------------------------------------------------- */
1842 /** \name Interactive Mask Operators
1845 namespace blender::ed::sculpt_paint::mask
{
1847 void SCULPT_OT_mask_filter(wmOperatorType
*ot
);
1848 void SCULPT_OT_mask_init(wmOperatorType
*ot
);
1856 /* -------------------------------------------------------------------- */
1857 /** \name Dyntopo/Retopology Operators
1860 namespace blender::ed::sculpt_paint::dyntopo
{
1862 void SCULPT_OT_detail_flood_fill(wmOperatorType
*ot
);
1863 void SCULPT_OT_sample_detail_size(wmOperatorType
*ot
);
1864 void SCULPT_OT_dyntopo_detail_size_edit(wmOperatorType
*ot
);
1865 void SCULPT_OT_dynamic_topology_toggle(wmOperatorType
*ot
);
1871 /* sculpt_brush_types.cc */
1873 /* -------------------------------------------------------------------- */
1877 namespace blender::ed::sculpt_paint::pose
{
1880 * Main Brush Function.
1882 void do_pose_brush(Sculpt
*sd
, Object
*ob
, blender::Span
<PBVHNode
*> nodes
);
1884 * Calculate the pose origin and (Optionally the pose factor)
1885 * that is used when using the pose brush.
1887 * \param r_pose_origin: Must be a valid pointer.
1888 * \param r_pose_factor: Optional, when set to NULL it won't be calculated.
1890 void calc_pose_data(Object
*ob
,
1892 const float3
&initial_location
,
1895 float3
&r_pose_origin
,
1896 MutableSpan
<float> r_pose_factor
);
1897 void pose_brush_init(Object
*ob
, SculptSession
*ss
, Brush
*br
);
1898 std::unique_ptr
<SculptPoseIKChain
> ik_chain_init(
1899 Object
*ob
, SculptSession
*ss
, Brush
*br
, const float3
&initial_location
, float radius
);
1903 namespace blender::ed::sculpt_paint::boundary
{
1906 * Main function to get #SculptBoundary data both for brush deformation and viewport preview.
1907 * Can return NULL if there is no boundary from the given vertex using the given radius.
1909 SculptBoundary
*data_init(Object
*object
, Brush
*brush
, PBVHVertRef initial_vertex
, float radius
);
1910 void data_free(SculptBoundary
*boundary
);
1911 /* Main Brush Function. */
1912 void do_boundary_brush(Sculpt
*sd
, Object
*ob
, blender::Span
<PBVHNode
*> nodes
);
1914 void edges_preview_draw(uint gpuattr
,
1916 const float outline_col
[3],
1917 float outline_alpha
);
1918 void pivot_line_preview_draw(uint gpuattr
, SculptSession
*ss
);
1922 /* Multi-plane Scrape Brush. */
1923 /* Main Brush Function. */
1924 void SCULPT_do_multiplane_scrape_brush(Sculpt
*sd
, Object
*ob
, blender::Span
<PBVHNode
*> nodes
);
1925 void SCULPT_multiplane_scrape_preview_draw(uint gpuattr
,
1928 const float outline_col
[3],
1929 float outline_alpha
);
1931 namespace blender::ed::sculpt_paint
{
1933 namespace face_set
{
1935 void do_draw_face_sets_brush(Sculpt
*sd
, Object
*ob
, Span
<PBVHNode
*> nodes
);
1941 void do_paint_brush(PaintModeSettings
*paint_mode_settings
,
1944 Span
<PBVHNode
*> nodes
,
1945 Span
<PBVHNode
*> texnodes
);
1946 void do_smear_brush(Sculpt
*sd
, Object
*ob
, Span
<PBVHNode
*> nodes
);
1951 * \brief Get the image canvas for painting on the given object.
1953 * \return #true if an image is found. The #r_image and #r_image_user fields are filled with
1954 * the image and image user. Returns false when the image isn't found. In the later case the
1955 * r_image and r_image_user are set to NULL.
1957 bool SCULPT_paint_image_canvas_get(PaintModeSettings
*paint_mode_settings
,
1960 ImageUser
**r_image_user
) ATTR_NONNULL();
1961 void SCULPT_do_paint_brush_image(PaintModeSettings
*paint_mode_settings
,
1964 blender::Span
<PBVHNode
*> texnodes
);
1965 bool SCULPT_use_image_paint_brush(PaintModeSettings
*settings
, Object
*ob
) ATTR_NONNULL();
1967 float SCULPT_clay_thumb_get_stabilized_pressure(blender::ed::sculpt_paint::StrokeCache
*cache
);
1969 void SCULPT_do_draw_brush(Sculpt
*sd
, Object
*ob
, blender::Span
<PBVHNode
*> nodes
);
1971 void SCULPT_do_fill_brush(Sculpt
*sd
, Object
*ob
, blender::Span
<PBVHNode
*> nodes
);
1972 void SCULPT_do_scrape_brush(Sculpt
*sd
, Object
*ob
, blender::Span
<PBVHNode
*> nodes
);
1973 void SCULPT_do_clay_thumb_brush(Sculpt
*sd
, Object
*ob
, blender::Span
<PBVHNode
*> nodes
);
1974 void SCULPT_do_flatten_brush(Sculpt
*sd
, Object
*ob
, blender::Span
<PBVHNode
*> nodes
);
1975 void SCULPT_do_clay_brush(Sculpt
*sd
, Object
*ob
, blender::Span
<PBVHNode
*> nodes
);
1976 void SCULPT_do_clay_strips_brush(Sculpt
*sd
, Object
*ob
, blender::Span
<PBVHNode
*> nodes
);
1977 void SCULPT_do_snake_hook_brush(Sculpt
*sd
, Object
*ob
, blender::Span
<PBVHNode
*> nodes
);
1978 void SCULPT_do_thumb_brush(Sculpt
*sd
, Object
*ob
, blender::Span
<PBVHNode
*> nodes
);
1979 void SCULPT_do_rotate_brush(Sculpt
*sd
, Object
*ob
, blender::Span
<PBVHNode
*> nodes
);
1980 void SCULPT_do_layer_brush(Sculpt
*sd
, Object
*ob
, blender::Span
<PBVHNode
*> nodes
);
1981 void SCULPT_do_inflate_brush(Sculpt
*sd
, Object
*ob
, blender::Span
<PBVHNode
*> nodes
);
1982 void SCULPT_do_nudge_brush(Sculpt
*sd
, Object
*ob
, blender::Span
<PBVHNode
*> nodes
);
1983 void SCULPT_do_crease_brush(Sculpt
*sd
, Object
*ob
, blender::Span
<PBVHNode
*> nodes
);
1984 void SCULPT_do_pinch_brush(Sculpt
*sd
, Object
*ob
, blender::Span
<PBVHNode
*> nodes
);
1985 void SCULPT_do_grab_brush(Sculpt
*sd
, Object
*ob
, blender::Span
<PBVHNode
*> nodes
);
1986 void SCULPT_do_elastic_deform_brush(Sculpt
*sd
, Object
*ob
, blender::Span
<PBVHNode
*> nodes
);
1987 void SCULPT_do_draw_sharp_brush(Sculpt
*sd
, Object
*ob
, blender::Span
<PBVHNode
*> nodes
);
1988 void SCULPT_do_slide_relax_brush(Sculpt
*sd
, Object
*ob
, blender::Span
<PBVHNode
*> nodes
);
1990 void SCULPT_do_displacement_smear_brush(Sculpt
*sd
, Object
*ob
, blender::Span
<PBVHNode
*> nodes
);
1991 void SCULPT_do_displacement_eraser_brush(Sculpt
*sd
, Object
*ob
, blender::Span
<PBVHNode
*> nodes
);
1992 void SCULPT_do_mask_brush_draw(Sculpt
*sd
, Object
*ob
, blender::Span
<PBVHNode
*> nodes
);
1993 void SCULPT_do_mask_brush(Sculpt
*sd
, Object
*ob
, blender::Span
<PBVHNode
*> nodes
);
1996 void SCULPT_bmesh_topology_rake(Sculpt
*sd
,
1998 blender::Span
<PBVHNode
*> nodes
,
2001 /* end sculpt_brush_types.cc */
2005 namespace blender::ed::sculpt_paint
{
2007 void SCULPT_OT_brush_stroke(wmOperatorType
*ot
);
2011 inline bool SCULPT_tool_is_paint(int tool
)
2013 return ELEM(tool
, SCULPT_TOOL_PAINT
, SCULPT_TOOL_SMEAR
);
2016 inline bool SCULPT_tool_is_mask(int tool
)
2018 return ELEM(tool
, SCULPT_TOOL_MASK
);
2021 BLI_INLINE
bool SCULPT_tool_is_attribute_only(int tool
)
2023 return SCULPT_tool_is_paint(tool
) || SCULPT_tool_is_mask(tool
) ||
2024 ELEM(tool
, SCULPT_TOOL_DRAW_FACE_SETS
);
2027 void SCULPT_stroke_id_ensure(Object
*ob
);
2028 void SCULPT_stroke_id_next(Object
*ob
);
2030 namespace blender::ed::sculpt_paint
{
2031 void ensure_valid_pivot(const Object
*ob
, Scene
*scene
);
2034 /* -------------------------------------------------------------------- */
2035 /** \name Topology island API
2037 * Each mesh island shell gets its own integer
2038 * key; these are temporary and internally limited to 8 bits.
2039 * Uses the `ss->topology_island_key` attribute.
2042 /* Ensures vertex island keys exist and are valid. */
2043 void SCULPT_topology_islands_ensure(Object
*ob
);
2046 * Mark vertex island keys as invalid.
2047 * Call when adding or hiding geometry.
2049 void SCULPT_topology_islands_invalidate(SculptSession
*ss
);
2051 /** Get vertex island key. */
2052 int SCULPT_vertex_island_get(const SculptSession
*ss
, PBVHVertRef vertex
);
2056 namespace blender::ed::sculpt_paint
{
2057 float sculpt_calc_radius(ViewContext
*vc
,
2060 const float3 location
);
2063 inline void *SCULPT_vertex_attr_get(const PBVHVertRef vertex
, const SculptAttribute
*attr
)
2066 char *p
= (char *)attr
->data
;
2067 int idx
= (int)vertex
.i
;
2069 if (attr
->data_for_bmesh
) {
2070 BMElem
*v
= (BMElem
*)vertex
.i
;
2071 idx
= v
->head
.index
;
2074 return p
+ attr
->elem_size
* (int)idx
;
2077 BMElem
*v
= (BMElem
*)vertex
.i
;
2078 return BM_ELEM_CD_GET_VOID_P(v
, attr
->bmesh_cd_offset
);