Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender-addons.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJulian Eisel <julian@blender.org>2020-09-02 22:58:28 +0300
committerJulian Eisel <julian@blender.org>2020-09-02 23:04:18 +0300
commit1346083086ab89851b2aa8d8568b643804f9cb34 (patch)
treed31c09b6d36b34b647c94829b3fdb6dc730eec98 /viewport_vr_preview.py
parent88685da2c5d21cbde0412dba98f65b05f0d0bba0 (diff)
VR Scene Inspection: Better way to calculate landmark angle from viewer
When using the "Add VR Landmark from Session" operator, we want to calculate an angle around the Z-axis for the landmark to use as ground-aligned base rotation. The former way to calculate this was rather primitive and broke easily (angle would point in the opposite direction). Now we project the camera direction onto the ground plane to get the angle. This should be a very reliable way to do it.
Diffstat (limited to 'viewport_vr_preview.py')
-rw-r--r--viewport_vr_preview.py26
1 files changed, 23 insertions, 3 deletions
diff --git a/viewport_vr_preview.py b/viewport_vr_preview.py
index 44e8897b..95303819 100644
--- a/viewport_vr_preview.py
+++ b/viewport_vr_preview.py
@@ -407,20 +407,40 @@ class VIEW3D_OT_vr_landmark_from_session(Operator):
def poll(cls, context):
return bpy.types.XrSessionState.is_running(context)
+ @staticmethod
+ def _calc_landmark_angle_from_viewer_rotation(rot):
+ from mathutils import Vector
+
+ # We want an angle around Z based on the current viewer rotation. Idea
+ # is to create a vector from the viewer rotation, project that onto a
+ # Z-Up plane and use the resulting vector to get an angle around Z.
+
+ view_rot_vec = Vector((0, 0, 1))
+ view_rot_vec.rotate(rot)
+ angle_vec = view_rot_vec - view_rot_vec.project(Vector((0, 0, 1)))
+
+ # We could probably use a 3D version of Vector.angle_signed() here, but
+ # that's not available. So manually calculate it via a quaternion delta.
+ forward_vec = Vector((0, -1, 0))
+ diff = angle_vec.rotation_difference(forward_vec)
+
+ return diff.angle * -diff.axis[2]
+
def execute(self, context):
scene = context.scene
landmarks = scene.vr_landmarks
wm = context.window_manager
lm = landmarks.add()
- lm.type = "CUSTOM"
+ lm.type = 'CUSTOM'
scene.vr_landmarks_selected = len(landmarks) - 1
loc = wm.xr_session_state.viewer_pose_location
- rot = wm.xr_session_state.viewer_pose_rotation.to_euler()
+ rot = wm.xr_session_state.viewer_pose_rotation
+ angle = self._calc_landmark_angle_from_viewer_rotation(rot)
lm.base_pose_location = loc
- lm.base_pose_angle = rot[2]
+ lm.base_pose_angle = angle
return {'FINISHED'}