Unity中使用XR Input获取XR设备中手柄的姿态信息

在上一篇博客Unity在XR设备中获取手柄的按键信息_XR风云-CSDN博客我们知道怎样获取手柄的按键信息,那怎样获取得到手柄的姿态信息呢?

也是很简单的了,Unity的XR Input已经帮我们对接上Oculus平台,能够获取手柄姿态信息。

1、根据输入设备的类型获取设备,设备类型包括如下,我们今天是获取手柄信息,主要用到Left或者Right。

    //
    // 摘要:
    //     A set of bit flags describing XR.InputDevice characteristics.
    [Flags]
    public enum InputDeviceCharacteristics : uint
    {
        //
        // 摘要:
        //     A default value specifying no flags.
        None = 0,
        //
        // 摘要:
        //     The InputDevice is attached to the head.
        HeadMounted = 1,
        //
        // 摘要:
        //     The InputDevice has a camera and associated camera tracking information.
        Camera = 2,
        //
        // 摘要:
        //     The InputDevice is held in the user's hand. Typically, a tracked controller.
        HeldInHand = 4,
        //
        // 摘要:
        //     The InputDevice provides hand tracking information via a Hand input feature.
        HandTracking = 8,
        //
        // 摘要:
        //     The InputDevice provides eye tracking information via an Eyes input feature.
        EyeTracking = 16,
        //
        // 摘要:
        //     The InputDevice provides 3DOF or 6DOF tracking data.
        TrackedDevice = 32,
        //
        // 摘要:
        //     The InputDevice is a game controller.
        Controller = 64,
        //
        // 摘要:
        //     The InputDevice is an unmoving reference object used to locate and track other
        //     objects in the world.
        TrackingReference = 128,
        //
        // 摘要:
        //     The InputDevice is associated with the left side of the user.
        Left = 256,
        //
        // 摘要:
        //     The InputDevice is associated with the right side of the user.
        Right = 512,
        //
        // 摘要:
        //     The InputDevice reports software approximated, positional data.
        Simulated6DOF = 1024
    }

2、 姿态信息主要使用2个值,CommonUsages.devicePosition, CommonUsages.deviceRotation,来获取得到位置信息以及旋转信息。我们封装了一些借口,获取得到某个手柄的姿态信息。

    public static bool Get(InputDeviceCharacteristics desiredCharacteristics, InputFeatureUsage<Vector3> inputFeatureUsage, out Vector3 value)
    {
        List<InputDevice> allDevices = new List<InputDevice>();
        InputDevices.GetDevicesWithCharacteristics(desiredCharacteristics, allDevices);
        if (allDevices.Count <= 0)
        {
            Debug.LogError($"not has device by {desiredCharacteristics}");
            value = default;
            return false;
        }

        if (allDevices.Count > 1)
        {
            Debug.LogError($"If the number of devices exceeds 1, the first device is selected by default. ");
            value = default;
            return false;
        }

        InputDevice device = allDevices[0];
        if (!device.TryGetFeatureValue(inputFeatureUsage, out value))
        {
            Debug.LogError($"TryGetFeatureValue {inputFeatureUsage} failed!");
            value = default;
            return false;
        }

        return true;
    }

    public static bool Get(InputDeviceCharacteristics desiredCharacteristics, InputFeatureUsage<Quaternion> inputFeatureUsage, out Quaternion value)
    {
        List<InputDevice> allDevices = new List<InputDevice>();
        InputDevices.GetDevicesWithCharacteristics(desiredCharacteristics, allDevices);
        if (allDevices.Count <= 0)
        {
            Debug.LogError($"not has device by {desiredCharacteristics}");
            value = default;
            return false;
        }

        if (allDevices.Count > 1)
        {
            Debug.LogError($"If the number of devices exceeds 1, the first device is selected by default. ");
            value = default;
            return false;
        }

        InputDevice device = allDevices[0];
        if (!device.TryGetFeatureValue(inputFeatureUsage, out value))
        {
            Debug.LogError($"TryGetFeatureValue {inputFeatureUsage} failed!");
            value = default;
            return false;
        }

        return true;
    }

3、获取右手柄的姿态信息调用如下,获取其他信息也是一样的方法。

XRMeetingInput.Get(InputDeviceCharacteristics.Right, CommonUsages.devicePosition, out Vector3 postion);
XRMeetingInput.Get(InputDeviceCharacteristics.Right, CommonUsages.deviceRotation, out Quaternion rotation);

4、获取得到的信息,我开始以为获取得到是手柄的世界坐标,但是我怎么调试都不正确。经过查询资料才得知,这个函数返回的姿态信息都是本地姿态localPosition,localRotation,不是世界坐标。

所以需要把他们转换成世界坐标,比如在Oculus中需要知道playspace的Transform,然后进行转换成世界坐标,如下转换:

Vector3 position = MixedRealityPlayspace.Transform.TransformPoint(localPosition);
Quaternion rotation = MixedRealityPlayspace.Rotation * localRotation;

这样就可以获取得到手柄的世界坐标。

参考文献:

Unity - Manual: Unity XR Input

Unity - Scripting API: CommonUsages

Unity - Scripting API: XR.InputDevices.GetDevicesWithCharacteristics

猜你喜欢

转载自blog.csdn.net/grace_yi/article/details/123268560