private static void UpdateHandJoints(InputDeviceCharacteristics flag, Hand hand)
        {
            List <InputDevice> inputDeviceList = new List <InputDevice>();

            InputDevices.GetDevicesWithCharacteristics(InputDeviceCharacteristics.HandTracking | flag, inputDeviceList);
            UnityEngine.XR.Hand xrHand = default;
            foreach (InputDevice device in inputDeviceList)
            {
                if (device.TryGetFeatureValue(CommonUsages.isTracked, out bool isTracked) &&
                    isTracked &&
                    device.TryGetFeatureValue(CommonUsages.handData, out xrHand))
                {
                    break;
                }
            }

            if (xrHand != default)
            {
                hand?.UpdateHandJoints(xrHand);
            }
            else
            {
                // If we get here, we didn't successfully update hand joints for any tracked input device
                hand?.DisableHandJoints();
            }
        }
Exemplo n.º 2
0
        public void FilledWithUnityXRHand(ref UnityEngine.XR.Hand hand)
        {
            var fingerBones = new List <UnityEngine.XR.Bone>();

            for (int i = 0; i < 5; i++)
            {
                fingerBones.Clear();
                hand.TryGetFingerBones((HandFinger)i, fingerBones);
                for (int j = 0; j < fingerBones.Count; j++)
                {
                    var fingerBone = fingerBones[j];
                    var bone       = bones[i * 3 + j];

                    Vector3 position = bone.position;
                    fingerBone.TryGetPosition(out position);
                    bone.position = position;

                    Quaternion rotation = bone.rotation;
                    fingerBone.TryGetRotation(out rotation);
                    bone.rotation = rotation;
                }
            }
        }
            /// <summary>
            /// Update this hand's internal state with the Unity XR InputDevice's hand data.
            /// </summary>
            /// <param name="device">The InputDevice to get the CommonUsages.handData feature value from.</param>
            public void UpdateHandJoints(UnityEngine.XR.Hand hand)
            {
                // If the hand was previously disabled, this is the first new update and it should be re-enabled
                if (!handRoot.activeSelf)
                {
                    handRoot.SetActive(true);
                }

                if (hand.TryGetRootBone(out Bone palm))
                {
                    if (palmGameObject == null)
                    {
                        if (InstantiateJointPrefab(out palmGameObject))
                        {
                            ColorJointObject(palmGameObject, null, null);
                        }
                    }

                    bool positionAvailable = palm.TryGetPosition(out Vector3 position);
                    bool rotationAvailable = palm.TryGetRotation(out Quaternion rotation);

                    if (positionAvailable || rotationAvailable)
                    {
                        palmGameObject.transform.SetPositionAndRotation(position, rotation);
                    }
                }

                foreach (HandFinger finger in HandFingers)
                {
                    if (hand.TryGetFingerBones(finger, fingerBones))
                    {
                        if (!handFingerGameObjects.ContainsKey(finger))
                        {
                            GameObject[] jointArray = new GameObject[fingerBones.Count];
                            for (int i = 0; i < fingerBones.Count; i++)
                            {
                                if (InstantiateJointPrefab(out GameObject jointObject))
                                {
                                    ColorJointObject(jointObject, finger, i);
                                }
                                jointArray[i] = jointObject;
                            }
                            handFingerGameObjects[finger] = jointArray;
                        }

                        GameObject[] fingerJointGameObjects = handFingerGameObjects[finger];

                        for (int i = 0; i < fingerBones.Count; i++)
                        {
                            Bone bone = fingerBones[i];

                            bool positionAvailable = bone.TryGetPosition(out Vector3 position);
                            bool rotationAvailable = bone.TryGetRotation(out Quaternion rotation);

                            if (positionAvailable || rotationAvailable)
                            {
                                fingerJointGameObjects[i].transform.SetPositionAndRotation(position, rotation);
                            }
                        }
                    }
                }
            }
 public static UnityEngine.XR.Hand GetDeviceFeatureValueOrDefault(InputDevice device, InputFeatureUsage <UnityEngine.XR.Hand> feature, UnityEngine.XR.Hand defaultValue = default(UnityEngine.XR.Hand))
 {
     UnityEngine.XR.Hand value; if (device.TryGetFeatureValue(feature, out value))
     {
         return(value);
     }
     LogWarningFeatureNotFound(device, feature);
     return(defaultValue);
 }