Blender V5.3
view3d_navigate_view_ndof.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8#include "BLI_bounds.hh"
9#include "BLI_math_geom_c.hh"
10#include "BLI_math_matrix.hh"
12#include "BLI_math_vector_c.hh"
13#include "BLI_rect.hh"
14
15#include "BKE_layer.hh"
16
18
19#include "WM_api.hh"
20
21#include "ED_screen.hh"
22
23#include "view3d_intern.hh"
24#include "view3d_navigate.hh" /* own include */
25
26namespace blender {
27
28#ifdef WITH_INPUT_NDOF
29static bool ndof_orbit_center_is_valid(const RegionView3D *rv3d, const float3 &center);
30static bool ndof_orbit_center_is_used_no_viewport();
31static bool ndof_orbit_center_is_used(const View3D *v3d, const RegionView3D *rv3d);
32#endif
33
34/* -------------------------------------------------------------------- */
37
38#ifdef WITH_INPUT_NDOF
39
41static bool is_bounding_box_in_frustum(const float projmat[4][4],
42 const Bounds<float3> &bounding_box)
43{
44 float planes[4][4];
45 planes_from_projmat(projmat, planes[0], planes[1], planes[2], planes[3], nullptr, nullptr);
46 int ret = isect_aabb_planes_v3(planes, 4, bounding_box.min, bounding_box.max);
47
49}
50
51enum {
52 HAS_TRANSLATE = (1 << 0),
53 HAS_ROTATE = (1 << 0),
54};
55
56static bool ndof_has_translate(const wmNDOFMotionData &ndof,
57 const View3D *v3d,
58 const RegionView3D *rv3d)
59{
60 return !is_zero_v3(ndof.tvec) && !ED_view3d_offset_lock_check(v3d, rv3d);
61}
62
63static bool ndof_has_translate_pan(const wmNDOFMotionData &ndof,
64 const View3D *v3d,
65 const RegionView3D *rv3d)
66{
67 return WM_event_ndof_translation_has_pan(ndof) && !ED_view3d_offset_lock_check(v3d, rv3d);
68}
69
70static bool ndof_has_rotate(const wmNDOFMotionData &ndof, const RegionView3D *rv3d)
71{
72 return !is_zero_v3(ndof.rvec) && ((RV3D_LOCK_FLAGS(rv3d) & RV3D_LOCK_ROTATION) == 0);
73}
74
78static float view3d_ndof_pan_speed_calc_ex(RegionView3D *rv3d, const float depth_pt[3])
79{
80 float speed = rv3d->pixsize * NDOF_PIXELS_PER_SECOND;
81
82 if (rv3d->is_persp) {
83 speed *= ED_view3d_calc_zfac(rv3d, depth_pt);
84 }
85
86 return speed;
87}
88
89static float view3d_ndof_pan_speed_calc_from_dist(RegionView3D *rv3d, const float dist)
90{
91 float viewinv[4];
92 float tvec[3];
93
94 BLI_assert(dist >= 0.0f);
95
96 copy_v3_fl3(tvec, 0.0f, 0.0f, dist);
97 /* rv3d->viewinv isn't always valid */
98# if 0
99 mul_mat3_m4_v3(rv3d->viewinv, tvec);
100# else
101 invert_qt_qt_normalized(viewinv, rv3d->viewquat);
102 mul_qt_v3(viewinv, tvec);
103# endif
104
105 return view3d_ndof_pan_speed_calc_ex(rv3d, tvec);
106}
107
108static float view3d_ndof_pan_speed_calc(RegionView3D *rv3d)
109{
110 float tvec[3];
111 if ((rv3d->ndof_flag & RV3D_NDOF_OFS_IS_VALID) && ndof_orbit_center_is_used_no_viewport()) {
112 negate_v3_v3(tvec, rv3d->ndof_ofs);
113 }
114 else {
115 negate_v3_v3(tvec, rv3d->ofs);
116 }
117
118 return view3d_ndof_pan_speed_calc_ex(rv3d, tvec);
119}
120
127static void view3d_ndof_pan_zoom(const wmNDOFMotionData &ndof,
128 ScrArea *area,
129 ARegion *region,
130 const bool has_translate,
131 const bool has_zoom)
132{
133 RegionView3D *rv3d = static_cast<RegionView3D *>(region->regiondata);
134
135 if (has_translate == false && has_zoom == false) {
136 return;
137 }
138
139 float3 pan_vec = WM_event_ndof_translation_get_for_navigation(ndof);
140 if (has_zoom) {
141 /* zoom with Z */
142
143 /* "zoom in" or "translate"? depends on zoom mode in user settings? */
144 if (pan_vec[2]) {
145 float zoom_distance = rv3d->dist * ndof.time_delta * pan_vec[2];
146 rv3d->dist += zoom_distance;
147 }
148
149 /* Zoom!
150 * velocity should be proportional to the linear velocity attained by rotational motion
151 * of same strength [got that?] proportional to `arclength = radius * angle`.
152 */
153
154 pan_vec[2] = 0.0f;
155 }
156 else {
157 /* dolly with Z */
158
159 /* all callers must check */
160 if (has_translate) {
161 BLI_assert(ED_view3d_offset_lock_check((View3D *)area->spacedata.first_, rv3d) == false);
162 }
163 }
164
165 if (has_translate) {
166
167 /* transform motion from view to world coordinates */
168 float view_inv[4];
169 invert_qt_qt_normalized(view_inv, rv3d->viewquat);
170
171 if (U.ndof_navigation_mode == NDOF_NAVIGATION_MODE_DRONE) {
172 /* Setting up higher margin for a smoother transition. */
173 const float eps_yz_swap = 0.02f;
174
175 /* If camera orientation is top/bottom, then swap panning axis
176 * and negate Y to simulate drone behavior. */
177 float zvec[3] = {0, 0, 1};
178 mul_qt_v3(view_inv, zvec);
179
180 if (std::abs(zvec[2]) > (1.0f - eps_yz_swap)) {
181 std::swap(pan_vec.z, pan_vec.y);
182 pan_vec.y *= -1.0f;
183 mul_qt_v3(view_inv, pan_vec);
184 }
185 else {
186 /* In other cases, buffer Y and discard it during calculations,
187 * limiting the pan movement to XZ plane. */
188 const float pan_vec_y_prev = pan_vec.y;
189 pan_vec.y = 0.0f;
190
191 /* Calculate the `pan_vec` in view space, and set Z to an absolute value afterwards. */
192 mul_qt_v3(view_inv, pan_vec);
193 pan_vec.z = pan_vec_y_prev;
194
195 /* If the view is turned upside down, then invert the pan Z value. */
196 float yvec[3] = {0, 1, 0};
197 mul_qt_v3(view_inv, yvec);
198
199 if (yvec[2] < 0.0f) {
200 pan_vec.z *= -1.0f;
201 }
202 }
203 }
204 else {
205 mul_qt_v3(view_inv, pan_vec);
206 }
207
208 const float speed = view3d_ndof_pan_speed_calc(rv3d);
209 pan_vec *= speed * ndof.time_delta;
210
211 /* Move center of view opposite of hand motion (this is camera mode, not object mode). */
212 sub_v3_v3(rv3d->ofs, pan_vec);
213
214 /* When in Fly mode with "Auto" speed, move `ndof_ofs` as well (to keep the speed constant). */
216 sub_v3_v3(rv3d->ndof_ofs, pan_vec);
217 }
218
219 if (RV3D_LOCK_FLAGS(rv3d) & RV3D_BOXVIEW) {
220 view3d_boxview_sync(area, region);
221 }
222 }
223}
224
225static void view3d_ndof_orbit(const wmNDOFMotionData &ndof,
226 ScrArea *area,
227 ARegion *region,
228 ViewOpsData *vod,
229 const bool apply_dyn_ofs)
230{
231
232 View3D *v3d = area->spacedata.first_as<View3D>();
233 RegionView3D *rv3d = static_cast<RegionView3D *>(region->regiondata);
234
235 float view_inv[4];
236
238
239 ED_view3d_persp_ensure(vod->depsgraph, v3d, region);
240
241 rv3d->view = RV3D_VIEW_USER;
242
243 invert_qt_qt_normalized(view_inv, rv3d->viewquat);
244
246 /* Turntable view code adapted for 3D mouse use. */
247 float angle, quat[4];
248 float xvec[3] = {1, 0, 0};
249 float yvec[3] = {0, 1, 0};
250
251 /* only use XY, ignore Z */
252 float3 rot = WM_event_ndof_rotation_get_for_navigation(ndof);
253
254 /* Determine the direction of the X vector (for rotating up and down). */
255 mul_qt_v3(view_inv, xvec);
256 /* Determine the direction of the Y vector (to check if the view is upside down). */
257 mul_qt_v3(view_inv, yvec);
258
259 /* Level the view to a "horizon plane", an upside down view is kept that way,
260 * the turn-table direction is flipped below. */
261 const float horizon_plane[3] = {0.0f, 0.0f, 1.0f};
262 view3d_horizon_correct_quat(rv3d->viewquat, horizon_plane, true, nullptr, 0.0f, 1.0f);
263
264 /* Perform the up/down rotation */
265 angle = ndof.time_delta * rot[0];
266 axis_angle_to_quat(quat, xvec, angle);
267 mul_qt_qtqt(rv3d->viewquat, rv3d->viewquat, quat);
268
269 /* Perform the Z rotation. */
270 angle = ndof.time_delta * rot[1];
271
272 /* Flip the turntable angle when the view is upside down. */
273 if (yvec[2] < 0.0f) {
274 angle *= -1.0f;
275 }
276
277 /* Update the onscreen axis-angle indicator. */
278 rv3d->ndof_rot_angle = angle;
279 rv3d->ndof_rot_axis[0] = 0;
280 rv3d->ndof_rot_axis[1] = 0;
281 rv3d->ndof_rot_axis[2] = 1;
282
284 mul_qt_qtqt(rv3d->viewquat, rv3d->viewquat, quat);
285 }
286 else {
287 float quat[4];
288 float axis[3];
289 float angle = ndof.time_delta *
290 WM_event_ndof_rotation_get_axis_angle_for_navigation(ndof, axis);
291
292 /* transform rotation axis from view to world coordinates */
293 mul_qt_v3(view_inv, axis);
294
295 /* Update the onscreen axis-angle indicator. */
296 rv3d->ndof_rot_angle = angle;
297 copy_v3_v3(rv3d->ndof_rot_axis, axis);
298
299 axis_angle_to_quat(quat, axis, angle);
300
301 /* apply rotation */
302 mul_qt_qtqt(rv3d->viewquat, rv3d->viewquat, quat);
303 }
304
305 if (apply_dyn_ofs) {
306 /* Use NDOF center as a dynamic offset. */
307 if (ndof_orbit_center_is_used(v3d, rv3d)) {
308 if (rv3d->ndof_flag & RV3D_NDOF_OFS_IS_VALID) {
309 if (ndof_orbit_center_is_valid(vod->rv3d, -float3(rv3d->ndof_ofs))) {
310 vod->use_dyn_ofs = true;
311 copy_v3_v3(vod->dyn_ofs, rv3d->ndof_ofs);
312 }
313 else {
314 rv3d->ndof_flag &= ~RV3D_NDOF_OFS_IS_VALID;
315 }
316 }
317 }
318 viewrotate_apply_dyn_ofs(vod, rv3d->viewquat);
319 }
320}
321
322void view3d_ndof_fly(const wmNDOFMotionData &ndof,
323 View3D *v3d,
324 RegionView3D *rv3d,
325 const bool use_precision,
326 const short protectflag,
327 bool *r_has_translate,
328 bool *r_has_rotate)
329{
330 bool has_translate = ndof_has_translate(ndof, v3d, rv3d);
331 bool has_rotate = ndof_has_rotate(ndof, rv3d);
332
333 float view_inv[4];
334 invert_qt_qt_normalized(view_inv, rv3d->viewquat);
335
336 rv3d->ndof_rot_angle = 0.0f; /* Disable onscreen rotation indicator. */
337
338 if (has_translate) {
339 /* ignore real 'dist' since fly has its own speed settings,
340 * also its overwritten at this point. */
341 float speed = view3d_ndof_pan_speed_calc_from_dist(rv3d, 1.0f);
342 float trans_orig_y;
343
344 if (use_precision) {
345 speed *= 0.2f;
346 }
347
348 float3 trans = (speed * ndof.time_delta) * WM_event_ndof_translation_get(ndof);
349 trans_orig_y = trans[1];
350
351 if (U.ndof_flag & NDOF_FLY_HELICOPTER) {
352 trans[1] = 0.0f;
353 }
354
355 /* transform motion from view to world coordinates */
356 mul_qt_v3(view_inv, trans);
357
358 if (U.ndof_flag & NDOF_FLY_HELICOPTER) {
359 /* replace world z component with device y (yes it makes sense) */
360 trans[2] = trans_orig_y;
361 }
362
363 if (rv3d->persp == RV3D_CAMOB) {
364 /* respect camera position locks */
365 if (protectflag & OB_LOCK_LOCX) {
366 trans[0] = 0.0f;
367 }
368 if (protectflag & OB_LOCK_LOCY) {
369 trans[1] = 0.0f;
370 }
371 if (protectflag & OB_LOCK_LOCZ) {
372 trans[2] = 0.0f;
373 }
374 }
375
376 if (!is_zero_v3(trans)) {
377 /* move center of view opposite of hand motion
378 * (this is camera mode, not object mode) */
379 sub_v3_v3(rv3d->ofs, trans);
380 has_translate = true;
381 }
382 else {
383 has_translate = false;
384 }
385 }
386
387 if (has_rotate) {
388 float rotation[4];
389 float axis[3];
390 float angle = ndof.time_delta * WM_event_ndof_rotation_get_axis_angle(ndof, axis);
391
392 if (fabsf(angle) > 0.0001f) {
393 has_rotate = true;
394
395 if (use_precision) {
396 angle *= 0.2f;
397 }
398
399 /* transform rotation axis from view to world coordinates */
400 mul_qt_v3(view_inv, axis);
401
402 /* apply rotation to view */
403 axis_angle_to_quat(rotation, axis, angle);
404 mul_qt_qtqt(rv3d->viewquat, rv3d->viewquat, rotation);
405
407 /* force an upright viewpoint
408 * TODO: make this less... sudden */
409 const float horizon_plane[3] = {0.0f, 0.0f, 1.0f};
410 view3d_horizon_correct_quat_ease_out(rv3d->viewquat, horizon_plane, false, 1.0f);
411 }
412
413 rv3d->view = RV3D_VIEW_USER;
414 }
415 else {
416 has_rotate = false;
417 }
418 }
419
420 *r_has_translate = has_translate;
421 *r_has_rotate = has_rotate;
422}
423
425
426/* -------------------------------------------------------------------- */
429
430static bool ndof_orbit_center_is_used_no_viewport()
431{
433 if ((U.ndof_flag & NDOF_ORBIT_CENTER_AUTO) == 0) {
434 return false;
435 }
436 }
437 else {
438 if ((U.ndof_flag & NDOF_FLY_SPEED_AUTO) == 0) {
439 return false;
440 }
441 }
442 return true;
443}
444
445static bool ndof_orbit_center_is_used(const View3D *v3d, const RegionView3D *rv3d)
446{
447 if (!ndof_orbit_center_is_used_no_viewport()) {
448 return false;
449 }
450 if (v3d->ob_center_cursor || v3d->ob_center) {
451 return false;
452 }
453
454 /* Check the caller is not calculating auto-center when there is no reason to do so. */
456 !((rv3d->persp == RV3D_CAMOB) && (v3d->flag2 & V3D_LOCK_CAMERA) == 0),
457 "This test should not run from a camera view unless the camera is locked to the viewport");
458 UNUSED_VARS_NDEBUG(rv3d);
459
460 return true;
461}
462
466static bool ndof_orbit_center_is_valid(const RegionView3D *rv3d, const float3 &center)
467{
468 /* NOTE: this is a fairly arbitrary check mainly to avoid obvious problems
469 * where the orbit center is going to seem buggy/unusable.
470 *
471 * Other cases could also be counted as invalid:
472 * - It's beyond the clip-end.
473 * - It's not inside the viewport frustum (with some margin perhaps).
474 *
475 * The value could also be clamped to make it valid however when function
476 * returns false the #RegionView3D::ofs is used instead, so it's not necessary
477 * to go to great lengths to attempt to use the value.
478 */
479 if (rv3d->is_persp) {
480 const float zfac = mul_project_m4_v3_zfac(rv3d->persmat, center);
481 if (zfac <= 0.0f) {
482 return false;
483 }
484 }
485
486 return true;
487}
488
489static std::optional<float3> ndof_orbit_center_calc_from_bounds(Depsgraph *depsgraph,
490 ScrArea *area,
491 ARegion *region)
492{
493 std::optional<Bounds<float3>> bounding_box = std::nullopt;
494
496 bool do_zoom = false;
497 bounding_box = view3d_calc_minmax_selected(depsgraph, area, region, false, false, &do_zoom);
498 }
499 else {
500 bounding_box = view3d_calc_minmax_visible(depsgraph, area, region, false, false);
501 }
502
503 if (bounding_box.has_value()) {
504 const RegionView3D *rv3d = static_cast<RegionView3D *>(region->regiondata);
505
506 /* Scale down the bounding box to provide some offset */
507 bounding_box->scale_from_center(float3(0.8));
508
509 if (is_bounding_box_in_frustum(rv3d->persmat, *bounding_box)) {
510 /* TODO: for perspective views it would be good to clip the bounds by the
511 * view-point's plane, so the only the portion of the bounds in front of the
512 * view-point is taken into account when calculating the center. */
513 const float3 center = bounding_box->center();
514 if (ndof_orbit_center_is_valid(rv3d, center)) {
515 return center;
516 }
517 }
518 }
519 return std::nullopt;
520}
521
522static float ndof_read_zbuf_rect(ARegion *region, const rcti &rect, int r_xy[2])
523{
524 /* Avoid allocating the whole depth buffer. */
525 ViewDepths depth_temp = {0};
526 rcti rect_clip = rect;
527 view3d_depths_rect_create(region, &rect_clip, &depth_temp);
528
529 /* Find the closest Z pixel. */
530 float depth_near;
531
532 if (r_xy) {
533 depth_near = view3d_depth_near_ex(&depth_temp, r_xy);
534 }
535 else {
536 depth_near = view3d_depth_near(&depth_temp);
537 }
538
539 MEM_SAFE_DELETE(depth_temp.depths);
540
541 return depth_near;
542}
543
550static std::optional<float3> ndof_get_min_depth_pt(ARegion *region, const rcti &rect)
551{
552 int xy[2] = {0, 0};
553 const float depth_near = ndof_read_zbuf_rect(region, rect, xy);
554 if (depth_near == FLT_MAX) {
555 return std::nullopt;
556 }
557 const float3 result = {float(xy[0]), float(xy[1]), depth_near};
558 return result;
559}
560
561static std::optional<float3> ndof_orbit_center_calc_from_zbuf(Depsgraph *depsgraph,
562 ScrArea *area,
563 ARegion *region)
564{
565 rcti sample_rect;
566
568 /* Move the region to the bottom to enhance navigation in architectural-visualization. */
569 sample_rect.xmin = 0.3f * region->winx;
570 sample_rect.xmax = 0.7f * region->winx;
571 sample_rect.ymin = 0.2f * region->winy;
572 sample_rect.ymax = 0.6f * region->winy;
573 }
574 else {
575 int view_center[2] = {region->winx / 2, region->winy / 2};
576 BLI_rcti_init_pt_radius(&sample_rect, view_center, 0.05f * region->winx);
577 }
578
579 const std::optional<float3> min_depth_pt = ndof_get_min_depth_pt(region, sample_rect);
580 if (!min_depth_pt) {
581 return std::nullopt;
582 }
583
584 float3 zbuf_center;
585 if (!ED_view3d_unproject_v3(region, UNPACK3(*min_depth_pt), zbuf_center)) {
586 return std::nullopt;
587 }
588
589 /* Since the center found with Z-buffer might be in some small distance from the mesh
590 * it's safer to scale the bounding box a little before testing if it contains that center. */
591 const float scale_margin = 1.05f;
592
593 /* Use the found center if either #NDOF_ORBIT_CENTER_SELECTED is not enabled,
594 * there are no selected objects center is within bounding box of selected objects. */
596 if ((U.ndof_flag & NDOF_ORBIT_CENTER_SELECTED) == 0) {
597 return zbuf_center;
598 }
599 }
600 else {
601 return zbuf_center;
602 }
603
604 Scene *scene = DEG_get_input_scene(depsgraph);
605 ViewLayer *view_layer = DEG_get_evaluated_view_layer(depsgraph);
606
608 *DEG_get_bmain(depsgraph), scene, view_layer, view_layer->active_collection))
609 {
610 return zbuf_center;
611 }
612
613 View3D *v3d = area->spacedata.first_as<View3D>();
614 if (view3d_calc_point_in_selected_bounds(depsgraph, view_layer, v3d, zbuf_center, scale_margin))
615 {
616 return zbuf_center;
617 }
618
619 return std::nullopt;
620}
621
622static std::optional<float3> ndof_orbit_center_calc(Depsgraph *depsgraph,
623 ScrArea *area,
624 ARegion *region)
625{
626 /* Auto orbit-center implements an intelligent way to dynamically choose the orbit-center
627 * based on objects on the scene and how close to the particular object is the camera.
628 *
629 * Auto center calculation algorithm works as following:
630 * 1) Calculate the bounding box of all objects in the scene
631 * 2) If at least 80% of that box is contained in view-port's camera frustum then:
632 * 2a) Store the center of that bounding box as the orbit-center.
633 * 3) Use Z buffer to find the depth under the middle of the view3d region
634 * 4) If some finite depth value was found then:
635 * 4a) Use that depth to unproject a point from the middle of the region to the 3D space
636 * 4b) Store that point as the Center of Rotation
637 * 5) Since no candidates were found, use the last stored value
638 * (when #RV3D_NDOF_OFS_IS_VALID is set).
639 */
640
641 std::optional<float3> center_test = ndof_orbit_center_calc_from_bounds(depsgraph, area, region);
642 if (!center_test.has_value()) {
643 center_test = ndof_orbit_center_calc_from_zbuf(depsgraph, area, region);
644 }
645 return center_test;
646}
647
649
650/* -------------------------------------------------------------------- */
653
658static wmOperatorStatus view3d_ndof_cameraview_pan_zoom(ViewOpsData *vod,
659 const wmNDOFMotionData &ndof)
660{
661 View3D *v3d = vod->v3d;
662 ARegion *region = vod->region;
663 RegionView3D *rv3d = vod->rv3d;
664
665 if (v3d->camera && (rv3d->persp == RV3D_CAMOB) && (v3d->flag2 & V3D_LOCK_CAMERA) == 0) {
666 /* pass */
667 }
668 else {
670 }
671
672 float3 pan_vec = WM_event_ndof_translation_get_for_navigation(ndof);
673 const float pan_speed = NDOF_PIXELS_PER_SECOND;
674 const bool has_translate = !is_zero_v2(pan_vec);
675 const bool has_zoom = pan_vec[2] != 0.0f;
676
677 pan_vec *= ndof.time_delta;
678
679 /* NOTE: unlike image and clip views, the 2D pan doesn't have to be scaled by the zoom level.
680 * #ED_view3d_camera_view_pan already takes the zoom level into account. */
681 mul_v2_fl(pan_vec, pan_speed);
682
683 /* NOTE(@ideasman42): In principle rotating could pass through to regular
684 * non-camera NDOF behavior (exiting the camera-view and rotating).
685 * Disabled this block since in practice it's difficult to control NDOF devices
686 * to perform some rotation with absolutely no translation. Causing rotation to
687 * randomly exit from the user perspective. Adjusting the dead-zone could avoid
688 * the motion feeling *glitchy* although in my own tests even then it didn't work reliably.
689 * Leave rotating out of camera-view disabled unless it can be made to work reliably. */
690 if (!(has_translate || has_zoom)) {
691 // return OPERATOR_PASS_THROUGH;
692 }
693
694 bool changed = false;
695
696 if (has_translate) {
697 /* Use the X & Y of `pan_vec`. */
698 if (ED_view3d_camera_view_pan(region, pan_vec)) {
699 changed = true;
700 }
701 }
702
703 if (has_zoom) {
704 if (ED_view3d_camera_view_zoom_scale(rv3d, max_ff(0.0f, 1.0f - pan_vec[2]))) {
705 changed = true;
706 }
707 }
708
709 if (changed) {
710 ED_region_tag_redraw(region);
711 return OPERATOR_FINISHED;
712 }
713 return OPERATOR_CANCELLED;
714}
715
717
718/* -------------------------------------------------------------------- */
721
722static wmOperatorStatus ndof_orbit_invoke_impl(bContext *C,
723 ViewOpsData *vod,
724 const wmEvent *event,
725 PointerRNA * /*ptr*/)
726{
727 if (event->type != NDOF_MOTION) {
728 return OPERATOR_CANCELLED;
729 }
730
731 const Depsgraph *depsgraph = vod->depsgraph;
732 View3D *v3d = vod->v3d;
733 RegionView3D *rv3d = vod->rv3d;
734 char xform_flag = 0;
735
736 const wmNDOFMotionData &ndof = *static_cast<const wmNDOFMotionData *>(event->customdata);
737
738 /* off by default, until changed later this function */
739 rv3d->ndof_rot_angle = 0.0f;
740
741 if (ndof.progress != P_FINISHING) {
742 const bool has_rotation = ndof_has_rotate(ndof, rv3d);
743 /* if we can't rotate, fall back to translate (locked axis views) */
744 const bool has_translate = (RV3D_LOCK_FLAGS(rv3d) & RV3D_LOCK_ROTATION) &&
745 ndof_has_translate(ndof, v3d, rv3d);
746 const bool has_zoom = (rv3d->is_persp == false) && WM_event_ndof_translation_has_zoom(ndof);
747
748 if (has_translate || has_zoom) {
749 view3d_ndof_pan_zoom(ndof, vod->area, vod->region, has_translate, has_zoom);
750 xform_flag |= HAS_TRANSLATE;
751 }
752
753 if (has_rotation) {
754 view3d_ndof_orbit(ndof, vod->area, vod->region, vod, true);
755 xform_flag |= HAS_ROTATE;
756 }
757 }
758
759 ED_view3d_camera_lock_sync(depsgraph, v3d, rv3d);
760 if (xform_flag) {
762 v3d, rv3d, C, xform_flag & HAS_ROTATE, xform_flag & HAS_TRANSLATE);
763 }
764
765 ED_region_tag_redraw(vod->region);
766
767 return OPERATOR_FINISHED;
768}
769
770static wmOperatorStatus ndof_orbit_invoke(bContext *C, wmOperator *op, const wmEvent *event)
771{
772 if (event->type != NDOF_MOTION) {
773 return OPERATOR_CANCELLED;
774 }
775
776 return view3d_navigate_invoke_impl(C, op, event, &ViewOpsType_ndof_orbit);
777}
778
779void VIEW3D_OT_ndof_orbit(wmOperatorType *ot)
780{
781 /* identifiers */
782 ot->name = "NDOF Orbit View";
783 ot->description = "Orbit the view using the 3D mouse";
784 ot->idname = ViewOpsType_ndof_orbit.idname;
785
786 /* API callbacks. */
787 ot->invoke = ndof_orbit_invoke;
788 ot->poll = ED_operator_view3d_active;
789
790 /* flags */
791 ot->flag = 0;
792}
793
795
796/* -------------------------------------------------------------------- */
799
800static wmOperatorStatus ndof_orbit_zoom_invoke_impl(bContext *C,
801 ViewOpsData *vod,
802 const wmEvent *event,
803 PointerRNA * /*ptr*/)
804{
805 if (event->type != NDOF_MOTION) {
806 return OPERATOR_CANCELLED;
807 }
808
809 const wmNDOFMotionData &ndof = *static_cast<const wmNDOFMotionData *>(event->customdata);
810
811 if (U.ndof_flag & NDOF_CAMERA_PAN_ZOOM) {
812 const wmOperatorStatus camera_retval = view3d_ndof_cameraview_pan_zoom(vod, ndof);
813 if (camera_retval != OPERATOR_PASS_THROUGH) {
814 return camera_retval;
815 }
816 }
817
818 View3D *v3d = vod->v3d;
819 RegionView3D *rv3d = vod->rv3d;
820 char xform_flag = 0;
821
822 /* off by default, until changed later this function */
823 rv3d->ndof_rot_angle = 0.0f;
824
825 if (ndof.progress == P_FINISHING) {
826 /* pass */
827 }
828 else if (ndof.progress == P_STARTING) {
829 if (ndof_orbit_center_is_used(v3d, rv3d)) {
830 /* If center was recalculated then update the point location for drawing. */
831 if (std::optional<float3> center_test = ndof_orbit_center_calc(
832 vod->depsgraph, vod->area, vod->region))
833 {
834 negate_v3_v3(rv3d->ndof_ofs, center_test.value());
835 /* When `ndof_ofs` is set `rv3d->dist` should be set based on distance to `ndof_ofs`.
836 * Without this the user is unable to zoom to the `ndof_ofs` point. See: #134732. */
837 if (rv3d->is_persp) {
838 const float dist_min = ED_view3d_dist_soft_min_get(v3d, true);
839 if (!ED_view3d_distance_set_from_location(rv3d, center_test.value(), dist_min)) {
840 ED_view3d_distance_set(rv3d, dist_min);
841 }
842 }
843 rv3d->ndof_flag |= RV3D_NDOF_OFS_IS_VALID;
844 }
845 }
846 }
847 else if ((rv3d->persp == RV3D_ORTHO) && RV3D_VIEW_IS_AXIS(rv3d->view)) {
848 /* if we can't rotate, fall back to translate (locked axis views) */
849 const bool has_translate = ndof_has_translate(ndof, v3d, rv3d);
850 const bool has_zoom = WM_event_ndof_translation_has_zoom(ndof) &&
852
853 if (has_translate || has_zoom) {
854 view3d_ndof_pan_zoom(ndof, vod->area, vod->region, has_translate, true);
855 xform_flag |= HAS_TRANSLATE;
856 }
857 }
858 else {
859 /* NOTE: based on feedback from #67579, users want to have pan and orbit enabled at once.
860 * It's arguable that orbit shouldn't pan (since we have a pan only operator),
861 * so if there are users who like to separate orbit/pan operations - it can be a preference. */
862 const bool is_orbit_around_pivot = NDOF_IS_ORBIT_AROUND_CENTER_MODE(&U) ||
864 const bool has_rotation = ndof_has_rotate(ndof, rv3d);
865 bool has_translate, has_zoom;
866
867 if (is_orbit_around_pivot) {
868 /* Orbit preference or forced lock (Z zooms). */
869 has_translate = ndof_has_translate_pan(ndof, v3d, rv3d);
870 has_zoom = WM_event_ndof_translation_has_zoom(ndof);
871 }
872 else {
873 /* Free preference (Z translates). */
874 has_translate = ndof_has_translate(ndof, v3d, rv3d);
875 has_zoom = false;
876 }
877
878 /* Rotation first because dynamic offset resets offset otherwise (and disables panning). */
879 if (has_rotation) {
880 const float dist_backup = rv3d->dist;
881 if (!is_orbit_around_pivot) {
882 ED_view3d_distance_set(rv3d, 0.0f);
883 }
884 view3d_ndof_orbit(ndof, vod->area, vod->region, vod, is_orbit_around_pivot);
885 xform_flag |= HAS_ROTATE;
886 if (!is_orbit_around_pivot) {
887 ED_view3d_distance_set(rv3d, dist_backup);
888 }
889 }
890
891 if (has_translate || has_zoom) {
892 view3d_ndof_pan_zoom(ndof, vod->area, vod->region, has_translate, has_zoom);
893 xform_flag |= HAS_TRANSLATE;
894 }
895 }
896
897 ED_view3d_camera_lock_sync(vod->depsgraph, v3d, rv3d);
898 if (xform_flag) {
900 v3d, rv3d, C, xform_flag & HAS_ROTATE, xform_flag & HAS_TRANSLATE);
901 }
902
903 ED_region_tag_redraw(vod->region);
904
905 return OPERATOR_FINISHED;
906}
907
908static wmOperatorStatus ndof_orbit_zoom_invoke(bContext *C, wmOperator *op, const wmEvent *event)
909{
910 if (event->type != NDOF_MOTION) {
911 return OPERATOR_CANCELLED;
912 }
913
914 return view3d_navigate_invoke_impl(C, op, event, &ViewOpsType_ndof_orbit_zoom);
915}
916
917void VIEW3D_OT_ndof_orbit_zoom(wmOperatorType *ot)
918{
919 /* identifiers */
920 ot->name = "NDOF Orbit View with Zoom";
921 ot->description = "Orbit and zoom the view using the 3D mouse";
922 ot->idname = ViewOpsType_ndof_orbit_zoom.idname;
923
924 /* API callbacks. */
925 ot->invoke = ndof_orbit_zoom_invoke;
926 ot->poll = ED_operator_view3d_active;
927
928 /* flags */
929 ot->flag = 0;
930}
931
933
934/* -------------------------------------------------------------------- */
937
938static wmOperatorStatus ndof_pan_invoke_impl(bContext *C,
939 ViewOpsData *vod,
940 const wmEvent *event,
941 PointerRNA * /*ptr*/)
942{
943 if (event->type != NDOF_MOTION) {
944 return OPERATOR_CANCELLED;
945 }
946
947 const wmNDOFMotionData &ndof = *static_cast<const wmNDOFMotionData *>(event->customdata);
948
949 if (U.ndof_flag & NDOF_CAMERA_PAN_ZOOM) {
950 const wmOperatorStatus camera_retval = view3d_ndof_cameraview_pan_zoom(vod, ndof);
951 if (camera_retval != OPERATOR_PASS_THROUGH) {
952 return camera_retval;
953 }
954 }
955
956 const Depsgraph *depsgraph = vod->depsgraph;
957 View3D *v3d = vod->v3d;
958 RegionView3D *rv3d = vod->rv3d;
959 ARegion *region = vod->region;
960 char xform_flag = 0;
961
962 const bool has_translate = ndof_has_translate(ndof, v3d, rv3d);
963 const bool has_zoom = (rv3d->is_persp == false) && WM_event_ndof_translation_has_zoom(ndof);
964
965 /* we're panning here! so erase any leftover rotation from other operators */
966 rv3d->ndof_rot_angle = 0.0f;
967
968 if (!(has_translate || has_zoom)) {
969 return OPERATOR_CANCELLED;
970 }
971
972 ED_view3d_camera_lock_init_ex(depsgraph, v3d, rv3d, false);
973
974 if (ndof.progress != P_FINISHING) {
975 ScrArea *area = vod->area;
976
977 if (has_translate || has_zoom) {
978 view3d_ndof_pan_zoom(ndof, area, region, has_translate, has_zoom);
979 xform_flag |= HAS_TRANSLATE;
980 }
981 }
982
983 ED_view3d_camera_lock_sync(depsgraph, v3d, rv3d);
984 if (xform_flag) {
985 ED_view3d_camera_lock_autokey(v3d, rv3d, C, false, xform_flag & HAS_TRANSLATE);
986 }
987
988 ED_region_tag_redraw(region);
989
990 return OPERATOR_FINISHED;
991}
992
993static wmOperatorStatus ndof_pan_invoke(bContext *C, wmOperator *op, const wmEvent *event)
994{
995 if (event->type != NDOF_MOTION) {
996 return OPERATOR_CANCELLED;
997 }
998
999 return view3d_navigate_invoke_impl(C, op, event, &ViewOpsType_ndof_pan);
1000}
1001
1002void VIEW3D_OT_ndof_pan(wmOperatorType *ot)
1003{
1004 /* identifiers */
1005 ot->name = "NDOF Pan View";
1006 ot->description = "Pan the view with the 3D mouse";
1007 ot->idname = ViewOpsType_ndof_pan.idname;
1008
1009 /* API callbacks. */
1010 ot->invoke = ndof_pan_invoke;
1011 ot->poll = ED_operator_view3d_active;
1012
1013 /* flags */
1014 ot->flag = 0;
1015}
1016
1018
1019/* -------------------------------------------------------------------- */
1022
1026static wmOperatorStatus ndof_all_invoke_impl(bContext *C,
1027 ViewOpsData *vod,
1028 const wmEvent *event,
1029 PointerRNA * /*ptr*/)
1030{
1031
1033
1034 /* weak!, but it works */
1035 const eNdof_Navigation_Mode ndof_navigation_mode_backup = U.ndof_navigation_mode;
1036 U.ndof_navigation_mode = NDOF_NAVIGATION_MODE_FLY;
1037
1038 ret = ndof_orbit_zoom_invoke_impl(C, vod, event, nullptr);
1039
1040 U.ndof_navigation_mode = ndof_navigation_mode_backup;
1041
1042 return ret;
1043}
1044
1045static wmOperatorStatus ndof_all_invoke(bContext *C, wmOperator *op, const wmEvent *event)
1046{
1047 if (event->type != NDOF_MOTION) {
1048 return OPERATOR_CANCELLED;
1049 }
1050
1051 return view3d_navigate_invoke_impl(C, op, event, &ViewOpsType_ndof_all);
1052}
1053
1054void VIEW3D_OT_ndof_all(wmOperatorType *ot)
1055{
1056 /* identifiers */
1057 ot->name = "NDOF Transform View";
1058 ot->description = "Pan and rotate the view with the 3D mouse";
1059 ot->idname = ViewOpsType_ndof_all.idname;
1060
1061 /* API callbacks. */
1062 ot->invoke = ndof_all_invoke;
1063 ot->poll = ED_operator_view3d_active;
1064
1065 /* flags */
1066 ot->flag = 0;
1067}
1068
1069const ViewOpsType ViewOpsType_ndof_orbit = {
1071 /*idname*/ "VIEW3D_OT_ndof_orbit",
1072 /*poll_fn*/ nullptr,
1073 /*init_fn*/ ndof_orbit_invoke_impl,
1074 /*apply_fn*/ nullptr,
1075};
1076
1077const ViewOpsType ViewOpsType_ndof_orbit_zoom = {
1079 /*idname*/ "VIEW3D_OT_ndof_orbit_zoom",
1080 /*poll_fn*/ nullptr,
1081 /*init_fn*/ ndof_orbit_zoom_invoke_impl,
1082 /*apply_fn*/ nullptr,
1083};
1084
1085const ViewOpsType ViewOpsType_ndof_pan = {
1086 /*flag*/ VIEWOPS_FLAG_NONE,
1087 /*idname*/ "VIEW3D_OT_ndof_pan",
1088 /*poll_fn*/ nullptr,
1089 /*init_fn*/ ndof_pan_invoke_impl,
1090 /*apply_fn*/ nullptr,
1091};
1092
1093const ViewOpsType ViewOpsType_ndof_all = {
1095 /*idname*/ "VIEW3D_OT_ndof_all",
1096 /*poll_fn*/ nullptr,
1097 /*init_fn*/ ndof_all_invoke_impl,
1098 /*apply_fn*/ nullptr,
1099};
1100
1101#endif /* WITH_INPUT_NDOF */
1102
1104
1105} // namespace blender
#define ISECT_AABB_PLANE_IN_FRONT_ALL
#define NDOF_IS_ORBIT_AROUND_CENTER_MODE(userdef)
#define NDOF_PIXELS_PER_SECOND
#define NDOF_IS_HORIZON_LOCKED(userdef)
#define RV3D_VIEW_IS_AXIS(view)
#define RV3D_LOCK_FLAGS(rv3d)
#define C
Definition RandGen.cpp:29
#define UNUSED_VARS_NDEBUG(...)
#define UNPACK3(a)
#define BLI_assert_msg(a, msg)
SIMD_FORCE_INLINE btScalar angle(const btVector3 &v) const
Return the angle between this and another vector.
Definition btVector3.h:356
#define rot(x, k)
#define FLT_MAX
BMesh Iterators.
bool ED_view3d_camera_lock_autokey(View3D *v3d, RegionView3D *rv3d, bContext *C, bool do_rotate, bool do_translate)
UserDef U
Definition blender.cc:58
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
MINLINE void negate_v3_v3(float r[3], const float a[3])
MINLINE bool is_zero_v2(const float v[2]) ATTR_WARN_UNUSED_RESULT
MINLINE void copy_v3_v3(float r[3], const float a[3])
void viewrotate_apply_dyn_ofs(ViewOpsData *vod, const float viewquat_new[4])
int isect_aabb_planes_v3(const float(*planes)[4], int totplane, const float bbmin[3], const float bbmax[3])
void ED_view3d_distance_set(RegionView3D *rv3d, float dist)
MINLINE void mul_v2_fl(float r[2], float f)
bool ED_view3d_camera_view_zoom_scale(RegionView3D *rv3d, const float scale)
@ NDOF_ORBIT_CENTER_SELECTED
void view3d_depths_rect_create(ARegion *region, rcti *rect, ViewDepths *r_d)
MINLINE float mul_project_m4_v3_zfac(const float mat[4][4], const float co[3]) ATTR_WARN_UNUSED_RESULT
bool ED_view3d_persp_ensure(const Depsgraph *depsgraph, View3D *v3d, ARegion *region)
void planes_from_projmat(const float mat[4][4], float left[4], float right[4], float bottom[4], float top[4], float near[4], float far[4])
float view3d_depth_near_ex(ViewDepths *d, int r_xy[2])
bool ED_view3d_camera_lock_sync(const Depsgraph *depsgraph, View3D *v3d, RegionView3D *rv3d)
void axis_angle_to_quat(float r[4], const float axis[3], float angle)
bool ED_view3d_distance_set_from_location(RegionView3D *rv3d, const float dist_co[3], float dist_min)
void mul_qt_qtqt(float q[4], const float a[4], const float b[4])
void ED_region_tag_redraw(ARegion *region)
Definition area.cc:681
bool ED_view3d_unproject_v3(const ARegion *region, float regionx, float regiony, float regionz, float world[3])
float ED_view3d_dist_soft_min_get(const View3D *v3d, bool use_persp_range)
MINLINE void sub_v3_v3(float r[3], const float a[3])
void mul_qt_v3(const float q[4], float r[3])
wmOperatorStatus view3d_navigate_invoke_impl(bContext *C, wmOperator *op, const wmEvent *event, const ViewOpsType *nav_type)
Main * DEG_get_bmain(const Depsgraph *graph)
bool ED_view3d_camera_view_pan(ARegion *region, const float event_ofs[2])
void axis_angle_to_quat_single(float q[4], char axis, float angle)
bool view3d_calc_point_in_selected_bounds(Depsgraph *depsgraph, struct ViewLayer *view_layer_eval, const View3D *v3d, const float3 &point, const float scale_margin)
ViewLayer * DEG_get_evaluated_view_layer(const Depsgraph *graph)
bool ED_operator_view3d_active(bContext *C)
float ED_view3d_calc_zfac(const RegionView3D *rv3d, const float co[3])
std::optional< Bounds< float3 > > view3d_calc_minmax_visible(Depsgraph *depsgraph, ScrArea *area, ARegion *region, bool use_all_regions, bool clip_bounds)
std::optional< Bounds< float3 > > view3d_calc_minmax_selected(Depsgraph *depsgraph, ScrArea *area, ARegion *region, bool use_all_regions, bool clip_bounds, bool *r_do_zoom)
@ VIEWOPS_FLAG_ORBIT_SELECT
void mul_mat3_m4_v3(const float mat[4][4], float r[3])
bool ED_view3d_offset_lock_check(const View3D *v3d, const RegionView3D *rv3d)
MINLINE void copy_v3_fl3(float v[3], float x, float y, float z)
@ NDOF_NAVIGATION_MODE_DRONE
MINLINE float max_ff(float a, float b)
void ED_view3d_camera_lock_init_ex(const Depsgraph *depsgraph, View3D *v3d, RegionView3D *rv3d, bool calc_dist)
Scene * DEG_get_input_scene(const Depsgraph *graph)
@ P_STARTING
Definition WM_types.hh:913
@ P_FINISHING
Definition WM_types.hh:917
float view3d_depth_near(ViewDepths *d)
void invert_qt_qt_normalized(float q1[4], const float q2[4])
VecBase< float, 3 > float3
void BLI_rcti_init_pt_radius(struct rcti *rect, const int xy[2], int size)
Definition rct.cc:480
void view3d_boxview_sync(ScrArea *area, ARegion *region)
float view3d_horizon_correct_quat(float quat[4], const float horizon_plane[3], const bool horizon_plane_no_flip, const float axis_fallback[3], const float angle_target, const float factor)
bool BKE_layer_collection_has_selected_objects(const Main &bmain, const Scene *scene, ViewLayer *view_layer, LayerCollection *lc)
MINLINE bool is_zero_v3(const float v[3]) ATTR_WARN_UNUSED_RESULT
float view3d_horizon_correct_quat_ease_out(float quat[4], const float horizon_plane[3], const bool horizon_plane_no_flip, const float factor)
#define fabsf
ccl_device_inline int4 rect_clip(const int4 a, const int4 b)
Definition rect.h:26
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
float z
Definition sky_math.h:136
float y
Definition sky_math.h:136