コード例 #1
0
        /// <summary>
        /// The OpenXR plug-in uses extensions to expose all possible data, which might be surfaced through multiple input devices.
        /// This method is overridden to account for multiple input devices and reuse MRTK controllers if a match is found.
        /// </summary>
        protected override GenericXRSDKController GetOrAddController(InputDevice inputDevice)
        {
            using (GetOrAddControllerPerfMarker.Auto())
            {
                // If this is a new input device, search if an existing input device has matching characteristics
                if (!ActiveControllers.ContainsKey(inputDevice))
                {
                    foreach (InputDevice device in ActiveControllers.Keys)
                    {
                        if (((device.characteristics.HasFlag(InputDeviceCharacteristics.Controller) && inputDevice.characteristics.HasFlag(InputDeviceCharacteristics.Controller)) ||
                             (device.characteristics.HasFlag(InputDeviceCharacteristics.HandTracking) && inputDevice.characteristics.HasFlag(InputDeviceCharacteristics.HandTracking))) &&
                            ((device.characteristics.HasFlag(InputDeviceCharacteristics.Left) && inputDevice.characteristics.HasFlag(InputDeviceCharacteristics.Left)) ||
                             (device.characteristics.HasFlag(InputDeviceCharacteristics.Right) && inputDevice.characteristics.HasFlag(InputDeviceCharacteristics.Right))))
                        {
                            ActiveControllers.Add(inputDevice, ActiveControllers[device]);
                            break;
                        }
                    }
                }

                if (inputDevice.characteristics.HasFlag(InputDeviceCharacteristics.HandTracking) &&
                    inputDevice.TryGetFeatureValue(CommonUsages.isTracked, out bool isTracked) &&
                    !isTracked)
                {
                    // If this is an input device from the Microsoft Hand Interaction profile, it doesn't go invalid but instead goes untracked. Ignore it if untracked.
                    return(null);
                }

                return(base.GetOrAddController(inputDevice));
            }
        }
コード例 #2
0
        /// <inheritdoc />
        protected override GenericUnityController GetOrAddController(string joystickName)
        {
            // If a device is already registered with the ID provided, just return it.
            if (ActiveControllers.ContainsKey(joystickName))
            {
                var controller = ActiveControllers[joystickName];
                Debug.Assert(controller != null);
                return(controller);
            }

            Handedness controllingHand;

            if (joystickName.Contains("Left"))
            {
                controllingHand = Handedness.Left;
            }
            else if (joystickName.Contains("Right"))
            {
                controllingHand = Handedness.Right;
            }
            else
            {
                controllingHand = Handedness.None;
            }

            var  currentControllerType = GetCurrentControllerType(joystickName);
            Type controllerType;

            switch (currentControllerType)
            {
            case SupportedControllerType.GenericOpenVR:
                controllerType = typeof(GenericOpenVRController);
                break;

            case SupportedControllerType.ViveWand:
                controllerType = typeof(ViveWandController);
                break;

            case SupportedControllerType.ViveKnuckles:
                controllerType = typeof(ViveKnucklesController);
                break;

            case SupportedControllerType.OculusTouch:
                controllerType = typeof(OculusTouchController);
                break;

            case SupportedControllerType.OculusRemote:
                controllerType = typeof(OculusRemoteController);
                break;

            case SupportedControllerType.WindowsMixedReality:
                controllerType = typeof(WindowsMixedRealityOpenVRMotionController);
                break;

            default:
                return(null);
            }

            var pointers           = RequestPointers(controllerType, controllingHand);
            var inputSource        = InputSystem?.RequestNewGenericInputSource($"{currentControllerType} Controller {controllingHand}", pointers);
            var detectedController = Activator.CreateInstance(controllerType, TrackingState.NotTracked, controllingHand, inputSource, null) as GenericOpenVRController;

            if (detectedController == null)
            {
                Debug.LogError($"Failed to create {controllerType.Name} controller");
                return(null);
            }

            if (!detectedController.SetupConfiguration(controllerType))
            {
                // Controller failed to be setup correctly.
                Debug.LogError($"Failed to Setup {controllerType.Name} controller");
                // Return null so we don't raise the source detected.
                return(null);
            }

            for (int i = 0; i < detectedController.InputSource?.Pointers?.Length; i++)
            {
                detectedController.InputSource.Pointers[i].Controller = detectedController;
            }

            ActiveControllers.Add(joystickName, detectedController);
            return(detectedController);
        }