[Bf-blender-cvs] [58ecc0d1bf1] xr-actions-D9124: XR: Add "constraint" objects to session settings

Peter Kim noreply at git.blender.org
Sat Oct 31 15:38:57 CET 2020


Commit: 58ecc0d1bf1758cb8b771370665ce6456d4e8067
Author: Peter Kim
Date:   Sat Oct 31 23:00:43 2020 +0900
Branches: xr-actions-D9124
https://developer.blender.org/rB58ecc0d1bf1758cb8b771370665ce6456d4e8067

XR: Add "constraint" objects to session settings

These objects (3 in total, corresponding to headset and left/right
controllers) will have their location and rotation determined by the
associated XR pose. This provides a way for add-ons to achieve
this "VR constraint" behavior without the need to constantly query
pose information.

===================================================================

M	source/blender/blenloader/intern/readfile.c
M	source/blender/makesdna/DNA_xr_types.h
M	source/blender/makesrna/intern/rna_xr.c
M	source/blender/windowmanager/xr/intern/wm_xr_session.c

===================================================================

diff --git a/source/blender/blenloader/intern/readfile.c b/source/blender/blenloader/intern/readfile.c
index c49808c3718..c982d6f280c 100644
--- a/source/blender/blenloader/intern/readfile.c
+++ b/source/blender/blenloader/intern/readfile.c
@@ -5761,6 +5761,15 @@ static void lib_link_wm_xr_data_restore(struct IDNameLib_Map *id_map, wmXrData *
 {
   xr_data->session_settings.base_pose_object = restore_pointer_by_name(
       id_map, (ID *)xr_data->session_settings.base_pose_object, USER_REAL);
+
+  xr_data->session_settings.headset_object = restore_pointer_by_name(
+      id_map, (ID *)xr_data->session_settings.headset_object, USER_REAL);
+
+  xr_data->session_settings.controller0_object = restore_pointer_by_name(
+      id_map, (ID *)xr_data->session_settings.controller0_object, USER_REAL);
+
+  xr_data->session_settings.controller1_object = restore_pointer_by_name(
+      id_map, (ID *)xr_data->session_settings.controller1_object, USER_REAL);
 }
 
 static void lib_link_window_scene_data_restore(wmWindow *win, Scene *scene, ViewLayer *view_layer)
diff --git a/source/blender/makesdna/DNA_xr_types.h b/source/blender/makesdna/DNA_xr_types.h
index 1a3f4a09c70..1e8aefbe532 100644
--- a/source/blender/makesdna/DNA_xr_types.h
+++ b/source/blender/makesdna/DNA_xr_types.h
@@ -46,6 +46,12 @@ typedef struct XrSessionSettings {
   float clip_start, clip_end;
 
   int flag;
+
+  /** Objects whose location and rotation will be constrained to the XR headset / controller poses.
+   */
+  Object *headset_object;
+  Object *controller0_object;
+  Object *controller1_object;
 } XrSessionSettings;
 
 typedef enum eXrSessionFlag {
diff --git a/source/blender/makesrna/intern/rna_xr.c b/source/blender/makesrna/intern/rna_xr.c
index 61d6112d3af..dd118dd0728 100644
--- a/source/blender/makesrna/intern/rna_xr.c
+++ b/source/blender/makesrna/intern/rna_xr.c
@@ -567,6 +567,30 @@ static void rna_def_xr_session_settings(BlenderRNA *brna)
       "Positional Tracking",
       "Allow VR headsets to affect the location in virtual space, in addition to the rotation");
   RNA_def_property_update(prop, NC_WM | ND_XR_DATA_CHANGED, NULL);
+
+  prop = RNA_def_property(srna, "headset_object", PROP_POINTER, PROP_NONE);
+  RNA_def_property_flag(prop, PROP_EDITABLE);
+  RNA_def_property_ui_text(
+      prop,
+      "Headset Object",
+      "Object whose location and rotation will be constrained to the VR headset's pose");
+  RNA_def_property_update(prop, NC_WM | ND_XR_DATA_CHANGED, NULL);
+
+  prop = RNA_def_property(srna, "controller0_object", PROP_POINTER, PROP_NONE);
+  RNA_def_property_flag(prop, PROP_EDITABLE);
+  RNA_def_property_ui_text(
+      prop,
+      "Controller 0 Object",
+      "Object whose location and rotation will be constrained to the first VR controller's pose");
+  RNA_def_property_update(prop, NC_WM | ND_XR_DATA_CHANGED, NULL);
+
+  prop = RNA_def_property(srna, "controller1_object", PROP_POINTER, PROP_NONE);
+  RNA_def_property_flag(prop, PROP_EDITABLE);
+  RNA_def_property_ui_text(
+      prop,
+      "Controller 1 Object",
+      "Object whose location and rotation will be constrained to the second VR controller's pose");
+  RNA_def_property_update(prop, NC_WM | ND_XR_DATA_CHANGED, NULL);
 }
 
 static void rna_def_xr_session_state(BlenderRNA *brna)
diff --git a/source/blender/windowmanager/xr/intern/wm_xr_session.c b/source/blender/windowmanager/xr/intern/wm_xr_session.c
index 78ce6c0957c..d44bdf194dd 100644
--- a/source/blender/windowmanager/xr/intern/wm_xr_session.c
+++ b/source/blender/windowmanager/xr/intern/wm_xr_session.c
@@ -331,6 +331,7 @@ void wm_xr_session_state_update(const XrSessionSettings *settings,
   GHOST_XrPose viewer_pose;
   const bool use_position_tracking = settings->flag & XR_SESSION_USE_POSITION_TRACKING;
   wmXrEyeData *eye = &state->eyes[draw_view->view];
+  Object *ob_constraint = settings->headset_object;
 
   mul_qt_qtqt(viewer_pose.orientation_quat,
               draw_data->base_pose.orientation_quat,
@@ -351,6 +352,12 @@ void wm_xr_session_state_update(const XrSessionSettings *settings,
   copy_qt_qt(state->viewer_pose.orientation_quat, viewer_pose.orientation_quat);
   wm_xr_pose_to_viewmat(&viewer_pose, state->viewer_viewmat);
 
+  if (ob_constraint) {
+    copy_v3_v3(ob_constraint->loc, viewer_pose.position);
+    quat_to_eul(ob_constraint->rot, viewer_pose.orientation_quat);
+    DEG_id_tag_update(&ob_constraint->id, ID_RECALC_TRANSFORM);
+  }
+
   eye->width = draw_view->width;
   eye->height = draw_view->height;
   /* No idea why, but multiplying by two seems to make it match the VR view more. */
@@ -496,7 +503,7 @@ static void wm_xr_session_controller_mats_update(const XrSessionSettings *settin
                                                  ViewLayer *view_layer)
 {
   const unsigned int count = (unsigned int)min_ii(
-      (int)controller_pose_action->count_subaction_paths, 2);
+      (int)controller_pose_action->count_subaction_paths, ARRAY_SIZE(state->controllers));
 
   float view_ofs[3];
   float base_inv[4][4];
@@ -512,6 +519,7 @@ static void wm_xr_session_controller_mats_update(const XrSessionSettings *settin
 
   for (unsigned int i = 0; i < count; ++i) {
     wmXrControllerData *controller = &state->controllers[i];
+    Object *ob_constraint = (i == 0) ? settings->controller0_object : settings->controller1_object;
 
     /* Calculate controller matrix in world space. */
     wm_xr_controller_pose_to_mat(&((GHOST_XrPose *)controller_pose_action->states)[i], tmp);
@@ -524,6 +532,13 @@ static void wm_xr_session_controller_mats_update(const XrSessionSettings *settin
     mat4_to_loc_quat(
         controller->pose.position, controller->pose.orientation_quat, controller->mat);
 
+    if (ob_constraint) {
+      copy_v3_v3(ob_constraint->loc, controller->pose.position);
+      quat_to_eul(ob_constraint->rot, controller->pose.orientation_quat);
+      DEG_id_tag_update(&ob_constraint->id, ID_RECALC_TRANSFORM);
+    }
+
+    /* Update controller representation object. */
     if (controller->ob) {
       /* TODO_XR: Handle case where object was deleted but then undone. */
       Base *base = BKE_view_layer_base_find(view_layer, controller->ob);



More information about the Bf-blender-cvs mailing list