From 0fccd9e89222ef5b872f03d0749c65663a5f8948 Mon Sep 17 00:00:00 2001 From: SDraw Date: Wed, 13 Sep 2023 19:03:49 +0300 Subject: [PATCH] Update to Ultraleap Unity Plugin 6.12.0 --- ml_lme/vendor/LeapCSharp/Device.cs | 65 ++++++++++++------- .../vendor/LeapCSharp/TransformExtensions.cs | 22 +++++++ 2 files changed, 64 insertions(+), 23 deletions(-) diff --git a/ml_lme/vendor/LeapCSharp/Device.cs b/ml_lme/vendor/LeapCSharp/Device.cs index 0f77f70..6c3b5f5 100644 --- a/ml_lme/vendor/LeapCSharp/Device.cs +++ b/ml_lme/vendor/LeapCSharp/Device.cs @@ -263,33 +263,52 @@ namespace Leap return devicePose; } - //float[] data = new float[16]; - //eLeapRS result = LeapC.GetDeviceTransform(Handle, data); + float[] data = new float[16]; + eLeapRS result = LeapC.GetDeviceTransform(Handle, data); - //if (result != eLeapRS.eLeapRS_Success || data == null) - //{ - // devicePose = Pose.identity; - // return devicePose; - //} + if (result != eLeapRS.eLeapRS_Success || data == null) + { + devicePose = Pose.identity; + poseSet = true; + return Pose.identity; + } - //// Get transform matrix and convert to unity space by inverting Z. - //Matrix4x4 transformMatrix = new Matrix4x4( - // new Vector4(data[0], data[1], data[2], data[3]), - // new Vector4(data[4], data[5], data[6], data[7]), - // new Vector4(data[8], data[9], data[10], data[11]), - // new Vector4(data[12], data[13], data[14], data[15])); - //Matrix4x4 toUnity = Matrix4x4.Scale(new Vector3(1, 1, -1)); - //transformMatrix = toUnity * transformMatrix; + // Using the LEAP->OPENXR device transform matrix + // Unitys matrices are generated as 4 columns: + Matrix4x4 deviceTransform = new Matrix4x4( + new Vector4(data[0], data[1], data[2], data[3]), + new Vector4(data[4], data[5], data[6], data[7]), + new Vector4(data[8], data[9], data[10], data[11]), + new Vector4(data[12], data[13], data[14], data[15])); - //// Identity matrix here means we have no device transform, also check validity. - //if (transformMatrix.isIdentity || !transformMatrix.ValidTRS()) - //{ - // devicePose = Pose.identity; - // return devicePose; - //} - //// Return the valid pose - //devicePose = new Pose(transformMatrix.GetColumn(3), transformMatrix.rotation); + // An example of the expected matrix if it were 8cm forward from the head origin + // Unitys matrices are generated as 4 columns: + //Matrix4x4 deviceTransform = new Matrix4x4( + // new Vector4(-0.001f, 0, 0, 0), + // new Vector4(0, 0, -0.001f, 0), + // new Vector4(0, -0.001f, 0, 0), + // new Vector4(0, 0, -0.08f, 1)); + + if (deviceTransform == Matrix4x4.identity) + { + devicePose = Pose.identity; + poseSet = true; + return Pose.identity; + } + + Matrix4x4 openXRToUnity = new Matrix4x4( + new Vector4(1f, 0, 0, 0), + new Vector4(0, 1f, 0, 0), + new Vector4(0, 0, -1f, 0), + new Vector4(0, 0, 0, 1)); + + deviceTransform = openXRToUnity * deviceTransform; + + Vector3 outputPos = deviceTransform.GetPosition(); + //Quaternion outputRot = deviceTransform.rotation; // Note: the matrices we receive are not rotatrion matrices. This produces unexpected results + + devicePose = new Pose(outputPos, Quaternion.identity); poseSet = true; return devicePose; diff --git a/ml_lme/vendor/LeapCSharp/TransformExtensions.cs b/ml_lme/vendor/LeapCSharp/TransformExtensions.cs index 6421247..49575dc 100644 --- a/ml_lme/vendor/LeapCSharp/TransformExtensions.cs +++ b/ml_lme/vendor/LeapCSharp/TransformExtensions.cs @@ -41,6 +41,17 @@ namespace Leap return new Frame().CopyFrom(frame).Transform(transform); } + /** + * Returns a new frame that is a copy of a frame, with an additional rigid + * transformation applied to it. + * + * @param transform The transformation to be applied to the copied frame. + */ + public static Frame TransformedCopy(this Frame frame, Vector3 position, Quaternion rotation) + { + return new Frame().CopyFrom(frame).Transform(new LeapTransform(position, rotation)); + } + /** * Does an in-place rigid transformation of a Hand. * @@ -79,6 +90,17 @@ namespace Leap return new Hand().CopyFrom(hand).Transform(transform); } + /** + * Returns a new hand that is a copy of a hand, with an additional rigid + * transformation applied to it. + * + * @param transform The transformation to be applied to the copied hand. + */ + public static Hand TransformedCopy(this Hand hand, Vector3 position, Quaternion rotation) + { + return new Hand().CopyFrom(hand).Transform(new LeapTransform(position, rotation)); + } + /** * Does an in-place rigid transformation of a Finger. *