Blender V5.3
sculpt_detail.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2020 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8#include "sculpt_dyntopo.hh"
9
10#include "MEM_guardedalloc.h"
11
12#include "BLI_index_mask.hh"
13#include "BLI_math_geom_c.hh"
14#include "BLI_math_matrix_c.hh"
16#include "BLI_math_vector.hh"
17#include "BLI_string_utf8.hh"
18#include "BLI_time.hh"
19
20#include "BLT_translation.hh"
21
22#include "DNA_brush_types.h"
23#include "DNA_mesh_types.h"
24
25#include "BKE_attribute.hh"
26#include "BKE_brush.hh"
27#include "BKE_context.hh"
28#include "BKE_layer.hh"
29#include "BKE_object_types.hh"
30#include "BKE_paint.hh"
31#include "BKE_paint_bvh.hh"
32#include "BKE_screen.hh"
33
34#include "GPU_immediate.hh"
35#include "GPU_immediate_util.hh"
36#include "GPU_matrix.hh"
37#include "GPU_state.hh"
38
39#include "WM_api.hh"
40#include "WM_types.hh"
41
42#include "ED_screen.hh"
43#include "ED_space_api.hh"
44#include "ED_view3d.hh"
45
46#include "DEG_depsgraph.hh"
47
48#include "sculpt_intern.hh"
49#include "sculpt_undo.hh"
50
51#include "RNA_access.hh"
52#include "RNA_define.hh"
53#include "RNA_prototypes.hh"
54
55#include "UI_interface.hh"
56
57#include "CLG_log.h"
58
59#include <cstdlib>
60
61#include "bmesh.hh"
62
63namespace blender {
64
66
67static CLG_LogRef LOG = {"sculpt.detail"};
68
69/* -------------------------------------------------------------------- */
72
81
90
97
99
100/* -------------------------------------------------------------------- */
103
105{
106 const Scene &scene = *CTX_data_scene(C);
107 const Depsgraph &depsgraph = *CTX_data_depsgraph_pointer(C);
111
112 const View3D *v3d = CTX_wm_view3d(C);
113 const Base *base = CTX_data_active_base(C);
114 if (!BKE_base_is_visible(v3d, base)) {
115 return OPERATOR_CANCELLED;
116 }
117
120
121 IndexMaskMemory memory;
122 const IndexMask node_mask = bke::pbvh::all_leaf_nodes(pbvh, memory);
123
124 if (nodes.is_empty()) {
125 return OPERATOR_CANCELLED;
126 }
127
128 node_mask.foreach_index([&](const int i) { BKE_pbvh_node_mark_topology_update(nodes[i]); });
129
130 /* Get the bounding box, its center and size. */
132 const float3 center = math::midpoint(bounds.min, bounds.max);
133 const float3 dim = bounds.max - bounds.min;
134 const float size = math::reduce_max(dim);
135
136 /* Update topology size. */
137 const float max_edge_len = 1.0f /
138 (sd->constant_detail * mat4_to_scale(ob.object_to_world().ptr()));
139 const float min_edge_len = max_edge_len * detail_size::EDGE_LENGTH_MIN_FACTOR;
140
141 undo::push_begin(scene, ob, op);
142 undo::push_node(depsgraph, ob, nullptr, undo::Type::Position);
143
144 const double start_time = BLI_time_now_seconds();
145
147 pbvh,
148 *ss.bm_log,
150 min_edge_len,
151 max_edge_len,
152 center,
153 std::nullopt,
154 size,
155 false,
156 false))
157 {
158 node_mask.foreach_index([&](const int i) { BKE_pbvh_node_mark_topology_update(nodes[i]); });
159 }
160
161 CLOG_DEBUG(&LOG, "Detail flood fill took %f seconds.", BLI_time_now_seconds() - start_time);
162
163 undo::push_end(ob);
164
165 /* Force rebuild of bke::pbvh::Tree for better BB placement. */
168
169 /* Redraw. */
171
172 return OPERATOR_FINISHED;
173}
174
176{
177 /* Identifiers. */
178 ot->name = "Detail Flood Fill";
179 ot->idname = "SCULPT_OT_detail_flood_fill";
180 ot->description = "Flood fill the mesh with the selected detail setting";
181
182 /* API callbacks. */
185
187}
188
190
191/* -------------------------------------------------------------------- */
194
197 Voxel = 1,
198};
199
201 {int(SampleDetailModeType::Dyntopo), "DYNTOPO", 0, "Dyntopo", "Sample dyntopo detail"},
202 {int(SampleDetailModeType::Voxel), "VOXEL", 0, "Voxel", "Sample mesh voxel size"},
203 {0, nullptr, 0, nullptr, nullptr},
204};
205
206static bool sample_detail_voxel(bContext *C, ViewContext *vc, const int mval[2])
207{
208 Depsgraph *depsgraph = CTX_data_depsgraph_pointer(C);
209 Object &ob = *vc->obact;
211 Mesh &mesh = *id_cast<Mesh *>(ob.data);
212 const Span<float3> positions = bke::pbvh::vert_positions_eval(*depsgraph, ob);
213 const OffsetIndices faces = mesh.faces();
214 const Span<int> corner_verts = mesh.corner_verts();
215 const GroupedSpan<int> vert_to_face_map = mesh.vert_to_face_map();
216 const bke::AttributeAccessor attributes = mesh.attributes();
217 const VArraySpan hide_poly = *attributes.lookup<bool>(".hide_poly", bke::AttrDomain::Face);
218
219 /* Update the active vertex. */
220 const float mval_fl[2] = {float(mval[0]), float(mval[1])};
221 if (!cursor_geometry_info_update(C, mval_fl, false)) {
222 return false;
223 }
224 BKE_sculptsession_update_for_edit(depsgraph, &ob, false);
225
226 /* Average the edge length of the connected edges to the active vertex. */
227 const int active_vert = std::get<int>(ss.active_vert());
228 const float3 active_vert_position = positions[active_vert];
229 float edge_length = 0.0f;
230 Vector<int> neighbors;
231 for (const int neighbor : vert_neighbors_get_mesh(
232 faces, corner_verts, vert_to_face_map, hide_poly, active_vert, neighbors))
233 {
234 edge_length += math::distance(active_vert_position, positions[neighbor]);
235 }
236 mesh.remesh_voxel_size = edge_length / float(neighbors.size());
237 return true;
238}
239
242 float *distance)
243{
245 node, srd.ray_start, &srd.isect_precalc, &srd.depth, &srd.edge_length))
246 {
247 srd.hit = true;
248 *distance = srd.depth;
249 }
250}
251
252static void sample_detail_dyntopo(bContext *C, ViewContext *vc, const int mval[2])
253{
255 Object &ob = *vc->obact;
257
259
260 const float2 mval_fl = {float(mval[0]), float(mval[1])};
261 float3 ray_start;
262 float3 ray_end;
263 float3 ray_normal;
264 float depth = raycast_init(vc, mval_fl, ray_start, ray_end, ray_normal, false);
265
267 srd.hit = false;
268 srd.ray_start = ray_start;
269 srd.depth = depth;
270 srd.edge_length = 0.0f;
272
274
276 pbvh,
277 [&](bke::pbvh::Node &node, float *distance) {
279 },
280 ray_start,
281 ray_normal,
282 false);
283
284 if (srd.hit && srd.edge_length > 0.0f) {
285 /* Convert edge length to world space detail resolution. */
286 sd.constant_detail = 1 / (srd.edge_length * mat4_to_scale(ob.object_to_world().ptr()));
287 }
288}
289
291 const int event_xy[2],
292 const SampleDetailModeType mode)
293{
294 /* Find 3D view to pick from. */
295 bScreen *screen = CTX_wm_screen(C);
296 ScrArea *area = BKE_screen_find_area_xy(screen, SPACE_VIEW3D, event_xy);
297 ARegion *region = (area) ? BKE_area_find_region_xy(area, RGN_TYPE_WINDOW, event_xy) : nullptr;
298 if (region == nullptr) {
299 return OPERATOR_CANCELLED;
300 }
301
302 /* Set context to 3D view. */
303 ScrArea *prev_area = CTX_wm_area(C);
304 ARegion *prev_region = CTX_wm_region(C);
305 CTX_wm_area_set(C, area);
306 CTX_wm_region_set(C, region);
307
308 Depsgraph *depsgraph = CTX_data_ensure_evaluated_depsgraph(C);
310
311 Object *ob = vc.obact;
312 if (ob == nullptr) {
313 return OPERATOR_CANCELLED;
314 }
315
317 if (!pbvh) {
318 return OPERATOR_CANCELLED;
319 }
320
321 const View3D *v3d = CTX_wm_view3d(C);
322 const Base *base = CTX_data_active_base(C);
323 if (!BKE_base_is_visible(v3d, base)) {
324 return OPERATOR_CANCELLED;
325 }
326
327 const int mval[2] = {
328 event_xy[0] - region->winrct.xmin,
329 event_xy[1] - region->winrct.ymin,
330 };
331
332 /* Pick sample detail. */
333 switch (mode) {
335 if (pbvh->type() != bke::pbvh::Type::BMesh) {
336 CTX_wm_area_set(C, prev_area);
337 CTX_wm_region_set(C, prev_region);
338 return OPERATOR_CANCELLED;
339 }
340 sample_detail_dyntopo(C, &vc, mval);
341 break;
343 if (pbvh->type() != bke::pbvh::Type::Mesh) {
344 CTX_wm_area_set(C, prev_area);
345 CTX_wm_region_set(C, prev_region);
346 return OPERATOR_CANCELLED;
347 }
348 if (!sample_detail_voxel(C, &vc, mval)) {
349 return OPERATOR_CANCELLED;
350 }
351 break;
352 }
353
354 /* Restore context. */
355 CTX_wm_area_set(C, prev_area);
356 CTX_wm_region_set(C, prev_region);
357
358 return OPERATOR_FINISHED;
359}
360
362{
363 int ss_co[2];
364 RNA_int_get_array(op->ptr, "location", ss_co);
366 return sample_detail(C, ss_co, mode);
367}
368
370 wmOperator *op,
371 const wmEvent * /*event*/)
372{
373 ED_workspace_status_text(C, IFACE_("Click on the mesh to set the detail"));
377}
378
380 wmOperator *op,
381 const wmEvent *event)
382{
383 switch (event->type) {
384 case LEFTMOUSE:
385 if (event->val == KM_PRESS) {
387 sample_detail(C, event->xy, mode);
388
389 RNA_int_set_array(op->ptr, "location", event->xy);
391 ED_workspace_status_text(C, nullptr);
393
394 return OPERATOR_FINISHED;
395 }
396 break;
397 case EVT_ESCKEY:
398 case RIGHTMOUSE: {
400 ED_workspace_status_text(C, nullptr);
401
402 return OPERATOR_CANCELLED;
403 }
404 default: {
405 break;
406 }
407 }
408
410}
411
413{
414 /* Identifiers. */
415 ot->name = "Sample Detail Size";
416 ot->idname = "SCULPT_OT_sample_detail_size";
417 ot->description = "Sample the mesh detail on clicked point";
418
419 /* API callbacks. */
424
426
427 PropertyRNA *prop;
428 prop = RNA_def_int_array(ot->srna,
429 "location",
430 2,
431 nullptr,
432 0,
433 SHRT_MAX,
434 "Location",
435 "Screen coordinates of sampling",
436 0,
437 SHRT_MAX);
439 prop = RNA_def_enum(ot->srna,
440 "mode",
443 "Detail Mode",
444 "Target sculpting workflow that is going to use the sampled size");
446}
447
449
450/* -------------------------------------------------------------------- */
453
454/* Defines how much the mouse movement will modify the detail size value. */
455#define DETAIL_SIZE_DELTA_SPEED 0.08f
456#define DETAIL_SIZE_DELTA_ACCURATE_SPEED 0.004f
457
463
467
469
470 float init_mval[2];
472
473 float outline_col[4];
474
477
478 /* The values stored here vary based on the detailing mode. */
482
483 float radius;
484
487
490
491 float preview_tri[3][3];
492 float gizmo_mat[4][4];
493};
494
497 const float start_co[3],
498 const float end_co[3],
499 bool flip,
500 const float angle)
501{
502 float object_space_constant_detail;
503 if (cd->mode == DETAILING_MODE_RESOLUTION) {
504 object_space_constant_detail = detail_size::constant_to_detail_size(cd->current_value,
505 *cd->active_object);
506 }
507 else if (cd->mode == DETAILING_MODE_BRUSH_PERCENT) {
508 object_space_constant_detail = detail_size::brush_to_detail_size(cd->current_value,
509 cd->brush_radius);
510 }
511 else {
512 object_space_constant_detail = detail_size::relative_to_detail_size(
513 cd->current_value, cd->brush_radius, cd->pixel_radius, U.pixelsize);
514 }
515
516 /* The constant detail represents the maximum edge length allowed before subdividing it. If the
517 * triangle grid preview is created with this value it will represent an ideal mesh density where
518 * all edges have the exact maximum length, which never happens in practice. As the minimum edge
519 * length for dyntopo is 0.4 * max_edge_length, this adjust the detail size to the average
520 * between max and min edge length so the preview is more accurate. */
521 object_space_constant_detail *= 0.7f;
522
523 const float total_len = len_v3v3(cd->preview_tri[0], cd->preview_tri[1]);
524 const int tot_lines = int(total_len / object_space_constant_detail) + 1;
525 const float tot_lines_fl = total_len / object_space_constant_detail;
526 float spacing_disp[3];
527 sub_v3_v3v3(spacing_disp, end_co, start_co);
528 normalize_v3(spacing_disp);
529
530 float line_disp[3];
531 zero_v3(line_disp);
532 rotate_v2_v2fl(line_disp, spacing_disp, DEG2RAD(angle));
533 mul_v3_fl(spacing_disp, total_len / tot_lines_fl);
534
535 immBegin(GPU_PRIM_LINES, uint(tot_lines) * 2);
536 for (int i = 0; i < tot_lines; i++) {
537 float line_length;
538 if (flip) {
539 line_length = total_len * (float(i) / float(tot_lines_fl));
540 }
541 else {
542 line_length = total_len * (1.0f - (float(i) / float(tot_lines_fl)));
543 }
544 float line_start[3];
545 copy_v3_v3(line_start, start_co);
546 madd_v3_v3v3fl(line_start, line_start, spacing_disp, i);
547 float line_end[3];
548 madd_v3_v3v3fl(line_end, line_start, line_disp, line_length);
549 immVertex3fv(pos3d, line_start);
550 immVertex3fv(pos3d, line_end);
551 }
552 immEnd();
553}
554
555static void dyntopo_detail_size_edit_draw(const bContext * /*C*/, ARegion * /*region*/, void *arg)
556{
559 GPU_line_smooth(true);
560
562 immVertexFormat(), "pos", gpu::VertAttrType::SFLOAT_32_32_32);
566
567 /* Draw Cursor */
569 GPU_line_width(3.0f);
570
571 imm_draw_circle_wire_3d(pos3d, 0, 0, cd->radius, 80);
572
573 /* Draw Triangle. */
574 immUniformColor4f(0.9f, 0.9f, 0.9f, 0.8f);
576 immVertex3fv(pos3d, cd->preview_tri[0]);
577 immVertex3fv(pos3d, cd->preview_tri[1]);
578
579 immVertex3fv(pos3d, cd->preview_tri[1]);
580 immVertex3fv(pos3d, cd->preview_tri[2]);
581
582 immVertex3fv(pos3d, cd->preview_tri[2]);
583 immVertex3fv(pos3d, cd->preview_tri[0]);
584 immEnd();
585
586 /* Draw Grid */
587 GPU_line_width(1.0f);
589 pos3d, cd, cd->preview_tri[0], cd->preview_tri[1], false, 60.0f);
591 pos3d, cd, cd->preview_tri[0], cd->preview_tri[1], true, 120.0f);
593 pos3d, cd, cd->preview_tri[0], cd->preview_tri[2], false, -60.0f);
594
598 GPU_line_smooth(false);
599}
600
602{
603 Object *active_object = CTX_data_active_object(C);
604 SculptSession &ss = *active_object->runtime->sculpt_session;
605 ARegion *region = CTX_wm_region(C);
607 op->customdata);
609 ss.draw_faded_cursor = false;
610 MEM_delete(cd);
611 op->customdata = nullptr;
612 ED_workspace_status_text(C, nullptr);
613
614 ScrArea *area = CTX_wm_area(C);
615 ED_area_status_text(area, nullptr);
616}
617
619{
620 /* TODO: Get range from RNA for these values? */
621 if (cd->mode == DETAILING_MODE_RESOLUTION) {
622 cd->min_value = 1.0f;
623 cd->max_value = 500.0f;
624 }
625 else if (cd->mode == DETAILING_MODE_BRUSH_PERCENT) {
626 cd->min_value = 0.5f;
627 cd->max_value = 100.0f;
628 }
629 else {
630 cd->min_value = 0.5f;
631 cd->max_value = 40.0f;
632 }
633}
634
637{
639 const ActiveVert active_vert = ss.active_vert();
640 if (std::holds_alternative<std::monostate>(active_vert)) {
641 return;
642 }
643 BMVert *active_vertex = std::get<BMVert *>(active_vert);
644
645 float len_accum = 0;
646 BMeshNeighborVerts neighbors;
647 for (BMVert *neighbor : vert_neighbors_get_bmesh(*active_vertex, neighbors)) {
648 len_accum += len_v3v3(active_vertex->co, neighbor->co);
649 }
650 const int num_neighbors = neighbors.size();
651
652 if (num_neighbors > 0) {
653 const float avg_edge_len = len_accum / num_neighbors;
654 /* Use 0.7 as the average of min and max dyntopo edge length. */
655 const float detail_size = 0.7f / (avg_edge_len *
656 mat4_to_scale(cd->active_object->object_to_world().ptr()));
657 float sampled_value;
658 if (cd->mode == DETAILING_MODE_RESOLUTION) {
659 sampled_value = detail_size;
660 }
661 else if (cd->mode == DETAILING_MODE_BRUSH_PERCENT) {
664 }
665 else {
667 detail_size, cd->brush_radius, cd->pixel_radius, U.pixelsize, *cd->active_object);
668 }
669 cd->current_value = clamp_f(sampled_value, cd->min_value, cd->max_value);
670 }
671}
672
674 const wmEvent *event)
675{
676 const float mval[2] = {float(event->mval[0]), float(event->mval[1])};
677
678 float detail_size_delta;
679 float invert = cd->mode == DETAILING_MODE_RESOLUTION ? 1.0f : -1.0f;
680 if (cd->accurate_mode) {
681 detail_size_delta = mval[0] - cd->accurate_mval[0];
682 cd->current_value = cd->accurate_value +
683 detail_size_delta * DETAIL_SIZE_DELTA_ACCURATE_SPEED * invert;
684 }
685 else {
686 detail_size_delta = mval[0] - cd->init_mval[0];
687 cd->current_value = cd->init_value + detail_size_delta * DETAIL_SIZE_DELTA_SPEED * invert;
688 }
689
690 if (event->type == EVT_LEFTSHIFTKEY && event->val == KM_PRESS) {
691 cd->accurate_mode = true;
692 copy_v2_v2(cd->accurate_mval, mval);
694 }
695 if (event->type == EVT_LEFTSHIFTKEY && event->val == KM_RELEASE) {
696 cd->accurate_mode = false;
697 cd->accurate_value = 0.0f;
698 }
699
701}
702
705{
706 Scene *scene = CTX_data_scene(C);
707
708 Sculpt *sd = scene->toolsettings->sculpt;
709 PointerRNA sculpt_ptr = RNA_pointer_create_discrete(&scene->id, RNA_Sculpt, sd);
710
711 char msg[UI_MAX_DRAW_STR];
712 const char *format_string;
713 const char *property_name;
714 if (cd->mode == DETAILING_MODE_RESOLUTION) {
715 property_name = "constant_detail_resolution";
716 format_string = "%s: %0.4f";
717 }
718 else if (cd->mode == DETAILING_MODE_BRUSH_PERCENT) {
719 property_name = "detail_percent";
720 format_string = "%s: %3.1f%%";
721 }
722 else {
723 property_name = "detail_size";
724 format_string = "%s: %0.4f";
725 }
726 const PropertyRNA *prop = RNA_struct_find_property(&sculpt_ptr, property_name);
727 const char *ui_name = RNA_property_ui_name(prop);
728 SNPRINTF_UTF8(msg, format_string, ui_name, cd->current_value);
729 ScrArea *area = CTX_wm_area(C);
730 ED_area_status_text(area, msg);
731
732 WorkspaceStatus status(C);
733 status.item(IFACE_("Confirm"), ICON_EVENT_RETURN, ICON_MOUSE_LMB);
734 status.item(IFACE_("Cancel"), ICON_EVENT_ESC, ICON_MOUSE_RMB);
735 status.item(IFACE_("Change Size"), ICON_MOUSE_MOVE);
736 status.item_bool(IFACE_("Sample Mode"), cd->sample_mode, ICON_EVENT_CTRL);
737 status.item_bool(IFACE_("Precision Mode"), cd->accurate_mode, ICON_EVENT_SHIFT);
738}
739
741 wmOperator *op,
742 const wmEvent *event)
743{
744 Object &active_object = *CTX_data_active_object(C);
745 SculptSession &ss = *active_object.runtime->sculpt_session;
746 ARegion *region = CTX_wm_region(C);
748 op->customdata);
750
751 /* Cancel modal operator */
752 if ((event->type == EVT_ESCKEY && event->val == KM_PRESS) ||
753 (event->type == RIGHTMOUSE && event->val == KM_PRESS))
754 {
756 ED_region_tag_redraw(region);
757 return OPERATOR_FINISHED;
758 }
759
760 /* Finish modal operator */
761 if ((event->type == LEFTMOUSE && event->val == KM_RELEASE) ||
762 (event->type == EVT_RETKEY && event->val == KM_PRESS) ||
763 (event->type == EVT_PADENTER && event->val == KM_PRESS))
764 {
766 if (cd->mode == DETAILING_MODE_RESOLUTION) {
768 }
769 else if (cd->mode == DETAILING_MODE_BRUSH_PERCENT) {
771 }
772 else {
773 sd->detail_size = cd->current_value;
774 }
775
776 ss.draw_faded_cursor = false;
777 MEM_delete(cd);
778 ED_region_tag_redraw(region);
779 ED_workspace_status_text(C, nullptr);
780
781 ScrArea *area = CTX_wm_area(C);
782 ED_area_status_text(area, nullptr);
783 return OPERATOR_FINISHED;
784 }
785
786 ED_region_tag_redraw(region);
787
789 if (event->val == KM_PRESS) {
790 cd->sample_mode = true;
791 }
792 else if (event->val == KM_RELEASE) {
793 cd->sample_mode = false;
794 }
795 }
796
797 /* Sample mode sets the detail size sampling the average edge length under the surface. */
798 if (cd->sample_mode) {
802 }
803 /* Regular mode, changes the detail size by moving the cursor. */
806
808}
809
811{
812 if (mode == DETAILING_MODE_RESOLUTION) {
813 return sd->constant_detail;
814 }
815 if (mode == DETAILING_MODE_BRUSH_PERCENT) {
816 return sd->detail_percent;
817 }
818 return sd->detail_size;
819}
820
822 wmOperator *op,
823 const wmEvent *event)
824{
825 const ToolSettings *tool_settings = CTX_data_tool_settings(C);
826 Sculpt *sd = tool_settings->sculpt;
827
828 ARegion *region = CTX_wm_region(C);
829 Object &active_object = *CTX_data_active_object(C);
831
833
834 /* Initial operator Custom Data setup. */
837 cd->active_object = &active_object;
838 cd->init_mval[0] = event->mval[0];
839 cd->init_mval[1] = event->mval[1];
842 }
843 else if (sd->flags & SCULPT_DYNTOPO_DETAIL_BRUSH) {
845 }
846 else {
848 }
849
850 const float initial_detail_size = dyntopo_detail_size_initial_value(sd, cd->mode);
851 cd->current_value = initial_detail_size;
852 cd->init_value = initial_detail_size;
853 copy_v4_v4(cd->outline_col, brush->add_col);
854 op->customdata = cd;
855
856 SculptSession &ss = *active_object.runtime->sculpt_session;
858 cd->radius = ss.cursor_radius;
859
860 Depsgraph *depsgraph = CTX_data_ensure_evaluated_depsgraph(C);
862
865
866 /* Generates the matrix to position the gizmo in the surface of the mesh using the same
867 * location and orientation as the brush cursor. */
868 float cursor_trans[4][4], cursor_rot[4][4];
869 const float z_axis[4] = {0.0f, 0.0f, 1.0f, 0.0f};
870 float quat[4];
871 copy_m4_m4(cursor_trans, active_object.object_to_world().ptr());
872 translate_m4(cursor_trans, ss.cursor_location[0], ss.cursor_location[1], ss.cursor_location[2]);
873
874 float cursor_normal[3];
875 if (ss.cursor_sampled_normal) {
876 copy_v3_v3(cursor_normal, *ss.cursor_sampled_normal);
877 }
878 else {
879 copy_v3_v3(cursor_normal, ss.cursor_normal);
880 }
881
882 rotation_between_vecs_to_quat(quat, z_axis, cursor_normal);
883 quat_to_mat4(cursor_rot, quat);
884 copy_m4_m4(cd->gizmo_mat, cursor_trans);
885 mul_m4_m4_post(cd->gizmo_mat, cursor_rot);
886
887 /* Initialize the position of the triangle vertices. */
888 const float y_axis[3] = {0.0f, cd->radius, 0.0f};
889 for (int i = 0; i < 3; i++) {
890 zero_v3(cd->preview_tri[i]);
891 rotate_v2_v2fl(cd->preview_tri[i], y_axis, DEG2RAD(120.0f * i));
892 }
893
894 vert_random_access_ensure(active_object);
895
897 ED_region_tag_redraw(region);
898
899 ss.draw_faded_cursor = true;
900
901 const char *status_str = IFACE_(
902 "Move the mouse to change the dyntopo detail size. LMB: confirm size, ESC/RMB: cancel, "
903 "SHIFT: precision mode, CTRL: sample detail size");
904
905 ED_workspace_status_text(C, status_str);
907
909}
910
912{
913 /* identifiers */
914 ot->name = "Edit Dyntopo Detail Size";
915 ot->description = "Modify the detail size of dyntopo interactively";
916 ot->idname = "SCULPT_OT_dyntopo_detail_size_edit";
917
918 /* API callbacks. */
923
925}
926
927} // namespace ed::sculpt_paint::dyntopo
928
930
931float constant_to_detail_size(const float constant_detail, const Object &ob)
932{
933 return 1.0f / (constant_detail * mat4_to_scale(ob.object_to_world().ptr()));
934}
935
936float brush_to_detail_size(const float brush_percent, const float brush_radius)
937{
938 return brush_radius * brush_percent / 100.0f;
939}
940
941float relative_to_detail_size(const float relative_detail,
942 const float brush_radius,
943 const float pixel_radius,
944 const float pixel_size)
945{
946 return (brush_radius / pixel_radius) * (relative_detail * pixel_size) / RELATIVE_SCALE_FACTOR;
947}
948
949float constant_to_brush_detail(const float constant_detail,
950 const float brush_radius,
951 const Object &ob)
952{
953 const float object_scale = mat4_to_scale(ob.object_to_world().ptr());
954
955 return 100.0f / (constant_detail * brush_radius * object_scale);
956}
957
958float constant_to_relative_detail(const float constant_detail,
959 const float brush_radius,
960 const float pixel_radius,
961 const float pixel_size,
962 const Object &ob)
963{
964 const float object_scale = mat4_to_scale(ob.object_to_world().ptr());
965
966 return (pixel_radius / brush_radius) * (RELATIVE_SCALE_FACTOR / pixel_size) *
967 (1.0f / (constant_detail * object_scale));
968}
969
970} // namespace ed::sculpt_paint::dyntopo::detail_size
971
973
974} // namespace blender
A BVH for high poly meshes.
#define DEG2RAD(_deg)
#define SNPRINTF_UTF8(dst, format,...)
Platform independent time functions.
#define IFACE_(msgid)
#define CLOG_DEBUG(clg_ref,...)
Definition CLG_log.h:191
#define REGION_DRAW_POST_VIEW
Read Blender memory allocation and freeing.
#define C
Definition RandGen.cpp:29
#define UI_MAX_DRAW_STR
#define ND_DRAW
Definition WM_types.hh:502
#define NC_SCENE
Definition WM_types.hh:391
#define ND_TOOLSETTINGS
Definition WM_types.hh:468
#define NC_OBJECT
Definition WM_types.hh:392
#define ELEM(...)
SIMD_FORCE_INLINE btScalar angle(const btVector3 &v) const
Return the angle between this and another vector.
Definition btVector3.h:356
int64_t size() const
void item(std::string text, int icon1, int icon2=0)
Definition area.cc:1076
void item_bool(std::string text, bool inverted, int icon1, int icon2=0)
Definition area.cc:1097
GAttributeReader lookup(const StringRef name) const
void foreach_index(Fn &&fn, Mode mode=exec_mode::serial) const
#define SHRT_MAX
float distance(float, float) RET
float2x2 invert(float2x2 mat)
pbvh::Tree * pbvh_get(Object &object)
void raycast(Tree &pbvh, FunctionRef< void(Node &node, float *tmin)> hit_fn, const float3 &ray_start, const float3 &ray_normal, bool original)
IndexMask all_leaf_nodes(const Tree &pbvh, IndexMaskMemory &memory)
Definition pbvh.cc:2522
Bounds< float3 > bounds_get(const Tree &pbvh)
Definition pbvh.cc:1590
bool bmesh_update_topology(BMesh &bm, Tree &pbvh, BMLog &bm_log, PBVHTopologyUpdateMode mode, float min_edge_len, float max_edge_len, const float3 &center, const std::optional< float3 > &view_normal, float radius, bool use_frontface, bool use_projected)
bool raycast_node_detail_bmesh(const BMeshNode &node, const float3 &ray_start, const IsectRayPrecalc *isect_precalc, float *depth, float *r_edge_length)
Span< float3 > vert_positions_eval(const Depsgraph &depsgraph, const Object &object_orig)
Definition pbvh.cc:933
float relative_to_detail_size(const float relative_detail, const float brush_radius, const float pixel_radius, const float pixel_size)
float constant_to_detail_size(const float constant_detail, const Object &ob)
float constant_to_relative_detail(const float constant_detail, const float brush_radius, const float pixel_radius, const float pixel_size, const Object &ob)
float constant_to_brush_detail(const float constant_detail, const float brush_radius, const Object &ob)
float brush_to_detail_size(const float brush_percent, const float brush_radius)
void SCULPT_OT_detail_flood_fill(wmOperatorType *ot)
static EnumPropertyItem prop_sculpt_sample_detail_mode_types[]
static void dyntopo_detail_size_edit_cancel(bContext *C, wmOperator *op)
static float dyntopo_detail_size_initial_value(const Sculpt *sd, const eDyntopoDetailingMode mode)
static wmOperatorStatus sculpt_sample_detail_size_exec(bContext *C, wmOperator *op)
static void dyntopo_detail_size_update_header(bContext *C, const DyntopoDetailSizeEditCustomData *cd)
static wmOperatorStatus dyntopo_detail_size_edit_modal(bContext *C, wmOperator *op, const wmEvent *event)
void SCULPT_OT_sample_detail_size(wmOperatorType *ot)
static void sample_detail_dyntopo(bContext *C, ViewContext *vc, const int mval[2])
static void dyntopo_detail_size_parallel_lines_draw(uint pos3d, DyntopoDetailSizeEditCustomData *cd, const float start_co[3], const float end_co[3], bool flip, const float angle)
static wmOperatorStatus dyntopo_detail_size_edit_invoke(bContext *C, wmOperator *op, const wmEvent *event)
static void dyntopo_detail_size_update_from_mouse_delta(DyntopoDetailSizeEditCustomData *cd, const wmEvent *event)
static void dyntopo_detail_size_sample_from_surface(Object &ob, DyntopoDetailSizeEditCustomData *cd)
static void dyntopo_detail_size_edit_draw(const bContext *, ARegion *, void *arg)
static bool sample_detail_voxel(bContext *C, ViewContext *vc, const int mval[2])
void SCULPT_OT_dyntopo_detail_size_edit(wmOperatorType *ot)
static wmOperatorStatus sculpt_sample_detail_size_modal(bContext *C, wmOperator *op, const wmEvent *event)
static void dyntopo_detail_size_bounds(DyntopoDetailSizeEditCustomData *cd)
static wmOperatorStatus sample_detail(bContext *C, const int event_xy[2], const SampleDetailModeType mode)
static void sculpt_raycast_detail_cb(bke::pbvh::BMeshNode &node, SculptDetailRaycastData &srd, float *distance)
static wmOperatorStatus sculpt_sample_detail_size_invoke(bContext *C, wmOperator *op, const wmEvent *)
static bool sculpt_and_dynamic_topology_poll(bContext *C)
static bool sculpt_and_constant_or_manual_detail_poll(bContext *C)
static wmOperatorStatus sculpt_detail_flood_fill_exec(bContext *C, wmOperator *op)
void push_node(const Depsgraph &depsgraph, const Object &object, const bke::pbvh::Node *node, const Type type)
void push_begin(const Scene &scene, Object &ob, const wmOperator *op)
void vert_random_access_ensure(Object &object)
Definition sculpt.cc:154
Vector< BMVert *, 64 > BMeshNeighborVerts
void stroke_modifiers_check(Depsgraph &depsgraph, Object &ob, const Brush *brush)
Definition sculpt.cc:4309
bool sculpt_mode_poll(bContext *C)
Definition sculpt.cc:3800
float raycast_init(ViewContext *vc, const float2 &mval, float3 &r_ray_start, float3 &r_ray_end, float3 &r_ray_normal, bool original)
Definition sculpt.cc:4471
Span< BMVert * > vert_neighbors_get_bmesh(BMVert &vert, BMeshNeighborVerts &r_neighbors)
Definition sculpt.cc:490
float object_space_radius_get(const ViewContext &vc, const Paint &paint, const Brush &brush, const float3 &location, const float scale_factor)
Definition sculpt.cc:121
std::optional< CursorGeometryInfo > cursor_geometry_info_update(bContext *C, const float2 &mval, const bool use_sampled_normal)
Definition sculpt.cc:4584
Span< int > vert_neighbors_get_mesh(const OffsetIndices< int > faces, const Span< int > corner_verts, const GroupedSpan< int > vert_to_face, const Span< bool > hide_poly, const int vert, Vector< int > &r_neighbors)
Definition sculpt.cc:534
T reduce_max(const VecBase< T, Size > &a)
T distance(const T &a, const T &b)
T midpoint(const T &a, const T &b)
BMesh Iterators.
void WM_main_add_notifier(uint type, void *reference)
MINLINE void madd_v3_v3v3fl(float r[3], const float a[3], const float b[3], float f)
UserDef U
Definition blender.cc:58
ScrArea * CTX_wm_area(const bContext *C)
int RNA_enum_get(PointerRNA *ptr, const char *name)
void immEnd()
PropertyRNA * RNA_struct_find_property(PointerRNA *ptr, const char *identifier)
std::variant< std::monostate, int, BMVert * > ActiveVert
Definition BKE_paint.hh:357
ViewContext ED_view3d_viewcontext_init(bContext *C, Depsgraph *depsgraph)
MINLINE void copy_v3_v3(float r[3], const float a[3])
unsigned int uint
Definition BKE_text.h:55
bool BKE_base_is_visible(const View3D *v3d, const Base *base)
Depsgraph * CTX_data_depsgraph_pointer(const bContext *C)
float mat4_to_scale(const float mat[4][4])
MINLINE void copy_v2_v2(float r[2], const float a[2])
GPUVertFormat * immVertexFormat()
float BKE_brush_radius_get(const Paint *paint, const Brush *brush)
Definition brush.cc:1366
void WM_cursor_modal_set(wmWindow *win, int val)
void immBegin(GPUPrimType, uint vertex_len)
PropertyRNA * RNA_def_enum(StructOrFunctionRNA *cont_, const char *identifier, const EnumPropertyItem *items, const int default_value, const char *ui_name, const char *ui_description)
void GPU_line_smooth(bool enable)
Definition gpu_state.cc:80
wmWindow * CTX_wm_window(const bContext *C)
void rotation_between_vecs_to_quat(float q[4], const float v1[3], const float v2[3])
void translate_m4(float mat[4][4], float Tx, float Ty, float Tz)
void immUniformColor4fv(const float rgba[4])
void ED_region_tag_redraw(ARegion *region)
Definition area.cc:681
Base * CTX_data_active_base(const bContext *C)
void immUnbindProgram()
void BKE_sculptsession_free_pbvh(Object &object)
void GPU_matrix_mul(const float m[4][4])
MINLINE void mul_v3_fl(float r[3], float f)
void GPU_line_width(float width)
Definition gpu_state.cc:151
@ GPU_BLEND_NONE
Definition GPU_state.hh:88
@ GPU_BLEND_ALPHA
Definition GPU_state.hh:90
void immBindBuiltinProgram(GPUBuiltinShader shader_id)
void * ED_region_draw_cb_activate(ARegionType *art, void(*draw)(const bContext *, ARegion *, void *), void *customdata, int type)
void RNA_int_set_array(PointerRNA *ptr, const char *name, const int *values)
VecBase< float, 2 > float2
void * MEM_new_zeroed(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
bool ED_region_draw_cb_exit(ARegionType *art, void *handle)
void rotate_v2_v2fl(float r[2], const float p[2], float angle)
void DEG_id_tag_update(ID *id, unsigned int flags)
void RNA_int_get_array(PointerRNA *ptr, const char *name, int *values)
Depsgraph * CTX_data_ensure_evaluated_depsgraph(const bContext *C, bool rna_write_check=false)
void WM_cursor_modal_restore(wmWindow *win)
void immUniformColor4f(float r, float g, float b, float a)
@ SCULPT_DYNTOPO_DETAIL_CONSTANT
@ SCULPT_DYNTOPO_DETAIL_MANUAL
@ SCULPT_DYNTOPO_DETAIL_BRUSH
PointerRNA RNA_pointer_create_discrete(ID *id, StructRNA *type, void *data)
void isect_ray_tri_watertight_v3_precalc(struct IsectRayPrecalc *isect_precalc, const float ray_direction[3])
void ED_area_status_text(ScrArea *area, const char *str)
Definition area.cc:975
ToolSettings * CTX_data_tool_settings(const bContext *C)
MINLINE void zero_v3(float r[3])
ScrArea ScrArea * BKE_screen_find_area_xy(const bScreen *screen, int spacetype, const int xy[2]) ATTR_NONNULL(1
Definition screen.cc:1226
uint GPU_vertformat_attr_add(GPUVertFormat *format, StringRef name, gpu::VertAttrType type)
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
const Brush * BKE_paint_brush_for_read(const Paint *paint)
MINLINE float normalize_v3(float n[3])
wmEventHandler_Op * WM_event_add_modal_handler(bContext *C, wmOperator *op)
void immVertex3fv(uint attr_id, const float data[3])
void RNA_def_property_flag(PropertyRNA *prop, PropertyFlag flag)
MINLINE void sub_v3_v3v3(float r[3], const float a[3], const float b[3])
@ WM_CURSOR_EYEDROPPER
Definition wm_cursors.hh:38
void mul_m4_m4_post(float R[4][4], const float B[4][4])
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:1074
@ PROP_SKIP_SAVE
Definition RNA_types.hh:414
@ PROP_HIDDEN
Definition RNA_types.hh:408
PropertyRNA * RNA_def_int_array(StructOrFunctionRNA *cont_, const char *identifier, const int len, const int *default_value, const int hardmin, const int hardmax, const char *ui_name, const char *ui_description, const int softmin, const int softmax)
void BKE_sculptsession_update_for_edit(Depsgraph *depsgraph, Object *ob_orig, bool is_paint_tool)
@ KM_RELEASE
Definition WM_types.hh:325
Brush * BKE_paint_brush(Paint *paint)
double BLI_time_now_seconds()
Definition time.cc:30
Scene * CTX_data_scene(const bContext *C)
bScreen * CTX_wm_screen(const bContext *C)
void CTX_wm_region_set(bContext *C, ARegion *region)
MINLINE void copy_v4_v4(float r[4], const float a[4])
void GPU_blend(GPUBlend blend)
Definition gpu_state.cc:44
@ OPTYPE_REGISTER
Definition WM_types.hh:188
@ OPTYPE_UNDO
Definition WM_types.hh:190
void copy_m4_m4(float m1[4][4], const float m2[4][4])
VecBase< float, 3 > float3
MINLINE float len_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
void GPU_matrix_pop()
void imm_draw_circle_wire_3d(uint pos, float x, float y, float radius, int nsegments)
void BKE_pbvh_node_mark_topology_update(bke::pbvh::Node &node)
Object * CTX_data_active_object(const bContext *C)
ARegion * CTX_wm_region(const bContext *C)
void ED_workspace_status_text(bContext *C, const char *str)
Definition area.cc:1153
View3D * CTX_wm_view3d(const bContext *C)
void GPU_matrix_push()
const char * RNA_property_ui_name(const PropertyRNA *prop, const PointerRNA *ptr)
void quat_to_mat4(float m[4][4], const float q[4])
MINLINE float clamp_f(float value, float min, float max)
void CTX_wm_area_set(bContext *C, ScrArea *area)
ARegion * BKE_area_find_region_xy(const ScrArea *area, int regiontype, const int xy[2]) ATTR_NONNULL(3)
Definition screen.cc:1053
#define DETAIL_SIZE_DELTA_ACCURATE_SPEED
#define DETAIL_SIZE_DELTA_SPEED
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
bke::ARegionRuntime * runtime
bke::ObjectRuntime * runtime
struct ToolSettings * toolsettings
std::optional< float3 > cursor_sampled_normal
Definition BKE_paint.hh:424
struct ARegionType * type
wmEventType type
Definition WM_types.hh:817
wmOperatorStatus(*) exec(bContext *C, wmOperator *op) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1125
const char * description
Definition WM_types.hh:1115
void(*) cancel(bContext *C, wmOperator *op)
Definition WM_types.hh:1149
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
wmOperatorStatus(*) modal(bContext *C, wmOperator *op, const wmEvent *event) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1157