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

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
【资源说明】 基于mediapipe在unity实现姿态追踪python源码+使用说明.zip 基于mediapipe在unity实现姿态追踪python源码+使用说明.zip基于mediapipe在unity实现姿态追踪python源码+使用说明.zip基于mediapipe在unity实现姿态追踪python源码+使用说明.zip基于mediapipe在unity实现姿态追踪python源码+使用说明.zip基于mediapipe在unity实现姿态追踪python源码+使用说明.zip 基于mediapipe在unity实现姿态追踪python源码+使用说明.zip # 第三方库要求 mediapipe # 使用方法 ## 1.首先运行udptracker.py 可以根据需要更改ip地址和端口 ## 2.然后使用unity2021.3.13f1c1打开Track副本 点击运行就可以实现追踪 ![image](blob/master/psc.png) # 一些碎碎念 ## 写了一个unity.py可以生成一个视频的追踪点的txt文件,这个文件可以用到unity实现追踪 ## 使用了udp来实现数据的传输,效果还可以 ## 只是在unity实现了点对点的复现,并不可以使用到通用模型上 【备注】 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载使用,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可直接用于毕设、课设、作业等。 欢迎下载,沟通交流,互相学习,共同进步!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

XR风云

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值