Blender V5.3
view3d_cursor_snap.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
10
11#include "DNA_object_types.h"
12
13#include "BLI_listbase.hh"
14#include "BLI_math_geom_c.hh"
15#include "BLI_math_matrix_c.hh"
17
18#include "MEM_guardedalloc.h"
19
20#include "BKE_context.hh"
21#include "BKE_global.hh"
22#include "BKE_layer.hh"
23#include "BKE_main.hh"
24#include "BKE_object.hh"
25#include "BKE_scene.hh"
26#include "BKE_screen.hh"
27
28#include "GPU_immediate.hh"
29#include "GPU_matrix.hh"
30#include "GPU_state.hh"
31
32#include "ED_screen.hh"
33#include "ED_transform.hh"
35#include "ED_view3d.hh"
36
37#include "UI_resources.hh"
38
39#include "RNA_access.hh"
40
42
43#include "WM_api.hh"
44
45namespace blender {
46
47#define STATE_INTERN_GET(state) \
48\
49 (SnapStateIntern *)((char *)state - offsetof(SnapStateIntern, snap_state))
50
55
58 ListBaseT<SnapStateIntern> state_intern;
60
62 const Scene *scene;
64
66
67 /* Copy of the parameters of the last event state in order to detect updates. */
72
73#ifdef USE_SNAP_DETECT_FROM_KEYMAP_HACK
76#endif
77
79
81};
82
85 data.state_default.flag = V3D_SNAPCURSOR_SNAP_EDIT_GEOM_FINAL;
86 copy_v4_v4_uchar(data.state_default.target_color, uchar4{255, 255, 255, 255});
87 copy_v4_v4_uchar(data.state_default.source_color, uchar4{255, 255, 255, 128});
88 copy_v4_v4_uchar(data.state_default.color_box, uchar4{255, 255, 255, 128});
89 copy_v3_fl(data.state_default.box_dimensions, 1.0f);
90 data.state_default.draw_point = true;
91 return data;
92}();
93
98static const float eps_view_align = 1e-2f;
99
103static void v3d_cursor_poject_surface_normal(const float normal[3],
104 const float obmat[4][4],
105 float r_mat[3][3])
106{
107 float mat[3][3];
108 copy_m3_m4(mat, obmat);
109 normalize_m3(mat);
110
111 float dot_best = fabsf(dot_v3v3(mat[0], normal));
112 int i_best = 0;
113 for (int i = 1; i < 3; i++) {
114 float dot_test = fabsf(dot_v3v3(mat[i], normal));
115 if (dot_test > dot_best) {
116 i_best = i;
117 dot_best = dot_test;
118 }
119 }
120 if (dot_v3v3(mat[i_best], normal) < 0.0f) {
121 negate_v3(mat[(i_best + 1) % 3]);
122 negate_v3(mat[(i_best + 2) % 3]);
123 }
124 copy_v3_v3(mat[i_best], normal);
125 orthogonalize_m3(mat, i_best);
126 normalize_m3(mat);
127
128 copy_v3_v3(r_mat[0], mat[(i_best + 1) % 3]);
129 copy_v3_v3(r_mat[1], mat[(i_best + 2) % 3]);
130 copy_v3_v3(r_mat[2], mat[i_best]);
131}
132
136static bool mat3_align_axis_to_v3(float mat[3][3], const int axis_align, const float v[3])
137{
138 float dot_best = -1.0f;
139 int axis_found = axis_align;
140 for (int i = 0; i < 3; i++) {
141 const float dot_test = fabsf(dot_v3v3(mat[i], v));
142 if (dot_test > dot_best) {
143 dot_best = dot_test;
144 axis_found = i;
145 }
146 }
147
148 if (axis_align != axis_found) {
149 float tmat[3][3];
150 copy_m3_m3(tmat, mat);
151 const int offset = mod_i(axis_found - axis_align, 3);
152 for (int i = 0; i < 3; i++) {
153 copy_v3_v3(mat[i], tmat[(i + offset) % 3]);
154 }
155 return true;
156 }
157 return false;
158}
159
160/* -------------------------------------------------------------------- */
163
164static void v3d_cursor_plane_draw_grid(const int resolution,
165 const float scale,
166 const float scale_fade,
167 const float matrix[4][4],
168 const int plane_axis,
169 const float color[4])
170{
171 BLI_assert(scale_fade <= scale);
172 const int resolution_min = resolution - 1;
173 float color_fade[4] = {UNPACK4(color)};
174 const float *center = matrix[3];
175
177 GPU_line_smooth(true);
178 GPU_line_width(1.0f);
179
180 GPUVertFormat *format = immVertexFormat();
181 const uint pos_id = GPU_vertformat_attr_add(format, "pos", gpu::VertAttrType::SFLOAT_32_32_32);
182 const uint col_id = GPU_vertformat_attr_add(
183 format, "color", gpu::VertAttrType::SFLOAT_32_32_32_32);
184
186
187 const size_t coords_len = resolution * resolution;
188 float (*coords)[3] = MEM_new_array_uninitialized<float[3]>(coords_len, __func__);
189
190 const int axis_x = (plane_axis + 0) % 3;
191 const int axis_y = (plane_axis + 1) % 3;
192 const int axis_z = (plane_axis + 2) % 3;
193
194 int i;
195 const float resolution_div = 1.0f / float(resolution);
196 i = 0;
197 for (int x = 0; x < resolution; x++) {
198 const float x_fl = (x * resolution_div) - 0.5f;
199 for (int y = 0; y < resolution; y++) {
200 const float y_fl = (y * resolution_div) - 0.5f;
201 coords[i][axis_x] = 0.0f;
202 coords[i][axis_y] = x_fl * scale;
203 coords[i][axis_z] = y_fl * scale;
204 mul_m4_v3(matrix, coords[i]);
205 i += 1;
206 }
207 }
208 BLI_assert(i == int(coords_len));
209 immBeginAtMost(GPU_PRIM_LINES, coords_len * 4);
210 i = 0;
211 for (int x = 0; x < resolution_min; x++) {
212 for (int y = 0; y < resolution_min; y++) {
213
214/* Add #resolution_div to ensure we fade-out entirely. */
215#define FADE(v) \
216\
217 max_ff(0.0f, (1.0f - square_f(((len_v3v3(v, center) / scale_fade) + resolution_div) * 2.0f)))
218
219 const float *v0 = coords[(resolution * x) + y];
220 const float *v1 = coords[(resolution * (x + 1)) + y];
221 const float *v2 = coords[(resolution * x) + (y + 1)];
222
223 const float f0 = FADE(v0);
224 const float f1 = FADE(v1);
225 const float f2 = FADE(v2);
226
227 if (f0 > 0.0f || f1 > 0.0f) {
228 color_fade[3] = color[3] * f0;
229 immAttr4fv(col_id, color_fade);
230 immVertex3fv(pos_id, v0);
231 color_fade[3] = color[3] * f1;
232 immAttr4fv(col_id, color_fade);
233 immVertex3fv(pos_id, v1);
234 }
235 if (f0 > 0.0f || f2 > 0.0f) {
236 color_fade[3] = color[3] * f0;
237 immAttr4fv(col_id, color_fade);
238 immVertex3fv(pos_id, v0);
239
240 color_fade[3] = color[3] * f2;
241 immAttr4fv(col_id, color_fade);
242 immVertex3fv(pos_id, v2);
243 }
244
245#undef FADE
246
247 i++;
248 }
249 }
250
251 MEM_delete(coords);
252
253 immEnd();
254
256
257 GPU_line_smooth(false);
259}
260
261static void v3d_cursor_plane_draw(const RegionView3D *rv3d,
262 const int plane_axis,
263 const float matrix[4][4])
264{
265 /* Draw */
266 float pixel_size;
267
268 if (rv3d->is_persp) {
269 float center[3];
270 negate_v3_v3(center, rv3d->ofs);
271 pixel_size = ED_view3d_pixel_size(rv3d, center);
272 }
273 else {
274 pixel_size = ED_view3d_pixel_size(rv3d, matrix[3]);
275 }
276
277 if (pixel_size > FLT_EPSILON) {
278
279 /* Arbitrary, 1.0 is a little too strong though. */
280 float color_alpha = 0.75f;
281 if (rv3d->is_persp) {
282 /* Scale down the alpha when this is drawn very small,
283 * since the add shader causes the small size to show too dense & bright. */
284 const float relative_pixel_scale = pixel_size / ED_view3d_pixel_size(rv3d, matrix[3]);
285 if (relative_pixel_scale < 1.0f) {
286 color_alpha *= max_ff(square_f(relative_pixel_scale), 0.3f);
287 }
288 }
289
290 {
291 /* Extra adjustment when it's near view-aligned as it seems overly bright. */
292 float view_vector[3];
293 ED_view3d_global_to_vector(rv3d, matrix[3], view_vector);
294 float view_dot = fabsf(dot_v3v3(matrix[plane_axis], view_vector));
295 color_alpha *= max_ff(0.3f, 1.0f - square_f(square_f(1.0f - view_dot)));
296 }
297
298 const float scale_mod = U.gizmo_size * 2 * UI_SCALE_FAC / U.pixelsize;
299
300 float final_scale = (scale_mod * pixel_size);
301
302 const int lines_subdiv = 10;
303 int lines = lines_subdiv;
304
305 float final_scale_fade = final_scale;
306 final_scale = ceil_power_of_10(final_scale);
307
308 float fac = final_scale_fade / final_scale;
309
310 float color[4] = {1, 1, 1, color_alpha};
311 color[3] *= square_f(1.0f - fac);
312 if (color[3] > 0.0f) {
314 lines * lines_subdiv, final_scale, final_scale_fade, matrix, plane_axis, color);
315 }
316
317 color[3] = color_alpha;
318 /* When the grid is large, we only need the 2x lines in the middle. */
319 if (fac < 0.2f) {
320 lines = 1;
321 final_scale = final_scale_fade;
322 }
323 v3d_cursor_plane_draw_grid(lines, final_scale, final_scale_fade, matrix, plane_axis, color);
324 }
325}
326
327static void cursor_box_draw(const float dimensions[3], uchar color[4])
328{
329 GPUVertFormat *format = immVertexFormat();
330 const uint pos_id = GPU_vertformat_attr_add(format, "pos", gpu::VertAttrType::SFLOAT_32_32_32);
331
333 GPU_line_smooth(true);
334 GPU_line_width(1.0f);
335
338 imm_draw_cube_corners_3d(pos_id, float3{0.0f, 0.0f, dimensions[2]}, dimensions, 0.15f);
340
341 GPU_line_smooth(false);
343}
344
346 uint attr_pos, const float loc[3], const float size, eSnapMode snap_type, const uchar color[4])
347{
348 if (snap_type == SCE_SNAP_TO_GRID) {
349 /* No drawing. */
350 return;
351 }
352
354
356
357 float model_view_new[4][4];
358 GPU_matrix_model_view_get(model_view_new);
359 translate_m4(model_view_new, UNPACK3(loc));
360 copy_v3_fl3(model_view_new[0], size, 0.0f, 0.0f);
361 copy_v3_fl3(model_view_new[1], 0.0f, size, 0.0f);
362 copy_v3_fl3(model_view_new[2], 0.0f, 0.0f, size);
363 GPU_matrix_set(model_view_new);
364
365 float size_b = 1.0f;
366 switch (snap_type) {
368 imm_draw_circle_wire_3d(attr_pos, 0.0f, 0.0f, 1.0f, 24);
369
371 immVertex3f(attr_pos, -size_b, -size_b, 0.0f);
372 immVertex3f(attr_pos, +size_b, +size_b, 0.0f);
373 immVertex3f(attr_pos, -size_b, +size_b, 0.0f);
374 immVertex3f(attr_pos, +size_b, -size_b, 0.0f);
375 immEnd();
376 break;
379 immVertex3f(attr_pos, -size_b, -size_b, 0.0f);
380 immVertex3f(attr_pos, -size_b, +size_b, 0.0f);
381 immVertex3f(attr_pos, +size_b, +size_b, 0.0f);
382 immVertex3f(attr_pos, +size_b, -size_b, 0.0f);
383 immEnd();
384 break;
387 immVertex3f(attr_pos, -size_b, -size_b, 0.0f);
388 immVertex3f(attr_pos, 0.0f, 0.866f * size_b, 0.0f);
389 immVertex3f(attr_pos, +size_b, -size_b, 0.0f);
390 immEnd();
391 break;
394 immVertex3f(attr_pos, -size_b, +size_b, 0.0f);
395 immVertex3f(attr_pos, -size_b, -size_b, 0.0f);
396 immVertex3f(attr_pos, +size_b, -size_b, 0.0f);
397 immEnd();
398
400 immVertex3f(attr_pos, -size_b, 0.0f, 0.0f);
401 immVertex3f(attr_pos, 0.0f, 0.0f, 0.0f);
402 immVertex3f(attr_pos, 0.0f, -size_b, 0.0f);
403 immEnd();
404 break;
405 case SCE_SNAP_TO_EDGE:
407 immVertex3f(attr_pos, -size_b, -size_b, 0.0f);
408 immVertex3f(attr_pos, +size_b, +size_b, 0.0f);
409 immVertex3f(attr_pos, -size_b, +size_b, 0.0f);
410 immVertex3f(attr_pos, +size_b, -size_b, 0.0f);
411 immEnd();
412 break;
414 imm_draw_circle_wire_3d(attr_pos, 0.0f, 0.0f, 1.0f, 24);
418 immVertex3f(attr_pos, 0.0f, 0.0f, 0.0f);
419 immEnd();
420 break;
421 case SCE_SNAP_TO_FACE:
422 default:
423 imm_draw_circle_wire_3d(attr_pos, 0.0f, 0.0f, 1.0f, 24);
424 break;
425 }
426
428}
429
431 const float source_loc[3],
432 const float target_loc[3],
433 const eSnapMode source_type,
434 const eSnapMode target_type,
435 const uchar source_color[4],
436 const uchar target_color[4])
437{
438 if (!source_loc && !target_loc) {
439 return;
440 }
441
442 /* The size of the symbol is larger than the vertex size.
443 * This prevents overlaps. */
444 float radius = 2.5f * ui::theme::get_value_f(TH_VERTEX_SIZE);
445 uint pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", gpu::VertAttrType::SFLOAT_32_32_32);
446
448 GPU_line_smooth(true);
449 GPU_line_width(1.5f);
451
452 if (target_loc) {
454 target_loc,
455 radius * ED_view3d_pixel_size(rv3d, target_loc),
456 target_type,
457 target_color);
458 }
459
460 if (source_loc) {
462 source_loc,
463 radius * ED_view3d_pixel_size(rv3d, source_loc),
464 source_type,
465 source_color);
466
467 if (target_loc && (target_type & SCE_SNAP_TO_EDGE_PERPENDICULAR)) {
468 /* Dashed line. */
470
472 float viewport_size[4];
473 GPU_viewport_size_get_f(viewport_size);
474 immUniform2f("viewport_size", viewport_size[2], viewport_size[3]);
475 immUniform1f("dash_width", 6.0f * U.pixelsize);
476 immUniform1f("udash_factor", 1.0f / 4.0f);
477 immUniformColor4ubv(source_color);
478
480 immVertex3fv(pos, source_loc);
481 immVertex3fv(pos, target_loc);
482 immEnd();
483 }
484 }
485
486 GPU_line_smooth(false);
489}
490
492
493/* -------------------------------------------------------------------- */
496
497/* Checks if the current event is different from the one captured in the last update. */
500 const int2 &mval,
501 uint8_t event_modifier)
502{
503 if (mval != data_intern->last_eventstate.mval) {
504 return true;
505 }
506
507#ifdef USE_SNAP_DETECT_FROM_KEYMAP_HACK
508 if (!(state && (state->flag & V3D_SNAPCURSOR_TOGGLE_ALWAYS_TRUE))) {
509 if (event_modifier != data_intern->last_eventstate.modifier) {
510 return true;
511 }
512 }
513#endif
514
515 return false;
516}
517
518/* Copies the current eventstate. */
519static void v3d_cursor_eventstate_save_xy(SnapCursorDataIntern *cursor_snap, const int2 &mval)
520{
521 cursor_snap->last_eventstate.mval = mval;
522}
523
524#ifdef USE_SNAP_DETECT_FROM_KEYMAP_HACK
526 uint8_t event_modifier)
527{
528 data_intern->last_eventstate.modifier = event_modifier;
529}
530
531static bool v3d_cursor_is_snap_invert(SnapCursorDataIntern *data_intern, uint8_t event_modifier)
532{
533 if (event_modifier == data_intern->last_eventstate.modifier) {
534 /* Nothing has changed. */
535 return data_intern->snap_data.is_snap_invert;
536 }
537
538 /* Save new eventstate. */
539 data_intern->last_eventstate.modifier = event_modifier;
540
541 const int snap_on = data_intern->snap_on;
542
543 const wmWindowManager *wm = G.main->wm.first();
544 wmKeyMap *keymap = WM_keymap_active(wm, data_intern->keymap);
545 for (const wmKeyMapItem &kmi : keymap->items) {
546 if (kmi.flag & KMI_INACTIVE) {
547 continue;
548 }
549
550 if ((kmi.propvalue == snap_on) &&
552 {
553 return true;
554 }
555 }
556 return false;
557}
558#endif
559
561
562/* -------------------------------------------------------------------- */
565
567{
568 if (tool_settings->snap_mode_tools == SCE_SNAP_TO_NONE) {
569 /* Use the snap modes defined in the scene instead. */
570 eSnapMode snap_mode = eSnapMode(tool_settings->snap_mode);
571 if ((snap_mode & SCE_SNAP_TO_INCREMENT) && (tool_settings->snap_flag & SCE_SNAP_ABS_GRID)) {
572 /* Convert snap to increment to snap to grid. */
573 snap_mode |= SCE_SNAP_TO_GRID;
574 }
575 return snap_mode;
576 }
577 return eSnapMode(tool_settings->snap_mode_tools);
578}
579
581{
582 SnapCursorDataIntern *data_intern = &g_data_intern;
583 if (data_intern->snap_context_v3d && (data_intern->scene != scene)) {
585 data_intern->snap_context_v3d = nullptr;
586 }
587 if (data_intern->snap_context_v3d == nullptr) {
589 data_intern->scene = scene;
590 }
591}
592
594{
595 /* If any of the states require the plane, calculate the `plane_omat`. */
597 if (state.snap_state.draw_plane || state.snap_state.draw_box) {
598 return true;
599 }
600 }
601 return false;
602}
603
605 const bContext *C,
606 Depsgraph *depsgraph,
607 Scene *scene,
608 const ARegion *region,
609 View3D *v3d,
610 const int2 &mval,
611 uint8_t event_modifier)
612{
613 SnapCursorDataIntern *data_intern = &g_data_intern;
614 V3DSnapCursorData *snap_data = &data_intern->snap_data;
615 ToolSettings *tool_settings = scene->toolsettings;
616
617 eSnapMode snap_elements = v3d_cursor_snap_elements(tool_settings);
618 const bool calc_plane_omat = v3d_cursor_snap_calc_plane();
619
620 snap_data->is_enabled = true;
622#ifdef USE_SNAP_DETECT_FROM_KEYMAP_HACK
623 snap_data->is_snap_invert = v3d_cursor_is_snap_invert(data_intern, event_modifier);
624#endif
625
626 if (snap_data->is_snap_invert != ((tool_settings->snap_flag & SCE_SNAP) == 0)) {
627 snap_data->is_enabled = false;
628 if (!calc_plane_omat) {
629 snap_data->type_target = SCE_SNAP_TO_NONE;
630 return;
631 }
632 snap_elements = SCE_SNAP_TO_NONE;
633 }
634 }
635
636 const bool use_surface_nor = tool_settings->plane_orient == V3D_PLACE_ORIENT_SURFACE;
637 const bool use_surface_co = snap_data->is_enabled ||
638 tool_settings->plane_depth == V3D_PLACE_DEPTH_SURFACE;
639
640 float co[3], no[3], face_nor[3], obmat[4][4], omat[3][3];
641 eSnapMode snap_elem = SCE_SNAP_TO_NONE;
642 int snap_elem_index[3] = {-1, -1, -1};
643 int index = -1;
644
645 const float2 mval_fl = float2(mval);
646 zero_v3(no);
647 zero_v3(face_nor);
648 unit_m3(omat);
649
650 if (use_surface_nor || use_surface_co) {
652
653 data_intern->snap_elem_hidden = SCE_SNAP_TO_NONE;
654 if (calc_plane_omat && !(snap_elements & SCE_SNAP_TO_FACE)) {
655 data_intern->snap_elem_hidden = SCE_SNAP_TO_FACE;
656 snap_elements |= SCE_SNAP_TO_FACE;
657 }
658
659 if (snap_elements & (SCE_SNAP_TO_GEOM | SCE_SNAP_TO_GRID)) {
660 float prev_co[3] = {0.0f};
661 if (state->prevpoint) {
662 copy_v3_v3(prev_co, state->prevpoint);
663 }
664 else {
665 snap_elements &= ~SCE_SNAP_TO_EDGE_PERPENDICULAR;
666 }
667
668 ed::transform::eSnapEditType edit_mode_type =
672
673 float dist_px = 12.0f * U.pixelsize;
674
677 params.edit_mode_type = edit_mode_type;
682 depsgraph,
683 region,
684 v3d,
685 snap_elements,
686 &params,
687 nullptr,
688 mval_fl,
689 prev_co,
690 &dist_px,
691 co,
692 no,
693 &index,
694 nullptr,
695 obmat,
696 face_nor);
697 if ((snap_elem & data_intern->snap_elem_hidden) && (snap_elements & SCE_SNAP_TO_GRID)) {
698 BLI_assert(snap_elem != SCE_SNAP_TO_GRID);
701 depsgraph,
702 region,
703 v3d,
705 &params,
706 co,
707 mval_fl,
708 prev_co,
709 &dist_px,
710 co,
711 no);
712 }
713 }
714 }
715#ifdef USE_SNAP_DETECT_FROM_KEYMAP_HACK
716 else {
717 v3d_cursor_eventstate_save_modifier(data_intern, event_modifier);
718 }
719#endif
720
721 if (calc_plane_omat) {
722 RegionView3D *rv3d = static_cast<RegionView3D *>(region->regiondata);
723 bool orient_surface = use_surface_nor && (snap_elem != SCE_SNAP_TO_NONE);
724 if (orient_surface) {
725 copy_m3_m4(omat, obmat);
726 }
727 else {
728 const Main *bmain = CTX_data_main(C);
729 ViewLayer *view_layer = CTX_data_view_layer(C);
730 BKE_view_layer_synced_ensure(*bmain, CTX_data_scene(C), view_layer);
732 const int orient_index = BKE_scene_orientation_get_index(scene, SCE_ORIENT_DEFAULT);
733 const int pivot_point = scene->toolsettings->transform_pivot_point;
735 *bmain, scene, view_layer, v3d, rv3d, ob, nullptr, orient_index, pivot_point, omat);
736
737 if (tool_settings->use_plane_axis_auto) {
738 mat3_align_axis_to_v3(omat, tool_settings->plane_axis, rv3d->viewinv[2]);
739 }
740 }
741
742 /* Non-orthogonal matrices cause the preview and final result not to match.
743 *
744 * While making orthogonal doesn't always work well (especially with gimbal orientation for
745 * e.g.) it's a corner case, without better alternatives as objects don't support shear. */
746 orthogonalize_m3(omat, tool_settings->plane_axis);
747
748 if (orient_surface) {
749 if (!is_zero_v3(face_nor)) {
750 /* Negate the face normal according to the view. */
751 float ray_dir[3];
752 if (rv3d->is_persp) {
753 BLI_assert_msg(snap_elem != SCE_SNAP_TO_NONE,
754 "Use of variable `co` without it being computed");
755
756 sub_v3_v3v3(ray_dir, co, rv3d->viewinv[3]); /* No need to normalize. */
757 }
758 else {
759 negate_v3_v3(ray_dir, rv3d->viewinv[2]);
760 }
761
762 if (dot_v3v3(ray_dir, face_nor) >= 0.0f) {
763 negate_v3(face_nor);
764 }
765 }
766 else if (!is_zero_v3(no)) {
767 copy_v3_v3(face_nor, no);
768 }
769 else {
770 face_nor[tool_settings->plane_axis] = 1.0f;
771 }
772 v3d_cursor_poject_surface_normal(face_nor, obmat, omat);
773 }
774 }
775
776 if (!use_surface_co) {
777 snap_elem = SCE_SNAP_TO_NONE;
778 }
779
780 float *co_depth = (snap_elem != SCE_SNAP_TO_NONE) ? co : scene->cursor.location;
781 snap_elem &= ~data_intern->snap_elem_hidden;
782 if (snap_elem == SCE_SNAP_TO_NONE) {
783 RegionView3D *rv3d = static_cast<RegionView3D *>(region->regiondata);
784 const float *plane_normal = omat[tool_settings->plane_axis];
785 bool do_plane_isect = (tool_settings->plane_depth != V3D_PLACE_DEPTH_CURSOR_VIEW) &&
786 (rv3d->is_persp ||
787 (fabsf(dot_v3v3(plane_normal, rv3d->viewinv[2])) > eps_view_align));
788
789 if (do_plane_isect) {
790 float plane[4];
791 plane_from_point_normal_v3(plane, co_depth, plane_normal);
792 do_plane_isect = ED_view3d_win_to_3d_on_plane(region, plane, mval_fl, rv3d->is_persp, co);
793 }
794
795 if (!do_plane_isect) {
796 ED_view3d_win_to_3d(v3d, region, co_depth, mval_fl, co);
797 }
798 }
799 else if (snap_elem & SCE_SNAP_TO_VERTEX) {
800 snap_elem_index[0] = index;
801 }
802 else if (snap_elem &
804 {
805 snap_elem_index[1] = index;
806 }
807 else if (snap_elem & (SCE_SNAP_TO_FACE | SCE_SNAP_TO_FACE_MIDPOINT)) {
808 snap_elem_index[2] = index;
809 }
810
811 snap_data->type_target = snap_elem;
812 copy_v3_v3(snap_data->loc, co);
813 copy_v3_v3(snap_data->nor, no);
814 copy_m4_m4(snap_data->obmat, obmat);
815 copy_v3_v3_int(snap_data->elem_index, snap_elem_index);
816
817 copy_m3_m3(snap_data->plane_omat, omat);
818
819 v3d_cursor_eventstate_save_xy(data_intern, mval);
820}
821
823
824/* -------------------------------------------------------------------- */
827
829{
830 if (G.moving) {
831 return false;
832 }
833
834 ScrArea *area = CTX_wm_area(C);
835 if (area->spacetype != SPACE_VIEW3D) {
836 return false;
837 }
838
839 ARegion *region = CTX_wm_region(C);
840 if (region->regiontype != RGN_TYPE_WINDOW) {
841 if (!region->overlap) {
842 return false;
843 }
844 /* Sometimes the cursor may be on an invisible part of an overlapping region. */
845 wmWindow *win = CTX_wm_window(C);
846 const wmEvent *event = win->runtime->eventstate;
847 if (ED_region_overlap_isect_xy(region, event->xy)) {
848 return false;
849 }
850 /* Find the visible region under the cursor.
851 * TODO(Germano): Shouldn't this be the region in context? */
853 }
854
855 RegionView3D *rv3d = static_cast<RegionView3D *>(region->regiondata);
856 if (rv3d->rflag & RV3D_NAVIGATING) {
857 /* Don't draw the cursor while navigating. It can be distracting. */
858 return false;
859 };
860
861 /* Call this callback last and don't reuse the `state` as the caller can free the cursor. */
863 if (state->poll && !state->poll(region, state->poll_data)) {
864 return false;
865 }
866
867 return true;
868}
869
871 const int2 &xy,
872 const float2 & /*tilt*/,
873 void * /*customdata*/)
874{
875 ScrArea *area = CTX_wm_area(C);
877 if (region->alignment == RGN_ALIGN_QSPLIT) {
878 /* Quad-View. */
879 region = BKE_area_find_region_xy(area, RGN_TYPE_WINDOW, xy);
880 if (region == nullptr) {
881 return;
882 }
883 }
884
885 const int2 mval(xy.x - region->winrct.xmin, xy.y - region->winrct.ymin);
886
887 SnapCursorDataIntern *data_intern = &g_data_intern;
889 V3DSnapCursorData *snap_data = &data_intern->snap_data;
890 Depsgraph *depsgraph = CTX_data_ensure_evaluated_depsgraph(C);
891 Scene *scene = DEG_get_input_scene(depsgraph);
892
893 const wmWindow *win = CTX_wm_window(C);
894 const wmEvent *event = win->runtime->eventstate;
895 if (event && v3d_cursor_eventstate_has_changed(data_intern, state, mval, event->modifier)) {
896 View3D *v3d = CTX_wm_view3d(C);
897 v3d_cursor_snap_update(state, C, depsgraph, scene, region, v3d, mval, event->modifier);
898 }
899
900 const bool draw_plane = state->draw_plane || state->draw_box;
901 if (snap_data->type_target == SCE_SNAP_TO_NONE && !draw_plane) {
902 return;
903 }
904
905 /* Setup viewport & matrix. */
906 RegionView3D *rv3d = static_cast<RegionView3D *>(region->regiondata);
907 wmViewport(&region->winrct);
909 GPU_matrix_set(rv3d->viewmat);
910
911 float matrix[4][4];
912 if (draw_plane) {
913 copy_m4_m3(matrix, snap_data->plane_omat);
914 copy_v3_v3(matrix[3], snap_data->loc);
915
916 v3d_cursor_plane_draw(rv3d, scene->toolsettings->plane_axis, matrix);
917 }
918
919 if (snap_data->type_target != SCE_SNAP_TO_NONE && (state->draw_point || state->draw_box)) {
920 const float *source_loc = (snap_data->type_target & SCE_SNAP_TO_EDGE_PERPENDICULAR) ?
921 state->prevpoint :
922 nullptr;
923
925 source_loc,
926 snap_data->loc,
927 snap_data->type_source,
928 snap_data->type_target,
929 state->source_color,
930 state->target_color);
931 }
932
933 if (state->draw_box) {
934 GPU_matrix_mul(matrix);
935 cursor_box_draw(state->box_dimensions, state->color_box);
936 }
937
938 /* Restore matrix. */
939 wmWindowViewport(win);
940}
941
943
945{
946 SnapCursorDataIntern *data_intern = &g_data_intern;
947 if (data_intern->state_intern.is_empty()) {
949 }
950 return &(data_intern->state_intern.last())->snap_state;
951}
952
954{
957 return;
958 }
959
960 SnapStateIntern *state_intern = STATE_INTERN_GET(state);
961 if (state_intern == g_data_intern.state_intern.last()) {
962 return;
963 }
964
965 if (!BLI_remlink_safe(&g_data_intern.state_intern, state_intern)) {
967 return;
968 }
969
970 BLI_addtail(&g_data_intern.state_intern, state_intern);
971}
972
974{
975 SnapCursorDataIntern *data_intern = &g_data_intern;
976
977 if (!data_intern->handle) {
978 if (!data_intern->is_initiated) {
979 /* Only initiate intern data once.
980 * TODO: ED_view3d_cursor_snap_init */
981
982#ifdef USE_SNAP_DETECT_FROM_KEYMAP_HACK
983 wmKeyConfig *keyconf = (G.main->wm.first())->runtime->defaultconf;
984
985 data_intern->keymap = WM_modalkeymap_find(keyconf, "Generic Gizmo Tweak Modal Map");
987 static_cast<const EnumPropertyItem *>(data_intern->keymap->modal_items),
988 "SNAP_ON",
989 &data_intern->snap_on);
990#endif
991 data_intern->is_initiated = true;
992 }
993
996 data_intern->handle = pc;
997 }
998}
999
1001{
1002 SnapCursorDataIntern *data_intern = &g_data_intern;
1003 if (data_intern->handle) {
1004 if (G_MAIN->wm.first_) {
1005 WM_paint_cursor_end(data_intern->handle);
1006 }
1007 data_intern->handle = nullptr;
1008 }
1009 if (data_intern->snap_context_v3d) {
1011 data_intern->snap_context_v3d = nullptr;
1012 }
1013}
1014
1016{
1018
1019 /* These values are temporarily set by the tool.
1020 * They are not convenient as default values.
1021 * So reset to null. */
1027}
1028
1030{
1031 SnapCursorDataIntern *data_intern = &g_data_intern;
1032 if (!data_intern->handle) {
1034 }
1035
1037 state_intern->snap_state = g_data_intern.state_default;
1038 BLI_addtail(&g_data_intern.state_intern, state_intern);
1039
1040 return &state_intern->snap_state;
1041}
1042
1044{
1045 SnapCursorDataIntern *data_intern = &g_data_intern;
1046 if (data_intern->state_intern.is_empty()) {
1047 return;
1048 }
1049
1050 SnapStateIntern *state_intern = STATE_INTERN_GET(state);
1051 BLI_remlink(&data_intern->state_intern, state_intern);
1052 MEM_delete(state_intern);
1053 if (data_intern->state_intern.is_empty()) {
1055 }
1056}
1057
1059 const float prev_point[3])
1060{
1061 SnapCursorDataIntern *data_intern = &g_data_intern;
1062 if (!state) {
1064 }
1065 if (prev_point) {
1066 copy_v3_v3(data_intern->prevpoint_stack, prev_point);
1067 state->prevpoint = data_intern->prevpoint_stack;
1068 }
1069 else {
1070 state->prevpoint = nullptr;
1071 }
1072}
1073
1075 const bContext *C,
1076 const ARegion *region,
1077 const int2 &mval)
1078{
1079 SnapCursorDataIntern *data_intern = &g_data_intern;
1080 const wmEvent *event = CTX_wm_window(C)->runtime->eventstate;
1081 if (event && v3d_cursor_eventstate_has_changed(data_intern, state, mval, event->modifier)) {
1082 Depsgraph *depsgraph = CTX_data_ensure_evaluated_depsgraph(C);
1083 Scene *scene = DEG_get_input_scene(depsgraph);
1084 View3D *v3d = CTX_wm_view3d(C);
1085
1086 if (!state) {
1088 }
1089 v3d_cursor_snap_update(state, C, depsgraph, scene, region, v3d, mval, event->modifier);
1090 }
1091}
1092
1094{
1095 SnapCursorDataIntern *data_intern = &g_data_intern;
1096 return &data_intern->snap_data;
1097}
1098
1105
1106} // namespace blender
#define G_MAIN
General operations, lookup, etc. for blender objects.
Object is a sort of wrapper for general info.
#define SCE_SNAP_TO_GEOM
#define SCE_SNAP_TO_VERTEX
#define UI_SCALE_FAC
Read Blender memory allocation and freeing.
#define C
Definition RandGen.cpp:29
#define UNPACK4(a)
#define BLI_assert_unreachable()
#define UNPACK3(a)
#define BLI_assert_msg(a, msg)
#define FLT_EPSILON
float3x3 scale(float3x3 mat, float2 scale)
const int state
eSnapMode snap_object_project_view3d_ex(SnapObjectContext *sctx, Depsgraph *depsgraph, const ARegion *region, const View3D *v3d, const eSnapMode snap_to, const SnapObjectParams *params, const float init_co[3], const float mval[2], const float prev_co[3], float *dist_px, float r_loc[3], float r_no[3], int *r_index, const Object **r_ob, float r_obmat[4][4], float r_face_nor[3])
void snap_object_context_destroy(SnapObjectContext *sctx)
SnapObjectContext * snap_object_context_create()
eSnapMode snap_object_project_view3d(SnapObjectContext *sctx, Depsgraph *depsgraph, const ARegion *region, const View3D *v3d, const eSnapMode snap_to, const SnapObjectParams *params, const float init_co[3], const float mval[2], const float prev_co[3], float *dist_px, float r_loc[3], float r_no[3])
short calc_orientation_from_type_ex(const Main &bmain, const Scene *scene, ViewLayer *view_layer, const View3D *v3d, const RegionView3D *rv3d, Object *ob, Object *obedit, short orientation_index, int pivot_point, float r_mat[3][3])
float get_value_f(int colorid)
BMesh Iterators.
void * MEM_new_uninitialized(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
UserDef U
Definition blender.cc:58
wmKeyMap * WM_keymap_active(const wmWindowManager *wm, wmKeyMap *keymap)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
MINLINE void negate_v3_v3(float r[3], const float a[3])
void orthogonalize_m3(float R[3][3], int axis)
VecBase< uint8_t, 4 > uchar4
MINLINE int mod_i(int i, int n)
ScrArea * CTX_wm_area(const bContext *C)
void wmViewport(const rcti *winrct)
Object * BKE_view_layer_active_object_get(const ViewLayer *view_layer)
void immEnd()
V3DSnapCursorState * ED_view3d_cursor_snap_state_create()
wmKeyMap * WM_modalkeymap_find(wmKeyConfig *keyconf, const char *idname)
Definition wm_keymap.cc:999
MINLINE void copy_v3_v3(float r[3], const float a[3])
unsigned int uint
Definition BKE_text.h:55
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
void copy_m3_m4(float m1[3][3], const float m2[4][4])
bool ED_region_overlap_isect_xy(const ARegion *region, const int event_xy[2])
Definition area_query.cc:52
V3DSnapCursorState * ED_view3d_cursor_snap_state_active_get()
void wmWindowViewport(const wmWindow *win)
void copy_m3_m3(float m1[3][3], const float m2[3][3])
void immUniform2f(const char *name, float x, float y)
void immBeginAtMost(GPUPrimType, uint max_vertex_len)
void copy_m4_m3(float m1[4][4], const float m2[3][3])
float ED_view3d_pixel_size(const RegionView3D *rv3d, const float co[3])
GPUVertFormat * immVertexFormat()
static void v3d_cursor_snap_draw_fn(bContext *C, const int2 &xy, const float2 &, void *)
void normalize_m3(float R[3][3]) ATTR_NONNULL()
MINLINE void copy_v3_v3_int(int r[3], const int a[3])
static void v3d_cursor_snap_context_ensure(Scene *scene)
void immBegin(GPUPrimType, uint vertex_len)
void GPU_line_smooth(bool enable)
Definition gpu_state.cc:80
int BKE_scene_orientation_get_index(Scene *scene, int slot_index)
Definition scene.cc:2678
@ GPU_PRIM_LINE_STRIP
@ GPU_PRIM_LINE_LOOP
static bool v3d_cursor_is_snap_invert(SnapCursorDataIntern *data_intern, uint8_t event_modifier)
wmWindow * CTX_wm_window(const bContext *C)
void translate_m4(float mat[4][4], float Tx, float Ty, float Tz)
ATTR_WARN_UNUSED_RESULT const BMVert * v
static SnapCursorDataIntern g_data_intern
float ceil_power_of_10(float f)
Definition math_base.cc:76
static void v3d_cursor_plane_draw_grid(const int resolution, const float scale, const float scale_fade, const float matrix[4][4], const int plane_axis, const float color[4])
void plane_from_point_normal_v3(float r_plane[4], const float plane_co[3], const float plane_no[3])
Definition math_geom.cc:225
void ED_view3d_global_to_vector(const RegionView3D *rv3d, const float coord[3], float r_out[3])
void immUnbindProgram()
ViewLayer * CTX_data_view_layer(const bContext *C)
void GPU_matrix_mul(const float m[4][4])
static const float eps_view_align
void GPU_line_width(float width)
Definition gpu_state.cc:151
wmEventModifierFlag
Definition WM_types.hh:290
@ GPU_BLEND_NONE
Definition GPU_state.hh:88
@ GPU_BLEND_ALPHA
Definition GPU_state.hh:90
@ GPU_BLEND_ADDITIVE
Definition GPU_state.hh:92
static void v3d_cursor_snap_free()
Global G
Definition blender.cc:57
bool RNA_enum_value_from_id(const EnumPropertyItem *item, const char *identifier, int *r_value)
V3DSnapCursorData * ED_view3d_cursor_snap_data_get()
static void v3d_cursor_poject_surface_normal(const float normal[3], const float obmat[4][4], float r_mat[3][3])
void ED_view3d_cursor_snap_state_default_set(V3DSnapCursorState *state)
void immBindBuiltinProgram(GPUBuiltinShader shader_id)
static bool v3d_cursor_snap_calc_plane()
static bool v3d_cursor_snap_poll_fn(bContext *C)
void mul_m4_v3(const float M[4][4], float r[3])
void ED_view3d_cursor_snap_state_free(V3DSnapCursorState *state)
VecBase< int32_t, 2 > int2
VecBase< float, 2 > float2
void immUniform1f(const char *name, float x)
ed::transform::SnapObjectContext * ED_view3d_cursor_snap_context_ensure(Scene *scene)
unsigned char uchar
Definition BKE_text.h:58
void BLI_remlink(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:138
void GPU_viewport_size_get_f(float coords[4])
Definition gpu_state.cc:258
void immAttr4fv(uint attr_id, const float data[4])
BMesh const char void * data
@ V3D_SNAPCURSOR_SNAP_EDIT_GEOM_CAGE
Definition ED_view3d.hh:350
@ V3D_SNAPCURSOR_TOGGLE_ALWAYS_TRUE
Definition ED_view3d.hh:346
@ V3D_SNAPCURSOR_SNAP_EDIT_GEOM_FINAL
Definition ED_view3d.hh:349
@ V3D_SNAPCURSOR_OCCLUSION_ALWAYS_TRUE
Definition ED_view3d.hh:347
bool ED_view3d_win_to_3d_on_plane(const ARegion *region, const float plane[4], const float mval[2], bool do_clip, float r_out[3])
void ED_view3d_cursor_snap_draw_util(RegionView3D *rv3d, const float source_loc[3], const float target_loc[3], const eSnapMode source_type, const eSnapMode target_type, const uchar source_color[4], const uchar target_color[4])
@ GPU_SHADER_3D_POINT_UNIFORM_COLOR
@ GPU_SHADER_3D_LINE_DASHED_UNIFORM_COLOR
static eSnapMode v3d_cursor_snap_elements(ToolSettings *tool_settings)
Depsgraph * CTX_data_ensure_evaluated_depsgraph(const bContext *C, bool rna_write_check=false)
void imm_draw_cube_corners_3d(uint pos, const float center[3], const float aspect[3], float factor)
unsigned char uint8_t
Definition BKE_text.h:79
@ SCE_SNAP_TO_EDGE_MIDPOINT
@ SCE_SNAP_TO_EDGE_PERPENDICULAR
@ SCE_SNAP_TO_FACE_MIDPOINT
@ SCE_SNAP_TO_EDGE_ENDPOINT
bool BKE_view_layer_synced_ensure(const Main &bmain, const Scene *scene, ViewLayer *view_layer)
MINLINE void negate_v3(float r[3])
void ED_view3d_cursor_snap_data_update(V3DSnapCursorState *state, const bContext *C, const ARegion *region, const int2 &mval)
void GPU_matrix_projection_set(const float m[4][4])
MINLINE void copy_v4_v4_uchar(unsigned char r[4], const unsigned char a[4])
MINLINE void zero_v3(float r[3])
uint GPU_vertformat_attr_add(GPUVertFormat *format, StringRef name, gpu::VertAttrType type)
static void v3d_cursor_eventstate_save_modifier(SnapCursorDataIntern *data_intern, uint8_t event_modifier)
bool WM_event_modifier_flag_match_kmi_press(const wmEventModifierFlag event_modifier, const wmKeyMapItem *kmi)
wmPaintCursor * WM_paint_cursor_activate(short space_type, short region_type, wmPaintCursorPoll poll, wmPaintCursorDraw draw, void *customdata)
static bool v3d_cursor_eventstate_has_changed(SnapCursorDataIntern *data_intern, V3DSnapCursorState *state, const int2 &mval, uint8_t event_modifier)
void ED_view3d_cursor_snap_state_prevpoint_set(V3DSnapCursorState *state, const float prev_point[3])
@ V3D_PLACE_DEPTH_SURFACE
@ V3D_PLACE_DEPTH_CURSOR_VIEW
ARegion * BKE_area_find_region_type(const ScrArea *area, int region_type)
Definition screen.cc:1024
void immVertex3fv(uint attr_id, const float data[3])
static void v3d_cursor_snap_activate()
MINLINE void sub_v3_v3v3(float r[3], const float a[3], const float b[3])
MINLINE void copy_v3_fl3(float v[3], float x, float y, float z)
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118
Main * CTX_data_main(const bContext *C)
void unit_m3(float m[3][3])
ATTR_WARN_UNUSED_RESULT const BMVert * v2
MINLINE void copy_v3_fl(float r[3], float f)
static void cursor_box_draw(const float dimensions[3], uchar color[4])
MINLINE float max_ff(float a, float b)
static void v3d_cursor_eventstate_save_xy(SnapCursorDataIntern *cursor_snap, const int2 &mval)
Scene * DEG_get_input_scene(const Depsgraph *graph)
void ED_view3d_cursor_snap_state_active_set(V3DSnapCursorState *state)
MINLINE float square_f(float a)
static bool mat3_align_axis_to_v3(float mat[3][3], const int axis_align, const float v[3])
bool BLI_remlink_safe(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:161
Scene * CTX_data_scene(const bContext *C)
void immVertex3f(uint attr_id, float x, float y, float z)
void GPU_blend(GPUBlend blend)
Definition gpu_state.cc:44
void copy_m4_m4(float m1[4][4], const float m2[4][4])
VecBase< float, 3 > float3
static void cursor_point_draw(uint attr_pos, const float loc[3], const float size, eSnapMode snap_type, const uchar color[4])
void immUniformColor4ubv(const unsigned char rgba[4])
void GPU_matrix_pop()
void imm_draw_circle_wire_3d(uint pos, float x, float y, float radius, int nsegments)
void ED_view3d_win_to_3d(const View3D *v3d, const ARegion *region, const float depth_pt[3], const float mval[2], float r_out[3])
@ V3D_PLACE_ORIENT_SURFACE
MINLINE float dot_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
ARegion * CTX_wm_region(const bContext *C)
void GPU_matrix_set(const float m[4][4])
MINLINE bool is_zero_v3(const float v[3]) ATTR_WARN_UNUSED_RESULT
static void v3d_cursor_plane_draw(const RegionView3D *rv3d, const int plane_axis, const float matrix[4][4])
View3D * CTX_wm_view3d(const bContext *C)
void GPU_matrix_push()
static void v3d_cursor_snap_update(V3DSnapCursorState *state, const bContext *C, Depsgraph *depsgraph, Scene *scene, const ARegion *region, View3D *v3d, const int2 &mval, uint8_t event_modifier)
bool WM_paint_cursor_end(wmPaintCursor *handle)
ARegion * BKE_area_find_region_xy(const ScrArea *area, int regiontype, const int xy[2]) ATTR_NONNULL(3)
Definition screen.cc:1053
const float(* GPU_matrix_model_view_get(float m[4][4]))[4]
#define fabsf
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
eRegion_Alignment alignment
eRegion_Type regiontype
eRegionView3D_Flag rflag
View3DCursor cursor
struct ToolSettings * toolsettings
struct blender::SnapCursorDataIntern::@074041070204043305046355314270342045133105135075 last_eventstate
ed::transform::SnapObjectContext * snap_context_v3d
ListBaseT< SnapStateIntern > state_intern
V3DSnapCursorState snap_state
eV3DPlaceOrient plane_orient
bool(*) poll(ARegion *region, void *custom_poll_data)
Definition ED_view3d.hh:380
ListBaseT< wmKeyMapItem > items
bke::WindowRuntime * runtime
#define FADE(v)
#define STATE_INTERN_GET(state)